shiftfs fixes and enhancements
cover shiftfs fixes and enhancements
I've done some work to fix and enhance shiftfs for a number of use cases, so that we would have an idea what a more full-featured shiftfs would look like. I'm intending for these to serve as a point of reference for discussing id shifting mounts/filesystems at plumbers in a couple of weeks [1].
Note that these are based on 4.18, and I've added a small fix to James' most recent patch to fix a build issue there. To work with 4.19 they will need a number of updates due to changes in the vfs.
The features I focused on fixing in or adding to shiftfs in these patches are inotify, file capabilities, posix acls, and nesting. These are all now working for at least simple use cases, but further testing and cleanups are needed before I'd consider these finished. I also kept all the changes within shiftfs, but some of the code might belong in the vfs instead (in particular some of the posix acl code).
I've also pushed these patches to:
git://git.kernel.org/pub/scm/linux/kernel/git/sforshee/linux.git shiftfs
Thanks,
Seth
[1] https://linuxplumbersconf.org/event/2/contributions/212/
---
James Bottomley (1):
shiftfs: uid/gid shifting bind mount
Seth Forshee (5):
shiftfs: map inodes to lower fs inodes instead of dentries
shiftfs: copy inode attrs up from underlying fs
shiftfs: translate uids using s_user_ns from lower fs
shiftfs: add support for posix acls
shiftfs: support nested shiftfs mounts
fs/Kconfig | 18 + fs/Makefile | 1 + fs/shiftfs.c | 1075 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/magic.h | 2 + 4 files changed, 1096 insertions(+) create mode 100644 fs/shiftfs.c
RFC PATCH 1/6 shiftfs: uid/gid shifting bind mount
From: James Bottomley <[email protected]>
This allows any subtree to be uid/gid shifted and bound elsewhere. It does this by operating simlarly to overlayfs. Its primary use is for shifting the underlying uids of filesystems used to support unpriviliged (uid shifted) containers. The usual use case here is that the container is operating with an uid shifted unprivileged root but sometimes needs to make use of or work with a filesystem image that has root at real uid 0.
The mechanism is to allow any subordinate mount namespace to mount a shiftfs filesystem (by marking it FS_USERNS_MOUNT) but only allowing it to mount marked subtrees (using the -o mark option as root). Once mounted, the subtree is mapped via the super block user namespace so that the interior ids of the mounting user namespace are the ids written to the filesystem.
[ saf: use designated initializers for path declarations to fix errors
with struct randomization ]
---
v3 - update to 4.14 (d_real changes)
v1 - based on original shiftfs with uid mappings now done via s_user_ns v2 - fix revalidation of dentries
add inode aliasing
--- fs/Kconfig | 8 + fs/Makefile | 1 + fs/shiftfs.c | 783 +++++++++++++++++++++++++++++++++++++ include/uapi/linux/magic.h | 2 + 4 files changed, 794 insertions(+) create mode 100644 fs/shiftfs.c
fs/Kconfig +8 -0
@@ -113,6 +113,14 @@ source "fs/autofs/Kconfig" source "fs/fuse/Kconfig" source "fs/overlayfs/Kconfig" +config SHIFT_FS + tristate "UID/GID shifting overlay filesystem for containers" + help + This filesystem can overlay any mounted filesystem and shift + the uid/gid the files appear at. The idea is that + unprivileged containers can use this to mount root volumes + using this technique. + menu "Caches" source "fs/fscache/Kconfig"
fs/Makefile +1 -0
@@ -128,3 +128,4 @@ obj-y += exofs/ # Multiple modules obj-$(CONFIG_CEPH_FS) += ceph/ obj-$(CONFIG_PSTORE) += pstore/ obj-$(CONFIG_EFIVAR_FS) += efivarfs/ +obj-$(CONFIG_SHIFT_FS) += shiftfs.o
fs/shiftfs.c new file +783 -0
783 lines not on this page · raw message
include/uapi/linux/magic.h +2 -0
@@ -90,4 +90,6 @@#define BALLOON_KVM_MAGIC 0x13661366#define ZSMALLOC_MAGIC 0x58295829 +#define SHIFTFS_MAGIC 0x6a656a62 +#endif /* __LINUX_MAGIC_H__ */
Signature · 1 line
2.19.1
RFC PATCH 2/6 shiftfs: map inodes to lower fs inodes instead of dentries
In reply to Seth ForsheeSince shiftfs inodes map to dentries in the lower fs, two links to the same lowerfs inode create separate inodes in shiftfs. This causes problems for inotify, as a watch on one of these files in shiftfs will not see changes made to the underlying inode via the other file.
Fix this by updating shiftfs to map its inodes to corresponding inodes in the lower fs. Inodes are cached using the pointer to the lower fs inode as the hash value. This fixes a second inotify problem whereby a watch is set on an inode, the dentry is evicted from the cache, and events on a new dentry are not reported back to the watch original inode.
--- fs/shiftfs.c | 105 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 26 deletions(-)
fs/shiftfs.c +79 -26
@@ -22,6 +22,7 @@ struct shiftfs_super_info {static struct inode *shiftfs_new_inode (struct super_block *sb,umode_t mode,struct dentry *dentry); +static void shiftfs_init_inode (struct inode *inode,umode_t mode);enum {OPT_MARK ,
@@ -278,15 +279,27 @@ static void shiftfs_fill_inode(struct inode *inode, struct dentry *dentry) inode->i_opflags |=IOP_NOFOLLOW ; inode->i_mapping = reali->i_mapping; - inode->i_private = dentry; + inode->i_private = reali; +set_nlink (inode, reali->i_nlink); +} + +static int shiftfs_inode_test (struct inode *inode,void *data) +{ +return inode->i_private == data; +} + +static int shiftfs_inode_set (struct inode *inode,void *data) +{ + inode->i_private = data; +return 0 ; }static int shiftfs_make_object (struct inode *dir,struct dentry *dentry,umode_t mode,const char *symlink,struct dentry *hardlink,bool excl) { -struct dentry *real = dir->i_private, *new = dentry->d_fsdata; -struct inode *reali = real->d_inode, *newi; +struct dentry *new = dentry->d_fsdata; +struct inode *reali = dir->i_private, *inode, *newi;const struct inode_operations *iop = reali->i_op;int err;const struct cred *oldcred, *newcred;
@@ -310,9 +323,14 @@ static int shiftfs_make_object(struct inode *dir, struct dentry *dentry,return -EINVAL ; - newi =shiftfs_new_inode (dentry->d_sb, mode,NULL ); -if (!newi) -return -ENOMEM ; +if (hardlink) { + inode =d_inode (hardlink); +ihold (inode); + }else { + inode =shiftfs_new_inode (dentry->d_sb, mode,NULL ); +if (!inode) +return -ENOMEM ; + } oldcred =shiftfs_new_creds (&newcred, dentry->d_sb);
@@ -341,16 +359,33 @@ static int shiftfs_make_object(struct inode *dir, struct dentry *dentry,if (err) goto out_dput; -shiftfs_fill_inode (newi, new); +if (hardlink) { +WARN_ON (inode->i_private != new->d_inode); +inc_nlink (inode); + }else { +shiftfs_fill_inode (inode, new); + + newi =inode_insert5 (inode, (unsigned long )new->d_inode, + shiftfs_inode_test, shiftfs_inode_set, + new->d_inode); +if (newi != inode) { +pr_warn_ratelimited ("shiftfs: newly created inode found in cache\n" ); +iput (inode); + inode = newi; + } + } + +if (inode->i_state &I_NEW ) +unlock_new_inode (inode); -d_instantiate (dentry, newi); +d_instantiate (dentry, inode); new =NULL ; - newi =NULL ; + inode =NULL ; out_dput:dput (new); -iput (newi); +iput (inode);inode_unlock (reali);return err;
@@ -386,8 +421,8 @@ static int shiftfs_symlink(struct inode *dir, struct dentry *dentry,static int shiftfs_rm (struct inode *dir,struct dentry *dentry,bool rmdir) { -struct dentry *real = dir->i_private, *new = dentry->d_fsdata; -struct inode *reali = real->d_inode; +struct dentry *new = dentry->d_fsdata; +struct inode *reali = dir->i_private;int err;const struct cred *oldcred, *newcred;
@@ -400,6 +435,13 @@ static int shiftfs_rm(struct inode *dir, struct dentry *dentry, bool rmdir)else err =vfs_unlink (reali, new,NULL ); +if (!err) { +if (rmdir) +clear_nlink (d_inode (dentry)); +else +drop_nlink (d_inode (dentry)); + } +shiftfs_old_creds (oldcred, &newcred);inode_unlock (reali);
@@ -420,7 +462,8 @@ static int shiftfs_rename(struct inode *olddir, struct dentry *old, struct inode *newdir, struct dentry *new, unsigned int flags) { - struct dentry *rodd = olddir->i_private, *rndd = newdir->i_private, + struct dentry *rodd = old->d_parent->d_fsdata, + *rndd = new->d_parent->d_fsdata, *realold = old->d_fsdata, *realnew = new->d_fsdata, *trap;struct inode *realolddir = rodd->d_inode, *realnewdir = rndd->d_inode;
@@ -448,8 +491,8 @@ static int shiftfs_rename(struct inode *olddir, struct dentry *old,static struct dentry *shiftfs_lookup (struct inode *dir,struct dentry *dentry,unsigned int flags) { -struct dentry *real = dir->i_private, *new; -struct inode *reali = real->d_inode, *newi; +struct dentry *real = dentry->d_parent->d_fsdata, *new; +struct inode *reali = real->d_inode, *newi, *inode;const struct cred *oldcred, *newcred;inode_lock (reali);
@@ -463,24 +506,30 @@ static struct dentry *shiftfs_lookup(struct inode *dir, struct dentry *dentry, dentry->d_fsdata = new; - newi =NULL ; -if (!new->d_inode) + inode =NULL ; + newi = new->d_inode; +if (!newi) goto out; - newi =shiftfs_new_inode (dentry->d_sb, new->d_inode->i_mode, new); -if (!newi) { + inode =iget5_locked (dentry->d_sb, (unsigned long )newi, + shiftfs_inode_test, shiftfs_inode_set, newi); +if (!inode) {dput (new);return ERR_PTR (-ENOMEM ); } +if (inode->i_state &I_NEW ) { +shiftfs_init_inode (inode, newi->i_mode); +shiftfs_fill_inode (inode, new); +unlock_new_inode (inode); + } out: -return d_splice_alias (newi, dentry); +return d_splice_alias (inode, dentry); }static int shiftfs_permission (struct inode *inode,int mask) { -struct dentry *real = inode->i_private; -struct inode *reali = real->d_inode; +struct inode *reali = inode->i_private;const struct inode_operations *iop = reali->i_op;int err;const struct cred *oldcred, *newcred;
@@ -579,6 +628,14 @@ static struct inode *shiftfs_new_inode(struct super_block *sb, umode_t mode,if (!inode)return NULL ; +shiftfs_init_inode (inode, mode); +shiftfs_fill_inode (inode, dentry); + +return inode; +} + +static void shiftfs_init_inode (struct inode *inode,umode_t mode) +{/* * our inode is completely vestigial. All lookups, getattr * and permission checks are done on the underlying inode, so
@@ -591,10 +648,6 @@ static struct inode *shiftfs_new_inode(struct super_block *sb, umode_t mode, inode->i_flags |=S_NOATIME |S_NOCMTIME ; inode->i_op = &shiftfs_inode_ops; - -shiftfs_fill_inode (inode, dentry); - -return inode; }static int shiftfs_show_options (struct seq_file *m,struct dentry *dentry)
Signature · 1 line
2.19.1
RFC PATCH 3/6 shiftfs: copy inode attrs up from underlying fs
In reply to Seth ForsheeNot all inode permission checks go through the permission callback, e.g. some checks related to file capabilities. Always copy up the inode attrs to ensure these checks work as expected.
Also introduce helpers helpers for shifting kernel ids from one user ns to another, as this is an operation that is going to be repeated.
--- fs/shiftfs.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)
fs/shiftfs.c +29 -1
@@ -266,6 +266,33 @@ static int shiftfs_xattr_set(const struct xattr_handler *handler,return shiftfs_setxattr (dentry, inode, name, value, size, flags); } +static kuid_t shift_kuid (struct user_namespace *from,struct user_namespace *to, +kuid_t kuid) +{ +uid_t uid =from_kuid (from, kuid); +return make_kuid (to, uid); +} + +static kgid_t shift_kgid (struct user_namespace *from,struct user_namespace *to, +kgid_t kgid) +{ +gid_t gid =from_kgid (from, kgid); +return make_kgid (to, gid); +} + +static void shiftfs_copyattr (struct inode *from,struct inode *to) +{ +struct user_namespace *from_ns = from->i_sb->s_user_ns; +struct user_namespace *to_ns = to->i_sb->s_user_ns; + + to->i_uid =shift_kuid (from_ns, to_ns, from->i_uid); + to->i_gid =shift_kgid (from_ns, to_ns, from->i_gid); + to->i_mode = from->i_mode; + to->i_atime = from->i_atime; + to->i_mtime = from->i_mtime; + to->i_ctime = from->i_ctime; +} +static void shiftfs_fill_inode (struct inode *inode,struct dentry *dentry) {struct inode *reali;
@@ -278,6 +305,7 @@ static void shiftfs_fill_inode(struct inode *inode, struct dentry *dentry)if (!reali->i_op->get_link) inode->i_opflags |=IOP_NOFOLLOW ; +shiftfs_copyattr (reali, inode); inode->i_mapping = reali->i_mapping; inode->i_private = reali;set_nlink (inode, reali->i_nlink);
@@ -573,7 +601,7 @@ static int shiftfs_setattr(struct dentry *dentry, struct iattr *attr)return err;/* all OK, reflect the change on our inode */ -setattr_copy (d_inode (dentry), attr); +shiftfs_copyattr (reali,d_inode (dentry));return 0 ; }
Signature · 1 line
2.19.1
RFC PATCH 4/6 shiftfs: translate uids using s_user_ns from lower fs
In reply to Seth ForsheeDo not assume that ids from the lower filesystem are from init_user_ns. Instead, translate them from that filesystem's s_user_ns and then to the shiftfs user ns.
--- fs/shiftfs.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
fs/shiftfs.c +4 -2
@@ -613,6 +613,8 @@ static int shiftfs_getattr(const struct path *path, struct kstat *stat,struct inode *reali = real->d_inode;const struct inode_operations *iop = reali->i_op;struct path newpath = { .mnt = path->dentry->d_sb->s_fs_info, .dentry = real }; +struct user_namespace *from_ns = reali->i_sb->s_user_ns; +struct user_namespace *to_ns = inode->i_sb->s_user_ns;int err =0 ;if (iop->getattr)
@@ -624,8 +626,8 @@ static int shiftfs_getattr(const struct path *path, struct kstat *stat,return err;/* transform the underlying id */ - stat->uid =make_kuid (inode->i_sb->s_user_ns,__kuid_val (stat->uid)); - stat->gid =make_kgid (inode->i_sb->s_user_ns,__kgid_val (stat->gid)); + stat->uid =shift_kuid (from_ns, to_ns, stat->uid); + stat->gid =shift_kgid (from_ns, to_ns, stat->gid);return 0 ; }
Signature · 1 line
2.19.1
RFC PATCH 5/6 shiftfs: add support for posix acls
In reply to Seth Forshee--- fs/Kconfig | 10 +++ fs/shiftfs.c | 185 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+)
fs/Kconfig +10 -0
@@ -121,6 +121,16 @@ config SHIFT_FS unprivileged containers can use this to mount root volumes using this technique. +config SHIFT_FS_POSIX_ACL + bool "shiftfs POSIX Access Control Lists" + depends on SHIFT_FS + select FS_POSIX_ACL + help + POSIX Access Control Lists (ACLs) support permissions for users and + groups beyond the owner/group/world scheme. + + If you don't know what Access Control Lists are, say N. + menu "Caches" source "fs/fscache/Kconfig"
fs/shiftfs.c +185 -0
215 lines not on this page · raw message
Signature · 1 line
2.19.1
RFC PATCH 6/6 shiftfs: support nested shiftfs mounts
In reply to Seth Forsheeshiftfs mounts cannot be nested for two reasons -- global CAP_SYS_ADMIN is required to set up a mark mount, and a single functional shiftfs mount meets the filesystem stacking depth limit.
The CAP_SYS_ADMIN requirement can be relaxed. All of the kernel ids in a mount must be within that mount's s_user_ns, so all that is needed is CAP_SYS_ADMIN within that s_user_ns.
The stack depth issue can be worked around with a couple of adjustments. First, a mark mount doesn't really need to count against the stacking depth as it doesn't contribute to the call stack depth during filesystem operations. Therefore the mount over the mark mount only needs to count as one more than the lower filesystems stack depth.
Second, when the lower mount is shiftfs we can just skip down to that mount's lower filesystem and shift ids relative to that. There is no reason to shift ids twice, and the lower path has already been marked safe for id shifting by a user privileged towards all ids in that mount's user ns.
--- fs/shiftfs.c | 68 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 22 deletions(-)
fs/shiftfs.c +46 -22
@@ -930,7 +930,7 @@ static int shiftfs_fill_super(struct super_block *sb, void *raw_data,struct shiftfs_data *data = raw_data;char *name =kstrdup (data->path,GFP_KERNEL );int err = -ENOMEM ; -struct shiftfs_super_info *ssi =NULL ; +struct shiftfs_super_info *ssi =NULL , *mp_ssi;struct path path;struct dentry *dentry;
@@ -946,11 +946,7 @@ static int shiftfs_fill_super(struct super_block *sb, void *raw_data,if (err) goto out; -/* to mark a mount point, must be real root */ -if (ssi->mark && !capable (CAP_SYS_ADMIN )) - goto out; - -/* else to mount a mark, must be userns admin */ +/* to mount a mark, must be userns admin */ if (!ssi->mark && !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) goto out;
@@ -962,41 +958,66 @@ static int shiftfs_fill_super(struct super_block *sb, void *raw_data,if (!S_ISDIR (path.dentry->d_inode->i_mode)) { err = -ENOTDIR ; - goto out_put; - } - - sb->s_stack_depth = path.dentry->d_sb->s_stack_depth +1 ; -if (sb->s_stack_depth >FILESYSTEM_MAX_STACK_DEPTH ) { -printk (KERN_ERR "shiftfs: maximum stacking depth exceeded\n" ); - err = -EINVAL ; - goto out_put; + goto out_put_path; }if (ssi->mark) { +struct super_block *lower_sb = path.mnt->mnt_sb; + +/* to mark a mount point, must root wrt lower s_user_ns */ +if (!ns_capable (lower_sb->s_user_ns,CAP_SYS_ADMIN )) + goto out_put_path; + +/* * this part is visible unshifted, so make sure no * executables that could be used to give suid * privileges */ sb->s_iflags =SB_I_NOEXEC ; - ssi->mnt = path.mnt; - dentry = path.dentry; - }else { -struct shiftfs_super_info *mp_ssi; +/* +* Handle nesting of shiftfs mounts by referring this mark +* mount back to the original mark mount. This is more +* efficient and alleviates concerns about stack depth. +*/ +if (lower_sb->s_magic ==SHIFTFS_MAGIC ) { + mp_ssi = lower_sb->s_fs_info; + +/* Doesn't make sense to mark a mark mount */ +if (mp_ssi->mark) { + err = -EINVAL ; + goto out_put_path; + } + + ssi->mnt =mntget (mp_ssi->mnt); + dentry =dget (path.dentry->d_fsdata); + }else { + ssi->mnt =mntget (path.mnt); + dentry =dget (path.dentry); + } + }else {/* * this leg executes if we're admin capable in * the namespace, so be very careful */ if (path.dentry->d_sb->s_magic !=SHIFTFS_MAGIC ) - goto out_put; + goto out_put_path; mp_ssi = path.dentry->d_sb->s_fs_info;if (!mp_ssi->mark) - goto out_put; + goto out_put_path; ssi->mnt =mntget (mp_ssi->mnt); dentry =dget (path.dentry->d_fsdata); -path_put (&path); } + + sb->s_stack_depth = dentry->d_sb->s_stack_depth +1 ; +if (sb->s_stack_depth >FILESYSTEM_MAX_STACK_DEPTH ) { +printk (KERN_ERR "shiftfs: maximum stacking depth exceeded\n" ); + err = -EINVAL ; + goto out_put_mnt; + } + +path_put (&path); ssi->userns =get_user_ns (dentry->d_sb->s_user_ns); sb->s_fs_info = ssi; sb->s_magic =SHIFTFS_MAGIC ;
@@ -1009,7 +1030,10 @@ static int shiftfs_fill_super(struct super_block *sb, void *raw_data,return 0 ; - out_put: + out_put_mnt: +mntput (ssi->mnt); +dput (dentry); + out_put_path:path_put (&path); out:kfree (name);
Signature · 1 line
2.19.1
Re: [RFC PATCH 6/6] shiftfs: support nested shiftfs mounts
Quoted 10 lines · Seth Forshee
shiftfs mounts cannot be nested for two reasons -- global
CAP_SYS_ADMINis required to set up a mark mount, and a single functional shiftfs mount meets the filesystem stacking depth limit.The
CAP_SYS_ADMINrequirement can be relaxed. All of the kernel ids in a mount must be within that mount'ss_user_ns, so all that is needed isCAP_SYS_ADMINwithin thats_user_ns.
Quoted 6 lines
The stack depth issue can be worked around with a couple of adjustments. First, a mark mount doesn't really need to count against the stacking depth as it doesn't contribute to the call stack depth during filesystem operations. Therefore the mount over the mark mount only needs to count as one more than the lower filesystems stack depth.
That's true, but it also highlights the point that the "mark" sb is completely unneeded and it really is just a nice little hack. All the information it really stores is a lower mount reference, a lower root dentry and a declarative statement "I am shiftable!".
Come to think about it, "container shiftable" really isn't that different from NFS export with "no_root_squash" and auto mounted USB drive.
I mean the shifting itself is different of course, but the declaration, not so much.
If I am allowing sudoers on another machine to mess with root owned files visible
on my machine, I am pretty much have the same issues as container admins accessing root owned files on my init_user_ns filesystem. In all those cases, I'd better not be executing suid binaries from the untrusted "external" source.
Instead of mounting a dummy filesystem to make the declaration, you could get the same thing with:
mount(path, path, "none", MS_BIND | MS_EXTERN | MS_NOEXEC)
and all you need to do is add MS_EXTERN (MS_SHIFTABLE MS_UNTRUSTED or whatnot) constant to uapi and manage to come up good man page description.
Then users could actually mount a filesystem in init_user_ns MS_EXTERN and avoid the extra bind mount step (for a full filesystem tree export). Declaring a mounted image MS_EXTERN has merits on its own even without containers and shitfs, for example with pluggable storage. Other LSMs could make good use of that declaration.
Quoted 27 lines
Second, when the lower mount is shiftfs we can just skip down to that mount's lower filesystem and shift ids relative to that. There is no reason to shift ids twice, and the lower path has already been marked safe for id shifting by a user privileged towards all ids in that mount's user ns.
Signed-off-by Seth Forshee <[email protected]>--- fs/shiftfs.c | 68 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 22 deletions(-)fs/shiftfs.c +1
-1@@ -930,7 +930,7 @@ static int shiftfs_fill_super(struct super_block *sb, void *raw_data,struct shiftfs_data *data = raw_data;char *name =kstrdup (data->path,GFP_KERNEL );int err = -ENOMEM ; -struct shiftfs_super_info *ssi =NULL ; +struct shiftfs_super_info *ssi =NULL , *mp_ssi;struct path path;struct dentry *dentry;@@ -946,11 +946,7 @@ static int shiftfs_fill_super(struct super_block *sb, void *raw_data,if (err)
fs/shiftfs.c static int shiftfs_fill_super(struct super_block *sb, void *raw_data, · line 951
Quoted 10 lines
fs/shiftfs.c +1
-5goto out; -/* to mark a mount point, must be real root */ -if (ssi->mark && !capable (CAP_SYS_ADMIN )) - goto out; - -/* else to mount a mark, must be userns admin */ +/* to mount a mark, must be userns admin */ if (!ssi->mark && !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) goto out;
Isn't this check performed by vfs anyway? i.e. in mount_nodev() -> sget()
Quoted 20 lines
fs/shiftfs.c +4
-8@@ -962,41 +958,66 @@ static int shiftfs_fill_super(struct super_block *sb, void *raw_data,if (!S_ISDIR (path.dentry->d_inode->i_mode)) { err = -ENOTDIR ; - goto out_put; - } - - sb->s_stack_depth = path.dentry->d_sb->s_stack_depth +1 ; -if (sb->s_stack_depth >FILESYSTEM_MAX_STACK_DEPTH ) { -printk (KERN_ERR "shiftfs: maximum stacking depth exceeded\n" ); - err = -EINVAL ; - goto out_put; + goto out_put_path; }if (ssi->mark) { +struct super_block *lower_sb = path.mnt->mnt_sb; + +/* to mark a mount point, must root wrt lower s_user_ns */
fs/shiftfs.c static int shiftfs_fill_super(struct super_block *sb, void *raw_data, · line 977
Quoted 10 lines
fs/shiftfs.c +4
-0+if (!ns_capable (lower_sb->s_user_ns,CAP_SYS_ADMIN )) + goto out_put_path; + +/* * this part is visible unshifted, so make sure no * executables that could be used to give suid * privileges */ sb->s_iflags =SB_I_NOEXEC ;
As commented on cover letter, why allow access to any files besides root at all? In fact, the only justification for a dummy sb (instead of bind mount with MS_EXTERN flag) would be in order to override inode operations with noop ops to prevent access to unshifted files from within container.
Thanks,
Amir.
Re: [RFC PATCH 6/6] shiftfs: support nested shiftfs mounts
Quoted 18 lines · Amir Goldstein
Quoted 10 lines · Seth Forshee
shiftfs mounts cannot be nested for two reasons -- global
CAP_SYS_ADMINis required to set up a mark mount, and a single functional shiftfs mount meets the filesystem stacking depth limit.The
CAP_SYS_ADMINrequirement can be relaxed. All of the kernel ids in a mount must be within that mount'ss_user_ns, so all that is needed isCAP_SYS_ADMINwithin thats_user_ns.Quoted 6 lines
The stack depth issue can be worked around with a couple of adjustments. First, a mark mount doesn't really need to count against the stacking depth as it doesn't contribute to the call stack depth during filesystem operations. Therefore the mount over the mark mount only needs to count as one more than the lower filesystems stack depth.
Quoted 4 lines
That's true, but it also highlights the point that the "mark" sb is completely unneeded and it really is just a nice little hack. All the information it really stores is a lower mount reference, a lower root dentry and a declarative statement "I am shiftable!".
Seems I should have saved some of the things I said in my previous response for this one. As you no doubt gleaned from that email, I agree with this.
Quoted 16 lines
Come to think about it, "container shiftable" really isn't that different from NFS export with "
no_root_squash" and auto mounted USB drive.
I mean the shifting itself is different of course, but the declaration, not so much.
If I am allowing sudoers on another machine to mess with root owned files visible
on my machine, I am pretty much have the same issues as container admins accessing root owned files on myinit_user_nsfilesystem. In all those cases, I'd better not be executing suid binaries from the untrusted "external" source.Instead of mounting a dummy filesystem to make the declaration, you could get the same thing with:
mount(path, path, "none", MS_BIND | MS_EXTERN | MS_NOEXEC)
and all you need to do is addMS_EXTERN(MS_SHIFTABLEMS_UNTRUSTEDor whatnot) constant to uapi and manage to come up good man page description.
Quoted 5 lines
Then users could actually mount a filesystem in
init_user_nsMS_EXTERNand avoid the extra bind mount step (for a full filesystem tree export). Declaring a mounted imageMS_EXTERNhas merits on its own even without containers and shitfs, for example with pluggable storage. Other LSMs could make good use of that declaration.
I'm missing how we figure out the target user ns in this scheme. We need a context with privileges towards the source path's s_user_ns to say it's okay to shift ids for the files under the source path, and then we need a target user ns for the id shifts. Currently the target is current_user_ns when the final shiftfs mount is created.
So, how do we determine the target s_user_ns in your scheme?
Quoted 30 lines
Quoted 30 lines
Second, when the lower mount is shiftfs we can just skip down to that mount's lower filesystem and shift ids relative to that. There is no reason to shift ids twice, and the lower path has already been marked safe for id shifting by a user privileged towards all ids in that mount's user ns.
Signed-off-by Seth Forshee <[email protected]>--- fs/shiftfs.c | 68 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 22 deletions(-)fs/shiftfs.c +1
-1@@ -930,7 +930,7 @@ static int shiftfs_fill_super(struct super_block *sb, void *raw_data,struct shiftfs_data *data = raw_data;char *name =kstrdup (data->path,GFP_KERNEL );int err = -ENOMEM ; -struct shiftfs_super_info *ssi =NULL ; +struct shiftfs_super_info *ssi =NULL , *mp_ssi;struct path path;struct dentry *dentry;@@ -946,11 +946,7 @@ static int shiftfs_fill_super(struct super_block *sb, void *raw_data,if (err) goto out;
Quoted 10 lines
fs/shiftfs.c static int shiftfs_fill_super(struct super_block *sb, void *raw_data, · line 951
Quoted 8 lines
fs/shiftfs.c +1
-5-/* to mark a mount point, must be real root */ -if (ssi->mark && !capable (CAP_SYS_ADMIN )) - goto out; - -/* else to mount a mark, must be userns admin */ +/* to mount a mark, must be userns admin */ if (!ssi->mark && !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) goto out;Isn't this check performed by vfs anyway? i.e. in
mount_nodev()->sget()
Yeah, I noticed that too. I left it in for the moment to emphasize the change I was making, but it can be removed.
Quoted 28 lines
Quoted 28 lines
fs/shiftfs.c +8
-8@@ -962,41 +958,66 @@ static int shiftfs_fill_super(struct super_block *sb, void *raw_data,if (!S_ISDIR (path.dentry->d_inode->i_mode)) { err = -ENOTDIR ; - goto out_put; - } - - sb->s_stack_depth = path.dentry->d_sb->s_stack_depth +1 ; -if (sb->s_stack_depth >FILESYSTEM_MAX_STACK_DEPTH ) { -printk (KERN_ERR "shiftfs: maximum stacking depth exceeded\n" ); - err = -EINVAL ; - goto out_put; + goto out_put_path; }if (ssi->mark) { +struct super_block *lower_sb = path.mnt->mnt_sb; + +/* to mark a mount point, must root wrt lower s_user_ns */ +if (!ns_capable (lower_sb->s_user_ns,CAP_SYS_ADMIN )) + goto out_put_path; + +/* * this part is visible unshifted, so make sure no * executables that could be used to give suid
Quoted 8 lines
fs/shiftfs.c static int shiftfs_fill_super(struct super_block *sb, void *raw_data, · line 977
Quoted 3 lines
fs/shiftfs.c +0
-0* privileges */ sb->s_iflags =SB_I_NOEXEC ;As commented on cover letter, why allow access to any files besides root at all? In fact, the only justification for a dummy sb (instead of bind mount with
MS_EXTERNflag) would be in order to override inode operations with noop ops to prevent access to unshifted files from within container.
Summarizing my response to the other message, if the mark mount is kept (and I would prefer that it isn't kept) that seems reasonable to me.
Thanks,
Seth
Re: [RFC PATCH 6/6] shiftfs: support nested shiftfs mounts
Quoted 51 lines · Seth Forshee
Quoted 18 lines · Amir Goldstein
Quoted 10 lines · Seth Forshee
shiftfs mounts cannot be nested for two reasons -- global
CAP_SYS_ADMINis required to set up a mark mount, and a single functional shiftfs mount meets the filesystem stacking depth limit.The
CAP_SYS_ADMINrequirement can be relaxed. All of the kernel ids in a mount must be within that mount'ss_user_ns, so all that is needed isCAP_SYS_ADMINwithin thats_user_ns.Quoted 6 lines
The stack depth issue can be worked around with a couple of adjustments. First, a mark mount doesn't really need to count against the stacking depth as it doesn't contribute to the call stack depth during filesystem operations. Therefore the mount over the mark mount only needs to count as one more than the lower filesystems stack depth.
Quoted 4 lines
That's true, but it also highlights the point that the "mark" sb is completely unneeded and it really is just a nice little hack. All the information it really stores is a lower mount reference, a lower root dentry and a declarative statement "I am shiftable!".
Seems I should have saved some of the things I said in my previous response for this one. As you no doubt gleaned from that email, I agree with this.
Quoted 16 lines
Come to think about it, "container shiftable" really isn't that different from NFS export with "
no_root_squash" and auto mounted USB drive.
I mean the shifting itself is different of course, but the declaration, not so much.
If I am allowing sudoers on another machine to mess with root owned files visible
on my machine, I am pretty much have the same issues as container admins accessing root owned files on myinit_user_nsfilesystem. In all those cases, I'd better not be executing suid binaries from the untrusted "external" source.Instead of mounting a dummy filesystem to make the declaration, you could get the same thing with:
mount(path, path, "none", MS_BIND | MS_EXTERN | MS_NOEXEC)
and all you need to do is addMS_EXTERN(MS_SHIFTABLEMS_UNTRUSTEDor whatnot) constant to uapi and manage to come up good man page description.Quoted 5 lines
Then users could actually mount a filesystem in
init_user_nsMS_EXTERNand avoid the extra bind mount step (for a full filesystem tree export). Declaring a mounted imageMS_EXTERNhas merits on its own even without containers and shitfs, for example with pluggable storage. Other LSMs could make good use of that declaration.
Quoted 8 lines
I'm missing how we figure out the target user ns in this scheme. We need a context with privileges towards the source path's
s_user_nsto say it's okay to shift ids for the files under the source path, and then we need a target user ns for the id shifts. Currently the target iscurrent_user_nswhen the final shiftfs mount is created.So, how do we determine the target
s_user_nsin your scheme?
Unless I am completely misunderstanding shiftfs, I think we are saying the same thing. You said you wish to get rid of the "mark" fs and that you had a POC of implementing the "mark" using xattr.
I'm just saying another option to implement the mark is using a super block flag and you get the target s_user_ns from mnt_sb.
I did miss the fact that a mount flag is not enough, so that makes the bind mount concept fail. Unless, maybe, the mount in the container is a slave mount and the "shiftable" mark is set on the master.
I know too little about mount ns vs. user ns to tell if any of this makes sense.
Feel free to ignore MS_EXTERN idea.
Hopefully, mount API v2 will provide the proper facility to implement mark.
Thanks,
Amir.
Re: [RFC PATCH 6/6] shiftfs: support nested shiftfs mounts
Quoted 58 lines · Amir Goldstein
Quoted 57 lines · Seth Forshee
Quoted 18 lines · Amir Goldstein
Quoted 10 lines · Seth Forshee
shiftfs mounts cannot be nested for two reasons -- global
CAP_SYS_ADMINis required to set up a mark mount, and a single functional shiftfs mount meets the filesystem stacking depth limit.The
CAP_SYS_ADMINrequirement can be relaxed. All of the kernel ids in a mount must be within that mount'ss_user_ns, so all that is needed isCAP_SYS_ADMINwithin thats_user_ns.Quoted 6 lines
The stack depth issue can be worked around with a couple of adjustments. First, a mark mount doesn't really need to count against the stacking depth as it doesn't contribute to the call stack depth during filesystem operations. Therefore the mount over the mark mount only needs to count as one more than the lower filesystems stack depth.
Quoted 4 lines
That's true, but it also highlights the point that the "mark" sb is completely unneeded and it really is just a nice little hack. All the information it really stores is a lower mount reference, a lower root dentry and a declarative statement "I am shiftable!".
Seems I should have saved some of the things I said in my previous response for this one. As you no doubt gleaned from that email, I agree with this.
Quoted 16 lines
Come to think about it, "container shiftable" really isn't that different from NFS export with "
no_root_squash" and auto mounted USB drive.
I mean the shifting itself is different of course, but the declaration, not so much.
If I am allowing sudoers on another machine to mess with root owned files visible
on my machine, I am pretty much have the same issues as container admins accessing root owned files on myinit_user_nsfilesystem. In all those cases, I'd better not be executing suid binaries from the untrusted "external" source.Instead of mounting a dummy filesystem to make the declaration, you could get the same thing with:
mount(path, path, "none", MS_BIND | MS_EXTERN | MS_NOEXEC)
and all you need to do is addMS_EXTERN(MS_SHIFTABLEMS_UNTRUSTEDor whatnot) constant to uapi and manage to come up good man page description.Quoted 5 lines
Then users could actually mount a filesystem in
init_user_nsMS_EXTERNand avoid the extra bind mount step (for a full filesystem tree export). Declaring a mounted imageMS_EXTERNhas merits on its own even without containers and shitfs, for example with pluggable storage. Other LSMs could make good use of that declaration.I'm missing how we figure out the target user ns in this scheme. We need a context with privileges towards the source path's
s_user_nsto say it's okay to shift ids for the files under the source path, and then we need a target user ns for the id shifts. Currently the target iscurrent_user_nswhen the final shiftfs mount is created.
Quoted 6 lines
Quoted 2 lines
So, how do we determine the target
s_user_nsin your scheme?Unless I am completely misunderstanding shiftfs, I think we are saying the same thing. You said you wish to get rid of the "mark" fs and that you had a POC of implementing the "mark" using xattr.
The PoC was using filesystem contexts and (an earlier version of) the new mount API, but yes I do think we're in agreement about the mark fs being awkward.
Quoted 6 lines
I'm just saying another option to implement the mark is using a super block flag and you get the target
s_user_nsfrommnt_sb.
I did miss the fact that a mount flag is not enough, so that makes the bind mount concept fail. Unless, maybe, the mount in the container is a slave mount and the "shiftable" mark is set on the master.
I know too little about mount ns vs. user ns to tell if any of this makes sense.
We need a source and target user ns for the shifting, currently they are the lower filesystem's s_user_ns and the s_user_ns of the shiftfs fs (which is current_user_ns at the time the real shiftfs fs is mounted). I think doing it this way makes sense.
The problem with the slave mount idea is that s_user_ns for the shiftfs sb is still not the target ns for the id shifting, which is going to cause all kinds of problems. Unless all of this is done in the vfs, then it could really be a bind mount. The shifting could be done based on the mount namespace's user_ns and all permission checks in the vfs could take this into account. This approach certainly has benefits and is one of the things I want to discuss.
Quoted 3 lines
Feel free to ignore
MS_EXTERNidea.Hopefully, mount API v2 will provide the proper facility to implement mark.
The approach I took was to have the source user_ns context set up an fd that was passed to the target user_ns context. Then that fd was used to attach the mount to the mount tree, and when that happened the target s_user_ns was set. It worked, but there were some awkware bits. I haven't kept up with the changes since then to know if things are any better or worse with the current patches.
Thanks,
Seth
Re: [RFC PATCH 6/6] shiftfs: support nested shiftfs mounts
In reply to Amir GoldsteinQuoted 79 lines · Amir Goldstein
Quoted 77 lines · Seth Forshee
Quoted 20 lines · Amir Goldstein
Quoted 11 lines · Seth Forshee
shiftfs mounts cannot be nested for two reasons -- global
CAP_SYS_ADMINis required to set up a mark mount, and a single functional shiftfs mount meets the filesystem stacking depth limit.The
CAP_SYS_ADMINrequirement can be relaxed. All of the kernel ids in a mount must be within that mount'ss_user_ns, so all that
is needed isCAP_SYS_ADMINwithin thats_user_ns.Quoted 6 lines
The stack depth issue can be worked around with a couple of adjustments. First, a mark mount doesn't really need to count against the stacking depth as it doesn't contribute to the call stack depth during filesystem operations. Therefore the mount over the mark mount only needs to count as one more than the lower filesystems stack depth.
Quoted 6 lines
That's true, but it also highlights the point that the "mark" sb is
completely unneeded and it really is just a nice little hack. All the information it really stores is a lower mount reference, a lower root dentry and a declarative statement "I am shiftable!".Seems I should have saved some of the things I said in my previous response for this one. As you no doubt gleaned from that email, I agree
with this.Quoted 24 lines
Come to think about it, "container shiftable" really isn't that different from
NFS export with "no_root_squash" and auto mounted USB drive. I mean the shifting itself is different of course, but the declaration, not so much.
If I am allowing sudoers on another machine to mess with root owned
files visible
on my machine, I am pretty much have the same issues as container admins
accessing root owned files on myinit_user_nsfilesystem. In all those cases,
I'd better not be executing suid binaries from the untrusted "external" source.Instead of mounting a dummy filesystem to make the declaration, you could
get the same thing with:
mount(path, path, "none", MS_BIND | MS_EXTERN | MS_NOEXEC)
and all you need to do is addMS_EXTERN(MS_SHIFTABLEMS_UNTRUSTED
or whatnot) constant to uapi and manage to come up good man page description.Quoted 9 lines
Then users could actually mount a filesystem in
init_user_nsMS_EXTERNand
avoid the extra bind mount step (for a full filesystem tree export).
Declaring a mounted imageMS_EXTERNhas merits on its own even without
containers and shitfs, for example with pluggable storage. Other LSMs could make
good use of that declaration.I'm missing how we figure out the target user ns in this scheme. We need
a context with privileges towards the source path'ss_user_nsto say
it's okay to shift ids for the files under the source path, and then we
need a target user ns for the id shifts. Currently the target iscurrent_user_nswhen the final shiftfs mount is created.
Quoted 6 lines
Quoted 2 lines
So, how do we determine the target
s_user_nsin your scheme?Unless I am completely misunderstanding shiftfs, I think we are saying the same thing. You said you wish to get rid of the "mark" fs and that you had a POC of implementing the "mark" using xattr.
I've got one of these too ... it works nicely.
Quoted 2 lines
I'm just saying another option to implement the mark is using a super block flag and you get the target
s_user_nsfrommnt_sb.
This works a lot less well because the entire mount becomes shiftable, not just a subtree, which is suboptimal for the unprivileged use case. The idea for getting around this was the one Ted mentioned of attaching properties to the vfsmount structure (he'd like to use this for case insensitive name comparisons in subtrees), but that requires rethreading quite a few vfs calls to take a struct path instead of a struct dentry.
James
[cc: linux-unionfs
It should the mailing list for *all* "stacking fs". We have a lot of common problems I think ;-) ]
Quoted 11 lines · Seth Forshee
I've done some work to fix and enhance shiftfs for a number of use cases, so that we would have an idea what a more full-featured shiftfs would look like. I'm intending for these to serve as a point of reference for discussing id shifting mounts/filesystems at plumbers in a couple of weeks [1].
Note that these are based on 4.18, and I've added a small fix to James' most recent patch to fix a build issue there. To work with 4.19 they will need a number of updates due to changes in the vfs.
Seth,
I like the way you addressed my concerns about nesting and stacking depth. Will provide specific nits on patch.
In preparation to the Plumbers talk (which I will not be attending), I wanted to get your opinion on the matters I brought up last time:
https://marc.info/?l=linux-fsdevel&m=153013920904844&w=2
1) Having seen what it takes to catch up with overlayfs w.r.t inotify bugs and having peeked into 4.19 to see what work you still have lined up for you to bring shitfs up to speed with vfs, did you have time to look into my proposal for sharing code with overlayfs in the manner that I have implemented the snapshotfs POC?
https://github.com/amir73il/linux/commit/25416757f2ca47759f59b115e6461b11898c4f06
Even if you end up not saving a single line of code for shiftfs v1 meaning that all shiftfs_inode_ops are completely separate implementation from overlayfs inode ops, this may still be beneficial to shitfs in the long run.
For example, you may, in fact, won't need to change anything to work with v4.19. shittfs (as an overlayfs alias) would use ovl_file_operations and shiftfs_inode_ops.
Another example, from the top of my head, see what it took to add NFS export support to snapshotfs, because of the code reuse with overlayfs:
https://github.com/amir73il/linux/commit/d082eb615133490ec26fa2efaa80ed4723860893
Its practically the exact same implementation shiftfs would need, so in the far future, shitfs and snapshotfs can share the same export_operations.
2) Regarding this part:
+ /*
+ * this part is visible unshifted, so make sure no + * executables that could be used to give suid + * privileges + */
+ sb->s_iflags = SB_I_NOEXEC;
Why would you want to make the unshifted fs visible at all? Is there a requirement for container users to access the unshifted fs content? Is there a requirement for container admin to mount shitfted fs NOT from the root of the marked mount?
If those are not required, then I propose NOOP inode operations for the unshifted fs, specifically, empty readdir, just enough ops to be able to use the mark mount point as the shitfed mount source - no more.
Thanks,
Amir.
Quoted 17 lines · Amir Goldstein
[cc: linux-unionfs
It should the mailing list for *all* "stacking fs". We have a lot of common problems I think ;-) ]Quoted 11 lines · Seth Forshee
I've done some work to fix and enhance shiftfs for a number of use cases, so that we would have an idea what a more full-featured shiftfs would look like. I'm intending for these to serve as a point of reference for discussing id shifting mounts/filesystems at plumbers in a couple of weeks [1].
Note that these are based on 4.18, and I've added a small fix to James' most recent patch to fix a build issue there. To work with 4.19 they will need a number of updates due to changes in the vfs.
Quoted 8 lines
Seth,
I like the way you addressed my concerns about nesting and stacking depth. Will provide specific nits on patch.
In preparation to the Plumbers talk (which I will not be attending), I wanted to get your opinion on the matters I brought up last time:
https://marc.info/?l=linux-fsdevel&m=153013920904844&w=2
I want the session at plumbers to not be a "talk" but more of a discussion of the sorts of things you raise below. But I'm also happy to talk about them here.
Quoted 7 lines
1) Having seen what it takes to catch up with overlayfs w.r.t inotify bugs and having peeked into 4.19 to see what work you still have lined up for you to bring shitfs up to speed with vfs, did you have time to look into my proposal for sharing code with overlayfs in the manner that I have implemented the snapshotfs POC?
https://github.com/amir73il/linux/commit/25416757f2ca47759f59b115e6461b11898c4f06
Quoted 7 lines
Even if you end up not saving a single line of code for shiftfs v1 meaning that all
shiftfs_inode_opsare completely separate implementation from overlayfs inode ops, this may still be beneficial to shitfs in the long run.
For example, you may, in fact, won't need to change anything to work with v4.19. shittfs (as an overlayfs alias) would useovl_file_operationsandshiftfs_inode_ops.
I don't recall seeing the shapshotfs patches before. If id shifting remains an overlay-style fs and not a feature of the vfs, then I absolutely think something like this will make life much easier.
Quoted 15 lines
Another example, from the top of my head, see what it took to add NFS export support to snapshotfs, because of the code reuse with overlayfs:
https://github.com/amir73il/linux/commit/d082eb615133490ec26fa2efaa80ed4723860893
Its practically the exact same implementation shiftfs would need, so in the far future, shitfs and snapshotfs can share the sameexport_operations.2) Regarding this part:
(quoted diff) +6
-0+ /* + * this part is visible unshifted, so make sure no + * executables that could be used to give suid + * privileges + */ + sb->s_iflags = SB_I_NOEXEC;
Quoted 8 lines
Why would you want to make the unshifted fs visible at all? Is there a requirement for container users to access the unshifted fs content? Is there a requirement for container admin to mount shitfted fs NOT from the root of the marked mount?
If those are not required, then I propose NOOP inode operations for the unshifted fs, specifically, empty readdir, just enough ops to be able to use the mark mount point as the shitfed mount source - no more.
This is part of the original implementation that I didn't touch with these updates. Imo the mark mount is kind of kludgy, and I'd like to see it done a different way.
A couple of alternatives have been suggested. One was to use xattrs for marking, or I did a PoC with an older version of the new mount API patches where an fsfd was passed to the less privileged context that it could attach to its mount tree:
https://lkml.kernel.org/r/20180717133847.GB15620@ubuntu-xps13
Either of these can accomplish the same things as the mark mount with better control over who can create an id-shifted mount of the subtree.
However if the mark mount is kept then no-op inode operations seems reasonable to me.
Thanks,
Seth