1 #include <linux/btrfs.h>
2 #include <linux/capability.h>
3 #include <linux/cred.h>
4 #include <linux/mount.h>
5 #include <linux/fdtable.h>
6 #include <linux/file.h>
8 #include <linux/namei.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/magic.h>
12 #include <linux/parser.h>
13 #include <linux/security.h>
14 #include <linux/seq_file.h>
15 #include <linux/statfs.h>
16 #include <linux/slab.h>
17 #include <linux/user_namespace.h>
18 #include <linux/uidgid.h>
19 #include <linux/xattr.h>
20 #include <linux/posix_acl.h>
21 #include <linux/posix_acl_xattr.h>
22 #include <linux/uio.h>
23 #include <linux/fiemap.h>
25 struct shiftfs_super_info
{
27 struct user_namespace
*userns
;
28 /* creds of process who created the super block */
29 const struct cred
*creator_cred
;
31 unsigned int passthrough
;
32 unsigned int passthrough_mark
;
35 static void shiftfs_fill_inode(struct inode
*inode
, unsigned long ino
,
36 umode_t mode
, dev_t dev
, struct dentry
*dentry
);
38 #define SHIFTFS_PASSTHROUGH_NONE 0
39 #define SHIFTFS_PASSTHROUGH_STAT 1
40 #define SHIFTFS_PASSTHROUGH_IOCTL 2
41 #define SHIFTFS_PASSTHROUGH_ALL \
42 (SHIFTFS_PASSTHROUGH_STAT | SHIFTFS_PASSTHROUGH_IOCTL)
44 static inline bool shiftfs_passthrough_ioctls(struct shiftfs_super_info
*info
)
46 if (!(info
->passthrough
& SHIFTFS_PASSTHROUGH_IOCTL
))
52 static inline bool shiftfs_passthrough_statfs(struct shiftfs_super_info
*info
)
54 if (!(info
->passthrough
& SHIFTFS_PASSTHROUGH_STAT
))
66 /* global filesystem options */
67 static const match_table_t tokens
= {
69 { OPT_PASSTHROUGH
, "passthrough=%u" },
73 static const struct cred
*shiftfs_override_creds(const struct super_block
*sb
)
75 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
77 return override_creds(sbinfo
->creator_cred
);
80 static inline void shiftfs_revert_object_creds(const struct cred
*oldcred
,
83 revert_creds(oldcred
);
87 static kuid_t
shift_kuid(struct user_namespace
*from
, struct user_namespace
*to
,
90 uid_t uid
= from_kuid(from
, kuid
);
91 return make_kuid(to
, uid
);
94 static kgid_t
shift_kgid(struct user_namespace
*from
, struct user_namespace
*to
,
97 gid_t gid
= from_kgid(from
, kgid
);
98 return make_kgid(to
, gid
);
101 static int shiftfs_override_object_creds(const struct super_block
*sb
,
102 const struct cred
**oldcred
,
103 struct cred
**newcred
,
104 struct dentry
*dentry
, umode_t mode
,
107 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
108 kuid_t fsuid
= current_fsuid();
109 kgid_t fsgid
= current_fsgid();
111 *oldcred
= shiftfs_override_creds(sb
);
113 *newcred
= prepare_creds();
115 revert_creds(*oldcred
);
119 (*newcred
)->fsuid
= shift_kuid(sb
->s_user_ns
, sbinfo
->userns
, fsuid
);
120 (*newcred
)->fsgid
= shift_kgid(sb
->s_user_ns
, sbinfo
->userns
, fsgid
);
123 int err
= security_dentry_create_files_as(dentry
, mode
,
127 shiftfs_revert_object_creds(*oldcred
, *newcred
);
132 put_cred(override_creds(*newcred
));
136 static void shiftfs_copyattr(struct inode
*from
, struct inode
*to
)
138 struct user_namespace
*from_ns
= from
->i_sb
->s_user_ns
;
139 struct user_namespace
*to_ns
= to
->i_sb
->s_user_ns
;
141 to
->i_uid
= shift_kuid(from_ns
, to_ns
, from
->i_uid
);
142 to
->i_gid
= shift_kgid(from_ns
, to_ns
, from
->i_gid
);
143 to
->i_mode
= from
->i_mode
;
144 to
->i_atime
= from
->i_atime
;
145 to
->i_mtime
= from
->i_mtime
;
146 to
->i_ctime
= from
->i_ctime
;
147 i_size_write(to
, i_size_read(from
));
150 static void shiftfs_copyflags(struct inode
*from
, struct inode
*to
)
152 unsigned int mask
= S_SYNC
| S_IMMUTABLE
| S_APPEND
| S_NOATIME
;
154 inode_set_flags(to
, from
->i_flags
& mask
, mask
);
157 static void shiftfs_file_accessed(struct file
*file
)
159 struct inode
*upperi
, *loweri
;
161 if (file
->f_flags
& O_NOATIME
)
164 upperi
= file_inode(file
);
165 loweri
= upperi
->i_private
;
170 upperi
->i_mtime
= loweri
->i_mtime
;
171 upperi
->i_ctime
= loweri
->i_ctime
;
173 touch_atime(&file
->f_path
);
176 static int shiftfs_parse_mount_options(struct shiftfs_super_info
*sbinfo
,
180 substring_t args
[MAX_OPT_ARGS
];
182 sbinfo
->mark
= false;
183 sbinfo
->passthrough
= 0;
185 while ((p
= strsep(&options
, ",")) != NULL
) {
186 int err
, intarg
, token
;
191 token
= match_token(p
, tokens
, args
);
196 case OPT_PASSTHROUGH
:
197 err
= match_int(&args
[0], &intarg
);
201 if (intarg
& ~SHIFTFS_PASSTHROUGH_ALL
)
204 sbinfo
->passthrough
= intarg
;
214 static void shiftfs_d_release(struct dentry
*dentry
)
216 struct dentry
*lowerd
= dentry
->d_fsdata
;
222 static struct dentry
*shiftfs_d_real(struct dentry
*dentry
,
223 const struct inode
*inode
)
225 struct dentry
*lowerd
= dentry
->d_fsdata
;
227 if (inode
&& d_inode(dentry
) == inode
)
230 lowerd
= d_real(lowerd
, inode
);
231 if (lowerd
&& (!inode
|| inode
== d_inode(lowerd
)))
234 WARN(1, "shiftfs_d_real(%pd4, %s:%lu): real dentry not found\n", dentry
,
235 inode
? inode
->i_sb
->s_id
: "NULL", inode
? inode
->i_ino
: 0);
239 static int shiftfs_d_weak_revalidate(struct dentry
*dentry
, unsigned int flags
)
242 struct dentry
*lowerd
= dentry
->d_fsdata
;
244 if (d_is_negative(lowerd
) != d_is_negative(dentry
))
247 if ((lowerd
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
))
248 err
= lowerd
->d_op
->d_weak_revalidate(lowerd
, flags
);
250 if (d_really_is_positive(dentry
)) {
251 struct inode
*inode
= d_inode(dentry
);
252 struct inode
*loweri
= d_inode(lowerd
);
254 shiftfs_copyattr(loweri
, inode
);
260 static int shiftfs_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
263 struct dentry
*lowerd
= dentry
->d_fsdata
;
265 if (d_unhashed(lowerd
) ||
266 ((d_is_negative(lowerd
) != d_is_negative(dentry
))))
269 if (flags
& LOOKUP_RCU
)
272 if ((lowerd
->d_flags
& DCACHE_OP_REVALIDATE
))
273 err
= lowerd
->d_op
->d_revalidate(lowerd
, flags
);
275 if (d_really_is_positive(dentry
)) {
276 struct inode
*inode
= d_inode(dentry
);
277 struct inode
*loweri
= d_inode(lowerd
);
279 shiftfs_copyattr(loweri
, inode
);
285 static const struct dentry_operations shiftfs_dentry_ops
= {
286 .d_release
= shiftfs_d_release
,
287 .d_real
= shiftfs_d_real
,
288 .d_revalidate
= shiftfs_d_revalidate
,
289 .d_weak_revalidate
= shiftfs_d_weak_revalidate
,
292 static const char *shiftfs_get_link(struct dentry
*dentry
, struct inode
*inode
,
293 struct delayed_call
*done
)
296 const struct cred
*oldcred
;
297 struct dentry
*lowerd
;
299 /* RCU lookup not supported */
301 return ERR_PTR(-ECHILD
);
303 lowerd
= dentry
->d_fsdata
;
304 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
305 p
= vfs_get_link(lowerd
, done
);
306 revert_creds(oldcred
);
311 static int shiftfs_setxattr(struct user_namespace
*ns
,
312 struct dentry
*dentry
, struct inode
*inode
,
313 const char *name
, const void *value
,
314 size_t size
, int flags
)
316 struct dentry
*lowerd
= dentry
->d_fsdata
;
318 const struct cred
*oldcred
;
320 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
321 err
= vfs_setxattr(ns
, lowerd
, name
, value
, size
, flags
);
322 revert_creds(oldcred
);
324 shiftfs_copyattr(lowerd
->d_inode
, inode
);
329 static int shiftfs_xattr_get(const struct xattr_handler
*handler
,
330 struct dentry
*dentry
, struct inode
*inode
,
331 const char *name
, void *value
, size_t size
)
333 struct dentry
*lowerd
= dentry
->d_fsdata
;
335 const struct cred
*oldcred
;
337 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
338 err
= vfs_getxattr(&init_user_ns
, lowerd
, name
, value
, size
);
339 revert_creds(oldcred
);
344 static ssize_t
shiftfs_listxattr(struct dentry
*dentry
, char *list
,
347 struct dentry
*lowerd
= dentry
->d_fsdata
;
349 const struct cred
*oldcred
;
351 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
352 err
= vfs_listxattr(lowerd
, list
, size
);
353 revert_creds(oldcred
);
358 static int shiftfs_removexattr(struct user_namespace
*ns
,
359 struct dentry
*dentry
, const char *name
)
361 struct dentry
*lowerd
= dentry
->d_fsdata
;
363 const struct cred
*oldcred
;
365 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
366 err
= vfs_removexattr(ns
, lowerd
, name
);
367 revert_creds(oldcred
);
370 shiftfs_copyattr(lowerd
->d_inode
, d_inode(dentry
));
375 static int shiftfs_xattr_set(const struct xattr_handler
*handler
,
376 struct user_namespace
*ns
,
377 struct dentry
*dentry
, struct inode
*inode
,
378 const char *name
, const void *value
, size_t size
,
382 return shiftfs_removexattr(ns
, dentry
, name
);
383 return shiftfs_setxattr(ns
, dentry
, inode
, name
, value
, size
, flags
);
386 static int shiftfs_inode_test(struct inode
*inode
, void *data
)
388 return inode
->i_private
== data
;
391 static int shiftfs_inode_set(struct inode
*inode
, void *data
)
393 inode
->i_private
= data
;
397 static int shiftfs_create_object(struct user_namespace
*ns
,
398 struct inode
*diri
, struct dentry
*dentry
,
399 umode_t mode
, const char *symlink
,
400 struct dentry
*hardlink
, bool excl
)
403 const struct cred
*oldcred
;
404 struct cred
*newcred
;
405 void *loweri_iop_ptr
= NULL
;
406 umode_t modei
= mode
;
407 struct super_block
*dir_sb
= diri
->i_sb
;
408 struct dentry
*lowerd_new
= dentry
->d_fsdata
;
409 struct inode
*inode
= NULL
, *loweri_dir
= diri
->i_private
;
410 const struct inode_operations
*loweri_dir_iop
= loweri_dir
->i_op
;
411 struct dentry
*lowerd_link
= NULL
;
414 loweri_iop_ptr
= loweri_dir_iop
->link
;
416 switch (mode
& S_IFMT
) {
418 loweri_iop_ptr
= loweri_dir_iop
->mkdir
;
421 loweri_iop_ptr
= loweri_dir_iop
->create
;
424 loweri_iop_ptr
= loweri_dir_iop
->symlink
;
429 loweri_iop_ptr
= loweri_dir_iop
->mknod
;
433 if (!loweri_iop_ptr
) {
438 inode_lock_nested(loweri_dir
, I_MUTEX_PARENT
);
441 inode
= new_inode(dir_sb
);
448 * new_inode() will have added the new inode to the super
449 * block's list of inodes. Further below we will call
450 * inode_insert5() Which would perform the same operation again
451 * thereby corrupting the list. To avoid this raise I_CREATING
452 * in i_state which will cause inode_insert5() to skip this
453 * step. I_CREATING will be cleared by d_instantiate_new()
456 spin_lock(&inode
->i_lock
);
457 inode
->i_state
|= I_CREATING
;
458 spin_unlock(&inode
->i_lock
);
460 inode_init_owner(ns
, inode
, diri
, mode
);
461 modei
= inode
->i_mode
;
464 err
= shiftfs_override_object_creds(dentry
->d_sb
, &oldcred
, &newcred
,
465 dentry
, modei
, hardlink
!= NULL
);
470 lowerd_link
= hardlink
->d_fsdata
;
471 err
= vfs_link(lowerd_link
, ns
, loweri_dir
, lowerd_new
, NULL
);
473 switch (modei
& S_IFMT
) {
475 err
= vfs_mkdir(ns
, loweri_dir
, lowerd_new
, modei
);
478 err
= vfs_create(ns
, loweri_dir
, lowerd_new
, modei
, excl
);
481 err
= vfs_symlink(ns
, loweri_dir
, lowerd_new
, symlink
);
486 err
= vfs_mknod(ns
, loweri_dir
, lowerd_new
, modei
, 0);
494 shiftfs_revert_object_creds(oldcred
, newcred
);
496 if (!err
&& WARN_ON(!lowerd_new
->d_inode
))
502 inode
= d_inode(hardlink
);
505 /* copy up times from lower inode */
506 shiftfs_copyattr(d_inode(lowerd_link
), inode
);
507 set_nlink(d_inode(hardlink
), d_inode(lowerd_link
)->i_nlink
);
508 d_instantiate(dentry
, inode
);
510 struct inode
*inode_tmp
;
511 struct inode
*loweri_new
= d_inode(lowerd_new
);
513 inode_tmp
= inode_insert5(inode
, (unsigned long)loweri_new
,
514 shiftfs_inode_test
, shiftfs_inode_set
,
516 if (unlikely(inode_tmp
!= inode
)) {
517 pr_err_ratelimited("shiftfs: newly created inode found in cache\n");
524 shiftfs_fill_inode(inode
, loweri_new
->i_ino
, loweri_new
->i_mode
,
526 d_instantiate_new(dentry
, inode
);
529 shiftfs_copyattr(loweri_dir
, diri
);
530 if (loweri_iop_ptr
== loweri_dir_iop
->mkdir
)
531 set_nlink(diri
, loweri_dir
->i_nlink
);
537 inode_unlock(loweri_dir
);
542 static int shiftfs_create(struct user_namespace
*ns
,
543 struct inode
*dir
, struct dentry
*dentry
,
544 umode_t mode
, bool excl
)
548 return shiftfs_create_object(ns
, dir
, dentry
, mode
, NULL
, NULL
, excl
);
551 static int shiftfs_mkdir(struct user_namespace
*ns
, struct inode
*dir
, struct dentry
*dentry
,
556 return shiftfs_create_object(ns
, dir
, dentry
, mode
, NULL
, NULL
, false);
559 static int shiftfs_link(struct dentry
*hardlink
, struct inode
*dir
,
560 struct dentry
*dentry
)
562 return shiftfs_create_object(&init_user_ns
, dir
, dentry
, 0, NULL
, hardlink
, false);
565 static int shiftfs_mknod(struct user_namespace
*ns
,
566 struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
569 if (!S_ISFIFO(mode
) && !S_ISSOCK(mode
))
572 return shiftfs_create_object(ns
, dir
, dentry
, mode
, NULL
, NULL
, false);
575 static int shiftfs_symlink(struct user_namespace
*ns
, struct inode
*dir
, struct dentry
*dentry
,
578 return shiftfs_create_object(ns
, dir
, dentry
, S_IFLNK
, symlink
, NULL
, false);
581 static int shiftfs_rm(struct inode
*dir
, struct dentry
*dentry
, bool rmdir
)
583 struct dentry
*lowerd
= dentry
->d_fsdata
;
584 struct inode
*loweri
= dir
->i_private
;
585 struct inode
*inode
= d_inode(dentry
);
587 const struct cred
*oldcred
;
590 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
591 inode_lock_nested(loweri
, I_MUTEX_PARENT
);
593 err
= vfs_rmdir(&init_user_ns
, loweri
, lowerd
);
595 err
= vfs_unlink(&init_user_ns
, loweri
, lowerd
, NULL
);
596 revert_creds(oldcred
);
606 inode_unlock(loweri
);
608 shiftfs_copyattr(loweri
, dir
);
614 static int shiftfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
616 return shiftfs_rm(dir
, dentry
, false);
619 static int shiftfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
621 return shiftfs_rm(dir
, dentry
, true);
624 static int shiftfs_rename(struct user_namespace
*ns
,
625 struct inode
*olddir
, struct dentry
*old
,
626 struct inode
*newdir
, struct dentry
*new,
629 struct dentry
*lowerd_dir_old
= old
->d_parent
->d_fsdata
,
630 *lowerd_dir_new
= new->d_parent
->d_fsdata
,
631 *lowerd_old
= old
->d_fsdata
, *lowerd_new
= new->d_fsdata
,
633 struct inode
*loweri_dir_old
= lowerd_dir_old
->d_inode
,
634 *loweri_dir_new
= lowerd_dir_new
->d_inode
;
635 struct renamedata rd
= {
636 .old_mnt_userns
= ns
,
637 .old_dir
= loweri_dir_old
,
638 .old_dentry
= lowerd_old
,
639 .new_mnt_userns
= ns
,
640 .new_dir
= loweri_dir_new
,
641 .new_dentry
= lowerd_new
,
644 const struct cred
*oldcred
;
646 trapd
= lock_rename(lowerd_dir_new
, lowerd_dir_old
);
648 if (trapd
== lowerd_old
|| trapd
== lowerd_new
)
651 oldcred
= shiftfs_override_creds(old
->d_sb
);
652 err
= vfs_rename(&rd
);
653 revert_creds(oldcred
);
655 shiftfs_copyattr(loweri_dir_old
, olddir
);
656 shiftfs_copyattr(loweri_dir_new
, newdir
);
659 unlock_rename(lowerd_dir_new
, lowerd_dir_old
);
664 static struct dentry
*shiftfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
669 const struct cred
*oldcred
;
670 struct dentry
*lowerd
= dentry
->d_parent
->d_fsdata
;
671 struct inode
*inode
= NULL
, *loweri
= lowerd
->d_inode
;
674 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
675 new = lookup_one_len(dentry
->d_name
.name
, lowerd
, dentry
->d_name
.len
);
676 revert_creds(oldcred
);
677 inode_unlock(loweri
);
682 dentry
->d_fsdata
= new;
688 inode
= iget5_locked(dentry
->d_sb
, (unsigned long)newi
,
689 shiftfs_inode_test
, shiftfs_inode_set
, newi
);
692 return ERR_PTR(-ENOMEM
);
694 if (inode
->i_state
& I_NEW
) {
696 * inode->i_private set by shiftfs_inode_set(), but we still
697 * need to take a reference
700 shiftfs_fill_inode(inode
, newi
->i_ino
, newi
->i_mode
, 0, new);
701 unlock_new_inode(inode
);
705 return d_splice_alias(inode
, dentry
);
708 static int shiftfs_permission(struct user_namespace
*ns
, struct inode
*inode
, int mask
)
711 const struct cred
*oldcred
;
712 struct inode
*loweri
= inode
->i_private
;
715 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
719 err
= generic_permission(ns
, inode
, mask
);
723 oldcred
= shiftfs_override_creds(inode
->i_sb
);
724 err
= inode_permission(ns
, loweri
, mask
);
725 revert_creds(oldcred
);
730 static int shiftfs_fiemap(struct inode
*inode
,
731 struct fiemap_extent_info
*fieinfo
, u64 start
,
735 const struct cred
*oldcred
;
736 struct inode
*loweri
= inode
->i_private
;
738 if (!loweri
->i_op
->fiemap
)
741 oldcred
= shiftfs_override_creds(inode
->i_sb
);
742 if (fieinfo
->fi_flags
& FIEMAP_FLAG_SYNC
)
743 filemap_write_and_wait(loweri
->i_mapping
);
744 err
= loweri
->i_op
->fiemap(loweri
, fieinfo
, start
, len
);
745 revert_creds(oldcred
);
750 static int shiftfs_tmpfile(struct user_namespace
*ns
,
751 struct inode
*dir
, struct dentry
*dentry
,
755 const struct cred
*oldcred
;
756 struct dentry
*lowerd
= dentry
->d_fsdata
;
757 struct inode
*loweri
= dir
->i_private
;
759 if (!loweri
->i_op
->tmpfile
)
762 oldcred
= shiftfs_override_creds(dir
->i_sb
);
763 err
= loweri
->i_op
->tmpfile(ns
, loweri
, lowerd
, mode
);
764 revert_creds(oldcred
);
769 static int shiftfs_setattr(struct user_namespace
*ns
, struct dentry
*dentry
, struct iattr
*attr
)
771 struct dentry
*lowerd
= dentry
->d_fsdata
;
772 struct inode
*loweri
= lowerd
->d_inode
;
773 struct iattr newattr
;
774 const struct cred
*oldcred
;
775 struct super_block
*sb
= dentry
->d_sb
;
776 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
779 err
= setattr_prepare(ns
, dentry
, attr
);
784 newattr
.ia_uid
= shift_kuid(sb
->s_user_ns
, sbinfo
->userns
, attr
->ia_uid
);
785 newattr
.ia_gid
= shift_kgid(sb
->s_user_ns
, sbinfo
->userns
, attr
->ia_gid
);
788 * mode change is for clearing setuid/setgid bits. Allow lower fs
789 * to interpret this in its own way.
791 if (newattr
.ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
))
792 newattr
.ia_valid
&= ~ATTR_MODE
;
795 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
796 err
= notify_change(ns
, lowerd
, &newattr
, NULL
);
797 revert_creds(oldcred
);
798 inode_unlock(loweri
);
800 shiftfs_copyattr(loweri
, d_inode(dentry
));
805 static int shiftfs_getattr(struct user_namespace
*ns
,
806 const struct path
*path
, struct kstat
*stat
,
807 u32 request_mask
, unsigned int query_flags
)
809 struct inode
*inode
= path
->dentry
->d_inode
;
810 struct dentry
*lowerd
= path
->dentry
->d_fsdata
;
811 struct inode
*loweri
= lowerd
->d_inode
;
812 struct shiftfs_super_info
*info
= path
->dentry
->d_sb
->s_fs_info
;
813 struct path newpath
= { .mnt
= info
->mnt
, .dentry
= lowerd
};
814 struct user_namespace
*from_ns
= loweri
->i_sb
->s_user_ns
;
815 struct user_namespace
*to_ns
= inode
->i_sb
->s_user_ns
;
816 const struct cred
*oldcred
;
819 oldcred
= shiftfs_override_creds(inode
->i_sb
);
820 err
= vfs_getattr(&newpath
, stat
, request_mask
, query_flags
);
821 revert_creds(oldcred
);
826 /* transform the underlying id */
827 stat
->uid
= shift_kuid(from_ns
, to_ns
, stat
->uid
);
828 stat
->gid
= shift_kgid(from_ns
, to_ns
, stat
->gid
);
832 #ifdef CONFIG_SHIFT_FS_POSIX_ACL
835 shift_acl_ids(struct user_namespace
*from
, struct user_namespace
*to
,
836 struct posix_acl
*acl
)
840 for (i
= 0; i
< acl
->a_count
; i
++) {
841 struct posix_acl_entry
*e
= &acl
->a_entries
[i
];
844 e
->e_uid
= shift_kuid(from
, to
, e
->e_uid
);
845 if (!uid_valid(e
->e_uid
))
849 e
->e_gid
= shift_kgid(from
, to
, e
->e_gid
);
850 if (!gid_valid(e
->e_gid
))
859 shift_acl_xattr_ids(struct user_namespace
*from
, struct user_namespace
*to
,
860 void *value
, size_t size
)
862 struct posix_acl_xattr_header
*header
= value
;
863 struct posix_acl_xattr_entry
*entry
= (void *)(header
+ 1), *end
;
870 if (size
< sizeof(struct posix_acl_xattr_header
))
872 if (header
->a_version
!= cpu_to_le32(POSIX_ACL_XATTR_VERSION
))
875 count
= posix_acl_xattr_count(size
);
881 for (end
= entry
+ count
; entry
!= end
; entry
++) {
882 switch(le16_to_cpu(entry
->e_tag
)) {
884 kuid
= make_kuid(&init_user_ns
, le32_to_cpu(entry
->e_id
));
885 kuid
= shift_kuid(from
, to
, kuid
);
886 entry
->e_id
= cpu_to_le32(from_kuid(&init_user_ns
, kuid
));
889 kgid
= make_kgid(from
, le32_to_cpu(entry
->e_id
));
890 kgid
= shift_kgid(from
, to
, kgid
);
891 entry
->e_id
= cpu_to_le32(from_kgid(from
, kgid
));
899 static struct posix_acl
*
900 shiftfs_get_acl(struct inode
*inode
, int type
, bool rcu
)
902 struct inode
*loweri
= inode
->i_private
;
903 const struct cred
*oldcred
;
904 struct posix_acl
*lower_acl
, *acl
= NULL
;
905 struct user_namespace
*from_ns
= loweri
->i_sb
->s_user_ns
;
906 struct user_namespace
*to_ns
= inode
->i_sb
->s_user_ns
;
911 return ERR_PTR(-ECHILD
);
913 if (!IS_POSIXACL(loweri
))
916 oldcred
= shiftfs_override_creds(inode
->i_sb
);
917 lower_acl
= get_acl(loweri
, type
);
918 revert_creds(oldcred
);
920 if (lower_acl
&& !IS_ERR(lower_acl
)) {
921 /* XXX: export posix_acl_clone? */
922 size
= sizeof(struct posix_acl
) +
923 lower_acl
->a_count
* sizeof(struct posix_acl_entry
);
924 acl
= kmemdup(lower_acl
, size
, GFP_KERNEL
);
925 posix_acl_release(lower_acl
);
928 return ERR_PTR(-ENOMEM
);
930 refcount_set(&acl
->a_refcount
, 1);
932 err
= shift_acl_ids(from_ns
, to_ns
, acl
);
943 shiftfs_posix_acl_xattr_get(const struct xattr_handler
*handler
,
944 struct dentry
*dentry
, struct inode
*inode
,
945 const char *name
, void *buffer
, size_t size
)
947 struct inode
*loweri
= inode
->i_private
;
950 ret
= shiftfs_xattr_get(NULL
, dentry
, inode
, handler
->name
,
956 shift_acl_xattr_ids(loweri
->i_sb
->s_user_ns
, inode
->i_sb
->s_user_ns
,
958 inode_unlock(loweri
);
963 shiftfs_posix_acl_xattr_set(const struct xattr_handler
*handler
,
964 struct user_namespace
*ns
,
965 struct dentry
*dentry
, struct inode
*inode
,
966 const char *name
, const void *value
,
967 size_t size
, int flags
)
969 struct inode
*loweri
= inode
->i_private
;
972 if (!IS_POSIXACL(loweri
) || !loweri
->i_op
->set_acl
)
974 if (handler
->flags
== ACL_TYPE_DEFAULT
&& !S_ISDIR(inode
->i_mode
))
975 return value
? -EACCES
: 0;
976 if (!inode_owner_or_capable(ns
, inode
))
980 shift_acl_xattr_ids(inode
->i_sb
->s_user_ns
,
981 loweri
->i_sb
->s_user_ns
,
982 (void *)value
, size
);
983 err
= shiftfs_setxattr(ns
, dentry
, inode
, handler
->name
, value
,
986 err
= shiftfs_removexattr(ns
, dentry
, handler
->name
);
990 shiftfs_copyattr(loweri
, inode
);
995 static const struct xattr_handler
996 shiftfs_posix_acl_access_xattr_handler
= {
997 .name
= XATTR_NAME_POSIX_ACL_ACCESS
,
998 .flags
= ACL_TYPE_ACCESS
,
999 .get
= shiftfs_posix_acl_xattr_get
,
1000 .set
= shiftfs_posix_acl_xattr_set
,
1003 static const struct xattr_handler
1004 shiftfs_posix_acl_default_xattr_handler
= {
1005 .name
= XATTR_NAME_POSIX_ACL_DEFAULT
,
1006 .flags
= ACL_TYPE_DEFAULT
,
1007 .get
= shiftfs_posix_acl_xattr_get
,
1008 .set
= shiftfs_posix_acl_xattr_set
,
1011 #else /* !CONFIG_SHIFT_FS_POSIX_ACL */
1013 #define shiftfs_get_acl NULL
1015 #endif /* CONFIG_SHIFT_FS_POSIX_ACL */
1017 static const struct inode_operations shiftfs_dir_inode_operations
= {
1018 .lookup
= shiftfs_lookup
,
1019 .mkdir
= shiftfs_mkdir
,
1020 .symlink
= shiftfs_symlink
,
1021 .unlink
= shiftfs_unlink
,
1022 .rmdir
= shiftfs_rmdir
,
1023 .rename
= shiftfs_rename
,
1024 .link
= shiftfs_link
,
1025 .setattr
= shiftfs_setattr
,
1026 .create
= shiftfs_create
,
1027 .mknod
= shiftfs_mknod
,
1028 .permission
= shiftfs_permission
,
1029 .getattr
= shiftfs_getattr
,
1030 .listxattr
= shiftfs_listxattr
,
1031 .get_acl
= shiftfs_get_acl
,
1034 static const struct inode_operations shiftfs_file_inode_operations
= {
1035 .fiemap
= shiftfs_fiemap
,
1036 .getattr
= shiftfs_getattr
,
1037 .get_acl
= shiftfs_get_acl
,
1038 .listxattr
= shiftfs_listxattr
,
1039 .permission
= shiftfs_permission
,
1040 .setattr
= shiftfs_setattr
,
1041 .tmpfile
= shiftfs_tmpfile
,
1044 static const struct inode_operations shiftfs_special_inode_operations
= {
1045 .getattr
= shiftfs_getattr
,
1046 .get_acl
= shiftfs_get_acl
,
1047 .listxattr
= shiftfs_listxattr
,
1048 .permission
= shiftfs_permission
,
1049 .setattr
= shiftfs_setattr
,
1052 static const struct inode_operations shiftfs_symlink_inode_operations
= {
1053 .getattr
= shiftfs_getattr
,
1054 .get_link
= shiftfs_get_link
,
1055 .listxattr
= shiftfs_listxattr
,
1056 .setattr
= shiftfs_setattr
,
1059 static struct file
*shiftfs_open_realfile(const struct file
*file
,
1060 struct inode
*realinode
)
1062 struct file
*realfile
;
1063 const struct cred
*old_cred
;
1064 struct inode
*inode
= file_inode(file
);
1065 struct dentry
*lowerd
= file
->f_path
.dentry
->d_fsdata
;
1066 struct shiftfs_super_info
*info
= inode
->i_sb
->s_fs_info
;
1067 struct path realpath
= { .mnt
= info
->mnt
, .dentry
= lowerd
};
1069 old_cred
= shiftfs_override_creds(inode
->i_sb
);
1070 realfile
= open_with_fake_path(&realpath
, file
->f_flags
, realinode
,
1071 info
->creator_cred
);
1072 revert_creds(old_cred
);
1077 #define SHIFTFS_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
1079 static int shiftfs_change_flags(struct file
*file
, unsigned int flags
)
1081 struct inode
*inode
= file_inode(file
);
1084 /* if some flag changed that cannot be changed then something's amiss */
1085 if (WARN_ON((file
->f_flags
^ flags
) & ~SHIFTFS_SETFL_MASK
))
1088 flags
&= SHIFTFS_SETFL_MASK
;
1090 if (((flags
^ file
->f_flags
) & O_APPEND
) && IS_APPEND(inode
))
1093 if (flags
& O_DIRECT
) {
1094 if (!file
->f_mapping
->a_ops
||
1095 !file
->f_mapping
->a_ops
->direct_IO
)
1099 if (file
->f_op
->check_flags
) {
1100 err
= file
->f_op
->check_flags(flags
);
1105 spin_lock(&file
->f_lock
);
1106 file
->f_flags
= (file
->f_flags
& ~SHIFTFS_SETFL_MASK
) | flags
;
1107 spin_unlock(&file
->f_lock
);
1112 static int shiftfs_open(struct inode
*inode
, struct file
*file
)
1114 struct file
*realfile
;
1116 realfile
= shiftfs_open_realfile(file
, inode
->i_private
);
1117 if (IS_ERR(realfile
))
1118 return PTR_ERR(realfile
);
1120 file
->private_data
= realfile
;
1121 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO. */
1122 file
->f_mapping
= realfile
->f_mapping
;
1127 static int shiftfs_dir_open(struct inode
*inode
, struct file
*file
)
1129 struct file
*realfile
;
1130 const struct cred
*oldcred
;
1131 struct dentry
*lowerd
= file
->f_path
.dentry
->d_fsdata
;
1132 struct shiftfs_super_info
*info
= inode
->i_sb
->s_fs_info
;
1133 struct path realpath
= { .mnt
= info
->mnt
, .dentry
= lowerd
};
1135 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1136 realfile
= dentry_open(&realpath
, file
->f_flags
| O_NOATIME
,
1137 info
->creator_cred
);
1138 revert_creds(oldcred
);
1139 if (IS_ERR(realfile
))
1140 return PTR_ERR(realfile
);
1142 file
->private_data
= realfile
;
1147 static int shiftfs_release(struct inode
*inode
, struct file
*file
)
1149 struct file
*realfile
= file
->private_data
;
1157 static int shiftfs_dir_release(struct inode
*inode
, struct file
*file
)
1159 return shiftfs_release(inode
, file
);
1162 static loff_t
shiftfs_dir_llseek(struct file
*file
, loff_t offset
, int whence
)
1164 struct file
*realfile
= file
->private_data
;
1166 return vfs_llseek(realfile
, offset
, whence
);
1169 static loff_t
shiftfs_file_llseek(struct file
*file
, loff_t offset
, int whence
)
1171 struct inode
*realinode
= file_inode(file
)->i_private
;
1173 return generic_file_llseek_size(file
, offset
, whence
,
1174 realinode
->i_sb
->s_maxbytes
,
1175 i_size_read(realinode
));
1178 /* XXX: Need to figure out what to to about atime updates, maybe other
1179 * timestamps too ... ref. ovl_file_accessed() */
1181 static rwf_t
shiftfs_iocb_to_rwf(struct kiocb
*iocb
)
1183 int ifl
= iocb
->ki_flags
;
1186 if (ifl
& IOCB_NOWAIT
)
1187 flags
|= RWF_NOWAIT
;
1188 if (ifl
& IOCB_HIPRI
)
1190 if (ifl
& IOCB_DSYNC
)
1192 if (ifl
& IOCB_SYNC
)
1198 static int shiftfs_real_fdget(const struct file
*file
, struct fd
*lowerfd
)
1200 struct file
*realfile
;
1202 if (file
->f_op
->open
!= shiftfs_open
&&
1203 file
->f_op
->open
!= shiftfs_dir_open
)
1206 realfile
= file
->private_data
;
1208 lowerfd
->file
= realfile
;
1210 /* Did the flags change since open? */
1211 if (unlikely(file
->f_flags
& ~lowerfd
->file
->f_flags
))
1212 return shiftfs_change_flags(lowerfd
->file
, file
->f_flags
);
1217 static ssize_t
shiftfs_read_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
1219 struct file
*file
= iocb
->ki_filp
;
1221 const struct cred
*oldcred
;
1224 if (!iov_iter_count(iter
))
1227 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1231 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1232 ret
= vfs_iter_read(lowerfd
.file
, iter
, &iocb
->ki_pos
,
1233 shiftfs_iocb_to_rwf(iocb
));
1234 revert_creds(oldcred
);
1236 shiftfs_file_accessed(file
);
1242 static ssize_t
shiftfs_write_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
1244 struct file
*file
= iocb
->ki_filp
;
1245 struct inode
*inode
= file_inode(file
);
1247 const struct cred
*oldcred
;
1250 if (!iov_iter_count(iter
))
1255 shiftfs_copyattr(inode
->i_private
, inode
);
1256 ret
= file_remove_privs(file
);
1260 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1264 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1265 file_start_write(lowerfd
.file
);
1266 ret
= vfs_iter_write(lowerfd
.file
, iter
, &iocb
->ki_pos
,
1267 shiftfs_iocb_to_rwf(iocb
));
1268 file_end_write(lowerfd
.file
);
1269 revert_creds(oldcred
);
1272 shiftfs_copyattr(inode
->i_private
, inode
);
1277 inode_unlock(inode
);
1281 static int shiftfs_fsync(struct file
*file
, loff_t start
, loff_t end
,
1285 const struct cred
*oldcred
;
1288 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1292 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1293 ret
= vfs_fsync_range(lowerfd
.file
, start
, end
, datasync
);
1294 revert_creds(oldcred
);
1300 static int shiftfs_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1302 struct file
*realfile
= file
->private_data
;
1303 const struct cred
*oldcred
;
1306 if (!realfile
->f_op
->mmap
)
1309 if (WARN_ON(file
!= vma
->vm_file
))
1312 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1313 vma
->vm_file
= get_file(realfile
);
1314 ret
= call_mmap(vma
->vm_file
, vma
);
1315 revert_creds(oldcred
);
1317 shiftfs_file_accessed(file
);
1321 * Drop refcount from new vm_file value and restore original
1324 vma
->vm_file
= file
;
1327 /* Drop refcount from previous vm_file value */
1334 static long shiftfs_fallocate(struct file
*file
, int mode
, loff_t offset
,
1337 struct inode
*inode
= file_inode(file
);
1338 struct inode
*loweri
= inode
->i_private
;
1340 const struct cred
*oldcred
;
1343 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1347 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1348 ret
= vfs_fallocate(lowerfd
.file
, mode
, offset
, len
);
1349 revert_creds(oldcred
);
1352 shiftfs_copyattr(loweri
, inode
);
1358 static int shiftfs_fadvise(struct file
*file
, loff_t offset
, loff_t len
,
1362 const struct cred
*oldcred
;
1365 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1369 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1370 ret
= vfs_fadvise(lowerfd
.file
, offset
, len
, advice
);
1371 revert_creds(oldcred
);
1377 static int shiftfs_override_ioctl_creds(int cmd
, const struct super_block
*sb
,
1378 const struct cred
**oldcred
,
1379 struct cred
**newcred
)
1381 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
1382 kuid_t fsuid
= current_fsuid();
1383 kgid_t fsgid
= current_fsgid();
1385 *oldcred
= shiftfs_override_creds(sb
);
1387 *newcred
= prepare_creds();
1389 revert_creds(*oldcred
);
1393 (*newcred
)->fsuid
= shift_kuid(sb
->s_user_ns
, sbinfo
->userns
, fsuid
);
1394 (*newcred
)->fsgid
= shift_kgid(sb
->s_user_ns
, sbinfo
->userns
, fsgid
);
1396 /* clear all caps to prevent bypassing capable() checks */
1397 cap_clear((*newcred
)->cap_bset
);
1398 cap_clear((*newcred
)->cap_effective
);
1399 cap_clear((*newcred
)->cap_inheritable
);
1400 cap_clear((*newcred
)->cap_permitted
);
1402 if (cmd
== BTRFS_IOC_SNAP_DESTROY
) {
1403 kuid_t kuid_root
= make_kuid(sb
->s_user_ns
, 0);
1405 * Allow the root user in the container to remove subvolumes
1408 if (uid_valid(kuid_root
) && uid_eq(fsuid
, kuid_root
))
1409 cap_raise((*newcred
)->cap_effective
, CAP_DAC_OVERRIDE
);
1412 put_cred(override_creds(*newcred
));
1416 static inline void shiftfs_revert_ioctl_creds(const struct cred
*oldcred
,
1417 struct cred
*newcred
)
1419 return shiftfs_revert_object_creds(oldcred
, newcred
);
1422 static inline bool is_btrfs_snap_ioctl(int cmd
)
1424 if ((cmd
== BTRFS_IOC_SNAP_CREATE
) || (cmd
== BTRFS_IOC_SNAP_CREATE_V2
))
1430 static int shiftfs_btrfs_ioctl_fd_restore(int cmd
, int fd
, void __user
*arg
,
1431 struct btrfs_ioctl_vol_args
*v1
,
1432 struct btrfs_ioctl_vol_args_v2
*v2
)
1436 if (!is_btrfs_snap_ioctl(cmd
))
1439 if (cmd
== BTRFS_IOC_SNAP_CREATE
)
1440 ret
= copy_to_user(arg
, v1
, sizeof(*v1
));
1442 ret
= copy_to_user(arg
, v2
, sizeof(*v2
));
1448 return ret
? -EFAULT
: 0;
1451 static int shiftfs_btrfs_ioctl_fd_replace(int cmd
, void __user
*arg
,
1452 struct btrfs_ioctl_vol_args
**b1
,
1453 struct btrfs_ioctl_vol_args_v2
**b2
,
1459 struct btrfs_ioctl_vol_args
*v1
= NULL
;
1460 struct btrfs_ioctl_vol_args_v2
*v2
= NULL
;
1465 if (!is_btrfs_snap_ioctl(cmd
))
1468 if (cmd
== BTRFS_IOC_SNAP_CREATE
) {
1469 v1
= memdup_user(arg
, sizeof(*v1
));
1474 v2
= memdup_user(arg
, sizeof(*v2
));
1486 ret
= shiftfs_real_fdget(src
.file
, &lfd
);
1493 * shiftfs_real_fdget() does not take a reference to lfd.file, so
1494 * take a reference here to offset the one which will be put by
1495 * close_fd(), and make sure that reference is put on fdput(lfd).
1498 lfd
.flags
|= FDPUT_FPUT
;
1501 *newfd
= get_unused_fd_flags(lfd
.file
->f_flags
);
1508 fd_install(*newfd
, lfd
.file
);
1510 if (cmd
== BTRFS_IOC_SNAP_CREATE
) {
1512 ret
= copy_to_user(arg
, v1
, sizeof(*v1
));
1516 ret
= copy_to_user(arg
, v2
, sizeof(*v2
));
1524 shiftfs_btrfs_ioctl_fd_restore(cmd
, *newfd
, arg
, v1
, v2
);
1537 static long shiftfs_real_ioctl(struct file
*file
, unsigned int cmd
,
1541 struct cred
*newcred
;
1542 const struct cred
*oldcred
;
1544 long err
= 0, ret
= 0;
1545 void __user
*argp
= (void __user
*)arg
;
1546 struct super_block
*sb
= file
->f_path
.dentry
->d_sb
;
1547 struct btrfs_ioctl_vol_args
*btrfs_v1
= NULL
;
1548 struct btrfs_ioctl_vol_args_v2
*btrfs_v2
= NULL
;
1550 ret
= shiftfs_btrfs_ioctl_fd_replace(cmd
, argp
, &btrfs_v1
, &btrfs_v2
,
1555 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1559 ret
= shiftfs_override_ioctl_creds(cmd
, sb
, &oldcred
, &newcred
);
1563 ret
= vfs_ioctl(lowerfd
.file
, cmd
, arg
);
1565 shiftfs_revert_ioctl_creds(oldcred
, newcred
);
1567 shiftfs_copyattr(file_inode(lowerfd
.file
), file_inode(file
));
1568 shiftfs_copyflags(file_inode(lowerfd
.file
), file_inode(file
));
1574 err
= shiftfs_btrfs_ioctl_fd_restore(cmd
, newfd
, argp
,
1575 btrfs_v1
, btrfs_v2
);
1582 static bool in_ioctl_whitelist(int flag
, unsigned long arg
)
1584 void __user
*argp
= (void __user
*)arg
;
1588 case BTRFS_IOC_FS_INFO
:
1590 case BTRFS_IOC_SNAP_CREATE
:
1592 case BTRFS_IOC_SNAP_CREATE_V2
:
1594 case BTRFS_IOC_SUBVOL_CREATE
:
1596 case BTRFS_IOC_SUBVOL_CREATE_V2
:
1598 case BTRFS_IOC_SUBVOL_GETFLAGS
:
1600 case BTRFS_IOC_SUBVOL_SETFLAGS
:
1601 if (copy_from_user(&flags
, argp
, sizeof(flags
)))
1604 if (flags
& ~BTRFS_SUBVOL_RDONLY
)
1608 case BTRFS_IOC_SNAP_DESTROY
:
1615 static long shiftfs_ioctl(struct file
*file
, unsigned int cmd
,
1619 case FS_IOC_GETVERSION
:
1621 case FS_IOC_GETFLAGS
:
1623 case FS_IOC_SETFLAGS
:
1626 if (!in_ioctl_whitelist(cmd
, arg
) ||
1627 !shiftfs_passthrough_ioctls(file
->f_path
.dentry
->d_sb
->s_fs_info
))
1631 return shiftfs_real_ioctl(file
, cmd
, arg
);
1634 static long shiftfs_compat_ioctl(struct file
*file
, unsigned int cmd
,
1638 case FS_IOC32_GETVERSION
:
1640 case FS_IOC32_GETFLAGS
:
1642 case FS_IOC32_SETFLAGS
:
1645 if (!in_ioctl_whitelist(cmd
, arg
) ||
1646 !shiftfs_passthrough_ioctls(file
->f_path
.dentry
->d_sb
->s_fs_info
))
1647 return -ENOIOCTLCMD
;
1650 return shiftfs_real_ioctl(file
, cmd
, arg
);
1653 enum shiftfs_copyop
{
1659 static ssize_t
shiftfs_copyfile(struct file
*file_in
, loff_t pos_in
,
1660 struct file
*file_out
, loff_t pos_out
, u64 len
,
1661 unsigned int flags
, enum shiftfs_copyop op
)
1664 struct fd real_in
, real_out
;
1665 const struct cred
*oldcred
;
1666 struct inode
*inode_out
= file_inode(file_out
);
1667 struct inode
*loweri
= inode_out
->i_private
;
1669 ret
= shiftfs_real_fdget(file_out
, &real_out
);
1673 ret
= shiftfs_real_fdget(file_in
, &real_in
);
1679 oldcred
= shiftfs_override_creds(inode_out
->i_sb
);
1682 ret
= vfs_copy_file_range(real_in
.file
, pos_in
, real_out
.file
,
1683 pos_out
, len
, flags
);
1687 ret
= vfs_clone_file_range(real_in
.file
, pos_in
, real_out
.file
,
1688 pos_out
, len
, flags
);
1691 case SHIFTFS_DEDUPE
:
1692 ret
= vfs_dedupe_file_range_one(real_in
.file
, pos_in
,
1693 real_out
.file
, pos_out
, len
,
1697 revert_creds(oldcred
);
1700 shiftfs_copyattr(loweri
, inode_out
);
1708 static ssize_t
shiftfs_copy_file_range(struct file
*file_in
, loff_t pos_in
,
1709 struct file
*file_out
, loff_t pos_out
,
1710 size_t len
, unsigned int flags
)
1712 return shiftfs_copyfile(file_in
, pos_in
, file_out
, pos_out
, len
, flags
,
1716 static loff_t
shiftfs_remap_file_range(struct file
*file_in
, loff_t pos_in
,
1717 struct file
*file_out
, loff_t pos_out
,
1718 loff_t len
, unsigned int remap_flags
)
1720 enum shiftfs_copyop op
;
1722 if (remap_flags
& ~(REMAP_FILE_DEDUP
| REMAP_FILE_ADVISORY
))
1725 if (remap_flags
& REMAP_FILE_DEDUP
)
1726 op
= SHIFTFS_DEDUPE
;
1730 return shiftfs_copyfile(file_in
, pos_in
, file_out
, pos_out
, len
,
1734 static int shiftfs_iterate_shared(struct file
*file
, struct dir_context
*ctx
)
1736 const struct cred
*oldcred
;
1738 struct file
*realfile
= file
->private_data
;
1740 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1741 err
= iterate_dir(realfile
, ctx
);
1742 revert_creds(oldcred
);
1747 const struct file_operations shiftfs_file_operations
= {
1748 .open
= shiftfs_open
,
1749 .release
= shiftfs_release
,
1750 .llseek
= shiftfs_file_llseek
,
1751 .read_iter
= shiftfs_read_iter
,
1752 .write_iter
= shiftfs_write_iter
,
1753 .fsync
= shiftfs_fsync
,
1754 .mmap
= shiftfs_mmap
,
1755 .fallocate
= shiftfs_fallocate
,
1756 .fadvise
= shiftfs_fadvise
,
1757 .unlocked_ioctl
= shiftfs_ioctl
,
1758 .compat_ioctl
= shiftfs_compat_ioctl
,
1759 .copy_file_range
= shiftfs_copy_file_range
,
1760 .remap_file_range
= shiftfs_remap_file_range
,
1761 .splice_read
= generic_file_splice_read
,
1762 .splice_write
= iter_file_splice_write
,
1765 const struct file_operations shiftfs_dir_operations
= {
1766 .open
= shiftfs_dir_open
,
1767 .release
= shiftfs_dir_release
,
1768 .compat_ioctl
= shiftfs_compat_ioctl
,
1769 .fsync
= shiftfs_fsync
,
1770 .iterate_shared
= shiftfs_iterate_shared
,
1771 .llseek
= shiftfs_dir_llseek
,
1772 .read
= generic_read_dir
,
1773 .unlocked_ioctl
= shiftfs_ioctl
,
1776 static const struct address_space_operations shiftfs_aops
= {
1777 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
1778 .direct_IO
= noop_direct_IO
,
1781 static void shiftfs_fill_inode(struct inode
*inode
, unsigned long ino
,
1782 umode_t mode
, dev_t dev
, struct dentry
*dentry
)
1784 struct inode
*loweri
;
1787 inode
->i_flags
|= S_NOCMTIME
;
1790 inode
->i_mode
= mode
;
1791 switch (mode
& S_IFMT
) {
1793 inode
->i_op
= &shiftfs_dir_inode_operations
;
1794 inode
->i_fop
= &shiftfs_dir_operations
;
1797 inode
->i_op
= &shiftfs_symlink_inode_operations
;
1800 inode
->i_op
= &shiftfs_file_inode_operations
;
1801 inode
->i_fop
= &shiftfs_file_operations
;
1802 inode
->i_mapping
->a_ops
= &shiftfs_aops
;
1805 inode
->i_op
= &shiftfs_special_inode_operations
;
1806 init_special_inode(inode
, mode
, dev
);
1813 loweri
= dentry
->d_inode
;
1814 if (!loweri
->i_op
->get_link
)
1815 inode
->i_opflags
|= IOP_NOFOLLOW
;
1817 shiftfs_copyattr(loweri
, inode
);
1818 shiftfs_copyflags(loweri
, inode
);
1819 set_nlink(inode
, loweri
->i_nlink
);
1822 static int shiftfs_show_options(struct seq_file
*m
, struct dentry
*dentry
)
1824 struct super_block
*sb
= dentry
->d_sb
;
1825 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
1828 seq_show_option(m
, "mark", NULL
);
1830 if (sbinfo
->passthrough
)
1831 seq_printf(m
, ",passthrough=%u", sbinfo
->passthrough
);
1836 static int shiftfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
1838 struct super_block
*sb
= dentry
->d_sb
;
1839 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
1840 struct dentry
*root
= sb
->s_root
;
1841 struct dentry
*realroot
= root
->d_fsdata
;
1842 struct path realpath
= { .mnt
= sbinfo
->mnt
, .dentry
= realroot
};
1845 err
= vfs_statfs(&realpath
, buf
);
1849 if (!shiftfs_passthrough_statfs(sbinfo
))
1850 buf
->f_type
= sb
->s_magic
;
1855 static void shiftfs_evict_inode(struct inode
*inode
)
1857 struct inode
*loweri
= inode
->i_private
;
1865 static void shiftfs_put_super(struct super_block
*sb
)
1867 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
1870 mntput(sbinfo
->mnt
);
1871 put_cred(sbinfo
->creator_cred
);
1876 static const struct xattr_handler shiftfs_xattr_handler
= {
1878 .get
= shiftfs_xattr_get
,
1879 .set
= shiftfs_xattr_set
,
1882 const struct xattr_handler
*shiftfs_xattr_handlers
[] = {
1883 #ifdef CONFIG_SHIFT_FS_POSIX_ACL
1884 &shiftfs_posix_acl_access_xattr_handler
,
1885 &shiftfs_posix_acl_default_xattr_handler
,
1887 &shiftfs_xattr_handler
,
1891 static inline bool passthrough_is_subset(int old_flags
, int new_flags
)
1893 if ((new_flags
& old_flags
) != new_flags
)
1899 static int shiftfs_super_check_flags(unsigned long old_flags
,
1900 unsigned long new_flags
)
1902 if ((old_flags
& SB_RDONLY
) && !(new_flags
& SB_RDONLY
))
1905 if ((old_flags
& SB_NOSUID
) && !(new_flags
& SB_NOSUID
))
1908 if ((old_flags
& SB_NODEV
) && !(new_flags
& SB_NODEV
))
1911 if ((old_flags
& SB_NOEXEC
) && !(new_flags
& SB_NOEXEC
))
1914 if ((old_flags
& SB_NOATIME
) && !(new_flags
& SB_NOATIME
))
1917 if ((old_flags
& SB_NODIRATIME
) && !(new_flags
& SB_NODIRATIME
))
1920 if (!(old_flags
& SB_POSIXACL
) && (new_flags
& SB_POSIXACL
))
1926 static int shiftfs_remount(struct super_block
*sb
, int *flags
, char *data
)
1929 struct shiftfs_super_info
new = {};
1930 struct shiftfs_super_info
*info
= sb
->s_fs_info
;
1932 err
= shiftfs_parse_mount_options(&new, data
);
1936 err
= shiftfs_super_check_flags(sb
->s_flags
, *flags
);
1940 /* Mark mount option cannot be changed. */
1941 if (info
->mark
|| (info
->mark
!= new.mark
))
1944 if (info
->passthrough
!= new.passthrough
) {
1945 /* Don't allow exceeding passthrough options of mark mount. */
1946 if (!passthrough_is_subset(info
->passthrough_mark
,
1950 info
->passthrough
= new.passthrough
;
1956 static const struct super_operations shiftfs_super_ops
= {
1957 .put_super
= shiftfs_put_super
,
1958 .show_options
= shiftfs_show_options
,
1959 .statfs
= shiftfs_statfs
,
1960 .remount_fs
= shiftfs_remount
,
1961 .evict_inode
= shiftfs_evict_inode
,
1964 struct shiftfs_data
{
1969 static void shiftfs_super_force_flags(struct super_block
*sb
,
1970 unsigned long lower_flags
)
1972 sb
->s_flags
|= lower_flags
& (SB_RDONLY
| SB_NOSUID
| SB_NODEV
|
1973 SB_NOEXEC
| SB_NOATIME
| SB_NODIRATIME
);
1975 if (!(lower_flags
& SB_POSIXACL
))
1976 sb
->s_flags
&= ~SB_POSIXACL
;
1979 static int shiftfs_fill_super(struct super_block
*sb
, void *raw_data
,
1983 struct path path
= {};
1984 struct shiftfs_super_info
*sbinfo_mp
;
1986 struct inode
*inode
= NULL
;
1987 struct dentry
*dentry
= NULL
;
1988 struct shiftfs_data
*data
= raw_data
;
1989 struct shiftfs_super_info
*sbinfo
= NULL
;
1994 sb
->s_fs_info
= kzalloc(sizeof(*sbinfo
), GFP_KERNEL
);
1997 sbinfo
= sb
->s_fs_info
;
1999 err
= shiftfs_parse_mount_options(sbinfo
, data
->data
);
2003 /* to mount a mark, must be userns admin */
2004 if (!sbinfo
->mark
&& !ns_capable(current_user_ns(), CAP_SYS_ADMIN
))
2007 name
= kstrdup(data
->path
, GFP_KERNEL
);
2011 err
= kern_path(name
, LOOKUP_FOLLOW
, &path
);
2015 if (!S_ISDIR(path
.dentry
->d_inode
->i_mode
)) {
2020 sb
->s_flags
|= SB_POSIXACL
;
2023 struct cred
*cred_tmp
;
2024 struct super_block
*lower_sb
= path
.mnt
->mnt_sb
;
2026 /* to mark a mount point, must root wrt lower s_user_ns */
2027 if (!ns_capable(lower_sb
->s_user_ns
, CAP_SYS_ADMIN
)) {
2033 * this part is visible unshifted, so make sure no
2034 * executables that could be used to give suid
2037 sb
->s_iflags
= SB_I_NOEXEC
;
2039 shiftfs_super_force_flags(sb
, lower_sb
->s_flags
);
2042 * Handle nesting of shiftfs mounts by referring this mark
2043 * mount back to the original mark mount. This is more
2044 * efficient and alleviates concerns about stack depth.
2046 if (lower_sb
->s_magic
== SHIFTFS_MAGIC
) {
2047 sbinfo_mp
= lower_sb
->s_fs_info
;
2049 /* Doesn't make sense to mark a mark mount */
2050 if (sbinfo_mp
->mark
) {
2055 if (!passthrough_is_subset(sbinfo_mp
->passthrough
,
2056 sbinfo
->passthrough
)) {
2061 sbinfo
->mnt
= mntget(sbinfo_mp
->mnt
);
2062 dentry
= dget(path
.dentry
->d_fsdata
);
2064 * Copy up the passthrough mount options from the
2065 * parent mark mountpoint.
2067 sbinfo
->passthrough_mark
= sbinfo_mp
->passthrough_mark
;
2068 sbinfo
->creator_cred
= get_cred(sbinfo_mp
->creator_cred
);
2070 sbinfo
->mnt
= mntget(path
.mnt
);
2071 dentry
= dget(path
.dentry
);
2073 * For a new mark passthrough_mark and passthrough
2076 sbinfo
->passthrough_mark
= sbinfo
->passthrough
;
2078 cred_tmp
= prepare_creds();
2083 /* Don't override disk quota limits or use reserved space. */
2084 cap_lower(cred_tmp
->cap_effective
, CAP_SYS_RESOURCE
);
2085 sbinfo
->creator_cred
= cred_tmp
;
2089 * This leg executes if we're admin capable in the namespace,
2090 * so be very careful.
2093 if (path
.dentry
->d_sb
->s_magic
!= SHIFTFS_MAGIC
)
2096 sbinfo_mp
= path
.dentry
->d_sb
->s_fs_info
;
2097 if (!sbinfo_mp
->mark
)
2100 if (!passthrough_is_subset(sbinfo_mp
->passthrough
,
2101 sbinfo
->passthrough
))
2104 sbinfo
->mnt
= mntget(sbinfo_mp
->mnt
);
2105 sbinfo
->creator_cred
= get_cred(sbinfo_mp
->creator_cred
);
2106 dentry
= dget(path
.dentry
->d_fsdata
);
2108 * Copy up passthrough settings from mark mountpoint so we can
2109 * verify when the overlay wants to remount with different
2110 * passthrough settings.
2112 sbinfo
->passthrough_mark
= sbinfo_mp
->passthrough
;
2113 shiftfs_super_force_flags(sb
, path
.mnt
->mnt_sb
->s_flags
);
2116 sb
->s_stack_depth
= dentry
->d_sb
->s_stack_depth
+ 1;
2117 if (sb
->s_stack_depth
> FILESYSTEM_MAX_STACK_DEPTH
) {
2118 printk(KERN_ERR
"shiftfs: maximum stacking depth exceeded\n");
2123 inode
= new_inode(sb
);
2128 shiftfs_fill_inode(inode
, dentry
->d_inode
->i_ino
, S_IFDIR
, 0, dentry
);
2130 ihold(dentry
->d_inode
);
2131 inode
->i_private
= dentry
->d_inode
;
2133 sb
->s_magic
= SHIFTFS_MAGIC
;
2134 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
2135 sb
->s_op
= &shiftfs_super_ops
;
2136 sb
->s_xattr
= shiftfs_xattr_handlers
;
2137 sb
->s_d_op
= &shiftfs_dentry_ops
;
2138 sb
->s_root
= d_make_root(inode
);
2144 sb
->s_root
->d_fsdata
= dentry
;
2145 sbinfo
->userns
= get_user_ns(dentry
->d_sb
->s_user_ns
);
2146 shiftfs_copyattr(dentry
->d_inode
, sb
->s_root
->d_inode
);
2162 static struct dentry
*shiftfs_mount(struct file_system_type
*fs_type
,
2163 int flags
, const char *dev_name
, void *data
)
2165 struct shiftfs_data d
= { data
, dev_name
};
2167 return mount_nodev(fs_type
, flags
, &d
, shiftfs_fill_super
);
2170 static struct file_system_type shiftfs_type
= {
2171 .owner
= THIS_MODULE
,
2173 .mount
= shiftfs_mount
,
2174 .kill_sb
= kill_anon_super
,
2175 .fs_flags
= FS_USERNS_MOUNT
,
2178 static int __init
shiftfs_init(void)
2180 return register_filesystem(&shiftfs_type
);
2183 static void __exit
shiftfs_exit(void)
2185 unregister_filesystem(&shiftfs_type
);
2188 MODULE_ALIAS_FS("shiftfs");
2189 MODULE_AUTHOR("James Bottomley");
2190 MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>");
2191 MODULE_AUTHOR("Christian Brauner <christian.brauner@ubuntu.com>");
2192 MODULE_DESCRIPTION("id shifting filesystem");
2193 MODULE_LICENSE("GPL v2");
2194 module_init(shiftfs_init
)
2195 module_exit(shiftfs_exit
)