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