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/proc_fs.h>
13 #include <linux/highuid.h>
14 #include <linux/cred.h>
15 #include <linux/securebits.h>
16 #include <linux/keyctl.h>
17 #include <linux/key-type.h>
18 #include <keys/user-type.h>
19 #include <linux/seq_file.h>
21 #include <linux/uaccess.h>
22 #include <linux/ctype.h>
23 #include <linux/projid.h>
25 static struct kmem_cache
*user_ns_cachep __read_mostly
;
27 static bool new_idmap_permitted(struct user_namespace
*ns
, int cap_setid
,
28 struct uid_gid_map
*map
);
30 static void set_cred_user_ns(struct cred
*cred
, struct user_namespace
*user_ns
)
32 /* Start with the same capabilities as init but useless for doing
33 * anything as the capabilities are bound to the new user namespace.
35 cred
->securebits
= SECUREBITS_DEFAULT
;
36 cred
->cap_inheritable
= CAP_EMPTY_SET
;
37 cred
->cap_permitted
= CAP_FULL_SET
;
38 cred
->cap_effective
= CAP_FULL_SET
;
39 cred
->cap_bset
= CAP_FULL_SET
;
41 key_put(cred
->request_key_auth
);
42 cred
->request_key_auth
= NULL
;
44 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
45 cred
->user_ns
= user_ns
;
49 * Create a new user namespace, deriving the creator from the user in the
50 * passed credentials, and replacing that user with the new root user for the
53 * This is called by copy_creds(), which will finish setting the target task's
56 int create_user_ns(struct cred
*new)
58 struct user_namespace
*ns
, *parent_ns
= new->user_ns
;
59 kuid_t owner
= new->euid
;
60 kgid_t group
= new->egid
;
62 /* The creator needs a mapping in the parent user namespace
63 * or else we won't be able to reasonably tell userspace who
64 * created a user_namespace.
66 if (!kuid_has_mapping(parent_ns
, owner
) ||
67 !kgid_has_mapping(parent_ns
, group
))
70 ns
= kmem_cache_zalloc(user_ns_cachep
, GFP_KERNEL
);
75 /* Leave the new->user_ns reference with the new user namespace. */
76 ns
->parent
= parent_ns
;
80 set_cred_user_ns(new, ns
);
85 int unshare_userns(unsigned long unshare_flags
, struct cred
**new_cred
)
89 if (!(unshare_flags
& CLONE_NEWUSER
))
92 cred
= prepare_creds();
97 return create_user_ns(cred
);
100 void free_user_ns(struct kref
*kref
)
102 struct user_namespace
*parent
, *ns
=
103 container_of(kref
, struct user_namespace
, kref
);
106 kmem_cache_free(user_ns_cachep
, ns
);
109 EXPORT_SYMBOL(free_user_ns
);
111 static u32
map_id_range_down(struct uid_gid_map
*map
, u32 id
, u32 count
)
113 unsigned idx
, extents
;
114 u32 first
, last
, id2
;
116 id2
= id
+ count
- 1;
118 /* Find the matching extent */
119 extents
= map
->nr_extents
;
120 smp_read_barrier_depends();
121 for (idx
= 0; idx
< extents
; idx
++) {
122 first
= map
->extent
[idx
].first
;
123 last
= first
+ map
->extent
[idx
].count
- 1;
124 if (id
>= first
&& id
<= last
&&
125 (id2
>= first
&& id2
<= last
))
128 /* Map the id or note failure */
130 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
137 static u32
map_id_down(struct uid_gid_map
*map
, u32 id
)
139 unsigned idx
, extents
;
142 /* Find the matching extent */
143 extents
= map
->nr_extents
;
144 smp_read_barrier_depends();
145 for (idx
= 0; idx
< extents
; idx
++) {
146 first
= map
->extent
[idx
].first
;
147 last
= first
+ map
->extent
[idx
].count
- 1;
148 if (id
>= first
&& id
<= last
)
151 /* Map the id or note failure */
153 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
160 static u32
map_id_up(struct uid_gid_map
*map
, u32 id
)
162 unsigned idx
, extents
;
165 /* Find the matching extent */
166 extents
= map
->nr_extents
;
167 smp_read_barrier_depends();
168 for (idx
= 0; idx
< extents
; idx
++) {
169 first
= map
->extent
[idx
].lower_first
;
170 last
= first
+ map
->extent
[idx
].count
- 1;
171 if (id
>= first
&& id
<= last
)
174 /* Map the id or note failure */
176 id
= (id
- first
) + map
->extent
[idx
].first
;
184 * make_kuid - Map a user-namespace uid pair into a kuid.
185 * @ns: User namespace that the uid is in
186 * @uid: User identifier
188 * Maps a user-namespace uid pair into a kernel internal kuid,
189 * and returns that kuid.
191 * When there is no mapping defined for the user-namespace uid
192 * pair INVALID_UID is returned. Callers are expected to test
193 * for and handle handle INVALID_UID being returned. INVALID_UID
194 * may be tested for using uid_valid().
196 kuid_t
make_kuid(struct user_namespace
*ns
, uid_t uid
)
198 /* Map the uid to a global kernel uid */
199 return KUIDT_INIT(map_id_down(&ns
->uid_map
, uid
));
201 EXPORT_SYMBOL(make_kuid
);
204 * from_kuid - 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 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
215 uid_t
from_kuid(struct user_namespace
*targ
, kuid_t kuid
)
217 /* Map the uid from a global kernel uid */
218 return map_id_up(&targ
->uid_map
, __kuid_val(kuid
));
220 EXPORT_SYMBOL(from_kuid
);
223 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
224 * @targ: The user namespace we want a uid in.
225 * @kuid: The kernel internal uid to start with.
227 * Map @kuid into the user-namespace specified by @targ and
228 * return the resulting uid.
230 * There is always a mapping into the initial user_namespace.
232 * Unlike from_kuid from_kuid_munged never fails and always
233 * returns a valid uid. This makes from_kuid_munged appropriate
234 * for use in syscalls like stat and getuid where failing the
235 * system call and failing to provide a valid uid are not an
238 * If @kuid has no mapping in @targ overflowuid is returned.
240 uid_t
from_kuid_munged(struct user_namespace
*targ
, kuid_t kuid
)
243 uid
= from_kuid(targ
, kuid
);
245 if (uid
== (uid_t
) -1)
249 EXPORT_SYMBOL(from_kuid_munged
);
252 * make_kgid - Map a user-namespace gid pair into a kgid.
253 * @ns: User namespace that the gid is in
254 * @uid: group identifier
256 * Maps a user-namespace gid pair into a kernel internal kgid,
257 * and returns that kgid.
259 * When there is no mapping defined for the user-namespace gid
260 * pair INVALID_GID is returned. Callers are expected to test
261 * for and handle INVALID_GID being returned. INVALID_GID may be
262 * tested for using gid_valid().
264 kgid_t
make_kgid(struct user_namespace
*ns
, gid_t gid
)
266 /* Map the gid to a global kernel gid */
267 return KGIDT_INIT(map_id_down(&ns
->gid_map
, gid
));
269 EXPORT_SYMBOL(make_kgid
);
272 * from_kgid - 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 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
283 gid_t
from_kgid(struct user_namespace
*targ
, kgid_t kgid
)
285 /* Map the gid from a global kernel gid */
286 return map_id_up(&targ
->gid_map
, __kgid_val(kgid
));
288 EXPORT_SYMBOL(from_kgid
);
291 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
292 * @targ: The user namespace we want a gid in.
293 * @kgid: The kernel internal gid to start with.
295 * Map @kgid into the user-namespace specified by @targ and
296 * return the resulting gid.
298 * There is always a mapping into the initial user_namespace.
300 * Unlike from_kgid from_kgid_munged never fails and always
301 * returns a valid gid. This makes from_kgid_munged appropriate
302 * for use in syscalls like stat and getgid where failing the
303 * system call and failing to provide a valid gid are not options.
305 * If @kgid has no mapping in @targ overflowgid is returned.
307 gid_t
from_kgid_munged(struct user_namespace
*targ
, kgid_t kgid
)
310 gid
= from_kgid(targ
, kgid
);
312 if (gid
== (gid_t
) -1)
316 EXPORT_SYMBOL(from_kgid_munged
);
319 * make_kprojid - Map a user-namespace projid pair into a kprojid.
320 * @ns: User namespace that the projid is in
321 * @projid: Project identifier
323 * Maps a user-namespace uid pair into a kernel internal kuid,
324 * and returns that kuid.
326 * When there is no mapping defined for the user-namespace projid
327 * pair INVALID_PROJID is returned. Callers are expected to test
328 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
329 * may be tested for using projid_valid().
331 kprojid_t
make_kprojid(struct user_namespace
*ns
, projid_t projid
)
333 /* Map the uid to a global kernel uid */
334 return KPROJIDT_INIT(map_id_down(&ns
->projid_map
, projid
));
336 EXPORT_SYMBOL(make_kprojid
);
339 * from_kprojid - Create a projid from a kprojid user-namespace pair.
340 * @targ: The user namespace we want a projid in.
341 * @kprojid: The kernel internal project identifier 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 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
350 projid_t
from_kprojid(struct user_namespace
*targ
, kprojid_t kprojid
)
352 /* Map the uid from a global kernel uid */
353 return map_id_up(&targ
->projid_map
, __kprojid_val(kprojid
));
355 EXPORT_SYMBOL(from_kprojid
);
358 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
359 * @targ: The user namespace we want a projid in.
360 * @kprojid: The kernel internal projid to start with.
362 * Map @kprojid into the user-namespace specified by @targ and
363 * return the resulting projid.
365 * There is always a mapping into the initial user_namespace.
367 * Unlike from_kprojid from_kprojid_munged never fails and always
368 * returns a valid projid. This makes from_kprojid_munged
369 * appropriate for use in syscalls like stat and where
370 * failing the system call and failing to provide a valid projid are
373 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
375 projid_t
from_kprojid_munged(struct user_namespace
*targ
, kprojid_t kprojid
)
378 projid
= from_kprojid(targ
, kprojid
);
380 if (projid
== (projid_t
) -1)
381 projid
= OVERFLOW_PROJID
;
384 EXPORT_SYMBOL(from_kprojid_munged
);
387 static int uid_m_show(struct seq_file
*seq
, void *v
)
389 struct user_namespace
*ns
= seq
->private;
390 struct uid_gid_extent
*extent
= v
;
391 struct user_namespace
*lower_ns
;
394 lower_ns
= current_user_ns();
395 if ((lower_ns
== ns
) && lower_ns
->parent
)
396 lower_ns
= lower_ns
->parent
;
398 lower
= from_kuid(lower_ns
, KUIDT_INIT(extent
->lower_first
));
400 seq_printf(seq
, "%10u %10u %10u\n",
408 static int gid_m_show(struct seq_file
*seq
, void *v
)
410 struct user_namespace
*ns
= seq
->private;
411 struct uid_gid_extent
*extent
= v
;
412 struct user_namespace
*lower_ns
;
415 lower_ns
= current_user_ns();
416 if ((lower_ns
== ns
) && lower_ns
->parent
)
417 lower_ns
= lower_ns
->parent
;
419 lower
= from_kgid(lower_ns
, KGIDT_INIT(extent
->lower_first
));
421 seq_printf(seq
, "%10u %10u %10u\n",
429 static int projid_m_show(struct seq_file
*seq
, void *v
)
431 struct user_namespace
*ns
= seq
->private;
432 struct uid_gid_extent
*extent
= v
;
433 struct user_namespace
*lower_ns
;
436 lower_ns
= seq_user_ns(seq
);
437 if ((lower_ns
== ns
) && lower_ns
->parent
)
438 lower_ns
= lower_ns
->parent
;
440 lower
= from_kprojid(lower_ns
, KPROJIDT_INIT(extent
->lower_first
));
442 seq_printf(seq
, "%10u %10u %10u\n",
450 static void *m_start(struct seq_file
*seq
, loff_t
*ppos
, struct uid_gid_map
*map
)
452 struct uid_gid_extent
*extent
= NULL
;
455 if (pos
< map
->nr_extents
)
456 extent
= &map
->extent
[pos
];
461 static void *uid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
463 struct user_namespace
*ns
= seq
->private;
465 return m_start(seq
, ppos
, &ns
->uid_map
);
468 static void *gid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
470 struct user_namespace
*ns
= seq
->private;
472 return m_start(seq
, ppos
, &ns
->gid_map
);
475 static void *projid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
477 struct user_namespace
*ns
= seq
->private;
479 return m_start(seq
, ppos
, &ns
->projid_map
);
482 static void *m_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
485 return seq
->op
->start(seq
, pos
);
488 static void m_stop(struct seq_file
*seq
, void *v
)
493 struct seq_operations proc_uid_seq_operations
= {
494 .start
= uid_m_start
,
500 struct seq_operations proc_gid_seq_operations
= {
501 .start
= gid_m_start
,
507 struct seq_operations proc_projid_seq_operations
= {
508 .start
= projid_m_start
,
511 .show
= projid_m_show
,
514 static DEFINE_MUTEX(id_map_mutex
);
516 static ssize_t
map_write(struct file
*file
, const char __user
*buf
,
517 size_t count
, loff_t
*ppos
,
519 struct uid_gid_map
*map
,
520 struct uid_gid_map
*parent_map
)
522 struct seq_file
*seq
= file
->private_data
;
523 struct user_namespace
*ns
= seq
->private;
524 struct uid_gid_map new_map
;
526 struct uid_gid_extent
*extent
, *last
= NULL
;
527 unsigned long page
= 0;
528 char *kbuf
, *pos
, *next_line
;
529 ssize_t ret
= -EINVAL
;
532 * The id_map_mutex serializes all writes to any given map.
534 * Any map is only ever written once.
536 * An id map fits within 1 cache line on most architectures.
538 * On read nothing needs to be done unless you are on an
539 * architecture with a crazy cache coherency model like alpha.
541 * There is a one time data dependency between reading the
542 * count of the extents and the values of the extents. The
543 * desired behavior is to see the values of the extents that
544 * were written before the count of the extents.
546 * To achieve this smp_wmb() is used on guarantee the write
547 * order and smp_read_barrier_depends() is guaranteed that we
548 * don't have crazy architectures returning stale data.
551 mutex_lock(&id_map_mutex
);
554 /* Only allow one successful write to the map */
555 if (map
->nr_extents
!= 0)
558 /* Require the appropriate privilege CAP_SETUID or CAP_SETGID
559 * over the user namespace in order to set the id mapping.
561 if (cap_valid(cap_setid
) && !ns_capable(ns
, cap_setid
))
566 page
= __get_free_page(GFP_TEMPORARY
);
567 kbuf
= (char *) page
;
571 /* Only allow <= page size writes at the beginning of the file */
573 if ((*ppos
!= 0) || (count
>= PAGE_SIZE
))
576 /* Slurp in the user data */
578 if (copy_from_user(kbuf
, buf
, count
))
582 /* Parse the user data */
585 new_map
.nr_extents
= 0;
586 for (;pos
; pos
= next_line
) {
587 extent
= &new_map
.extent
[new_map
.nr_extents
];
589 /* Find the end of line and ensure I don't look past it */
590 next_line
= strchr(pos
, '\n');
594 if (*next_line
== '\0')
598 pos
= skip_spaces(pos
);
599 extent
->first
= simple_strtoul(pos
, &pos
, 10);
603 pos
= skip_spaces(pos
);
604 extent
->lower_first
= simple_strtoul(pos
, &pos
, 10);
608 pos
= skip_spaces(pos
);
609 extent
->count
= simple_strtoul(pos
, &pos
, 10);
610 if (*pos
&& !isspace(*pos
))
613 /* Verify there is not trailing junk on the line */
614 pos
= skip_spaces(pos
);
618 /* Verify we have been given valid starting values */
619 if ((extent
->first
== (u32
) -1) ||
620 (extent
->lower_first
== (u32
) -1 ))
623 /* Verify count is not zero and does not cause the extent to wrap */
624 if ((extent
->first
+ extent
->count
) <= extent
->first
)
626 if ((extent
->lower_first
+ extent
->count
) <= extent
->lower_first
)
629 /* For now only accept extents that are strictly in order */
631 (((last
->first
+ last
->count
) > extent
->first
) ||
632 ((last
->lower_first
+ last
->count
) > extent
->lower_first
)))
635 new_map
.nr_extents
++;
638 /* Fail if the file contains too many extents */
639 if ((new_map
.nr_extents
== UID_GID_MAP_MAX_EXTENTS
) &&
643 /* Be very certaint the new map actually exists */
644 if (new_map
.nr_extents
== 0)
648 /* Validate the user is allowed to use user id's mapped to. */
649 if (!new_idmap_permitted(ns
, cap_setid
, &new_map
))
652 /* Map the lower ids from the parent user namespace to the
653 * kernel global id space.
655 for (idx
= 0; idx
< new_map
.nr_extents
; idx
++) {
657 extent
= &new_map
.extent
[idx
];
659 lower_first
= map_id_range_down(parent_map
,
663 /* Fail if we can not map the specified extent to
664 * the kernel global id space.
666 if (lower_first
== (u32
) -1)
669 extent
->lower_first
= lower_first
;
672 /* Install the map */
673 memcpy(map
->extent
, new_map
.extent
,
674 new_map
.nr_extents
*sizeof(new_map
.extent
[0]));
676 map
->nr_extents
= new_map
.nr_extents
;
681 mutex_unlock(&id_map_mutex
);
687 ssize_t
proc_uid_map_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*ppos
)
689 struct seq_file
*seq
= file
->private_data
;
690 struct user_namespace
*ns
= seq
->private;
695 return map_write(file
, buf
, size
, ppos
, CAP_SETUID
,
696 &ns
->uid_map
, &ns
->parent
->uid_map
);
699 ssize_t
proc_gid_map_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*ppos
)
701 struct seq_file
*seq
= file
->private_data
;
702 struct user_namespace
*ns
= seq
->private;
707 return map_write(file
, buf
, size
, ppos
, CAP_SETGID
,
708 &ns
->gid_map
, &ns
->parent
->gid_map
);
711 ssize_t
proc_projid_map_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*ppos
)
713 struct seq_file
*seq
= file
->private_data
;
714 struct user_namespace
*ns
= seq
->private;
715 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
720 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
723 /* Anyone can set any valid project id no capability needed */
724 return map_write(file
, buf
, size
, ppos
, -1,
725 &ns
->projid_map
, &ns
->parent
->projid_map
);
728 static bool new_idmap_permitted(struct user_namespace
*ns
, int cap_setid
,
729 struct uid_gid_map
*new_map
)
731 /* Allow mapping to your own filesystem ids */
732 if ((new_map
->nr_extents
== 1) && (new_map
->extent
[0].count
== 1)) {
733 u32 id
= new_map
->extent
[0].lower_first
;
734 if (cap_setid
== CAP_SETUID
) {
735 kuid_t uid
= make_kuid(ns
->parent
, id
);
736 if (uid_eq(uid
, current_fsuid()))
739 else if (cap_setid
== CAP_SETGID
) {
740 kgid_t gid
= make_kgid(ns
->parent
, id
);
741 if (gid_eq(gid
, current_fsgid()))
746 /* Allow anyone to set a mapping that doesn't require privilege */
747 if (!cap_valid(cap_setid
))
750 /* Allow the specified ids if we have the appropriate capability
751 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
753 if (ns_capable(ns
->parent
, cap_setid
))
759 static void *userns_get(struct task_struct
*task
)
761 struct user_namespace
*user_ns
;
764 user_ns
= get_user_ns(__task_cred(task
)->user_ns
);
770 static void userns_put(void *ns
)
775 static int userns_install(struct nsproxy
*nsproxy
, void *ns
)
777 struct user_namespace
*user_ns
= ns
;
780 /* Don't allow gaining capabilities by reentering
781 * the same user namespace.
783 if (user_ns
== current_user_ns())
786 /* Threaded many not enter a different user namespace */
787 if (atomic_read(¤t
->mm
->mm_users
) > 1)
790 if (!ns_capable(user_ns
, CAP_SYS_ADMIN
))
793 cred
= prepare_creds();
797 put_user_ns(cred
->user_ns
);
798 set_cred_user_ns(cred
, get_user_ns(user_ns
));
800 return commit_creds(cred
);
803 const struct proc_ns_operations userns_operations
= {
805 .type
= CLONE_NEWUSER
,
808 .install
= userns_install
,
811 static __init
int user_namespaces_init(void)
813 user_ns_cachep
= KMEM_CACHE(user_namespace
, SLAB_PANIC
);
816 module_init(user_namespaces_init
);