shiftfs: uid/gid shifting filesystem (s_user_ns version)
v2 cover shiftfs: uid/gid shifting filesystem (s_user_ns version)
This is a rewrite of the original shiftfs code to make use of super block user namespaces. I've also removed the mappings passed in as mount options in favour of using the mappings in s_user_ns. The upshot is that it probably needs retesting for all the bugs people found, since there's a lot of new code, and the use case has changed. Now, to use it, you have to mark the filesystems you want to be mountable inside a user namespace as root:
mount -t shiftfs -o mark <origin> <mark location>
The origin should be inaccessible to the unprivileged user, and the access to the <mark location> can be controlled by the usual filesystem permissions. Once this is done, any user who can get access to the <mark location> can do (as the local user namespace root):
mount -t shiftfs <mark location> <somewhere in my local mount ns>
And they will be able to write at their user namespace shifts, but have the interior view of the uid/gid be what appears on the <origin>
In using the s_user_ns, a lot of the code actually simplified, because now our credential shifting code simply becomes use the <origin> s_user_ns and the shifted uid/gid. The updated d_real() code from overlayfs is also used, so shiftfs now no-longer needs its own file operations.
---
[original blurb]
My use case for this is that I run a lot of unprivileged architectural emulation containers on my system using user namespaces. Details here:
http://blog.hansenpartnership.com/unprivileged-build-containers/
They're mostly for building non-x86 stuff (like aarch64 and arm secure boot and mips images). For builds, I have all the environments in my home directory with downshifted uids; however, sometimes I need to use them to administer real images that run on systems, meaning the uids are the usual privileged ones not the downshifted ones. The only current choice I have is to start the emulation as root so the uid/gids match. The reason for this filesystem is to use my standard unprivileged containers to maintain these images. The way I do this is crack the image with a loop and then shift the uids before bringing up the container. I usually loop mount into /var/tmp/images/, so it's owned by real root there:
jarvis:~ # ls -l /var/tmp/images/mips|head -4
total 0
drwxr-xr-x 1 root root 8192 May 12 08:33 bin
drwxr-xr-x 1 root root 6 May 12 08:33 boot
drwxr-xr-x 1 root root 167 May 12 08:33 dev
And I usually run my build containers with a uid_map of
0 100000 1000
1000 1000 1
65534 101000 1(maps 0-999 shifted, then shifts nobody to 1000 and keeps my uid [1000] fixed so I can mount my home directory into the namespace) and something similar with gid_map. So I shift mount the mips image with
mount -t shiftfs -o idmap=0:100000:1000,uidmap=65534:101000:1,gidmap=0:100000:100,gidmap=101:100101:899,gidmap=65533:101000:2 /var/tmp/images/mips /home/jejb/containers/mips
and I now see it as
jejb@jarvis:~> ls -l containers/mips|head -4
total 0
drwxr-xr-x 1 100000 100000 8192 May 12 08:33 bin/
drwxr-xr-x 1 100000 100000 6 May 12 08:33 boot/
drwxr-xr-x 1 100000 100000 167 May 12 08:33 dev/
Like my usual unprivileged build roots and I can now use an unprivileged container to enter and administer the image.
It seems like a lot of container systems need to do something similar when they try and provide unprivileged access to standard images.
Right at the moment, the security mechanism only allows root in the
host to use this, but it's not impossible to come up with a scheme for marking trees that can safely be shift mounted by unprivileged user namespaces.
James
---
James Bottomley (1):
shiftfs: uid/gid shifting bind mount
fs/Kconfig | 8 + fs/Makefile | 1 + fs/shiftfs.c | 847 +++++++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/magic.h | 2 + 4 files changed, 858 insertions(+) create mode 100644 fs/shiftfs.c
Signature · 1 line
2.6.6
RFC v2 1/1 shiftfs: uid/gid shifting bind mount
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.
---
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 | 847 +++++++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/magic.h | 2 + 4 files changed, 858 insertions(+) create mode 100644 fs/shiftfs.c
fs/Kconfig +8 -0
@@ -104,6 +104,14 @@ source "fs/autofs4/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 +847 -0
847 lines not on this page · raw message
include/uapi/linux/magic.h +2 -0
@@ -85,4 +85,6 @@#define BALLOON_KVM_MAGIC 0x13661366#define ZSMALLOC_MAGIC 0x58295829 +#define SHIFTFS_MAGIC 0x6a656a62 +#endif /* __LINUX_MAGIC_H__ */
Signature · 1 line
2.6.6
Re: [RFC v2 1/1] shiftfs: uid/gid shifting bind mount
On Mon, Feb 20, 2017 at 04:47:05PM -0800, James Bottomley wrote:
[..]
fs/shiftfs.c line 469
Quoted 5 lines
fs/shiftfs.c new file +5
-0+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;
newi needs to be initialized to NULL. Otherwise if a file does not exist, we try to call d_splice_alias() with uninitialized value of newi.
This crashes for the new file creation case.
Vivek
Re: [RFC v2 1/1] shiftfs: uid/gid shifting bind mount
Quoted 14 lines · Vivek Goyal
On Mon, Feb 20, 2017 at 04:47:05PM -0800, James Bottomley wrote:
[..]
fs/shiftfs.c line 469
Quoted 6 lines
+static struct dentry *shiftfs_lookup(struct inode *dir, struct dentry *dentry,
fs/shiftfs.c new file +4
-0+ unsigned int flags) +{ +struct dentry *real = dir->i_private, *new; +struct inode *reali = real->d_inode, *newi;newi needs to be initialized to NULL. Otherwise if a file does not exist, we try to call
d_splice_alias()with uninitialized value of newi.
Quoted 1 line
This crashes for the new file creation case.
I think you've still got an older version. The code around that region should read:
oldcred = shiftfs_new_creds(&newcred, dentry->d_sb);
new = lookup_one_len(dentry->d_name.name, real, dentry->d_name.len);
shiftfs_old_creds(oldcred, &newcred);
inode_unlock(reali);
if (IS_ERR(new))
return new;
dentry->d_fsdata = new;
newi = NULL;
if (!new->d_inode)
goto out;
newi = shiftfs_new_inode(dentry->d_sb, new->d_inode->i_mode, new);
if (!newi) {
dput(new);
return ERR_PTR(-ENOMEM);
}out:
return d_splice_alias(newi, dentry);
So newi is set to NULL before out is taken. I checked: this hunk did make it to the v2 patch.
James
Re: [RFC v2 1/1] shiftfs: uid/gid shifting bind mount
Quoted 42 lines · James Bottomley
Quoted 14 lines · Vivek Goyal
On Mon, Feb 20, 2017 at 04:47:05PM -0800, James Bottomley wrote:
[..]
fs/shiftfs.c line 469
Quoted 6 lines
+static struct dentry *shiftfs_lookup(struct inode *dir, struct dentry *dentry,
fs/shiftfs.c new file +4
-0+ unsigned int flags) +{ +struct dentry *real = dir->i_private, *new; +struct inode *reali = real->d_inode, *newi;newi needs to be initialized to NULL. Otherwise if a file does not exist, we try to call
d_splice_alias()with uninitialized value of newi.Quoted 1 line
This crashes for the new file creation case.
I think you've still got an older version. The code around that region should read:
oldcred = shiftfs_new_creds(&newcred, dentry->d_sb); new = lookup_one_len(dentry->d_name.name, real, dentry->d_name.len); shiftfs_old_creds(oldcred, &newcred); inode_unlock(reali); if (IS_ERR(new)) return new; dentry->d_fsdata = new; newi = NULL; if (!new->d_inode) goto out; newi = shiftfs_new_inode(dentry->d_sb, new->d_inode->i_mode, new); if (!newi) { dput(new); return ERR_PTR(-ENOMEM); }
Quoted 4 lines
out:
return d_splice_alias(newi, dentry);So newi is set to NULL before out is taken. I checked: this hunk did make it to the v2 patch.
Aha.., you are right. I did not pay attention of newi=NULL in V2 and went on to provide feeback based on my V1 testing.
Vivek
Re: [RFC v2 1/1] shiftfs: uid/gid shifting bind mount
In reply to James BottomleyOn Mon, Feb 20, 2017 at 04:47:05PM -0800, James Bottomley wrote:
[..]
Review
Quoted 12 lines
(quoted diff) +12
-0+static int shiftfs_readlink(struct dentry *dentry, char __user *data, + int flags) +{ + struct dentry *real = dentry->d_fsdata; + const struct inode_operations *iop = real->d_inode->i_op; + + if (iop->readlink) + return iop->readlink(real, data, flags); + + return -EINVAL; +} +
Hi James,
I was testing shiftfs on top of xfs and noticed readlink() is failing as xfs does not define readlink.
Following patch fixes it for me.
Vivek
--- fs/shiftfs.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
fs/shiftfs.c +1 -5
@@ -179,12 +179,8 @@ static int shiftfs_readlink(struct dentr int flags) {struct dentry *real = dentry->d_fsdata; -const struct inode_operations *iop = real->d_inode->i_op; -if (iop->readlink) -return iop->readlink (real, data, flags); - -return -EINVAL ; +return vfs_readlink (real, data, flags); } static const char *shiftfs_get_link(struct dentry *dentry, struct inode *inode,