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
8 #include <linux/export.h>
9 #include <linux/nsproxy.h>
10 #include <linux/slab.h>
11 #include <linux/user_namespace.h>
12 #include <linux/highuid.h>
13 #include <linux/cred.h>
14 #include <linux/securebits.h>
15 #include <linux/keyctl.h>
16 #include <linux/key-type.h>
17 #include <keys/user-type.h>
18 #include <linux/seq_file.h>
20 #include <linux/uaccess.h>
21 #include <linux/ctype.h>
23 static struct kmem_cache
*user_ns_cachep __read_mostly
;
25 static bool new_idmap_permitted(struct user_namespace
*ns
, int cap_setid
,
26 struct uid_gid_map
*map
);
29 * Create a new user namespace, deriving the creator from the user in the
30 * passed credentials, and replacing that user with the new root user for the
33 * This is called by copy_creds(), which will finish setting the target task's
36 int create_user_ns(struct cred
*new)
38 struct user_namespace
*ns
, *parent_ns
= new->user_ns
;
39 kuid_t owner
= new->euid
;
40 kgid_t group
= new->egid
;
42 /* The creator needs a mapping in the parent user namespace
43 * or else we won't be able to reasonably tell userspace who
44 * created a user_namespace.
46 if (!kuid_has_mapping(parent_ns
, owner
) ||
47 !kgid_has_mapping(parent_ns
, group
))
50 ns
= kmem_cache_zalloc(user_ns_cachep
, GFP_KERNEL
);
55 ns
->parent
= parent_ns
;
59 /* Start with the same capabilities as init but useless for doing
60 * anything as the capabilities are bound to the new user namespace.
62 new->securebits
= SECUREBITS_DEFAULT
;
63 new->cap_inheritable
= CAP_EMPTY_SET
;
64 new->cap_permitted
= CAP_FULL_SET
;
65 new->cap_effective
= CAP_FULL_SET
;
66 new->cap_bset
= CAP_FULL_SET
;
68 key_put(new->request_key_auth
);
69 new->request_key_auth
= NULL
;
71 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
73 /* Leave the new->user_ns reference with the new user namespace. */
74 /* Leave the reference to our user_ns with the new cred. */
80 void free_user_ns(struct kref
*kref
)
82 struct user_namespace
*parent
, *ns
=
83 container_of(kref
, struct user_namespace
, kref
);
86 kmem_cache_free(user_ns_cachep
, ns
);
89 EXPORT_SYMBOL(free_user_ns
);
91 static u32
map_id_range_down(struct uid_gid_map
*map
, u32 id
, u32 count
)
93 unsigned idx
, extents
;
98 /* Find the matching extent */
99 extents
= map
->nr_extents
;
100 smp_read_barrier_depends();
101 for (idx
= 0; idx
< extents
; idx
++) {
102 first
= map
->extent
[idx
].first
;
103 last
= first
+ map
->extent
[idx
].count
- 1;
104 if (id
>= first
&& id
<= last
&&
105 (id2
>= first
&& id2
<= last
))
108 /* Map the id or note failure */
110 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
117 static u32
map_id_down(struct uid_gid_map
*map
, u32 id
)
119 unsigned idx
, extents
;
122 /* Find the matching extent */
123 extents
= map
->nr_extents
;
124 smp_read_barrier_depends();
125 for (idx
= 0; idx
< extents
; idx
++) {
126 first
= map
->extent
[idx
].first
;
127 last
= first
+ map
->extent
[idx
].count
- 1;
128 if (id
>= first
&& id
<= last
)
131 /* Map the id or note failure */
133 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
140 static u32
map_id_up(struct uid_gid_map
*map
, u32 id
)
142 unsigned idx
, extents
;
145 /* Find the matching extent */
146 extents
= map
->nr_extents
;
147 smp_read_barrier_depends();
148 for (idx
= 0; idx
< extents
; idx
++) {
149 first
= map
->extent
[idx
].lower_first
;
150 last
= first
+ map
->extent
[idx
].count
- 1;
151 if (id
>= first
&& id
<= last
)
154 /* Map the id or note failure */
156 id
= (id
- first
) + map
->extent
[idx
].first
;
164 * make_kuid - Map a user-namespace uid pair into a kuid.
165 * @ns: User namespace that the uid is in
166 * @uid: User identifier
168 * Maps a user-namespace uid pair into a kernel internal kuid,
169 * and returns that kuid.
171 * When there is no mapping defined for the user-namespace uid
172 * pair INVALID_UID is returned. Callers are expected to test
173 * for and handle handle INVALID_UID being returned. INVALID_UID
174 * may be tested for using uid_valid().
176 kuid_t
make_kuid(struct user_namespace
*ns
, uid_t uid
)
178 /* Map the uid to a global kernel uid */
179 return KUIDT_INIT(map_id_down(&ns
->uid_map
, uid
));
181 EXPORT_SYMBOL(make_kuid
);
184 * from_kuid - Create a uid from a kuid user-namespace pair.
185 * @targ: The user namespace we want a uid in.
186 * @kuid: The kernel internal uid to start with.
188 * Map @kuid into the user-namespace specified by @targ and
189 * return the resulting uid.
191 * There is always a mapping into the initial user_namespace.
193 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
195 uid_t
from_kuid(struct user_namespace
*targ
, kuid_t kuid
)
197 /* Map the uid from a global kernel uid */
198 return map_id_up(&targ
->uid_map
, __kuid_val(kuid
));
200 EXPORT_SYMBOL(from_kuid
);
203 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
204 * @targ: The user namespace we want a uid in.
205 * @kuid: The kernel internal uid to start with.
207 * Map @kuid into the user-namespace specified by @targ and
208 * return the resulting uid.
210 * There is always a mapping into the initial user_namespace.
212 * Unlike from_kuid from_kuid_munged never fails and always
213 * returns a valid uid. This makes from_kuid_munged appropriate
214 * for use in syscalls like stat and getuid where failing the
215 * system call and failing to provide a valid uid are not an
218 * If @kuid has no mapping in @targ overflowuid is returned.
220 uid_t
from_kuid_munged(struct user_namespace
*targ
, kuid_t kuid
)
223 uid
= from_kuid(targ
, kuid
);
225 if (uid
== (uid_t
) -1)
229 EXPORT_SYMBOL(from_kuid_munged
);
232 * make_kgid - Map a user-namespace gid pair into a kgid.
233 * @ns: User namespace that the gid is in
234 * @uid: group identifier
236 * Maps a user-namespace gid pair into a kernel internal kgid,
237 * and returns that kgid.
239 * When there is no mapping defined for the user-namespace gid
240 * pair INVALID_GID is returned. Callers are expected to test
241 * for and handle INVALID_GID being returned. INVALID_GID may be
242 * tested for using gid_valid().
244 kgid_t
make_kgid(struct user_namespace
*ns
, gid_t gid
)
246 /* Map the gid to a global kernel gid */
247 return KGIDT_INIT(map_id_down(&ns
->gid_map
, gid
));
249 EXPORT_SYMBOL(make_kgid
);
252 * from_kgid - Create a gid from a kgid user-namespace pair.
253 * @targ: The user namespace we want a gid in.
254 * @kgid: The kernel internal gid to start with.
256 * Map @kgid into the user-namespace specified by @targ and
257 * return the resulting gid.
259 * There is always a mapping into the initial user_namespace.
261 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
263 gid_t
from_kgid(struct user_namespace
*targ
, kgid_t kgid
)
265 /* Map the gid from a global kernel gid */
266 return map_id_up(&targ
->gid_map
, __kgid_val(kgid
));
268 EXPORT_SYMBOL(from_kgid
);
271 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
272 * @targ: The user namespace we want a gid in.
273 * @kgid: The kernel internal gid to start with.
275 * Map @kgid into the user-namespace specified by @targ and
276 * return the resulting gid.
278 * There is always a mapping into the initial user_namespace.
280 * Unlike from_kgid from_kgid_munged never fails and always
281 * returns a valid gid. This makes from_kgid_munged appropriate
282 * for use in syscalls like stat and getgid where failing the
283 * system call and failing to provide a valid gid are not options.
285 * If @kgid has no mapping in @targ overflowgid is returned.
287 gid_t
from_kgid_munged(struct user_namespace
*targ
, kgid_t kgid
)
290 gid
= from_kgid(targ
, kgid
);
292 if (gid
== (gid_t
) -1)
296 EXPORT_SYMBOL(from_kgid_munged
);
298 static int uid_m_show(struct seq_file
*seq
, void *v
)
300 struct user_namespace
*ns
= seq
->private;
301 struct uid_gid_extent
*extent
= v
;
302 struct user_namespace
*lower_ns
;
305 lower_ns
= current_user_ns();
306 if ((lower_ns
== ns
) && lower_ns
->parent
)
307 lower_ns
= lower_ns
->parent
;
309 lower
= from_kuid(lower_ns
, KUIDT_INIT(extent
->lower_first
));
311 seq_printf(seq
, "%10u %10u %10u\n",
319 static int gid_m_show(struct seq_file
*seq
, void *v
)
321 struct user_namespace
*ns
= seq
->private;
322 struct uid_gid_extent
*extent
= v
;
323 struct user_namespace
*lower_ns
;
326 lower_ns
= current_user_ns();
327 if ((lower_ns
== ns
) && lower_ns
->parent
)
328 lower_ns
= lower_ns
->parent
;
330 lower
= from_kgid(lower_ns
, KGIDT_INIT(extent
->lower_first
));
332 seq_printf(seq
, "%10u %10u %10u\n",
340 static void *m_start(struct seq_file
*seq
, loff_t
*ppos
, struct uid_gid_map
*map
)
342 struct uid_gid_extent
*extent
= NULL
;
345 if (pos
< map
->nr_extents
)
346 extent
= &map
->extent
[pos
];
351 static void *uid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
353 struct user_namespace
*ns
= seq
->private;
355 return m_start(seq
, ppos
, &ns
->uid_map
);
358 static void *gid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
360 struct user_namespace
*ns
= seq
->private;
362 return m_start(seq
, ppos
, &ns
->gid_map
);
365 static void *m_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
368 return seq
->op
->start(seq
, pos
);
371 static void m_stop(struct seq_file
*seq
, void *v
)
376 struct seq_operations proc_uid_seq_operations
= {
377 .start
= uid_m_start
,
383 struct seq_operations proc_gid_seq_operations
= {
384 .start
= gid_m_start
,
390 static DEFINE_MUTEX(id_map_mutex
);
392 static ssize_t
map_write(struct file
*file
, const char __user
*buf
,
393 size_t count
, loff_t
*ppos
,
395 struct uid_gid_map
*map
,
396 struct uid_gid_map
*parent_map
)
398 struct seq_file
*seq
= file
->private_data
;
399 struct user_namespace
*ns
= seq
->private;
400 struct uid_gid_map new_map
;
402 struct uid_gid_extent
*extent
, *last
= NULL
;
403 unsigned long page
= 0;
404 char *kbuf
, *pos
, *next_line
;
405 ssize_t ret
= -EINVAL
;
408 * The id_map_mutex serializes all writes to any given map.
410 * Any map is only ever written once.
412 * An id map fits within 1 cache line on most architectures.
414 * On read nothing needs to be done unless you are on an
415 * architecture with a crazy cache coherency model like alpha.
417 * There is a one time data dependency between reading the
418 * count of the extents and the values of the extents. The
419 * desired behavior is to see the values of the extents that
420 * were written before the count of the extents.
422 * To achieve this smp_wmb() is used on guarantee the write
423 * order and smp_read_barrier_depends() is guaranteed that we
424 * don't have crazy architectures returning stale data.
427 mutex_lock(&id_map_mutex
);
430 /* Only allow one successful write to the map */
431 if (map
->nr_extents
!= 0)
434 /* Require the appropriate privilege CAP_SETUID or CAP_SETGID
435 * over the user namespace in order to set the id mapping.
437 if (!ns_capable(ns
, cap_setid
))
442 page
= __get_free_page(GFP_TEMPORARY
);
443 kbuf
= (char *) page
;
447 /* Only allow <= page size writes at the beginning of the file */
449 if ((*ppos
!= 0) || (count
>= PAGE_SIZE
))
452 /* Slurp in the user data */
454 if (copy_from_user(kbuf
, buf
, count
))
458 /* Parse the user data */
461 new_map
.nr_extents
= 0;
462 for (;pos
; pos
= next_line
) {
463 extent
= &new_map
.extent
[new_map
.nr_extents
];
465 /* Find the end of line and ensure I don't look past it */
466 next_line
= strchr(pos
, '\n');
470 if (*next_line
== '\0')
474 pos
= skip_spaces(pos
);
475 extent
->first
= simple_strtoul(pos
, &pos
, 10);
479 pos
= skip_spaces(pos
);
480 extent
->lower_first
= simple_strtoul(pos
, &pos
, 10);
484 pos
= skip_spaces(pos
);
485 extent
->count
= simple_strtoul(pos
, &pos
, 10);
486 if (*pos
&& !isspace(*pos
))
489 /* Verify there is not trailing junk on the line */
490 pos
= skip_spaces(pos
);
494 /* Verify we have been given valid starting values */
495 if ((extent
->first
== (u32
) -1) ||
496 (extent
->lower_first
== (u32
) -1 ))
499 /* Verify count is not zero and does not cause the extent to wrap */
500 if ((extent
->first
+ extent
->count
) <= extent
->first
)
502 if ((extent
->lower_first
+ extent
->count
) <= extent
->lower_first
)
505 /* For now only accept extents that are strictly in order */
507 (((last
->first
+ last
->count
) > extent
->first
) ||
508 ((last
->lower_first
+ last
->count
) > extent
->lower_first
)))
511 new_map
.nr_extents
++;
514 /* Fail if the file contains too many extents */
515 if ((new_map
.nr_extents
== UID_GID_MAP_MAX_EXTENTS
) &&
519 /* Be very certaint the new map actually exists */
520 if (new_map
.nr_extents
== 0)
524 /* Validate the user is allowed to use user id's mapped to. */
525 if (!new_idmap_permitted(ns
, cap_setid
, &new_map
))
528 /* Map the lower ids from the parent user namespace to the
529 * kernel global id space.
531 for (idx
= 0; idx
< new_map
.nr_extents
; idx
++) {
533 extent
= &new_map
.extent
[idx
];
535 lower_first
= map_id_range_down(parent_map
,
539 /* Fail if we can not map the specified extent to
540 * the kernel global id space.
542 if (lower_first
== (u32
) -1)
545 extent
->lower_first
= lower_first
;
548 /* Install the map */
549 memcpy(map
->extent
, new_map
.extent
,
550 new_map
.nr_extents
*sizeof(new_map
.extent
[0]));
552 map
->nr_extents
= new_map
.nr_extents
;
557 mutex_unlock(&id_map_mutex
);
563 ssize_t
proc_uid_map_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*ppos
)
565 struct seq_file
*seq
= file
->private_data
;
566 struct user_namespace
*ns
= seq
->private;
571 return map_write(file
, buf
, size
, ppos
, CAP_SETUID
,
572 &ns
->uid_map
, &ns
->parent
->uid_map
);
575 ssize_t
proc_gid_map_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*ppos
)
577 struct seq_file
*seq
= file
->private_data
;
578 struct user_namespace
*ns
= seq
->private;
583 return map_write(file
, buf
, size
, ppos
, CAP_SETGID
,
584 &ns
->gid_map
, &ns
->parent
->gid_map
);
587 static bool new_idmap_permitted(struct user_namespace
*ns
, int cap_setid
,
588 struct uid_gid_map
*new_map
)
590 /* Allow the specified ids if we have the appropriate capability
591 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
593 if (ns_capable(ns
->parent
, cap_setid
))
599 static __init
int user_namespaces_init(void)
601 user_ns_cachep
= KMEM_CACHE(user_namespace
, SLAB_PANIC
);
604 module_init(user_namespaces_init
);