Debian Codegraph @

upstream/fedora-44/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 2025-01-07 "Linux man-pages 6.13"
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 Time namespaces
241 In order to reassociate itself with a new time 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 .IP
247 A multithreaded process may not change time namespace with
248 .BR setns ().
249 .TP
250 Network, IPC, and UTS namespaces
251 In order to reassociate itself with a new network, IPC, time, or UTS namespace,
252 the caller must have the
253 .B CAP_SYS_ADMIN
254 capability both in its own user namespace and in the user namespace
255 that owns the target namespace.
256 .SH RETURN VALUE
257 On success,
258 .BR setns ()
259 returns 0.
260 On failure, \-1 is returned and
261 .I errno
262 is set to indicate the error.
263 .SH ERRORS
264 .TP
265 .B EBADF
266 .I fd
267 is not a valid file descriptor.
268 .TP
269 .B EINVAL
270 .I fd
271 refers to a namespace whose type does not match that specified in
272 .IR nstype .
273 .TP
274 .B EINVAL
275 There is problem with reassociating
276 the thread with the specified namespace.
277 .TP
278 .\" See kernel/pid_namespace.c::pidns_install() [kernel 3.18 sources]
279 .B EINVAL
280 The caller tried to join an ancestor (parent, grandparent, and so on)
281 PID namespace.
282 .TP
283 .B EINVAL
284 The caller attempted to join the user namespace
285 in which it is already a member.
286 .TP
287 .B EINVAL
288 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
289 The caller shares filesystem
290 .RB ( CLONE_FS )
291 state (in particular, the root directory)
292 with other processes and tried to join a new user namespace.
293 .TP
294 .B EINVAL
295 .\" See kernel/user_namespace.c::userns_install() [kernel 3.15 sources]
296 The caller is multithreaded and tried to join a new user namespace.
297 .TP
298 .B EINVAL
299 .I fd
300 is a PID file descriptor and
301 .I nstype
302 is invalid (e.g., it is 0).
303 .TP
304 .B ENOMEM
305 Cannot allocate sufficient memory to change the specified namespace.
306 .TP
307 .B EPERM
308 The calling thread did not have the required capability
309 for this operation.
310 .TP
311 .B ESRCH
312 .I fd
313 is a PID file descriptor but the process it refers to no longer exists
314 (i.e., it has terminated and been waited on).
315 .SH STANDARDS
316 Linux.
317 .SH VERSIONS
318 Linux 3.0,
319 glibc 2.14.
320 .SH NOTES
321 For further information on the
322 .IR /proc/ pid /ns/
323 magic links, see
324 .BR namespaces (7).
325 .P
326 Not all of the attributes that can be shared when
327 a new thread is created using
328 .BR clone (2)
329 can be changed using
330 .BR setns ().
331 .SH EXAMPLES
332 The program below takes two or more arguments.
333 The first argument specifies the pathname of a namespace file in an existing
334 .IR /proc/ pid /ns/
335 directory.
336 The remaining arguments specify a command and its arguments.
337 The program opens the namespace file, joins that namespace using
338 .BR setns (),
339 and executes the specified command inside that namespace.
340 .P
341 The following shell session demonstrates the use of this program
342 (compiled as a binary named
343 .IR ns_exec )
344 in conjunction with the
345 .B CLONE_NEWUTS
346 example program in the
347 .BR clone (2)
348 man page (complied as a binary named
349 .IR newuts ).
350 .P
351 We begin by executing the example program in
352 .BR clone (2)
353 in the background.
354 That program creates a child in a separate UTS namespace.
355 The child changes the hostname in its namespace,
356 and then both processes display the hostnames in their UTS namespaces,
357 so that we can see that they are different.
358 .P
359 .in +4n
360 .EX
361 $ \fBsu\fP # Need privilege for namespace operations
362 Password:
363 # \fB./newuts bizarro &\fP
364 [1] 3549
365 clone() returned 3550
366 uts.nodename in child: bizarro
367 uts.nodename in parent: antero
368 # \fBuname \-n\fP # Verify hostname in the shell
369 antero
370 .EE
371 .in
372 .P
373 We then run the program shown below,
374 using it to execute a shell.
375 Inside that shell, we verify that the hostname is the one
376 set by the child created by the first program:
377 .P
378 .in +4n
379 .EX
380 # \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP
381 # \fBuname \-n\fP # Executed in shell started by ns_exec
382 bizarro
383 .EE
384 .in
385 .SS Program source
386 .\" SRC BEGIN (setns.c)
387 .EX
388 #define _GNU_SOURCE
389 #include <err.h>
390 #include <fcntl.h>
391 #include <sched.h>
392 #include <stdio.h>
393 #include <stdlib.h>
394 #include <unistd.h>
395 \&
396 int
397 main(int argc, char *argv[])
398 {
399 int fd;
400 \&
401 if (argc < 3) {
402 fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\[rs]n", argv[0]);
403 exit(EXIT_FAILURE);
404 }
405 \&
406 /* Get file descriptor for namespace; the file descriptor is opened
407 with O_CLOEXEC so as to ensure that it is not inherited by the
408 program that is later executed. */
409 \&
410 fd = open(argv[1], O_RDONLY | O_CLOEXEC);
411 if (fd == \-1)
412 err(EXIT_FAILURE, "open");
413 \&
414 if (setns(fd, 0) == \-1) /* Join that namespace */
415 err(EXIT_FAILURE, "setns");
416 \&
417 execvp(argv[2], &argv[2]); /* Execute a command in namespace */
418 err(EXIT_FAILURE, "execvp");
419 }
420 .EE
421 .\" SRC END
422 .SH SEE ALSO
423 .BR nsenter (1),
424 .BR clone (2),
425 .BR fork (2),
426 .BR unshare (2),
427 .BR vfork (2),
428 .BR namespaces (7),
429 .BR unix (7)