readv2: Note preadv2(..., RWF_NOWAIT) bug in BUGS section
[man-pages.git] / man2 / setns.2
blob535271eaecb25ea46f068cdf0d8ccf1ed63e9bee
1 .\" Copyright (C) 2011, Eric Biederman <ebiederm@xmission.com>
2 .\" and Copyright (C) 2011, 2012, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(GPLv2_ONELINE)
5 .\" Licensed under the GPLv2
6 .\" %%%LICENSE_END
7 .\"
8 .TH SETNS 2 2020-08-13 "Linux" "Linux Programmer's Manual"
9 .SH NAME
10 setns \- reassociate thread with a namespace
11 .SH SYNOPSIS
12 .nf
13 .BR "#define _GNU_SOURCE" "             /* See feature_test_macros(7) */"
14 .B #include <sched.h>
15 .PP
16 .BI "int setns(int " fd ", int " nstype );
17 .fi
18 .SH DESCRIPTION
19 The
20 .BR setns ()
21 system call allows the calling thread to move into different namespaces.
22 The
23 .I fd
24 argument is one of the following:
25 .IP \(bu 2
26 a file descriptor referring to one of the magic links in a
27 .I /proc/[pid]/ns/
28 directory (or a bind mount to such a link);
29 .IP \(bu
30 a PID file descriptor (see
31 .BR pidfd_open (2)).
32 .PP
33 The
34 .I nstype
35 argument is interpreted differently in each case.
36 .\"
37 .SS fd refers to a /proc/[pid]/ns/ link
39 .I fd
40 refers to a
41 .I /proc/[pid]/ns/
42 link, then
43 .BR setns ()
44 reassociates the calling thread with the namespace associated with that link,
45 subject to any constraints imposed by the
46 .I nstype
47 argument.
48 In this usage, each call to
49 .BR setns ()
50 changes just one of the caller's namespace memberships.
51 .PP
52 The
53 .I nstype
54 argument specifies which type of namespace
55 the calling thread may be reassociated with.
56 This argument can have
57 .I one
58 of the following values:
59 .TP
60 .BR 0
61 Allow any type of namespace to be joined.
62 .TP
63 .BR CLONE_NEWCGROUP " (since Linux 4.6)"
64 .I fd
65 must refer to a cgroup namespace.
66 .TP
67 .BR CLONE_NEWIPC " (since Linux 3.0)"
68 .I fd
69 must refer to an IPC namespace.
70 .TP
71 .BR CLONE_NEWNET " (since Linux 3.0)"
72 .I fd
73 must refer to a network namespace.
74 .TP
75 .BR CLONE_NEWNS " (since Linux 3.8)"
76 .I fd
77 must refer to a mount namespace.
78 .TP
79 .BR CLONE_NEWPID " (since Linux 3.8)"
80 .I fd
81 must refer to a descendant PID namespace.
82 .TP
83 .BR CLONE_NEWTIME " (since Linux 5.8)"
84 .\" commit 76c12881a38aaa83e1eb4ce2fada36c3a732bad4
85 .I fd
86 must refer to a time namespace.
87 .TP
88 .BR CLONE_NEWUSER " (since Linux 3.8)"
89 .I fd
90 must refer to a user namespace.
91 .TP
92 .BR CLONE_NEWUTS " (since Linux 3.0)"
93 .I fd
94 must refer to a UTS namespace.
95 .PP
96 Specifying
97 .I nstype
98 as 0 suffices if the caller knows (or does not care)
99 what type of namespace is referred to by
100 .IR fd .
101 Specifying a nonzero value for
102 .I nstype
103 is useful if the caller does not know what type of namespace is referred to by
104 .IR fd
105 and wants to ensure that the namespace is of a particular type.
106 (The caller might not know the type of the namespace referred to by
107 .IR fd
108 if the file descriptor was opened by another process and, for example,
109 passed to the caller via a UNIX domain socket.)
111 .SS fd is a PID file descriptor
112 Since Linux 5.8,
113 .I fd
114 may refer to a PID file descriptor obtained from
115 .BR pidfd_open (2)
117 .BR clone (2).
118 In this usage,
119 .BR setns ()
120 atomically moves the calling thread into one or more of the same namespaces
121 as the thread referred to by
122 .IR fd .
125 .IR nstype
126 argument is a bit mask specified by ORing together
127 .I "one or more"
128 of the
129 .BR CLONE_NEW*
130 namespace constants listed above.
131 The caller is moved into each of the target thread's namespaces
132 that is specified in
133 .IR nstype ;
134 the caller's memberships in the remaining namespaces are left unchanged.
136 For example, the following code would move the caller into the
137 same user, network, and UTS namespaces as PID 1234,
138 but would leave the caller's other namespace memberships unchanged:
140 .in +4n
142 int fd = pidfd_open(1234, 0);
143 setns(fd, CLONE_NEWUSER | CLONE_NEWNET | CLONE_NEWUTS);
147 .SS Details for specific namespace types
148 Note the following details and restrictions when reassociating with
149 specific namespace types:
151 User namespaces
152 A process reassociating itself with a user namespace must have the
153 .B CAP_SYS_ADMIN
154 .\" See kernel/user_namespace.c:userns_install() [3.8 source]
155 capability in the target user namespace.
156 (This necessarily implies that it is only possible to join
157 a descendant user namespace.)
158 Upon successfully joining a user namespace,
159 a process is granted all capabilities in that namespace,
160 regardless of its user and group IDs.
162 A multithreaded process may not change user namespace with
163 .BR setns ().
165 It is not permitted to use
166 .BR setns ()
167 to reenter the caller's current user namespace.
168 This prevents a caller that has dropped capabilities from regaining
169 those capabilities via a call to
170 .BR setns ().
172 For security reasons,
173 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
174 .\" https://lwn.net/Articles/543273/
175 a process can't join a new user namespace if it is sharing
176 filesystem-related attributes
177 (the attributes whose sharing is controlled by the
178 .BR clone (2)
179 .B CLONE_FS
180 flag) with another process.
182 For further details on user namespaces, see
183 .BR user_namespaces (7).
185 Mount namespaces
186 Changing the mount namespace requires that the caller possess both
187 .B CAP_SYS_CHROOT
189 .BR CAP_SYS_ADMIN
190 capabilities in its own user namespace and
191 .BR CAP_SYS_ADMIN
192 in the user namespace that owns the target mount namespace.
194 A process can't join a new mount namespace if it is sharing
195 filesystem-related attributes
196 (the attributes whose sharing is controlled by the
197 .BR clone (2)
198 .B CLONE_FS
199 flag) with another process.
200 .\" Above check is in fs/namespace.c:mntns_install() [3.8 source]
203 .BR user_namespaces (7)
204 for details on the interaction of user namespaces and mount namespaces.
206 PID namespaces
207 In order to reassociate itself with a new PID namespace,
208 the caller must have the
209 .B CAP_SYS_ADMIN
210 capability both in its own user namespace and in the user namespace
211 that owns the target PID namespace.
213 Reassociating the PID namespace has somewhat different
214 from other namespace types.
215 Reassociating the calling thread with a PID namespace changes only
216 the PID namespace that subsequently created child processes of
217 the caller will be placed in;
218 it does not change the PID namespace of the caller itself.
220 Reassociating with a PID namespace is allowed only if the target
221 PID namespace is a descendant (child, grandchild, etc.)
222 of, or is the same as, the current PID namespace of the caller.
224 For further details on PID namespaces, see
225 .BR pid_namespaces (7).
227 Cgroup namespaces
228 In order to reassociate itself with a new cgroup namespace,
229 the caller must have the
230 .B CAP_SYS_ADMIN
231 capability both in its own user namespace and in the user namespace
232 that owns the target cgroup namespace.
234 Using
235 .BR setns ()
236 to change the caller's cgroup namespace does not change
237 the caller's cgroup memberships.
239 Network, IPC, time, and UTS namespaces
240 In order to reassociate itself with a new network, IPC, time, or UTS namespace,
241 the caller must have the
242 .B CAP_SYS_ADMIN
243 capability both in its own user namespace and in the user namespace
244 that owns the target namespace.
245 .SH RETURN VALUE
246 On success,
247 .BR setns ()
248 returns 0.
249 On failure, \-1 is returned and
250 .I errno
251 is set to indicate the error.
252 .SH ERRORS
254 .B EBADF
255 .I fd
256 is not a valid file descriptor.
258 .B EINVAL
259 .I fd
260 refers to a namespace whose type does not match that specified in
261 .IR nstype .
263 .B EINVAL
264 There is problem with reassociating
265 the thread with the specified namespace.
267 .\" See kernel/pid_namespace.c::pidns_install() [kernel 3.18 sources]
268 .B EINVAL
269 The caller tried to join an ancestor (parent, grandparent, and so on)
270 PID namespace.
272 .B EINVAL
273 The caller attempted to join the user namespace
274 in which it is already a member.
276 .B EINVAL
277 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
278 The caller shares filesystem
279 .RB ( CLONE_FS )
280 state (in particular, the root directory)
281 with other processes and tried to join a new user namespace.
283 .B EINVAL
284 .\" See kernel/user_namespace.c::userns_install() [kernel 3.15 sources]
285 The caller is multithreaded and tried to join a new user namespace.
287 .B EINVAL
288 .I fd
289 is a PID file descriptor and
290 .I nstype
291 is invalid (e.g., it is 0).
293 .B ENOMEM
294 Cannot allocate sufficient memory to change the specified namespace.
296 .B EPERM
297 The calling thread did not have the required capability
298 for this operation.
300 .B ESRCH
301 .I fd
302 is a PID file descriptor but the process it refers to no longer exists
303 (i.e., it has terminated and been waited on).
304 .SH VERSIONS
306 .BR setns ()
307 system call first appeared in Linux in kernel 3.0;
308 library support was added to glibc in version 2.14.
309 .SH CONFORMING TO
311 .BR setns ()
312 system call is Linux-specific.
313 .SH NOTES
314 For further information on the
315 .IR /proc/[pid]/ns/
316 magic links, see
317 .BR namespaces (7).
319 Not all of the attributes that can be shared when
320 a new thread is created using
321 .BR clone (2)
322 can be changed using
323 .BR setns ().
324 .SH EXAMPLES
325 The program below takes two or more arguments.
326 The first argument specifies the pathname of a namespace file in an existing
327 .I /proc/[pid]/ns/
328 directory.
329 The remaining arguments specify a command and its arguments.
330 The program opens the namespace file, joins that namespace using
331 .BR setns (),
332 and executes the specified command inside that namespace.
334 The following shell session demonstrates the use of this program
335 (compiled as a binary named
336 .IR ns_exec )
337 in conjunction with the
338 .BR CLONE_NEWUTS
339 example program in the
340 .BR clone (2)
341 man page (complied as a binary named
342 .IR newuts ).
344 We begin by executing the example program in
345 .BR clone (2)
346 in the background.
347 That program creates a child in a separate UTS namespace.
348 The child changes the hostname in its namespace,
349 and then both processes display the hostnames in their UTS namespaces,
350 so that we can see that they are different.
352 .in +4n
354 $ \fBsu\fP                   # Need privilege for namespace operations
355 Password:
356 # \fB./newuts bizarro &\fP
357 [1] 3549
358 clone() returned 3550
359 uts.nodename in child:  bizarro
360 uts.nodename in parent: antero
361 # \fBuname \-n\fP             # Verify hostname in the shell
362 antero
366 We then run the program shown below,
367 using it to execute a shell.
368 Inside that shell, we verify that the hostname is the one
369 set by the child created by the first program:
371 .in +4n
373 # \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP
374 # \fBuname \-n\fP             # Executed in shell started by ns_exec
375 bizarro
378 .SS Program source
380 #define _GNU_SOURCE
381 #include <fcntl.h>
382 #include <sched.h>
383 #include <unistd.h>
384 #include <stdlib.h>
385 #include <stdio.h>
387 #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \e
388                         } while (0)
391 main(int argc, char *argv[])
393     int fd;
395     if (argc < 3) {
396         fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\en", argv[0]);
397         exit(EXIT_FAILURE);
398     }
400     /* Get file descriptor for namespace; the file descriptor is opened
401        with O_CLOEXEC so as to ensure that it is not inherited by the
402        program that is later executed. */
404     fd = open(argv[1], O_RDONLY | O_CLOEXEC);
405     if (fd == \-1)
406         errExit("open");
408     if (setns(fd, 0) == \-1)       /* Join that namespace */
409         errExit("setns");
411     execvp(argv[2], &argv[2]);    /* Execute a command in namespace */
412     errExit("execvp");
415 .SH SEE ALSO
416 .BR nsenter (1),
417 .BR clone (2),
418 .BR fork (2),
419 .BR unshare (2),
420 .BR vfork (2),
421 .BR namespaces (7),
422 .BR unix (7)