GNU Linux-libre 4.14.290-gnu1
[releases.git] / fs / overlayfs / copy_up.c
1 /*
2  *
3  * Copyright (C) 2011 Novell Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
9
10 #include <linux/module.h>
11 #include <linux/fs.h>
12 #include <linux/slab.h>
13 #include <linux/file.h>
14 #include <linux/splice.h>
15 #include <linux/xattr.h>
16 #include <linux/security.h>
17 #include <linux/uaccess.h>
18 #include <linux/sched/signal.h>
19 #include <linux/cred.h>
20 #include <linux/namei.h>
21 #include <linux/fdtable.h>
22 #include <linux/ratelimit.h>
23 #include <linux/exportfs.h>
24 #include "overlayfs.h"
25 #include "ovl_entry.h"
26
27 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
28
29 static bool __read_mostly ovl_check_copy_up;
30 module_param_named(check_copy_up, ovl_check_copy_up, bool,
31                    S_IWUSR | S_IRUGO);
32 MODULE_PARM_DESC(ovl_check_copy_up,
33                  "Warn on copy-up when causing process also has a R/O fd open");
34
35 static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
36 {
37         const struct dentry *dentry = data;
38
39         if (file_inode(f) == d_inode(dentry))
40                 pr_warn_ratelimited("overlayfs: Warning: Copying up %pD, but open R/O on fd %u which will cease to be coherent [pid=%d %s]\n",
41                                     f, fd, current->pid, current->comm);
42         return 0;
43 }
44
45 /*
46  * Check the fds open by this process and warn if something like the following
47  * scenario is about to occur:
48  *
49  *      fd1 = open("foo", O_RDONLY);
50  *      fd2 = open("foo", O_RDWR);
51  */
52 static void ovl_do_check_copy_up(struct dentry *dentry)
53 {
54         if (ovl_check_copy_up)
55                 iterate_fd(current->files, 0, ovl_check_fd, dentry);
56 }
57
58 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
59 {
60         ssize_t list_size, size, value_size = 0;
61         char *buf, *name, *value = NULL;
62         int error = 0;
63         size_t slen;
64
65         if (!(old->d_inode->i_opflags & IOP_XATTR) ||
66             !(new->d_inode->i_opflags & IOP_XATTR))
67                 return 0;
68
69         list_size = vfs_listxattr(old, NULL, 0);
70         if (list_size <= 0) {
71                 if (list_size == -EOPNOTSUPP)
72                         return 0;
73                 return list_size;
74         }
75
76         buf = kzalloc(list_size, GFP_KERNEL);
77         if (!buf)
78                 return -ENOMEM;
79
80         list_size = vfs_listxattr(old, buf, list_size);
81         if (list_size <= 0) {
82                 error = list_size;
83                 goto out;
84         }
85
86         for (name = buf; list_size; name += slen) {
87                 slen = strnlen(name, list_size) + 1;
88
89                 /* underlying fs providing us with an broken xattr list? */
90                 if (WARN_ON(slen > list_size)) {
91                         error = -EIO;
92                         break;
93                 }
94                 list_size -= slen;
95
96                 if (ovl_is_private_xattr(name))
97                         continue;
98
99                 error = security_inode_copy_up_xattr(name);
100                 if (error < 0 && error != -EOPNOTSUPP)
101                         break;
102                 if (error == 1) {
103                         error = 0;
104                         continue; /* Discard */
105                 }
106 retry:
107                 size = vfs_getxattr(old, name, value, value_size);
108                 if (size == -ERANGE)
109                         size = vfs_getxattr(old, name, NULL, 0);
110
111                 if (size < 0) {
112                         error = size;
113                         break;
114                 }
115
116                 if (size > value_size) {
117                         void *new;
118
119                         new = krealloc(value, size, GFP_KERNEL);
120                         if (!new) {
121                                 error = -ENOMEM;
122                                 break;
123                         }
124                         value = new;
125                         value_size = size;
126                         goto retry;
127                 }
128
129                 error = vfs_setxattr(new, name, value, size, 0);
130                 if (error)
131                         break;
132         }
133         kfree(value);
134 out:
135         kfree(buf);
136         return error;
137 }
138
139 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
140 {
141         struct file *old_file;
142         struct file *new_file;
143         loff_t old_pos = 0;
144         loff_t new_pos = 0;
145         int error = 0;
146
147         if (len == 0)
148                 return 0;
149
150         old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
151         if (IS_ERR(old_file))
152                 return PTR_ERR(old_file);
153
154         new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
155         if (IS_ERR(new_file)) {
156                 error = PTR_ERR(new_file);
157                 goto out_fput;
158         }
159
160         /* Try to use clone_file_range to clone up within the same fs */
161         error = do_clone_file_range(old_file, 0, new_file, 0, len);
162         if (!error)
163                 goto out;
164         /* Couldn't clone, so now we try to copy the data */
165         error = 0;
166
167         /* FIXME: copy up sparse files efficiently */
168         while (len) {
169                 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
170                 long bytes;
171
172                 if (len < this_len)
173                         this_len = len;
174
175                 if (signal_pending_state(TASK_KILLABLE, current)) {
176                         error = -EINTR;
177                         break;
178                 }
179
180                 bytes = do_splice_direct(old_file, &old_pos,
181                                          new_file, &new_pos,
182                                          this_len, SPLICE_F_MOVE);
183                 if (bytes <= 0) {
184                         error = bytes;
185                         break;
186                 }
187                 WARN_ON(old_pos != new_pos);
188
189                 len -= bytes;
190         }
191 out:
192         if (!error)
193                 error = vfs_fsync(new_file, 0);
194         fput(new_file);
195 out_fput:
196         fput(old_file);
197         return error;
198 }
199
200 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
201 {
202         struct iattr attr = {
203                 .ia_valid =
204                      ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
205                 .ia_atime = stat->atime,
206                 .ia_mtime = stat->mtime,
207         };
208
209         return notify_change(upperdentry, &attr, NULL);
210 }
211
212 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
213 {
214         int err = 0;
215
216         if (!S_ISLNK(stat->mode)) {
217                 struct iattr attr = {
218                         .ia_valid = ATTR_MODE,
219                         .ia_mode = stat->mode,
220                 };
221                 err = notify_change(upperdentry, &attr, NULL);
222         }
223         if (!err) {
224                 struct iattr attr = {
225                         .ia_valid = ATTR_UID | ATTR_GID,
226                         .ia_uid = stat->uid,
227                         .ia_gid = stat->gid,
228                 };
229                 err = notify_change(upperdentry, &attr, NULL);
230         }
231         if (!err)
232                 ovl_set_timestamps(upperdentry, stat);
233
234         return err;
235 }
236
237 struct ovl_fh *ovl_encode_fh(struct dentry *lower, bool is_upper)
238 {
239         struct ovl_fh *fh;
240         int fh_type, fh_len, dwords;
241         void *buf;
242         int buflen = MAX_HANDLE_SZ;
243         uuid_t *uuid = &lower->d_sb->s_uuid;
244
245         buf = kmalloc(buflen, GFP_KERNEL);
246         if (!buf)
247                 return ERR_PTR(-ENOMEM);
248
249         /*
250          * We encode a non-connectable file handle for non-dir, because we
251          * only need to find the lower inode number and we don't want to pay
252          * the price or reconnecting the dentry.
253          */
254         dwords = buflen >> 2;
255         fh_type = exportfs_encode_fh(lower, buf, &dwords, 0);
256         buflen = (dwords << 2);
257
258         fh = ERR_PTR(-EIO);
259         if (WARN_ON(fh_type < 0) ||
260             WARN_ON(buflen > MAX_HANDLE_SZ) ||
261             WARN_ON(fh_type == FILEID_INVALID))
262                 goto out;
263
264         BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255);
265         fh_len = offsetof(struct ovl_fh, fid) + buflen;
266         fh = kmalloc(fh_len, GFP_KERNEL);
267         if (!fh) {
268                 fh = ERR_PTR(-ENOMEM);
269                 goto out;
270         }
271
272         fh->version = OVL_FH_VERSION;
273         fh->magic = OVL_FH_MAGIC;
274         fh->type = fh_type;
275         fh->flags = OVL_FH_FLAG_CPU_ENDIAN;
276         /*
277          * When we will want to decode an overlay dentry from this handle
278          * and all layers are on the same fs, if we get a disconncted real
279          * dentry when we decode fid, the only way to tell if we should assign
280          * it to upperdentry or to lowerstack is by checking this flag.
281          */
282         if (is_upper)
283                 fh->flags |= OVL_FH_FLAG_PATH_UPPER;
284         fh->len = fh_len;
285         fh->uuid = *uuid;
286         memcpy(fh->fid, buf, buflen);
287
288 out:
289         kfree(buf);
290         return fh;
291 }
292
293 static int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
294                           struct dentry *upper)
295 {
296         const struct ovl_fh *fh = NULL;
297         int err;
298
299         /*
300          * When lower layer doesn't support export operations store a 'null' fh,
301          * so we can use the overlay.origin xattr to distignuish between a copy
302          * up and a pure upper inode.
303          */
304         if (ovl_can_decode_fh(lower->d_sb)) {
305                 fh = ovl_encode_fh(lower, false);
306                 if (IS_ERR(fh))
307                         return PTR_ERR(fh);
308         }
309
310         /*
311          * Do not fail when upper doesn't support xattrs.
312          */
313         err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh,
314                                  fh ? fh->len : 0, 0);
315         kfree(fh);
316
317         return err;
318 }
319
320 struct ovl_copy_up_ctx {
321         struct dentry *parent;
322         struct dentry *dentry;
323         struct path lowerpath;
324         struct kstat stat;
325         struct kstat pstat;
326         const char *link;
327         struct dentry *destdir;
328         struct qstr destname;
329         struct dentry *workdir;
330         bool tmpfile;
331         bool origin;
332 };
333
334 static int ovl_link_up(struct ovl_copy_up_ctx *c)
335 {
336         int err;
337         struct dentry *upper;
338         struct dentry *upperdir = ovl_dentry_upper(c->parent);
339         struct inode *udir = d_inode(upperdir);
340
341         /* Mark parent "impure" because it may now contain non-pure upper */
342         err = ovl_set_impure(c->parent, upperdir);
343         if (err)
344                 return err;
345
346         err = ovl_set_nlink_lower(c->dentry);
347         if (err)
348                 return err;
349
350         inode_lock_nested(udir, I_MUTEX_PARENT);
351         upper = lookup_one_len(c->dentry->d_name.name, upperdir,
352                                c->dentry->d_name.len);
353         err = PTR_ERR(upper);
354         if (!IS_ERR(upper)) {
355                 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper,
356                                   true);
357                 dput(upper);
358
359                 if (!err) {
360                         /* Restore timestamps on parent (best effort) */
361                         ovl_set_timestamps(upperdir, &c->pstat);
362                         ovl_dentry_set_upper_alias(c->dentry);
363                 }
364         }
365         inode_unlock(udir);
366         ovl_set_nlink_upper(c->dentry);
367
368         return err;
369 }
370
371 static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
372                             struct dentry **newdentry)
373 {
374         int err;
375         struct dentry *upper;
376         struct inode *udir = d_inode(c->destdir);
377
378         upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
379         if (IS_ERR(upper))
380                 return PTR_ERR(upper);
381
382         if (c->tmpfile)
383                 err = ovl_do_link(temp, udir, upper, true);
384         else
385                 err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0);
386
387         if (!err)
388                 *newdentry = dget(c->tmpfile ? upper : temp);
389         dput(upper);
390
391         return err;
392 }
393
394 static int ovl_get_tmpfile(struct ovl_copy_up_ctx *c, struct dentry **tempp)
395 {
396         int err;
397         struct dentry *temp;
398         const struct cred *old_creds = NULL;
399         struct cred *new_creds = NULL;
400         struct cattr cattr = {
401                 /* Can't properly set mode on creation because of the umask */
402                 .mode = c->stat.mode & S_IFMT,
403                 .rdev = c->stat.rdev,
404                 .link = c->link
405         };
406
407         err = security_inode_copy_up(c->dentry, &new_creds);
408         if (err < 0)
409                 goto out;
410
411         if (new_creds)
412                 old_creds = override_creds(new_creds);
413
414         if (c->tmpfile) {
415                 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
416                 if (IS_ERR(temp))
417                         goto temp_err;
418         } else {
419                 temp = ovl_lookup_temp(c->workdir);
420                 if (IS_ERR(temp))
421                         goto temp_err;
422
423                 err = ovl_create_real(d_inode(c->workdir), temp, &cattr,
424                                       NULL, true);
425                 if (err) {
426                         dput(temp);
427                         goto out;
428                 }
429         }
430         err = 0;
431         *tempp = temp;
432 out:
433         if (new_creds) {
434                 revert_creds(old_creds);
435                 put_cred(new_creds);
436         }
437
438         return err;
439
440 temp_err:
441         err = PTR_ERR(temp);
442         goto out;
443 }
444
445 static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
446 {
447         int err;
448
449         if (S_ISREG(c->stat.mode)) {
450                 struct path upperpath;
451
452                 ovl_path_upper(c->dentry, &upperpath);
453                 BUG_ON(upperpath.dentry != NULL);
454                 upperpath.dentry = temp;
455
456                 err = ovl_copy_up_data(&c->lowerpath, &upperpath, c->stat.size);
457                 if (err)
458                         return err;
459         }
460
461         err = ovl_copy_xattr(c->lowerpath.dentry, temp);
462         if (err)
463                 return err;
464
465         inode_lock(temp->d_inode);
466         err = ovl_set_attr(temp, &c->stat);
467         inode_unlock(temp->d_inode);
468         if (err)
469                 return err;
470
471         /*
472          * Store identifier of lower inode in upper inode xattr to
473          * allow lookup of the copy up origin inode.
474          *
475          * Don't set origin when we are breaking the association with a lower
476          * hard link.
477          */
478         if (c->origin) {
479                 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
480                 if (err)
481                         return err;
482         }
483
484         return 0;
485 }
486
487 static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
488 {
489         struct inode *udir = c->destdir->d_inode;
490         struct dentry *newdentry = NULL;
491         struct dentry *temp = NULL;
492         int err;
493
494         err = ovl_get_tmpfile(c, &temp);
495         if (err)
496                 goto out;
497
498         err = ovl_copy_up_inode(c, temp);
499         if (err)
500                 goto out_cleanup;
501
502         if (c->tmpfile) {
503                 inode_lock_nested(udir, I_MUTEX_PARENT);
504                 err = ovl_install_temp(c, temp, &newdentry);
505                 inode_unlock(udir);
506         } else {
507                 err = ovl_install_temp(c, temp, &newdentry);
508         }
509         if (err)
510                 goto out_cleanup;
511
512         ovl_inode_update(d_inode(c->dentry), newdentry);
513 out:
514         dput(temp);
515         return err;
516
517 out_cleanup:
518         if (!c->tmpfile)
519                 ovl_cleanup(d_inode(c->workdir), temp);
520         goto out;
521 }
522
523 /*
524  * Copy up a single dentry
525  *
526  * All renames start with copy up of source if necessary.  The actual
527  * rename will only proceed once the copy up was successful.  Copy up uses
528  * upper parent i_mutex for exclusion.  Since rename can change d_parent it
529  * is possible that the copy up will lock the old parent.  At that point
530  * the file will have already been copied up anyway.
531  */
532 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
533 {
534         int err;
535         struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
536         bool indexed = false;
537
538         if (ovl_indexdir(c->dentry->d_sb) && !S_ISDIR(c->stat.mode) &&
539             c->stat.nlink > 1)
540                 indexed = true;
541
542         if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || indexed)
543                 c->origin = true;
544
545         if (indexed) {
546                 c->destdir = ovl_indexdir(c->dentry->d_sb);
547                 err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
548                 if (err)
549                         return err;
550         } else {
551                 /*
552                  * Mark parent "impure" because it may now contain non-pure
553                  * upper
554                  */
555                 err = ovl_set_impure(c->parent, c->destdir);
556                 if (err)
557                         return err;
558         }
559
560         /* Should we copyup with O_TMPFILE or with workdir? */
561         if (S_ISREG(c->stat.mode) && ofs->tmpfile) {
562                 c->tmpfile = true;
563                 err = ovl_copy_up_locked(c);
564         } else {
565                 err = ovl_lock_rename_workdir(c->workdir, c->destdir);
566                 if (!err) {
567                         err = ovl_copy_up_locked(c);
568                         unlock_rename(c->workdir, c->destdir);
569                 }
570         }
571
572         if (indexed) {
573                 if (!err)
574                         ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
575                 kfree(c->destname.name);
576         } else if (!err) {
577                 struct inode *udir = d_inode(c->destdir);
578
579                 /* Restore timestamps on parent (best effort) */
580                 inode_lock(udir);
581                 ovl_set_timestamps(c->destdir, &c->pstat);
582                 inode_unlock(udir);
583
584                 ovl_dentry_set_upper_alias(c->dentry);
585         }
586
587         return err;
588 }
589
590 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
591                            int flags)
592 {
593         int err;
594         DEFINE_DELAYED_CALL(done);
595         struct path parentpath;
596         struct ovl_copy_up_ctx ctx = {
597                 .parent = parent,
598                 .dentry = dentry,
599                 .workdir = ovl_workdir(dentry),
600         };
601
602         if (WARN_ON(!ctx.workdir))
603                 return -EROFS;
604
605         ovl_path_lower(dentry, &ctx.lowerpath);
606         err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
607                           STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
608         if (err)
609                 return err;
610
611         ovl_path_upper(parent, &parentpath);
612         ctx.destdir = parentpath.dentry;
613         ctx.destname = dentry->d_name;
614
615         err = vfs_getattr(&parentpath, &ctx.pstat,
616                           STATX_ATIME | STATX_MTIME, AT_STATX_SYNC_AS_STAT);
617         if (err)
618                 return err;
619
620         /* maybe truncate regular file. this has no effect on dirs */
621         if (flags & O_TRUNC)
622                 ctx.stat.size = 0;
623
624         if (S_ISLNK(ctx.stat.mode)) {
625                 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
626                 if (IS_ERR(ctx.link))
627                         return PTR_ERR(ctx.link);
628         }
629         ovl_do_check_copy_up(ctx.lowerpath.dentry);
630
631         err = ovl_copy_up_start(dentry);
632         /* err < 0: interrupted, err > 0: raced with another copy-up */
633         if (unlikely(err)) {
634                 if (err > 0)
635                         err = 0;
636         } else {
637                 if (!ovl_dentry_upper(dentry))
638                         err = ovl_do_copy_up(&ctx);
639                 if (!err && !ovl_dentry_has_upper_alias(dentry))
640                         err = ovl_link_up(&ctx);
641                 ovl_copy_up_end(dentry);
642         }
643         do_delayed_call(&done);
644
645         return err;
646 }
647
648 int ovl_copy_up_flags(struct dentry *dentry, int flags)
649 {
650         int err = 0;
651         const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
652
653         while (!err) {
654                 struct dentry *next;
655                 struct dentry *parent;
656
657                 /*
658                  * Check if copy-up has happened as well as for upper alias (in
659                  * case of hard links) is there.
660                  *
661                  * Both checks are lockless:
662                  *  - false negatives: will recheck under oi->lock
663                  *  - false positives:
664                  *    + ovl_dentry_upper() uses memory barriers to ensure the
665                  *      upper dentry is up-to-date
666                  *    + ovl_dentry_has_upper_alias() relies on locking of
667                  *      upper parent i_rwsem to prevent reordering copy-up
668                  *      with rename.
669                  */
670                 if (ovl_dentry_upper(dentry) &&
671                     ovl_dentry_has_upper_alias(dentry))
672                         break;
673
674                 next = dget(dentry);
675                 /* find the topmost dentry not yet copied up */
676                 for (;;) {
677                         parent = dget_parent(next);
678
679                         if (ovl_dentry_upper(parent))
680                                 break;
681
682                         dput(next);
683                         next = parent;
684                 }
685
686                 err = ovl_copy_up_one(parent, next, flags);
687
688                 dput(parent);
689                 dput(next);
690         }
691         revert_creds(old_cred);
692
693         return err;
694 }
695
696 int ovl_copy_up(struct dentry *dentry)
697 {
698         return ovl_copy_up_flags(dentry, 0);
699 }