mirror of
https://github.com/torvalds/linux.git
synced 2024-11-10 14:11:52 +00:00
fs: introduce dedicated idmap type for mounts
Last cycle we've already made the interaction with idmapped mounts more robust and type safe by introducing the vfs{g,u}id_t type. This cycle we concluded the conversion and removed the legacy helpers. Currently we still pass around the plain namespace that was attached to a mount. This is in general pretty convenient but it makes it easy to conflate filesystem and mount namespaces and what different roles they have to play. Especially for filesystem developers without much experience in this area this is an easy source for bugs. Instead of passing the plain namespace we introduce a dedicated type struct mnt_idmap and replace the pointer with a pointer to a struct mnt_idmap. There are no semantic or size changes for the mount struct caused by this. We then start converting all places aware of idmapped mounts to rely on struct mnt_idmap. Once the conversion is done all helpers down to the really low-level make_vfs{g,u}id() and from_vfs{g,u}id() will take a struct mnt_idmap argument instead of two namespace arguments. This way it becomes impossible to conflate the two, removing and thus eliminating the possibility of any bugs. Fwiw, I fixed some issues in that area a while ago in ntfs3 and ksmbd in the past. Afterwards, only low-level code can ultimately use the associated namespace for any permission checks. Even most of the vfs can be ultimately completely oblivious about this and filesystems will never interact with it directly in any form in the future. A struct mnt_idmap currently encompasses a simple refcount and a pointer to the relevant namespace the mount is idmapped to. If a mount isn't idmapped then it will point to a static nop_mnt_idmap. If it is an idmapped mount it will point to a new struct mnt_idmap. As usual there are no allocations or anything happening for non-idmapped mounts. Everthing is carefully written to be a nop for non-idmapped mounts as has always been the case. If an idmapped mount or mount tree is created a new struct mnt_idmap is allocated and a reference taken on the relevant namespace. For each mount in a mount tree that gets idmapped or a mount that inherits the idmap when it is cloned the reference count on the associated struct mnt_idmap is bumped. Just a reminder that we only allow a mount to change it's idmapping a single time and only if it hasn't already been attached to the filesystems and has no active writers. The actual changes are fairly straightforward. This will have huge benefits for maintenance and security in the long run even if it causes some churn. I'm aware that there's some cost for all of you. And I'll commit to doing this work and make this as painless as I can. Note that this also makes it possible to extend struct mount_idmap in the future. For example, it would be possible to place the namespace pointer in an anonymous union together with an idmapping struct. This would allow us to expose an api to userspace that would let it specify idmappings directly instead of having to go through the detour of setting up namespaces at all. This just adds the infrastructure and doesn't do any conversions. Reviewed-by: Seth Forshee (DigitalOcean) <sforshee@kernel.org> Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
This commit is contained in:
parent
f7adeea9eb
commit
256c8aed2b
176
fs/namespace.c
176
fs/namespace.c
@ -75,6 +75,22 @@ static DECLARE_RWSEM(namespace_sem);
|
||||
static HLIST_HEAD(unmounted); /* protected by namespace_sem */
|
||||
static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
|
||||
|
||||
struct mnt_idmap {
|
||||
struct user_namespace *owner;
|
||||
refcount_t count;
|
||||
};
|
||||
|
||||
/*
|
||||
* Carries the initial idmapping of 0:0:4294967295 which is an identity
|
||||
* mapping. This means that {g,u}id 0 is mapped to {g,u}id 0, {g,u}id 1 is
|
||||
* mapped to {g,u}id 1, [...], {g,u}id 1000 to {g,u}id 1000, [...].
|
||||
*/
|
||||
struct mnt_idmap nop_mnt_idmap = {
|
||||
.owner = &init_user_ns,
|
||||
.count = REFCOUNT_INIT(1),
|
||||
};
|
||||
EXPORT_SYMBOL_GPL(nop_mnt_idmap);
|
||||
|
||||
struct mount_kattr {
|
||||
unsigned int attr_set;
|
||||
unsigned int attr_clr;
|
||||
@ -82,6 +98,7 @@ struct mount_kattr {
|
||||
unsigned int lookup_flags;
|
||||
bool recurse;
|
||||
struct user_namespace *mnt_userns;
|
||||
struct mnt_idmap *mnt_idmap;
|
||||
};
|
||||
|
||||
/* /sys/fs */
|
||||
@ -193,6 +210,104 @@ int mnt_get_count(struct mount *mnt)
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* mnt_idmap_owner - retrieve owner of the mount's idmapping
|
||||
* @idmap: mount idmapping
|
||||
*
|
||||
* This helper will go away once the conversion to use struct mnt_idmap
|
||||
* everywhere has finished at which point the helper will be unexported.
|
||||
*
|
||||
* Only code that needs to perform permission checks based on the owner of the
|
||||
* idmapping will get access to it. All other code will solely rely on
|
||||
* idmappings. This will get us type safety so it's impossible to conflate
|
||||
* filesystems idmappings with mount idmappings.
|
||||
*
|
||||
* Return: The owner of the idmapping.
|
||||
*/
|
||||
struct user_namespace *mnt_idmap_owner(const struct mnt_idmap *idmap)
|
||||
{
|
||||
return idmap->owner;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(mnt_idmap_owner);
|
||||
|
||||
/**
|
||||
* mnt_user_ns - retrieve owner of an idmapped mount
|
||||
* @mnt: the relevant vfsmount
|
||||
*
|
||||
* This helper will go away once the conversion to use struct mnt_idmap
|
||||
* everywhere has finished at which point the helper will be unexported.
|
||||
*
|
||||
* Only code that needs to perform permission checks based on the owner of the
|
||||
* idmapping will get access to it. All other code will solely rely on
|
||||
* idmappings. This will get us type safety so it's impossible to conflate
|
||||
* filesystems idmappings with mount idmappings.
|
||||
*
|
||||
* Return: The owner of the idmapped.
|
||||
*/
|
||||
struct user_namespace *mnt_user_ns(const struct vfsmount *mnt)
|
||||
{
|
||||
struct mnt_idmap *idmap = mnt_idmap(mnt);
|
||||
|
||||
/* Return the actual owner of the filesystem instead of the nop. */
|
||||
if (idmap == &nop_mnt_idmap &&
|
||||
!initial_idmapping(mnt->mnt_sb->s_user_ns))
|
||||
return mnt->mnt_sb->s_user_ns;
|
||||
return mnt_idmap_owner(idmap);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(mnt_user_ns);
|
||||
|
||||
/**
|
||||
* alloc_mnt_idmap - allocate a new idmapping for the mount
|
||||
* @mnt_userns: owning userns of the idmapping
|
||||
*
|
||||
* Allocate a new struct mnt_idmap which carries the idmapping of the mount.
|
||||
*
|
||||
* Return: On success a new idmap, on error an error pointer is returned.
|
||||
*/
|
||||
static struct mnt_idmap *alloc_mnt_idmap(struct user_namespace *mnt_userns)
|
||||
{
|
||||
struct mnt_idmap *idmap;
|
||||
|
||||
idmap = kzalloc(sizeof(struct mnt_idmap), GFP_KERNEL_ACCOUNT);
|
||||
if (!idmap)
|
||||
return ERR_PTR(-ENOMEM);
|
||||
|
||||
idmap->owner = get_user_ns(mnt_userns);
|
||||
refcount_set(&idmap->count, 1);
|
||||
return idmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* mnt_idmap_get - get a reference to an idmapping
|
||||
* @idmap: the idmap to bump the reference on
|
||||
*
|
||||
* If @idmap is not the @nop_mnt_idmap bump the reference count.
|
||||
*
|
||||
* Return: @idmap with reference count bumped if @not_mnt_idmap isn't passed.
|
||||
*/
|
||||
static inline struct mnt_idmap *mnt_idmap_get(struct mnt_idmap *idmap)
|
||||
{
|
||||
if (idmap != &nop_mnt_idmap)
|
||||
refcount_inc(&idmap->count);
|
||||
|
||||
return idmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* mnt_idmap_put - put a reference to an idmapping
|
||||
* @idmap: the idmap to put the reference on
|
||||
*
|
||||
* If this is a non-initial idmapping, put the reference count when a mount is
|
||||
* released and free it if we're the last user.
|
||||
*/
|
||||
static inline void mnt_idmap_put(struct mnt_idmap *idmap)
|
||||
{
|
||||
if (idmap != &nop_mnt_idmap && refcount_dec_and_test(&idmap->count)) {
|
||||
put_user_ns(idmap->owner);
|
||||
kfree(idmap);
|
||||
}
|
||||
}
|
||||
|
||||
static struct mount *alloc_vfsmnt(const char *name)
|
||||
{
|
||||
struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
|
||||
@ -232,7 +347,7 @@ static struct mount *alloc_vfsmnt(const char *name)
|
||||
INIT_HLIST_NODE(&mnt->mnt_mp_list);
|
||||
INIT_LIST_HEAD(&mnt->mnt_umounting);
|
||||
INIT_HLIST_HEAD(&mnt->mnt_stuck_children);
|
||||
mnt->mnt.mnt_userns = &init_user_ns;
|
||||
mnt->mnt.mnt_idmap = &nop_mnt_idmap;
|
||||
}
|
||||
return mnt;
|
||||
|
||||
@ -602,11 +717,7 @@ int sb_prepare_remount_readonly(struct super_block *sb)
|
||||
|
||||
static void free_vfsmnt(struct mount *mnt)
|
||||
{
|
||||
struct user_namespace *mnt_userns;
|
||||
|
||||
mnt_userns = mnt_user_ns(&mnt->mnt);
|
||||
if (!initial_idmapping(mnt_userns))
|
||||
put_user_ns(mnt_userns);
|
||||
mnt_idmap_put(mnt_idmap(&mnt->mnt));
|
||||
kfree_const(mnt->mnt_devname);
|
||||
#ifdef CONFIG_SMP
|
||||
free_percpu(mnt->mnt_pcp);
|
||||
@ -1009,7 +1120,6 @@ static struct mount *skip_mnt_tree(struct mount *p)
|
||||
struct vfsmount *vfs_create_mount(struct fs_context *fc)
|
||||
{
|
||||
struct mount *mnt;
|
||||
struct user_namespace *fs_userns;
|
||||
|
||||
if (!fc->root)
|
||||
return ERR_PTR(-EINVAL);
|
||||
@ -1027,10 +1137,6 @@ struct vfsmount *vfs_create_mount(struct fs_context *fc)
|
||||
mnt->mnt_mountpoint = mnt->mnt.mnt_root;
|
||||
mnt->mnt_parent = mnt;
|
||||
|
||||
fs_userns = mnt->mnt.mnt_sb->s_user_ns;
|
||||
if (!initial_idmapping(fs_userns))
|
||||
mnt->mnt.mnt_userns = get_user_ns(fs_userns);
|
||||
|
||||
lock_mount_hash();
|
||||
list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts);
|
||||
unlock_mount_hash();
|
||||
@ -1120,9 +1226,8 @@ static struct mount *clone_mnt(struct mount *old, struct dentry *root,
|
||||
mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
|
||||
|
||||
atomic_inc(&sb->s_active);
|
||||
mnt->mnt.mnt_userns = mnt_user_ns(&old->mnt);
|
||||
if (!initial_idmapping(mnt->mnt.mnt_userns))
|
||||
mnt->mnt.mnt_userns = get_user_ns(mnt->mnt.mnt_userns);
|
||||
mnt->mnt.mnt_idmap = mnt_idmap_get(mnt_idmap(&old->mnt));
|
||||
|
||||
mnt->mnt.mnt_sb = sb;
|
||||
mnt->mnt.mnt_root = dget(root);
|
||||
mnt->mnt_mountpoint = mnt->mnt.mnt_root;
|
||||
@ -3981,14 +4086,14 @@ static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
|
||||
struct vfsmount *m = &mnt->mnt;
|
||||
struct user_namespace *fs_userns = m->mnt_sb->s_user_ns;
|
||||
|
||||
if (!kattr->mnt_userns)
|
||||
if (!kattr->mnt_idmap)
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* Creating an idmapped mount with the filesystem wide idmapping
|
||||
* doesn't make sense so block that. We don't allow mushy semantics.
|
||||
*/
|
||||
if (kattr->mnt_userns == fs_userns)
|
||||
if (mnt_idmap_owner(kattr->mnt_idmap) == fs_userns)
|
||||
return -EINVAL;
|
||||
|
||||
/*
|
||||
@ -4028,7 +4133,7 @@ static inline bool mnt_allow_writers(const struct mount_kattr *kattr,
|
||||
{
|
||||
return (!(kattr->attr_set & MNT_READONLY) ||
|
||||
(mnt->mnt.mnt_flags & MNT_READONLY)) &&
|
||||
!kattr->mnt_userns;
|
||||
!kattr->mnt_idmap;
|
||||
}
|
||||
|
||||
static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt)
|
||||
@ -4082,27 +4187,18 @@ static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt)
|
||||
|
||||
static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
|
||||
{
|
||||
struct user_namespace *mnt_userns, *old_mnt_userns;
|
||||
|
||||
if (!kattr->mnt_userns)
|
||||
if (!kattr->mnt_idmap)
|
||||
return;
|
||||
|
||||
/*
|
||||
* We're the only ones able to change the mount's idmapping. So
|
||||
* mnt->mnt.mnt_userns is stable and we can retrieve it directly.
|
||||
* Pairs with smp_load_acquire() in mnt_idmap().
|
||||
*
|
||||
* Since we only allow a mount to change the idmapping once and
|
||||
* verified this in can_idmap_mount() we know that the mount has
|
||||
* @nop_mnt_idmap attached to it. So there's no need to drop any
|
||||
* references.
|
||||
*/
|
||||
old_mnt_userns = mnt->mnt.mnt_userns;
|
||||
|
||||
mnt_userns = get_user_ns(kattr->mnt_userns);
|
||||
/* Pairs with smp_load_acquire() in mnt_user_ns(). */
|
||||
smp_store_release(&mnt->mnt.mnt_userns, mnt_userns);
|
||||
|
||||
/*
|
||||
* If this is an idmapped filesystem drop the reference we've taken
|
||||
* in vfs_create_mount() before.
|
||||
*/
|
||||
if (!initial_idmapping(old_mnt_userns))
|
||||
put_user_ns(old_mnt_userns);
|
||||
smp_store_release(&mnt->mnt.mnt_idmap, mnt_idmap_get(kattr->mnt_idmap));
|
||||
}
|
||||
|
||||
static void mount_setattr_commit(struct mount_kattr *kattr, struct mount *mnt)
|
||||
@ -4136,6 +4232,15 @@ static int do_mount_setattr(struct path *path, struct mount_kattr *kattr)
|
||||
if (path->dentry != mnt->mnt.mnt_root)
|
||||
return -EINVAL;
|
||||
|
||||
if (kattr->mnt_userns) {
|
||||
struct mnt_idmap *mnt_idmap;
|
||||
|
||||
mnt_idmap = alloc_mnt_idmap(kattr->mnt_userns);
|
||||
if (IS_ERR(mnt_idmap))
|
||||
return PTR_ERR(mnt_idmap);
|
||||
kattr->mnt_idmap = mnt_idmap;
|
||||
}
|
||||
|
||||
if (kattr->propagation) {
|
||||
/*
|
||||
* Only take namespace_lock() if we're actually changing
|
||||
@ -4323,6 +4428,9 @@ static void finish_mount_kattr(struct mount_kattr *kattr)
|
||||
{
|
||||
put_user_ns(kattr->mnt_userns);
|
||||
kattr->mnt_userns = NULL;
|
||||
|
||||
if (kattr->mnt_idmap)
|
||||
mnt_idmap_put(kattr->mnt_idmap);
|
||||
}
|
||||
|
||||
SYSCALL_DEFINE5(mount_setattr, int, dfd, const char __user *, path,
|
||||
|
@ -2700,18 +2700,22 @@ static inline struct user_namespace *file_mnt_user_ns(struct file *file)
|
||||
return mnt_user_ns(file->f_path.mnt);
|
||||
}
|
||||
|
||||
static inline struct mnt_idmap *file_mnt_idmap(struct file *file)
|
||||
{
|
||||
return mnt_idmap(file->f_path.mnt);
|
||||
}
|
||||
|
||||
/**
|
||||
* is_idmapped_mnt - check whether a mount is mapped
|
||||
* @mnt: the mount to check
|
||||
*
|
||||
* If @mnt has an idmapping attached different from the
|
||||
* filesystem's idmapping then @mnt is mapped.
|
||||
* If @mnt has an non @nop_mnt_idmap attached to it then @mnt is mapped.
|
||||
*
|
||||
* Return: true if mount is mapped, false if not.
|
||||
*/
|
||||
static inline bool is_idmapped_mnt(const struct vfsmount *mnt)
|
||||
{
|
||||
return mnt_user_ns(mnt) != mnt->mnt_sb->s_user_ns;
|
||||
return mnt_idmap(mnt) != &nop_mnt_idmap;
|
||||
}
|
||||
|
||||
extern long vfs_truncate(const struct path *, loff_t);
|
||||
|
@ -5,12 +5,10 @@
|
||||
#include <linux/types.h>
|
||||
#include <linux/uidgid.h>
|
||||
|
||||
struct mnt_idmap;
|
||||
struct user_namespace;
|
||||
/*
|
||||
* Carries the initial idmapping of 0:0:4294967295 which is an identity
|
||||
* mapping. This means that {g,u}id 0 is mapped to {g,u}id 0, {g,u}id 1 is
|
||||
* mapped to {g,u}id 1, [...], {g,u}id 1000 to {g,u}id 1000, [...].
|
||||
*/
|
||||
|
||||
extern struct mnt_idmap nop_mnt_idmap;
|
||||
extern struct user_namespace init_user_ns;
|
||||
|
||||
typedef struct {
|
||||
|
@ -16,6 +16,7 @@
|
||||
struct super_block;
|
||||
struct dentry;
|
||||
struct user_namespace;
|
||||
struct mnt_idmap;
|
||||
struct file_system_type;
|
||||
struct fs_context;
|
||||
struct file;
|
||||
@ -70,13 +71,15 @@ struct vfsmount {
|
||||
struct dentry *mnt_root; /* root of the mounted tree */
|
||||
struct super_block *mnt_sb; /* pointer to superblock */
|
||||
int mnt_flags;
|
||||
struct user_namespace *mnt_userns;
|
||||
struct mnt_idmap *mnt_idmap;
|
||||
} __randomize_layout;
|
||||
|
||||
static inline struct user_namespace *mnt_user_ns(const struct vfsmount *mnt)
|
||||
struct user_namespace *mnt_user_ns(const struct vfsmount *mnt);
|
||||
struct user_namespace *mnt_idmap_owner(const struct mnt_idmap *idmap);
|
||||
static inline struct mnt_idmap *mnt_idmap(const struct vfsmount *mnt)
|
||||
{
|
||||
/* Pairs with smp_store_release() in do_idmap_mount(). */
|
||||
return smp_load_acquire(&mnt->mnt_userns);
|
||||
return smp_load_acquire(&mnt->mnt_idmap);
|
||||
}
|
||||
|
||||
extern int mnt_want_write(struct vfsmount *mnt);
|
||||
|
Loading…
Reference in New Issue
Block a user