Debian Codegraph @

upstream/debian-trixie/man2/setns.2

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