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>
22 #include <linux/projid.h>
24 static struct kmem_cache
*user_ns_cachep __read_mostly
;
26 static bool new_idmap_permitted(struct user_namespace
*ns
, int cap_setid
,
27 struct uid_gid_map
*map
);
30 * Create a new user namespace, deriving the creator from the user in the
31 * passed credentials, and replacing that user with the new root user for the
34 * This is called by copy_creds(), which will finish setting the target task's
37 int create_user_ns(struct cred
*new)
39 struct user_namespace
*ns
, *parent_ns
= new->user_ns
;
40 kuid_t owner
= new->euid
;
41 kgid_t group
= new->egid
;
43 /* The creator needs a mapping in the parent user namespace
44 * or else we won't be able to reasonably tell userspace who
45 * created a user_namespace.
47 if (!kuid_has_mapping(parent_ns
, owner
) ||
48 !kgid_has_mapping(parent_ns
, group
))
51 ns
= kmem_cache_zalloc(user_ns_cachep
, GFP_KERNEL
);
56 ns
->parent
= parent_ns
;
60 /* Start with the same capabilities as init but useless for doing
61 * anything as the capabilities are bound to the new user namespace.
63 new->securebits
= SECUREBITS_DEFAULT
;
64 new->cap_inheritable
= CAP_EMPTY_SET
;
65 new->cap_permitted
= CAP_FULL_SET
;
66 new->cap_effective
= CAP_FULL_SET
;
67 new->cap_bset
= CAP_FULL_SET
;
69 key_put(new->request_key_auth
);
70 new->request_key_auth
= NULL
;
72 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
74 /* Leave the new->user_ns reference with the new user namespace. */
75 /* Leave the reference to our user_ns with the new cred. */
81 void free_user_ns(struct kref
*kref
)
83 struct user_namespace
*parent
, *ns
=
84 container_of(kref
, struct user_namespace
, kref
);
87 kmem_cache_free(user_ns_cachep
, ns
);
90 EXPORT_SYMBOL(free_user_ns
);
92 static u32
map_id_range_down(struct uid_gid_map
*map
, u32 id
, u32 count
)
94 unsigned idx
, extents
;
99 /* Find the matching extent */
100 extents
= map
->nr_extents
;
101 smp_read_barrier_depends();
102 for (idx
= 0; idx
< extents
; idx
++) {
103 first
= map
->extent
[idx
].first
;
104 last
= first
+ map
->extent
[idx
].count
- 1;
105 if (id
>= first
&& id
<= last
&&
106 (id2
>= first
&& id2
<= last
))
109 /* Map the id or note failure */
111 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
118 static u32
map_id_down(struct uid_gid_map
*map
, u32 id
)
120 unsigned idx
, extents
;
123 /* Find the matching extent */
124 extents
= map
->nr_extents
;
125 smp_read_barrier_depends();
126 for (idx
= 0; idx
< extents
; idx
++) {
127 first
= map
->extent
[idx
].first
;
128 last
= first
+ map
->extent
[idx
].count
- 1;
129 if (id
>= first
&& id
<= last
)
132 /* Map the id or note failure */
134 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
141 static u32
map_id_up(struct uid_gid_map
*map
, u32 id
)
143 unsigned idx
, extents
;
146 /* Find the matching extent */
147 extents
= map
->nr_extents
;
148 smp_read_barrier_depends();
149 for (idx
= 0; idx
< extents
; idx
++) {
150 first
= map
->extent
[idx
].lower_first
;
151 last
= first
+ map
->extent
[idx
].count
- 1;
152 if (id
>= first
&& id
<= last
)
155 /* Map the id or note failure */
157 id
= (id
- first
) + map
->extent
[idx
].first
;
165 * make_kuid - Map a user-namespace uid pair into a kuid.
166 * @ns: User namespace that the uid is in
167 * @uid: User identifier
169 * Maps a user-namespace uid pair into a kernel internal kuid,
170 * and returns that kuid.
172 * When there is no mapping defined for the user-namespace uid
173 * pair INVALID_UID is returned. Callers are expected to test
174 * for and handle handle INVALID_UID being returned. INVALID_UID
175 * may be tested for using uid_valid().
177 kuid_t
make_kuid(struct user_namespace
*ns
, uid_t uid
)
179 /* Map the uid to a global kernel uid */
180 return KUIDT_INIT(map_id_down(&ns
->uid_map
, uid
));
182 EXPORT_SYMBOL(make_kuid
);
185 * from_kuid - Create a uid from a kuid user-namespace pair.
186 * @targ: The user namespace we want a uid in.
187 * @kuid: The kernel internal uid to start with.
189 * Map @kuid into the user-namespace specified by @targ and
190 * return the resulting uid.
192 * There is always a mapping into the initial user_namespace.
194 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
196 uid_t
from_kuid(struct user_namespace
*targ
, kuid_t kuid
)
198 /* Map the uid from a global kernel uid */
199 return map_id_up(&targ
->uid_map
, __kuid_val(kuid
));
201 EXPORT_SYMBOL(from_kuid
);
204 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
205 * @targ: The user namespace we want a uid in.
206 * @kuid: The kernel internal uid to start with.
208 * Map @kuid into the user-namespace specified by @targ and
209 * return the resulting uid.
211 * There is always a mapping into the initial user_namespace.
213 * Unlike from_kuid from_kuid_munged never fails and always
214 * returns a valid uid. This makes from_kuid_munged appropriate
215 * for use in syscalls like stat and getuid where failing the
216 * system call and failing to provide a valid uid are not an
219 * If @kuid has no mapping in @targ overflowuid is returned.
221 uid_t
from_kuid_munged(struct user_namespace
*targ
, kuid_t kuid
)
224 uid
= from_kuid(targ
, kuid
);
226 if (uid
== (uid_t
) -1)
230 EXPORT_SYMBOL(from_kuid_munged
);
233 * make_kgid - Map a user-namespace gid pair into a kgid.
234 * @ns: User namespace that the gid is in
235 * @uid: group identifier
237 * Maps a user-namespace gid pair into a kernel internal kgid,
238 * and returns that kgid.
240 * When there is no mapping defined for the user-namespace gid
241 * pair INVALID_GID is returned. Callers are expected to test
242 * for and handle INVALID_GID being returned. INVALID_GID may be
243 * tested for using gid_valid().
245 kgid_t
make_kgid(struct user_namespace
*ns
, gid_t gid
)
247 /* Map the gid to a global kernel gid */
248 return KGIDT_INIT(map_id_down(&ns
->gid_map
, gid
));
250 EXPORT_SYMBOL(make_kgid
);
253 * from_kgid - Create a gid from a kgid user-namespace pair.
254 * @targ: The user namespace we want a gid in.
255 * @kgid: The kernel internal gid to start with.
257 * Map @kgid into the user-namespace specified by @targ and
258 * return the resulting gid.
260 * There is always a mapping into the initial user_namespace.
262 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
264 gid_t
from_kgid(struct user_namespace
*targ
, kgid_t kgid
)
266 /* Map the gid from a global kernel gid */
267 return map_id_up(&targ
->gid_map
, __kgid_val(kgid
));
269 EXPORT_SYMBOL(from_kgid
);
272 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
273 * @targ: The user namespace we want a gid in.
274 * @kgid: The kernel internal gid to start with.
276 * Map @kgid into the user-namespace specified by @targ and
277 * return the resulting gid.
279 * There is always a mapping into the initial user_namespace.
281 * Unlike from_kgid from_kgid_munged never fails and always
282 * returns a valid gid. This makes from_kgid_munged appropriate
283 * for use in syscalls like stat and getgid where failing the
284 * system call and failing to provide a valid gid are not options.
286 * If @kgid has no mapping in @targ overflowgid is returned.
288 gid_t
from_kgid_munged(struct user_namespace
*targ
, kgid_t kgid
)
291 gid
= from_kgid(targ
, kgid
);
293 if (gid
== (gid_t
) -1)
297 EXPORT_SYMBOL(from_kgid_munged
);
300 * make_kprojid - Map a user-namespace projid pair into a kprojid.
301 * @ns: User namespace that the projid is in
302 * @projid: Project identifier
304 * Maps a user-namespace uid pair into a kernel internal kuid,
305 * and returns that kuid.
307 * When there is no mapping defined for the user-namespace projid
308 * pair INVALID_PROJID is returned. Callers are expected to test
309 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
310 * may be tested for using projid_valid().
312 kprojid_t
make_kprojid(struct user_namespace
*ns
, projid_t projid
)
314 /* Map the uid to a global kernel uid */
315 return KPROJIDT_INIT(map_id_down(&ns
->projid_map
, projid
));
317 EXPORT_SYMBOL(make_kprojid
);
320 * from_kprojid - Create a projid from a kprojid user-namespace pair.
321 * @targ: The user namespace we want a projid in.
322 * @kprojid: The kernel internal project identifier to start with.
324 * Map @kprojid into the user-namespace specified by @targ and
325 * return the resulting projid.
327 * There is always a mapping into the initial user_namespace.
329 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
331 projid_t
from_kprojid(struct user_namespace
*targ
, kprojid_t kprojid
)
333 /* Map the uid from a global kernel uid */
334 return map_id_up(&targ
->projid_map
, __kprojid_val(kprojid
));
336 EXPORT_SYMBOL(from_kprojid
);
339 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
340 * @targ: The user namespace we want a projid in.
341 * @kprojid: The kernel internal projid to start with.
343 * Map @kprojid into the user-namespace specified by @targ and
344 * return the resulting projid.
346 * There is always a mapping into the initial user_namespace.
348 * Unlike from_kprojid from_kprojid_munged never fails and always
349 * returns a valid projid. This makes from_kprojid_munged
350 * appropriate for use in syscalls like stat and where
351 * failing the system call and failing to provide a valid projid are
354 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
356 projid_t
from_kprojid_munged(struct user_namespace
*targ
, kprojid_t kprojid
)
359 projid
= from_kprojid(targ
, kprojid
);
361 if (projid
== (projid_t
) -1)
362 projid
= OVERFLOW_PROJID
;
365 EXPORT_SYMBOL(from_kprojid_munged
);
368 static int uid_m_show(struct seq_file
*seq
, void *v
)
370 struct user_namespace
*ns
= seq
->private;
371 struct uid_gid_extent
*extent
= v
;
372 struct user_namespace
*lower_ns
;
375 lower_ns
= current_user_ns();
376 if ((lower_ns
== ns
) && lower_ns
->parent
)
377 lower_ns
= lower_ns
->parent
;
379 lower
= from_kuid(lower_ns
, KUIDT_INIT(extent
->lower_first
));
381 seq_printf(seq
, "%10u %10u %10u\n",
389 static int gid_m_show(struct seq_file
*seq
, void *v
)
391 struct user_namespace
*ns
= seq
->private;
392 struct uid_gid_extent
*extent
= v
;
393 struct user_namespace
*lower_ns
;
396 lower_ns
= current_user_ns();
397 if ((lower_ns
== ns
) && lower_ns
->parent
)
398 lower_ns
= lower_ns
->parent
;
400 lower
= from_kgid(lower_ns
, KGIDT_INIT(extent
->lower_first
));
402 seq_printf(seq
, "%10u %10u %10u\n",
410 static int projid_m_show(struct seq_file
*seq
, void *v
)
412 struct user_namespace
*ns
= seq
->private;
413 struct uid_gid_extent
*extent
= v
;
414 struct user_namespace
*lower_ns
;
417 lower_ns
= seq_user_ns(seq
);
418 if ((lower_ns
== ns
) && lower_ns
->parent
)
419 lower_ns
= lower_ns
->parent
;
421 lower
= from_kprojid(lower_ns
, KPROJIDT_INIT(extent
->lower_first
));
423 seq_printf(seq
, "%10u %10u %10u\n",
431 static void *m_start(struct seq_file
*seq
, loff_t
*ppos
, struct uid_gid_map
*map
)
433 struct uid_gid_extent
*extent
= NULL
;
436 if (pos
< map
->nr_extents
)
437 extent
= &map
->extent
[pos
];
442 static void *uid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
444 struct user_namespace
*ns
= seq
->private;
446 return m_start(seq
, ppos
, &ns
->uid_map
);
449 static void *gid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
451 struct user_namespace
*ns
= seq
->private;
453 return m_start(seq
, ppos
, &ns
->gid_map
);
456 static void *projid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
458 struct user_namespace
*ns
= seq
->private;
460 return m_start(seq
, ppos
, &ns
->projid_map
);
463 static void *m_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
466 return seq
->op
->start(seq
, pos
);
469 static void m_stop(struct seq_file
*seq
, void *v
)
474 struct seq_operations proc_uid_seq_operations
= {
475 .start
= uid_m_start
,
481 struct seq_operations proc_gid_seq_operations
= {
482 .start
= gid_m_start
,
488 struct seq_operations proc_projid_seq_operations
= {
489 .start
= projid_m_start
,
492 .show
= projid_m_show
,
495 static DEFINE_MUTEX(id_map_mutex
);
497 static ssize_t
map_write(struct file
*file
, const char __user
*buf
,
498 size_t count
, loff_t
*ppos
,
500 struct uid_gid_map
*map
,
501 struct uid_gid_map
*parent_map
)
503 struct seq_file
*seq
= file
->private_data
;
504 struct user_namespace
*ns
= seq
->private;
505 struct uid_gid_map new_map
;
507 struct uid_gid_extent
*extent
, *last
= NULL
;
508 unsigned long page
= 0;
509 char *kbuf
, *pos
, *next_line
;
510 ssize_t ret
= -EINVAL
;
513 * The id_map_mutex serializes all writes to any given map.
515 * Any map is only ever written once.
517 * An id map fits within 1 cache line on most architectures.
519 * On read nothing needs to be done unless you are on an
520 * architecture with a crazy cache coherency model like alpha.
522 * There is a one time data dependency between reading the
523 * count of the extents and the values of the extents. The
524 * desired behavior is to see the values of the extents that
525 * were written before the count of the extents.
527 * To achieve this smp_wmb() is used on guarantee the write
528 * order and smp_read_barrier_depends() is guaranteed that we
529 * don't have crazy architectures returning stale data.
532 mutex_lock(&id_map_mutex
);
535 /* Only allow one successful write to the map */
536 if (map
->nr_extents
!= 0)
539 /* Require the appropriate privilege CAP_SETUID or CAP_SETGID
540 * over the user namespace in order to set the id mapping.
542 if (cap_valid(cap_setid
) && !ns_capable(ns
, cap_setid
))
547 page
= __get_free_page(GFP_TEMPORARY
);
548 kbuf
= (char *) page
;
552 /* Only allow <= page size writes at the beginning of the file */
554 if ((*ppos
!= 0) || (count
>= PAGE_SIZE
))
557 /* Slurp in the user data */
559 if (copy_from_user(kbuf
, buf
, count
))
563 /* Parse the user data */
566 new_map
.nr_extents
= 0;
567 for (;pos
; pos
= next_line
) {
568 extent
= &new_map
.extent
[new_map
.nr_extents
];
570 /* Find the end of line and ensure I don't look past it */
571 next_line
= strchr(pos
, '\n');
575 if (*next_line
== '\0')
579 pos
= skip_spaces(pos
);
580 extent
->first
= simple_strtoul(pos
, &pos
, 10);
584 pos
= skip_spaces(pos
);
585 extent
->lower_first
= simple_strtoul(pos
, &pos
, 10);
589 pos
= skip_spaces(pos
);
590 extent
->count
= simple_strtoul(pos
, &pos
, 10);
591 if (*pos
&& !isspace(*pos
))
594 /* Verify there is not trailing junk on the line */
595 pos
= skip_spaces(pos
);
599 /* Verify we have been given valid starting values */
600 if ((extent
->first
== (u32
) -1) ||
601 (extent
->lower_first
== (u32
) -1 ))
604 /* Verify count is not zero and does not cause the extent to wrap */
605 if ((extent
->first
+ extent
->count
) <= extent
->first
)
607 if ((extent
->lower_first
+ extent
->count
) <= extent
->lower_first
)
610 /* For now only accept extents that are strictly in order */
612 (((last
->first
+ last
->count
) > extent
->first
) ||
613 ((last
->lower_first
+ last
->count
) > extent
->lower_first
)))
616 new_map
.nr_extents
++;
619 /* Fail if the file contains too many extents */
620 if ((new_map
.nr_extents
== UID_GID_MAP_MAX_EXTENTS
) &&
624 /* Be very certaint the new map actually exists */
625 if (new_map
.nr_extents
== 0)
629 /* Validate the user is allowed to use user id's mapped to. */
630 if (!new_idmap_permitted(ns
, cap_setid
, &new_map
))
633 /* Map the lower ids from the parent user namespace to the
634 * kernel global id space.
636 for (idx
= 0; idx
< new_map
.nr_extents
; idx
++) {
638 extent
= &new_map
.extent
[idx
];
640 lower_first
= map_id_range_down(parent_map
,
644 /* Fail if we can not map the specified extent to
645 * the kernel global id space.
647 if (lower_first
== (u32
) -1)
650 extent
->lower_first
= lower_first
;
653 /* Install the map */
654 memcpy(map
->extent
, new_map
.extent
,
655 new_map
.nr_extents
*sizeof(new_map
.extent
[0]));
657 map
->nr_extents
= new_map
.nr_extents
;
662 mutex_unlock(&id_map_mutex
);
668 ssize_t
proc_uid_map_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*ppos
)
670 struct seq_file
*seq
= file
->private_data
;
671 struct user_namespace
*ns
= seq
->private;
676 return map_write(file
, buf
, size
, ppos
, CAP_SETUID
,
677 &ns
->uid_map
, &ns
->parent
->uid_map
);
680 ssize_t
proc_gid_map_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*ppos
)
682 struct seq_file
*seq
= file
->private_data
;
683 struct user_namespace
*ns
= seq
->private;
688 return map_write(file
, buf
, size
, ppos
, CAP_SETGID
,
689 &ns
->gid_map
, &ns
->parent
->gid_map
);
692 ssize_t
proc_projid_map_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*ppos
)
694 struct seq_file
*seq
= file
->private_data
;
695 struct user_namespace
*ns
= seq
->private;
696 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
701 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
704 /* Anyone can set any valid project id no capability needed */
705 return map_write(file
, buf
, size
, ppos
, -1,
706 &ns
->projid_map
, &ns
->parent
->projid_map
);
709 static bool new_idmap_permitted(struct user_namespace
*ns
, int cap_setid
,
710 struct uid_gid_map
*new_map
)
712 /* Allow anyone to set a mapping that doesn't require privilege */
713 if (!cap_valid(cap_setid
))
716 /* Allow the specified ids if we have the appropriate capability
717 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
719 if (ns_capable(ns
->parent
, cap_setid
))
725 static __init
int user_namespaces_init(void)
727 user_ns_cachep
= KMEM_CACHE(user_namespace
, SLAB_PANIC
);
730 module_init(user_namespaces_init
);