blob: 5fd2d53dbc75ab08a808e69e8e829aa1a7b6f5dd [file] [log] [blame]
Cedric Le Goateracce2922007-07-15 23:40:59 -07001/*
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 Gortmaker9984de12011-05-23 14:51:41 -04008#include <linux/export.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -07009#include <linux/nsproxy.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070010#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010011#include <linux/sched/signal.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070012#include <linux/user_namespace.h>
David Howells0bb80f22013-04-12 01:50:06 +010013#include <linux/proc_ns.h>
Eric W. Biederman5c1469d2010-06-13 03:28:03 +000014#include <linux/highuid.h>
Serge Hallyn18b6e042008-10-15 16:38:45 -050015#include <linux/cred.h>
Eric W. Biederman973c5912011-11-17 01:59:07 -080016#include <linux/securebits.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080017#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. Biedermanf76d2072012-08-30 01:24:05 -070024#include <linux/projid.h>
Eric W. Biedermane66eded2013-03-13 11:51:49 -070025#include <linux/fs_struct.h>
Christian Brauner6397fac2017-10-25 00:04:41 +020026#include <linux/bsearch.h>
27#include <linux/sort.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070028
Pavel Emelyanov61642812011-01-12 17:00:46 -080029static struct kmem_cache *user_ns_cachep __read_mostly;
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -060030static DEFINE_MUTEX(userns_state_mutex);
Pavel Emelyanov61642812011-01-12 17:00:46 -080031
Eric W. Biederman67080752013-04-14 13:47:02 -070032static bool new_idmap_permitted(const struct file *file,
33 struct user_namespace *ns, int cap_setid,
Eric W. Biederman22d917d2011-11-17 00:11:58 -080034 struct uid_gid_map *map);
Eric W. Biedermanb0321322016-07-30 13:53:37 -050035static void free_user_ns(struct work_struct *work);
Eric W. Biederman22d917d2011-11-17 00:11:58 -080036
Eric W. Biederman25f9c082016-08-08 14:41:52 -050037static struct ucounts *inc_user_namespaces(struct user_namespace *ns, kuid_t uid)
38{
39 return inc_ucount(ns, uid, UCOUNT_USER_NAMESPACES);
40}
41
42static void dec_user_namespaces(struct ucounts *ucounts)
43{
44 return dec_ucount(ucounts, UCOUNT_USER_NAMESPACES);
45}
46
Eric W. Biedermancde19752012-07-26 06:24:06 -070047static 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 Lutomirski58319052015-09-04 15:42:45 -070056 cred->cap_ambient = CAP_EMPTY_SET;
Eric W. Biedermancde19752012-07-26 06:24:06 -070057 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. Hallyn77ec7392007-07-15 23:41:01 -070066/*
Serge Hallyn18b6e042008-10-15 16:38:45 -050067 * 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. Hallyn77ec7392007-07-15 23:41:01 -070073 */
Serge Hallyn18b6e042008-10-15 16:38:45 -050074int create_user_ns(struct cred *new)
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070075{
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080076 struct user_namespace *ns, *parent_ns = new->user_ns;
Eric W. Biederman078de5f2012-02-08 07:00:08 -080077 kuid_t owner = new->euid;
78 kgid_t group = new->egid;
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -050079 struct ucounts *ucounts;
Eric W. Biederman25f9c082016-08-08 14:41:52 -050080 int ret, i;
Eric W. Biederman783291e2011-11-17 01:32:59 -080081
Eric W. Biedermandf75e772016-09-22 13:08:36 -050082 ret = -ENOSPC;
Oleg Nesterov8742f222013-08-08 18:55:32 +020083 if (parent_ns->level > 32)
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -050084 goto fail;
85
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -050086 ucounts = inc_user_namespaces(parent_ns, owner);
87 if (!ucounts)
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -050088 goto fail;
Oleg Nesterov8742f222013-08-08 18:55:32 +020089
Eric W. Biederman31515272013-03-15 01:45:51 -070090 /*
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. Biedermanb376c3e2016-08-08 13:41:24 -050096 ret = -EPERM;
Eric W. Biederman31515272013-03-15 01:45:51 -070097 if (current_chrooted())
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -050098 goto fail_dec;
Eric W. Biederman31515272013-03-15 01:45:51 -070099
Eric W. Biederman783291e2011-11-17 01:32:59 -0800100 /* 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. Biedermanb376c3e2016-08-08 13:41:24 -0500104 ret = -EPERM;
Eric W. Biederman783291e2011-11-17 01:32:59 -0800105 if (!kuid_has_mapping(parent_ns, owner) ||
106 !kgid_has_mapping(parent_ns, group))
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -0500107 goto fail_dec;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -0700108
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -0500109 ret = -ENOMEM;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800110 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -0700111 if (!ns)
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -0500112 goto fail_dec;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -0700113
Al Viro6344c432014-11-01 00:45:45 -0400114 ret = ns_alloc_inum(&ns->ns);
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -0500115 if (ret)
116 goto fail_free;
Al Viro33c42942014-11-01 02:32:53 -0400117 ns->ns.ops = &userns_operations;
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700118
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800119 atomic_set(&ns->count, 1);
Eric W. Biedermancde19752012-07-26 06:24:06 -0700120 /* Leave the new->user_ns reference with the new user namespace. */
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -0800121 ns->parent = parent_ns;
Oleg Nesterov8742f222013-08-08 18:55:32 +0200122 ns->level = parent_ns->level + 1;
Eric W. Biederman783291e2011-11-17 01:32:59 -0800123 ns->owner = owner;
124 ns->group = group;
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500125 INIT_WORK(&ns->work, free_user_ns);
Eric W. Biederman25f9c082016-08-08 14:41:52 -0500126 for (i = 0; i < UCOUNT_COUNTS; i++) {
127 ns->ucount_max[i] = INT_MAX;
128 }
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -0500129 ns->ucounts = ucounts;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800130
Eric W. Biederman9cc46512014-12-02 12:27:26 -0600131 /* 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 Howellsf36f8c72013-09-24 10:35:19 +0100136#ifdef CONFIG_PERSISTENT_KEYRINGS
137 init_rwsem(&ns->persistent_keyring_register_sem);
138#endif
Eric W. Biedermandbec2842016-07-30 13:58:49 -0500139 ret = -ENOMEM;
140 if (!setup_userns_sysctls(ns))
141 goto fail_keyring;
142
143 set_cred_user_ns(new, ns);
Serge Hallyn18b6e042008-10-15 16:38:45 -0500144 return 0;
Eric W. Biedermandbec2842016-07-30 13:58:49 -0500145fail_keyring:
146#ifdef CONFIG_PERSISTENT_KEYRINGS
147 key_put(ns->persistent_keyring_register);
148#endif
149 ns_free_inum(&ns->ns);
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -0500150fail_free:
Eric W. Biedermandbec2842016-07-30 13:58:49 -0500151 kmem_cache_free(user_ns_cachep, ns);
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -0500152fail_dec:
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -0500153 dec_user_namespaces(ucounts);
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -0500154fail:
Eric W. Biedermandbec2842016-07-30 13:58:49 -0500155 return ret;
Cedric Le Goateracce2922007-07-15 23:40:59 -0700156}
157
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700158int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
159{
160 struct cred *cred;
Oleg Nesterov61609682013-08-06 19:38:55 +0200161 int err = -ENOMEM;
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700162
163 if (!(unshare_flags & CLONE_NEWUSER))
164 return 0;
165
166 cred = prepare_creds();
Oleg Nesterov61609682013-08-06 19:38:55 +0200167 if (cred) {
168 err = create_user_ns(cred);
169 if (err)
170 put_cred(cred);
171 else
172 *new_cred = cred;
173 }
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700174
Oleg Nesterov61609682013-08-06 19:38:55 +0200175 return err;
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700176}
177
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500178static void free_user_ns(struct work_struct *work)
David Howells51708362009-02-27 14:03:03 -0800179{
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500180 struct user_namespace *parent, *ns =
181 container_of(work, struct user_namespace, work);
David Howells51708362009-02-27 14:03:03 -0800182
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800183 do {
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -0500184 struct ucounts *ucounts = ns->ucounts;
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800185 parent = ns->parent;
Christian Brauner6397fac2017-10-25 00:04:41 +0200186 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. Biedermandbec2842016-07-30 13:58:49 -0500198 retire_userns_sysctls(ns);
David Howellsf36f8c72013-09-24 10:35:19 +0100199#ifdef CONFIG_PERSISTENT_KEYRINGS
200 key_put(ns->persistent_keyring_register);
201#endif
Al Viro6344c432014-11-01 00:45:45 -0400202 ns_free_inum(&ns->ns);
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800203 kmem_cache_free(user_ns_cachep, ns);
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -0500204 dec_user_namespaces(ucounts);
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800205 ns = parent;
206 } while (atomic_dec_and_test(&parent->count));
David Howells51708362009-02-27 14:03:03 -0800207}
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500208
209void __put_user_ns(struct user_namespace *ns)
210{
211 schedule_work(&ns->work);
212}
213EXPORT_SYMBOL(__put_user_ns);
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000214
Christian Brauner6397fac2017-10-25 00:04:41 +0200215/**
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 */
219struct 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 */
229static 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
235 /* handle map_id_range_down() */
236 if (key->count)
237 id2 = key->id + key->count - 1;
238 else
239 id2 = key->id;
240
241 /* handle map_id_{down,up}() */
242 if (key->map_up)
243 first = el->lower_first;
244 else
245 first = el->first;
246
247 last = first + el->count - 1;
248
249 if (key->id >= first && key->id <= last &&
250 (id2 >= first && id2 <= last))
251 return 0;
252
253 if (key->id < first || id2 < first)
254 return -1;
255
256 return 1;
257}
258
259/**
260 * map_id_range_down_max - Find idmap via binary search in ordered idmap array.
261 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
262 */
263static u32 map_id_range_down_max(struct uid_gid_map *map, u32 id, u32 count)
264{
265 u32 extents;
266 struct uid_gid_extent *extent;
267 struct idmap_key key;
268
269 key.map_up = false;
270 key.count = count;
271 key.id = id;
272
273 extents = map->nr_extents;
274 smp_rmb();
275
276 extent = bsearch(&key, map->forward, extents,
277 sizeof(struct uid_gid_extent), cmp_map_id);
278 /* Map the id or note failure */
279 if (extent)
280 id = (id - extent->first) + extent->lower_first;
281 else
282 id = (u32) -1;
283
284 return id;
285}
286
287/**
288 * map_id_range_down_base - Find idmap via binary search in static extent array.
289 * Can only be called if number of mappings is equal or less than
290 * UID_GID_MAP_MAX_BASE_EXTENTS.
291 */
292static u32 map_id_range_down_base(struct uid_gid_map *map, u32 id, u32 count)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000293{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800294 unsigned idx, extents;
295 u32 first, last, id2;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000296
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800297 id2 = id + count - 1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000298
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800299 /* Find the matching extent */
300 extents = map->nr_extents;
Mikulas Patockae79323b2014-04-14 16:58:55 -0400301 smp_rmb();
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800302 for (idx = 0; idx < extents; idx++) {
303 first = map->extent[idx].first;
304 last = first + map->extent[idx].count - 1;
305 if (id >= first && id <= last &&
306 (id2 >= first && id2 <= last))
307 break;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000308 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800309 /* Map the id or note failure */
310 if (idx < extents)
311 id = (id - first) + map->extent[idx].lower_first;
312 else
313 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000314
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800315 return id;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000316}
317
Christian Brauner6397fac2017-10-25 00:04:41 +0200318static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
319{
320 u32 extents = map->nr_extents;
321 smp_rmb();
322
323 if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
324 return map_id_range_down_base(map, id, count);
325
326 return map_id_range_down_max(map, id, count);
327}
328
329/**
330 * map_id_down_base - Find idmap via binary search in static extent array.
331 * Can only be called if number of mappings is equal or less than
332 * UID_GID_MAP_MAX_BASE_EXTENTS.
333 */
334static u32 map_id_down_base(struct uid_gid_map *map, u32 id)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000335{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800336 unsigned idx, extents;
337 u32 first, last;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000338
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800339 /* Find the matching extent */
340 extents = map->nr_extents;
Mikulas Patockae79323b2014-04-14 16:58:55 -0400341 smp_rmb();
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800342 for (idx = 0; idx < extents; idx++) {
343 first = map->extent[idx].first;
344 last = first + map->extent[idx].count - 1;
345 if (id >= first && id <= last)
346 break;
347 }
348 /* Map the id or note failure */
349 if (idx < extents)
350 id = (id - first) + map->extent[idx].lower_first;
351 else
352 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000353
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800354 return id;
355}
356
Christian Brauner6397fac2017-10-25 00:04:41 +0200357static u32 map_id_down(struct uid_gid_map *map, u32 id)
358{
359 u32 extents = map->nr_extents;
360 smp_rmb();
361
362 if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
363 return map_id_down_base(map, id);
364
365 return map_id_range_down_max(map, id, 0);
366}
367
368/**
369 * map_id_up_base - Find idmap via binary search in static extent array.
370 * Can only be called if number of mappings is equal or less than
371 * UID_GID_MAP_MAX_BASE_EXTENTS.
372 */
373static u32 map_id_up_base(struct uid_gid_map *map, u32 id)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800374{
375 unsigned idx, extents;
376 u32 first, last;
377
378 /* Find the matching extent */
379 extents = map->nr_extents;
Mikulas Patockae79323b2014-04-14 16:58:55 -0400380 smp_rmb();
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800381 for (idx = 0; idx < extents; idx++) {
382 first = map->extent[idx].lower_first;
383 last = first + map->extent[idx].count - 1;
384 if (id >= first && id <= last)
385 break;
386 }
387 /* Map the id or note failure */
388 if (idx < extents)
389 id = (id - first) + map->extent[idx].first;
390 else
391 id = (u32) -1;
392
393 return id;
394}
395
396/**
Christian Brauner6397fac2017-10-25 00:04:41 +0200397 * map_id_up_max - Find idmap via binary search in ordered idmap array.
398 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
399 */
400static u32 map_id_up_max(struct uid_gid_map *map, u32 id)
401{
402 u32 extents;
403 struct uid_gid_extent *extent;
404 struct idmap_key key;
405
406 key.map_up = true;
407 key.count = 0;
408 key.id = id;
409
410 extents = map->nr_extents;
411 smp_rmb();
412
413 extent = bsearch(&key, map->reverse, extents,
414 sizeof(struct uid_gid_extent), cmp_map_id);
415 /* Map the id or note failure */
416 if (extent)
417 id = (id - extent->lower_first) + extent->first;
418 else
419 id = (u32) -1;
420
421 return id;
422}
423
424static u32 map_id_up(struct uid_gid_map *map, u32 id)
425{
426 u32 extents = map->nr_extents;
427 smp_rmb();
428
429 if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
430 return map_id_up_base(map, id);
431
432 return map_id_up_max(map, id);
433}
434
435/**
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800436 * make_kuid - Map a user-namespace uid pair into a kuid.
437 * @ns: User namespace that the uid is in
438 * @uid: User identifier
439 *
440 * Maps a user-namespace uid pair into a kernel internal kuid,
441 * and returns that kuid.
442 *
443 * When there is no mapping defined for the user-namespace uid
444 * pair INVALID_UID is returned. Callers are expected to test
Brian Campbellb080e042014-02-16 22:58:12 -0500445 * for and handle INVALID_UID being returned. INVALID_UID
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800446 * may be tested for using uid_valid().
447 */
448kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
449{
450 /* Map the uid to a global kernel uid */
451 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
452}
453EXPORT_SYMBOL(make_kuid);
454
455/**
456 * from_kuid - Create a uid from a kuid user-namespace pair.
457 * @targ: The user namespace we want a uid in.
458 * @kuid: The kernel internal uid to start with.
459 *
460 * Map @kuid into the user-namespace specified by @targ and
461 * return the resulting uid.
462 *
463 * There is always a mapping into the initial user_namespace.
464 *
465 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
466 */
467uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
468{
469 /* Map the uid from a global kernel uid */
470 return map_id_up(&targ->uid_map, __kuid_val(kuid));
471}
472EXPORT_SYMBOL(from_kuid);
473
474/**
475 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
476 * @targ: The user namespace we want a uid in.
477 * @kuid: The kernel internal uid to start with.
478 *
479 * Map @kuid into the user-namespace specified by @targ and
480 * return the resulting uid.
481 *
482 * There is always a mapping into the initial user_namespace.
483 *
484 * Unlike from_kuid from_kuid_munged never fails and always
485 * returns a valid uid. This makes from_kuid_munged appropriate
486 * for use in syscalls like stat and getuid where failing the
487 * system call and failing to provide a valid uid are not an
488 * options.
489 *
490 * If @kuid has no mapping in @targ overflowuid is returned.
491 */
492uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
493{
494 uid_t uid;
495 uid = from_kuid(targ, kuid);
496
497 if (uid == (uid_t) -1)
498 uid = overflowuid;
499 return uid;
500}
501EXPORT_SYMBOL(from_kuid_munged);
502
503/**
504 * make_kgid - Map a user-namespace gid pair into a kgid.
505 * @ns: User namespace that the gid is in
Fabian Frederick68a9a432014-06-06 14:37:21 -0700506 * @gid: group identifier
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800507 *
508 * Maps a user-namespace gid pair into a kernel internal kgid,
509 * and returns that kgid.
510 *
511 * When there is no mapping defined for the user-namespace gid
512 * pair INVALID_GID is returned. Callers are expected to test
513 * for and handle INVALID_GID being returned. INVALID_GID may be
514 * tested for using gid_valid().
515 */
516kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
517{
518 /* Map the gid to a global kernel gid */
519 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
520}
521EXPORT_SYMBOL(make_kgid);
522
523/**
524 * from_kgid - Create a gid from a kgid user-namespace pair.
525 * @targ: The user namespace we want a gid in.
526 * @kgid: The kernel internal gid to start with.
527 *
528 * Map @kgid into the user-namespace specified by @targ and
529 * return the resulting gid.
530 *
531 * There is always a mapping into the initial user_namespace.
532 *
533 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
534 */
535gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
536{
537 /* Map the gid from a global kernel gid */
538 return map_id_up(&targ->gid_map, __kgid_val(kgid));
539}
540EXPORT_SYMBOL(from_kgid);
541
542/**
543 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
544 * @targ: The user namespace we want a gid in.
545 * @kgid: The kernel internal gid to start with.
546 *
547 * Map @kgid into the user-namespace specified by @targ and
548 * return the resulting gid.
549 *
550 * There is always a mapping into the initial user_namespace.
551 *
552 * Unlike from_kgid from_kgid_munged never fails and always
553 * returns a valid gid. This makes from_kgid_munged appropriate
554 * for use in syscalls like stat and getgid where failing the
555 * system call and failing to provide a valid gid are not options.
556 *
557 * If @kgid has no mapping in @targ overflowgid is returned.
558 */
559gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
560{
561 gid_t gid;
562 gid = from_kgid(targ, kgid);
563
564 if (gid == (gid_t) -1)
565 gid = overflowgid;
566 return gid;
567}
568EXPORT_SYMBOL(from_kgid_munged);
569
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700570/**
571 * make_kprojid - Map a user-namespace projid pair into a kprojid.
572 * @ns: User namespace that the projid is in
573 * @projid: Project identifier
574 *
575 * Maps a user-namespace uid pair into a kernel internal kuid,
576 * and returns that kuid.
577 *
578 * When there is no mapping defined for the user-namespace projid
579 * pair INVALID_PROJID is returned. Callers are expected to test
580 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
581 * may be tested for using projid_valid().
582 */
583kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
584{
585 /* Map the uid to a global kernel uid */
586 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
587}
588EXPORT_SYMBOL(make_kprojid);
589
590/**
591 * from_kprojid - Create a projid from a kprojid user-namespace pair.
592 * @targ: The user namespace we want a projid in.
593 * @kprojid: The kernel internal project identifier to start with.
594 *
595 * Map @kprojid into the user-namespace specified by @targ and
596 * return the resulting projid.
597 *
598 * There is always a mapping into the initial user_namespace.
599 *
600 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
601 */
602projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
603{
604 /* Map the uid from a global kernel uid */
605 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
606}
607EXPORT_SYMBOL(from_kprojid);
608
609/**
610 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
611 * @targ: The user namespace we want a projid in.
612 * @kprojid: The kernel internal projid to start with.
613 *
614 * Map @kprojid into the user-namespace specified by @targ and
615 * return the resulting projid.
616 *
617 * There is always a mapping into the initial user_namespace.
618 *
619 * Unlike from_kprojid from_kprojid_munged never fails and always
620 * returns a valid projid. This makes from_kprojid_munged
621 * appropriate for use in syscalls like stat and where
622 * failing the system call and failing to provide a valid projid are
623 * not an options.
624 *
625 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
626 */
627projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
628{
629 projid_t projid;
630 projid = from_kprojid(targ, kprojid);
631
632 if (projid == (projid_t) -1)
633 projid = OVERFLOW_PROJID;
634 return projid;
635}
636EXPORT_SYMBOL(from_kprojid_munged);
637
638
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800639static int uid_m_show(struct seq_file *seq, void *v)
640{
641 struct user_namespace *ns = seq->private;
642 struct uid_gid_extent *extent = v;
643 struct user_namespace *lower_ns;
644 uid_t lower;
645
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700646 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800647 if ((lower_ns == ns) && lower_ns->parent)
648 lower_ns = lower_ns->parent;
649
650 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
651
652 seq_printf(seq, "%10u %10u %10u\n",
653 extent->first,
654 lower,
655 extent->count);
656
657 return 0;
658}
659
660static int gid_m_show(struct seq_file *seq, void *v)
661{
662 struct user_namespace *ns = seq->private;
663 struct uid_gid_extent *extent = v;
664 struct user_namespace *lower_ns;
665 gid_t lower;
666
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700667 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800668 if ((lower_ns == ns) && lower_ns->parent)
669 lower_ns = lower_ns->parent;
670
671 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
672
673 seq_printf(seq, "%10u %10u %10u\n",
674 extent->first,
675 lower,
676 extent->count);
677
678 return 0;
679}
680
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700681static int projid_m_show(struct seq_file *seq, void *v)
682{
683 struct user_namespace *ns = seq->private;
684 struct uid_gid_extent *extent = v;
685 struct user_namespace *lower_ns;
686 projid_t lower;
687
688 lower_ns = seq_user_ns(seq);
689 if ((lower_ns == ns) && lower_ns->parent)
690 lower_ns = lower_ns->parent;
691
692 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
693
694 seq_printf(seq, "%10u %10u %10u\n",
695 extent->first,
696 lower,
697 extent->count);
698
699 return 0;
700}
701
Fabian Frederick68a9a432014-06-06 14:37:21 -0700702static void *m_start(struct seq_file *seq, loff_t *ppos,
703 struct uid_gid_map *map)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800704{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800705 loff_t pos = *ppos;
706
Christian Brauner6397fac2017-10-25 00:04:41 +0200707 if (pos >= map->nr_extents)
708 return NULL;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800709
Christian Brauner6397fac2017-10-25 00:04:41 +0200710 if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
711 return &map->extent[pos];
712
713 return &map->forward[pos];
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800714}
715
716static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
717{
718 struct user_namespace *ns = seq->private;
719
720 return m_start(seq, ppos, &ns->uid_map);
721}
722
723static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
724{
725 struct user_namespace *ns = seq->private;
726
727 return m_start(seq, ppos, &ns->gid_map);
728}
729
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700730static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
731{
732 struct user_namespace *ns = seq->private;
733
734 return m_start(seq, ppos, &ns->projid_map);
735}
736
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800737static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
738{
739 (*pos)++;
740 return seq->op->start(seq, pos);
741}
742
743static void m_stop(struct seq_file *seq, void *v)
744{
745 return;
746}
747
Fabian Frederickccf94f12014-08-08 14:21:22 -0700748const struct seq_operations proc_uid_seq_operations = {
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800749 .start = uid_m_start,
750 .stop = m_stop,
751 .next = m_next,
752 .show = uid_m_show,
753};
754
Fabian Frederickccf94f12014-08-08 14:21:22 -0700755const struct seq_operations proc_gid_seq_operations = {
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800756 .start = gid_m_start,
757 .stop = m_stop,
758 .next = m_next,
759 .show = gid_m_show,
760};
761
Fabian Frederickccf94f12014-08-08 14:21:22 -0700762const struct seq_operations proc_projid_seq_operations = {
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700763 .start = projid_m_start,
764 .stop = m_stop,
765 .next = m_next,
766 .show = projid_m_show,
767};
768
Fabian Frederick68a9a432014-06-06 14:37:21 -0700769static bool mappings_overlap(struct uid_gid_map *new_map,
770 struct uid_gid_extent *extent)
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800771{
772 u32 upper_first, lower_first, upper_last, lower_last;
773 unsigned idx;
774
775 upper_first = extent->first;
776 lower_first = extent->lower_first;
777 upper_last = upper_first + extent->count - 1;
778 lower_last = lower_first + extent->count - 1;
779
780 for (idx = 0; idx < new_map->nr_extents; idx++) {
781 u32 prev_upper_first, prev_lower_first;
782 u32 prev_upper_last, prev_lower_last;
783 struct uid_gid_extent *prev;
784
Christian Brauner6397fac2017-10-25 00:04:41 +0200785 if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
786 prev = &new_map->extent[idx];
787 else
788 prev = &new_map->forward[idx];
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800789
790 prev_upper_first = prev->first;
791 prev_lower_first = prev->lower_first;
792 prev_upper_last = prev_upper_first + prev->count - 1;
793 prev_lower_last = prev_lower_first + prev->count - 1;
794
795 /* Does the upper range intersect a previous extent? */
796 if ((prev_upper_first <= upper_last) &&
797 (prev_upper_last >= upper_first))
798 return true;
799
800 /* Does the lower range intersect a previous extent? */
801 if ((prev_lower_first <= lower_last) &&
802 (prev_lower_last >= lower_first))
803 return true;
804 }
805 return false;
806}
807
Christian Brauner6397fac2017-10-25 00:04:41 +0200808/**
809 * insert_extent - Safely insert a new idmap extent into struct uid_gid_map.
810 * Takes care to allocate a 4K block of memory if the number of mappings exceeds
811 * UID_GID_MAP_MAX_BASE_EXTENTS.
812 */
813static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
814{
815 if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS) {
816 map->extent[map->nr_extents].first = extent->first;
817 map->extent[map->nr_extents].lower_first = extent->lower_first;
818 map->extent[map->nr_extents].count = extent->count;
819 return 0;
820 }
821
822 if (map->nr_extents == UID_GID_MAP_MAX_BASE_EXTENTS) {
823 struct uid_gid_extent *forward;
824
825 /* Allocate memory for 340 mappings. */
826 forward = kmalloc(sizeof(struct uid_gid_extent) *
827 UID_GID_MAP_MAX_EXTENTS, GFP_KERNEL);
828 if (!forward)
829 return -ENOMEM;
830
831 /* Copy over memory. Only set up memory for the forward pointer.
832 * Defer the memory setup for the reverse pointer.
833 */
834 memcpy(forward, map->extent,
835 map->nr_extents * sizeof(map->extent[0]));
836
837 map->forward = forward;
838 map->reverse = NULL;
839 }
840
841 map->forward[map->nr_extents].first = extent->first;
842 map->forward[map->nr_extents].lower_first = extent->lower_first;
843 map->forward[map->nr_extents].count = extent->count;
844 return 0;
845}
846
847/* cmp function to sort() forward mappings */
848static int cmp_extents_forward(const void *a, const void *b)
849{
850 const struct uid_gid_extent *e1 = a;
851 const struct uid_gid_extent *e2 = b;
852
853 if (e1->first < e2->first)
854 return -1;
855
856 if (e1->first > e2->first)
857 return 1;
858
859 return 0;
860}
861
862/* cmp function to sort() reverse mappings */
863static int cmp_extents_reverse(const void *a, const void *b)
864{
865 const struct uid_gid_extent *e1 = a;
866 const struct uid_gid_extent *e2 = b;
867
868 if (e1->lower_first < e2->lower_first)
869 return -1;
870
871 if (e1->lower_first > e2->lower_first)
872 return 1;
873
874 return 0;
875}
876
877/**
878 * sort_idmaps - Sorts an array of idmap entries.
879 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
880 */
881static int sort_idmaps(struct uid_gid_map *map)
882{
883 if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
884 return 0;
885
886 /* Sort forward array. */
887 sort(map->forward, map->nr_extents, sizeof(struct uid_gid_extent),
888 cmp_extents_forward, NULL);
889
890 /* Only copy the memory from forward we actually need. */
891 map->reverse = kmemdup(map->forward,
892 map->nr_extents * sizeof(struct uid_gid_extent),
893 GFP_KERNEL);
894 if (!map->reverse)
895 return -ENOMEM;
896
897 /* Sort reverse array. */
898 sort(map->reverse, map->nr_extents, sizeof(struct uid_gid_extent),
899 cmp_extents_reverse, NULL);
900
901 return 0;
902}
903
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800904static ssize_t map_write(struct file *file, const char __user *buf,
905 size_t count, loff_t *ppos,
906 int cap_setid,
907 struct uid_gid_map *map,
908 struct uid_gid_map *parent_map)
909{
910 struct seq_file *seq = file->private_data;
911 struct user_namespace *ns = seq->private;
912 struct uid_gid_map new_map;
913 unsigned idx;
Christian Brauner6397fac2017-10-25 00:04:41 +0200914 struct uid_gid_extent extent;
Al Viro70f6cbb2015-12-24 00:13:10 -0500915 char *kbuf = NULL, *pos, *next_line;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800916 ssize_t ret = -EINVAL;
917
918 /*
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600919 * The userns_state_mutex serializes all writes to any given map.
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800920 *
921 * Any map is only ever written once.
922 *
923 * An id map fits within 1 cache line on most architectures.
924 *
925 * On read nothing needs to be done unless you are on an
926 * architecture with a crazy cache coherency model like alpha.
927 *
928 * There is a one time data dependency between reading the
929 * count of the extents and the values of the extents. The
930 * desired behavior is to see the values of the extents that
931 * were written before the count of the extents.
932 *
933 * To achieve this smp_wmb() is used on guarantee the write
Mikulas Patockae79323b2014-04-14 16:58:55 -0400934 * order and smp_rmb() is guaranteed that we don't have crazy
935 * architectures returning stale data.
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000936 */
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600937 mutex_lock(&userns_state_mutex);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800938
Christian Brauner6397fac2017-10-25 00:04:41 +0200939 memset(&new_map, 0, sizeof(struct uid_gid_map));
940
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800941 ret = -EPERM;
942 /* Only allow one successful write to the map */
943 if (map->nr_extents != 0)
944 goto out;
945
Andy Lutomirski41c21e32013-04-14 11:44:04 -0700946 /*
947 * Adjusting namespace settings requires capabilities on the target.
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800948 */
Andy Lutomirski41c21e32013-04-14 11:44:04 -0700949 if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800950 goto out;
951
Eric W. Biederman36476be2014-12-05 20:03:28 -0600952 /* Only allow < page size writes at the beginning of the file */
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800953 ret = -EINVAL;
954 if ((*ppos != 0) || (count >= PAGE_SIZE))
955 goto out;
956
957 /* Slurp in the user data */
Al Viro70f6cbb2015-12-24 00:13:10 -0500958 kbuf = memdup_user_nul(buf, count);
959 if (IS_ERR(kbuf)) {
960 ret = PTR_ERR(kbuf);
961 kbuf = NULL;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800962 goto out;
Al Viro70f6cbb2015-12-24 00:13:10 -0500963 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800964
965 /* Parse the user data */
966 ret = -EINVAL;
967 pos = kbuf;
Fabian Frederick68a9a432014-06-06 14:37:21 -0700968 for (; pos; pos = next_line) {
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800969
970 /* Find the end of line and ensure I don't look past it */
971 next_line = strchr(pos, '\n');
972 if (next_line) {
973 *next_line = '\0';
974 next_line++;
975 if (*next_line == '\0')
976 next_line = NULL;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000977 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800978
979 pos = skip_spaces(pos);
Christian Brauner6397fac2017-10-25 00:04:41 +0200980 extent.first = simple_strtoul(pos, &pos, 10);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800981 if (!isspace(*pos))
982 goto out;
983
984 pos = skip_spaces(pos);
Christian Brauner6397fac2017-10-25 00:04:41 +0200985 extent.lower_first = simple_strtoul(pos, &pos, 10);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800986 if (!isspace(*pos))
987 goto out;
988
989 pos = skip_spaces(pos);
Christian Brauner6397fac2017-10-25 00:04:41 +0200990 extent.count = simple_strtoul(pos, &pos, 10);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800991 if (*pos && !isspace(*pos))
992 goto out;
993
994 /* Verify there is not trailing junk on the line */
995 pos = skip_spaces(pos);
996 if (*pos != '\0')
997 goto out;
998
999 /* Verify we have been given valid starting values */
Christian Brauner6397fac2017-10-25 00:04:41 +02001000 if ((extent.first == (u32) -1) ||
1001 (extent.lower_first == (u32) -1))
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001002 goto out;
1003
Fabian Frederick68a9a432014-06-06 14:37:21 -07001004 /* Verify count is not zero and does not cause the
1005 * extent to wrap
1006 */
Christian Brauner6397fac2017-10-25 00:04:41 +02001007 if ((extent.first + extent.count) <= extent.first)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001008 goto out;
Christian Brauner6397fac2017-10-25 00:04:41 +02001009 if ((extent.lower_first + extent.count) <=
1010 extent.lower_first)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001011 goto out;
1012
Eric W. Biederman0bd14b42012-12-27 22:27:29 -08001013 /* Do the ranges in extent overlap any previous extents? */
Christian Brauner6397fac2017-10-25 00:04:41 +02001014 if (mappings_overlap(&new_map, &extent))
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001015 goto out;
1016
Christian Brauner6397fac2017-10-25 00:04:41 +02001017 if ((new_map.nr_extents + 1) == UID_GID_MAP_MAX_EXTENTS &&
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001018 (next_line != NULL))
1019 goto out;
Christian Brauner6397fac2017-10-25 00:04:41 +02001020
1021 ret = insert_extent(&new_map, &extent);
1022 if (ret < 0)
1023 goto out;
1024 ret = -EINVAL;
1025
1026 new_map.nr_extents++;
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001027 }
1028 /* Be very certaint the new map actually exists */
1029 if (new_map.nr_extents == 0)
1030 goto out;
1031
1032 ret = -EPERM;
1033 /* Validate the user is allowed to use user id's mapped to. */
Eric W. Biederman67080752013-04-14 13:47:02 -07001034 if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001035 goto out;
1036
Christian Brauner6397fac2017-10-25 00:04:41 +02001037 ret = sort_idmaps(&new_map);
1038 if (ret < 0)
1039 goto out;
1040
1041 ret = -EPERM;
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001042 /* Map the lower ids from the parent user namespace to the
1043 * kernel global id space.
1044 */
1045 for (idx = 0; idx < new_map.nr_extents; idx++) {
Christian Brauner6397fac2017-10-25 00:04:41 +02001046 struct uid_gid_extent *e;
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001047 u32 lower_first;
Christian Brauner6397fac2017-10-25 00:04:41 +02001048
1049 if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
1050 e = &new_map.extent[idx];
1051 else
1052 e = &new_map.forward[idx];
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001053
1054 lower_first = map_id_range_down(parent_map,
Christian Brauner6397fac2017-10-25 00:04:41 +02001055 e->lower_first,
1056 e->count);
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001057
1058 /* Fail if we can not map the specified extent to
1059 * the kernel global id space.
1060 */
1061 if (lower_first == (u32) -1)
1062 goto out;
1063
Christian Brauner6397fac2017-10-25 00:04:41 +02001064 e->lower_first = lower_first;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +00001065 }
1066
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001067 /* Install the map */
Christian Brauner6397fac2017-10-25 00:04:41 +02001068 if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) {
1069 memcpy(map->extent, new_map.extent,
1070 new_map.nr_extents * sizeof(new_map.extent[0]));
1071 } else {
1072 map->forward = new_map.forward;
1073 map->reverse = new_map.reverse;
1074 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001075 smp_wmb();
1076 map->nr_extents = new_map.nr_extents;
1077
1078 *ppos = count;
1079 ret = count;
1080out:
Christian Brauner6397fac2017-10-25 00:04:41 +02001081 if (ret < 0 && new_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
1082 kfree(new_map.forward);
1083 kfree(new_map.reverse);
1084 map->forward = NULL;
1085 map->reverse = NULL;
1086 map->nr_extents = 0;
1087 }
1088
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -06001089 mutex_unlock(&userns_state_mutex);
Al Viro70f6cbb2015-12-24 00:13:10 -05001090 kfree(kbuf);
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001091 return ret;
1092}
1093
Fabian Frederick68a9a432014-06-06 14:37:21 -07001094ssize_t proc_uid_map_write(struct file *file, const char __user *buf,
1095 size_t size, loff_t *ppos)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001096{
1097 struct seq_file *seq = file->private_data;
1098 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -07001099 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001100
1101 if (!ns->parent)
1102 return -EPERM;
1103
Eric W. Biedermanc450f372012-08-14 21:25:13 -07001104 if ((seq_ns != ns) && (seq_ns != ns->parent))
1105 return -EPERM;
1106
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001107 return map_write(file, buf, size, ppos, CAP_SETUID,
1108 &ns->uid_map, &ns->parent->uid_map);
1109}
1110
Fabian Frederick68a9a432014-06-06 14:37:21 -07001111ssize_t proc_gid_map_write(struct file *file, const char __user *buf,
1112 size_t size, loff_t *ppos)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001113{
1114 struct seq_file *seq = file->private_data;
1115 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -07001116 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001117
1118 if (!ns->parent)
1119 return -EPERM;
1120
Eric W. Biedermanc450f372012-08-14 21:25:13 -07001121 if ((seq_ns != ns) && (seq_ns != ns->parent))
1122 return -EPERM;
1123
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001124 return map_write(file, buf, size, ppos, CAP_SETGID,
1125 &ns->gid_map, &ns->parent->gid_map);
1126}
1127
Fabian Frederick68a9a432014-06-06 14:37:21 -07001128ssize_t proc_projid_map_write(struct file *file, const char __user *buf,
1129 size_t size, loff_t *ppos)
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07001130{
1131 struct seq_file *seq = file->private_data;
1132 struct user_namespace *ns = seq->private;
1133 struct user_namespace *seq_ns = seq_user_ns(seq);
1134
1135 if (!ns->parent)
1136 return -EPERM;
1137
1138 if ((seq_ns != ns) && (seq_ns != ns->parent))
1139 return -EPERM;
1140
1141 /* Anyone can set any valid project id no capability needed */
1142 return map_write(file, buf, size, ppos, -1,
1143 &ns->projid_map, &ns->parent->projid_map);
1144}
1145
Fabian Frederick68a9a432014-06-06 14:37:21 -07001146static bool new_idmap_permitted(const struct file *file,
Eric W. Biederman67080752013-04-14 13:47:02 -07001147 struct user_namespace *ns, int cap_setid,
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001148 struct uid_gid_map *new_map)
1149{
Eric W. Biedermanf95d7912014-11-26 23:22:14 -06001150 const struct cred *cred = file->f_cred;
Eric W. Biederman0542f172014-12-05 17:51:47 -06001151 /* Don't allow mappings that would allow anything that wouldn't
1152 * be allowed without the establishment of unprivileged mappings.
1153 */
Eric W. Biedermanf95d7912014-11-26 23:22:14 -06001154 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
1155 uid_eq(ns->owner, cred->euid)) {
Eric W. Biederman37657da2012-07-27 06:21:27 -07001156 u32 id = new_map->extent[0].lower_first;
1157 if (cap_setid == CAP_SETUID) {
1158 kuid_t uid = make_kuid(ns->parent, id);
Eric W. Biedermanf95d7912014-11-26 23:22:14 -06001159 if (uid_eq(uid, cred->euid))
Eric W. Biederman37657da2012-07-27 06:21:27 -07001160 return true;
Fabian Frederick68a9a432014-06-06 14:37:21 -07001161 } else if (cap_setid == CAP_SETGID) {
Eric W. Biederman37657da2012-07-27 06:21:27 -07001162 kgid_t gid = make_kgid(ns->parent, id);
Eric W. Biederman66d2f332014-12-05 19:36:04 -06001163 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
1164 gid_eq(gid, cred->egid))
Eric W. Biederman37657da2012-07-27 06:21:27 -07001165 return true;
1166 }
1167 }
1168
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07001169 /* Allow anyone to set a mapping that doesn't require privilege */
1170 if (!cap_valid(cap_setid))
1171 return true;
1172
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001173 /* Allow the specified ids if we have the appropriate capability
1174 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
Eric W. Biederman67080752013-04-14 13:47:02 -07001175 * And the opener of the id file also had the approprpiate capability.
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001176 */
Eric W. Biederman67080752013-04-14 13:47:02 -07001177 if (ns_capable(ns->parent, cap_setid) &&
1178 file_ns_capable(file, ns->parent, cap_setid))
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001179 return true;
1180
1181 return false;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +00001182}
Pavel Emelyanov61642812011-01-12 17:00:46 -08001183
Eric W. Biederman9cc46512014-12-02 12:27:26 -06001184int proc_setgroups_show(struct seq_file *seq, void *v)
1185{
1186 struct user_namespace *ns = seq->private;
1187 unsigned long userns_flags = ACCESS_ONCE(ns->flags);
1188
1189 seq_printf(seq, "%s\n",
1190 (userns_flags & USERNS_SETGROUPS_ALLOWED) ?
1191 "allow" : "deny");
1192 return 0;
1193}
1194
1195ssize_t proc_setgroups_write(struct file *file, const char __user *buf,
1196 size_t count, loff_t *ppos)
1197{
1198 struct seq_file *seq = file->private_data;
1199 struct user_namespace *ns = seq->private;
1200 char kbuf[8], *pos;
1201 bool setgroups_allowed;
1202 ssize_t ret;
1203
1204 /* Only allow a very narrow range of strings to be written */
1205 ret = -EINVAL;
1206 if ((*ppos != 0) || (count >= sizeof(kbuf)))
1207 goto out;
1208
1209 /* What was written? */
1210 ret = -EFAULT;
1211 if (copy_from_user(kbuf, buf, count))
1212 goto out;
1213 kbuf[count] = '\0';
1214 pos = kbuf;
1215
1216 /* What is being requested? */
1217 ret = -EINVAL;
1218 if (strncmp(pos, "allow", 5) == 0) {
1219 pos += 5;
1220 setgroups_allowed = true;
1221 }
1222 else if (strncmp(pos, "deny", 4) == 0) {
1223 pos += 4;
1224 setgroups_allowed = false;
1225 }
1226 else
1227 goto out;
1228
1229 /* Verify there is not trailing junk on the line */
1230 pos = skip_spaces(pos);
1231 if (*pos != '\0')
1232 goto out;
1233
1234 ret = -EPERM;
1235 mutex_lock(&userns_state_mutex);
1236 if (setgroups_allowed) {
1237 /* Enabling setgroups after setgroups has been disabled
1238 * is not allowed.
1239 */
1240 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED))
1241 goto out_unlock;
1242 } else {
1243 /* Permanently disabling setgroups after setgroups has
1244 * been enabled by writing the gid_map is not allowed.
1245 */
1246 if (ns->gid_map.nr_extents != 0)
1247 goto out_unlock;
1248 ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
1249 }
1250 mutex_unlock(&userns_state_mutex);
1251
1252 /* Report a successful write */
1253 *ppos = count;
1254 ret = count;
1255out:
1256 return ret;
1257out_unlock:
1258 mutex_unlock(&userns_state_mutex);
1259 goto out;
1260}
1261
Eric W. Biederman273d2c62014-12-05 18:01:11 -06001262bool userns_may_setgroups(const struct user_namespace *ns)
1263{
1264 bool allowed;
1265
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -06001266 mutex_lock(&userns_state_mutex);
Eric W. Biederman273d2c62014-12-05 18:01:11 -06001267 /* It is not safe to use setgroups until a gid mapping in
1268 * the user namespace has been established.
1269 */
1270 allowed = ns->gid_map.nr_extents != 0;
Eric W. Biederman9cc46512014-12-02 12:27:26 -06001271 /* Is setgroups allowed? */
1272 allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -06001273 mutex_unlock(&userns_state_mutex);
Eric W. Biederman273d2c62014-12-05 18:01:11 -06001274
1275 return allowed;
1276}
1277
Seth Forsheed07b8462015-09-23 15:16:04 -05001278/*
Eric W. Biedermana2b42622017-04-29 14:12:15 -05001279 * Returns true if @child is the same namespace or a descendant of
1280 * @ancestor.
Seth Forsheed07b8462015-09-23 15:16:04 -05001281 */
Eric W. Biedermana2b42622017-04-29 14:12:15 -05001282bool in_userns(const struct user_namespace *ancestor,
1283 const struct user_namespace *child)
1284{
1285 const struct user_namespace *ns;
1286 for (ns = child; ns->level > ancestor->level; ns = ns->parent)
1287 ;
1288 return (ns == ancestor);
1289}
1290
Seth Forsheed07b8462015-09-23 15:16:04 -05001291bool current_in_userns(const struct user_namespace *target_ns)
1292{
Eric W. Biedermana2b42622017-04-29 14:12:15 -05001293 return in_userns(target_ns, current_user_ns());
Seth Forsheed07b8462015-09-23 15:16:04 -05001294}
1295
Al Viro3c041182014-11-01 00:25:30 -04001296static inline struct user_namespace *to_user_ns(struct ns_common *ns)
1297{
1298 return container_of(ns, struct user_namespace, ns);
1299}
1300
Al Viro64964522014-11-01 00:37:32 -04001301static struct ns_common *userns_get(struct task_struct *task)
Eric W. Biedermancde19752012-07-26 06:24:06 -07001302{
1303 struct user_namespace *user_ns;
1304
1305 rcu_read_lock();
1306 user_ns = get_user_ns(__task_cred(task)->user_ns);
1307 rcu_read_unlock();
1308
Al Viro3c041182014-11-01 00:25:30 -04001309 return user_ns ? &user_ns->ns : NULL;
Eric W. Biedermancde19752012-07-26 06:24:06 -07001310}
1311
Al Viro64964522014-11-01 00:37:32 -04001312static void userns_put(struct ns_common *ns)
Eric W. Biedermancde19752012-07-26 06:24:06 -07001313{
Al Viro3c041182014-11-01 00:25:30 -04001314 put_user_ns(to_user_ns(ns));
Eric W. Biedermancde19752012-07-26 06:24:06 -07001315}
1316
Al Viro64964522014-11-01 00:37:32 -04001317static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
Eric W. Biedermancde19752012-07-26 06:24:06 -07001318{
Al Viro3c041182014-11-01 00:25:30 -04001319 struct user_namespace *user_ns = to_user_ns(ns);
Eric W. Biedermancde19752012-07-26 06:24:06 -07001320 struct cred *cred;
1321
1322 /* Don't allow gaining capabilities by reentering
1323 * the same user namespace.
1324 */
1325 if (user_ns == current_user_ns())
1326 return -EINVAL;
1327
Eric W. Biedermanfaf00da2015-08-10 18:25:44 -05001328 /* Tasks that share a thread group must share a user namespace */
1329 if (!thread_group_empty(current))
Eric W. Biedermancde19752012-07-26 06:24:06 -07001330 return -EINVAL;
1331
Eric W. Biedermane66eded2013-03-13 11:51:49 -07001332 if (current->fs->users != 1)
1333 return -EINVAL;
1334
Eric W. Biedermancde19752012-07-26 06:24:06 -07001335 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
1336 return -EPERM;
1337
1338 cred = prepare_creds();
1339 if (!cred)
1340 return -ENOMEM;
1341
1342 put_user_ns(cred->user_ns);
1343 set_cred_user_ns(cred, get_user_ns(user_ns));
1344
1345 return commit_creds(cred);
1346}
1347
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001348struct ns_common *ns_get_owner(struct ns_common *ns)
1349{
1350 struct user_namespace *my_user_ns = current_user_ns();
1351 struct user_namespace *owner, *p;
1352
1353 /* See if the owner is in the current user namespace */
1354 owner = p = ns->ops->owner(ns);
1355 for (;;) {
1356 if (!p)
1357 return ERR_PTR(-EPERM);
1358 if (p == my_user_ns)
1359 break;
1360 p = p->parent;
1361 }
1362
1363 return &get_user_ns(owner)->ns;
1364}
1365
1366static struct user_namespace *userns_owner(struct ns_common *ns)
1367{
1368 return to_user_ns(ns)->parent;
1369}
1370
Eric W. Biedermancde19752012-07-26 06:24:06 -07001371const struct proc_ns_operations userns_operations = {
1372 .name = "user",
1373 .type = CLONE_NEWUSER,
1374 .get = userns_get,
1375 .put = userns_put,
1376 .install = userns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001377 .owner = userns_owner,
Andrey Vagina7306ed2016-09-06 00:47:15 -07001378 .get_parent = ns_get_owner,
Eric W. Biedermancde19752012-07-26 06:24:06 -07001379};
1380
Pavel Emelyanov61642812011-01-12 17:00:46 -08001381static __init int user_namespaces_init(void)
1382{
1383 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
1384 return 0;
1385}
Paul Gortmakerc96d6662014-04-03 14:48:35 -07001386subsys_initcall(user_namespaces_init);