History

fs: allow changing idmappings

This patchset makes it possible to create a new idmapped mount from an already idmapped mount and to clear idmappings. // Create a first idmapped mount struct mount_attr attr = { .attr_set = MOUNT_ATTR_IDMAP .userns_fd = fd_userns }; fd_tree = open_tree(-EBADF, "/", OPEN_TREE_CLONE, &attr, sizeof(attr)); move_mount(fd_tree, "", -EBADF, "/mnt", MOVE_MOUNT_F_EMPTY_PATH); // Create a second idmapped mount from the first idmapped mount attr.attr_set = MOUNT_ATTR_IDMAP; attr.userns_fd = fd_userns2; fd_tree2 = open_tree(-EBADF, "/mnt", OPEN_TREE_CLONE, &attr, sizeof(attr)); // Create a second non-idmapped mount from the first idmapped mount: memset(&attr, 0, sizeof(attr)); attr.attr_clr = MOUNT_ATTR_IDMAP; fd_tree2 = open_tree(-EBADF, "/mnt", OPEN_TREE_CLONE, &attr, sizeof(attr));

Christian Brauner committed 1 year ago

Commit 24626511 parent 325cca81 file · +32 −21Browse files

Unified · Split

fs/namespace.c+32−21
@@ -89,6 +89,7 @@ static LIST_HEAD(mnt_ns_list); /* protected by mnt_ns_tree_lock */ · 88 unmodified lines
89 89
90enum mount_kattr_flags_t {90enum mount_kattr_flags_t {
91 MOUNT_KATTR_RECURSE = (1 << 0),91 MOUNT_KATTR_RECURSE = (1 << 0),
92 MOUNT_KATTR_IDMAP_REPLACE = (1 << 1),
92};93};
93 94
94struct mount_kattr {95struct mount_kattr {
@@ -4612,11 +4613,10 @@ static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt) · 4517 unmodified lines
4612 return -EINVAL;4613 return -EINVAL;
4613 4614
4614 /*4615 /*
4615 * Once a mount has been idmapped we don't allow it to change its4616 * We only allow an mount to change it's idmapping if it has
4616 * mapping. It makes things simpler and callers can just create4617 * never been accessible to userspace.
4617 * another bind-mount they can idmap if they want to.
4618 */4618 */
4619 if (is_idmapped_mnt(m))4619 if (!(kattr->kflags & MOUNT_KATTR_IDMAP_REPLACE) && is_idmapped_mnt(m))
4620 return -EPERM;4620 return -EPERM;
4621 4621
4622 /* The underlying filesystem doesn't support idmapped mounts yet. */4622 /* The underlying filesystem doesn't support idmapped mounts yet. */
@@ -4706,18 +4706,16 @@ static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt) · 83 unmodified lines
4706 4706
4707static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)4707static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
4708{4708{
4709 struct mnt_idmap *old_idmap;
4710
4709 if (!kattr->mnt_idmap)4711 if (!kattr->mnt_idmap)
4710 return;4712 return;
4711 4713
4712 /*4714 old_idmap = mnt_idmap(&mnt->mnt);
4713 * Pairs with smp_load_acquire() in mnt_idmap().4715
4714 *4716 /* Pairs with smp_load_acquire() in mnt_idmap(). */
4715 * Since we only allow a mount to change the idmapping once and
4716 * verified this in can_idmap_mount() we know that the mount has
4717 * @nop_mnt_idmap attached to it. So there's no need to drop any
4718 * references.
4719 */
4720 smp_store_release(&mnt->mnt.mnt_idmap, mnt_idmap_get(kattr->mnt_idmap));4717 smp_store_release(&mnt->mnt.mnt_idmap, mnt_idmap_get(kattr->mnt_idmap));
4718 mnt_idmap_put(old_idmap);
4721}4719}
4722 4720
4723static void mount_setattr_commit(struct mount_kattr *kattr, struct mount *mnt)4721static void mount_setattr_commit(struct mount_kattr *kattr, struct mount *mnt)
@@ -4826,13 +4824,23 @@ static int build_mount_idmapped(const struct mount_attr *attr, size_t usize, · 102 unmodified lines
4826 if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP))4824 if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP))
4827 return 0;4825 return 0;
4828 4826
4829 /*4827 if (attr->attr_clr & MOUNT_ATTR_IDMAP) {
4830 * We currently do not support clearing an idmapped mount. If this ever4828 /*
4831 * is a use-case we can revisit this but for now let's keep it simple4829 * We can only remove an idmapping if it's never been
4832 * and not allow it.4830 * exposed to userspace.
4833 */4831 */
4834 if (attr->attr_clr & MOUNT_ATTR_IDMAP)4832 if (!(kattr->kflags & MOUNT_KATTR_IDMAP_REPLACE))
4835 return -EINVAL;4833 return -EINVAL;
4834
4835 /*
4836 * Removal of idmappings is equivalent to setting
4837 * nop_mnt_idmap.
4838 */
4839 if (!(attr->attr_set & MOUNT_ATTR_IDMAP)) {
4840 kattr->mnt_idmap = &nop_mnt_idmap;
4841 return 0;
4842 }
4843 }
4836 4844
4837 if (attr->userns_fd > INT_MAX)4845 if (attr->userns_fd > INT_MAX)
4838 return -EINVAL;4846 return -EINVAL;
@@ -4923,8 +4931,10 @@ static int build_mount_kattr(const struct mount_attr *attr, size_t usize, · 84 unmodified lines
4923 4931
4924static void finish_mount_kattr(struct mount_kattr *kattr)4932static void finish_mount_kattr(struct mount_kattr *kattr)
4925{4933{
4926 put_user_ns(kattr->mnt_userns);4934 if (kattr->mnt_userns) {
4927 kattr->mnt_userns = NULL;4935 put_user_ns(kattr->mnt_userns);
4936 kattr->mnt_userns = NULL;
4937 }
4928 4938
4929 if (kattr->mnt_idmap)4939 if (kattr->mnt_idmap)
4930 mnt_idmap_put(kattr->mnt_idmap);4940 mnt_idmap_put(kattr->mnt_idmap);
@@ -5019,6 +5029,7 @@ SYSCALL_DEFINE5(open_tree_attr, int, dfd, const char __user *, filename, · 88 unmodified lines
5019 int ret;5029 int ret;
5020 struct mount_kattr kattr = {};5030 struct mount_kattr kattr = {};
5021 5031
5032 kattr.kflags = MOUNT_KATTR_IDMAP_REPLACE;
5022 if (flags & AT_RECURSIVE)5033 if (flags & AT_RECURSIVE)
5023 kattr.kflags |= MOUNT_KATTR_RECURSE;5034 kattr.kflags |= MOUNT_KATTR_RECURSE;
5024 5035

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t