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