]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/overlayfs/copy_up.c
ovl: fix missing unlock_rename() in ovl_do_copy_up()
[mirror_ubuntu-artful-kernel.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 uninitialized_var(error);
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 retry:
99 size = vfs_getxattr(old, name, value, value_size);
100 if (size == -ERANGE)
101 size = vfs_getxattr(old, name, NULL, 0);
102
103 if (size < 0) {
104 error = size;
105 break;
106 }
107
108 if (size > value_size) {
109 void *new;
110
111 new = krealloc(value, size, GFP_KERNEL);
112 if (!new) {
113 error = -ENOMEM;
114 break;
115 }
116 value = new;
117 value_size = size;
118 goto retry;
119 }
120
121 error = security_inode_copy_up_xattr(name);
122 if (error < 0 && error != -EOPNOTSUPP)
123 break;
124 if (error == 1) {
125 error = 0;
126 continue; /* Discard */
127 }
128 error = vfs_setxattr(new, name, value, size, 0);
129 if (error)
130 break;
131 }
132 kfree(value);
133 out:
134 kfree(buf);
135 return error;
136 }
137
138 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
139 {
140 struct file *old_file;
141 struct file *new_file;
142 loff_t old_pos = 0;
143 loff_t new_pos = 0;
144 int error = 0;
145
146 if (len == 0)
147 return 0;
148
149 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
150 if (IS_ERR(old_file))
151 return PTR_ERR(old_file);
152
153 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
154 if (IS_ERR(new_file)) {
155 error = PTR_ERR(new_file);
156 goto out_fput;
157 }
158
159 /* Try to use clone_file_range to clone up within the same fs */
160 error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
161 if (!error)
162 goto out;
163 /* Couldn't clone, so now we try to copy the data */
164 error = 0;
165
166 /* FIXME: copy up sparse files efficiently */
167 while (len) {
168 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
169 long bytes;
170
171 if (len < this_len)
172 this_len = len;
173
174 if (signal_pending_state(TASK_KILLABLE, current)) {
175 error = -EINTR;
176 break;
177 }
178
179 bytes = do_splice_direct(old_file, &old_pos,
180 new_file, &new_pos,
181 this_len, SPLICE_F_MOVE);
182 if (bytes <= 0) {
183 error = bytes;
184 break;
185 }
186 WARN_ON(old_pos != new_pos);
187
188 len -= bytes;
189 }
190 out:
191 if (!error)
192 error = vfs_fsync(new_file, 0);
193 fput(new_file);
194 out_fput:
195 fput(old_file);
196 return error;
197 }
198
199 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
200 {
201 struct iattr attr = {
202 .ia_valid =
203 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
204 .ia_atime = stat->atime,
205 .ia_mtime = stat->mtime,
206 };
207
208 return notify_change(upperdentry, &attr, NULL);
209 }
210
211 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
212 {
213 int err = 0;
214
215 /*
216 * For the most part we want to set the mode bits before setting
217 * the user, otherwise the current context might lack permission
218 * for setting the mode. However for sxid/sticky bits we want
219 * the operation to fail if the current user isn't privileged
220 * towards the resulting inode. So we first set the mode but
221 * exclude the sxid/sticky bits, then set the user, then set the
222 * mode again if any of the sxid/sticky bits are set.
223 */
224 if (!S_ISLNK(stat->mode)) {
225 struct iattr attr = {
226 .ia_valid = ATTR_MODE,
227 .ia_mode = stat->mode & ~(S_ISUID|S_ISGID|S_ISVTX),
228 };
229 err = notify_change(upperdentry, &attr, NULL);
230 }
231 if (!err) {
232 struct iattr attr = {
233 .ia_valid = ATTR_UID | ATTR_GID,
234 .ia_uid = stat->uid,
235 .ia_gid = stat->gid,
236 };
237 err = notify_change(upperdentry, &attr, NULL);
238 }
239 if (!err && !S_ISLNK(stat->mode) &&
240 (stat->mode & (S_ISUID|S_ISGID|S_ISVTX))) {
241 struct iattr attr = {
242 .ia_valid = ATTR_MODE,
243 .ia_mode = stat->mode,
244 };
245 err = notify_change(upperdentry, &attr, NULL);
246 }
247 if (!err)
248 ovl_set_timestamps(upperdentry, stat);
249
250 return err;
251 }
252
253 struct ovl_fh *ovl_encode_fh(struct dentry *lower, bool is_upper)
254 {
255 struct ovl_fh *fh;
256 int fh_type, fh_len, dwords;
257 void *buf;
258 int buflen = MAX_HANDLE_SZ;
259 uuid_t *uuid = &lower->d_sb->s_uuid;
260
261 buf = kmalloc(buflen, GFP_TEMPORARY);
262 if (!buf)
263 return ERR_PTR(-ENOMEM);
264
265 /*
266 * We encode a non-connectable file handle for non-dir, because we
267 * only need to find the lower inode number and we don't want to pay
268 * the price or reconnecting the dentry.
269 */
270 dwords = buflen >> 2;
271 fh_type = exportfs_encode_fh(lower, buf, &dwords, 0);
272 buflen = (dwords << 2);
273
274 fh = ERR_PTR(-EIO);
275 if (WARN_ON(fh_type < 0) ||
276 WARN_ON(buflen > MAX_HANDLE_SZ) ||
277 WARN_ON(fh_type == FILEID_INVALID))
278 goto out;
279
280 BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255);
281 fh_len = offsetof(struct ovl_fh, fid) + buflen;
282 fh = kmalloc(fh_len, GFP_KERNEL);
283 if (!fh) {
284 fh = ERR_PTR(-ENOMEM);
285 goto out;
286 }
287
288 fh->version = OVL_FH_VERSION;
289 fh->magic = OVL_FH_MAGIC;
290 fh->type = fh_type;
291 fh->flags = OVL_FH_FLAG_CPU_ENDIAN;
292 /*
293 * When we will want to decode an overlay dentry from this handle
294 * and all layers are on the same fs, if we get a disconncted real
295 * dentry when we decode fid, the only way to tell if we should assign
296 * it to upperdentry or to lowerstack is by checking this flag.
297 */
298 if (is_upper)
299 fh->flags |= OVL_FH_FLAG_PATH_UPPER;
300 fh->len = fh_len;
301 fh->uuid = *uuid;
302 memcpy(fh->fid, buf, buflen);
303
304 out:
305 kfree(buf);
306 return fh;
307 }
308
309 static int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
310 struct dentry *upper)
311 {
312 const struct ovl_fh *fh = NULL;
313 int err;
314
315 /*
316 * When lower layer doesn't support export operations store a 'null' fh,
317 * so we can use the overlay.origin xattr to distignuish between a copy
318 * up and a pure upper inode.
319 */
320 if (ovl_can_decode_fh(lower->d_sb)) {
321 fh = ovl_encode_fh(lower, false);
322 if (IS_ERR(fh))
323 return PTR_ERR(fh);
324 }
325
326 /*
327 * Do not fail when upper doesn't support xattrs.
328 */
329 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh,
330 fh ? fh->len : 0, 0);
331 kfree(fh);
332
333 return err;
334 }
335
336 struct ovl_copy_up_ctx {
337 struct dentry *parent;
338 struct dentry *dentry;
339 struct path lowerpath;
340 struct kstat stat;
341 struct kstat pstat;
342 const char *link;
343 struct dentry *destdir;
344 struct qstr destname;
345 struct dentry *workdir;
346 bool tmpfile;
347 bool origin;
348 };
349
350 static int ovl_link_up(struct ovl_copy_up_ctx *c)
351 {
352 int err;
353 struct dentry *upper;
354 struct dentry *upperdir = ovl_dentry_upper(c->parent);
355 struct inode *udir = d_inode(upperdir);
356
357 /* Mark parent "impure" because it may now contain non-pure upper */
358 err = ovl_set_impure(c->parent, upperdir);
359 if (err)
360 return err;
361
362 err = ovl_set_nlink_lower(c->dentry);
363 if (err)
364 return err;
365
366 inode_lock_nested(udir, I_MUTEX_PARENT);
367 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
368 c->dentry->d_name.len);
369 err = PTR_ERR(upper);
370 if (!IS_ERR(upper)) {
371 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper,
372 true);
373 dput(upper);
374
375 if (!err) {
376 /* Restore timestamps on parent (best effort) */
377 ovl_set_timestamps(upperdir, &c->pstat);
378 ovl_dentry_set_upper_alias(c->dentry);
379 }
380 }
381 inode_unlock(udir);
382 ovl_set_nlink_upper(c->dentry);
383
384 return err;
385 }
386
387 static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
388 struct dentry **newdentry)
389 {
390 int err;
391 struct dentry *upper;
392 struct inode *udir = d_inode(c->destdir);
393
394 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
395 if (IS_ERR(upper))
396 return PTR_ERR(upper);
397
398 if (c->tmpfile)
399 err = ovl_do_link(temp, udir, upper, true);
400 else
401 err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0);
402
403 if (!err)
404 *newdentry = dget(c->tmpfile ? upper : temp);
405 dput(upper);
406
407 return err;
408 }
409
410 static int ovl_get_tmpfile(struct ovl_copy_up_ctx *c, struct dentry **tempp)
411 {
412 int err;
413 struct dentry *temp;
414 const struct cred *old_creds = NULL;
415 struct cred *new_creds = NULL;
416 struct cattr cattr = {
417 /* Can't properly set mode on creation because of the umask */
418 .mode = c->stat.mode & S_IFMT,
419 .rdev = c->stat.rdev,
420 .link = c->link
421 };
422
423 err = security_inode_copy_up(c->dentry, &new_creds);
424 if (err < 0)
425 goto out;
426
427 if (new_creds)
428 old_creds = override_creds(new_creds);
429
430 if (c->tmpfile) {
431 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
432 if (IS_ERR(temp))
433 goto temp_err;
434 } else {
435 temp = ovl_lookup_temp(c->workdir);
436 if (IS_ERR(temp))
437 goto temp_err;
438
439 err = ovl_create_real(d_inode(c->workdir), temp, &cattr,
440 NULL, true);
441 if (err) {
442 dput(temp);
443 goto out;
444 }
445 }
446 err = 0;
447 *tempp = temp;
448 out:
449 if (new_creds) {
450 revert_creds(old_creds);
451 put_cred(new_creds);
452 }
453
454 return err;
455
456 temp_err:
457 err = PTR_ERR(temp);
458 goto out;
459 }
460
461 static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
462 {
463 int err;
464
465 if (S_ISREG(c->stat.mode)) {
466 struct path upperpath;
467
468 ovl_path_upper(c->dentry, &upperpath);
469 BUG_ON(upperpath.dentry != NULL);
470 upperpath.dentry = temp;
471
472 err = ovl_copy_up_data(&c->lowerpath, &upperpath, c->stat.size);
473 if (err)
474 return err;
475 }
476
477 err = ovl_copy_xattr(c->lowerpath.dentry, temp);
478 if (err)
479 return err;
480
481 inode_lock(temp->d_inode);
482 err = ovl_set_attr(temp, &c->stat);
483 inode_unlock(temp->d_inode);
484 if (err)
485 return err;
486
487 /*
488 * Store identifier of lower inode in upper inode xattr to
489 * allow lookup of the copy up origin inode.
490 *
491 * Don't set origin when we are breaking the association with a lower
492 * hard link.
493 */
494 if (c->origin) {
495 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
496 if (err)
497 return err;
498 }
499
500 return 0;
501 }
502
503 static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
504 {
505 struct inode *udir = c->destdir->d_inode;
506 struct dentry *newdentry = NULL;
507 struct dentry *temp = NULL;
508 int err;
509
510 err = ovl_get_tmpfile(c, &temp);
511 if (err)
512 goto out;
513
514 err = ovl_copy_up_inode(c, temp);
515 if (err)
516 goto out_cleanup;
517
518 if (c->tmpfile) {
519 inode_lock_nested(udir, I_MUTEX_PARENT);
520 err = ovl_install_temp(c, temp, &newdentry);
521 inode_unlock(udir);
522 } else {
523 err = ovl_install_temp(c, temp, &newdentry);
524 }
525 if (err)
526 goto out_cleanup;
527
528 ovl_inode_update(d_inode(c->dentry), newdentry);
529 out:
530 dput(temp);
531 return err;
532
533 out_cleanup:
534 if (!c->tmpfile)
535 ovl_cleanup(d_inode(c->workdir), temp);
536 goto out;
537 }
538
539 /*
540 * Copy up a single dentry
541 *
542 * All renames start with copy up of source if necessary. The actual
543 * rename will only proceed once the copy up was successful. Copy up uses
544 * upper parent i_mutex for exclusion. Since rename can change d_parent it
545 * is possible that the copy up will lock the old parent. At that point
546 * the file will have already been copied up anyway.
547 */
548 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
549 {
550 int err;
551 struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
552 bool indexed = false;
553
554 if (ovl_indexdir(c->dentry->d_sb) && !S_ISDIR(c->stat.mode) &&
555 c->stat.nlink > 1)
556 indexed = true;
557
558 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || indexed)
559 c->origin = true;
560
561 if (indexed) {
562 c->destdir = ovl_indexdir(c->dentry->d_sb);
563 err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
564 if (err)
565 return err;
566 } else {
567 /*
568 * Mark parent "impure" because it may now contain non-pure
569 * upper
570 */
571 err = ovl_set_impure(c->parent, c->destdir);
572 if (err)
573 return err;
574 }
575
576 /* Should we copyup with O_TMPFILE or with workdir? */
577 if (S_ISREG(c->stat.mode) && ofs->tmpfile) {
578 c->tmpfile = true;
579 err = ovl_copy_up_locked(c);
580 } else {
581 err = ovl_lock_rename_workdir(c->workdir, c->destdir);
582 if (!err) {
583 err = ovl_copy_up_locked(c);
584 unlock_rename(c->workdir, c->destdir);
585 }
586 }
587
588 if (indexed) {
589 if (!err)
590 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
591 kfree(c->destname.name);
592 } else if (!err) {
593 struct inode *udir = d_inode(c->destdir);
594
595 /* Restore timestamps on parent (best effort) */
596 inode_lock(udir);
597 ovl_set_timestamps(c->destdir, &c->pstat);
598 inode_unlock(udir);
599
600 ovl_dentry_set_upper_alias(c->dentry);
601 }
602
603 return err;
604 }
605
606 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
607 int flags)
608 {
609 int err;
610 DEFINE_DELAYED_CALL(done);
611 struct path parentpath;
612 struct ovl_copy_up_ctx ctx = {
613 .parent = parent,
614 .dentry = dentry,
615 .workdir = ovl_workdir(dentry),
616 };
617
618 if (WARN_ON(!ctx.workdir))
619 return -EROFS;
620
621 ovl_path_lower(dentry, &ctx.lowerpath);
622 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
623 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
624 if (err)
625 return err;
626
627 ovl_path_upper(parent, &parentpath);
628 ctx.destdir = parentpath.dentry;
629 ctx.destname = dentry->d_name;
630
631 err = vfs_getattr(&parentpath, &ctx.pstat,
632 STATX_ATIME | STATX_MTIME, AT_STATX_SYNC_AS_STAT);
633 if (err)
634 return err;
635
636 /* maybe truncate regular file. this has no effect on dirs */
637 if (flags & O_TRUNC)
638 ctx.stat.size = 0;
639
640 if (S_ISLNK(ctx.stat.mode)) {
641 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
642 if (IS_ERR(ctx.link))
643 return PTR_ERR(ctx.link);
644 }
645 ovl_do_check_copy_up(ctx.lowerpath.dentry);
646
647 err = ovl_copy_up_start(dentry);
648 /* err < 0: interrupted, err > 0: raced with another copy-up */
649 if (unlikely(err)) {
650 if (err > 0)
651 err = 0;
652 } else {
653 if (!ovl_dentry_upper(dentry))
654 err = ovl_do_copy_up(&ctx);
655 if (!err && !ovl_dentry_has_upper_alias(dentry))
656 err = ovl_link_up(&ctx);
657 ovl_copy_up_end(dentry);
658 }
659 do_delayed_call(&done);
660
661 return err;
662 }
663
664 int ovl_copy_up_flags(struct dentry *dentry, int flags)
665 {
666 int err = 0;
667 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
668
669 while (!err) {
670 struct dentry *next;
671 struct dentry *parent;
672
673 /*
674 * Check if copy-up has happened as well as for upper alias (in
675 * case of hard links) is there.
676 *
677 * Both checks are lockless:
678 * - false negatives: will recheck under oi->lock
679 * - false positives:
680 * + ovl_dentry_upper() uses memory barriers to ensure the
681 * upper dentry is up-to-date
682 * + ovl_dentry_has_upper_alias() relies on locking of
683 * upper parent i_rwsem to prevent reordering copy-up
684 * with rename.
685 */
686 if (ovl_dentry_upper(dentry) &&
687 ovl_dentry_has_upper_alias(dentry))
688 break;
689
690 next = dget(dentry);
691 /* find the topmost dentry not yet copied up */
692 for (;;) {
693 parent = dget_parent(next);
694
695 if (ovl_dentry_upper(parent))
696 break;
697
698 dput(next);
699 next = parent;
700 }
701
702 err = ovl_copy_up_one(parent, next, flags);
703
704 dput(parent);
705 dput(next);
706 }
707 revert_creds(old_cred);
708
709 return err;
710 }
711
712 int ovl_copy_up(struct dentry *dentry)
713 {
714 return ovl_copy_up_flags(dentry, 0);
715 }