3 * Copyright (C) 2011 Novell Inc.
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.
10 #include <linux/module.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"
26 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
28 static bool __read_mostly ovl_check_copy_up
;
29 module_param_named(check_copy_up
, ovl_check_copy_up
, bool,
31 MODULE_PARM_DESC(ovl_check_copy_up
,
32 "Warn on copy-up when causing process also has a R/O fd open");
34 static int ovl_check_fd(const void *data
, struct file
*f
, unsigned int fd
)
36 const struct dentry
*dentry
= data
;
38 if (file_inode(f
) == d_inode(dentry
))
39 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",
40 f
, fd
, current
->pid
, current
->comm
);
45 * Check the fds open by this process and warn if something like the following
46 * scenario is about to occur:
48 * fd1 = open("foo", O_RDONLY);
49 * fd2 = open("foo", O_RDWR);
51 static void ovl_do_check_copy_up(struct dentry
*dentry
)
53 if (ovl_check_copy_up
)
54 iterate_fd(current
->files
, 0, ovl_check_fd
, dentry
);
57 int ovl_copy_xattr(struct dentry
*old
, struct dentry
*new)
59 ssize_t list_size
, size
, value_size
= 0;
60 char *buf
, *name
, *value
= NULL
;
61 int uninitialized_var(error
);
64 if (!(old
->d_inode
->i_opflags
& IOP_XATTR
) ||
65 !(new->d_inode
->i_opflags
& IOP_XATTR
))
68 list_size
= vfs_listxattr(old
, NULL
, 0);
70 if (list_size
== -EOPNOTSUPP
)
75 buf
= kzalloc(list_size
, GFP_KERNEL
);
79 list_size
= vfs_listxattr(old
, buf
, list_size
);
85 for (name
= buf
; list_size
; name
+= slen
) {
86 slen
= strnlen(name
, list_size
) + 1;
88 /* underlying fs providing us with an broken xattr list? */
89 if (WARN_ON(slen
> list_size
)) {
95 if (ovl_is_private_xattr(name
))
98 size
= vfs_getxattr(old
, name
, value
, value_size
);
100 size
= vfs_getxattr(old
, name
, NULL
, 0);
107 if (size
> value_size
) {
110 new = krealloc(value
, size
, GFP_KERNEL
);
120 error
= security_inode_copy_up_xattr(name
);
121 if (error
< 0 && error
!= -EOPNOTSUPP
)
125 continue; /* Discard */
127 error
= vfs_setxattr(new, name
, value
, size
, 0);
137 static int ovl_copy_up_data(struct path
*old
, struct path
*new, loff_t len
)
139 struct file
*old_file
;
140 struct file
*new_file
;
148 old_file
= ovl_path_open(old
, O_LARGEFILE
| O_RDONLY
);
149 if (IS_ERR(old_file
))
150 return PTR_ERR(old_file
);
152 new_file
= ovl_path_open(new, O_LARGEFILE
| O_WRONLY
);
153 if (IS_ERR(new_file
)) {
154 error
= PTR_ERR(new_file
);
158 /* Try to use clone_file_range to clone up within the same fs */
159 error
= vfs_clone_file_range(old_file
, 0, new_file
, 0, len
);
162 /* Couldn't clone, so now we try to copy the data */
165 /* FIXME: copy up sparse files efficiently */
167 size_t this_len
= OVL_COPY_UP_CHUNK_SIZE
;
173 if (signal_pending_state(TASK_KILLABLE
, current
)) {
178 bytes
= do_splice_direct(old_file
, &old_pos
,
180 this_len
, SPLICE_F_MOVE
);
185 WARN_ON(old_pos
!= new_pos
);
191 error
= vfs_fsync(new_file
, 0);
198 static int ovl_set_timestamps(struct dentry
*upperdentry
, struct kstat
*stat
)
200 struct iattr attr
= {
202 ATTR_ATIME
| ATTR_MTIME
| ATTR_ATIME_SET
| ATTR_MTIME_SET
,
203 .ia_atime
= stat
->atime
,
204 .ia_mtime
= stat
->mtime
,
207 return notify_change(upperdentry
, &attr
, NULL
);
210 int ovl_set_attr(struct dentry
*upperdentry
, struct kstat
*stat
)
215 * For the most part we want to set the mode bits before setting
216 * the user, otherwise the current context might lack permission
217 * for setting the mode. However for sxid/sticky bits we want
218 * the operation to fail if the current user isn't privileged
219 * towards the resulting inode. So we first set the mode but
220 * exclude the sxid/sticky bits, then set the user, then set the
221 * mode again if any of the sxid/sticky bits are set.
223 if (!S_ISLNK(stat
->mode
)) {
224 struct iattr attr
= {
225 .ia_valid
= ATTR_MODE
,
226 .ia_mode
= stat
->mode
& ~(S_ISUID
|S_ISGID
|S_ISVTX
),
228 err
= notify_change(upperdentry
, &attr
, NULL
);
231 struct iattr attr
= {
232 .ia_valid
= ATTR_UID
| ATTR_GID
,
236 err
= notify_change(upperdentry
, &attr
, NULL
);
238 if (!err
&& !S_ISLNK(stat
->mode
) &&
239 (stat
->mode
& (S_ISUID
|S_ISGID
|S_ISVTX
))) {
240 struct iattr attr
= {
241 .ia_valid
= ATTR_MODE
,
242 .ia_mode
= stat
->mode
,
244 err
= notify_change(upperdentry
, &attr
, NULL
);
247 ovl_set_timestamps(upperdentry
, stat
);
252 struct ovl_fh
*ovl_encode_fh(struct dentry
*lower
, bool is_upper
)
255 int fh_type
, fh_len
, dwords
;
257 int buflen
= MAX_HANDLE_SZ
;
258 uuid_t
*uuid
= &lower
->d_sb
->s_uuid
;
260 buf
= kmalloc(buflen
, GFP_KERNEL
);
262 return ERR_PTR(-ENOMEM
);
265 * We encode a non-connectable file handle for non-dir, because we
266 * only need to find the lower inode number and we don't want to pay
267 * the price or reconnecting the dentry.
269 dwords
= buflen
>> 2;
270 fh_type
= exportfs_encode_fh(lower
, buf
, &dwords
, 0);
271 buflen
= (dwords
<< 2);
274 if (WARN_ON(fh_type
< 0) ||
275 WARN_ON(buflen
> MAX_HANDLE_SZ
) ||
276 WARN_ON(fh_type
== FILEID_INVALID
))
279 BUILD_BUG_ON(MAX_HANDLE_SZ
+ offsetof(struct ovl_fh
, fid
) > 255);
280 fh_len
= offsetof(struct ovl_fh
, fid
) + buflen
;
281 fh
= kmalloc(fh_len
, GFP_KERNEL
);
283 fh
= ERR_PTR(-ENOMEM
);
287 fh
->version
= OVL_FH_VERSION
;
288 fh
->magic
= OVL_FH_MAGIC
;
290 fh
->flags
= OVL_FH_FLAG_CPU_ENDIAN
;
292 * When we will want to decode an overlay dentry from this handle
293 * and all layers are on the same fs, if we get a disconncted real
294 * dentry when we decode fid, the only way to tell if we should assign
295 * it to upperdentry or to lowerstack is by checking this flag.
298 fh
->flags
|= OVL_FH_FLAG_PATH_UPPER
;
301 memcpy(fh
->fid
, buf
, buflen
);
308 static int ovl_set_origin(struct dentry
*dentry
, struct dentry
*lower
,
309 struct dentry
*upper
)
311 const struct ovl_fh
*fh
= NULL
;
315 * When lower layer doesn't support export operations store a 'null' fh,
316 * so we can use the overlay.origin xattr to distignuish between a copy
317 * up and a pure upper inode.
319 if (ovl_can_decode_fh(lower
->d_sb
)) {
320 fh
= ovl_encode_fh(lower
, false);
326 * Do not fail when upper doesn't support xattrs.
328 err
= ovl_check_setxattr(dentry
, upper
, OVL_XATTR_ORIGIN
, fh
,
329 fh
? fh
->len
: 0, 0);
335 struct ovl_copy_up_ctx
{
336 struct dentry
*parent
;
337 struct dentry
*dentry
;
338 struct path lowerpath
;
342 struct dentry
*destdir
;
343 struct qstr destname
;
344 struct dentry
*workdir
;
349 static int ovl_link_up(struct ovl_copy_up_ctx
*c
)
352 struct dentry
*upper
;
353 struct dentry
*upperdir
= ovl_dentry_upper(c
->parent
);
354 struct inode
*udir
= d_inode(upperdir
);
356 /* Mark parent "impure" because it may now contain non-pure upper */
357 err
= ovl_set_impure(c
->parent
, upperdir
);
361 err
= ovl_set_nlink_lower(c
->dentry
);
365 inode_lock_nested(udir
, I_MUTEX_PARENT
);
366 upper
= lookup_one_len(c
->dentry
->d_name
.name
, upperdir
,
367 c
->dentry
->d_name
.len
);
368 err
= PTR_ERR(upper
);
369 if (!IS_ERR(upper
)) {
370 err
= ovl_do_link(ovl_dentry_upper(c
->dentry
), udir
, upper
,
375 /* Restore timestamps on parent (best effort) */
376 ovl_set_timestamps(upperdir
, &c
->pstat
);
377 ovl_dentry_set_upper_alias(c
->dentry
);
381 ovl_set_nlink_upper(c
->dentry
);
386 static int ovl_install_temp(struct ovl_copy_up_ctx
*c
, struct dentry
*temp
,
387 struct dentry
**newdentry
)
390 struct dentry
*upper
;
391 struct inode
*udir
= d_inode(c
->destdir
);
393 upper
= lookup_one_len(c
->destname
.name
, c
->destdir
, c
->destname
.len
);
395 return PTR_ERR(upper
);
398 err
= ovl_do_link(temp
, udir
, upper
, true);
400 err
= ovl_do_rename(d_inode(c
->workdir
), temp
, udir
, upper
, 0);
403 *newdentry
= dget(c
->tmpfile
? upper
: temp
);
409 static int ovl_get_tmpfile(struct ovl_copy_up_ctx
*c
, struct dentry
**tempp
)
413 const struct cred
*old_creds
= NULL
;
414 struct cred
*new_creds
= NULL
;
415 struct cattr cattr
= {
416 /* Can't properly set mode on creation because of the umask */
417 .mode
= c
->stat
.mode
& S_IFMT
,
418 .rdev
= c
->stat
.rdev
,
422 err
= security_inode_copy_up(c
->dentry
, &new_creds
);
427 old_creds
= override_creds(new_creds
);
430 temp
= ovl_do_tmpfile(c
->workdir
, c
->stat
.mode
);
434 temp
= ovl_lookup_temp(c
->workdir
);
438 err
= ovl_create_real(d_inode(c
->workdir
), temp
, &cattr
,
449 revert_creds(old_creds
);
460 static int ovl_copy_up_inode(struct ovl_copy_up_ctx
*c
, struct dentry
*temp
)
464 if (S_ISREG(c
->stat
.mode
)) {
465 struct path upperpath
;
467 ovl_path_upper(c
->dentry
, &upperpath
);
468 BUG_ON(upperpath
.dentry
!= NULL
);
469 upperpath
.dentry
= temp
;
471 err
= ovl_copy_up_data(&c
->lowerpath
, &upperpath
, c
->stat
.size
);
476 err
= ovl_copy_xattr(c
->lowerpath
.dentry
, temp
);
480 inode_lock(temp
->d_inode
);
481 err
= ovl_set_attr(temp
, &c
->stat
);
482 inode_unlock(temp
->d_inode
);
487 * Store identifier of lower inode in upper inode xattr to
488 * allow lookup of the copy up origin inode.
490 * Don't set origin when we are breaking the association with a lower
494 err
= ovl_set_origin(c
->dentry
, c
->lowerpath
.dentry
, temp
);
502 static int ovl_copy_up_locked(struct ovl_copy_up_ctx
*c
)
504 struct inode
*udir
= c
->destdir
->d_inode
;
506 struct dentry
*newdentry
= NULL
;
507 struct dentry
*temp
= NULL
;
510 err
= ovl_get_tmpfile(c
, &temp
);
514 err
= ovl_copy_up_inode(c
, temp
);
519 inode_lock_nested(udir
, I_MUTEX_PARENT
);
520 err
= ovl_install_temp(c
, temp
, &newdentry
);
523 err
= ovl_install_temp(c
, temp
, &newdentry
);
528 inode
= d_inode(c
->dentry
);
529 ovl_inode_update(inode
, newdentry
);
530 if (S_ISDIR(inode
->i_mode
))
531 ovl_set_flag(OVL_WHITEOUTS
, inode
);
539 ovl_cleanup(d_inode(c
->workdir
), temp
);
544 * Copy up a single dentry
546 * All renames start with copy up of source if necessary. The actual
547 * rename will only proceed once the copy up was successful. Copy up uses
548 * upper parent i_mutex for exclusion. Since rename can change d_parent it
549 * is possible that the copy up will lock the old parent. At that point
550 * the file will have already been copied up anyway.
552 static int ovl_do_copy_up(struct ovl_copy_up_ctx
*c
)
555 struct ovl_fs
*ofs
= c
->dentry
->d_sb
->s_fs_info
;
556 bool indexed
= false;
558 if (ovl_indexdir(c
->dentry
->d_sb
) && !S_ISDIR(c
->stat
.mode
) &&
562 if (S_ISDIR(c
->stat
.mode
) || c
->stat
.nlink
== 1 || indexed
)
566 c
->destdir
= ovl_indexdir(c
->dentry
->d_sb
);
567 err
= ovl_get_index_name(c
->lowerpath
.dentry
, &c
->destname
);
572 * Mark parent "impure" because it may now contain non-pure
575 err
= ovl_set_impure(c
->parent
, c
->destdir
);
580 /* Should we copyup with O_TMPFILE or with workdir? */
581 if (S_ISREG(c
->stat
.mode
) && ofs
->tmpfile
) {
583 err
= ovl_copy_up_locked(c
);
585 err
= ovl_lock_rename_workdir(c
->workdir
, c
->destdir
);
587 err
= ovl_copy_up_locked(c
);
588 unlock_rename(c
->workdir
, c
->destdir
);
594 ovl_set_flag(OVL_INDEX
, d_inode(c
->dentry
));
595 kfree(c
->destname
.name
);
597 struct inode
*udir
= d_inode(c
->destdir
);
599 /* Restore timestamps on parent (best effort) */
601 ovl_set_timestamps(c
->destdir
, &c
->pstat
);
604 ovl_dentry_set_upper_alias(c
->dentry
);
610 static int ovl_copy_up_one(struct dentry
*parent
, struct dentry
*dentry
,
614 DEFINE_DELAYED_CALL(done
);
615 struct path parentpath
;
616 struct ovl_copy_up_ctx ctx
= {
619 .workdir
= ovl_workdir(dentry
),
622 if (WARN_ON(!ctx
.workdir
))
625 ovl_path_lower(dentry
, &ctx
.lowerpath
);
626 err
= vfs_getattr(&ctx
.lowerpath
, &ctx
.stat
,
627 STATX_BASIC_STATS
, AT_STATX_SYNC_AS_STAT
);
631 ovl_path_upper(parent
, &parentpath
);
632 ctx
.destdir
= parentpath
.dentry
;
633 ctx
.destname
= dentry
->d_name
;
635 err
= vfs_getattr(&parentpath
, &ctx
.pstat
,
636 STATX_ATIME
| STATX_MTIME
, AT_STATX_SYNC_AS_STAT
);
640 /* maybe truncate regular file. this has no effect on dirs */
644 if (S_ISLNK(ctx
.stat
.mode
)) {
645 ctx
.link
= vfs_get_link(ctx
.lowerpath
.dentry
, &done
);
646 if (IS_ERR(ctx
.link
))
647 return PTR_ERR(ctx
.link
);
649 ovl_do_check_copy_up(ctx
.lowerpath
.dentry
);
651 err
= ovl_copy_up_start(dentry
);
652 /* err < 0: interrupted, err > 0: raced with another copy-up */
657 if (!ovl_dentry_upper(dentry
))
658 err
= ovl_do_copy_up(&ctx
);
659 if (!err
&& !ovl_dentry_has_upper_alias(dentry
))
660 err
= ovl_link_up(&ctx
);
661 ovl_copy_up_end(dentry
);
663 do_delayed_call(&done
);
668 int ovl_copy_up_flags(struct dentry
*dentry
, int flags
)
671 const struct cred
*old_cred
= ovl_override_creds(dentry
->d_sb
);
675 struct dentry
*parent
;
678 * Check if copy-up has happened as well as for upper alias (in
679 * case of hard links) is there.
681 * Both checks are lockless:
682 * - false negatives: will recheck under oi->lock
684 * + ovl_dentry_upper() uses memory barriers to ensure the
685 * upper dentry is up-to-date
686 * + ovl_dentry_has_upper_alias() relies on locking of
687 * upper parent i_rwsem to prevent reordering copy-up
690 if (ovl_dentry_upper(dentry
) &&
691 ovl_dentry_has_upper_alias(dentry
))
695 /* find the topmost dentry not yet copied up */
697 parent
= dget_parent(next
);
699 if (ovl_dentry_upper(parent
))
706 err
= ovl_copy_up_one(parent
, next
, flags
);
711 revert_creds(old_cred
);
716 int ovl_copy_up(struct dentry
*dentry
)
718 return ovl_copy_up_flags(dentry
, 0);