]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/user_namespaces.7
execve.2, ioctl_console.2, ioctl_iflags.2, ioctl_ns.2, ioctl_userfaultfd.2, kcmp...
[thirdparty/man-pages.git] / man7 / user_namespaces.7
1 .\" Copyright (c) 2013, 2014 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (c) 2012, 2014 by Eric W. Biederman <ebiederm@xmission.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\"
27 .TH USER_NAMESPACES 7 2017-05-03 "Linux" "Linux Programmer's Manual"
28 .SH NAME
29 user_namespaces \- overview of Linux user namespaces
30 .SH DESCRIPTION
31 For an overview of namespaces, see
32 .BR namespaces (7).
33 .PP
34 User namespaces isolate security-related identifiers and attributes,
35 in particular,
36 user IDs and group IDs (see
37 .BR credentials (7)),
38 the root directory,
39 keys (see
40 .BR keyrings (7)),
41 .\" FIXME: This page says very little about the interaction
42 .\" of user namespaces and keys. Add something on this topic.
43 and capabilities (see
44 .BR capabilities (7)).
45 A process's user and group IDs can be different
46 inside and outside a user namespace.
47 In particular,
48 a process can have a normal unprivileged user ID outside a user namespace
49 while at the same time having a user ID of 0 inside the namespace;
50 in other words,
51 the process has full privileges for operations inside the user namespace,
52 but is unprivileged for operations outside the namespace.
53 .\"
54 .\" ============================================================
55 .\"
56 .SS Nested namespaces, namespace membership
57 User namespaces can be nested;
58 that is, each user namespace\(emexcept the initial ("root")
59 namespace\(emhas a parent user namespace,
60 and can have zero or more child user namespaces.
61 The parent user namespace is the user namespace
62 of the process that creates the user namespace via a call to
63 .BR unshare (2)
64 or
65 .BR clone (2)
66 with the
67 .BR CLONE_NEWUSER
68 flag.
69 .PP
70 The kernel imposes (since version 3.11) a limit of 32 nested levels of
71 .\" commit 8742f229b635bf1c1c84a3dfe5e47c814c20b5c8
72 user namespaces.
73 .\" FIXME Explain the rationale for this limit. (What is the rationale?)
74 Calls to
75 .BR unshare (2)
76 or
77 .BR clone (2)
78 that would cause this limit to be exceeded fail with the error
79 .BR EUSERS .
80 .PP
81 Each process is a member of exactly one user namespace.
82 A process created via
83 .BR fork (2)
84 or
85 .BR clone (2)
86 without the
87 .BR CLONE_NEWUSER
88 flag is a member of the same user namespace as its parent.
89 A single-threaded process can join another user namespace with
90 .BR setns (2)
91 if it has the
92 .BR CAP_SYS_ADMIN
93 in that namespace;
94 upon doing so, it gains a full set of capabilities in that namespace.
95 .PP
96 A call to
97 .BR clone (2)
98 or
99 .BR unshare (2)
100 with the
101 .BR CLONE_NEWUSER
102 flag makes the new child process (for
103 .BR clone (2))
104 or the caller (for
105 .BR unshare (2))
106 a member of the new user namespace created by the call.
107 .PP
108 The
109 .BR NS_GET_PARENT
110 .BR ioctl (2)
111 operation can be used to discover the parental relationship
112 between user namespaces; see
113 .BR ioctl_ns (2).
114 .\"
115 .\" ============================================================
116 .\"
117 .SS Capabilities
118 The child process created by
119 .BR clone (2)
120 with the
121 .BR CLONE_NEWUSER
122 flag starts out with a complete set
123 of capabilities in the new user namespace.
124 Likewise, a process that creates a new user namespace using
125 .BR unshare (2)
126 or joins an existing user namespace using
127 .BR setns (2)
128 gains a full set of capabilities in that namespace.
129 On the other hand,
130 that process has no capabilities in the parent (in the case of
131 .BR clone (2))
132 or previous (in the case of
133 .BR unshare (2)
134 and
135 .BR setns (2))
136 user namespace,
137 even if the new namespace is created or joined by the root user
138 (i.e., a process with user ID 0 in the root namespace).
139 .PP
140 Note that a call to
141 .BR execve (2)
142 will cause a process's capabilities to be recalculated in the usual way (see
143 .BR capabilities (7)).
144 Consequently,
145 unless the process has a user ID of 0 within the namespace,
146 or the executable file has a nonempty inheritable capabilities mask,
147 the process will lose all capabilities.
148 See the discussion of user and group ID mappings, below.
149 .PP
150 A call to
151 .BR clone (2),
152 .BR unshare (2),
153 or
154 .BR setns (2)
155 using the
156 .BR CLONE_NEWUSER
157 flag sets the "securebits" flags
158 (see
159 .BR capabilities (7))
160 to their default values (all flags disabled) in the child (for
161 .BR clone (2))
162 or caller (for
163 .BR unshare (2),
164 or
165 .BR setns (2)).
166 Note that because the caller no longer has capabilities
167 in its original user namespace after a call to
168 .BR setns (2),
169 it is not possible for a process to reset its "securebits" flags while
170 retaining its user namespace membership by using a pair of
171 .BR setns (2)
172 calls to move to another user namespace and then return to
173 its original user namespace.
174 .PP
175 The rules for determining whether or not a process has a capability
176 in a particular user namespace are as follows:
177 .IP 1. 3
178 A process has a capability inside a user namespace
179 if it is a member of that namespace and
180 it has the capability in its effective capability set.
181 A process can gain capabilities in its effective capability
182 set in various ways.
183 For example, it may execute a set-user-ID program or an
184 executable with associated file capabilities.
185 In addition,
186 a process may gain capabilities via the effect of
187 .BR clone (2),
188 .BR unshare (2),
189 or
190 .BR setns (2),
191 as already described.
192 .\" In the 3.8 sources, see security/commoncap.c::cap_capable():
193 .IP 2.
194 If a process has a capability in a user namespace,
195 then it has that capability in all child (and further removed descendant)
196 namespaces as well.
197 .IP 3.
198 .\" * The owner of the user namespace in the parent of the
199 .\" * user namespace has all caps.
200 When a user namespace is created, the kernel records the effective
201 user ID of the creating process as being the "owner" of the namespace.
202 .\" (and likewise associates the effective group ID of the creating process
203 .\" with the namespace).
204 A process that resides
205 in the parent of the user namespace
206 .\" See kernel commit 520d9eabce18edfef76a60b7b839d54facafe1f9 for a fix
207 .\" on this point
208 and whose effective user ID matches the owner of the namespace
209 has all capabilities in the namespace.
210 .\" This includes the case where the process executes a set-user-ID
211 .\" program that confers the effective UID of the creator of the namespace.
212 By virtue of the previous rule,
213 this means that the process has all capabilities in all
214 further removed descendant user namespaces as well.
215 .\"
216 .\" ============================================================
217 .\"
218 .SS Effect of capabilities within a user namespace
219 Having a capability inside a user namespace
220 permits a process to perform operations (that require privilege)
221 only on resources governed by that namespace.
222 In other words, having a capability in a user namespace permits a process
223 to perform privileged operations on resources that are governed by (nonuser)
224 namespaces associated with the user namespace (see the next subsection).
225 .PP
226 On the other hand, there are many privileged operations that affect
227 resources that are not associated with any namespace type,
228 for example, changing the system time (governed by
229 .BR CAP_SYS_TIME ),
230 loading a kernel module (governed by
231 .BR CAP_SYS_MODULE ),
232 and creating a device (governed by
233 .BR CAP_MKNOD ).
234 Only a process with privileges in the
235 .I initial
236 user namespace can perform such operations.
237 .PP
238 Holding
239 .B CAP_SYS_ADMIN
240 within the user namespace associated with a process's mount namespace
241 allows that process to create bind mounts
242 and mount the following types of filesystems:
243 .\" fs_flags = FS_USERNS_MOUNT in kernel sources
244 .PP
245 .RS 4
246 .PD 0
247 .IP * 2
248 .IR /proc
249 (since Linux 3.8)
250 .IP *
251 .IR /sys
252 (since Linux 3.8)
253 .IP *
254 .IR devpts
255 (since Linux 3.9)
256 .IP *
257 .BR tmpfs (5)
258 (since Linux 3.9)
259 .IP *
260 .IR ramfs
261 (since Linux 3.9)
262 .IP *
263 .IR mqueue
264 (since Linux 3.9)
265 .IP *
266 .IR bpf
267 .\" commit b2197755b2633e164a439682fb05a9b5ea48f706
268 (since Linux 4.4)
269 .PD
270 .RE
271 .PP
272 Holding
273 .B CAP_SYS_ADMIN
274 within the user namespace associated with a process's cgroup namespace
275 allows (since Linux 4.6)
276 that process to the mount cgroup version 2 filesystem and
277 cgroup version 1 named hierarchies
278 (i.e., cgroup filesystems mounted with the
279 .BR """none,name="""
280 option).
281 .PP
282 Holding
283 .B CAP_SYS_ADMIN
284 within the user namespace associated with a process's PID namespace
285 allows (since Linux 3.8)
286 that process to mount
287 .I /proc
288 filesystems.
289 .PP
290 Note however, that mounting block-based filesystems can be done
291 only by a process that holds
292 .BR CAP_SYS_ADMIN
293 in the initial user namespace.
294 .\"
295 .\" ============================================================
296 .\"
297 .SS Interaction of user namespaces and other types of namespaces
298 Starting in Linux 3.8, unprivileged processes can create user namespaces,
299 and other the other types of namespaces can be created with just the
300 .B CAP_SYS_ADMIN
301 capability in the caller's user namespace.
302 .PP
303 When a non-user-namespace is created,
304 it is owned by the user namespace in which the creating process
305 was a member at the time of the creation of the namespace.
306 Actions on the non-user-namespace
307 require capabilities in the corresponding user namespace.
308 .PP
309 If
310 .BR CLONE_NEWUSER
311 is specified along with other
312 .B CLONE_NEW*
313 flags in a single
314 .BR clone (2)
315 or
316 .BR unshare (2)
317 call, the user namespace is guaranteed to be created first,
318 giving the child
319 .RB ( clone (2))
320 or caller
321 .RB ( unshare (2))
322 privileges over the remaining namespaces created by the call.
323 Thus, it is possible for an unprivileged caller to specify this combination
324 of flags.
325 .PP
326 When a new namespace (other than a user namespace) is created via
327 .BR clone (2)
328 or
329 .BR unshare (2),
330 the kernel records the user namespace of the creating process against
331 the new namespace.
332 (This association can't be changed.)
333 When a process in the new namespace subsequently performs
334 privileged operations that operate on global
335 resources isolated by the namespace,
336 the permission checks are performed according to the process's capabilities
337 in the user namespace that the kernel associated with the new namespace.
338 For example, suppose that a process attempts to change the hostname
339 .RB ( sethostname (2)),
340 a resource governed by the UTS namespace.
341 In this case,
342 the kernel will determine which user namespace is associated with
343 the process's UTS namespace, and check whether the process has the
344 required capability
345 .RB ( CAP_SYS_ADMIN )
346 in that user namespace.
347 .PP
348 The
349 .BR NS_GET_USERNS
350 .BR ioctl (2)
351 operation can be used to discover the user namespace with which
352 a non-user namespace is associated; see
353 .BR ioctl_ns (2).
354 .\"
355 .\" ============================================================
356 .\"
357 .SS User and group ID mappings: uid_map and gid_map
358 When a user namespace is created,
359 it starts out without a mapping of user IDs (group IDs)
360 to the parent user namespace.
361 The
362 .IR /proc/[pid]/uid_map
363 and
364 .IR /proc/[pid]/gid_map
365 files (available since Linux 3.5)
366 .\" commit 22d917d80e842829d0ca0a561967d728eb1d6303
367 expose the mappings for user and group IDs
368 inside the user namespace for the process
369 .IR pid .
370 These files can be read to view the mappings in a user namespace and
371 written to (once) to define the mappings.
372 .PP
373 The description in the following paragraphs explains the details for
374 .IR uid_map ;
375 .IR gid_map
376 is exactly the same,
377 but each instance of "user ID" is replaced by "group ID".
378 .PP
379 The
380 .I uid_map
381 file exposes the mapping of user IDs from the user namespace
382 of the process
383 .IR pid
384 to the user namespace of the process that opened
385 .IR uid_map
386 (but see a qualification to this point below).
387 In other words, processes that are in different user namespaces
388 will potentially see different values when reading from a particular
389 .I uid_map
390 file, depending on the user ID mappings for the user namespaces
391 of the reading processes.
392 .PP
393 Each line in the
394 .I uid_map
395 file specifies a 1-to-1 mapping of a range of contiguous
396 user IDs between two user namespaces.
397 (When a user namespace is first created, this file is empty.)
398 The specification in each line takes the form of
399 three numbers delimited by white space.
400 The first two numbers specify the starting user ID in
401 each of the two user namespaces.
402 The third number specifies the length of the mapped range.
403 In detail, the fields are interpreted as follows:
404 .IP (1) 4
405 The start of the range of user IDs in
406 the user namespace of the process
407 .IR pid .
408 .IP (2)
409 The start of the range of user
410 IDs to which the user IDs specified by field one map.
411 How field two is interpreted depends on whether the process that opened
412 .I uid_map
413 and the process
414 .IR pid
415 are in the same user namespace, as follows:
416 .RS
417 .IP a) 3
418 If the two processes are in different user namespaces:
419 field two is the start of a range of
420 user IDs in the user namespace of the process that opened
421 .IR uid_map .
422 .IP b)
423 If the two processes are in the same user namespace:
424 field two is the start of the range of
425 user IDs in the parent user namespace of the process
426 .IR pid .
427 This case enables the opener of
428 .I uid_map
429 (the common case here is opening
430 .IR /proc/self/uid_map )
431 to see the mapping of user IDs into the user namespace of the process
432 that created this user namespace.
433 .RE
434 .IP (3)
435 The length of the range of user IDs that is mapped between the two
436 user namespaces.
437 .PP
438 System calls that return user IDs (group IDs)\(emfor example,
439 .BR getuid (2),
440 .BR getgid (2),
441 and the credential fields in the structure returned by
442 .BR stat (2)\(emreturn
443 the user ID (group ID) mapped into the caller's user namespace.
444 .PP
445 When a process accesses a file, its user and group IDs
446 are mapped into the initial user namespace for the purpose of permission
447 checking and assigning IDs when creating a file.
448 When a process retrieves file user and group IDs via
449 .BR stat (2),
450 the IDs are mapped in the opposite direction,
451 to produce values relative to the process user and group ID mappings.
452 .PP
453 The initial user namespace has no parent namespace,
454 but, for consistency, the kernel provides dummy user and group
455 ID mapping files for this namespace.
456 Looking at the
457 .I uid_map
458 file
459 .RI ( gid_map
460 is the same) from a shell in the initial namespace shows:
461 .PP
462 .in +4n
463 .EX
464 $ \fBcat /proc/$$/uid_map\fP
465 0 0 4294967295
466 .EE
467 .in
468 .PP
469 This mapping tells us
470 that the range starting at user ID 0 in this namespace
471 maps to a range starting at 0 in the (nonexistent) parent namespace,
472 and the length of the range is the largest 32-bit unsigned integer.
473 This leaves 4294967295 (the 32-bit signed \-1 value) unmapped.
474 This is deliberate:
475 .IR "(uid_t)\ \-1"
476 is used in several interfaces (e.g.,
477 .BR setreuid (2))
478 as a way to specify "no user ID".
479 Leaving
480 .IR "(uid_t)\ \-1"
481 unmapped and unusable guarantees that there will be no
482 confusion when using these interfaces.
483 .\"
484 .\" ============================================================
485 .\"
486 .SS Defining user and group ID mappings: writing to uid_map and gid_map
487 .PP
488 After the creation of a new user namespace, the
489 .I uid_map
490 file of
491 .I one
492 of the processes in the namespace may be written to
493 .I once
494 to define the mapping of user IDs in the new user namespace.
495 An attempt to write more than once to a
496 .I uid_map
497 file in a user namespace fails with the error
498 .BR EPERM .
499 Similar rules apply for
500 .I gid_map
501 files.
502 .PP
503 The lines written to
504 .IR uid_map
505 .RI ( gid_map )
506 must conform to the following rules:
507 .IP * 3
508 The three fields must be valid numbers,
509 and the last field must be greater than 0.
510 .IP *
511 Lines are terminated by newline characters.
512 .IP *
513 There is an (arbitrary) limit on the number of lines in the file.
514 As at Linux 3.18, the limit is five lines.
515 In addition, the number of bytes written to
516 the file must be less than the system page size,
517 .\" FIXME(Eric): the restriction "less than" rather than "less than or equal"
518 .\" seems strangely arbitrary. Furthermore, the comment does not agree
519 .\" with the code in kernel/user_namespace.c. Which is correct?
520 and the write must be performed at the start of the file (i.e.,
521 .BR lseek (2)
522 and
523 .BR pwrite (2)
524 can't be used to write to nonzero offsets in the file).
525 .IP *
526 The range of user IDs (group IDs)
527 specified in each line cannot overlap with the ranges
528 in any other lines.
529 In the initial implementation (Linux 3.8), this requirement was
530 satisfied by a simplistic implementation that imposed the further
531 requirement that
532 the values in both field 1 and field 2 of successive lines must be
533 in ascending numerical order,
534 which prevented some otherwise valid maps from being created.
535 Linux 3.9 and later
536 .\" commit 0bd14b4fd72afd5df41e9fd59f356740f22fceba
537 fix this limitation, allowing any valid set of nonoverlapping maps.
538 .IP *
539 At least one line must be written to the file.
540 .PP
541 Writes that violate the above rules fail with the error
542 .BR EINVAL .
543 .PP
544 In order for a process to write to the
545 .I /proc/[pid]/uid_map
546 .RI ( /proc/[pid]/gid_map )
547 file, all of the following requirements must be met:
548 .IP 1. 3
549 The writing process must have the
550 .BR CAP_SETUID
551 .RB ( CAP_SETGID )
552 capability in the user namespace of the process
553 .IR pid .
554 .IP 2.
555 The writing process must either be in the user namespace of the process
556 .I pid
557 or be in the parent user namespace of the process
558 .IR pid .
559 .IP 3.
560 The mapped user IDs (group IDs) must in turn have a mapping
561 in the parent user namespace.
562 .IP 4.
563 One of the following two cases applies:
564 .RS
565 .IP * 3
566 .IR Either
567 the writing process has the
568 .BR CAP_SETUID
569 .RB ( CAP_SETGID )
570 capability in the
571 .I parent
572 user namespace.
573 .RS
574 .IP + 3
575 No further restrictions apply:
576 the process can make mappings to arbitrary user IDs (group IDs)
577 in the parent user namespace.
578 .RE
579 .IP * 3
580 .IR Or
581 otherwise all of the following restrictions apply:
582 .RS
583 .IP + 3
584 The data written to
585 .I uid_map
586 .RI ( gid_map )
587 must consist of a single line that maps
588 the writing process's effective user ID
589 (group ID) in the parent user namespace to a user ID (group ID)
590 in the user namespace.
591 .IP +
592 The writing process must have the same effective user ID as the process
593 that created the user namespace.
594 .IP +
595 In the case of
596 .IR gid_map ,
597 use of the
598 .BR setgroups (2)
599 system call must first be denied by writing
600 .RI \(dq deny \(dq
601 to the
602 .I /proc/[pid]/setgroups
603 file (see below) before writing to
604 .IR gid_map .
605 .RE
606 .RE
607 .PP
608 Writes that violate the above rules fail with the error
609 .BR EPERM .
610 .\"
611 .\" ============================================================
612 .\"
613 .SS Interaction with system calls that change process UIDs or GIDs
614 In a user namespace where the
615 .I uid_map
616 file has not been written, the system calls that change user IDs will fail.
617 Similarly, if the
618 .I gid_map
619 file has not been written, the system calls that change group IDs will fail.
620 After the
621 .I uid_map
622 and
623 .I gid_map
624 files have been written, only the mapped values may be used in
625 system calls that change user and group IDs.
626 .PP
627 For user IDs, the relevant system calls include
628 .BR setuid (2),
629 .BR setfsuid (2),
630 .BR setreuid (2),
631 and
632 .BR setresuid (2).
633 For group IDs, the relevant system calls include
634 .BR setgid (2),
635 .BR setfsgid (2),
636 .BR setregid (2),
637 .BR setresgid (2),
638 and
639 .BR setgroups (2).
640 .PP
641 Writing
642 .RI \(dq deny \(dq
643 to the
644 .I /proc/[pid]/setgroups
645 file before writing to
646 .I /proc/[pid]/gid_map
647 .\" Things changed in Linux 3.19
648 .\" commit 9cc46516ddf497ea16e8d7cb986ae03a0f6b92f8
649 .\" commit 66d2f338ee4c449396b6f99f5e75cd18eb6df272
650 .\" http://lwn.net/Articles/626665/
651 will permanently disable
652 .BR setgroups (2)
653 in a user namespace and allow writing to
654 .I /proc/[pid]/gid_map
655 without having the
656 .BR CAP_SETGID
657 capability in the parent user namespace.
658 .\"
659 .\" ============================================================
660 .\"
661 .SS The /proc/[pid]/setgroups file
662 .\"
663 .\" commit 9cc46516ddf497ea16e8d7cb986ae03a0f6b92f8
664 .\" commit 66d2f338ee4c449396b6f99f5e75cd18eb6df272
665 .\" http://lwn.net/Articles/626665/
666 .\" http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-8989
667 .\"
668 The
669 .I /proc/[pid]/setgroups
670 file displays the string
671 .RI \(dq allow \(dq
672 if processes in the user namespace that contains the process
673 .I pid
674 are permitted to employ the
675 .BR setgroups (2)
676 system call; it displays
677 .RI \(dq deny \(dq
678 if
679 .BR setgroups (2)
680 is not permitted in that user namespace.
681 Note that regardless of the value in the
682 .I /proc/[pid]/setgroups
683 file (and regardless of the process's capabilities), calls to
684 .BR setgroups (2)
685 are also not permitted if
686 .IR /proc/[pid]/gid_map
687 has not yet been set.
688 .PP
689 A privileged process (one with the
690 .BR CAP_SYS_ADMIN
691 capability in the namespace) may write either of the strings
692 .RI \(dq allow \(dq
693 or
694 .RI \(dq deny \(dq
695 to this file
696 .I before
697 writing a group ID mapping
698 for this user namespace to the file
699 .IR /proc/[pid]/gid_map .
700 Writing the string
701 .RI \(dq deny \(dq
702 prevents any process in the user namespace from employing
703 .BR setgroups (2).
704 .PP
705 The essence of the restrictions described in the preceding
706 paragraph is that it is permitted to write to
707 .I /proc/[pid]/setgroups
708 only so long as calling
709 .BR setgroups (2)
710 is disallowed because
711 .I /proc/[pid]gid_map
712 has not been set.
713 This ensures that a process cannot transition from a state where
714 .BR setgroups (2)
715 is allowed to a state where
716 .BR setgroups (2)
717 is denied;
718 a process can transition only from
719 .BR setgroups (2)
720 being disallowed to
721 .BR setgroups (2)
722 being allowed.
723 .PP
724 The default value of this file in the initial user namespace is
725 .RI \(dq allow \(dq.
726 .PP
727 Once
728 .IR /proc/[pid]/gid_map
729 has been written to
730 (which has the effect of enabling
731 .BR setgroups (2)
732 in the user namespace),
733 it is no longer possible to disallow
734 .BR setgroups (2)
735 by writing
736 .RI \(dq deny \(dq
737 to
738 .IR /proc/[pid]/setgroups
739 (the write fails with the error
740 .BR EPERM ).
741 .PP
742 A child user namespace inherits the
743 .IR /proc/[pid]/setgroups
744 setting from its parent.
745 .PP
746 If the
747 .I setgroups
748 file has the value
749 .RI \(dq deny \(dq,
750 then the
751 .BR setgroups (2)
752 system call can't subsequently be reenabled (by writing
753 .RI \(dq allow \(dq
754 to the file) in this user namespace.
755 (Attempts to do so will fail with the error
756 .BR EPERM .)
757 This restriction also propagates down to all child user namespaces of
758 this user namespace.
759 .PP
760 The
761 .I /proc/[pid]/setgroups
762 file was added in Linux 3.19,
763 but was backported to many earlier stable kernel series,
764 because it addresses a security issue.
765 The issue concerned files with permissions such as "rwx\-\-\-rwx".
766 Such files give fewer permissions to "group" than they do to "other".
767 This means that dropping groups using
768 .BR setgroups (2)
769 might allow a process file access that it did not formerly have.
770 Before the existence of user namespaces this was not a concern,
771 since only a privileged process (one with the
772 .BR CAP_SETGID
773 capability) could call
774 .BR setgroups (2).
775 However, with the introduction of user namespaces,
776 it became possible for an unprivileged process to create
777 a new namespace in which the user had all privileges.
778 This then allowed formerly unprivileged
779 users to drop groups and thus gain file access
780 that they did not previously have.
781 The
782 .I /proc/[pid]/setgroups
783 file was added to address this security issue,
784 by denying any pathway for an unprivileged process to drop groups with
785 .BR setgroups (2).
786 .\"
787 .\" /proc/PID/setgroups
788 .\" [allow == setgroups() is allowed, "deny" == setgroups() is disallowed]
789 .\" * Can write if have CAP_SYS_ADMIN in NS
790 .\" * Must write BEFORE writing to /proc/PID/gid_map
791 .\"
792 .\" setgroups()
793 .\" * Must already have written to gid_maps
794 .\" * /proc/PID/setgroups must be "allow"
795 .\"
796 .\" /proc/PID/gid_map -- writing
797 .\" * Must already have written "deny" to /proc/PID/setgroups
798 .\"
799 .\" ============================================================
800 .\"
801 .SS Unmapped user and group IDs
802 .PP
803 There are various places where an unmapped user ID (group ID)
804 may be exposed to user space.
805 For example, the first process in a new user namespace may call
806 .BR getuid (2)
807 before a user ID mapping has been defined for the namespace.
808 In most such cases, an unmapped user ID is converted
809 .\" from_kuid_munged(), from_kgid_munged()
810 to the overflow user ID (group ID);
811 the default value for the overflow user ID (group ID) is 65534.
812 See the descriptions of
813 .IR /proc/sys/kernel/overflowuid
814 and
815 .IR /proc/sys/kernel/overflowgid
816 in
817 .BR proc (5).
818 .PP
819 The cases where unmapped IDs are mapped in this fashion include
820 system calls that return user IDs
821 .RB ( getuid (2),
822 .BR getgid (2),
823 and similar),
824 credentials passed over a UNIX domain socket,
825 .\" also SO_PEERCRED
826 credentials returned by
827 .BR stat (2),
828 .BR waitid (2),
829 and the System V IPC "ctl"
830 .B IPC_STAT
831 operations,
832 credentials exposed by
833 .IR /proc/[pid]/status
834 and the files in
835 .IR /proc/sysvipc/* ,
836 credentials returned via the
837 .I si_uid
838 field in the
839 .I siginfo_t
840 received with a signal (see
841 .BR sigaction (2)),
842 credentials written to the process accounting file (see
843 .BR acct (5)),
844 and credentials returned with POSIX message queue notifications (see
845 .BR mq_notify (3)).
846 .PP
847 There is one notable case where unmapped user and group IDs are
848 .I not
849 .\" from_kuid(), from_kgid()
850 .\" Also F_GETOWNER_UIDS is an exception
851 converted to the corresponding overflow ID value.
852 When viewing a
853 .I uid_map
854 or
855 .I gid_map
856 file in which there is no mapping for the second field,
857 that field is displayed as 4294967295 (\-1 as an unsigned integer);
858 .\"
859 .\" ============================================================
860 .\"
861 .SS Set-user-ID and set-group-ID programs
862 .PP
863 When a process inside a user namespace executes
864 a set-user-ID (set-group-ID) program,
865 the process's effective user (group) ID inside the namespace is changed
866 to whatever value is mapped for the user (group) ID of the file.
867 However, if either the user
868 .I or
869 the group ID of the file has no mapping inside the namespace,
870 the set-user-ID (set-group-ID) bit is silently ignored:
871 the new program is executed,
872 but the process's effective user (group) ID is left unchanged.
873 (This mirrors the semantics of executing a set-user-ID or set-group-ID
874 program that resides on a filesystem that was mounted with the
875 .BR MS_NOSUID
876 flag, as described in
877 .BR mount (2).)
878 .\"
879 .\" ============================================================
880 .\"
881 .SS Miscellaneous
882 .PP
883 When a process's user and group IDs are passed over a UNIX domain socket
884 to a process in a different user namespace (see the description of
885 .B SCM_CREDENTIALS
886 in
887 .BR unix (7)),
888 they are translated into the corresponding values as per the
889 receiving process's user and group ID mappings.
890 .\"
891 .SH CONFORMING TO
892 Namespaces are a Linux-specific feature.
893 .\"
894 .SH NOTES
895 Over the years, there have been a lot of features that have been added
896 to the Linux kernel that have been made available only to privileged users
897 because of their potential to confuse set-user-ID-root applications.
898 In general, it becomes safe to allow the root user in a user namespace to
899 use those features because it is impossible, while in a user namespace,
900 to gain more privilege than the root user of a user namespace has.
901 .\"
902 .\" ============================================================
903 .\"
904 .SS Availability
905 Use of user namespaces requires a kernel that is configured with the
906 .B CONFIG_USER_NS
907 option.
908 User namespaces require support in a range of subsystems across
909 the kernel.
910 When an unsupported subsystem is configured into the kernel,
911 it is not possible to configure user namespaces support.
912 .PP
913 As at Linux 3.8, most relevant subsystems supported user namespaces,
914 but a number of filesystems did not have the infrastructure needed
915 to map user and group IDs between user namespaces.
916 Linux 3.9 added the required infrastructure support for many of
917 the remaining unsupported filesystems
918 (Plan 9 (9P), Andrew File System (AFS), Ceph, CIFS, CODA, NFS, and OCFS2).
919 Linux 3.12 added support the last of the unsupported major filesystems,
920 .\" commit d6970d4b726cea6d7a9bc4120814f95c09571fc3
921 XFS.
922 .\"
923 .SH EXAMPLE
924 The program below is designed to allow experimenting with
925 user namespaces, as well as other types of namespaces.
926 It creates namespaces as specified by command-line options and then executes
927 a command inside those namespaces.
928 The comments and
929 .I usage()
930 function inside the program provide a full explanation of the program.
931 The following shell session demonstrates its use.
932 .PP
933 First, we look at the run-time environment:
934 .PP
935 .in +4n
936 .EX
937 $ \fBuname \-rs\fP # Need Linux 3.8 or later
938 Linux 3.8.0
939 $ \fBid \-u\fP # Running as unprivileged user
940 1000
941 $ \fBid \-g\fP
942 1000
943 .EE
944 .in
945 .PP
946 Now start a new shell in new user
947 .RI ( \-U ),
948 mount
949 .RI ( \-m ),
950 and PID
951 .RI ( \-p )
952 namespaces, with user ID
953 .RI ( \-M )
954 and group ID
955 .RI ( \-G )
956 1000 mapped to 0 inside the user namespace:
957 .PP
958 .in +4n
959 .EX
960 $ \fB./userns_child_exec \-p \-m \-U \-M '0 1000 1' \-G '0 1000 1' bash\fP
961 .EE
962 .in
963 .PP
964 The shell has PID 1, because it is the first process in the new
965 PID namespace:
966 .PP
967 .in +4n
968 .EX
969 bash$ \fBecho $$\fP
970 1
971 .EE
972 .in
973 Mounting a new
974 .I /proc
975 filesystem and listing all of the processes visible
976 in the new PID namespace shows that the shell can't see
977 any processes outside the PID namespace:
978 .PP
979 .in +4n
980 .EX
981 bash$ \fBmount \-t proc proc /proc\fP
982 bash$ \fBps ax\fP
983 PID TTY STAT TIME COMMAND
984 1 pts/3 S 0:00 bash
985 22 pts/3 R+ 0:00 ps ax
986 .EE
987 .in
988 .PP
989 Inside the user namespace, the shell has user and group ID 0,
990 and a full set of permitted and effective capabilities:
991 .PP
992 .in +4n
993 .EX
994 bash$ \fBcat /proc/$$/status | egrep '^[UG]id'\fP
995 Uid: 0 0 0 0
996 Gid: 0 0 0 0
997 bash$ \fBcat /proc/$$/status | egrep '^Cap(Prm|Inh|Eff)'\fP
998 CapInh: 0000000000000000
999 CapPrm: 0000001fffffffff
1000 CapEff: 0000001fffffffff
1001 .EE
1002 .in
1003 .SS Program source
1004 \&
1005 .EX
1006 /* userns_child_exec.c
1007
1008 Licensed under GNU General Public License v2 or later
1009
1010 Create a child process that executes a shell command in new
1011 namespace(s); allow UID and GID mappings to be specified when
1012 creating a user namespace.
1013 */
1014 #define _GNU_SOURCE
1015 #include <sched.h>
1016 #include <unistd.h>
1017 #include <stdlib.h>
1018 #include <sys/wait.h>
1019 #include <signal.h>
1020 #include <fcntl.h>
1021 #include <stdio.h>
1022 #include <string.h>
1023 #include <limits.h>
1024 #include <errno.h>
1025
1026 /* A simple error\-handling function: print an error message based
1027 on the value in \(aqerrno\(aq and terminate the calling process */
1028
1029 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
1030 } while (0)
1031
1032 struct child_args {
1033 char **argv; /* Command to be executed by child, with args */
1034 int pipe_fd[2]; /* Pipe used to synchronize parent and child */
1035 };
1036
1037 static int verbose;
1038
1039 static void
1040 usage(char *pname)
1041 {
1042 fprintf(stderr, "Usage: %s [options] cmd [arg...]\\n\\n", pname);
1043 fprintf(stderr, "Create a child process that executes a shell "
1044 "command in a new user namespace,\\n"
1045 "and possibly also other new namespace(s).\\n\\n");
1046 fprintf(stderr, "Options can be:\\n\\n");
1047 #define fpe(str) fprintf(stderr, " %s", str);
1048 fpe("\-i New IPC namespace\\n");
1049 fpe("\-m New mount namespace\\n");
1050 fpe("\-n New network namespace\\n");
1051 fpe("\-p New PID namespace\\n");
1052 fpe("\-u New UTS namespace\\n");
1053 fpe("\-U New user namespace\\n");
1054 fpe("\-M uid_map Specify UID map for user namespace\\n");
1055 fpe("\-G gid_map Specify GID map for user namespace\\n");
1056 fpe("\-z Map user\(aqs UID and GID to 0 in user namespace\\n");
1057 fpe(" (equivalent to: \-M \(aq0 <uid> 1\(aq \-G \(aq0 <gid> 1\(aq)\\n");
1058 fpe("\-v Display verbose messages\\n");
1059 fpe("\\n");
1060 fpe("If \-z, \-M, or \-G is specified, \-U is required.\\n");
1061 fpe("It is not permitted to specify both \-z and either \-M or \-G.\\n");
1062 fpe("\\n");
1063 fpe("Map strings for \-M and \-G consist of records of the form:\\n");
1064 fpe("\\n");
1065 fpe(" ID\-inside\-ns ID\-outside\-ns len\\n");
1066 fpe("\\n");
1067 fpe("A map string can contain multiple records, separated"
1068 " by commas;\\n");
1069 fpe("the commas are replaced by newlines before writing"
1070 " to map files.\\n");
1071
1072 exit(EXIT_FAILURE);
1073 }
1074
1075 /* Update the mapping file \(aqmap_file\(aq, with the value provided in
1076 \(aqmapping\(aq, a string that defines a UID or GID mapping. A UID or
1077 GID mapping consists of one or more newline\-delimited records
1078 of the form:
1079
1080 ID_inside\-ns ID\-outside\-ns length
1081
1082 Requiring the user to supply a string that contains newlines is
1083 of course inconvenient for command\-line use. Thus, we permit the
1084 use of commas to delimit records in this string, and replace them
1085 with newlines before writing the string to the file. */
1086
1087 static void
1088 update_map(char *mapping, char *map_file)
1089 {
1090 int fd, j;
1091 size_t map_len; /* Length of \(aqmapping\(aq */
1092
1093 /* Replace commas in mapping string with newlines */
1094
1095 map_len = strlen(mapping);
1096 for (j = 0; j < map_len; j++)
1097 if (mapping[j] == \(aq,\(aq)
1098 mapping[j] = \(aq\\n\(aq;
1099
1100 fd = open(map_file, O_RDWR);
1101 if (fd == \-1) {
1102 fprintf(stderr, "ERROR: open %s: %s\\n", map_file,
1103 strerror(errno));
1104 exit(EXIT_FAILURE);
1105 }
1106
1107 if (write(fd, mapping, map_len) != map_len) {
1108 fprintf(stderr, "ERROR: write %s: %s\\n", map_file,
1109 strerror(errno));
1110 exit(EXIT_FAILURE);
1111 }
1112
1113 close(fd);
1114 }
1115
1116 /* Linux 3.19 made a change in the handling of setgroups(2) and the
1117 \(aqgid_map\(aq file to address a security issue. The issue allowed
1118 *unprivileged* users to employ user namespaces in order to drop
1119 The upshot of the 3.19 changes is that in order to update the
1120 \(aqgid_maps\(aq file, use of the setgroups() system call in this
1121 user namespace must first be disabled by writing "deny" to one of
1122 the /proc/PID/setgroups files for this namespace. That is the
1123 purpose of the following function. */
1124
1125 static void
1126 proc_setgroups_write(pid_t child_pid, char *str)
1127 {
1128 char setgroups_path[PATH_MAX];
1129 int fd;
1130
1131 snprintf(setgroups_path, PATH_MAX, "/proc/%ld/setgroups",
1132 (long) child_pid);
1133
1134 fd = open(setgroups_path, O_RDWR);
1135 if (fd == \-1) {
1136
1137 /* We may be on a system that doesn\(aqt support
1138 /proc/PID/setgroups. In that case, the file won\(aqt exist,
1139 and the system won\(aqt impose the restrictions that Linux 3.19
1140 added. That\(aqs fine: we don\(aqt need to do anything in order
1141 to permit \(aqgid_map\(aq to be updated.
1142
1143 However, if the error from open() was something other than
1144 the ENOENT error that is expected for that case, let the
1145 user know. */
1146
1147 if (errno != ENOENT)
1148 fprintf(stderr, "ERROR: open %s: %s\\n", setgroups_path,
1149 strerror(errno));
1150 return;
1151 }
1152
1153 if (write(fd, str, strlen(str)) == \-1)
1154 fprintf(stderr, "ERROR: write %s: %s\\n", setgroups_path,
1155 strerror(errno));
1156
1157 close(fd);
1158 }
1159
1160 static int /* Start function for cloned child */
1161 childFunc(void *arg)
1162 {
1163 struct child_args *args = (struct child_args *) arg;
1164 char ch;
1165
1166 /* Wait until the parent has updated the UID and GID mappings.
1167 See the comment in main(). We wait for end of file on a
1168 pipe that will be closed by the parent process once it has
1169 updated the mappings. */
1170
1171 close(args\->pipe_fd[1]); /* Close our descriptor for the write
1172 end of the pipe so that we see EOF
1173 when parent closes its descriptor */
1174 if (read(args\->pipe_fd[0], &ch, 1) != 0) {
1175 fprintf(stderr,
1176 "Failure in child: read from pipe returned != 0\\n");
1177 exit(EXIT_FAILURE);
1178 }
1179
1180 close(args\->pipe_fd[0]);
1181
1182 /* Execute a shell command */
1183
1184 printf("About to exec %s\\n", args\->argv[0]);
1185 execvp(args\->argv[0], args\->argv);
1186 errExit("execvp");
1187 }
1188
1189 #define STACK_SIZE (1024 * 1024)
1190
1191 static char child_stack[STACK_SIZE]; /* Space for child\(aqs stack */
1192
1193 int
1194 main(int argc, char *argv[])
1195 {
1196 int flags, opt, map_zero;
1197 pid_t child_pid;
1198 struct child_args args;
1199 char *uid_map, *gid_map;
1200 const int MAP_BUF_SIZE = 100;
1201 char map_buf[MAP_BUF_SIZE];
1202 char map_path[PATH_MAX];
1203
1204 /* Parse command\-line options. The initial \(aq+\(aq character in
1205 the final getopt() argument prevents GNU\-style permutation
1206 of command\-line options. That\(aqs useful, since sometimes
1207 the \(aqcommand\(aq to be executed by this program itself
1208 has command\-line options. We don\(aqt want getopt() to treat
1209 those as options to this program. */
1210
1211 flags = 0;
1212 verbose = 0;
1213 gid_map = NULL;
1214 uid_map = NULL;
1215 map_zero = 0;
1216 while ((opt = getopt(argc, argv, "+imnpuUM:G:zv")) != \-1) {
1217 switch (opt) {
1218 case \(aqi\(aq: flags |= CLONE_NEWIPC; break;
1219 case \(aqm\(aq: flags |= CLONE_NEWNS; break;
1220 case \(aqn\(aq: flags |= CLONE_NEWNET; break;
1221 case \(aqp\(aq: flags |= CLONE_NEWPID; break;
1222 case \(aqu\(aq: flags |= CLONE_NEWUTS; break;
1223 case \(aqv\(aq: verbose = 1; break;
1224 case \(aqz\(aq: map_zero = 1; break;
1225 case \(aqM\(aq: uid_map = optarg; break;
1226 case \(aqG\(aq: gid_map = optarg; break;
1227 case \(aqU\(aq: flags |= CLONE_NEWUSER; break;
1228 default: usage(argv[0]);
1229 }
1230 }
1231
1232 /* \-M or \-G without \-U is nonsensical */
1233
1234 if (((uid_map != NULL || gid_map != NULL || map_zero) &&
1235 !(flags & CLONE_NEWUSER)) ||
1236 (map_zero && (uid_map != NULL || gid_map != NULL)))
1237 usage(argv[0]);
1238
1239 args.argv = &argv[optind];
1240
1241 /* We use a pipe to synchronize the parent and child, in order to
1242 ensure that the parent sets the UID and GID maps before the child
1243 calls execve(). This ensures that the child maintains its
1244 capabilities during the execve() in the common case where we
1245 want to map the child\(aqs effective user ID to 0 in the new user
1246 namespace. Without this synchronization, the child would lose
1247 its capabilities if it performed an execve() with nonzero
1248 user IDs (see the capabilities(7) man page for details of the
1249 transformation of a process\(aqs capabilities during execve()). */
1250
1251 if (pipe(args.pipe_fd) == \-1)
1252 errExit("pipe");
1253
1254 /* Create the child in new namespace(s) */
1255
1256 child_pid = clone(childFunc, child_stack + STACK_SIZE,
1257 flags | SIGCHLD, &args);
1258 if (child_pid == \-1)
1259 errExit("clone");
1260
1261 /* Parent falls through to here */
1262
1263 if (verbose)
1264 printf("%s: PID of child created by clone() is %ld\\n",
1265 argv[0], (long) child_pid);
1266
1267 /* Update the UID and GID maps in the child */
1268
1269 if (uid_map != NULL || map_zero) {
1270 snprintf(map_path, PATH_MAX, "/proc/%ld/uid_map",
1271 (long) child_pid);
1272 if (map_zero) {
1273 snprintf(map_buf, MAP_BUF_SIZE, "0 %ld 1", (long) getuid());
1274 uid_map = map_buf;
1275 }
1276 update_map(uid_map, map_path);
1277 }
1278
1279 if (gid_map != NULL || map_zero) {
1280 proc_setgroups_write(child_pid, "deny");
1281
1282 snprintf(map_path, PATH_MAX, "/proc/%ld/gid_map",
1283 (long) child_pid);
1284 if (map_zero) {
1285 snprintf(map_buf, MAP_BUF_SIZE, "0 %ld 1", (long) getgid());
1286 gid_map = map_buf;
1287 }
1288 update_map(gid_map, map_path);
1289 }
1290
1291 /* Close the write end of the pipe, to signal to the child that we
1292 have updated the UID and GID maps */
1293
1294 close(args.pipe_fd[1]);
1295
1296 if (waitpid(child_pid, NULL, 0) == \-1) /* Wait for child */
1297 errExit("waitpid");
1298
1299 if (verbose)
1300 printf("%s: terminating\\n", argv[0]);
1301
1302 exit(EXIT_SUCCESS);
1303 }
1304 .EE
1305 .SH SEE ALSO
1306 .BR newgidmap (1), \" From the shadow package
1307 .BR newuidmap (1), \" From the shadow package
1308 .BR clone (2),
1309 .BR ptrace (2),
1310 .BR setns (2),
1311 .BR unshare (2),
1312 .BR proc (5),
1313 .BR subgid (5), \" From the shadow package
1314 .BR subuid (5), \" From the shadow package
1315 .BR capabilities (7),
1316 .BR cgroup_namespaces (7)
1317 .BR credentials (7),
1318 .BR namespaces (7),
1319 .BR pid_namespaces (7)
1320 .PP
1321 The kernel source file
1322 .IR Documentation/namespaces/resource-control.txt .