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