landlock_restrict_self.2: tfix
[man-pages.git] / man2 / unshare.2
blobc7d0b0560f16df3aa506ed77ae0c1bac005acd4c
1 .\" Copyright (C) 2006, Janak Desai <janak@us.ibm.com>
2 .\" and Copyright (C) 2006, 2012 Michael Kerrisk <mtk.manpages@gmail.com>
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 2021-03-22 "Linux" "Linux Programmer's Manual"
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 .PP
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)
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 .PP
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 .PP
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.
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.
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).
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).
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).
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.
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.
217 For further information on user namespaces, see
218 .BR user_namespaces (7).
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.
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.
254 In addition,
255 .BR CLONE_THREAD ,
256 .BR CLONE_SIGHAND ,
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.
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()
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
294 .B EINVAL
295 An invalid bit was specified in
296 .IR flags .
298 .B EINVAL
299 .BR CLONE_THREAD ,
300 .BR CLONE_SIGHAND ,
302 .B CLONE_VM
303 was specified in
304 .IR flags ,
305 and the caller is multithreaded.
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
314 .B CONFIG_IPC_NS
315 options.
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.
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.
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.
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.
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.
359 .B ENOMEM
360 Cannot allocate sufficient memory to copy parts of caller's
361 context that need to be unshared.
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).
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.
378 .BR user_namespaces (7).
380 From Linux 3.11 to Linux 4.8, the error diagnosed in this case was
381 .BR EUSERS .
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).
393 .B EPERM
394 The calling process did not have the required privileges for this operation.
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)).
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).
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 VERSIONS
424 .BR unshare ()
425 system call was added to Linux in kernel 2.6.16.
426 .SH STANDARDS
428 .BR unshare ()
429 system call is Linux-specific.
430 .SH NOTES
431 Not all of the process attributes that can be shared when
432 a new process is created using
433 .BR clone (2)
434 can be unshared using
435 .BR unshare ().
436 In particular, as at kernel 3.8,
437 .\" FIXME all of the following needs to be reviewed for the current kernel
438 .BR unshare ()
439 does not implement flags that reverse the effects of
440 .BR CLONE_SIGHAND ,
441 .\" However, we can do unshare(CLONE_SIGHAND) if CLONE_SIGHAND
442 .\" was not specified when doing clone(); i.e., unsharing
443 .\" signal handlers is permitted if we are not actually
444 .\" sharing signal handlers.   mtk
445 .BR CLONE_THREAD ,
447 .BR CLONE_VM .
448 .\" However, we can do unshare(CLONE_VM) if CLONE_VM
449 .\" was not specified when doing clone(); i.e., unsharing
450 .\" virtual memory is permitted if we are not actually
451 .\" sharing virtual memory.   mtk
452 Such functionality may be added in the future, if required.
454 .\"9) Future Work
455 .\"--------------
456 .\"The current implementation of unshare does not allow unsharing of
457 .\"signals and signal handlers. Signals are complex to begin with and
458 .\"to unshare signals and/or signal handlers of a currently running
459 .\"process is even more complex. If in the future there is a specific
460 .\"need to allow unsharing of signals and/or signal handlers, it can
461 .\"be incrementally added to unshare without affecting legacy
462 .\"applications using unshare.
464 .SH EXAMPLES
465 The program below provides a simple implementation of the
466 .BR unshare (1)
467 command, which unshares one or more namespaces and executes the
468 command supplied in its command-line arguments.
469 Here's an example of the use of this program,
470 running a shell in a new mount namespace,
471 and verifying that the original shell and the
472 new shell are in separate mount namespaces:
474 .in +4n
476 $ \fBreadlink /proc/$$/ns/mnt\fP
477 mnt:[4026531840]
478 $ \fBsudo ./unshare \-m /bin/bash\fP
479 # \fBreadlink /proc/$$/ns/mnt\fP
480 mnt:[4026532325]
484 The differing output of the two
485 .BR readlink (1)
486 commands shows that the two shells are in different mount namespaces.
487 .SS Program source
489 .\" SRC BEGIN (unshare.c)
491 /* unshare.c
493    A simple implementation of the unshare(1) command: unshare
494    namespaces and execute a command.
496 #define _GNU_SOURCE
497 #include <sched.h>
498 #include <stdio.h>
499 #include <stdlib.h>
500 #include <unistd.h>
502 /* A simple error\-handling function: print an error message based
503    on the value in \(aqerrno\(aq and terminate the calling process. */
505 #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \e
506                         } while (0)
508 static void
509 usage(char *pname)
511     fprintf(stderr, "Usage: %s [options] program [arg...]\en", pname);
512     fprintf(stderr, "Options can be:\en");
513     fprintf(stderr, "    \-C   unshare cgroup namespace\en");
514     fprintf(stderr, "    \-i   unshare IPC namespace\en");
515     fprintf(stderr, "    \-m   unshare mount namespace\en");
516     fprintf(stderr, "    \-n   unshare network namespace\en");
517     fprintf(stderr, "    \-p   unshare PID namespace\en");
518     fprintf(stderr, "    \-t   unshare time namespace\en");
519     fprintf(stderr, "    \-u   unshare UTS namespace\en");
520     fprintf(stderr, "    \-U   unshare user namespace\en");
521     exit(EXIT_FAILURE);
525 main(int argc, char *argv[])
527     int flags, opt;
529     flags = 0;
531     while ((opt = getopt(argc, argv, "CimnptuU")) != \-1) {
532         switch (opt) {
533         case \(aqC\(aq: flags |= CLONE_NEWCGROUP;      break;
534         case \(aqi\(aq: flags |= CLONE_NEWIPC;        break;
535         case \(aqm\(aq: flags |= CLONE_NEWNS;         break;
536         case \(aqn\(aq: flags |= CLONE_NEWNET;        break;
537         case \(aqp\(aq: flags |= CLONE_NEWPID;        break;
538         case \(aqt\(aq: flags |= CLONE_NEWTIME;        break;
539         case \(aqu\(aq: flags |= CLONE_NEWUTS;        break;
540         case \(aqU\(aq: flags |= CLONE_NEWUSER;       break;
541         default:  usage(argv[0]);
542         }
543     }
545     if (optind >= argc)
546         usage(argv[0]);
548     if (unshare(flags) == \-1)
549         errExit("unshare");
551     execvp(argv[optind], &argv[optind]);
552     errExit("execvp");
555 .\" SRC END
556 .SH SEE ALSO
557 .BR unshare (1),
558 .BR clone (2),
559 .BR fork (2),
560 .BR kcmp (2),
561 .BR setns (2),
562 .BR vfork (2),
563 .BR namespaces (7)
565 .I Documentation/userspace\-api/unshare.rst
566 in the Linux kernel source tree
567 .\" commit f504d47be5e8fa7ecf2bf660b18b42e6960c0eb2
569 .I Documentation/unshare.txt
570 before Linux 4.12)