1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2011 Novell Inc.
7 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/file.h>
11 #include <linux/splice.h>
12 #include <linux/xattr.h>
13 #include <linux/security.h>
14 #include <linux/uaccess.h>
15 #include <linux/sched/signal.h>
16 #include <linux/cred.h>
17 #include <linux/namei.h>
18 #include <linux/fdtable.h>
19 #include <linux/ratelimit.h>
20 #include <linux/exportfs.h>
21 #include "overlayfs.h"
23 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
25 static int ovl_ccup_set(const char *buf, const struct kernel_param *param)
27 pr_warn("\"check_copy_up\" module option is obsolete\n");
31 static int ovl_ccup_get(char *buf, const struct kernel_param *param)
33 return sprintf(buf, "N\n");
36 module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644);
37 MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing");
39 static bool ovl_must_copy_xattr(const char *name)
41 return !strcmp(name, XATTR_POSIX_ACL_ACCESS) ||
42 !strcmp(name, XATTR_POSIX_ACL_DEFAULT) ||
43 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN);
46 int ovl_copy_xattr(struct super_block *sb, struct dentry *old,
49 ssize_t list_size, size, value_size = 0;
50 char *buf, *name, *value = NULL;
54 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
55 !(new->d_inode->i_opflags & IOP_XATTR))
58 list_size = vfs_listxattr(old, NULL, 0);
60 if (list_size == -EOPNOTSUPP)
65 buf = kzalloc(list_size, GFP_KERNEL);
69 list_size = vfs_listxattr(old, buf, list_size);
75 for (name = buf; list_size; name += slen) {
76 slen = strnlen(name, list_size) + 1;
78 /* underlying fs providing us with an broken xattr list? */
79 if (WARN_ON(slen > list_size)) {
85 if (ovl_is_private_xattr(sb, name))
88 error = security_inode_copy_up_xattr(name);
89 if (error < 0 && error != -EOPNOTSUPP)
93 continue; /* Discard */
96 size = vfs_getxattr(&init_user_ns, old, name, value, value_size);
98 size = vfs_getxattr(&init_user_ns, old, name, NULL, 0);
105 if (size > value_size) {
108 new = krealloc(value, size, GFP_KERNEL);
118 error = vfs_setxattr(&init_user_ns, new, name, value, size, 0);
120 if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name))
123 /* Ignore failure to copy unknown xattrs */
133 static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old,
134 struct path *new, loff_t len)
136 struct file *old_file;
137 struct file *new_file;
141 loff_t data_pos = -1;
143 bool skip_hole = false;
149 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
150 if (IS_ERR(old_file))
151 return PTR_ERR(old_file);
153 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
154 if (IS_ERR(new_file)) {
155 error = PTR_ERR(new_file);
159 /* Try to use clone_file_range to clone up within the same fs */
160 cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
163 /* Couldn't clone, so now we try to copy the data */
165 /* Check if lower fs supports seek operation */
166 if (old_file->f_mode & FMODE_LSEEK &&
167 old_file->f_op->llseek)
171 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
177 if (signal_pending_state(TASK_KILLABLE, current)) {
183 * Fill zero for hole will cost unnecessary disk space
184 * and meanwhile slow down the copy-up speed, so we do
185 * an optimization for hole during copy-up, it relies
186 * on SEEK_DATA implementation in lower fs so if lower
187 * fs does not support it, copy-up will behave as before.
189 * Detail logic of hole detection as below:
190 * When we detect next data position is larger than current
191 * position we will skip that hole, otherwise we copy
192 * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually,
193 * it may not recognize all kind of holes and sometimes
194 * only skips partial of hole area. However, it will be
195 * enough for most of the use cases.
198 if (skip_hole && data_pos < old_pos) {
199 data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
200 if (data_pos > old_pos) {
201 hole_len = data_pos - old_pos;
203 old_pos = new_pos = data_pos;
205 } else if (data_pos == -ENXIO) {
207 } else if (data_pos < 0) {
212 bytes = do_splice_direct(old_file, &old_pos,
214 this_len, SPLICE_F_MOVE);
219 WARN_ON(old_pos != new_pos);
224 if (!error && ovl_should_sync(ofs))
225 error = vfs_fsync(new_file, 0);
232 static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
234 struct iattr attr = {
235 .ia_valid = ATTR_SIZE,
236 .ia_size = stat->size,
239 return notify_change(&init_user_ns, upperdentry, &attr, NULL);
242 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
244 struct iattr attr = {
246 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
247 .ia_atime = stat->atime,
248 .ia_mtime = stat->mtime,
251 return notify_change(&init_user_ns, upperdentry, &attr, NULL);
254 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
258 if (!S_ISLNK(stat->mode)) {
259 struct iattr attr = {
260 .ia_valid = ATTR_MODE,
261 .ia_mode = stat->mode,
263 err = notify_change(&init_user_ns, upperdentry, &attr, NULL);
266 struct iattr attr = {
267 .ia_valid = ATTR_UID | ATTR_GID,
271 err = notify_change(&init_user_ns, upperdentry, &attr, NULL);
274 ovl_set_timestamps(upperdentry, stat);
279 struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real,
284 int buflen = MAX_HANDLE_SZ;
285 uuid_t *uuid = &real->d_sb->s_uuid;
288 /* Make sure the real fid stays 32bit aligned */
289 BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4);
290 BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255);
292 fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL);
294 return ERR_PTR(-ENOMEM);
297 * We encode a non-connectable file handle for non-dir, because we
298 * only need to find the lower inode number and we don't want to pay
299 * the price or reconnecting the dentry.
301 dwords = buflen >> 2;
302 fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0);
303 buflen = (dwords << 2);
306 if (WARN_ON(fh_type < 0) ||
307 WARN_ON(buflen > MAX_HANDLE_SZ) ||
308 WARN_ON(fh_type == FILEID_INVALID))
311 fh->fb.version = OVL_FH_VERSION;
312 fh->fb.magic = OVL_FH_MAGIC;
313 fh->fb.type = fh_type;
314 fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN;
316 * When we will want to decode an overlay dentry from this handle
317 * and all layers are on the same fs, if we get a disconncted real
318 * dentry when we decode fid, the only way to tell if we should assign
319 * it to upperdentry or to lowerstack is by checking this flag.
322 fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
323 fh->fb.len = sizeof(fh->fb) + buflen;
324 if (ofs->config.uuid)
334 int ovl_set_origin(struct ovl_fs *ofs, struct dentry *dentry,
335 struct dentry *lower, struct dentry *upper)
337 const struct ovl_fh *fh = NULL;
341 * When lower layer doesn't support export operations store a 'null' fh,
342 * so we can use the overlay.origin xattr to distignuish between a copy
343 * up and a pure upper inode.
345 if (ovl_can_decode_fh(lower->d_sb)) {
346 fh = ovl_encode_real_fh(ofs, lower, false);
352 * Do not fail when upper doesn't support xattrs.
354 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh->buf,
355 fh ? fh->fb.len : 0, 0);
358 /* Ignore -EPERM from setting "user.*" on symlink/special */
359 return err == -EPERM ? 0 : err;
362 /* Store file handle of @upper dir in @index dir entry */
363 static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper,
364 struct dentry *index)
366 const struct ovl_fh *fh;
369 fh = ovl_encode_real_fh(ofs, upper, true);
373 err = ovl_do_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len);
380 * Create and install index entry.
382 * Caller must hold i_mutex on indexdir.
384 static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
385 struct dentry *upper)
387 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
388 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
389 struct inode *dir = d_inode(indexdir);
390 struct dentry *index = NULL;
391 struct dentry *temp = NULL;
392 struct qstr name = { };
396 * For now this is only used for creating index entry for directories,
397 * because non-dir are copied up directly to index and then hardlinked
400 * TODO: implement create index for non-dir, so we can call it when
401 * encoding file handle for non-dir in case index does not exist.
403 if (WARN_ON(!d_is_dir(dentry)))
406 /* Directory not expected to be indexed before copy up */
407 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
410 err = ovl_get_index_name(ofs, origin, &name);
414 temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
419 err = ovl_set_upper_fh(ofs, upper, temp);
423 index = lookup_one_len(name.name, indexdir, name.len);
425 err = PTR_ERR(index);
427 err = ovl_do_rename(dir, temp, dir, index, 0);
432 ovl_cleanup(dir, temp);
439 struct ovl_copy_up_ctx {
440 struct dentry *parent;
441 struct dentry *dentry;
442 struct path lowerpath;
446 struct dentry *destdir;
447 struct qstr destname;
448 struct dentry *workdir;
454 static int ovl_link_up(struct ovl_copy_up_ctx *c)
457 struct dentry *upper;
458 struct dentry *upperdir = ovl_dentry_upper(c->parent);
459 struct inode *udir = d_inode(upperdir);
461 /* Mark parent "impure" because it may now contain non-pure upper */
462 err = ovl_set_impure(c->parent, upperdir);
466 err = ovl_set_nlink_lower(c->dentry);
470 inode_lock_nested(udir, I_MUTEX_PARENT);
471 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
472 c->dentry->d_name.len);
473 err = PTR_ERR(upper);
474 if (!IS_ERR(upper)) {
475 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
479 /* Restore timestamps on parent (best effort) */
480 ovl_set_timestamps(upperdir, &c->pstat);
481 ovl_dentry_set_upper_alias(c->dentry);
488 err = ovl_set_nlink_upper(c->dentry);
493 static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
495 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
499 * Copy up data first and then xattrs. Writing data after
500 * xattrs will remove security.capability xattr automatically.
502 if (S_ISREG(c->stat.mode) && !c->metacopy) {
503 struct path upperpath, datapath;
505 ovl_path_upper(c->dentry, &upperpath);
506 if (WARN_ON(upperpath.dentry != NULL))
508 upperpath.dentry = temp;
510 ovl_path_lowerdata(c->dentry, &datapath);
511 err = ovl_copy_up_data(ofs, &datapath, &upperpath,
517 err = ovl_copy_xattr(c->dentry->d_sb, c->lowerpath.dentry, temp);
522 * Store identifier of lower inode in upper inode xattr to
523 * allow lookup of the copy up origin inode.
525 * Don't set origin when we are breaking the association with a lower
529 err = ovl_set_origin(ofs, c->dentry, c->lowerpath.dentry, temp);
535 err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
536 NULL, 0, -EOPNOTSUPP);
541 inode_lock(temp->d_inode);
542 if (S_ISREG(c->stat.mode))
543 err = ovl_set_size(temp, &c->stat);
545 err = ovl_set_attr(temp, &c->stat);
546 inode_unlock(temp->d_inode);
551 struct ovl_cu_creds {
552 const struct cred *old;
556 static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
560 cc->old = cc->new = NULL;
561 err = security_inode_copy_up(dentry, &cc->new);
566 cc->old = override_creds(cc->new);
571 static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
574 revert_creds(cc->old);
580 * Copyup using workdir to prepare temp file. Used when copying up directories,
581 * special files or when upper fs doesn't support O_TMPFILE.
583 static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
586 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
587 struct dentry *temp, *upper;
588 struct ovl_cu_creds cc;
590 struct ovl_cattr cattr = {
591 /* Can't properly set mode on creation because of the umask */
592 .mode = c->stat.mode & S_IFMT,
593 .rdev = c->stat.rdev,
597 /* workdir and destdir could be the same when copying up to indexdir */
599 if (lock_rename(c->workdir, c->destdir) != NULL)
602 err = ovl_prep_cu_creds(c->dentry, &cc);
606 temp = ovl_create_temp(c->workdir, &cattr);
607 ovl_revert_cu_creds(&cc);
613 err = ovl_copy_up_inode(c, temp);
617 if (S_ISDIR(c->stat.mode) && c->indexed) {
618 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
623 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
624 err = PTR_ERR(upper);
628 err = ovl_do_rename(wdir, temp, udir, upper, 0);
634 ovl_set_upperdata(d_inode(c->dentry));
635 inode = d_inode(c->dentry);
636 ovl_inode_update(inode, temp);
637 if (S_ISDIR(inode->i_mode))
638 ovl_set_flag(OVL_WHITEOUTS, inode);
640 unlock_rename(c->workdir, c->destdir);
645 ovl_cleanup(wdir, temp);
650 /* Copyup using O_TMPFILE which does not require cross dir locking */
651 static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
653 struct inode *udir = d_inode(c->destdir);
654 struct dentry *temp, *upper;
655 struct ovl_cu_creds cc;
658 err = ovl_prep_cu_creds(c->dentry, &cc);
662 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
663 ovl_revert_cu_creds(&cc);
666 return PTR_ERR(temp);
668 err = ovl_copy_up_inode(c, temp);
672 inode_lock_nested(udir, I_MUTEX_PARENT);
674 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
675 err = PTR_ERR(upper);
676 if (!IS_ERR(upper)) {
677 err = ovl_do_link(temp, udir, upper);
686 ovl_set_upperdata(d_inode(c->dentry));
687 ovl_inode_update(d_inode(c->dentry), temp);
697 * Copy up a single dentry
699 * All renames start with copy up of source if necessary. The actual
700 * rename will only proceed once the copy up was successful. Copy up uses
701 * upper parent i_mutex for exclusion. Since rename can change d_parent it
702 * is possible that the copy up will lock the old parent. At that point
703 * the file will have already been copied up anyway.
705 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
708 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
709 bool to_index = false;
712 * Indexed non-dir is copied up directly to the index entry and then
713 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
714 * then index entry is created and then copied up dir installed.
715 * Copying dir up to indexdir instead of workdir simplifies locking.
717 if (ovl_need_index(c->dentry)) {
719 if (S_ISDIR(c->stat.mode))
720 c->workdir = ovl_indexdir(c->dentry->d_sb);
725 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
729 c->destdir = ovl_indexdir(c->dentry->d_sb);
730 err = ovl_get_index_name(ofs, c->lowerpath.dentry, &c->destname);
733 } else if (WARN_ON(!c->parent)) {
734 /* Disconnected dentry must be copied up to index dir */
738 * Mark parent "impure" because it may now contain non-pure
741 err = ovl_set_impure(c->parent, c->destdir);
746 /* Should we copyup with O_TMPFILE or with workdir? */
747 if (S_ISREG(c->stat.mode) && ofs->tmpfile)
748 err = ovl_copy_up_tmpfile(c);
750 err = ovl_copy_up_workdir(c);
755 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
758 /* Initialize nlink for copy up of disconnected dentry */
759 err = ovl_set_nlink_upper(c->dentry);
761 struct inode *udir = d_inode(c->destdir);
763 /* Restore timestamps on parent (best effort) */
765 ovl_set_timestamps(c->destdir, &c->pstat);
768 ovl_dentry_set_upper_alias(c->dentry);
773 kfree(c->destname.name);
777 static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
780 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
782 if (!ofs->config.metacopy)
788 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
794 static ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value)
799 res = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0);
800 if (res == -ENODATA || res == -EOPNOTSUPP)
804 buf = kzalloc(res, GFP_KERNEL);
808 res = vfs_getxattr(&init_user_ns, dentry, name, buf, res);
817 /* Copy up data of an inode which was copied up metadata only in the past. */
818 static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
820 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
821 struct path upperpath, datapath;
823 char *capability = NULL;
826 ovl_path_upper(c->dentry, &upperpath);
827 if (WARN_ON(upperpath.dentry == NULL))
830 ovl_path_lowerdata(c->dentry, &datapath);
831 if (WARN_ON(datapath.dentry == NULL))
835 err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS,
841 err = ovl_copy_up_data(ofs, &datapath, &upperpath, c->stat.size);
846 * Writing to upper file will clear security.capability xattr. We
847 * don't want that to happen for normal copy-up operation.
850 err = vfs_setxattr(&init_user_ns, upperpath.dentry,
851 XATTR_NAME_CAPS, capability, cap_size, 0);
857 err = ovl_do_removexattr(ofs, upperpath.dentry, OVL_XATTR_METACOPY);
861 ovl_set_upperdata(d_inode(c->dentry));
868 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
872 DEFINE_DELAYED_CALL(done);
873 struct path parentpath;
874 struct ovl_copy_up_ctx ctx = {
877 .workdir = ovl_workdir(dentry),
880 if (WARN_ON(!ctx.workdir))
883 ovl_path_lower(dentry, &ctx.lowerpath);
884 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
885 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
889 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
892 ovl_path_upper(parent, &parentpath);
893 ctx.destdir = parentpath.dentry;
894 ctx.destname = dentry->d_name;
896 err = vfs_getattr(&parentpath, &ctx.pstat,
897 STATX_ATIME | STATX_MTIME,
898 AT_STATX_SYNC_AS_STAT);
903 /* maybe truncate regular file. this has no effect on dirs */
907 if (S_ISLNK(ctx.stat.mode)) {
908 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
909 if (IS_ERR(ctx.link))
910 return PTR_ERR(ctx.link);
913 err = ovl_copy_up_start(dentry, flags);
914 /* err < 0: interrupted, err > 0: raced with another copy-up */
919 if (!ovl_dentry_upper(dentry))
920 err = ovl_do_copy_up(&ctx);
921 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
922 err = ovl_link_up(&ctx);
923 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
924 err = ovl_copy_up_meta_inode_data(&ctx);
925 ovl_copy_up_end(dentry);
927 do_delayed_call(&done);
932 static int ovl_copy_up_flags(struct dentry *dentry, int flags)
935 const struct cred *old_cred;
936 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
939 * With NFS export, copy up can get called for a disconnected non-dir.
940 * In this case, we will copy up lower inode to index dir without
941 * linking it to upper dir.
943 if (WARN_ON(disconnected && d_is_dir(dentry)))
946 old_cred = ovl_override_creds(dentry->d_sb);
949 struct dentry *parent = NULL;
951 if (ovl_already_copied_up(dentry, flags))
955 /* find the topmost dentry not yet copied up */
956 for (; !disconnected;) {
957 parent = dget_parent(next);
959 if (ovl_dentry_upper(parent))
966 err = ovl_copy_up_one(parent, next, flags);
971 revert_creds(old_cred);
976 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
978 /* Copy up of disconnected dentry does not set upper alias */
979 if (ovl_already_copied_up(dentry, flags))
982 if (special_file(d_inode(dentry)->i_mode))
985 if (!ovl_open_flags_need_copy_up(flags))
991 int ovl_maybe_copy_up(struct dentry *dentry, int flags)
995 if (ovl_open_need_copy_up(dentry, flags)) {
996 err = ovl_want_write(dentry);
998 err = ovl_copy_up_flags(dentry, flags);
999 ovl_drop_write(dentry);
1006 int ovl_copy_up_with_data(struct dentry *dentry)
1008 return ovl_copy_up_flags(dentry, O_WRONLY);
1011 int ovl_copy_up(struct dentry *dentry)
1013 return ovl_copy_up_flags(dentry, 0);