Debian Codegraph @

upstream/debian-trixie/man2/unshare.2

raw
1 .\" Copyright (C) 2006, Janak Desai <[email protected]>
2 .\" and Copyright (C) 2006, 2012 Michael Kerrisk <[email protected]>
3 .\"
4 .\" SPDX-License-Identifier: GPL-1.0-or-later
5 .\"
6 .\" Patch Justification:
7 .\" unshare system call is needed to implement, using PAM,
8 .\" per-security_context and/or per-user namespace to provide
9 .\" polyinstantiated directories. Using unshare and bind mounts, a
10 .\" PAM module can create private namespace with appropriate
11 .\" directories(based on user's security context) bind mounted on
12 .\" public directories such as /tmp, thus providing an instance of
13 .\" /tmp that is based on user's security context. Without the
14 .\" unshare system call, namespace separation can only be achieved
15 .\" by clone, which would require porting and maintaining all commands
16 .\" such as login, and su, that establish a user session.
17 .\"
18 .TH unshare 2 2024-06-15 "Linux man-pages 6.9.1"
19 .SH NAME
20 unshare \- disassociate parts of the process execution context
21 .SH LIBRARY
22 Standard C library
23 .RI ( libc ", " \-lc )
24 .SH SYNOPSIS
25 .nf
26 .B #define _GNU_SOURCE
27 .B #include <sched.h>
28 .P
29 .BI "int unshare(int " flags );
30 .fi
31 .SH DESCRIPTION
32 .BR unshare ()
33 allows a process (or thread) to disassociate parts of its execution
34 context that are currently being shared with other processes (or threads).
35 Part of the execution context, such as the mount namespace, is shared
36 implicitly when a new process is created using
37 .BR fork (2)
38 or
39 .BR vfork (2),
40 while other parts, such as virtual memory, may be
41 shared by explicit request when creating a process or thread using
42 .BR clone (2).
43 .P
44 The main use of
45 .BR unshare ()
46 is to allow a process to control its
47 shared execution context without creating a new process.
48 .P
49 The
50 .I flags
51 argument is a bit mask that specifies which parts of
52 the execution context should be unshared.
53 This argument is specified by ORing together zero or more
54 of the following constants:
55 .TP
56 .B CLONE_FILES
57 Reverse the effect of the
58 .BR clone (2)
59 .B CLONE_FILES
60 flag.
61 Unshare the file descriptor table, so that the calling process
62 no longer shares its file descriptors with any other process.
63 .TP
64 .B CLONE_FS
65 Reverse the effect of the
66 .BR clone (2)
67 .B CLONE_FS
68 flag.
69 Unshare filesystem attributes, so that the calling process
70 no longer shares its root directory
71 .RB ( chroot (2)),
72 current directory
73 .RB ( chdir (2)),
74 or umask
75 .RB ( umask (2))
76 attributes with any other process.
77 .TP
78 .BR CLONE_NEWCGROUP " (since Linux 4.6)"
79 This flag has the same effect as the
80 .BR clone (2)
81 .B CLONE_NEWCGROUP
82 flag.
83 Unshare the cgroup namespace.
84 Use of
85 .B CLONE_NEWCGROUP
86 requires the
87 .B CAP_SYS_ADMIN
88 capability.
89 .TP
90 .BR CLONE_NEWIPC " (since Linux 2.6.19)"
91 This flag has the same effect as the
92 .BR clone (2)
93 .B CLONE_NEWIPC
94 flag.
95 Unshare the IPC namespace,
96 so that the calling process has a private copy of the
97 IPC namespace which is not shared with any other process.
98 Specifying this flag automatically implies
99 .B CLONE_SYSVSEM
100 as well.
101 Use of
102 .B CLONE_NEWIPC
103 requires the
104 .B CAP_SYS_ADMIN
105 capability.
106 .TP
107 .BR CLONE_NEWNET " (since Linux 2.6.24)"
108 This flag has the same effect as the
109 .BR clone (2)
110 .B CLONE_NEWNET
111 flag.
112 Unshare the network namespace,
113 so that the calling process is moved into a
114 new network namespace which is not shared
115 with any previously existing process.
116 Use of
117 .B CLONE_NEWNET
118 requires the
119 .B CAP_SYS_ADMIN
120 capability.
121 .TP
122 .B CLONE_NEWNS
123 .\" These flag name are inconsistent:
124 .\" CLONE_NEWNS does the same thing in clone(), but CLONE_VM,
125 .\" CLONE_FS, and CLONE_FILES reverse the action of the clone()
126 .\" flags of the same name.
127 This flag has the same effect as the
128 .BR clone (2)
129 .B CLONE_NEWNS
130 flag.
131 Unshare the mount namespace,
132 so that the calling process has a private copy of
133 its namespace which is not shared with any other process.
134 Specifying this flag automatically implies
135 .B CLONE_FS
136 as well.
137 Use of
138 .B CLONE_NEWNS
139 requires the
140 .B CAP_SYS_ADMIN
141 capability.
142 For further information, see
143 .BR mount_namespaces (7).
144 .TP
145 .BR CLONE_NEWPID " (since Linux 3.8)"
146 This flag has the same effect as the
147 .BR clone (2)
148 .B CLONE_NEWPID
149 flag.
150 Unshare the PID namespace,
151 so that the calling process has a new PID namespace for its children
152 which is not shared with any previously existing process.
153 The calling process is
154 .I not
155 moved into the new namespace.
156 The first child created by the calling process will have
157 the process ID 1 and will assume the role of
158 .BR init (1)
159 in the new namespace.
160 .B CLONE_NEWPID
161 automatically implies
162 .B CLONE_THREAD
163 as well.
164 Use of
165 .B CLONE_NEWPID
166 requires the
167 .B CAP_SYS_ADMIN
168 capability.
169 For further information, see
170 .BR pid_namespaces (7).
171 .TP
172 .BR CLONE_NEWTIME " (since Linux 5.6)"
173 Unshare the time namespace,
174 so that the calling process has a new time namespace for its children
175 which is not shared with any previously existing process.
176 The calling process is
177 .I not
178 moved into the new namespace.
179 Use of
180 .B CLONE_NEWTIME
181 requires the
182 .B CAP_SYS_ADMIN
183 capability.
184 For further information, see
185 .BR time_namespaces (7).
186 .TP
187 .BR CLONE_NEWUSER " (since Linux 3.8)"
188 This flag has the same effect as the
189 .BR clone (2)
190 .B CLONE_NEWUSER
191 flag.
192 Unshare the user namespace,
193 so that the calling process is moved into a new user namespace
194 which is not shared with any previously existing process.
195 As with the child process created by
196 .BR clone (2)
197 with the
198 .B CLONE_NEWUSER
199 flag, the caller obtains a full set of capabilities in the new namespace.
200 .IP
201 .B CLONE_NEWUSER
202 requires that the calling process is not threaded; specifying
203 .B CLONE_NEWUSER
204 automatically implies
205 .BR CLONE_THREAD .
206 Since Linux 3.9,
207 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
208 .\" https://lwn.net/Articles/543273/
209 .B CLONE_NEWUSER
210 also automatically implies
211 .BR CLONE_FS .
212 .B CLONE_NEWUSER
213 requires that the user ID and group ID
214 of the calling process are mapped to user IDs and group IDs in the
215 user namespace of the calling process at the time of the call.
216 .IP
217 For further information on user namespaces, see
218 .BR user_namespaces (7).
219 .TP
220 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
221 This flag has the same effect as the
222 .BR clone (2)
223 .B CLONE_NEWUTS
224 flag.
225 Unshare the UTS IPC namespace,
226 so that the calling process has a private copy of the
227 UTS namespace which is not shared with any other process.
228 Use of
229 .B CLONE_NEWUTS
230 requires the
231 .B CAP_SYS_ADMIN
232 capability.
233 .TP
234 .BR CLONE_SYSVSEM " (since Linux 2.6.26)"
235 .\" commit 9edff4ab1f8d82675277a04e359d0ed8bf14a7b7
236 This flag reverses the effect of the
237 .BR clone (2)
238 .B CLONE_SYSVSEM
239 flag.
240 Unshare System\ V semaphore adjustment
241 .RI ( semadj )
242 values,
243 so that the calling process has a new empty
244 .I semadj
245 list that is not shared with any other process.
246 If this is the last process that has a reference to the process's current
247 .I semadj
248 list, then the adjustments in that list are applied
249 to the corresponding semaphores, as described in
250 .BR semop (2).
251 .\" CLONE_NEWNS If CLONE_SIGHAND is set and signals are also being shared
252 .\" (i.e., current->signal->count > 1), force CLONE_THREAD.
253 .P
254 In addition,
255 .BR CLONE_THREAD ,
256 .BR CLONE_SIGHAND ,
257 and
258 .B CLONE_VM
259 can be specified in
260 .I flags
261 if the caller is single threaded (i.e., it is not sharing
262 its address space with another process or thread).
263 In this case, these flags have no effect.
264 (Note also that specifying
265 .B CLONE_THREAD
266 automatically implies
267 .BR CLONE_VM ,
268 and specifying
269 .B CLONE_VM
270 automatically implies
271 .BR CLONE_SIGHAND .)
272 .\" As at 3.9, the following forced implications also apply,
273 .\" although the relevant flags are not yet implemented.
274 .\" If CLONE_THREAD is set force CLONE_VM.
275 .\" If CLONE_VM is set, force CLONE_SIGHAND.
276 .\"
277 If the process is multithreaded, then
278 the use of these flags results in an error.
279 .\" See kernel/fork.c::check_unshare_flags()
280 .P
281 If
282 .I flags
283 is specified as zero, then
284 .BR unshare ()
285 is a no-op;
286 no changes are made to the calling process's execution context.
287 .SH RETURN VALUE
288 On success, zero returned.
289 On failure, \-1 is returned and
290 .I errno
291 is set to indicate the error.
292 .SH ERRORS
293 .TP
294 .B EINVAL
295 An invalid bit was specified in
296 .IR flags .
297 .TP
298 .B EINVAL
299 .BR CLONE_THREAD ,
300 .BR CLONE_SIGHAND ,
301 or
302 .B CLONE_VM
303 was specified in
304 .IR flags ,
305 and the caller is multithreaded.
306 .TP
307 .B EINVAL
308 .B CLONE_NEWIPC
309 was specified in
310 .IR flags ,
311 but the kernel was not configured with the
312 .B CONFIG_SYSVIPC
313 and
314 .B CONFIG_IPC_NS
315 options.
316 .TP
317 .B EINVAL
318 .B CLONE_NEWNET
319 was specified in
320 .IR flags ,
321 but the kernel was not configured with the
322 .B CONFIG_NET_NS
323 option.
324 .TP
325 .B EINVAL
326 .B CLONE_NEWPID
327 was specified in
328 .IR flags ,
329 but the kernel was not configured with the
330 .B CONFIG_PID_NS
331 option.
332 .TP
333 .B EINVAL
334 .B CLONE_NEWUSER
335 was specified in
336 .IR flags ,
337 but the kernel was not configured with the
338 .B CONFIG_USER_NS
339 option.
340 .TP
341 .B EINVAL
342 .B CLONE_NEWUTS
343 was specified in
344 .IR flags ,
345 but the kernel was not configured with the
346 .B CONFIG_UTS_NS
347 option.
348 .TP
349 .B EINVAL
350 .B CLONE_NEWPID
351 was specified in
352 .IR flags ,
353 but the process has previously called
354 .BR unshare ()
355 with the
356 .B CLONE_NEWPID
357 flag.
358 .TP
359 .B ENOMEM
360 Cannot allocate sufficient memory to copy parts of caller's
361 context that need to be unshared.
362 .TP
363 .BR ENOSPC " (since Linux 3.7)"
364 .\" commit f2302505775fd13ba93f034206f1e2a587017929
365 .B CLONE_NEWPID
366 was specified in flags,
367 but the limit on the nesting depth of PID namespaces
368 would have been exceeded; see
369 .BR pid_namespaces (7).
370 .TP
371 .BR ENOSPC " (since Linux 4.9; beforehand " EUSERS )
372 .B CLONE_NEWUSER
373 was specified in
374 .IR flags ,
375 and the call would cause the limit on the number of
376 nested user namespaces to be exceeded.
377 See
378 .BR user_namespaces (7).
379 .IP
380 From Linux 3.11 to Linux 4.8, the error diagnosed in this case was
381 .BR EUSERS .
382 .TP
383 .BR ENOSPC " (since Linux 4.9)"
384 One of the values in
385 .I flags
386 specified the creation of a new user namespace,
387 but doing so would have caused the limit defined by the corresponding file in
388 .I /proc/sys/user
389 to be exceeded.
390 For further details, see
391 .BR namespaces (7).
392 .TP
393 .B EPERM
394 The calling process did not have the required privileges for this operation.
395 .TP
396 .B EPERM
397 .B CLONE_NEWUSER
398 was specified in
399 .IR flags ,
400 but either the effective user ID or the effective group ID of the caller
401 does not have a mapping in the parent namespace (see
402 .BR user_namespaces (7)).
403 .TP
404 .BR EPERM " (since Linux 3.9)"
405 .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d
406 .B CLONE_NEWUSER
407 was specified in
408 .I flags
409 and the caller is in a chroot environment
410 .\" FIXME What is the rationale for this restriction?
411 (i.e., the caller's root directory does not match the root directory
412 of the mount namespace in which it resides).
413 .TP
414 .BR EUSERS " (from Linux 3.11 to Linux 4.8)"
415 .B CLONE_NEWUSER
416 was specified in
417 .IR flags ,
418 and the limit on the number of nested user namespaces would be exceeded.
419 See the discussion of the
420 .B ENOSPC
421 error above.
422 .SH STANDARDS
423 Linux.
424 .SH HISTORY
425 Linux 2.6.16.
426 .SH NOTES
427 Not all of the process attributes that can be shared when
428 a new process is created using
429 .BR clone (2)
430 can be unshared using
431 .BR unshare ().
432 In particular, as at kernel 3.8,
433 .\" FIXME all of the following needs to be reviewed for the current kernel
434 .BR unshare ()
435 does not implement flags that reverse the effects of
436 .BR CLONE_SIGHAND ,
437 .\" However, we can do unshare(CLONE_SIGHAND) if CLONE_SIGHAND
438 .\" was not specified when doing clone(); i.e., unsharing
439 .\" signal handlers is permitted if we are not actually
440 .\" sharing signal handlers. mtk
441 .BR CLONE_THREAD ,
442 or
443 .BR CLONE_VM .
444 .\" However, we can do unshare(CLONE_VM) if CLONE_VM
445 .\" was not specified when doing clone(); i.e., unsharing
446 .\" virtual memory is permitted if we are not actually
447 .\" sharing virtual memory. mtk
448 Such functionality may be added in the future, if required.
449 .\"
450 .\"9) Future Work
451 .\"--------------
452 .\"The current implementation of unshare does not allow unsharing of
453 .\"signals and signal handlers. Signals are complex to begin with and
454 .\"to unshare signals and/or signal handlers of a currently running
455 .\"process is even more complex. If in the future there is a specific
456 .\"need to allow unsharing of signals and/or signal handlers, it can
457 .\"be incrementally added to unshare without affecting legacy
458 .\"applications using unshare.
459 .\"
460 .P
461 Creating all kinds of namespace, except user namespaces, requires the
462 .B CAP_SYS_ADMIN
463 capability.
464 However, since creating a user namespace automatically confers a full set of
465 capabilities,
466 creating both a user namespace and any other type of namespace in the same
467 .BR unshare ()
468 call does not require the
469 .B CAP_SYS_ADMIN
470 capability in the original namespace.
471 .SH EXAMPLES
472 The program below provides a simple implementation of the
473 .BR unshare (1)
474 command, which unshares one or more namespaces and executes the
475 command supplied in its command-line arguments.
476 Here's an example of the use of this program,
477 running a shell in a new mount namespace,
478 and verifying that the original shell and the
479 new shell are in separate mount namespaces:
480 .P
481 .in +4n
482 .EX
483 $ \fBreadlink /proc/$$/ns/mnt\fP
484 mnt:[4026531840]
485 $ \fBsudo ./unshare \-m /bin/bash\fP
486 # \fBreadlink /proc/$$/ns/mnt\fP
487 mnt:[4026532325]
488 .EE
489 .in
490 .P
491 The differing output of the two
492 .BR readlink (1)
493 commands shows that the two shells are in different mount namespaces.
494 .SS Program source
495 \&
496 .\" SRC BEGIN (unshare.c)
497 .EX
498 /* unshare.c
499 \&
500 A simple implementation of the unshare(1) command: unshare
501 namespaces and execute a command.
502 */
503 #define _GNU_SOURCE
504 #include <err.h>
505 #include <sched.h>
506 #include <stdio.h>
507 #include <stdlib.h>
508 #include <unistd.h>
509 \&
510 static void
511 usage(char *pname)
512 {
513 fprintf(stderr, "Usage: %s [options] program [arg...]\[rs]n", pname);
514 fprintf(stderr, "Options can be:\[rs]n");
515 fprintf(stderr, " \-C unshare cgroup namespace\[rs]n");
516 fprintf(stderr, " \-i unshare IPC namespace\[rs]n");
517 fprintf(stderr, " \-m unshare mount namespace\[rs]n");
518 fprintf(stderr, " \-n unshare network namespace\[rs]n");
519 fprintf(stderr, " \-p unshare PID namespace\[rs]n");
520 fprintf(stderr, " \-t unshare time namespace\[rs]n");
521 fprintf(stderr, " \-u unshare UTS namespace\[rs]n");
522 fprintf(stderr, " \-U unshare user namespace\[rs]n");
523 exit(EXIT_FAILURE);
524 }
525 \&
526 int
527 main(int argc, char *argv[])
528 {
529 int flags, opt;
530 \&
531 flags = 0;
532 \&
533 while ((opt = getopt(argc, argv, "CimnptuU")) != \-1) {
534 switch (opt) {
535 case \[aq]C\[aq]: flags |= CLONE_NEWCGROUP; break;
536 case \[aq]i\[aq]: flags |= CLONE_NEWIPC; break;
537 case \[aq]m\[aq]: flags |= CLONE_NEWNS; break;
538 case \[aq]n\[aq]: flags |= CLONE_NEWNET; break;
539 case \[aq]p\[aq]: flags |= CLONE_NEWPID; break;
540 case \[aq]t\[aq]: flags |= CLONE_NEWTIME; break;
541 case \[aq]u\[aq]: flags |= CLONE_NEWUTS; break;
542 case \[aq]U\[aq]: flags |= CLONE_NEWUSER; break;
543 default: usage(argv[0]);
544 }
545 }
546 \&
547 if (optind >= argc)
548 usage(argv[0]);
549 \&
550 if (unshare(flags) == \-1)
551 err(EXIT_FAILURE, "unshare");
552 \&
553 execvp(argv[optind], &argv[optind]);
554 err(EXIT_FAILURE, "execvp");
555 }
556 .EE
557 .\" SRC END
558 .SH SEE ALSO
559 .BR unshare (1),
560 .BR clone (2),
561 .BR fork (2),
562 .BR kcmp (2),
563 .BR setns (2),
564 .BR vfork (2),
565 .BR namespaces (7)
566 .P
567 .I Documentation/userspace\-api/unshare.rst
568 in the Linux kernel source tree
569 .\" commit f504d47be5e8fa7ecf2bf660b18b42e6960c0eb2
570 (or
571 .I Documentation/unshare.txt
572 before Linux 4.12)