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