blob: 58c67e5f851c6a022d4b6c01af96c458c9f985b0 [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>
Cedric Le Goateracce2922007-07-15 23:40:59 -070011#include <linux/user_namespace.h>
David Howells0bb80f22013-04-12 01:50:06 +010012#include <linux/proc_ns.h>
Eric W. Biederman5c1469d2010-06-13 03:28:03 +000013#include <linux/highuid.h>
Serge Hallyn18b6e042008-10-15 16:38:45 -050014#include <linux/cred.h>
Eric W. Biederman973c5912011-11-17 01:59:07 -080015#include <linux/securebits.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080016#include <linux/keyctl.h>
17#include <linux/key-type.h>
18#include <keys/user-type.h>
19#include <linux/seq_file.h>
20#include <linux/fs.h>
21#include <linux/uaccess.h>
22#include <linux/ctype.h>
Eric W. Biedermanf76d2072012-08-30 01:24:05 -070023#include <linux/projid.h>
Eric W. Biedermane66eded2013-03-13 11:51:49 -070024#include <linux/fs_struct.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070025
Pavel Emelyanov61642812011-01-12 17:00:46 -080026static struct kmem_cache *user_ns_cachep __read_mostly;
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -060027static DEFINE_MUTEX(userns_state_mutex);
Pavel Emelyanov61642812011-01-12 17:00:46 -080028
Eric W. Biederman67080752013-04-14 13:47:02 -070029static bool new_idmap_permitted(const struct file *file,
30 struct user_namespace *ns, int cap_setid,
Eric W. Biederman22d917d2011-11-17 00:11:58 -080031 struct uid_gid_map *map);
Eric W. Biedermanb0321322016-07-30 13:53:37 -050032static void free_user_ns(struct work_struct *work);
Eric W. Biederman22d917d2011-11-17 00:11:58 -080033
Eric W. Biedermancde19752012-07-26 06:24:06 -070034static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
35{
36 /* Start with the same capabilities as init but useless for doing
37 * anything as the capabilities are bound to the new user namespace.
38 */
39 cred->securebits = SECUREBITS_DEFAULT;
40 cred->cap_inheritable = CAP_EMPTY_SET;
41 cred->cap_permitted = CAP_FULL_SET;
42 cred->cap_effective = CAP_FULL_SET;
Andy Lutomirski58319052015-09-04 15:42:45 -070043 cred->cap_ambient = CAP_EMPTY_SET;
Eric W. Biedermancde19752012-07-26 06:24:06 -070044 cred->cap_bset = CAP_FULL_SET;
45#ifdef CONFIG_KEYS
46 key_put(cred->request_key_auth);
47 cred->request_key_auth = NULL;
48#endif
49 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
50 cred->user_ns = user_ns;
51}
52
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070053/*
Serge Hallyn18b6e042008-10-15 16:38:45 -050054 * Create a new user namespace, deriving the creator from the user in the
55 * passed credentials, and replacing that user with the new root user for the
56 * new namespace.
57 *
58 * This is called by copy_creds(), which will finish setting the target task's
59 * credentials.
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070060 */
Serge Hallyn18b6e042008-10-15 16:38:45 -050061int create_user_ns(struct cred *new)
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070062{
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080063 struct user_namespace *ns, *parent_ns = new->user_ns;
Eric W. Biederman078de5f2012-02-08 07:00:08 -080064 kuid_t owner = new->euid;
65 kgid_t group = new->egid;
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -050066 struct ucounts *ucounts;
Eric W. Biederman98f842e2011-06-15 10:21:48 -070067 int ret;
Eric W. Biederman783291e2011-11-17 01:32:59 -080068
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -050069 ret = -EUSERS;
Oleg Nesterov8742f222013-08-08 18:55:32 +020070 if (parent_ns->level > 32)
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -050071 goto fail;
72
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -050073 ucounts = inc_user_namespaces(parent_ns, owner);
74 if (!ucounts)
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -050075 goto fail;
Oleg Nesterov8742f222013-08-08 18:55:32 +020076
Eric W. Biederman31515272013-03-15 01:45:51 -070077 /*
78 * Verify that we can not violate the policy of which files
79 * may be accessed that is specified by the root directory,
80 * by verifing that the root directory is at the root of the
81 * mount namespace which allows all files to be accessed.
82 */
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -050083 ret = -EPERM;
Eric W. Biederman31515272013-03-15 01:45:51 -070084 if (current_chrooted())
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -050085 goto fail_dec;
Eric W. Biederman31515272013-03-15 01:45:51 -070086
Eric W. Biederman783291e2011-11-17 01:32:59 -080087 /* The creator needs a mapping in the parent user namespace
88 * or else we won't be able to reasonably tell userspace who
89 * created a user_namespace.
90 */
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -050091 ret = -EPERM;
Eric W. Biederman783291e2011-11-17 01:32:59 -080092 if (!kuid_has_mapping(parent_ns, owner) ||
93 !kgid_has_mapping(parent_ns, group))
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -050094 goto fail_dec;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070095
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -050096 ret = -ENOMEM;
Eric W. Biederman22d917d2011-11-17 00:11:58 -080097 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070098 if (!ns)
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -050099 goto fail_dec;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -0700100
Al Viro6344c432014-11-01 00:45:45 -0400101 ret = ns_alloc_inum(&ns->ns);
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -0500102 if (ret)
103 goto fail_free;
Al Viro33c42942014-11-01 02:32:53 -0400104 ns->ns.ops = &userns_operations;
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700105
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800106 atomic_set(&ns->count, 1);
Eric W. Biedermancde19752012-07-26 06:24:06 -0700107 /* Leave the new->user_ns reference with the new user namespace. */
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -0800108 ns->parent = parent_ns;
Oleg Nesterov8742f222013-08-08 18:55:32 +0200109 ns->level = parent_ns->level + 1;
Eric W. Biederman783291e2011-11-17 01:32:59 -0800110 ns->owner = owner;
111 ns->group = group;
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500112 INIT_WORK(&ns->work, free_user_ns);
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -0500113 ns->max_user_namespaces = INT_MAX;
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -0500114 ns->ucounts = ucounts;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800115
Eric W. Biederman9cc46512014-12-02 12:27:26 -0600116 /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
117 mutex_lock(&userns_state_mutex);
118 ns->flags = parent_ns->flags;
119 mutex_unlock(&userns_state_mutex);
120
David Howellsf36f8c72013-09-24 10:35:19 +0100121#ifdef CONFIG_PERSISTENT_KEYRINGS
122 init_rwsem(&ns->persistent_keyring_register_sem);
123#endif
Eric W. Biedermandbec2842016-07-30 13:58:49 -0500124 ret = -ENOMEM;
125 if (!setup_userns_sysctls(ns))
126 goto fail_keyring;
127
128 set_cred_user_ns(new, ns);
Serge Hallyn18b6e042008-10-15 16:38:45 -0500129 return 0;
Eric W. Biedermandbec2842016-07-30 13:58:49 -0500130fail_keyring:
131#ifdef CONFIG_PERSISTENT_KEYRINGS
132 key_put(ns->persistent_keyring_register);
133#endif
134 ns_free_inum(&ns->ns);
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -0500135fail_free:
Eric W. Biedermandbec2842016-07-30 13:58:49 -0500136 kmem_cache_free(user_ns_cachep, ns);
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -0500137fail_dec:
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -0500138 dec_user_namespaces(ucounts);
Eric W. Biedermanb376c3e2016-08-08 13:41:24 -0500139fail:
Eric W. Biedermandbec2842016-07-30 13:58:49 -0500140 return ret;
Cedric Le Goateracce2922007-07-15 23:40:59 -0700141}
142
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700143int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
144{
145 struct cred *cred;
Oleg Nesterov61609682013-08-06 19:38:55 +0200146 int err = -ENOMEM;
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700147
148 if (!(unshare_flags & CLONE_NEWUSER))
149 return 0;
150
151 cred = prepare_creds();
Oleg Nesterov61609682013-08-06 19:38:55 +0200152 if (cred) {
153 err = create_user_ns(cred);
154 if (err)
155 put_cred(cred);
156 else
157 *new_cred = cred;
158 }
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700159
Oleg Nesterov61609682013-08-06 19:38:55 +0200160 return err;
Eric W. Biedermanb2e0d9872012-07-26 05:15:35 -0700161}
162
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500163static void free_user_ns(struct work_struct *work)
David Howells51708362009-02-27 14:03:03 -0800164{
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500165 struct user_namespace *parent, *ns =
166 container_of(work, struct user_namespace, work);
David Howells51708362009-02-27 14:03:03 -0800167
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800168 do {
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -0500169 struct ucounts *ucounts = ns->ucounts;
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800170 parent = ns->parent;
Eric W. Biedermandbec2842016-07-30 13:58:49 -0500171 retire_userns_sysctls(ns);
David Howellsf36f8c72013-09-24 10:35:19 +0100172#ifdef CONFIG_PERSISTENT_KEYRINGS
173 key_put(ns->persistent_keyring_register);
174#endif
Al Viro6344c432014-11-01 00:45:45 -0400175 ns_free_inum(&ns->ns);
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800176 kmem_cache_free(user_ns_cachep, ns);
Eric W. Biedermanf6b2db12016-08-08 13:54:50 -0500177 dec_user_namespaces(ucounts);
Eric W. Biedermanc61a2812012-12-28 18:58:39 -0800178 ns = parent;
179 } while (atomic_dec_and_test(&parent->count));
David Howells51708362009-02-27 14:03:03 -0800180}
Eric W. Biedermanb0321322016-07-30 13:53:37 -0500181
182void __put_user_ns(struct user_namespace *ns)
183{
184 schedule_work(&ns->work);
185}
186EXPORT_SYMBOL(__put_user_ns);
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000187
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800188static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000189{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800190 unsigned idx, extents;
191 u32 first, last, id2;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000192
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800193 id2 = id + count - 1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000194
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800195 /* Find the matching extent */
196 extents = map->nr_extents;
Mikulas Patockae79323b2014-04-14 16:58:55 -0400197 smp_rmb();
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800198 for (idx = 0; idx < extents; idx++) {
199 first = map->extent[idx].first;
200 last = first + map->extent[idx].count - 1;
201 if (id >= first && id <= last &&
202 (id2 >= first && id2 <= last))
203 break;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000204 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800205 /* Map the id or note failure */
206 if (idx < extents)
207 id = (id - first) + map->extent[idx].lower_first;
208 else
209 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000210
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800211 return id;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000212}
213
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800214static u32 map_id_down(struct uid_gid_map *map, u32 id)
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000215{
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800216 unsigned idx, extents;
217 u32 first, last;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000218
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800219 /* Find the matching extent */
220 extents = map->nr_extents;
Mikulas Patockae79323b2014-04-14 16:58:55 -0400221 smp_rmb();
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800222 for (idx = 0; idx < extents; idx++) {
223 first = map->extent[idx].first;
224 last = first + map->extent[idx].count - 1;
225 if (id >= first && id <= last)
226 break;
227 }
228 /* Map the id or note failure */
229 if (idx < extents)
230 id = (id - first) + map->extent[idx].lower_first;
231 else
232 id = (u32) -1;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000233
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800234 return id;
235}
236
237static u32 map_id_up(struct uid_gid_map *map, u32 id)
238{
239 unsigned idx, extents;
240 u32 first, last;
241
242 /* Find the matching extent */
243 extents = map->nr_extents;
Mikulas Patockae79323b2014-04-14 16:58:55 -0400244 smp_rmb();
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800245 for (idx = 0; idx < extents; idx++) {
246 first = map->extent[idx].lower_first;
247 last = first + map->extent[idx].count - 1;
248 if (id >= first && id <= last)
249 break;
250 }
251 /* Map the id or note failure */
252 if (idx < extents)
253 id = (id - first) + map->extent[idx].first;
254 else
255 id = (u32) -1;
256
257 return id;
258}
259
260/**
261 * make_kuid - Map a user-namespace uid pair into a kuid.
262 * @ns: User namespace that the uid is in
263 * @uid: User identifier
264 *
265 * Maps a user-namespace uid pair into a kernel internal kuid,
266 * and returns that kuid.
267 *
268 * When there is no mapping defined for the user-namespace uid
269 * pair INVALID_UID is returned. Callers are expected to test
Brian Campbellb080e042014-02-16 22:58:12 -0500270 * for and handle INVALID_UID being returned. INVALID_UID
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800271 * may be tested for using uid_valid().
272 */
273kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
274{
275 /* Map the uid to a global kernel uid */
276 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
277}
278EXPORT_SYMBOL(make_kuid);
279
280/**
281 * from_kuid - Create a uid from a kuid user-namespace pair.
282 * @targ: The user namespace we want a uid in.
283 * @kuid: The kernel internal uid to start with.
284 *
285 * Map @kuid into the user-namespace specified by @targ and
286 * return the resulting uid.
287 *
288 * There is always a mapping into the initial user_namespace.
289 *
290 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
291 */
292uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
293{
294 /* Map the uid from a global kernel uid */
295 return map_id_up(&targ->uid_map, __kuid_val(kuid));
296}
297EXPORT_SYMBOL(from_kuid);
298
299/**
300 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
301 * @targ: The user namespace we want a uid in.
302 * @kuid: The kernel internal uid to start with.
303 *
304 * Map @kuid into the user-namespace specified by @targ and
305 * return the resulting uid.
306 *
307 * There is always a mapping into the initial user_namespace.
308 *
309 * Unlike from_kuid from_kuid_munged never fails and always
310 * returns a valid uid. This makes from_kuid_munged appropriate
311 * for use in syscalls like stat and getuid where failing the
312 * system call and failing to provide a valid uid are not an
313 * options.
314 *
315 * If @kuid has no mapping in @targ overflowuid is returned.
316 */
317uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
318{
319 uid_t uid;
320 uid = from_kuid(targ, kuid);
321
322 if (uid == (uid_t) -1)
323 uid = overflowuid;
324 return uid;
325}
326EXPORT_SYMBOL(from_kuid_munged);
327
328/**
329 * make_kgid - Map a user-namespace gid pair into a kgid.
330 * @ns: User namespace that the gid is in
Fabian Frederick68a9a432014-06-06 14:37:21 -0700331 * @gid: group identifier
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800332 *
333 * Maps a user-namespace gid pair into a kernel internal kgid,
334 * and returns that kgid.
335 *
336 * When there is no mapping defined for the user-namespace gid
337 * pair INVALID_GID is returned. Callers are expected to test
338 * for and handle INVALID_GID being returned. INVALID_GID may be
339 * tested for using gid_valid().
340 */
341kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
342{
343 /* Map the gid to a global kernel gid */
344 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
345}
346EXPORT_SYMBOL(make_kgid);
347
348/**
349 * from_kgid - Create a gid from a kgid user-namespace pair.
350 * @targ: The user namespace we want a gid in.
351 * @kgid: The kernel internal gid to start with.
352 *
353 * Map @kgid into the user-namespace specified by @targ and
354 * return the resulting gid.
355 *
356 * There is always a mapping into the initial user_namespace.
357 *
358 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
359 */
360gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
361{
362 /* Map the gid from a global kernel gid */
363 return map_id_up(&targ->gid_map, __kgid_val(kgid));
364}
365EXPORT_SYMBOL(from_kgid);
366
367/**
368 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
369 * @targ: The user namespace we want a gid in.
370 * @kgid: The kernel internal gid to start with.
371 *
372 * Map @kgid into the user-namespace specified by @targ and
373 * return the resulting gid.
374 *
375 * There is always a mapping into the initial user_namespace.
376 *
377 * Unlike from_kgid from_kgid_munged never fails and always
378 * returns a valid gid. This makes from_kgid_munged appropriate
379 * for use in syscalls like stat and getgid where failing the
380 * system call and failing to provide a valid gid are not options.
381 *
382 * If @kgid has no mapping in @targ overflowgid is returned.
383 */
384gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
385{
386 gid_t gid;
387 gid = from_kgid(targ, kgid);
388
389 if (gid == (gid_t) -1)
390 gid = overflowgid;
391 return gid;
392}
393EXPORT_SYMBOL(from_kgid_munged);
394
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700395/**
396 * make_kprojid - Map a user-namespace projid pair into a kprojid.
397 * @ns: User namespace that the projid is in
398 * @projid: Project identifier
399 *
400 * Maps a user-namespace uid pair into a kernel internal kuid,
401 * and returns that kuid.
402 *
403 * When there is no mapping defined for the user-namespace projid
404 * pair INVALID_PROJID is returned. Callers are expected to test
405 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
406 * may be tested for using projid_valid().
407 */
408kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
409{
410 /* Map the uid to a global kernel uid */
411 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
412}
413EXPORT_SYMBOL(make_kprojid);
414
415/**
416 * from_kprojid - Create a projid from a kprojid user-namespace pair.
417 * @targ: The user namespace we want a projid in.
418 * @kprojid: The kernel internal project identifier to start with.
419 *
420 * Map @kprojid into the user-namespace specified by @targ and
421 * return the resulting projid.
422 *
423 * There is always a mapping into the initial user_namespace.
424 *
425 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
426 */
427projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
428{
429 /* Map the uid from a global kernel uid */
430 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
431}
432EXPORT_SYMBOL(from_kprojid);
433
434/**
435 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
436 * @targ: The user namespace we want a projid in.
437 * @kprojid: The kernel internal projid to start with.
438 *
439 * Map @kprojid into the user-namespace specified by @targ and
440 * return the resulting projid.
441 *
442 * There is always a mapping into the initial user_namespace.
443 *
444 * Unlike from_kprojid from_kprojid_munged never fails and always
445 * returns a valid projid. This makes from_kprojid_munged
446 * appropriate for use in syscalls like stat and where
447 * failing the system call and failing to provide a valid projid are
448 * not an options.
449 *
450 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
451 */
452projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
453{
454 projid_t projid;
455 projid = from_kprojid(targ, kprojid);
456
457 if (projid == (projid_t) -1)
458 projid = OVERFLOW_PROJID;
459 return projid;
460}
461EXPORT_SYMBOL(from_kprojid_munged);
462
463
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800464static int uid_m_show(struct seq_file *seq, void *v)
465{
466 struct user_namespace *ns = seq->private;
467 struct uid_gid_extent *extent = v;
468 struct user_namespace *lower_ns;
469 uid_t lower;
470
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700471 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800472 if ((lower_ns == ns) && lower_ns->parent)
473 lower_ns = lower_ns->parent;
474
475 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
476
477 seq_printf(seq, "%10u %10u %10u\n",
478 extent->first,
479 lower,
480 extent->count);
481
482 return 0;
483}
484
485static int gid_m_show(struct seq_file *seq, void *v)
486{
487 struct user_namespace *ns = seq->private;
488 struct uid_gid_extent *extent = v;
489 struct user_namespace *lower_ns;
490 gid_t lower;
491
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700492 lower_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800493 if ((lower_ns == ns) && lower_ns->parent)
494 lower_ns = lower_ns->parent;
495
496 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
497
498 seq_printf(seq, "%10u %10u %10u\n",
499 extent->first,
500 lower,
501 extent->count);
502
503 return 0;
504}
505
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700506static int projid_m_show(struct seq_file *seq, void *v)
507{
508 struct user_namespace *ns = seq->private;
509 struct uid_gid_extent *extent = v;
510 struct user_namespace *lower_ns;
511 projid_t lower;
512
513 lower_ns = seq_user_ns(seq);
514 if ((lower_ns == ns) && lower_ns->parent)
515 lower_ns = lower_ns->parent;
516
517 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
518
519 seq_printf(seq, "%10u %10u %10u\n",
520 extent->first,
521 lower,
522 extent->count);
523
524 return 0;
525}
526
Fabian Frederick68a9a432014-06-06 14:37:21 -0700527static void *m_start(struct seq_file *seq, loff_t *ppos,
528 struct uid_gid_map *map)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800529{
530 struct uid_gid_extent *extent = NULL;
531 loff_t pos = *ppos;
532
533 if (pos < map->nr_extents)
534 extent = &map->extent[pos];
535
536 return extent;
537}
538
539static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
540{
541 struct user_namespace *ns = seq->private;
542
543 return m_start(seq, ppos, &ns->uid_map);
544}
545
546static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
547{
548 struct user_namespace *ns = seq->private;
549
550 return m_start(seq, ppos, &ns->gid_map);
551}
552
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700553static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
554{
555 struct user_namespace *ns = seq->private;
556
557 return m_start(seq, ppos, &ns->projid_map);
558}
559
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800560static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
561{
562 (*pos)++;
563 return seq->op->start(seq, pos);
564}
565
566static void m_stop(struct seq_file *seq, void *v)
567{
568 return;
569}
570
Fabian Frederickccf94f12014-08-08 14:21:22 -0700571const struct seq_operations proc_uid_seq_operations = {
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800572 .start = uid_m_start,
573 .stop = m_stop,
574 .next = m_next,
575 .show = uid_m_show,
576};
577
Fabian Frederickccf94f12014-08-08 14:21:22 -0700578const struct seq_operations proc_gid_seq_operations = {
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800579 .start = gid_m_start,
580 .stop = m_stop,
581 .next = m_next,
582 .show = gid_m_show,
583};
584
Fabian Frederickccf94f12014-08-08 14:21:22 -0700585const struct seq_operations proc_projid_seq_operations = {
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700586 .start = projid_m_start,
587 .stop = m_stop,
588 .next = m_next,
589 .show = projid_m_show,
590};
591
Fabian Frederick68a9a432014-06-06 14:37:21 -0700592static bool mappings_overlap(struct uid_gid_map *new_map,
593 struct uid_gid_extent *extent)
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800594{
595 u32 upper_first, lower_first, upper_last, lower_last;
596 unsigned idx;
597
598 upper_first = extent->first;
599 lower_first = extent->lower_first;
600 upper_last = upper_first + extent->count - 1;
601 lower_last = lower_first + extent->count - 1;
602
603 for (idx = 0; idx < new_map->nr_extents; idx++) {
604 u32 prev_upper_first, prev_lower_first;
605 u32 prev_upper_last, prev_lower_last;
606 struct uid_gid_extent *prev;
607
608 prev = &new_map->extent[idx];
609
610 prev_upper_first = prev->first;
611 prev_lower_first = prev->lower_first;
612 prev_upper_last = prev_upper_first + prev->count - 1;
613 prev_lower_last = prev_lower_first + prev->count - 1;
614
615 /* Does the upper range intersect a previous extent? */
616 if ((prev_upper_first <= upper_last) &&
617 (prev_upper_last >= upper_first))
618 return true;
619
620 /* Does the lower range intersect a previous extent? */
621 if ((prev_lower_first <= lower_last) &&
622 (prev_lower_last >= lower_first))
623 return true;
624 }
625 return false;
626}
627
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800628static ssize_t map_write(struct file *file, const char __user *buf,
629 size_t count, loff_t *ppos,
630 int cap_setid,
631 struct uid_gid_map *map,
632 struct uid_gid_map *parent_map)
633{
634 struct seq_file *seq = file->private_data;
635 struct user_namespace *ns = seq->private;
636 struct uid_gid_map new_map;
637 unsigned idx;
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800638 struct uid_gid_extent *extent = NULL;
Al Viro70f6cbb2015-12-24 00:13:10 -0500639 char *kbuf = NULL, *pos, *next_line;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800640 ssize_t ret = -EINVAL;
641
642 /*
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600643 * The userns_state_mutex serializes all writes to any given map.
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800644 *
645 * Any map is only ever written once.
646 *
647 * An id map fits within 1 cache line on most architectures.
648 *
649 * On read nothing needs to be done unless you are on an
650 * architecture with a crazy cache coherency model like alpha.
651 *
652 * There is a one time data dependency between reading the
653 * count of the extents and the values of the extents. The
654 * desired behavior is to see the values of the extents that
655 * were written before the count of the extents.
656 *
657 * To achieve this smp_wmb() is used on guarantee the write
Mikulas Patockae79323b2014-04-14 16:58:55 -0400658 * order and smp_rmb() is guaranteed that we don't have crazy
659 * architectures returning stale data.
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000660 */
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600661 mutex_lock(&userns_state_mutex);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800662
663 ret = -EPERM;
664 /* Only allow one successful write to the map */
665 if (map->nr_extents != 0)
666 goto out;
667
Andy Lutomirski41c21e32013-04-14 11:44:04 -0700668 /*
669 * Adjusting namespace settings requires capabilities on the target.
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800670 */
Andy Lutomirski41c21e32013-04-14 11:44:04 -0700671 if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800672 goto out;
673
Eric W. Biederman36476be2014-12-05 20:03:28 -0600674 /* Only allow < page size writes at the beginning of the file */
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800675 ret = -EINVAL;
676 if ((*ppos != 0) || (count >= PAGE_SIZE))
677 goto out;
678
679 /* Slurp in the user data */
Al Viro70f6cbb2015-12-24 00:13:10 -0500680 kbuf = memdup_user_nul(buf, count);
681 if (IS_ERR(kbuf)) {
682 ret = PTR_ERR(kbuf);
683 kbuf = NULL;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800684 goto out;
Al Viro70f6cbb2015-12-24 00:13:10 -0500685 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800686
687 /* Parse the user data */
688 ret = -EINVAL;
689 pos = kbuf;
690 new_map.nr_extents = 0;
Fabian Frederick68a9a432014-06-06 14:37:21 -0700691 for (; pos; pos = next_line) {
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800692 extent = &new_map.extent[new_map.nr_extents];
693
694 /* Find the end of line and ensure I don't look past it */
695 next_line = strchr(pos, '\n');
696 if (next_line) {
697 *next_line = '\0';
698 next_line++;
699 if (*next_line == '\0')
700 next_line = NULL;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000701 }
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800702
703 pos = skip_spaces(pos);
704 extent->first = simple_strtoul(pos, &pos, 10);
705 if (!isspace(*pos))
706 goto out;
707
708 pos = skip_spaces(pos);
709 extent->lower_first = simple_strtoul(pos, &pos, 10);
710 if (!isspace(*pos))
711 goto out;
712
713 pos = skip_spaces(pos);
714 extent->count = simple_strtoul(pos, &pos, 10);
715 if (*pos && !isspace(*pos))
716 goto out;
717
718 /* Verify there is not trailing junk on the line */
719 pos = skip_spaces(pos);
720 if (*pos != '\0')
721 goto out;
722
723 /* Verify we have been given valid starting values */
724 if ((extent->first == (u32) -1) ||
Fabian Frederick68a9a432014-06-06 14:37:21 -0700725 (extent->lower_first == (u32) -1))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800726 goto out;
727
Fabian Frederick68a9a432014-06-06 14:37:21 -0700728 /* Verify count is not zero and does not cause the
729 * extent to wrap
730 */
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800731 if ((extent->first + extent->count) <= extent->first)
732 goto out;
Fabian Frederick68a9a432014-06-06 14:37:21 -0700733 if ((extent->lower_first + extent->count) <=
734 extent->lower_first)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800735 goto out;
736
Eric W. Biederman0bd14b42012-12-27 22:27:29 -0800737 /* Do the ranges in extent overlap any previous extents? */
738 if (mappings_overlap(&new_map, extent))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800739 goto out;
740
741 new_map.nr_extents++;
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800742
743 /* Fail if the file contains too many extents */
744 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
745 (next_line != NULL))
746 goto out;
747 }
748 /* Be very certaint the new map actually exists */
749 if (new_map.nr_extents == 0)
750 goto out;
751
752 ret = -EPERM;
753 /* Validate the user is allowed to use user id's mapped to. */
Eric W. Biederman67080752013-04-14 13:47:02 -0700754 if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800755 goto out;
756
757 /* Map the lower ids from the parent user namespace to the
758 * kernel global id space.
759 */
760 for (idx = 0; idx < new_map.nr_extents; idx++) {
761 u32 lower_first;
762 extent = &new_map.extent[idx];
763
764 lower_first = map_id_range_down(parent_map,
765 extent->lower_first,
766 extent->count);
767
768 /* Fail if we can not map the specified extent to
769 * the kernel global id space.
770 */
771 if (lower_first == (u32) -1)
772 goto out;
773
774 extent->lower_first = lower_first;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000775 }
776
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800777 /* Install the map */
778 memcpy(map->extent, new_map.extent,
779 new_map.nr_extents*sizeof(new_map.extent[0]));
780 smp_wmb();
781 map->nr_extents = new_map.nr_extents;
782
783 *ppos = count;
784 ret = count;
785out:
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600786 mutex_unlock(&userns_state_mutex);
Al Viro70f6cbb2015-12-24 00:13:10 -0500787 kfree(kbuf);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800788 return ret;
789}
790
Fabian Frederick68a9a432014-06-06 14:37:21 -0700791ssize_t proc_uid_map_write(struct file *file, const char __user *buf,
792 size_t size, loff_t *ppos)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800793{
794 struct seq_file *seq = file->private_data;
795 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700796 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800797
798 if (!ns->parent)
799 return -EPERM;
800
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700801 if ((seq_ns != ns) && (seq_ns != ns->parent))
802 return -EPERM;
803
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800804 return map_write(file, buf, size, ppos, CAP_SETUID,
805 &ns->uid_map, &ns->parent->uid_map);
806}
807
Fabian Frederick68a9a432014-06-06 14:37:21 -0700808ssize_t proc_gid_map_write(struct file *file, const char __user *buf,
809 size_t size, loff_t *ppos)
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800810{
811 struct seq_file *seq = file->private_data;
812 struct user_namespace *ns = seq->private;
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700813 struct user_namespace *seq_ns = seq_user_ns(seq);
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800814
815 if (!ns->parent)
816 return -EPERM;
817
Eric W. Biedermanc450f372012-08-14 21:25:13 -0700818 if ((seq_ns != ns) && (seq_ns != ns->parent))
819 return -EPERM;
820
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800821 return map_write(file, buf, size, ppos, CAP_SETGID,
822 &ns->gid_map, &ns->parent->gid_map);
823}
824
Fabian Frederick68a9a432014-06-06 14:37:21 -0700825ssize_t proc_projid_map_write(struct file *file, const char __user *buf,
826 size_t size, loff_t *ppos)
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700827{
828 struct seq_file *seq = file->private_data;
829 struct user_namespace *ns = seq->private;
830 struct user_namespace *seq_ns = seq_user_ns(seq);
831
832 if (!ns->parent)
833 return -EPERM;
834
835 if ((seq_ns != ns) && (seq_ns != ns->parent))
836 return -EPERM;
837
838 /* Anyone can set any valid project id no capability needed */
839 return map_write(file, buf, size, ppos, -1,
840 &ns->projid_map, &ns->parent->projid_map);
841}
842
Fabian Frederick68a9a432014-06-06 14:37:21 -0700843static bool new_idmap_permitted(const struct file *file,
Eric W. Biederman67080752013-04-14 13:47:02 -0700844 struct user_namespace *ns, int cap_setid,
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800845 struct uid_gid_map *new_map)
846{
Eric W. Biedermanf95d7912014-11-26 23:22:14 -0600847 const struct cred *cred = file->f_cred;
Eric W. Biederman0542f172014-12-05 17:51:47 -0600848 /* Don't allow mappings that would allow anything that wouldn't
849 * be allowed without the establishment of unprivileged mappings.
850 */
Eric W. Biedermanf95d7912014-11-26 23:22:14 -0600851 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
852 uid_eq(ns->owner, cred->euid)) {
Eric W. Biederman37657da2012-07-27 06:21:27 -0700853 u32 id = new_map->extent[0].lower_first;
854 if (cap_setid == CAP_SETUID) {
855 kuid_t uid = make_kuid(ns->parent, id);
Eric W. Biedermanf95d7912014-11-26 23:22:14 -0600856 if (uid_eq(uid, cred->euid))
Eric W. Biederman37657da2012-07-27 06:21:27 -0700857 return true;
Fabian Frederick68a9a432014-06-06 14:37:21 -0700858 } else if (cap_setid == CAP_SETGID) {
Eric W. Biederman37657da2012-07-27 06:21:27 -0700859 kgid_t gid = make_kgid(ns->parent, id);
Eric W. Biederman66d2f332014-12-05 19:36:04 -0600860 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
861 gid_eq(gid, cred->egid))
Eric W. Biederman37657da2012-07-27 06:21:27 -0700862 return true;
863 }
864 }
865
Eric W. Biedermanf76d2072012-08-30 01:24:05 -0700866 /* Allow anyone to set a mapping that doesn't require privilege */
867 if (!cap_valid(cap_setid))
868 return true;
869
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800870 /* Allow the specified ids if we have the appropriate capability
871 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
Eric W. Biederman67080752013-04-14 13:47:02 -0700872 * And the opener of the id file also had the approprpiate capability.
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800873 */
Eric W. Biederman67080752013-04-14 13:47:02 -0700874 if (ns_capable(ns->parent, cap_setid) &&
875 file_ns_capable(file, ns->parent, cap_setid))
Eric W. Biederman22d917d2011-11-17 00:11:58 -0800876 return true;
877
878 return false;
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000879}
Pavel Emelyanov61642812011-01-12 17:00:46 -0800880
Eric W. Biederman9cc46512014-12-02 12:27:26 -0600881int proc_setgroups_show(struct seq_file *seq, void *v)
882{
883 struct user_namespace *ns = seq->private;
884 unsigned long userns_flags = ACCESS_ONCE(ns->flags);
885
886 seq_printf(seq, "%s\n",
887 (userns_flags & USERNS_SETGROUPS_ALLOWED) ?
888 "allow" : "deny");
889 return 0;
890}
891
892ssize_t proc_setgroups_write(struct file *file, const char __user *buf,
893 size_t count, loff_t *ppos)
894{
895 struct seq_file *seq = file->private_data;
896 struct user_namespace *ns = seq->private;
897 char kbuf[8], *pos;
898 bool setgroups_allowed;
899 ssize_t ret;
900
901 /* Only allow a very narrow range of strings to be written */
902 ret = -EINVAL;
903 if ((*ppos != 0) || (count >= sizeof(kbuf)))
904 goto out;
905
906 /* What was written? */
907 ret = -EFAULT;
908 if (copy_from_user(kbuf, buf, count))
909 goto out;
910 kbuf[count] = '\0';
911 pos = kbuf;
912
913 /* What is being requested? */
914 ret = -EINVAL;
915 if (strncmp(pos, "allow", 5) == 0) {
916 pos += 5;
917 setgroups_allowed = true;
918 }
919 else if (strncmp(pos, "deny", 4) == 0) {
920 pos += 4;
921 setgroups_allowed = false;
922 }
923 else
924 goto out;
925
926 /* Verify there is not trailing junk on the line */
927 pos = skip_spaces(pos);
928 if (*pos != '\0')
929 goto out;
930
931 ret = -EPERM;
932 mutex_lock(&userns_state_mutex);
933 if (setgroups_allowed) {
934 /* Enabling setgroups after setgroups has been disabled
935 * is not allowed.
936 */
937 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED))
938 goto out_unlock;
939 } else {
940 /* Permanently disabling setgroups after setgroups has
941 * been enabled by writing the gid_map is not allowed.
942 */
943 if (ns->gid_map.nr_extents != 0)
944 goto out_unlock;
945 ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
946 }
947 mutex_unlock(&userns_state_mutex);
948
949 /* Report a successful write */
950 *ppos = count;
951 ret = count;
952out:
953 return ret;
954out_unlock:
955 mutex_unlock(&userns_state_mutex);
956 goto out;
957}
958
Eric W. Biederman273d2c62014-12-05 18:01:11 -0600959bool userns_may_setgroups(const struct user_namespace *ns)
960{
961 bool allowed;
962
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600963 mutex_lock(&userns_state_mutex);
Eric W. Biederman273d2c62014-12-05 18:01:11 -0600964 /* It is not safe to use setgroups until a gid mapping in
965 * the user namespace has been established.
966 */
967 allowed = ns->gid_map.nr_extents != 0;
Eric W. Biederman9cc46512014-12-02 12:27:26 -0600968 /* Is setgroups allowed? */
969 allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
Eric W. Biedermanf0d62ae2014-12-09 14:03:14 -0600970 mutex_unlock(&userns_state_mutex);
Eric W. Biederman273d2c62014-12-05 18:01:11 -0600971
972 return allowed;
973}
974
Seth Forsheed07b8462015-09-23 15:16:04 -0500975/*
976 * Returns true if @ns is the same namespace as or a descendant of
977 * @target_ns.
978 */
979bool current_in_userns(const struct user_namespace *target_ns)
980{
981 struct user_namespace *ns;
982 for (ns = current_user_ns(); ns; ns = ns->parent) {
983 if (ns == target_ns)
984 return true;
985 }
986 return false;
987}
988
Al Viro3c041182014-11-01 00:25:30 -0400989static inline struct user_namespace *to_user_ns(struct ns_common *ns)
990{
991 return container_of(ns, struct user_namespace, ns);
992}
993
Al Viro64964522014-11-01 00:37:32 -0400994static struct ns_common *userns_get(struct task_struct *task)
Eric W. Biedermancde19752012-07-26 06:24:06 -0700995{
996 struct user_namespace *user_ns;
997
998 rcu_read_lock();
999 user_ns = get_user_ns(__task_cred(task)->user_ns);
1000 rcu_read_unlock();
1001
Al Viro3c041182014-11-01 00:25:30 -04001002 return user_ns ? &user_ns->ns : NULL;
Eric W. Biedermancde19752012-07-26 06:24:06 -07001003}
1004
Al Viro64964522014-11-01 00:37:32 -04001005static void userns_put(struct ns_common *ns)
Eric W. Biedermancde19752012-07-26 06:24:06 -07001006{
Al Viro3c041182014-11-01 00:25:30 -04001007 put_user_ns(to_user_ns(ns));
Eric W. Biedermancde19752012-07-26 06:24:06 -07001008}
1009
Al Viro64964522014-11-01 00:37:32 -04001010static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
Eric W. Biedermancde19752012-07-26 06:24:06 -07001011{
Al Viro3c041182014-11-01 00:25:30 -04001012 struct user_namespace *user_ns = to_user_ns(ns);
Eric W. Biedermancde19752012-07-26 06:24:06 -07001013 struct cred *cred;
1014
1015 /* Don't allow gaining capabilities by reentering
1016 * the same user namespace.
1017 */
1018 if (user_ns == current_user_ns())
1019 return -EINVAL;
1020
Eric W. Biedermanfaf00da2015-08-10 18:25:44 -05001021 /* Tasks that share a thread group must share a user namespace */
1022 if (!thread_group_empty(current))
Eric W. Biedermancde19752012-07-26 06:24:06 -07001023 return -EINVAL;
1024
Eric W. Biedermane66eded2013-03-13 11:51:49 -07001025 if (current->fs->users != 1)
1026 return -EINVAL;
1027
Eric W. Biedermancde19752012-07-26 06:24:06 -07001028 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
1029 return -EPERM;
1030
1031 cred = prepare_creds();
1032 if (!cred)
1033 return -ENOMEM;
1034
1035 put_user_ns(cred->user_ns);
1036 set_cred_user_ns(cred, get_user_ns(user_ns));
1037
1038 return commit_creds(cred);
1039}
1040
1041const struct proc_ns_operations userns_operations = {
1042 .name = "user",
1043 .type = CLONE_NEWUSER,
1044 .get = userns_get,
1045 .put = userns_put,
1046 .install = userns_install,
1047};
1048
Pavel Emelyanov61642812011-01-12 17:00:46 -08001049static __init int user_namespaces_init(void)
1050{
1051 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
1052 return 0;
1053}
Paul Gortmakerc96d6662014-04-03 14:48:35 -07001054subsys_initcall(user_namespaces_init);