]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/setns.2
sock_diag.7: Tweaks to Dmitry Levin's page
[thirdparty/man-pages.git] / man2 / setns.2
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 2016-03-15 "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 .sp
16 .BI "int setns(int " fd ", int " nstype );
17 .fi
18 .SH DESCRIPTION
19 Given a file descriptor referring to a namespace,
20 reassociate the calling thread with that namespace.
21
22 The
23 .I fd
24 argument is a file descriptor referring to one of the namespace entries in a
25 .I /proc/[pid]/ns/
26 directory; see
27 .BR namespaces (7)
28 for further information on
29 .IR /proc/[pid]/ns/ .
30 The calling thread will be reassociated with the corresponding namespace,
31 subject to any constraints imposed by the
32 .I nstype
33 argument.
34
35 The
36 .I nstype
37 argument specifies which type of namespace
38 the calling thread may be reassociated with.
39 This argument can have one of the following values:
40 .TP
41 .BR 0
42 Allow any type of namespace to be joined.
43 .TP
44 .BR CLONE_NEWCGROUP " (since Linux 4.6)"
45 .I fd
46 must refer to a cgroup namespace.
47 .TP
48 .BR CLONE_NEWIPC " (since Linux 3.0)"
49 .I fd
50 must refer to an IPC namespace.
51 .TP
52 .BR CLONE_NEWNET " (since Linux 3.0)"
53 .I fd
54 must refer to a network namespace.
55 .TP
56 .BR CLONE_NEWNS " (since Linux 3.8)"
57 .I fd
58 must refer to a mount namespace.
59 .TP
60 .BR CLONE_NEWPID " (since Linux 3.8)"
61 .I fd
62 must refer to a descendant PID namespace.
63 .TP
64 .BR CLONE_NEWUSER " (since Linux 3.8)"
65 .I fd
66 must refer to a user namespace.
67 .TP
68 .BR CLONE_NEWUTS " (since Linux 3.0)"
69 .I fd
70 must refer to a UTS namespace.
71 .PP
72 Specifying
73 .I nstype
74 as 0 suffices if the caller knows (or does not care)
75 what type of namespace is referred to by
76 .IR fd .
77 Specifying a nonzero value for
78 .I nstype
79 is useful if the caller does not know what type of namespace is referred to by
80 .IR fd
81 and wants to ensure that the namespace is of a particular type.
82 (The caller might not know the type of the namespace referred to by
83 .IR fd
84 if the file descriptor was opened by another process and, for example,
85 passed to the caller via a UNIX domain socket.)
86
87 .B CLONE_NEWPID
88 behaves somewhat differently from the other
89 .I nstype
90 values:
91 reassociating the calling thread with a PID namespace changes only
92 the PID namespace that child processes of the caller will be created in;
93 it does not change the PID namespace of the caller itself.
94 Reassociating with a PID namespace is allowed only if the
95 PID namespace specified by
96 .IR fd
97 is a descendant (child, grandchild, etc.)
98 of the PID namespace of the caller.
99 For further details on PID namespaces, see
100 .BR pid_namespaces (7).
101
102 A process reassociating itself with a user namespace must have the
103 .B CAP_SYS_ADMIN
104 .\" See kernel/user_namespace.c:userns_install() [3.8 source]
105 capability in the target user namespace.
106 Upon successfully joining a user namespace,
107 a process is granted all capabilities in that namespace,
108 regardless of its user and group IDs.
109 A multithreaded process may not change user namespace with
110 .BR setns ().
111 It is not permitted to use
112 .BR setns ()
113 to reenter the caller's current user namespace.
114 This prevents a caller that has dropped capabilities from regaining
115 those capabilities via a call to
116 .BR setns ().
117 For security reasons,
118 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
119 .\" https://lwn.net/Articles/543273/
120 a process can't join a new user namespace if it is sharing
121 filesystem-related attributes
122 (the attributes whose sharing is controlled by the
123 .BR clone (2)
124 .B CLONE_FS
125 flag) with another process.
126 For further details on user namespaces, see
127 .BR user_namespaces (7).
128
129 A process may not be reassociated with a new mount namespace if it is
130 multithreaded.
131 .\" Above check is in fs/namespace.c:mntns_install() [3.8 source]
132 Changing the mount namespace requires that the caller possess both
133 .B CAP_SYS_CHROOT
134 and
135 .BR CAP_SYS_ADMIN
136 capabilities in its own user namespace and
137 .BR CAP_SYS_ADMIN
138 in the target mount namespace.
139 See
140 .BR user_namespaces (7)
141 for details on the interaction of user namespaces and mount namespaces.
142
143 Using
144 .BR setns ()
145 to change the caller's cgroup namespace does not change
146 the caller's cgroup memberships.
147 .SH RETURN VALUE
148 On success,
149 .BR setns ()
150 returns 0.
151 On failure, \-1 is returned and
152 .I errno
153 is set to indicate the error.
154 .SH ERRORS
155 .TP
156 .B EBADF
157 .I fd
158 is not a valid file descriptor.
159 .TP
160 .B EINVAL
161 .I fd
162 refers to a namespace whose type does not match that specified in
163 .IR nstype .
164 .TP
165 .B EINVAL
166 There is problem with reassociating
167 the thread with the specified namespace.
168 .TP
169 .\" See kernel/pid_namespace.c::pidns_install() [kernel 3.18 sources]
170 .B EINVAL
171 The caller tried to join an ancestor (parent, grandparent, and so on)
172 PID namespace.
173 .TP
174 .B EINVAL
175 The caller attempted to join the user namespace
176 in which it is already a member.
177 .TP
178 .B EINVAL
179 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
180 The caller shares filesystem
181 .RB ( CLONE_FS )
182 state (in particular, the root directory)
183 with other processes and tried to join a new user namespace.
184 .TP
185 .B EINVAL
186 .\" See kernel/user_namespace.c::userns_install() [kernel 3.15 sources]
187 The caller is multithreaded and tried to join a new user namespace.
188 .TP
189 .B ENOMEM
190 Cannot allocate sufficient memory to change the specified namespace.
191 .TP
192 .B EPERM
193 The calling thread did not have the required capability
194 for this operation.
195 .SH VERSIONS
196 The
197 .BR setns ()
198 system call first appeared in Linux in kernel 3.0;
199 library support was added to glibc in version 2.14.
200 .SH CONFORMING TO
201 The
202 .BR setns ()
203 system call is Linux-specific.
204 .SH NOTES
205 Not all of the attributes that can be shared when
206 a new thread is created using
207 .BR clone (2)
208 can be changed using
209 .BR setns ().
210 .SH EXAMPLE
211 The program below takes two or more arguments.
212 The first argument specifies the pathname of a namespace file in an existing
213 .I /proc/[pid]/ns/
214 directory.
215 The remaining arguments specify a command and its arguments.
216 The program opens the namespace file, joins that namespace using
217 .BR setns (),
218 and executes the specified command inside that namespace.
219
220 The following shell session demonstrates the use of this program
221 (compiled as a binary named
222 .IR ns_exec )
223 in conjunction with the
224 .BR CLONE_NEWUTS
225 example program in the
226 .BR clone (2)
227 man page (complied as a binary named
228 .IR newuts ).
229
230 We begin by executing the example program in
231 .BR clone (2)
232 in the background.
233 That program creates a child in a separate UTS namespace.
234 The child changes the hostname in its namespace,
235 and then both processes display the hostnames in their UTS namespaces,
236 so that we can see that they are different.
237
238 .nf
239 .in +4n
240 $ \fBsu\fP # Need privilege for namespace operations
241 Password:
242 # \fB./newuts bizarro &\fP
243 [1] 3549
244 clone() returned 3550
245 uts.nodename in child: bizarro
246 uts.nodename in parent: antero
247 # \fBuname \-n\fP # Verify hostname in the shell
248 antero
249 .in
250 .fi
251
252 We then run the program shown below,
253 using it to execute a shell.
254 Inside that shell, we verify that the hostname is the one
255 set by the child created by the first program:
256
257 .nf
258 .in +4n
259 # \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP
260 # \fBuname \-n\fP # Executed in shell started by ns_exec
261 bizarro
262 .in
263 .fi
264 .SS Program source
265 .nf
266 #define _GNU_SOURCE
267 #include <fcntl.h>
268 #include <sched.h>
269 #include <unistd.h>
270 #include <stdlib.h>
271 #include <stdio.h>
272
273 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
274 } while (0)
275
276 int
277 main(int argc, char *argv[])
278 {
279 int fd;
280
281 if (argc < 3) {
282 fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\\n", argv[0]);
283 exit(EXIT_FAILURE);
284 }
285
286 fd = open(argv[1], O_RDONLY); /* Get file descriptor for namespace */
287 if (fd == \-1)
288 errExit("open");
289
290 if (setns(fd, 0) == \-1) /* Join that namespace */
291 errExit("setns");
292
293 execvp(argv[2], &argv[2]); /* Execute a command in namespace */
294 errExit("execvp");
295 }
296 .fi
297 .SH SEE ALSO
298 .BR clone (2),
299 .BR fork (2),
300 .BR unshare (2),
301 .BR vfork (2),
302 .BR namespaces (7),
303 .BR unix (7)