^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/nsproxy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/user_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/proc_ns.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/highuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/securebits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/keyctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/key-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <keys/user-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/projid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/fs_struct.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/bsearch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/sort.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static struct kmem_cache *user_ns_cachep __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static DEFINE_MUTEX(userns_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static bool new_idmap_permitted(const struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct user_namespace *ns, int cap_setid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct uid_gid_map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static void free_user_ns(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static struct ucounts *inc_user_namespaces(struct user_namespace *ns, kuid_t uid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return inc_ucount(ns, uid, UCOUNT_USER_NAMESPACES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static void dec_user_namespaces(struct ucounts *ucounts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return dec_ucount(ucounts, UCOUNT_USER_NAMESPACES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* Start with the same capabilities as init but useless for doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * anything as the capabilities are bound to the new user namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) cred->securebits = SECUREBITS_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) cred->cap_inheritable = CAP_EMPTY_SET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) cred->cap_permitted = CAP_FULL_SET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) cred->cap_effective = CAP_FULL_SET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) cred->cap_ambient = CAP_EMPTY_SET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) cred->cap_bset = CAP_FULL_SET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #ifdef CONFIG_KEYS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) key_put(cred->request_key_auth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) cred->request_key_auth = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) cred->user_ns = user_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * Create a new user namespace, deriving the creator from the user in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * passed credentials, and replacing that user with the new root user for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * new namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * This is called by copy_creds(), which will finish setting the target task's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * credentials.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int create_user_ns(struct cred *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct user_namespace *ns, *parent_ns = new->user_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) kuid_t owner = new->euid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) kgid_t group = new->egid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct ucounts *ucounts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (parent_ns->level > 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) ucounts = inc_user_namespaces(parent_ns, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!ucounts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * Verify that we can not violate the policy of which files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * may be accessed that is specified by the root directory,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * by verifing that the root directory is at the root of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * mount namespace which allows all files to be accessed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (current_chrooted())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) goto fail_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* The creator needs a mapping in the parent user namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * or else we won't be able to reasonably tell userspace who
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * created a user_namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (!kuid_has_mapping(parent_ns, owner) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) !kgid_has_mapping(parent_ns, group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) goto fail_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (!ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) goto fail_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ns->parent_could_setfcap = cap_raised(new->cap_effective, CAP_SETFCAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ret = ns_alloc_inum(&ns->ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) goto fail_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ns->ns.ops = &userns_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) atomic_set(&ns->count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /* Leave the new->user_ns reference with the new user namespace. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ns->parent = parent_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ns->level = parent_ns->level + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) ns->owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ns->group = group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) INIT_WORK(&ns->work, free_user_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) for (i = 0; i < UCOUNT_COUNTS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) ns->ucount_max[i] = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ns->ucounts = ucounts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) mutex_lock(&userns_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ns->flags = parent_ns->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) mutex_unlock(&userns_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #ifdef CONFIG_KEYS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) INIT_LIST_HEAD(&ns->keyring_name_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) init_rwsem(&ns->keyring_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!setup_userns_sysctls(ns))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) goto fail_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) set_cred_user_ns(new, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) fail_keyring:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #ifdef CONFIG_PERSISTENT_KEYRINGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) key_put(ns->persistent_keyring_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) ns_free_inum(&ns->ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) fail_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) kmem_cache_free(user_ns_cachep, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) fail_dec:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) dec_user_namespaces(ucounts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct cred *cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!(unshare_flags & CLONE_NEWUSER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) cred = prepare_creds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (cred) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) err = create_user_ns(cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) put_cred(cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) *new_cred = cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static void free_user_ns(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct user_namespace *parent, *ns =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) container_of(work, struct user_namespace, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct ucounts *ucounts = ns->ucounts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) parent = ns->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (ns->gid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) kfree(ns->gid_map.forward);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) kfree(ns->gid_map.reverse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (ns->uid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) kfree(ns->uid_map.forward);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) kfree(ns->uid_map.reverse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (ns->projid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) kfree(ns->projid_map.forward);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) kfree(ns->projid_map.reverse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) retire_userns_sysctls(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) key_free_user_ns(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ns_free_inum(&ns->ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) kmem_cache_free(user_ns_cachep, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) dec_user_namespaces(ucounts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ns = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) } while (atomic_dec_and_test(&parent->count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) void __put_user_ns(struct user_namespace *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) schedule_work(&ns->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) EXPORT_SYMBOL(__put_user_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * idmap_key struct holds the information necessary to find an idmapping in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * sorted idmap array. It is passed to cmp_map_id() as first argument.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct idmap_key {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) bool map_up; /* true -> id from kid; false -> kid from id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) u32 id; /* id to find */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) u32 count; /* == 0 unless used with map_id_range_down() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * cmp_map_id - Function to be passed to bsearch() to find the requested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * idmapping. Expects struct idmap_key to be passed via @k.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static int cmp_map_id(const void *k, const void *e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) u32 first, last, id2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) const struct idmap_key *key = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) const struct uid_gid_extent *el = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) id2 = key->id + key->count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* handle map_id_{down,up}() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (key->map_up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) first = el->lower_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) first = el->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) last = first + el->count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (key->id >= first && key->id <= last &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) (id2 >= first && id2 <= last))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (key->id < first || id2 < first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * map_id_range_down_max - Find idmap via binary search in ordered idmap array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static struct uid_gid_extent *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) map_id_range_down_max(unsigned extents, struct uid_gid_map *map, u32 id, u32 count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct idmap_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) key.map_up = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) key.count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) key.id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return bsearch(&key, map->forward, extents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) sizeof(struct uid_gid_extent), cmp_map_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * map_id_range_down_base - Find idmap via binary search in static extent array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * Can only be called if number of mappings is equal or less than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * UID_GID_MAP_MAX_BASE_EXTENTS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static struct uid_gid_extent *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) map_id_range_down_base(unsigned extents, struct uid_gid_map *map, u32 id, u32 count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) unsigned idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) u32 first, last, id2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) id2 = id + count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /* Find the matching extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) for (idx = 0; idx < extents; idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) first = map->extent[idx].first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) last = first + map->extent[idx].count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (id >= first && id <= last &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) (id2 >= first && id2 <= last))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return &map->extent[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct uid_gid_extent *extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) unsigned extents = map->nr_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) extent = map_id_range_down_base(extents, map, id, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) extent = map_id_range_down_max(extents, map, id, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /* Map the id or note failure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (extent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) id = (id - extent->first) + extent->lower_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) id = (u32) -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static u32 map_id_down(struct uid_gid_map *map, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return map_id_range_down(map, id, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * map_id_up_base - Find idmap via binary search in static extent array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * Can only be called if number of mappings is equal or less than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * UID_GID_MAP_MAX_BASE_EXTENTS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static struct uid_gid_extent *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) map_id_up_base(unsigned extents, struct uid_gid_map *map, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) unsigned idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) u32 first, last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) /* Find the matching extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) for (idx = 0; idx < extents; idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) first = map->extent[idx].lower_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) last = first + map->extent[idx].count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (id >= first && id <= last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return &map->extent[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * map_id_up_max - Find idmap via binary search in ordered idmap array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static struct uid_gid_extent *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) map_id_up_max(unsigned extents, struct uid_gid_map *map, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) struct idmap_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) key.map_up = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) key.count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) key.id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return bsearch(&key, map->reverse, extents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) sizeof(struct uid_gid_extent), cmp_map_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static u32 map_id_up(struct uid_gid_map *map, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct uid_gid_extent *extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) unsigned extents = map->nr_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) extent = map_id_up_base(extents, map, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) extent = map_id_up_max(extents, map, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /* Map the id or note failure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (extent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) id = (id - extent->lower_first) + extent->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) id = (u32) -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * make_kuid - Map a user-namespace uid pair into a kuid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * @ns: User namespace that the uid is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * @uid: User identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * Maps a user-namespace uid pair into a kernel internal kuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * and returns that kuid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * When there is no mapping defined for the user-namespace uid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * pair INVALID_UID is returned. Callers are expected to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * for and handle INVALID_UID being returned. INVALID_UID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * may be tested for using uid_valid().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) /* Map the uid to a global kernel uid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) EXPORT_SYMBOL(make_kuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * from_kuid - Create a uid from a kuid user-namespace pair.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * @targ: The user namespace we want a uid in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * @kuid: The kernel internal uid to start with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * Map @kuid into the user-namespace specified by @targ and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * return the resulting uid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * There is always a mapping into the initial user_namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * If @kuid has no mapping in @targ (uid_t)-1 is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) /* Map the uid from a global kernel uid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return map_id_up(&targ->uid_map, __kuid_val(kuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) EXPORT_SYMBOL(from_kuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * from_kuid_munged - Create a uid from a kuid user-namespace pair.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * @targ: The user namespace we want a uid in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * @kuid: The kernel internal uid to start with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * Map @kuid into the user-namespace specified by @targ and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * return the resulting uid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * There is always a mapping into the initial user_namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * Unlike from_kuid from_kuid_munged never fails and always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * returns a valid uid. This makes from_kuid_munged appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * for use in syscalls like stat and getuid where failing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * system call and failing to provide a valid uid are not an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * If @kuid has no mapping in @targ overflowuid is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) uid_t uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) uid = from_kuid(targ, kuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (uid == (uid_t) -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) uid = overflowuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) EXPORT_SYMBOL(from_kuid_munged);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * make_kgid - Map a user-namespace gid pair into a kgid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * @ns: User namespace that the gid is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * @gid: group identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * Maps a user-namespace gid pair into a kernel internal kgid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * and returns that kgid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * When there is no mapping defined for the user-namespace gid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * pair INVALID_GID is returned. Callers are expected to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * for and handle INVALID_GID being returned. INVALID_GID may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * tested for using gid_valid().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /* Map the gid to a global kernel gid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) EXPORT_SYMBOL(make_kgid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * from_kgid - Create a gid from a kgid user-namespace pair.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * @targ: The user namespace we want a gid in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * @kgid: The kernel internal gid to start with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * Map @kgid into the user-namespace specified by @targ and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * return the resulting gid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * There is always a mapping into the initial user_namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * If @kgid has no mapping in @targ (gid_t)-1 is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) /* Map the gid from a global kernel gid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) return map_id_up(&targ->gid_map, __kgid_val(kgid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) EXPORT_SYMBOL(from_kgid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * from_kgid_munged - Create a gid from a kgid user-namespace pair.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * @targ: The user namespace we want a gid in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * @kgid: The kernel internal gid to start with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * Map @kgid into the user-namespace specified by @targ and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * return the resulting gid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) * There is always a mapping into the initial user_namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) * Unlike from_kgid from_kgid_munged never fails and always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) * returns a valid gid. This makes from_kgid_munged appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * for use in syscalls like stat and getgid where failing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * system call and failing to provide a valid gid are not options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) * If @kgid has no mapping in @targ overflowgid is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) gid_t gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) gid = from_kgid(targ, kgid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (gid == (gid_t) -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) gid = overflowgid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) return gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) EXPORT_SYMBOL(from_kgid_munged);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * make_kprojid - Map a user-namespace projid pair into a kprojid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * @ns: User namespace that the projid is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) * @projid: Project identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * Maps a user-namespace uid pair into a kernel internal kuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * and returns that kuid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * When there is no mapping defined for the user-namespace projid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) * pair INVALID_PROJID is returned. Callers are expected to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) * for and handle INVALID_PROJID being returned. INVALID_PROJID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) * may be tested for using projid_valid().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) /* Map the uid to a global kernel uid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) EXPORT_SYMBOL(make_kprojid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * from_kprojid - Create a projid from a kprojid user-namespace pair.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * @targ: The user namespace we want a projid in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) * @kprojid: The kernel internal project identifier to start with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * Map @kprojid into the user-namespace specified by @targ and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * return the resulting projid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * There is always a mapping into the initial user_namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) /* Map the uid from a global kernel uid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) EXPORT_SYMBOL(from_kprojid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * @targ: The user namespace we want a projid in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * @kprojid: The kernel internal projid to start with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * Map @kprojid into the user-namespace specified by @targ and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * return the resulting projid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * There is always a mapping into the initial user_namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * Unlike from_kprojid from_kprojid_munged never fails and always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * returns a valid projid. This makes from_kprojid_munged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * appropriate for use in syscalls like stat and where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * failing the system call and failing to provide a valid projid are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * not an options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) projid_t projid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) projid = from_kprojid(targ, kprojid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) if (projid == (projid_t) -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) projid = OVERFLOW_PROJID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) return projid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) EXPORT_SYMBOL(from_kprojid_munged);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) static int uid_m_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) struct user_namespace *ns = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) struct uid_gid_extent *extent = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) struct user_namespace *lower_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) uid_t lower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) lower_ns = seq_user_ns(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if ((lower_ns == ns) && lower_ns->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) lower_ns = lower_ns->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) seq_printf(seq, "%10u %10u %10u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) extent->first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) lower,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) extent->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) static int gid_m_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) struct user_namespace *ns = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) struct uid_gid_extent *extent = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) struct user_namespace *lower_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) gid_t lower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) lower_ns = seq_user_ns(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if ((lower_ns == ns) && lower_ns->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) lower_ns = lower_ns->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) seq_printf(seq, "%10u %10u %10u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) extent->first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) lower,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) extent->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) static int projid_m_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) struct user_namespace *ns = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) struct uid_gid_extent *extent = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) struct user_namespace *lower_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) projid_t lower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) lower_ns = seq_user_ns(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) if ((lower_ns == ns) && lower_ns->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) lower_ns = lower_ns->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) seq_printf(seq, "%10u %10u %10u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) extent->first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) lower,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) extent->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) static void *m_start(struct seq_file *seq, loff_t *ppos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) struct uid_gid_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) loff_t pos = *ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) unsigned extents = map->nr_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) if (pos >= extents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) return &map->extent[pos];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return &map->forward[pos];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) struct user_namespace *ns = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) return m_start(seq, ppos, &ns->uid_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) struct user_namespace *ns = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) return m_start(seq, ppos, &ns->gid_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) struct user_namespace *ns = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) return m_start(seq, ppos, &ns->projid_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) (*pos)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) return seq->op->start(seq, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) static void m_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) const struct seq_operations proc_uid_seq_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) .start = uid_m_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) .stop = m_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) .next = m_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) .show = uid_m_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) const struct seq_operations proc_gid_seq_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) .start = gid_m_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) .stop = m_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) .next = m_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) .show = gid_m_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) const struct seq_operations proc_projid_seq_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) .start = projid_m_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) .stop = m_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) .next = m_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) .show = projid_m_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) static bool mappings_overlap(struct uid_gid_map *new_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) struct uid_gid_extent *extent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) u32 upper_first, lower_first, upper_last, lower_last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) unsigned idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) upper_first = extent->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) lower_first = extent->lower_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) upper_last = upper_first + extent->count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) lower_last = lower_first + extent->count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) for (idx = 0; idx < new_map->nr_extents; idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) u32 prev_upper_first, prev_lower_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) u32 prev_upper_last, prev_lower_last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) struct uid_gid_extent *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) prev = &new_map->extent[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) prev = &new_map->forward[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) prev_upper_first = prev->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) prev_lower_first = prev->lower_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) prev_upper_last = prev_upper_first + prev->count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) prev_lower_last = prev_lower_first + prev->count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) /* Does the upper range intersect a previous extent? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) if ((prev_upper_first <= upper_last) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) (prev_upper_last >= upper_first))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) /* Does the lower range intersect a previous extent? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) if ((prev_lower_first <= lower_last) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) (prev_lower_last >= lower_first))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) * insert_extent - Safely insert a new idmap extent into struct uid_gid_map.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) * Takes care to allocate a 4K block of memory if the number of mappings exceeds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) * UID_GID_MAP_MAX_BASE_EXTENTS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) struct uid_gid_extent *dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) if (map->nr_extents == UID_GID_MAP_MAX_BASE_EXTENTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) struct uid_gid_extent *forward;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) /* Allocate memory for 340 mappings. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) forward = kmalloc_array(UID_GID_MAP_MAX_EXTENTS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) sizeof(struct uid_gid_extent),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) if (!forward)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) /* Copy over memory. Only set up memory for the forward pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) * Defer the memory setup for the reverse pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) memcpy(forward, map->extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) map->nr_extents * sizeof(map->extent[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) map->forward = forward;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) map->reverse = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) dest = &map->extent[map->nr_extents];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) dest = &map->forward[map->nr_extents];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) *dest = *extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) map->nr_extents++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) /* cmp function to sort() forward mappings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) static int cmp_extents_forward(const void *a, const void *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) const struct uid_gid_extent *e1 = a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) const struct uid_gid_extent *e2 = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) if (e1->first < e2->first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (e1->first > e2->first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) /* cmp function to sort() reverse mappings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) static int cmp_extents_reverse(const void *a, const void *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) const struct uid_gid_extent *e1 = a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) const struct uid_gid_extent *e2 = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (e1->lower_first < e2->lower_first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (e1->lower_first > e2->lower_first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) * sort_idmaps - Sorts an array of idmap entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) static int sort_idmaps(struct uid_gid_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) /* Sort forward array. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) sort(map->forward, map->nr_extents, sizeof(struct uid_gid_extent),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) cmp_extents_forward, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) /* Only copy the memory from forward we actually need. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) map->reverse = kmemdup(map->forward,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) map->nr_extents * sizeof(struct uid_gid_extent),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) if (!map->reverse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) /* Sort reverse array. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) sort(map->reverse, map->nr_extents, sizeof(struct uid_gid_extent),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) cmp_extents_reverse, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) * verify_root_map() - check the uid 0 mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) * @file: idmapping file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) * @map_ns: user namespace of the target process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) * @new_map: requested idmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) * If a process requests mapping parent uid 0 into the new ns, verify that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) * process writing the map had the CAP_SETFCAP capability as the target process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) * will be able to write fscaps that are valid in ancestor user namespaces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) * Return: true if the mapping is allowed, false if not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) static bool verify_root_map(const struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) struct user_namespace *map_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) struct uid_gid_map *new_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) const struct user_namespace *file_ns = file->f_cred->user_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) struct uid_gid_extent *extent0 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) for (idx = 0; idx < new_map->nr_extents; idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) extent0 = &new_map->extent[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) extent0 = &new_map->forward[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) if (extent0->lower_first == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) extent0 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) if (!extent0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (map_ns == file_ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) /* The process unshared its ns and is writing to its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) * /proc/self/uid_map. User already has full capabilites in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) * the new namespace. Verify that the parent had CAP_SETFCAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) * when it unshared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) * */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) if (!file_ns->parent_could_setfcap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) /* Process p1 is writing to uid_map of p2, who is in a child
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) * user namespace to p1's. Verify that the opener of the map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) * file has CAP_SETFCAP against the parent of the new map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) * namespace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) if (!file_ns_capable(file, map_ns->parent, CAP_SETFCAP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) static ssize_t map_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) size_t count, loff_t *ppos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) int cap_setid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) struct uid_gid_map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) struct uid_gid_map *parent_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) struct seq_file *seq = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) struct user_namespace *map_ns = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) struct uid_gid_map new_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) unsigned idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) struct uid_gid_extent extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) char *kbuf = NULL, *pos, *next_line;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) /* Only allow < page size writes at the beginning of the file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) if ((*ppos != 0) || (count >= PAGE_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) /* Slurp in the user data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) kbuf = memdup_user_nul(buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) if (IS_ERR(kbuf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) return PTR_ERR(kbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) * The userns_state_mutex serializes all writes to any given map.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) * Any map is only ever written once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) * An id map fits within 1 cache line on most architectures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) * On read nothing needs to be done unless you are on an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) * architecture with a crazy cache coherency model like alpha.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) * There is a one time data dependency between reading the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) * count of the extents and the values of the extents. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * desired behavior is to see the values of the extents that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) * were written before the count of the extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) * To achieve this smp_wmb() is used on guarantee the write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) * order and smp_rmb() is guaranteed that we don't have crazy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) * architectures returning stale data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) mutex_lock(&userns_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) memset(&new_map, 0, sizeof(struct uid_gid_map));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) /* Only allow one successful write to the map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) if (map->nr_extents != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) * Adjusting namespace settings requires capabilities on the target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) if (cap_valid(cap_setid) && !file_ns_capable(file, map_ns, CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) /* Parse the user data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) pos = kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) for (; pos; pos = next_line) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) /* Find the end of line and ensure I don't look past it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) next_line = strchr(pos, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) if (next_line) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) *next_line = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) next_line++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) if (*next_line == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) next_line = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) pos = skip_spaces(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) extent.first = simple_strtoul(pos, &pos, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) if (!isspace(*pos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) pos = skip_spaces(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) extent.lower_first = simple_strtoul(pos, &pos, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) if (!isspace(*pos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) pos = skip_spaces(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) extent.count = simple_strtoul(pos, &pos, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) if (*pos && !isspace(*pos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) /* Verify there is not trailing junk on the line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) pos = skip_spaces(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) if (*pos != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) /* Verify we have been given valid starting values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) if ((extent.first == (u32) -1) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) (extent.lower_first == (u32) -1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) /* Verify count is not zero and does not cause the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) * extent to wrap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) if ((extent.first + extent.count) <= extent.first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) if ((extent.lower_first + extent.count) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) extent.lower_first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) /* Do the ranges in extent overlap any previous extents? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) if (mappings_overlap(&new_map, &extent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) if ((new_map.nr_extents + 1) == UID_GID_MAP_MAX_EXTENTS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) (next_line != NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) ret = insert_extent(&new_map, &extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) /* Be very certaint the new map actually exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) if (new_map.nr_extents == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) /* Validate the user is allowed to use user id's mapped to. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) if (!new_idmap_permitted(file, map_ns, cap_setid, &new_map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) /* Map the lower ids from the parent user namespace to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) * kernel global id space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) for (idx = 0; idx < new_map.nr_extents; idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) struct uid_gid_extent *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) u32 lower_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) e = &new_map.extent[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) e = &new_map.forward[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) lower_first = map_id_range_down(parent_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) e->lower_first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) e->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) /* Fail if we can not map the specified extent to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) * the kernel global id space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) if (lower_first == (u32) -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) e->lower_first = lower_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) * If we want to use binary search for lookup, this clones the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) * array and sorts both copies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) ret = sort_idmaps(&new_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) /* Install the map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) memcpy(map->extent, new_map.extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) new_map.nr_extents * sizeof(new_map.extent[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) map->forward = new_map.forward;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) map->reverse = new_map.reverse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) map->nr_extents = new_map.nr_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) *ppos = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) if (ret < 0 && new_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) kfree(new_map.forward);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) kfree(new_map.reverse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) map->forward = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) map->reverse = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) map->nr_extents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) mutex_unlock(&userns_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) kfree(kbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) ssize_t proc_uid_map_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) size_t size, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) struct seq_file *seq = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) struct user_namespace *ns = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) struct user_namespace *seq_ns = seq_user_ns(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) if (!ns->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) if ((seq_ns != ns) && (seq_ns != ns->parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) return map_write(file, buf, size, ppos, CAP_SETUID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) &ns->uid_map, &ns->parent->uid_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) ssize_t proc_gid_map_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) size_t size, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) struct seq_file *seq = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) struct user_namespace *ns = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) struct user_namespace *seq_ns = seq_user_ns(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) if (!ns->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) if ((seq_ns != ns) && (seq_ns != ns->parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) return map_write(file, buf, size, ppos, CAP_SETGID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) &ns->gid_map, &ns->parent->gid_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) ssize_t proc_projid_map_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) size_t size, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) struct seq_file *seq = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) struct user_namespace *ns = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) struct user_namespace *seq_ns = seq_user_ns(seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) if (!ns->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) if ((seq_ns != ns) && (seq_ns != ns->parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) /* Anyone can set any valid project id no capability needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) return map_write(file, buf, size, ppos, -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) &ns->projid_map, &ns->parent->projid_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) static bool new_idmap_permitted(const struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) struct user_namespace *ns, int cap_setid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) struct uid_gid_map *new_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) const struct cred *cred = file->f_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) if (cap_setid == CAP_SETUID && !verify_root_map(file, ns, new_map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) /* Don't allow mappings that would allow anything that wouldn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) * be allowed without the establishment of unprivileged mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) uid_eq(ns->owner, cred->euid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) u32 id = new_map->extent[0].lower_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) if (cap_setid == CAP_SETUID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) kuid_t uid = make_kuid(ns->parent, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) if (uid_eq(uid, cred->euid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) } else if (cap_setid == CAP_SETGID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) kgid_t gid = make_kgid(ns->parent, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) gid_eq(gid, cred->egid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) /* Allow anyone to set a mapping that doesn't require privilege */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) if (!cap_valid(cap_setid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) /* Allow the specified ids if we have the appropriate capability
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) * And the opener of the id file also had the approprpiate capability.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) if (ns_capable(ns->parent, cap_setid) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) file_ns_capable(file, ns->parent, cap_setid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) int proc_setgroups_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) struct user_namespace *ns = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) unsigned long userns_flags = READ_ONCE(ns->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) seq_printf(seq, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) (userns_flags & USERNS_SETGROUPS_ALLOWED) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) "allow" : "deny");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) ssize_t proc_setgroups_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) struct seq_file *seq = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) struct user_namespace *ns = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) char kbuf[8], *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) bool setgroups_allowed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) /* Only allow a very narrow range of strings to be written */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) if ((*ppos != 0) || (count >= sizeof(kbuf)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) /* What was written? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) if (copy_from_user(kbuf, buf, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) kbuf[count] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) pos = kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) /* What is being requested? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) if (strncmp(pos, "allow", 5) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) pos += 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) setgroups_allowed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) else if (strncmp(pos, "deny", 4) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) pos += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) setgroups_allowed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) /* Verify there is not trailing junk on the line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) pos = skip_spaces(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) if (*pos != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) mutex_lock(&userns_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) if (setgroups_allowed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) /* Enabling setgroups after setgroups has been disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) * is not allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) if (!(ns->flags & USERNS_SETGROUPS_ALLOWED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) /* Permanently disabling setgroups after setgroups has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) * been enabled by writing the gid_map is not allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) if (ns->gid_map.nr_extents != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) mutex_unlock(&userns_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) /* Report a successful write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) *ppos = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) mutex_unlock(&userns_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) bool userns_may_setgroups(const struct user_namespace *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) bool allowed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) mutex_lock(&userns_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) /* It is not safe to use setgroups until a gid mapping in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) * the user namespace has been established.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) allowed = ns->gid_map.nr_extents != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) /* Is setgroups allowed? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) mutex_unlock(&userns_state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) return allowed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) * Returns true if @child is the same namespace or a descendant of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) * @ancestor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) bool in_userns(const struct user_namespace *ancestor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) const struct user_namespace *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) const struct user_namespace *ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) for (ns = child; ns->level > ancestor->level; ns = ns->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) return (ns == ancestor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) bool current_in_userns(const struct user_namespace *target_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) return in_userns(target_ns, current_user_ns());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) EXPORT_SYMBOL(current_in_userns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) static inline struct user_namespace *to_user_ns(struct ns_common *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) return container_of(ns, struct user_namespace, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) static struct ns_common *userns_get(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) struct user_namespace *user_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) user_ns = get_user_ns(__task_cred(task)->user_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) return user_ns ? &user_ns->ns : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) static void userns_put(struct ns_common *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) put_user_ns(to_user_ns(ns));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) static int userns_install(struct nsset *nsset, struct ns_common *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) struct user_namespace *user_ns = to_user_ns(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) struct cred *cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) /* Don't allow gaining capabilities by reentering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) * the same user namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) if (user_ns == current_user_ns())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) /* Tasks that share a thread group must share a user namespace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) if (!thread_group_empty(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) if (current->fs->users != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) if (!ns_capable(user_ns, CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) cred = nsset_cred(nsset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) if (!cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) put_user_ns(cred->user_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) set_cred_user_ns(cred, get_user_ns(user_ns));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) struct ns_common *ns_get_owner(struct ns_common *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) struct user_namespace *my_user_ns = current_user_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) struct user_namespace *owner, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) /* See if the owner is in the current user namespace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) owner = p = ns->ops->owner(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) return ERR_PTR(-EPERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) if (p == my_user_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) p = p->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) return &get_user_ns(owner)->ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) static struct user_namespace *userns_owner(struct ns_common *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) return to_user_ns(ns)->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) const struct proc_ns_operations userns_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) .name = "user",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) .type = CLONE_NEWUSER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) .get = userns_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) .put = userns_put,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) .install = userns_install,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) .owner = userns_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) .get_parent = ns_get_owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) static __init int user_namespaces_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) subsys_initcall(user_namespaces_init);