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