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 dentry
*dentry
, struct inode
*inode
,
312 const char *name
, const void *value
,
313 size_t size
, int flags
)
315 struct dentry
*lowerd
= dentry
->d_fsdata
;
317 const struct cred
*oldcred
;
319 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
320 err
= vfs_setxattr(lowerd
, name
, value
, size
, flags
);
321 revert_creds(oldcred
);
323 shiftfs_copyattr(lowerd
->d_inode
, inode
);
328 static int shiftfs_xattr_get(const struct xattr_handler
*handler
,
329 struct dentry
*dentry
, struct inode
*inode
,
330 const char *name
, void *value
, size_t size
)
332 struct dentry
*lowerd
= dentry
->d_fsdata
;
334 const struct cred
*oldcred
;
336 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
337 err
= vfs_getxattr(lowerd
, name
, value
, size
);
338 revert_creds(oldcred
);
343 static ssize_t
shiftfs_listxattr(struct dentry
*dentry
, char *list
,
346 struct dentry
*lowerd
= dentry
->d_fsdata
;
348 const struct cred
*oldcred
;
350 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
351 err
= vfs_listxattr(lowerd
, list
, size
);
352 revert_creds(oldcred
);
357 static int shiftfs_removexattr(struct dentry
*dentry
, const char *name
)
359 struct dentry
*lowerd
= dentry
->d_fsdata
;
361 const struct cred
*oldcred
;
363 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
364 err
= vfs_removexattr(lowerd
, name
);
365 revert_creds(oldcred
);
368 shiftfs_copyattr(lowerd
->d_inode
, d_inode(dentry
));
373 static int shiftfs_xattr_set(const struct xattr_handler
*handler
,
374 struct dentry
*dentry
, struct inode
*inode
,
375 const char *name
, const void *value
, size_t size
,
379 return shiftfs_removexattr(dentry
, name
);
380 return shiftfs_setxattr(dentry
, inode
, name
, value
, size
, flags
);
383 static int shiftfs_inode_test(struct inode
*inode
, void *data
)
385 return inode
->i_private
== data
;
388 static int shiftfs_inode_set(struct inode
*inode
, void *data
)
390 inode
->i_private
= data
;
394 static int shiftfs_create_object(struct inode
*diri
, struct dentry
*dentry
,
395 umode_t mode
, const char *symlink
,
396 struct dentry
*hardlink
, bool excl
)
399 const struct cred
*oldcred
;
400 struct cred
*newcred
;
401 void *loweri_iop_ptr
= NULL
;
402 umode_t modei
= mode
;
403 struct super_block
*dir_sb
= diri
->i_sb
;
404 struct dentry
*lowerd_new
= dentry
->d_fsdata
;
405 struct inode
*inode
= NULL
, *loweri_dir
= diri
->i_private
;
406 const struct inode_operations
*loweri_dir_iop
= loweri_dir
->i_op
;
407 struct dentry
*lowerd_link
= NULL
;
410 loweri_iop_ptr
= loweri_dir_iop
->link
;
412 switch (mode
& S_IFMT
) {
414 loweri_iop_ptr
= loweri_dir_iop
->mkdir
;
417 loweri_iop_ptr
= loweri_dir_iop
->create
;
420 loweri_iop_ptr
= loweri_dir_iop
->symlink
;
425 loweri_iop_ptr
= loweri_dir_iop
->mknod
;
429 if (!loweri_iop_ptr
) {
434 inode_lock_nested(loweri_dir
, I_MUTEX_PARENT
);
437 inode
= new_inode(dir_sb
);
444 * new_inode() will have added the new inode to the super
445 * block's list of inodes. Further below we will call
446 * inode_insert5() Which would perform the same operation again
447 * thereby corrupting the list. To avoid this raise I_CREATING
448 * in i_state which will cause inode_insert5() to skip this
449 * step. I_CREATING will be cleared by d_instantiate_new()
452 spin_lock(&inode
->i_lock
);
453 inode
->i_state
|= I_CREATING
;
454 spin_unlock(&inode
->i_lock
);
456 inode_init_owner(inode
, diri
, mode
);
457 modei
= inode
->i_mode
;
460 err
= shiftfs_override_object_creds(dentry
->d_sb
, &oldcred
, &newcred
,
461 dentry
, modei
, hardlink
!= NULL
);
466 lowerd_link
= hardlink
->d_fsdata
;
467 err
= vfs_link(lowerd_link
, loweri_dir
, lowerd_new
, NULL
);
469 switch (modei
& S_IFMT
) {
471 err
= vfs_mkdir(loweri_dir
, lowerd_new
, modei
);
474 err
= vfs_create(loweri_dir
, lowerd_new
, modei
, excl
);
477 err
= vfs_symlink(loweri_dir
, lowerd_new
, symlink
);
482 err
= vfs_mknod(loweri_dir
, lowerd_new
, modei
, 0);
490 shiftfs_revert_object_creds(oldcred
, newcred
);
492 if (!err
&& WARN_ON(!lowerd_new
->d_inode
))
498 inode
= d_inode(hardlink
);
501 /* copy up times from lower inode */
502 shiftfs_copyattr(d_inode(lowerd_link
), inode
);
503 set_nlink(d_inode(hardlink
), d_inode(lowerd_link
)->i_nlink
);
504 d_instantiate(dentry
, inode
);
506 struct inode
*inode_tmp
;
507 struct inode
*loweri_new
= d_inode(lowerd_new
);
509 inode_tmp
= inode_insert5(inode
, (unsigned long)loweri_new
,
510 shiftfs_inode_test
, shiftfs_inode_set
,
512 if (unlikely(inode_tmp
!= inode
)) {
513 pr_err_ratelimited("shiftfs: newly created inode found in cache\n");
520 shiftfs_fill_inode(inode
, loweri_new
->i_ino
, loweri_new
->i_mode
,
522 d_instantiate_new(dentry
, inode
);
525 shiftfs_copyattr(loweri_dir
, diri
);
526 if (loweri_iop_ptr
== loweri_dir_iop
->mkdir
)
527 set_nlink(diri
, loweri_dir
->i_nlink
);
533 inode_unlock(loweri_dir
);
538 static int shiftfs_create(struct inode
*dir
, struct dentry
*dentry
,
539 umode_t mode
, bool excl
)
543 return shiftfs_create_object(dir
, dentry
, mode
, NULL
, NULL
, excl
);
546 static int shiftfs_mkdir(struct inode
*dir
, struct dentry
*dentry
,
551 return shiftfs_create_object(dir
, dentry
, mode
, NULL
, NULL
, false);
554 static int shiftfs_link(struct dentry
*hardlink
, struct inode
*dir
,
555 struct dentry
*dentry
)
557 return shiftfs_create_object(dir
, dentry
, 0, NULL
, hardlink
, false);
560 static int shiftfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
563 if (!S_ISFIFO(mode
) && !S_ISSOCK(mode
))
566 return shiftfs_create_object(dir
, dentry
, mode
, NULL
, NULL
, false);
569 static int shiftfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
572 return shiftfs_create_object(dir
, dentry
, S_IFLNK
, symlink
, NULL
, false);
575 static int shiftfs_rm(struct inode
*dir
, struct dentry
*dentry
, bool rmdir
)
577 struct dentry
*lowerd
= dentry
->d_fsdata
;
578 struct inode
*loweri
= dir
->i_private
;
579 struct inode
*inode
= d_inode(dentry
);
581 const struct cred
*oldcred
;
584 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
585 inode_lock_nested(loweri
, I_MUTEX_PARENT
);
587 err
= vfs_rmdir(loweri
, lowerd
);
589 err
= vfs_unlink(loweri
, lowerd
, NULL
);
590 revert_creds(oldcred
);
600 inode_unlock(loweri
);
602 shiftfs_copyattr(loweri
, dir
);
608 static int shiftfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
610 return shiftfs_rm(dir
, dentry
, false);
613 static int shiftfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
615 return shiftfs_rm(dir
, dentry
, true);
618 static int shiftfs_rename(struct inode
*olddir
, struct dentry
*old
,
619 struct inode
*newdir
, struct dentry
*new,
622 struct dentry
*lowerd_dir_old
= old
->d_parent
->d_fsdata
,
623 *lowerd_dir_new
= new->d_parent
->d_fsdata
,
624 *lowerd_old
= old
->d_fsdata
, *lowerd_new
= new->d_fsdata
,
626 struct inode
*loweri_dir_old
= lowerd_dir_old
->d_inode
,
627 *loweri_dir_new
= lowerd_dir_new
->d_inode
;
629 const struct cred
*oldcred
;
631 trapd
= lock_rename(lowerd_dir_new
, lowerd_dir_old
);
633 if (trapd
== lowerd_old
|| trapd
== lowerd_new
)
636 oldcred
= shiftfs_override_creds(old
->d_sb
);
637 err
= vfs_rename(loweri_dir_old
, lowerd_old
, loweri_dir_new
, lowerd_new
,
639 revert_creds(oldcred
);
641 shiftfs_copyattr(loweri_dir_old
, olddir
);
642 shiftfs_copyattr(loweri_dir_new
, newdir
);
645 unlock_rename(lowerd_dir_new
, lowerd_dir_old
);
650 static struct dentry
*shiftfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
655 const struct cred
*oldcred
;
656 struct dentry
*lowerd
= dentry
->d_parent
->d_fsdata
;
657 struct inode
*inode
= NULL
, *loweri
= lowerd
->d_inode
;
660 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
661 new = lookup_one_len(dentry
->d_name
.name
, lowerd
, dentry
->d_name
.len
);
662 revert_creds(oldcred
);
663 inode_unlock(loweri
);
668 dentry
->d_fsdata
= new;
674 inode
= iget5_locked(dentry
->d_sb
, (unsigned long)newi
,
675 shiftfs_inode_test
, shiftfs_inode_set
, newi
);
678 return ERR_PTR(-ENOMEM
);
680 if (inode
->i_state
& I_NEW
) {
682 * inode->i_private set by shiftfs_inode_set(), but we still
683 * need to take a reference
686 shiftfs_fill_inode(inode
, newi
->i_ino
, newi
->i_mode
, 0, new);
687 unlock_new_inode(inode
);
691 return d_splice_alias(inode
, dentry
);
694 static int shiftfs_permission(struct inode
*inode
, int mask
)
697 const struct cred
*oldcred
;
698 struct inode
*loweri
= inode
->i_private
;
701 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
705 err
= generic_permission(inode
, mask
);
709 oldcred
= shiftfs_override_creds(inode
->i_sb
);
710 err
= inode_permission(loweri
, mask
);
711 revert_creds(oldcred
);
716 static int shiftfs_fiemap(struct inode
*inode
,
717 struct fiemap_extent_info
*fieinfo
, u64 start
,
721 const struct cred
*oldcred
;
722 struct inode
*loweri
= inode
->i_private
;
724 if (!loweri
->i_op
->fiemap
)
727 oldcred
= shiftfs_override_creds(inode
->i_sb
);
728 if (fieinfo
->fi_flags
& FIEMAP_FLAG_SYNC
)
729 filemap_write_and_wait(loweri
->i_mapping
);
730 err
= loweri
->i_op
->fiemap(loweri
, fieinfo
, start
, len
);
731 revert_creds(oldcred
);
736 static int shiftfs_tmpfile(struct inode
*dir
, struct dentry
*dentry
,
740 const struct cred
*oldcred
;
741 struct dentry
*lowerd
= dentry
->d_fsdata
;
742 struct inode
*loweri
= dir
->i_private
;
744 if (!loweri
->i_op
->tmpfile
)
747 oldcred
= shiftfs_override_creds(dir
->i_sb
);
748 err
= loweri
->i_op
->tmpfile(loweri
, lowerd
, mode
);
749 revert_creds(oldcred
);
754 static int shiftfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
756 struct dentry
*lowerd
= dentry
->d_fsdata
;
757 struct inode
*loweri
= lowerd
->d_inode
;
758 struct iattr newattr
;
759 const struct cred
*oldcred
;
760 struct super_block
*sb
= dentry
->d_sb
;
761 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
764 err
= setattr_prepare(dentry
, attr
);
769 newattr
.ia_uid
= shift_kuid(sb
->s_user_ns
, sbinfo
->userns
, attr
->ia_uid
);
770 newattr
.ia_gid
= shift_kgid(sb
->s_user_ns
, sbinfo
->userns
, attr
->ia_gid
);
773 * mode change is for clearing setuid/setgid bits. Allow lower fs
774 * to interpret this in its own way.
776 if (newattr
.ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
))
777 newattr
.ia_valid
&= ~ATTR_MODE
;
780 oldcred
= shiftfs_override_creds(dentry
->d_sb
);
781 err
= notify_change(lowerd
, &newattr
, NULL
);
782 revert_creds(oldcred
);
783 inode_unlock(loweri
);
785 shiftfs_copyattr(loweri
, d_inode(dentry
));
790 static int shiftfs_getattr(const struct path
*path
, struct kstat
*stat
,
791 u32 request_mask
, unsigned int query_flags
)
793 struct inode
*inode
= path
->dentry
->d_inode
;
794 struct dentry
*lowerd
= path
->dentry
->d_fsdata
;
795 struct inode
*loweri
= lowerd
->d_inode
;
796 struct shiftfs_super_info
*info
= path
->dentry
->d_sb
->s_fs_info
;
797 struct path newpath
= { .mnt
= info
->mnt
, .dentry
= lowerd
};
798 struct user_namespace
*from_ns
= loweri
->i_sb
->s_user_ns
;
799 struct user_namespace
*to_ns
= inode
->i_sb
->s_user_ns
;
800 const struct cred
*oldcred
;
803 oldcred
= shiftfs_override_creds(inode
->i_sb
);
804 err
= vfs_getattr(&newpath
, stat
, request_mask
, query_flags
);
805 revert_creds(oldcred
);
810 /* transform the underlying id */
811 stat
->uid
= shift_kuid(from_ns
, to_ns
, stat
->uid
);
812 stat
->gid
= shift_kgid(from_ns
, to_ns
, stat
->gid
);
816 #ifdef CONFIG_SHIFT_FS_POSIX_ACL
819 shift_acl_ids(struct user_namespace
*from
, struct user_namespace
*to
,
820 struct posix_acl
*acl
)
824 for (i
= 0; i
< acl
->a_count
; i
++) {
825 struct posix_acl_entry
*e
= &acl
->a_entries
[i
];
828 e
->e_uid
= shift_kuid(from
, to
, e
->e_uid
);
829 if (!uid_valid(e
->e_uid
))
833 e
->e_gid
= shift_kgid(from
, to
, e
->e_gid
);
834 if (!gid_valid(e
->e_gid
))
843 shift_acl_xattr_ids(struct user_namespace
*from
, struct user_namespace
*to
,
844 void *value
, size_t size
)
846 struct posix_acl_xattr_header
*header
= value
;
847 struct posix_acl_xattr_entry
*entry
= (void *)(header
+ 1), *end
;
854 if (size
< sizeof(struct posix_acl_xattr_header
))
856 if (header
->a_version
!= cpu_to_le32(POSIX_ACL_XATTR_VERSION
))
859 count
= posix_acl_xattr_count(size
);
865 for (end
= entry
+ count
; entry
!= end
; entry
++) {
866 switch(le16_to_cpu(entry
->e_tag
)) {
868 kuid
= make_kuid(&init_user_ns
, le32_to_cpu(entry
->e_id
));
869 kuid
= shift_kuid(from
, to
, kuid
);
870 entry
->e_id
= cpu_to_le32(from_kuid(&init_user_ns
, kuid
));
873 kgid
= make_kgid(&init_user_ns
, le32_to_cpu(entry
->e_id
));
874 kgid
= shift_kgid(from
, to
, kgid
);
875 entry
->e_id
= cpu_to_le32(from_kgid(&init_user_ns
, kgid
));
883 static struct posix_acl
*shiftfs_get_acl(struct inode
*inode
, int type
)
885 struct inode
*loweri
= inode
->i_private
;
886 const struct cred
*oldcred
;
887 struct posix_acl
*lower_acl
, *acl
= NULL
;
888 struct user_namespace
*from_ns
= loweri
->i_sb
->s_user_ns
;
889 struct user_namespace
*to_ns
= inode
->i_sb
->s_user_ns
;
893 if (!IS_POSIXACL(loweri
))
896 oldcred
= shiftfs_override_creds(inode
->i_sb
);
897 lower_acl
= get_acl(loweri
, type
);
898 revert_creds(oldcred
);
900 if (lower_acl
&& !IS_ERR(lower_acl
)) {
901 /* XXX: export posix_acl_clone? */
902 size
= sizeof(struct posix_acl
) +
903 lower_acl
->a_count
* sizeof(struct posix_acl_entry
);
904 acl
= kmemdup(lower_acl
, size
, GFP_KERNEL
);
905 posix_acl_release(lower_acl
);
908 return ERR_PTR(-ENOMEM
);
910 refcount_set(&acl
->a_refcount
, 1);
912 err
= shift_acl_ids(from_ns
, to_ns
, acl
);
923 shiftfs_posix_acl_xattr_get(const struct xattr_handler
*handler
,
924 struct dentry
*dentry
, struct inode
*inode
,
925 const char *name
, void *buffer
, size_t size
)
927 struct inode
*loweri
= inode
->i_private
;
930 ret
= shiftfs_xattr_get(NULL
, dentry
, inode
, handler
->name
,
936 shift_acl_xattr_ids(loweri
->i_sb
->s_user_ns
, inode
->i_sb
->s_user_ns
,
938 inode_unlock(loweri
);
943 shiftfs_posix_acl_xattr_set(const struct xattr_handler
*handler
,
944 struct dentry
*dentry
, struct inode
*inode
,
945 const char *name
, const void *value
,
946 size_t size
, int flags
)
948 struct inode
*loweri
= inode
->i_private
;
951 if (!IS_POSIXACL(loweri
) || !loweri
->i_op
->set_acl
)
953 if (handler
->flags
== ACL_TYPE_DEFAULT
&& !S_ISDIR(inode
->i_mode
))
954 return value
? -EACCES
: 0;
955 if (!inode_owner_or_capable(inode
))
959 shift_acl_xattr_ids(inode
->i_sb
->s_user_ns
,
960 loweri
->i_sb
->s_user_ns
,
961 (void *)value
, size
);
962 err
= shiftfs_setxattr(dentry
, inode
, handler
->name
, value
,
965 err
= shiftfs_removexattr(dentry
, handler
->name
);
969 shiftfs_copyattr(loweri
, inode
);
974 static const struct xattr_handler
975 shiftfs_posix_acl_access_xattr_handler
= {
976 .name
= XATTR_NAME_POSIX_ACL_ACCESS
,
977 .flags
= ACL_TYPE_ACCESS
,
978 .get
= shiftfs_posix_acl_xattr_get
,
979 .set
= shiftfs_posix_acl_xattr_set
,
982 static const struct xattr_handler
983 shiftfs_posix_acl_default_xattr_handler
= {
984 .name
= XATTR_NAME_POSIX_ACL_DEFAULT
,
985 .flags
= ACL_TYPE_DEFAULT
,
986 .get
= shiftfs_posix_acl_xattr_get
,
987 .set
= shiftfs_posix_acl_xattr_set
,
990 #else /* !CONFIG_SHIFT_FS_POSIX_ACL */
992 #define shiftfs_get_acl NULL
994 #endif /* CONFIG_SHIFT_FS_POSIX_ACL */
996 static const struct inode_operations shiftfs_dir_inode_operations
= {
997 .lookup
= shiftfs_lookup
,
998 .mkdir
= shiftfs_mkdir
,
999 .symlink
= shiftfs_symlink
,
1000 .unlink
= shiftfs_unlink
,
1001 .rmdir
= shiftfs_rmdir
,
1002 .rename
= shiftfs_rename
,
1003 .link
= shiftfs_link
,
1004 .setattr
= shiftfs_setattr
,
1005 .create
= shiftfs_create
,
1006 .mknod
= shiftfs_mknod
,
1007 .permission
= shiftfs_permission
,
1008 .getattr
= shiftfs_getattr
,
1009 .listxattr
= shiftfs_listxattr
,
1010 .get_acl
= shiftfs_get_acl
,
1013 static const struct inode_operations shiftfs_file_inode_operations
= {
1014 .fiemap
= shiftfs_fiemap
,
1015 .getattr
= shiftfs_getattr
,
1016 .get_acl
= shiftfs_get_acl
,
1017 .listxattr
= shiftfs_listxattr
,
1018 .permission
= shiftfs_permission
,
1019 .setattr
= shiftfs_setattr
,
1020 .tmpfile
= shiftfs_tmpfile
,
1023 static const struct inode_operations shiftfs_special_inode_operations
= {
1024 .getattr
= shiftfs_getattr
,
1025 .get_acl
= shiftfs_get_acl
,
1026 .listxattr
= shiftfs_listxattr
,
1027 .permission
= shiftfs_permission
,
1028 .setattr
= shiftfs_setattr
,
1031 static const struct inode_operations shiftfs_symlink_inode_operations
= {
1032 .getattr
= shiftfs_getattr
,
1033 .get_link
= shiftfs_get_link
,
1034 .listxattr
= shiftfs_listxattr
,
1035 .setattr
= shiftfs_setattr
,
1038 static struct file
*shiftfs_open_realfile(const struct file
*file
,
1039 struct inode
*realinode
)
1041 struct file
*realfile
;
1042 const struct cred
*old_cred
;
1043 struct inode
*inode
= file_inode(file
);
1044 struct dentry
*lowerd
= file
->f_path
.dentry
->d_fsdata
;
1045 struct shiftfs_super_info
*info
= inode
->i_sb
->s_fs_info
;
1046 struct path realpath
= { .mnt
= info
->mnt
, .dentry
= lowerd
};
1048 old_cred
= shiftfs_override_creds(inode
->i_sb
);
1049 realfile
= open_with_fake_path(&realpath
, file
->f_flags
, realinode
,
1050 info
->creator_cred
);
1051 revert_creds(old_cred
);
1056 #define SHIFTFS_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
1058 static int shiftfs_change_flags(struct file
*file
, unsigned int flags
)
1060 struct inode
*inode
= file_inode(file
);
1063 /* if some flag changed that cannot be changed then something's amiss */
1064 if (WARN_ON((file
->f_flags
^ flags
) & ~SHIFTFS_SETFL_MASK
))
1067 flags
&= SHIFTFS_SETFL_MASK
;
1069 if (((flags
^ file
->f_flags
) & O_APPEND
) && IS_APPEND(inode
))
1072 if (flags
& O_DIRECT
) {
1073 if (!file
->f_mapping
->a_ops
||
1074 !file
->f_mapping
->a_ops
->direct_IO
)
1078 if (file
->f_op
->check_flags
) {
1079 err
= file
->f_op
->check_flags(flags
);
1084 spin_lock(&file
->f_lock
);
1085 file
->f_flags
= (file
->f_flags
& ~SHIFTFS_SETFL_MASK
) | flags
;
1086 spin_unlock(&file
->f_lock
);
1091 static int shiftfs_open(struct inode
*inode
, struct file
*file
)
1093 struct file
*realfile
;
1095 realfile
= shiftfs_open_realfile(file
, inode
->i_private
);
1096 if (IS_ERR(realfile
))
1097 return PTR_ERR(realfile
);
1099 file
->private_data
= realfile
;
1100 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO. */
1101 file
->f_mapping
= realfile
->f_mapping
;
1106 static int shiftfs_dir_open(struct inode
*inode
, struct file
*file
)
1108 struct file
*realfile
;
1109 const struct cred
*oldcred
;
1110 struct dentry
*lowerd
= file
->f_path
.dentry
->d_fsdata
;
1111 struct shiftfs_super_info
*info
= inode
->i_sb
->s_fs_info
;
1112 struct path realpath
= { .mnt
= info
->mnt
, .dentry
= lowerd
};
1114 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1115 realfile
= dentry_open(&realpath
, file
->f_flags
| O_NOATIME
,
1116 info
->creator_cred
);
1117 revert_creds(oldcred
);
1118 if (IS_ERR(realfile
))
1119 return PTR_ERR(realfile
);
1121 file
->private_data
= realfile
;
1126 static int shiftfs_release(struct inode
*inode
, struct file
*file
)
1128 struct file
*realfile
= file
->private_data
;
1136 static int shiftfs_dir_release(struct inode
*inode
, struct file
*file
)
1138 return shiftfs_release(inode
, file
);
1141 static loff_t
shiftfs_dir_llseek(struct file
*file
, loff_t offset
, int whence
)
1143 struct file
*realfile
= file
->private_data
;
1145 return vfs_llseek(realfile
, offset
, whence
);
1148 static loff_t
shiftfs_file_llseek(struct file
*file
, loff_t offset
, int whence
)
1150 struct inode
*realinode
= file_inode(file
)->i_private
;
1152 return generic_file_llseek_size(file
, offset
, whence
,
1153 realinode
->i_sb
->s_maxbytes
,
1154 i_size_read(realinode
));
1157 /* XXX: Need to figure out what to to about atime updates, maybe other
1158 * timestamps too ... ref. ovl_file_accessed() */
1160 static rwf_t
shiftfs_iocb_to_rwf(struct kiocb
*iocb
)
1162 int ifl
= iocb
->ki_flags
;
1165 if (ifl
& IOCB_NOWAIT
)
1166 flags
|= RWF_NOWAIT
;
1167 if (ifl
& IOCB_HIPRI
)
1169 if (ifl
& IOCB_DSYNC
)
1171 if (ifl
& IOCB_SYNC
)
1177 static int shiftfs_real_fdget(const struct file
*file
, struct fd
*lowerfd
)
1179 struct file
*realfile
;
1181 if (file
->f_op
->open
!= shiftfs_open
&&
1182 file
->f_op
->open
!= shiftfs_dir_open
)
1185 realfile
= file
->private_data
;
1187 lowerfd
->file
= realfile
;
1189 /* Did the flags change since open? */
1190 if (unlikely(file
->f_flags
& ~lowerfd
->file
->f_flags
))
1191 return shiftfs_change_flags(lowerfd
->file
, file
->f_flags
);
1196 static ssize_t
shiftfs_read_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
1198 struct file
*file
= iocb
->ki_filp
;
1200 const struct cred
*oldcred
;
1203 if (!iov_iter_count(iter
))
1206 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1210 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1211 ret
= vfs_iter_read(lowerfd
.file
, iter
, &iocb
->ki_pos
,
1212 shiftfs_iocb_to_rwf(iocb
));
1213 revert_creds(oldcred
);
1215 shiftfs_file_accessed(file
);
1221 static ssize_t
shiftfs_write_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
1223 struct file
*file
= iocb
->ki_filp
;
1224 struct inode
*inode
= file_inode(file
);
1226 const struct cred
*oldcred
;
1229 if (!iov_iter_count(iter
))
1234 shiftfs_copyattr(inode
->i_private
, inode
);
1235 ret
= file_remove_privs(file
);
1239 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1243 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1244 file_start_write(lowerfd
.file
);
1245 ret
= vfs_iter_write(lowerfd
.file
, iter
, &iocb
->ki_pos
,
1246 shiftfs_iocb_to_rwf(iocb
));
1247 file_end_write(lowerfd
.file
);
1248 revert_creds(oldcred
);
1251 shiftfs_copyattr(inode
->i_private
, inode
);
1256 inode_unlock(inode
);
1260 static int shiftfs_fsync(struct file
*file
, loff_t start
, loff_t end
,
1264 const struct cred
*oldcred
;
1267 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1271 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1272 ret
= vfs_fsync_range(lowerfd
.file
, start
, end
, datasync
);
1273 revert_creds(oldcred
);
1279 static int shiftfs_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1281 struct file
*realfile
= file
->private_data
;
1282 const struct cred
*oldcred
;
1285 if (!realfile
->f_op
->mmap
)
1288 if (WARN_ON(file
!= vma
->vm_file
))
1291 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1292 vma
->vm_file
= get_file(realfile
);
1293 ret
= call_mmap(vma
->vm_file
, vma
);
1294 revert_creds(oldcred
);
1296 shiftfs_file_accessed(file
);
1300 * Drop refcount from new vm_file value and restore original
1303 vma
->vm_file
= file
;
1306 /* Drop refcount from previous vm_file value */
1313 static long shiftfs_fallocate(struct file
*file
, int mode
, loff_t offset
,
1316 struct inode
*inode
= file_inode(file
);
1317 struct inode
*loweri
= inode
->i_private
;
1319 const struct cred
*oldcred
;
1322 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1326 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1327 ret
= vfs_fallocate(lowerfd
.file
, mode
, offset
, len
);
1328 revert_creds(oldcred
);
1331 shiftfs_copyattr(loweri
, inode
);
1337 static int shiftfs_fadvise(struct file
*file
, loff_t offset
, loff_t len
,
1341 const struct cred
*oldcred
;
1344 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1348 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1349 ret
= vfs_fadvise(lowerfd
.file
, offset
, len
, advice
);
1350 revert_creds(oldcred
);
1356 static int shiftfs_override_ioctl_creds(int cmd
, const struct super_block
*sb
,
1357 const struct cred
**oldcred
,
1358 struct cred
**newcred
)
1360 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
1361 kuid_t fsuid
= current_fsuid();
1362 kgid_t fsgid
= current_fsgid();
1364 *oldcred
= shiftfs_override_creds(sb
);
1366 *newcred
= prepare_creds();
1368 revert_creds(*oldcred
);
1372 (*newcred
)->fsuid
= shift_kuid(sb
->s_user_ns
, sbinfo
->userns
, fsuid
);
1373 (*newcred
)->fsgid
= shift_kgid(sb
->s_user_ns
, sbinfo
->userns
, fsgid
);
1375 /* clear all caps to prevent bypassing capable() checks */
1376 cap_clear((*newcred
)->cap_bset
);
1377 cap_clear((*newcred
)->cap_effective
);
1378 cap_clear((*newcred
)->cap_inheritable
);
1379 cap_clear((*newcred
)->cap_permitted
);
1381 if (cmd
== BTRFS_IOC_SNAP_DESTROY
) {
1382 kuid_t kuid_root
= make_kuid(sb
->s_user_ns
, 0);
1384 * Allow the root user in the container to remove subvolumes
1387 if (uid_valid(kuid_root
) && uid_eq(fsuid
, kuid_root
))
1388 cap_raise((*newcred
)->cap_effective
, CAP_DAC_OVERRIDE
);
1391 put_cred(override_creds(*newcred
));
1395 static inline void shiftfs_revert_ioctl_creds(const struct cred
*oldcred
,
1396 struct cred
*newcred
)
1398 return shiftfs_revert_object_creds(oldcred
, newcred
);
1401 static inline bool is_btrfs_snap_ioctl(int cmd
)
1403 if ((cmd
== BTRFS_IOC_SNAP_CREATE
) || (cmd
== BTRFS_IOC_SNAP_CREATE_V2
))
1409 static int shiftfs_btrfs_ioctl_fd_restore(int cmd
, int fd
, void __user
*arg
,
1410 struct btrfs_ioctl_vol_args
*v1
,
1411 struct btrfs_ioctl_vol_args_v2
*v2
)
1415 if (!is_btrfs_snap_ioctl(cmd
))
1418 if (cmd
== BTRFS_IOC_SNAP_CREATE
)
1419 ret
= copy_to_user(arg
, v1
, sizeof(*v1
));
1421 ret
= copy_to_user(arg
, v2
, sizeof(*v2
));
1427 return ret
? -EFAULT
: 0;
1430 static int shiftfs_btrfs_ioctl_fd_replace(int cmd
, void __user
*arg
,
1431 struct btrfs_ioctl_vol_args
**b1
,
1432 struct btrfs_ioctl_vol_args_v2
**b2
,
1438 struct btrfs_ioctl_vol_args
*v1
= NULL
;
1439 struct btrfs_ioctl_vol_args_v2
*v2
= NULL
;
1444 if (!is_btrfs_snap_ioctl(cmd
))
1447 if (cmd
== BTRFS_IOC_SNAP_CREATE
) {
1448 v1
= memdup_user(arg
, sizeof(*v1
));
1453 v2
= memdup_user(arg
, sizeof(*v2
));
1465 ret
= shiftfs_real_fdget(src
.file
, &lfd
);
1472 * shiftfs_real_fdget() does not take a reference to lfd.file, so
1473 * take a reference here to offset the one which will be put by
1474 * close_fd(), and make sure that reference is put on fdput(lfd).
1477 lfd
.flags
|= FDPUT_FPUT
;
1480 *newfd
= get_unused_fd_flags(lfd
.file
->f_flags
);
1487 fd_install(*newfd
, lfd
.file
);
1489 if (cmd
== BTRFS_IOC_SNAP_CREATE
) {
1491 ret
= copy_to_user(arg
, v1
, sizeof(*v1
));
1495 ret
= copy_to_user(arg
, v2
, sizeof(*v2
));
1503 shiftfs_btrfs_ioctl_fd_restore(cmd
, *newfd
, arg
, v1
, v2
);
1516 static long shiftfs_real_ioctl(struct file
*file
, unsigned int cmd
,
1520 struct cred
*newcred
;
1521 const struct cred
*oldcred
;
1523 long err
= 0, ret
= 0;
1524 void __user
*argp
= (void __user
*)arg
;
1525 struct super_block
*sb
= file
->f_path
.dentry
->d_sb
;
1526 struct btrfs_ioctl_vol_args
*btrfs_v1
= NULL
;
1527 struct btrfs_ioctl_vol_args_v2
*btrfs_v2
= NULL
;
1529 ret
= shiftfs_btrfs_ioctl_fd_replace(cmd
, argp
, &btrfs_v1
, &btrfs_v2
,
1534 ret
= shiftfs_real_fdget(file
, &lowerfd
);
1538 ret
= shiftfs_override_ioctl_creds(cmd
, sb
, &oldcred
, &newcred
);
1542 ret
= vfs_ioctl(lowerfd
.file
, cmd
, arg
);
1544 shiftfs_revert_ioctl_creds(oldcred
, newcred
);
1546 shiftfs_copyattr(file_inode(lowerfd
.file
), file_inode(file
));
1547 shiftfs_copyflags(file_inode(lowerfd
.file
), file_inode(file
));
1553 err
= shiftfs_btrfs_ioctl_fd_restore(cmd
, newfd
, argp
,
1554 btrfs_v1
, btrfs_v2
);
1561 static bool in_ioctl_whitelist(int flag
, unsigned long arg
)
1563 void __user
*argp
= (void __user
*)arg
;
1567 case BTRFS_IOC_FS_INFO
:
1569 case BTRFS_IOC_SNAP_CREATE
:
1571 case BTRFS_IOC_SNAP_CREATE_V2
:
1573 case BTRFS_IOC_SUBVOL_CREATE
:
1575 case BTRFS_IOC_SUBVOL_CREATE_V2
:
1577 case BTRFS_IOC_SUBVOL_GETFLAGS
:
1579 case BTRFS_IOC_SUBVOL_SETFLAGS
:
1580 if (copy_from_user(&flags
, argp
, sizeof(flags
)))
1583 if (flags
& ~BTRFS_SUBVOL_RDONLY
)
1587 case BTRFS_IOC_SNAP_DESTROY
:
1594 static long shiftfs_ioctl(struct file
*file
, unsigned int cmd
,
1598 case FS_IOC_GETVERSION
:
1600 case FS_IOC_GETFLAGS
:
1602 case FS_IOC_SETFLAGS
:
1605 if (!in_ioctl_whitelist(cmd
, arg
) ||
1606 !shiftfs_passthrough_ioctls(file
->f_path
.dentry
->d_sb
->s_fs_info
))
1610 return shiftfs_real_ioctl(file
, cmd
, arg
);
1613 static long shiftfs_compat_ioctl(struct file
*file
, unsigned int cmd
,
1617 case FS_IOC32_GETVERSION
:
1619 case FS_IOC32_GETFLAGS
:
1621 case FS_IOC32_SETFLAGS
:
1624 if (!in_ioctl_whitelist(cmd
, arg
) ||
1625 !shiftfs_passthrough_ioctls(file
->f_path
.dentry
->d_sb
->s_fs_info
))
1626 return -ENOIOCTLCMD
;
1629 return shiftfs_real_ioctl(file
, cmd
, arg
);
1632 enum shiftfs_copyop
{
1638 static ssize_t
shiftfs_copyfile(struct file
*file_in
, loff_t pos_in
,
1639 struct file
*file_out
, loff_t pos_out
, u64 len
,
1640 unsigned int flags
, enum shiftfs_copyop op
)
1643 struct fd real_in
, real_out
;
1644 const struct cred
*oldcred
;
1645 struct inode
*inode_out
= file_inode(file_out
);
1646 struct inode
*loweri
= inode_out
->i_private
;
1648 ret
= shiftfs_real_fdget(file_out
, &real_out
);
1652 ret
= shiftfs_real_fdget(file_in
, &real_in
);
1658 oldcred
= shiftfs_override_creds(inode_out
->i_sb
);
1661 ret
= vfs_copy_file_range(real_in
.file
, pos_in
, real_out
.file
,
1662 pos_out
, len
, flags
);
1666 ret
= vfs_clone_file_range(real_in
.file
, pos_in
, real_out
.file
,
1667 pos_out
, len
, flags
);
1670 case SHIFTFS_DEDUPE
:
1671 ret
= vfs_dedupe_file_range_one(real_in
.file
, pos_in
,
1672 real_out
.file
, pos_out
, len
,
1676 revert_creds(oldcred
);
1679 shiftfs_copyattr(loweri
, inode_out
);
1687 static ssize_t
shiftfs_copy_file_range(struct file
*file_in
, loff_t pos_in
,
1688 struct file
*file_out
, loff_t pos_out
,
1689 size_t len
, unsigned int flags
)
1691 return shiftfs_copyfile(file_in
, pos_in
, file_out
, pos_out
, len
, flags
,
1695 static loff_t
shiftfs_remap_file_range(struct file
*file_in
, loff_t pos_in
,
1696 struct file
*file_out
, loff_t pos_out
,
1697 loff_t len
, unsigned int remap_flags
)
1699 enum shiftfs_copyop op
;
1701 if (remap_flags
& ~(REMAP_FILE_DEDUP
| REMAP_FILE_ADVISORY
))
1704 if (remap_flags
& REMAP_FILE_DEDUP
)
1705 op
= SHIFTFS_DEDUPE
;
1709 return shiftfs_copyfile(file_in
, pos_in
, file_out
, pos_out
, len
,
1713 static int shiftfs_iterate_shared(struct file
*file
, struct dir_context
*ctx
)
1715 const struct cred
*oldcred
;
1717 struct file
*realfile
= file
->private_data
;
1719 oldcred
= shiftfs_override_creds(file
->f_path
.dentry
->d_sb
);
1720 err
= iterate_dir(realfile
, ctx
);
1721 revert_creds(oldcred
);
1726 const struct file_operations shiftfs_file_operations
= {
1727 .open
= shiftfs_open
,
1728 .release
= shiftfs_release
,
1729 .llseek
= shiftfs_file_llseek
,
1730 .read_iter
= shiftfs_read_iter
,
1731 .write_iter
= shiftfs_write_iter
,
1732 .fsync
= shiftfs_fsync
,
1733 .mmap
= shiftfs_mmap
,
1734 .fallocate
= shiftfs_fallocate
,
1735 .fadvise
= shiftfs_fadvise
,
1736 .unlocked_ioctl
= shiftfs_ioctl
,
1737 .compat_ioctl
= shiftfs_compat_ioctl
,
1738 .copy_file_range
= shiftfs_copy_file_range
,
1739 .remap_file_range
= shiftfs_remap_file_range
,
1740 .splice_read
= generic_file_splice_read
,
1741 .splice_write
= iter_file_splice_write
,
1744 const struct file_operations shiftfs_dir_operations
= {
1745 .open
= shiftfs_dir_open
,
1746 .release
= shiftfs_dir_release
,
1747 .compat_ioctl
= shiftfs_compat_ioctl
,
1748 .fsync
= shiftfs_fsync
,
1749 .iterate_shared
= shiftfs_iterate_shared
,
1750 .llseek
= shiftfs_dir_llseek
,
1751 .read
= generic_read_dir
,
1752 .unlocked_ioctl
= shiftfs_ioctl
,
1755 static const struct address_space_operations shiftfs_aops
= {
1756 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
1757 .direct_IO
= noop_direct_IO
,
1760 static void shiftfs_fill_inode(struct inode
*inode
, unsigned long ino
,
1761 umode_t mode
, dev_t dev
, struct dentry
*dentry
)
1763 struct inode
*loweri
;
1766 inode
->i_flags
|= S_NOCMTIME
;
1769 inode
->i_mode
= mode
;
1770 switch (mode
& S_IFMT
) {
1772 inode
->i_op
= &shiftfs_dir_inode_operations
;
1773 inode
->i_fop
= &shiftfs_dir_operations
;
1776 inode
->i_op
= &shiftfs_symlink_inode_operations
;
1779 inode
->i_op
= &shiftfs_file_inode_operations
;
1780 inode
->i_fop
= &shiftfs_file_operations
;
1781 inode
->i_mapping
->a_ops
= &shiftfs_aops
;
1784 inode
->i_op
= &shiftfs_special_inode_operations
;
1785 init_special_inode(inode
, mode
, dev
);
1792 loweri
= dentry
->d_inode
;
1793 if (!loweri
->i_op
->get_link
)
1794 inode
->i_opflags
|= IOP_NOFOLLOW
;
1796 shiftfs_copyattr(loweri
, inode
);
1797 shiftfs_copyflags(loweri
, inode
);
1798 set_nlink(inode
, loweri
->i_nlink
);
1801 static int shiftfs_show_options(struct seq_file
*m
, struct dentry
*dentry
)
1803 struct super_block
*sb
= dentry
->d_sb
;
1804 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
1807 seq_show_option(m
, "mark", NULL
);
1809 if (sbinfo
->passthrough
)
1810 seq_printf(m
, ",passthrough=%u", sbinfo
->passthrough
);
1815 static int shiftfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
1817 struct super_block
*sb
= dentry
->d_sb
;
1818 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
1819 struct dentry
*root
= sb
->s_root
;
1820 struct dentry
*realroot
= root
->d_fsdata
;
1821 struct path realpath
= { .mnt
= sbinfo
->mnt
, .dentry
= realroot
};
1824 err
= vfs_statfs(&realpath
, buf
);
1828 if (!shiftfs_passthrough_statfs(sbinfo
))
1829 buf
->f_type
= sb
->s_magic
;
1834 static void shiftfs_evict_inode(struct inode
*inode
)
1836 struct inode
*loweri
= inode
->i_private
;
1844 static void shiftfs_put_super(struct super_block
*sb
)
1846 struct shiftfs_super_info
*sbinfo
= sb
->s_fs_info
;
1849 mntput(sbinfo
->mnt
);
1850 put_cred(sbinfo
->creator_cred
);
1855 static const struct xattr_handler shiftfs_xattr_handler
= {
1857 .get
= shiftfs_xattr_get
,
1858 .set
= shiftfs_xattr_set
,
1861 const struct xattr_handler
*shiftfs_xattr_handlers
[] = {
1862 #ifdef CONFIG_SHIFT_FS_POSIX_ACL
1863 &shiftfs_posix_acl_access_xattr_handler
,
1864 &shiftfs_posix_acl_default_xattr_handler
,
1866 &shiftfs_xattr_handler
,
1870 static inline bool passthrough_is_subset(int old_flags
, int new_flags
)
1872 if ((new_flags
& old_flags
) != new_flags
)
1878 static int shiftfs_super_check_flags(unsigned long old_flags
,
1879 unsigned long new_flags
)
1881 if ((old_flags
& SB_RDONLY
) && !(new_flags
& SB_RDONLY
))
1884 if ((old_flags
& SB_NOSUID
) && !(new_flags
& SB_NOSUID
))
1887 if ((old_flags
& SB_NODEV
) && !(new_flags
& SB_NODEV
))
1890 if ((old_flags
& SB_NOEXEC
) && !(new_flags
& SB_NOEXEC
))
1893 if ((old_flags
& SB_NOATIME
) && !(new_flags
& SB_NOATIME
))
1896 if ((old_flags
& SB_NODIRATIME
) && !(new_flags
& SB_NODIRATIME
))
1899 if (!(old_flags
& SB_POSIXACL
) && (new_flags
& SB_POSIXACL
))
1905 static int shiftfs_remount(struct super_block
*sb
, int *flags
, char *data
)
1908 struct shiftfs_super_info
new = {};
1909 struct shiftfs_super_info
*info
= sb
->s_fs_info
;
1911 err
= shiftfs_parse_mount_options(&new, data
);
1915 err
= shiftfs_super_check_flags(sb
->s_flags
, *flags
);
1919 /* Mark mount option cannot be changed. */
1920 if (info
->mark
|| (info
->mark
!= new.mark
))
1923 if (info
->passthrough
!= new.passthrough
) {
1924 /* Don't allow exceeding passthrough options of mark mount. */
1925 if (!passthrough_is_subset(info
->passthrough_mark
,
1929 info
->passthrough
= new.passthrough
;
1935 static const struct super_operations shiftfs_super_ops
= {
1936 .put_super
= shiftfs_put_super
,
1937 .show_options
= shiftfs_show_options
,
1938 .statfs
= shiftfs_statfs
,
1939 .remount_fs
= shiftfs_remount
,
1940 .evict_inode
= shiftfs_evict_inode
,
1943 struct shiftfs_data
{
1948 static void shiftfs_super_force_flags(struct super_block
*sb
,
1949 unsigned long lower_flags
)
1951 sb
->s_flags
|= lower_flags
& (SB_RDONLY
| SB_NOSUID
| SB_NODEV
|
1952 SB_NOEXEC
| SB_NOATIME
| SB_NODIRATIME
);
1954 if (!(lower_flags
& SB_POSIXACL
))
1955 sb
->s_flags
&= ~SB_POSIXACL
;
1958 static int shiftfs_fill_super(struct super_block
*sb
, void *raw_data
,
1962 struct path path
= {};
1963 struct shiftfs_super_info
*sbinfo_mp
;
1965 struct inode
*inode
= NULL
;
1966 struct dentry
*dentry
= NULL
;
1967 struct shiftfs_data
*data
= raw_data
;
1968 struct shiftfs_super_info
*sbinfo
= NULL
;
1973 sb
->s_fs_info
= kzalloc(sizeof(*sbinfo
), GFP_KERNEL
);
1976 sbinfo
= sb
->s_fs_info
;
1978 err
= shiftfs_parse_mount_options(sbinfo
, data
->data
);
1982 /* to mount a mark, must be userns admin */
1983 if (!sbinfo
->mark
&& !ns_capable(current_user_ns(), CAP_SYS_ADMIN
))
1986 name
= kstrdup(data
->path
, GFP_KERNEL
);
1990 err
= kern_path(name
, LOOKUP_FOLLOW
, &path
);
1994 if (!S_ISDIR(path
.dentry
->d_inode
->i_mode
)) {
1999 sb
->s_flags
|= SB_POSIXACL
;
2002 struct cred
*cred_tmp
;
2003 struct super_block
*lower_sb
= path
.mnt
->mnt_sb
;
2005 /* to mark a mount point, must root wrt lower s_user_ns */
2006 if (!ns_capable(lower_sb
->s_user_ns
, CAP_SYS_ADMIN
)) {
2012 * this part is visible unshifted, so make sure no
2013 * executables that could be used to give suid
2016 sb
->s_iflags
= SB_I_NOEXEC
;
2018 shiftfs_super_force_flags(sb
, lower_sb
->s_flags
);
2021 * Handle nesting of shiftfs mounts by referring this mark
2022 * mount back to the original mark mount. This is more
2023 * efficient and alleviates concerns about stack depth.
2025 if (lower_sb
->s_magic
== SHIFTFS_MAGIC
) {
2026 sbinfo_mp
= lower_sb
->s_fs_info
;
2028 /* Doesn't make sense to mark a mark mount */
2029 if (sbinfo_mp
->mark
) {
2034 if (!passthrough_is_subset(sbinfo_mp
->passthrough
,
2035 sbinfo
->passthrough
)) {
2040 sbinfo
->mnt
= mntget(sbinfo_mp
->mnt
);
2041 dentry
= dget(path
.dentry
->d_fsdata
);
2043 * Copy up the passthrough mount options from the
2044 * parent mark mountpoint.
2046 sbinfo
->passthrough_mark
= sbinfo_mp
->passthrough_mark
;
2047 sbinfo
->creator_cred
= get_cred(sbinfo_mp
->creator_cred
);
2049 sbinfo
->mnt
= mntget(path
.mnt
);
2050 dentry
= dget(path
.dentry
);
2052 * For a new mark passthrough_mark and passthrough
2055 sbinfo
->passthrough_mark
= sbinfo
->passthrough
;
2057 cred_tmp
= prepare_creds();
2062 /* Don't override disk quota limits or use reserved space. */
2063 cap_lower(cred_tmp
->cap_effective
, CAP_SYS_RESOURCE
);
2064 sbinfo
->creator_cred
= cred_tmp
;
2068 * This leg executes if we're admin capable in the namespace,
2069 * so be very careful.
2072 if (path
.dentry
->d_sb
->s_magic
!= SHIFTFS_MAGIC
)
2075 sbinfo_mp
= path
.dentry
->d_sb
->s_fs_info
;
2076 if (!sbinfo_mp
->mark
)
2079 if (!passthrough_is_subset(sbinfo_mp
->passthrough
,
2080 sbinfo
->passthrough
))
2083 sbinfo
->mnt
= mntget(sbinfo_mp
->mnt
);
2084 sbinfo
->creator_cred
= get_cred(sbinfo_mp
->creator_cred
);
2085 dentry
= dget(path
.dentry
->d_fsdata
);
2087 * Copy up passthrough settings from mark mountpoint so we can
2088 * verify when the overlay wants to remount with different
2089 * passthrough settings.
2091 sbinfo
->passthrough_mark
= sbinfo_mp
->passthrough
;
2092 shiftfs_super_force_flags(sb
, path
.mnt
->mnt_sb
->s_flags
);
2095 sb
->s_stack_depth
= dentry
->d_sb
->s_stack_depth
+ 1;
2096 if (sb
->s_stack_depth
> FILESYSTEM_MAX_STACK_DEPTH
) {
2097 printk(KERN_ERR
"shiftfs: maximum stacking depth exceeded\n");
2102 inode
= new_inode(sb
);
2107 shiftfs_fill_inode(inode
, dentry
->d_inode
->i_ino
, S_IFDIR
, 0, dentry
);
2109 ihold(dentry
->d_inode
);
2110 inode
->i_private
= dentry
->d_inode
;
2112 sb
->s_magic
= SHIFTFS_MAGIC
;
2113 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
2114 sb
->s_op
= &shiftfs_super_ops
;
2115 sb
->s_xattr
= shiftfs_xattr_handlers
;
2116 sb
->s_d_op
= &shiftfs_dentry_ops
;
2117 sb
->s_root
= d_make_root(inode
);
2123 sb
->s_root
->d_fsdata
= dentry
;
2124 sbinfo
->userns
= get_user_ns(dentry
->d_sb
->s_user_ns
);
2125 shiftfs_copyattr(dentry
->d_inode
, sb
->s_root
->d_inode
);
2141 static struct dentry
*shiftfs_mount(struct file_system_type
*fs_type
,
2142 int flags
, const char *dev_name
, void *data
)
2144 struct shiftfs_data d
= { data
, dev_name
};
2146 return mount_nodev(fs_type
, flags
, &d
, shiftfs_fill_super
);
2149 static struct file_system_type shiftfs_type
= {
2150 .owner
= THIS_MODULE
,
2152 .mount
= shiftfs_mount
,
2153 .kill_sb
= kill_anon_super
,
2154 .fs_flags
= FS_USERNS_MOUNT
,
2157 static int __init
shiftfs_init(void)
2159 return register_filesystem(&shiftfs_type
);
2162 static void __exit
shiftfs_exit(void)
2164 unregister_filesystem(&shiftfs_type
);
2167 MODULE_ALIAS_FS("shiftfs");
2168 MODULE_AUTHOR("James Bottomley");
2169 MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>");
2170 MODULE_AUTHOR("Christian Brauner <christian.brauner@ubuntu.com>");
2171 MODULE_DESCRIPTION("id shifting filesystem");
2172 MODULE_LICENSE("GPL v2");
2173 module_init(shiftfs_init
)
2174 module_exit(shiftfs_exit
)