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));
Unified · Split
| @@ -89,6 +89,7 @@ static LIST_HEAD(mnt_ns_list); /* protected by mnt_ns_tree_lock */ · 88 unmodified lines | ||
| 89 | 89 | |
| 90 | 90 | enum mount_kattr_flags_t { |
| 91 | 91 | MOUNT_KATTR_RECURSE = (1 << 0), |
| 92 | + MOUNT_KATTR_IDMAP_REPLACE = (1 << 1), | |
| 92 | 93 | }; |
| 93 | 94 | |
| 94 | 95 | struct mount_kattr { |
| @@ -4612,11 +4613,10 @@ static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt) · 4517 unmodified lines | ||
| 4612 | 4613 | return -EINVAL; |
| 4613 | 4614 | |
| 4614 | 4615 | /* |
| 4615 | − * Once a mount has been idmapped we don't allow it to change its | |
| 4616 | − * mapping. It makes things simpler and callers can just create | |
| 4617 | − * another bind-mount they can idmap if they want to. | |
| 4616 | + * We only allow an mount to change it's idmapping if it has | |
| 4617 | + * never been accessible to userspace. | |
| 4618 | 4618 | */ |
| 4619 | − if (is_idmapped_mnt(m)) | |
| 4619 | + if (!(kattr->kflags & MOUNT_KATTR_IDMAP_REPLACE) && is_idmapped_mnt(m)) | |
| 4620 | 4620 | return -EPERM; |
| 4621 | 4621 | |
| 4622 | 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 | |
| 4707 | 4707 | static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt) |
| 4708 | 4708 | { |
| 4709 | + struct mnt_idmap *old_idmap; | |
| 4710 | + | |
| 4709 | 4711 | if (!kattr->mnt_idmap) |
| 4710 | 4712 | return; |
| 4711 | 4713 | |
| 4712 | − /* | |
| 4713 | − * Pairs with smp_load_acquire() in mnt_idmap(). | |
| 4714 | − * | |
| 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 | − */ | |
| 4714 | + old_idmap = mnt_idmap(&mnt->mnt); | |
| 4715 | + | |
| 4716 | + /* Pairs with smp_load_acquire() in mnt_idmap(). */ | |
| 4720 | 4717 | smp_store_release(&mnt->mnt.mnt_idmap, mnt_idmap_get(kattr->mnt_idmap)); |
| 4718 | + mnt_idmap_put(old_idmap); | |
| 4721 | 4719 | } |
| 4722 | 4720 | |
| 4723 | 4721 | static 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 | 4824 | if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP)) |
| 4827 | 4825 | return 0; |
| 4828 | 4826 | |
| 4829 | − /* | |
| 4830 | − * We currently do not support clearing an idmapped mount. If this ever | |
| 4831 | − * is a use-case we can revisit this but for now let's keep it simple | |
| 4832 | − * and not allow it. | |
| 4833 | − */ | |
| 4834 | − if (attr->attr_clr & MOUNT_ATTR_IDMAP) | |
| 4835 | − return -EINVAL; | |
| 4827 | + if (attr->attr_clr & MOUNT_ATTR_IDMAP) { | |
| 4828 | + /* | |
| 4829 | + * We can only remove an idmapping if it's never been | |
| 4830 | + * exposed to userspace. | |
| 4831 | + */ | |
| 4832 | + if (!(kattr->kflags & MOUNT_KATTR_IDMAP_REPLACE)) | |
| 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 | 4845 | if (attr->userns_fd > INT_MAX) |
| 4838 | 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 | |
| 4924 | 4932 | static void finish_mount_kattr(struct mount_kattr *kattr) |
| 4925 | 4933 | { |
| 4926 | − put_user_ns(kattr->mnt_userns); | |
| 4927 | − kattr->mnt_userns = NULL; | |
| 4934 | + if (kattr->mnt_userns) { | |
| 4935 | + put_user_ns(kattr->mnt_userns); | |
| 4936 | + kattr->mnt_userns = NULL; | |
| 4937 | + } | |
| 4928 | 4938 | |
| 4929 | 4939 | if (kattr->mnt_idmap) |
| 4930 | 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 | 5029 | int ret; |
| 5020 | 5030 | struct mount_kattr kattr = {}; |
| 5021 | 5031 | |
| 5032 | + kattr.kflags = MOUNT_KATTR_IDMAP_REPLACE; | |
| 5022 | 5033 | if (flags & AT_RECURSIVE) |
| 5023 | 5034 | kattr.kflags |= MOUNT_KATTR_RECURSE; |
| 5024 | 5035 | |