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