blob: 563a2981d7c79e894d772ac2657d1f7262c74c5b [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
Eric W. Biederman11a8b922017-10-31 15:54:32 -0500235 id2 = key->id + key->count - 1;
Christian Brauner6397fac2017-10-25 00:04:41 +0200236
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 */
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500259static struct uid_gid_extent *
260map_id_range_down_max(unsigned extents, struct uid_gid_map *map, u32 id, u32 count)
Christian Brauner6397fac2017-10-25 00:04:41 +0200261{
Christian Brauner6397fac2017-10-25 00:04:41 +0200262 struct idmap_key key;
263
264 key.map_up = false;
265 key.count = count;
266 key.id = id;
267
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500268 return bsearch(&key, map->forward, extents,
269 sizeof(struct uid_gid_extent), cmp_map_id);
270}
271
272/**
273 * map_id_range_down_base - Find idmap via binary search in static extent array.
274 * Can only be called if number of mappings is equal or less than
275 * UID_GID_MAP_MAX_BASE_EXTENTS.
276 */
277static struct uid_gid_extent *
278map_id_range_down_base(unsigned extents, struct uid_gid_map *map, u32 id, u32 count)
279{
280 unsigned idx;
281 u32 first, last, id2;
282
283 id2 = id + count - 1;
284
285 /* Find the matching extent */
286 for (idx = 0; idx < extents; idx++) {
287 first = map->extent[idx].first;
288 last = first + map->extent[idx].count - 1;
289 if (id >= first && id <= last &&
290 (id2 >= first && id2 <= last))
291 return &map->extent[idx];
292 }
293 return NULL;
294}
295
296static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
297{
298 struct uid_gid_extent *extent;
299 unsigned extents = map->nr_extents;
Christian Brauner6397fac2017-10-25 00:04:41 +0200300 smp_rmb();
301
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500302 if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
303 extent = map_id_range_down_base(extents, map, id, count);
304 else
305 extent = map_id_range_down_max(extents, map, id, count);
306
Christian Brauner6397fac2017-10-25 00:04:41 +0200307 /* Map the id or note failure */
308 if (extent)
309 id = (id - extent->first) + extent->lower_first;
310 else
311 id = (u32) -1;
312
313 return id;
314}
315
316/**
Christian Brauner6397fac2017-10-25 00:04:41 +0200317 * map_id_down_base - Find idmap via binary search in static extent array.
318 * Can only be called if number of mappings is equal or less than
319 * UID_GID_MAP_MAX_BASE_EXTENTS.
320 */
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500321static struct uid_gid_extent *
322map_id_down_base(unsigned extents, struct uid_gid_map *map, u32 id)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000323{
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500324 unsigned idx;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800325 u32 first, last;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000326
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800327 /* Find the matching extent */
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800328 for (idx = 0; idx < extents; idx++) {
329 first = map->extent[idx].first;
330 last = first + map->extent[idx].count - 1;
331 if (id >= first && id <= last)
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500332 return &map->extent[idx];
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800333 }
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500334 return NULL;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800335}
336
Christian Brauner6397fac2017-10-25 00:04:41 +0200337static u32 map_id_down(struct uid_gid_map *map, u32 id)
338{
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500339 struct uid_gid_extent *extent;
340 unsigned extents = map->nr_extents;
Christian Brauner6397fac2017-10-25 00:04:41 +0200341 smp_rmb();
342
343 if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500344 extent = map_id_down_base(extents, map, id);
345 else
346 extent = map_id_range_down_max(extents, map, id, 1);
Christian Brauner6397fac2017-10-25 00:04:41 +0200347
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500348 /* Map the id or note failure */
349 if (extent)
350 id = (id - extent->first) + extent->lower_first;
351 else
352 id = (u32) -1;
353
354 return id;
Christian Brauner6397fac2017-10-25 00:04:41 +0200355}
356
357/**
358 * map_id_up_base - Find idmap via binary search in static extent array.
359 * Can only be called if number of mappings is equal or less than
360 * UID_GID_MAP_MAX_BASE_EXTENTS.
361 */
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500362static struct uid_gid_extent *
363map_id_up_base(unsigned extents, struct uid_gid_map *map, u32 id)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800364{
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500365 unsigned idx;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800366 u32 first, last;
367
368 /* Find the matching extent */
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800369 for (idx = 0; idx < extents; idx++) {
370 first = map->extent[idx].lower_first;
371 last = first + map->extent[idx].count - 1;
372 if (id >= first && id <= last)
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500373 return &map->extent[idx];
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800374 }
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500375 return NULL;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800376}
377
378/**
Christian Brauner6397fac2017-10-25 00:04:41 +0200379 * map_id_up_max - Find idmap via binary search in ordered idmap array.
380 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
381 */
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500382static struct uid_gid_extent *
383map_id_up_max(unsigned extents, struct uid_gid_map *map, u32 id)
Christian Brauner6397fac2017-10-25 00:04:41 +0200384{
Christian Brauner6397fac2017-10-25 00:04:41 +0200385 struct idmap_key key;
386
387 key.map_up = true;
Eric W. Biederman11a8b922017-10-31 15:54:32 -0500388 key.count = 1;
Christian Brauner6397fac2017-10-25 00:04:41 +0200389 key.id = id;
390
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500391 return bsearch(&key, map->reverse, extents,
392 sizeof(struct uid_gid_extent), cmp_map_id);
393}
394
395static u32 map_id_up(struct uid_gid_map *map, u32 id)
396{
397 struct uid_gid_extent *extent;
398 unsigned extents = map->nr_extents;
Christian Brauner6397fac2017-10-25 00:04:41 +0200399 smp_rmb();
400
Eric W. Biederman3edf6522017-10-31 16:27:29 -0500401 if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
402 extent = map_id_up_base(extents, map, id);
403 else
404 extent = map_id_up_max(extents, map, id);
405
Christian Brauner6397fac2017-10-25 00:04:41 +0200406 /* Map the id or note failure */
407 if (extent)
408 id = (id - extent->lower_first) + extent->first;
409 else
410 id = (u32) -1;
411
412 return id;
413}
414
Christian Brauner6397fac2017-10-25 00:04:41 +0200415/**
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800416 * make_kuid - Map a user-namespace uid pair into a kuid.
417 * @ns: User namespace that the uid is in
418 * @uid: User identifier
419 *
420 * Maps a user-namespace uid pair into a kernel internal kuid,
421 * and returns that kuid.
422 *
423 * When there is no mapping defined for the user-namespace uid
424 * pair INVALID_UID is returned. Callers are expected to test
Brian Campbellb080e042014-02-16 22:58:12 -0500425 * for and handle INVALID_UID being returned. INVALID_UID
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800426 * may be tested for using uid_valid().
427 */
428kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
429{
430 /* Map the uid to a global kernel uid */
431 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
432}
433EXPORT_SYMBOL(make_kuid);
434
435/**
436 * from_kuid - Create a uid from a kuid user-namespace pair.
437 * @targ: The user namespace we want a uid in.
438 * @kuid: The kernel internal uid to start with.
439 *
440 * Map @kuid into the user-namespace specified by @targ and
441 * return the resulting uid.
442 *
443 * There is always a mapping into the initial user_namespace.
444 *
445 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
446 */
447uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
448{
449 /* Map the uid from a global kernel uid */
450 return map_id_up(&targ->uid_map, __kuid_val(kuid));
451}
452EXPORT_SYMBOL(from_kuid);
453
454/**
455 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
456 * @targ: The user namespace we want a uid in.
457 * @kuid: The kernel internal uid to start with.
458 *
459 * Map @kuid into the user-namespace specified by @targ and
460 * return the resulting uid.
461 *
462 * There is always a mapping into the initial user_namespace.
463 *
464 * Unlike from_kuid from_kuid_munged never fails and always
465 * returns a valid uid. This makes from_kuid_munged appropriate
466 * for use in syscalls like stat and getuid where failing the
467 * system call and failing to provide a valid uid are not an
468 * options.
469 *
470 * If @kuid has no mapping in @targ overflowuid is returned.
471 */
472uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
473{
474 uid_t uid;
475 uid = from_kuid(targ, kuid);
476
477 if (uid == (uid_t) -1)
478 uid = overflowuid;
479 return uid;
480}
481EXPORT_SYMBOL(from_kuid_munged);
482
483/**
484 * make_kgid - Map a user-namespace gid pair into a kgid.
485 * @ns: User namespace that the gid is in
Fabian Frederick68a9a432014-06-06 14:37:21 -0700486 * @gid: group identifier
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800487 *
488 * Maps a user-namespace gid pair into a kernel internal kgid,
489 * and returns that kgid.
490 *
491 * When there is no mapping defined for the user-namespace gid
492 * pair INVALID_GID is returned. Callers are expected to test
493 * for and handle INVALID_GID being returned. INVALID_GID may be
494 * tested for using gid_valid().
495 */
496kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
497{
498 /* Map the gid to a global kernel gid */
499 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
500}
501EXPORT_SYMBOL(make_kgid);
502
503/**
504 * from_kgid - Create a gid from a kgid user-namespace pair.
505 * @targ: The user namespace we want a gid in.
506 * @kgid: The kernel internal gid to start with.
507 *
508 * Map @kgid into the user-namespace specified by @targ and
509 * return the resulting gid.
510 *
511 * There is always a mapping into the initial user_namespace.
512 *
513 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
514 */
515gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
516{
517 /* Map the gid from a global kernel gid */
518 return map_id_up(&targ->gid_map, __kgid_val(kgid));
519}
520EXPORT_SYMBOL(from_kgid);
521
522/**
523 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
524 * @targ: The user namespace we want a gid in.
525 * @kgid: The kernel internal gid to start with.
526 *
527 * Map @kgid into the user-namespace specified by @targ and
528 * return the resulting gid.
529 *
530 * There is always a mapping into the initial user_namespace.
531 *
532 * Unlike from_kgid from_kgid_munged never fails and always
533 * returns a valid gid. This makes from_kgid_munged appropriate
534 * for use in syscalls like stat and getgid where failing the
535 * system call and failing to provide a valid gid are not options.
536 *
537 * If @kgid has no mapping in @targ overflowgid is returned.
538 */
539gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
540{
541 gid_t gid;
542 gid = from_kgid(targ, kgid);
543
544 if (gid == (gid_t) -1)
545 gid = overflowgid;
546 return gid;
547}
548EXPORT_SYMBOL(from_kgid_munged);
549
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700550/**
551 * make_kprojid - Map a user-namespace projid pair into a kprojid.
552 * @ns: User namespace that the projid is in
553 * @projid: Project identifier
554 *
555 * Maps a user-namespace uid pair into a kernel internal kuid,
556 * and returns that kuid.
557 *
558 * When there is no mapping defined for the user-namespace projid
559 * pair INVALID_PROJID is returned. Callers are expected to test
560 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
561 * may be tested for using projid_valid().
562 */
563kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
564{
565 /* Map the uid to a global kernel uid */
566 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
567}
568EXPORT_SYMBOL(make_kprojid);
569
570/**
571 * from_kprojid - Create a projid from a kprojid user-namespace pair.
572 * @targ: The user namespace we want a projid in.
573 * @kprojid: The kernel internal project identifier to start with.
574 *
575 * Map @kprojid into the user-namespace specified by @targ and
576 * return the resulting projid.
577 *
578 * There is always a mapping into the initial user_namespace.
579 *
580 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
581 */
582projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
583{
584 /* Map the uid from a global kernel uid */
585 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
586}
587EXPORT_SYMBOL(from_kprojid);
588
589/**
590 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
591 * @targ: The user namespace we want a projid in.
592 * @kprojid: The kernel internal projid to start with.
593 *
594 * Map @kprojid into the user-namespace specified by @targ and
595 * return the resulting projid.
596 *
597 * There is always a mapping into the initial user_namespace.
598 *
599 * Unlike from_kprojid from_kprojid_munged never fails and always
600 * returns a valid projid. This makes from_kprojid_munged
601 * appropriate for use in syscalls like stat and where
602 * failing the system call and failing to provide a valid projid are
603 * not an options.
604 *
605 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
606 */
607projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
608{
609 projid_t projid;
610 projid = from_kprojid(targ, kprojid);
611
612 if (projid == (projid_t) -1)
613 projid = OVERFLOW_PROJID;
614 return projid;
615}
616EXPORT_SYMBOL(from_kprojid_munged);
617
618
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800619static int uid_m_show(struct seq_file *seq, void *v)
620{
621 struct user_namespace *ns = seq->private;
622 struct uid_gid_extent *extent = v;
623 struct user_namespace *lower_ns;
624 uid_t lower;
625
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700626 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800627 if ((lower_ns == ns) && lower_ns->parent)
628 lower_ns = lower_ns->parent;
629
630 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
631
632 seq_printf(seq, "%10u %10u %10u\n",
633 extent->first,
634 lower,
635 extent->count);
636
637 return 0;
638}
639
640static int gid_m_show(struct seq_file *seq, void *v)
641{
642 struct user_namespace *ns = seq->private;
643 struct uid_gid_extent *extent = v;
644 struct user_namespace *lower_ns;
645 gid_t lower;
646
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700647 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800648 if ((lower_ns == ns) && lower_ns->parent)
649 lower_ns = lower_ns->parent;
650
651 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
652
653 seq_printf(seq, "%10u %10u %10u\n",
654 extent->first,
655 lower,
656 extent->count);
657
658 return 0;
659}
660
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700661static int projid_m_show(struct seq_file *seq, void *v)
662{
663 struct user_namespace *ns = seq->private;
664 struct uid_gid_extent *extent = v;
665 struct user_namespace *lower_ns;
666 projid_t lower;
667
668 lower_ns = seq_user_ns(seq);
669 if ((lower_ns == ns) && lower_ns->parent)
670 lower_ns = lower_ns->parent;
671
672 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
673
674 seq_printf(seq, "%10u %10u %10u\n",
675 extent->first,
676 lower,
677 extent->count);
678
679 return 0;
680}
681
Fabian Frederick68a9a432014-06-06 14:37:21 -0700682static void *m_start(struct seq_file *seq, loff_t *ppos,
683 struct uid_gid_map *map)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800684{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800685 loff_t pos = *ppos;
686
Christian Brauner6397fac2017-10-25 00:04:41 +0200687 if (pos >= map->nr_extents)
688 return NULL;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800689
Christian Brauner6397fac2017-10-25 00:04:41 +0200690 if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
691 return &map->extent[pos];
692
693 return &map->forward[pos];
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800694}
695
696static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
697{
698 struct user_namespace *ns = seq->private;
699
700 return m_start(seq, ppos, &ns->uid_map);
701}
702
703static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
704{
705 struct user_namespace *ns = seq->private;
706
707 return m_start(seq, ppos, &ns->gid_map);
708}
709
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700710static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
711{
712 struct user_namespace *ns = seq->private;
713
714 return m_start(seq, ppos, &ns->projid_map);
715}
716
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800717static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
718{
719 (*pos)++;
720 return seq->op->start(seq, pos);
721}
722
723static void m_stop(struct seq_file *seq, void *v)
724{
725 return;
726}
727
Fabian Frederickccf94f12014-08-08 14:21:22 -0700728const struct seq_operations proc_uid_seq_operations = {
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800729 .start = uid_m_start,
730 .stop = m_stop,
731 .next = m_next,
732 .show = uid_m_show,
733};
734
Fabian Frederickccf94f12014-08-08 14:21:22 -0700735const struct seq_operations proc_gid_seq_operations = {
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800736 .start = gid_m_start,
737 .stop = m_stop,
738 .next = m_next,
739 .show = gid_m_show,
740};
741
Fabian Frederickccf94f12014-08-08 14:21:22 -0700742const struct seq_operations proc_projid_seq_operations = {
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700743 .start = projid_m_start,
744 .stop = m_stop,
745 .next = m_next,
746 .show = projid_m_show,
747};
748
Fabian Frederick68a9a432014-06-06 14:37:21 -0700749static bool mappings_overlap(struct uid_gid_map *new_map,
750 struct uid_gid_extent *extent)
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800751{
752 u32 upper_first, lower_first, upper_last, lower_last;
753 unsigned idx;
754
755 upper_first = extent->first;
756 lower_first = extent->lower_first;
757 upper_last = upper_first + extent->count - 1;
758 lower_last = lower_first + extent->count - 1;
759
760 for (idx = 0; idx < new_map->nr_extents; idx++) {
761 u32 prev_upper_first, prev_lower_first;
762 u32 prev_upper_last, prev_lower_last;
763 struct uid_gid_extent *prev;
764
Christian Brauner6397fac2017-10-25 00:04:41 +0200765 if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
766 prev = &new_map->extent[idx];
767 else
768 prev = &new_map->forward[idx];
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800769
770 prev_upper_first = prev->first;
771 prev_lower_first = prev->lower_first;
772 prev_upper_last = prev_upper_first + prev->count - 1;
773 prev_lower_last = prev_lower_first + prev->count - 1;
774
775 /* Does the upper range intersect a previous extent? */
776 if ((prev_upper_first <= upper_last) &&
777 (prev_upper_last >= upper_first))
778 return true;
779
780 /* Does the lower range intersect a previous extent? */
781 if ((prev_lower_first <= lower_last) &&
782 (prev_lower_last >= lower_first))
783 return true;
784 }
785 return false;
786}
787
Christian Brauner6397fac2017-10-25 00:04:41 +0200788/**
789 * insert_extent - Safely insert a new idmap extent into struct uid_gid_map.
790 * Takes care to allocate a 4K block of memory if the number of mappings exceeds
791 * UID_GID_MAP_MAX_BASE_EXTENTS.
792 */
793static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
794{
795 if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS) {
796 map->extent[map->nr_extents].first = extent->first;
797 map->extent[map->nr_extents].lower_first = extent->lower_first;
798 map->extent[map->nr_extents].count = extent->count;
799 return 0;
800 }
801
802 if (map->nr_extents == UID_GID_MAP_MAX_BASE_EXTENTS) {
803 struct uid_gid_extent *forward;
804
805 /* Allocate memory for 340 mappings. */
806 forward = kmalloc(sizeof(struct uid_gid_extent) *
807 UID_GID_MAP_MAX_EXTENTS, GFP_KERNEL);
808 if (!forward)
809 return -ENOMEM;
810
811 /* Copy over memory. Only set up memory for the forward pointer.
812 * Defer the memory setup for the reverse pointer.
813 */
814 memcpy(forward, map->extent,
815 map->nr_extents * sizeof(map->extent[0]));
816
817 map->forward = forward;
818 map->reverse = NULL;
819 }
820
821 map->forward[map->nr_extents].first = extent->first;
822 map->forward[map->nr_extents].lower_first = extent->lower_first;
823 map->forward[map->nr_extents].count = extent->count;
824 return 0;
825}
826
827/* cmp function to sort() forward mappings */
828static int cmp_extents_forward(const void *a, const void *b)
829{
830 const struct uid_gid_extent *e1 = a;
831 const struct uid_gid_extent *e2 = b;
832
833 if (e1->first < e2->first)
834 return -1;
835
836 if (e1->first > e2->first)
837 return 1;
838
839 return 0;
840}
841
842/* cmp function to sort() reverse mappings */
843static int cmp_extents_reverse(const void *a, const void *b)
844{
845 const struct uid_gid_extent *e1 = a;
846 const struct uid_gid_extent *e2 = b;
847
848 if (e1->lower_first < e2->lower_first)
849 return -1;
850
851 if (e1->lower_first > e2->lower_first)
852 return 1;
853
854 return 0;
855}
856
857/**
858 * sort_idmaps - Sorts an array of idmap entries.
859 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
860 */
861static int sort_idmaps(struct uid_gid_map *map)
862{
863 if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
864 return 0;
865
866 /* Sort forward array. */
867 sort(map->forward, map->nr_extents, sizeof(struct uid_gid_extent),
868 cmp_extents_forward, NULL);
869
870 /* Only copy the memory from forward we actually need. */
871 map->reverse = kmemdup(map->forward,
872 map->nr_extents * sizeof(struct uid_gid_extent),
873 GFP_KERNEL);
874 if (!map->reverse)
875 return -ENOMEM;
876
877 /* Sort reverse array. */
878 sort(map->reverse, map->nr_extents, sizeof(struct uid_gid_extent),
879 cmp_extents_reverse, NULL);
880
881 return 0;
882}
883
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800884static ssize_t map_write(struct file *file, const char __user *buf,
885 size_t count, loff_t *ppos,
886 int cap_setid,
887 struct uid_gid_map *map,
888 struct uid_gid_map *parent_map)
889{
890 struct seq_file *seq = file->private_data;
891 struct user_namespace *ns = seq->private;
892 struct uid_gid_map new_map;
893 unsigned idx;
Christian Brauner6397fac2017-10-25 00:04:41 +0200894 struct uid_gid_extent extent;
Al Viro70f6cbb2015-12-24 00:13:10 -0500895 char *kbuf = NULL, *pos, *next_line;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800896 ssize_t ret = -EINVAL;
897
898 /*
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600899 * The userns_state_mutex serializes all writes to any given map.
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800900 *
901 * Any map is only ever written once.
902 *
903 * An id map fits within 1 cache line on most architectures.
904 *
905 * On read nothing needs to be done unless you are on an
906 * architecture with a crazy cache coherency model like alpha.
907 *
908 * There is a one time data dependency between reading the
909 * count of the extents and the values of the extents. The
910 * desired behavior is to see the values of the extents that
911 * were written before the count of the extents.
912 *
913 * To achieve this smp_wmb() is used on guarantee the write
Mikulas Patockae79323b2014-04-14 16:58:55 -0400914 * order and smp_rmb() is guaranteed that we don't have crazy
915 * architectures returning stale data.
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000916 */
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600917 mutex_lock(&userns_state_mutex);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800918
Christian Brauner6397fac2017-10-25 00:04:41 +0200919 memset(&new_map, 0, sizeof(struct uid_gid_map));
920
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800921 ret = -EPERM;
922 /* Only allow one successful write to the map */
923 if (map->nr_extents != 0)
924 goto out;
925
Andy Lutomirski41c21e32013-04-14 11:44:04 -0700926 /*
927 * Adjusting namespace settings requires capabilities on the target.
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800928 */
Andy Lutomirski41c21e32013-04-14 11:44:04 -0700929 if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800930 goto out;
931
Eric W. Biederman36476be2014-12-05 20:03:28 -0600932 /* Only allow < page size writes at the beginning of the file */
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800933 ret = -EINVAL;
934 if ((*ppos != 0) || (count >= PAGE_SIZE))
935 goto out;
936
937 /* Slurp in the user data */
Al Viro70f6cbb2015-12-24 00:13:10 -0500938 kbuf = memdup_user_nul(buf, count);
939 if (IS_ERR(kbuf)) {
940 ret = PTR_ERR(kbuf);
941 kbuf = NULL;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800942 goto out;
Al Viro70f6cbb2015-12-24 00:13:10 -0500943 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800944
945 /* Parse the user data */
946 ret = -EINVAL;
947 pos = kbuf;
Fabian Frederick68a9a432014-06-06 14:37:21 -0700948 for (; pos; pos = next_line) {
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800949
950 /* Find the end of line and ensure I don't look past it */
951 next_line = strchr(pos, '\n');
952 if (next_line) {
953 *next_line = '\0';
954 next_line++;
955 if (*next_line == '\0')
956 next_line = NULL;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000957 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800958
959 pos = skip_spaces(pos);
Christian Brauner6397fac2017-10-25 00:04:41 +0200960 extent.first = simple_strtoul(pos, &pos, 10);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800961 if (!isspace(*pos))
962 goto out;
963
964 pos = skip_spaces(pos);
Christian Brauner6397fac2017-10-25 00:04:41 +0200965 extent.lower_first = simple_strtoul(pos, &pos, 10);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800966 if (!isspace(*pos))
967 goto out;
968
969 pos = skip_spaces(pos);
Christian Brauner6397fac2017-10-25 00:04:41 +0200970 extent.count = simple_strtoul(pos, &pos, 10);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800971 if (*pos && !isspace(*pos))
972 goto out;
973
974 /* Verify there is not trailing junk on the line */
975 pos = skip_spaces(pos);
976 if (*pos != '\0')
977 goto out;
978
979 /* Verify we have been given valid starting values */
Christian Brauner6397fac2017-10-25 00:04:41 +0200980 if ((extent.first == (u32) -1) ||
981 (extent.lower_first == (u32) -1))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800982 goto out;
983
Fabian Frederick68a9a432014-06-06 14:37:21 -0700984 /* Verify count is not zero and does not cause the
985 * extent to wrap
986 */
Christian Brauner6397fac2017-10-25 00:04:41 +0200987 if ((extent.first + extent.count) <= extent.first)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800988 goto out;
Christian Brauner6397fac2017-10-25 00:04:41 +0200989 if ((extent.lower_first + extent.count) <=
990 extent.lower_first)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800991 goto out;
992
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800993 /* Do the ranges in extent overlap any previous extents? */
Christian Brauner6397fac2017-10-25 00:04:41 +0200994 if (mappings_overlap(&new_map, &extent))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800995 goto out;
996
Christian Brauner6397fac2017-10-25 00:04:41 +0200997 if ((new_map.nr_extents + 1) == UID_GID_MAP_MAX_EXTENTS &&
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800998 (next_line != NULL))
999 goto out;
Christian Brauner6397fac2017-10-25 00:04:41 +02001000
1001 ret = insert_extent(&new_map, &extent);
1002 if (ret < 0)
1003 goto out;
1004 ret = -EINVAL;
1005
1006 new_map.nr_extents++;
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001007 }
1008 /* Be very certaint the new map actually exists */
1009 if (new_map.nr_extents == 0)
1010 goto out;
1011
1012 ret = -EPERM;
1013 /* Validate the user is allowed to use user id's mapped to. */
Eric W. Biederman67080752013-04-14 13:47:02 -07001014 if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001015 goto out;
1016
Christian Brauner6397fac2017-10-25 00:04:41 +02001017 ret = sort_idmaps(&new_map);
1018 if (ret < 0)
1019 goto out;
1020
1021 ret = -EPERM;
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001022 /* Map the lower ids from the parent user namespace to the
1023 * kernel global id space.
1024 */
1025 for (idx = 0; idx < new_map.nr_extents; idx++) {
Christian Brauner6397fac2017-10-25 00:04:41 +02001026 struct uid_gid_extent *e;
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001027 u32 lower_first;
Christian Brauner6397fac2017-10-25 00:04:41 +02001028
1029 if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
1030 e = &new_map.extent[idx];
1031 else
1032 e = &new_map.forward[idx];
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001033
1034 lower_first = map_id_range_down(parent_map,
Christian Brauner6397fac2017-10-25 00:04:41 +02001035 e->lower_first,
1036 e->count);
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001037
1038 /* Fail if we can not map the specified extent to
1039 * the kernel global id space.
1040 */
1041 if (lower_first == (u32) -1)
1042 goto out;
1043
Christian Brauner6397fac2017-10-25 00:04:41 +02001044 e->lower_first = lower_first;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +00001045 }
1046
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001047 /* Install the map */
Christian Brauner6397fac2017-10-25 00:04:41 +02001048 if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) {
1049 memcpy(map->extent, new_map.extent,
1050 new_map.nr_extents * sizeof(new_map.extent[0]));
1051 } else {
1052 map->forward = new_map.forward;
1053 map->reverse = new_map.reverse;
1054 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001055 smp_wmb();
1056 map->nr_extents = new_map.nr_extents;
1057
1058 *ppos = count;
1059 ret = count;
1060out:
Christian Brauner6397fac2017-10-25 00:04:41 +02001061 if (ret < 0 && new_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
1062 kfree(new_map.forward);
1063 kfree(new_map.reverse);
1064 map->forward = NULL;
1065 map->reverse = NULL;
1066 map->nr_extents = 0;
1067 }
1068
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -06001069 mutex_unlock(&userns_state_mutex);
Al Viro70f6cbb2015-12-24 00:13:10 -05001070 kfree(kbuf);
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001071 return ret;
1072}
1073
Fabian Frederick68a9a432014-06-06 14:37:21 -07001074ssize_t proc_uid_map_write(struct file *file, const char __user *buf,
1075 size_t size, loff_t *ppos)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001076{
1077 struct seq_file *seq = file->private_data;
1078 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -07001079 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001080
1081 if (!ns->parent)
1082 return -EPERM;
1083
Eric W. Biedermanc450f372012-08-14 21:25:13 -07001084 if ((seq_ns != ns) && (seq_ns != ns->parent))
1085 return -EPERM;
1086
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001087 return map_write(file, buf, size, ppos, CAP_SETUID,
1088 &ns->uid_map, &ns->parent->uid_map);
1089}
1090
Fabian Frederick68a9a432014-06-06 14:37:21 -07001091ssize_t proc_gid_map_write(struct file *file, const char __user *buf,
1092 size_t size, loff_t *ppos)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001093{
1094 struct seq_file *seq = file->private_data;
1095 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -07001096 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001097
1098 if (!ns->parent)
1099 return -EPERM;
1100
Eric W. Biedermanc450f372012-08-14 21:25:13 -07001101 if ((seq_ns != ns) && (seq_ns != ns->parent))
1102 return -EPERM;
1103
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001104 return map_write(file, buf, size, ppos, CAP_SETGID,
1105 &ns->gid_map, &ns->parent->gid_map);
1106}
1107
Fabian Frederick68a9a432014-06-06 14:37:21 -07001108ssize_t proc_projid_map_write(struct file *file, const char __user *buf,
1109 size_t size, loff_t *ppos)
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07001110{
1111 struct seq_file *seq = file->private_data;
1112 struct user_namespace *ns = seq->private;
1113 struct user_namespace *seq_ns = seq_user_ns(seq);
1114
1115 if (!ns->parent)
1116 return -EPERM;
1117
1118 if ((seq_ns != ns) && (seq_ns != ns->parent))
1119 return -EPERM;
1120
1121 /* Anyone can set any valid project id no capability needed */
1122 return map_write(file, buf, size, ppos, -1,
1123 &ns->projid_map, &ns->parent->projid_map);
1124}
1125
Fabian Frederick68a9a432014-06-06 14:37:21 -07001126static bool new_idmap_permitted(const struct file *file,
Eric W. Biederman67080752013-04-14 13:47:02 -07001127 struct user_namespace *ns, int cap_setid,
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001128 struct uid_gid_map *new_map)
1129{
Eric W. Biedermanf95d7912014-11-26 23:22:14 -06001130 const struct cred *cred = file->f_cred;
Eric W. Biederman0542f172014-12-05 17:51:47 -06001131 /* Don't allow mappings that would allow anything that wouldn't
1132 * be allowed without the establishment of unprivileged mappings.
1133 */
Eric W. Biedermanf95d7912014-11-26 23:22:14 -06001134 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
1135 uid_eq(ns->owner, cred->euid)) {
Eric W. Biederman37657da2012-07-27 06:21:27 -07001136 u32 id = new_map->extent[0].lower_first;
1137 if (cap_setid == CAP_SETUID) {
1138 kuid_t uid = make_kuid(ns->parent, id);
Eric W. Biedermanf95d7912014-11-26 23:22:14 -06001139 if (uid_eq(uid, cred->euid))
Eric W. Biederman37657da2012-07-27 06:21:27 -07001140 return true;
Fabian Frederick68a9a432014-06-06 14:37:21 -07001141 } else if (cap_setid == CAP_SETGID) {
Eric W. Biederman37657da2012-07-27 06:21:27 -07001142 kgid_t gid = make_kgid(ns->parent, id);
Eric W. Biederman66d2f332014-12-05 19:36:04 -06001143 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
1144 gid_eq(gid, cred->egid))
Eric W. Biederman37657da2012-07-27 06:21:27 -07001145 return true;
1146 }
1147 }
1148
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07001149 /* Allow anyone to set a mapping that doesn't require privilege */
1150 if (!cap_valid(cap_setid))
1151 return true;
1152
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001153 /* Allow the specified ids if we have the appropriate capability
1154 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
Eric W. Biederman67080752013-04-14 13:47:02 -07001155 * And the opener of the id file also had the approprpiate capability.
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001156 */
Eric W. Biederman67080752013-04-14 13:47:02 -07001157 if (ns_capable(ns->parent, cap_setid) &&
1158 file_ns_capable(file, ns->parent, cap_setid))
Eric W. Biederman22d917d2011-11-17 00:11:58 -08001159 return true;
1160
1161 return false;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +00001162}
Pavel Emelyanov61642812011-01-12 17:00:46 -08001163
Eric W. Biederman9cc46512014-12-02 12:27:26 -06001164int proc_setgroups_show(struct seq_file *seq, void *v)
1165{
1166 struct user_namespace *ns = seq->private;
1167 unsigned long userns_flags = ACCESS_ONCE(ns->flags);
1168
1169 seq_printf(seq, "%s\n",
1170 (userns_flags & USERNS_SETGROUPS_ALLOWED) ?
1171 "allow" : "deny");
1172 return 0;
1173}
1174
1175ssize_t proc_setgroups_write(struct file *file, const char __user *buf,
1176 size_t count, loff_t *ppos)
1177{
1178 struct seq_file *seq = file->private_data;
1179 struct user_namespace *ns = seq->private;
1180 char kbuf[8], *pos;
1181 bool setgroups_allowed;
1182 ssize_t ret;
1183
1184 /* Only allow a very narrow range of strings to be written */
1185 ret = -EINVAL;
1186 if ((*ppos != 0) || (count >= sizeof(kbuf)))
1187 goto out;
1188
1189 /* What was written? */
1190 ret = -EFAULT;
1191 if (copy_from_user(kbuf, buf, count))
1192 goto out;
1193 kbuf[count] = '\0';
1194 pos = kbuf;
1195
1196 /* What is being requested? */
1197 ret = -EINVAL;
1198 if (strncmp(pos, "allow", 5) == 0) {
1199 pos += 5;
1200 setgroups_allowed = true;
1201 }
1202 else if (strncmp(pos, "deny", 4) == 0) {
1203 pos += 4;
1204 setgroups_allowed = false;
1205 }
1206 else
1207 goto out;
1208
1209 /* Verify there is not trailing junk on the line */
1210 pos = skip_spaces(pos);
1211 if (*pos != '\0')
1212 goto out;
1213
1214 ret = -EPERM;
1215 mutex_lock(&userns_state_mutex);
1216 if (setgroups_allowed) {
1217 /* Enabling setgroups after setgroups has been disabled
1218 * is not allowed.
1219 */
1220 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED))
1221 goto out_unlock;
1222 } else {
1223 /* Permanently disabling setgroups after setgroups has
1224 * been enabled by writing the gid_map is not allowed.
1225 */
1226 if (ns->gid_map.nr_extents != 0)
1227 goto out_unlock;
1228 ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
1229 }
1230 mutex_unlock(&userns_state_mutex);
1231
1232 /* Report a successful write */
1233 *ppos = count;
1234 ret = count;
1235out:
1236 return ret;
1237out_unlock:
1238 mutex_unlock(&userns_state_mutex);
1239 goto out;
1240}
1241
Eric W. Biederman273d2c62014-12-05 18:01:11 -06001242bool userns_may_setgroups(const struct user_namespace *ns)
1243{
1244 bool allowed;
1245
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -06001246 mutex_lock(&userns_state_mutex);
Eric W. Biederman273d2c62014-12-05 18:01:11 -06001247 /* It is not safe to use setgroups until a gid mapping in
1248 * the user namespace has been established.
1249 */
1250 allowed = ns->gid_map.nr_extents != 0;
Eric W. Biederman9cc46512014-12-02 12:27:26 -06001251 /* Is setgroups allowed? */
1252 allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -06001253 mutex_unlock(&userns_state_mutex);
Eric W. Biederman273d2c62014-12-05 18:01:11 -06001254
1255 return allowed;
1256}
1257
Seth Forsheed07b8462015-09-23 15:16:04 -05001258/*
Eric W. Biedermana2b42622017-04-29 14:12:15 -05001259 * Returns true if @child is the same namespace or a descendant of
1260 * @ancestor.
Seth Forsheed07b8462015-09-23 15:16:04 -05001261 */
Eric W. Biedermana2b42622017-04-29 14:12:15 -05001262bool in_userns(const struct user_namespace *ancestor,
1263 const struct user_namespace *child)
1264{
1265 const struct user_namespace *ns;
1266 for (ns = child; ns->level > ancestor->level; ns = ns->parent)
1267 ;
1268 return (ns == ancestor);
1269}
1270
Seth Forsheed07b8462015-09-23 15:16:04 -05001271bool current_in_userns(const struct user_namespace *target_ns)
1272{
Eric W. Biedermana2b42622017-04-29 14:12:15 -05001273 return in_userns(target_ns, current_user_ns());
Seth Forsheed07b8462015-09-23 15:16:04 -05001274}
1275
Al Viro3c041182014-11-01 00:25:30 -04001276static inline struct user_namespace *to_user_ns(struct ns_common *ns)
1277{
1278 return container_of(ns, struct user_namespace, ns);
1279}
1280
Al Viro64964522014-11-01 00:37:32 -04001281static struct ns_common *userns_get(struct task_struct *task)
Eric W. Biedermancde19752012-07-26 06:24:06 -07001282{
1283 struct user_namespace *user_ns;
1284
1285 rcu_read_lock();
1286 user_ns = get_user_ns(__task_cred(task)->user_ns);
1287 rcu_read_unlock();
1288
Al Viro3c041182014-11-01 00:25:30 -04001289 return user_ns ? &user_ns->ns : NULL;
Eric W. Biedermancde19752012-07-26 06:24:06 -07001290}
1291
Al Viro64964522014-11-01 00:37:32 -04001292static void userns_put(struct ns_common *ns)
Eric W. Biedermancde19752012-07-26 06:24:06 -07001293{
Al Viro3c041182014-11-01 00:25:30 -04001294 put_user_ns(to_user_ns(ns));
Eric W. Biedermancde19752012-07-26 06:24:06 -07001295}
1296
Al Viro64964522014-11-01 00:37:32 -04001297static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
Eric W. Biedermancde19752012-07-26 06:24:06 -07001298{
Al Viro3c041182014-11-01 00:25:30 -04001299 struct user_namespace *user_ns = to_user_ns(ns);
Eric W. Biedermancde19752012-07-26 06:24:06 -07001300 struct cred *cred;
1301
1302 /* Don't allow gaining capabilities by reentering
1303 * the same user namespace.
1304 */
1305 if (user_ns == current_user_ns())
1306 return -EINVAL;
1307
Eric W. Biedermanfaf00da2015-08-10 18:25:44 -05001308 /* Tasks that share a thread group must share a user namespace */
1309 if (!thread_group_empty(current))
Eric W. Biedermancde19752012-07-26 06:24:06 -07001310 return -EINVAL;
1311
Eric W. Biedermane66eded2013-03-13 11:51:49 -07001312 if (current->fs->users != 1)
1313 return -EINVAL;
1314
Eric W. Biedermancde19752012-07-26 06:24:06 -07001315 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
1316 return -EPERM;
1317
1318 cred = prepare_creds();
1319 if (!cred)
1320 return -ENOMEM;
1321
1322 put_user_ns(cred->user_ns);
1323 set_cred_user_ns(cred, get_user_ns(user_ns));
1324
1325 return commit_creds(cred);
1326}
1327
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001328struct ns_common *ns_get_owner(struct ns_common *ns)
1329{
1330 struct user_namespace *my_user_ns = current_user_ns();
1331 struct user_namespace *owner, *p;
1332
1333 /* See if the owner is in the current user namespace */
1334 owner = p = ns->ops->owner(ns);
1335 for (;;) {
1336 if (!p)
1337 return ERR_PTR(-EPERM);
1338 if (p == my_user_ns)
1339 break;
1340 p = p->parent;
1341 }
1342
1343 return &get_user_ns(owner)->ns;
1344}
1345
1346static struct user_namespace *userns_owner(struct ns_common *ns)
1347{
1348 return to_user_ns(ns)->parent;
1349}
1350
Eric W. Biedermancde19752012-07-26 06:24:06 -07001351const struct proc_ns_operations userns_operations = {
1352 .name = "user",
1353 .type = CLONE_NEWUSER,
1354 .get = userns_get,
1355 .put = userns_put,
1356 .install = userns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001357 .owner = userns_owner,
Andrey Vagina7306ed2016-09-06 00:47:15 -07001358 .get_parent = ns_get_owner,
Eric W. Biedermancde19752012-07-26 06:24:06 -07001359};
1360
Pavel Emelyanov61642812011-01-12 17:00:46 -08001361static __init int user_namespaces_init(void)
1362{
1363 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
1364 return 0;
1365}
Paul Gortmakerc96d6662014-04-03 14:48:35 -07001366subsys_initcall(user_namespaces_init);