]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/unshare.2
unshare.2: Document use of CLONE_THREAD, CLONE_SIGHAND, and CLONE_VM
[thirdparty/man-pages.git] / man2 / unshare.2
1 .\" Copyright (C) 2006, Janak Desai <janak@us.ibm.com>
2 .\" and Copyright (C) 2006, 2012 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
5 .\" Licensed under the GPL
6 .\" %%%LICENSE_END
7 .\"
8 .\" Patch Justification:
9 .\" unshare system call is needed to implement, using PAM,
10 .\" per-security_context and/or per-user namespace to provide
11 .\" polyinstantiated directories. Using unshare and bind mounts, a
12 .\" PAM module can create private namespace with appropriate
13 .\" directories(based on user's security context) bind mounted on
14 .\" public directories such as /tmp, thus providing an instance of
15 .\" /tmp that is based on user's security context. Without the
16 .\" unshare system call, namespace separation can only be achieved
17 .\" by clone, which would require porting and maintaining all commands
18 .\" such as login, and su, that establish a user session.
19 .\"
20 .TH UNSHARE 2 2013-04-17 "Linux" "Linux Programmer's Manual"
21 .SH NAME
22 unshare \- disassociate parts of the process execution context
23 .SH SYNOPSIS
24 .nf
25 .B #include <sched.h>
26 .sp
27 .BI "int unshare(int " flags );
28 .fi
29 .sp
30 .in -4n
31 Feature Test Macro Requirements for glibc (see
32 .BR feature_test_macros (7)):
33 .in
34 .sp
35 .BR unshare ():
36 .ad l
37 .RS 4
38 .PD 0
39 .TP 4
40 Since glibc 2.14:
41 _GNU_SOURCE
42 .TP 4
43 .\" See http://sources.redhat.com/bugzilla/show_bug.cgi?id=4749
44 Before glibc 2.14:
45 _BSD_SOURCE || _SVID_SOURCE
46 /* _GNU_SOURCE also suffices */
47 .PD
48 .RE
49 .ad b
50 .SH DESCRIPTION
51 .BR unshare ()
52 allows a process to disassociate parts of its execution
53 context that are currently being shared with other processes.
54 Part of the execution context, such as the mount namespace, is shared
55 implicitly when a new process is created using
56 .BR fork (2)
57 or
58 .BR vfork (2),
59 while other parts, such as virtual memory, may be
60 shared by explicit request when creating a process using
61 .BR clone (2).
62
63 The main use of
64 .BR unshare ()
65 is to allow a process to control its
66 shared execution context without creating a new process.
67
68 The
69 .I flags
70 argument is a bit mask that specifies which parts of
71 the execution context should be unshared.
72 This argument is specified by ORing together zero or more
73 of the following constants:
74 .TP
75 .B CLONE_FILES
76 Reverse the effect of the
77 .BR clone (2)
78 .B CLONE_FILES
79 flag.
80 Unshare the file descriptor table, so that the calling process
81 no longer shares its file descriptors with any other process.
82 .TP
83 .B CLONE_FS
84 Reverse the effect of the
85 .BR clone (2)
86 .B CLONE_FS
87 flag.
88 Unshare filesystem attributes, so that the calling process
89 no longer shares its root directory
90 .RB ( chroot (2)),
91 current directory
92 .RB ( chdir (2)),
93 or umask
94 .RB ( umask (2))
95 attributes with any other process.
96 .TP
97 .BR CLONE_NEWIPC " (since Linux 2.6.19)
98 This flag has the same effect as the
99 .BR clone (2)
100 .B CLONE_NEWIPC
101 flag.
102 Unshare the System\ V IPC namespace,
103 so that the calling process has a private copy of the
104 System\ V IPC namespace which is not shared with any other process.
105 Specifying this flag automatically implies
106 .BR CLONE_SYSVSEM
107 as well.
108 Use of
109 .BR CLONE_NEWIPC
110 requires the
111 .BR CAP_SYS_ADMIN
112 capability.
113 .TP
114 .BR CLONE_NEWNET " (since Linux 2.6.24)
115 This flag has the same effect as the
116 .BR clone (2)
117 .B CLONE_NEWNET
118 flag.
119 Unshare the network namespace,
120 so that the calling process is moved into a
121 new network namespace which is not shared
122 with any previously existing process.
123 Use of
124 .BR CLONE_NEWNET
125 requires the
126 .BR CAP_SYS_ADMIN
127 capability.
128 .TP
129 .B CLONE_NEWNS
130 .\" These flag name are inconsistent:
131 .\" CLONE_NEWNS does the same thing in clone(), but CLONE_VM,
132 .\" CLONE_FS, and CLONE_FILES reverse the action of the clone()
133 .\" flags of the same name.
134 This flag has the same effect as the
135 .BR clone (2)
136 .B CLONE_NEWNS
137 flag.
138 Unshare the mount namespace,
139 so that the calling process has a private copy of
140 its namespace which is not shared with any other process.
141 Specifying this flag automatically implies
142 .B CLONE_FS
143 as well.
144 Use of
145 .BR CLONE_NEWNS
146 requires the
147 .BR CAP_SYS_ADMIN
148 capability.
149 .TP
150 .BR CLONE_NEWPID " (since Linux 3.8)"
151 This flag has the same effect as the
152 .BR clone (2)
153 .B CLONE_NEWPID
154 flag.
155 Unshare the PID namespace,
156 so that the calling process has a new PID namespace for its children
157 which is not shared with any previously existing process.
158 The calling process is
159 .I not
160 moved into the new namespace.
161 The first child created by the calling process will have
162 the process ID 1 and will assume the role of
163 .BR init (1)
164 in the new namespace.
165 Use of
166 .BR CLONE_NEWPID
167 requires the
168 .BR CAP_SYS_ADMIN
169 capability.
170 For further information, see
171 .BR pid_namespaces (7).
172 .TP
173 .BR CLONE_NEWUSER " (since Linux 3.8)"
174 This flag has the same effect as the
175 .BR clone (2)
176 .B CLONE_NEWUSER
177 flag.
178 Unshare the user namespace,
179 so that the calling process is moved into a new user namespace
180 which is not shared with any previously existing process.
181 like the child process created by
182 .BR clone (2)
183 with the
184 .B CLONE_NEWUSER
185 flag, the caller obtains a full set of capabilities in the new namespace.
186 .BR CLONE_NEWUSER
187 requires that the calling process is not threaded and the user ID and group ID
188 of the calling process are mapped user IDs and group IDs in the
189 user namespace of the calling process at the time of the call.
190 .TP
191 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
192 This flag has the same effect as the
193 .BR clone (2)
194 .B CLONE_NEWUTS
195 flag.
196 Unshare the UTS IPC namespace,
197 so that the calling process has a private copy of the
198 UTS namespace which is not shared with any other process.
199 Use of
200 .BR CLONE_NEWUTS
201 requires the
202 .BR CAP_SYS_ADMIN
203 capability.
204 .TP
205 .BR CLONE_SYSVSEM " (since Linux 2.6.26)
206 .\" commit 9edff4ab1f8d82675277a04e359d0ed8bf14a7b7
207 This flag reverses the effect of the
208 .BR clone (2)
209 .B CLONE_SYSVSEM
210 flag.
211 Unshare System\ V semaphore undo values,
212 so that the calling process has a private copy
213 which is not shared with any other process.
214 Use of
215 .BR CLONE_SYSVSEM
216 requires the
217 .BR CAP_SYS_ADMIN
218 capability.
219 .\" As at 2.6.16, the following forced implications also apply,
220 .\" although the relevant flags are not yet implemented.
221 .\" If CLONE_THREAD is set force CLONE_VM.
222 .\" If CLONE_VM is set, force CLONE_SIGHAND.
223 .\" CLONE_NEWNS If CLONE_SIGHAND is set and signals are also being shared
224 .\" (i.e., current->signal->count > 1), force CLONE_THREAD.
225 .\"
226 .\" FIXME . CLONE_VM is not (yet, as at 2.6.16) implemented.
227 .\" .TP
228 .\" .B CLONE_VM
229 .\" Reverse the effect of the
230 .\" .BR clone (2)
231 .\" .B CLONE_VM
232 .\" flag.
233 .\" .RB ( CLONE_VM
234 .\" is also implicitly set by
235 .\" .BR vfork (2),
236 .\" and can be reversed using this
237 .\" .BR unshare ()
238 .\" flag.)
239 .\" Unshare virtual memory, so that the calling process no
240 .\" longer shares its virtual address space with any other process.
241 .PP
242 In addition,
243 .BR CLONE_THREAD ,
244 .BR CLONE_SIGHAND ,
245 and
246 .BR CLONE_VM
247 can be specified in
248 .I flags
249 if the caller is single threaded (i.e., it is not sharing
250 its address space with another process or thread).
251 In this case, these flags have no effect.
252 If the process is multithreaded, then
253 the use of these flags results in an error.
254 .\" See kernel/fork.c::check_unshare_flags()
255 .PP
256 If
257 .I flags
258 is specified as zero, then
259 .BR unshare ()
260 is a no-op;
261 no changes are made to the calling process's execution context.
262 .SH RETURN VALUE
263 On success, zero returned.
264 On failure, \-1 is returned and
265 .I errno
266 is set to indicate the error.
267 .SH ERRORS
268 .TP
269 .B EINVAL
270 An invalid bit was specified in
271 .IR flags .
272 .TP
273 .B EINVAL
274 .BR CLONE_THREAD ,
275 .BR CLONE_SIGHAND ,
276 or
277 .BR CLONE_VM
278 was specified in
279 .IR flags ,
280 and the caller is multithreaded.
281 .TP
282 .B ENOMEM
283 Cannot allocate sufficient memory to copy parts of caller's
284 context that need to be unshared.
285 .TP
286 .B EPERM
287 The calling process did not have the required privileges for this operation.
288 .TP
289 .B EPERM
290 .BR CLONE_NEWUSER
291 was specified in
292 .IR flags ,
293 but either the effective user ID or the effective group ID of the caller
294 does not have a mapping in the parent namespace (see
295 .BR user_namespaces (7)).
296 .SH VERSIONS
297 The
298 .BR unshare ()
299 system call was added to Linux in kernel 2.6.16.
300 .SH CONFORMING TO
301 The
302 .BR unshare ()
303 system call is Linux-specific.
304 .SH NOTES
305 Not all of the process attributes that can be shared when
306 a new process is created using
307 .BR clone (2)
308 can be unshared using
309 .BR unshare ().
310 In particular, as at kernel 3.8,
311 .\" FIXME all of the following needs to be reviewed for the current kernel
312 .BR unshare ()
313 does not implement flags that reverse the effects of
314 .BR CLONE_SIGHAND ,
315 .\" However, we can do unshare(CLONE_SIGHAND) if CLONE_SIGHAND
316 .\" was not specified when doing clone(); i.e., unsharing
317 .\" signal handlers is permitted if we are not actually
318 .\" sharing signal handlers. mtk
319 .BR CLONE_THREAD ,
320 or
321 .BR CLONE_VM .
322 .\" However, we can do unshare(CLONE_VM) if CLONE_VM
323 .\" was not specified when doing clone(); i.e., unsharing
324 .\" virtual memory is permitted if we are not actually
325 .\" sharing virtual memory. mtk
326 Such functionality may be added in the future, if required.
327 .\"
328 .\"9) Future Work
329 .\"--------------
330 .\"The current implementation of unshare does not allow unsharing of
331 .\"signals and signal handlers. Signals are complex to begin with and
332 .\"to unshare signals and/or signal handlers of a currently running
333 .\"process is even more complex. If in the future there is a specific
334 .\"need to allow unsharing of signals and/or signal handlers, it can
335 .\"be incrementally added to unshare without affecting legacy
336 .\"applications using unshare.
337 .\"
338 .SH EXAMPLE
339 The program below provides a simple implementation of the
340 .BR unshare (1)
341 command, which unshares one or more namespaces and executes the
342 command supplied in its command line arguments.
343 Here's an example of the use of this program,
344 running a shell in a new mount namespace,
345 and verifying that the original shell and the
346 new shell are in separate mount namespaces:
347 .in +4n
348 .nf
349
350 $ \fBreadlink /proc/$$/ns/mnt\fP
351 mnt:[4026531840]
352 $ \fBsudo ./unshare -m /bin/bash\fP
353 [sudo] password for cecilia:
354 # \fBreadlink /proc/$$/ns/mnt\fP
355 mnt:[4026532325]
356 .fi
357 .in
358
359 The differing output of the two
360 .BR readlink (1)
361 commands shows that the two shells are in different mount namespaces.
362 .SS Program source
363 \&
364 .nf
365 /* unshare.c
366
367 A simple implementation of the unshare(1) command: unshare
368 namespaces and execute a command.
369 */
370 #define _GNU_SOURCE
371 #include <sched.h>
372 #include <unistd.h>
373 #include <stdlib.h>
374 #include <stdio.h>
375
376 /* A simple error\-handling function: print an error message based
377 on the value in \(aqerrno\(aq and terminate the calling process */
378
379 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
380 } while (0)
381
382 static void
383 usage(char *pname)
384 {
385 fprintf(stderr, "Usage: %s [options] program [arg...]\\n", pname);
386 fprintf(stderr, "Options can be:\\n");
387 fprintf(stderr, " \-i unshare IPC namespace\\n");
388 fprintf(stderr, " \-m unshare mount namespace\\n");
389 fprintf(stderr, " \-n unshare network namespace\\n");
390 fprintf(stderr, " \-p unshare PID namespace\\n");
391 fprintf(stderr, " \-u unshare UTS namespace\\n");
392 fprintf(stderr, " \-U unshare user namespace\\n");
393 exit(EXIT_FAILURE);
394 }
395
396 int
397 main(int argc, char *argv[])
398 {
399 int flags, opt;
400
401 flags = 0;
402
403 while ((opt = getopt(argc, argv, "imnpuU")) != \-1) {
404 switch (opt) {
405 case \(aqi\(aq: flags |= CLONE_NEWIPC; break;
406 case \(aqm\(aq: flags |= CLONE_NEWNS; break;
407 case \(aqn\(aq: flags |= CLONE_NEWNET; break;
408 case \(aqp\(aq: flags |= CLONE_NEWPID; break;
409 case \(aqu\(aq: flags |= CLONE_NEWUTS; break;
410 case \(aqU\(aq: flags |= CLONE_NEWUSER; break;
411 default: usage(argv[0]);
412 }
413 }
414
415 if (optind >= argc)
416 usage(argv[0]);
417
418 if (unshare(flags) == \-1)
419 errExit("unshare");
420
421 execvp(argv[optind], &argv[optind]);
422 errExit("execvp");
423 }
424 .fi
425 .SH SEE ALSO
426 .BR unshare (1),
427 .BR clone (2),
428 .BR fork (2),
429 .BR kcmp (2),
430 .BR setns (2),
431 .BR vfork (2),
432 .BR namespaces (7)
433
434 .I Documentation/unshare.txt
435 in the Linux kernel source tree