]>
Commit | Line | Data |
---|---|---|
dfa22c8b | 1 | .\" Copyright (C) 2011, Eric Biederman <ebiederm@xmission.com> |
83825f79 | 2 | .\" and Copyright (C) 2011, 2012, Michael Kerrisk <mtk.manpages@gmail.com> |
2297bf0e | 3 | .\" |
c4a99c30 | 4 | .\" %%%LICENSE_START(GPLv2_ONELINE) |
dfa22c8b | 5 | .\" Licensed under the GPLv2 |
8ff7380d | 6 | .\" %%%LICENSE_END |
dfa22c8b | 7 | .\" |
4b8c67d9 | 8 | .TH SETNS 2 2017-09-15 "Linux" "Linux Programmer's Manual" |
dfa22c8b | 9 | .SH NAME |
7aa166c5 | 10 | setns \- reassociate thread with a namespace |
dfa22c8b EB |
11 | .SH SYNOPSIS |
12 | .nf | |
13 | .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */" | |
14 | .B #include <sched.h> | |
68e4db0a | 15 | .PP |
dfa22c8b EB |
16 | .BI "int setns(int " fd ", int " nstype ); |
17 | .fi | |
18 | .SH DESCRIPTION | |
c07ad242 | 19 | Given a file descriptor referring to a namespace, |
7aa166c5 | 20 | reassociate the calling thread with that namespace. |
efeece04 | 21 | .PP |
92dcb220 MK |
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 | |
6edfe909 | 27 | .BR namespaces (7) |
c409c4ff | 28 | for further information on |
92dcb220 | 29 | .IR /proc/[pid]/ns/ . |
7aa166c5 | 30 | The calling thread will be reassociated with the corresponding namespace, |
92dcb220 MK |
31 | subject to any constraints imposed by the |
32 | .I nstype | |
33 | argument. | |
efeece04 | 34 | .PP |
dfa22c8b EB |
35 | The |
36 | .I nstype | |
c07ad242 | 37 | argument specifies which type of namespace |
7aa166c5 | 38 | the calling thread may be reassociated with. |
4e1e208d | 39 | This argument can have one of the following values: |
dfa22c8b EB |
40 | .TP |
41 | .BR 0 | |
92dcb220 | 42 | Allow any type of namespace to be joined. |
dfa22c8b | 43 | .TP |
46dd3176 MK |
44 | .BR CLONE_NEWCGROUP " (since Linux 4.6)" |
45 | .I fd | |
46 | must refer to a cgroup namespace. | |
47 | .TP | |
268a93cb | 48 | .BR CLONE_NEWIPC " (since Linux 3.0)" |
92dcb220 MK |
49 | .I fd |
50 | must refer to an IPC namespace. | |
dfa22c8b | 51 | .TP |
268a93cb | 52 | .BR CLONE_NEWNET " (since Linux 3.0)" |
92dcb220 MK |
53 | .I fd |
54 | must refer to a network namespace. | |
dfa22c8b | 55 | .TP |
268a93cb | 56 | .BR CLONE_NEWNS " (since Linux 3.8)" |
99fd2fe3 EB |
57 | .I fd |
58 | must refer to a mount namespace. | |
59 | .TP | |
268a93cb | 60 | .BR CLONE_NEWPID " (since Linux 3.8)" |
99fd2fe3 | 61 | .I fd |
ba7d7ed9 | 62 | must refer to a descendant PID namespace. |
99fd2fe3 | 63 | .TP |
268a93cb | 64 | .BR CLONE_NEWUSER " (since Linux 3.8)" |
99fd2fe3 EB |
65 | .I fd |
66 | must refer to a user namespace. | |
67 | .TP | |
268a93cb | 68 | .BR CLONE_NEWUTS " (since Linux 3.0)" |
92dcb220 MK |
69 | .I fd |
70 | must refer to a UTS namespace. | |
dfa22c8b | 71 | .PP |
92dcb220 MK |
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.) | |
8e23f12b MK |
86 | .\" |
87 | .SS Details for specific namespace types | |
88 | Note the following details and restrictions when reassociating with | |
89 | specific namespace types: | |
90 | .TP | |
91 | User namespaces | |
ec66fbff MK |
92 | A process reassociating itself with a user namespace must have the |
93 | .B CAP_SYS_ADMIN | |
94 | .\" See kernel/user_namespace.c:userns_install() [3.8 source] | |
95 | capability in the target user namespace. | |
96 | Upon successfully joining a user namespace, | |
97 | a process is granted all capabilities in that namespace, | |
98 | regardless of its user and group IDs. | |
2e4a22de | 99 | .IP |
5c67baab | 100 | A multithreaded process may not change user namespace with |
cd7e05aa | 101 | .BR setns (). |
2e4a22de | 102 | .IP |
81714b4f MK |
103 | It is not permitted to use |
104 | .BR setns () | |
105 | to reenter the caller's current user namespace. | |
106 | This prevents a caller that has dropped capabilities from regaining | |
107 | those capabilities via a call to | |
108 | .BR setns (). | |
2e4a22de | 109 | .IP |
85e34225 MK |
110 | For security reasons, |
111 | .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71 | |
112 | .\" https://lwn.net/Articles/543273/ | |
113 | a process can't join a new user namespace if it is sharing | |
8db37760 | 114 | filesystem-related attributes |
85e34225 MK |
115 | (the attributes whose sharing is controlled by the |
116 | .BR clone (2) | |
117 | .B CLONE_FS | |
118 | flag) with another process. | |
2e4a22de | 119 | .IP |
e57c3979 | 120 | For further details on user namespaces, see |
ec66fbff | 121 | .BR user_namespaces (7). |
8e23f12b MK |
122 | .TP |
123 | Mount namespaces | |
49af76fe | 124 | Changing the mount namespace requires that the caller possess both |
cd7e05aa MK |
125 | .B CAP_SYS_CHROOT |
126 | and | |
f5d401dd | 127 | .BR CAP_SYS_ADMIN |
21bfe3e9 | 128 | capabilities in its own user namespace and |
f5d401dd | 129 | .BR CAP_SYS_ADMIN |
6f91075e | 130 | in the the user namespace that owns the target mount namespace. |
2e4a22de | 131 | .IP |
c6b1b95b MK |
132 | A process may not be reassociated with a new mount namespace if it is |
133 | multithreaded. | |
134 | .\" Above check is in fs/namespace.c:mntns_install() [3.8 source] | |
135 | .IP | |
53d084e8 MK |
136 | See |
137 | .BR user_namespaces (7) | |
138 | for details on the interaction of user namespaces and mount namespaces. | |
8e23f12b | 139 | .TP |
0ecd5804 MK |
140 | PID namespaces |
141 | If | |
142 | .I fd | |
143 | refers to a PID namespaces, the semantics are somewhat different | |
144 | from other namespace types: | |
145 | reassociating the calling thread with a PID namespace changes only | |
146 | the PID namespace that subsequently created child processes of | |
147 | the caller will be placed in; | |
148 | it does not change the PID namespace of the caller itself. | |
149 | .IP | |
150 | Reassociating with a PID namespace is allowed only if the | |
151 | PID namespace specified by | |
152 | .IR fd | |
153 | is a descendant (child, grandchild, etc.) | |
154 | of the PID namespace of the caller. | |
155 | .IP | |
156 | For further details on PID namespaces, see | |
157 | .BR pid_namespaces (7). | |
158 | .TP | |
8e23f12b | 159 | Cgroup namespaces |
46dd3176 MK |
160 | Using |
161 | .BR setns () | |
162 | to change the caller's cgroup namespace does not change | |
163 | the caller's cgroup memberships. | |
dfa22c8b | 164 | .SH RETURN VALUE |
92dcb220 | 165 | On success, |
577b1ed5 | 166 | .BR setns () |
92dcb220 | 167 | returns 0. |
dfa22c8b EB |
168 | On failure, \-1 is returned and |
169 | .I errno | |
170 | is set to indicate the error. | |
171 | .SH ERRORS | |
172 | .TP | |
dfa22c8b | 173 | .B EBADF |
c07ad242 MK |
174 | .I fd |
175 | is not a valid file descriptor. | |
dfa22c8b EB |
176 | .TP |
177 | .B EINVAL | |
92dcb220 MK |
178 | .I fd |
179 | refers to a namespace whose type does not match that specified in | |
16fe718f MK |
180 | .IR nstype . |
181 | .TP | |
182 | .B EINVAL | |
183 | There is problem with reassociating | |
7aa166c5 | 184 | the thread with the specified namespace. |
dfa22c8b | 185 | .TP |
ba7d7ed9 MF |
186 | .\" See kernel/pid_namespace.c::pidns_install() [kernel 3.18 sources] |
187 | .B EINVAL | |
c9b0afde MK |
188 | The caller tried to join an ancestor (parent, grandparent, and so on) |
189 | PID namespace. | |
ba7d7ed9 | 190 | .TP |
edc3c3b4 MK |
191 | .B EINVAL |
192 | The caller attempted to join the user namespace | |
193 | in which it is already a member. | |
194 | .TP | |
e63259f2 MK |
195 | .B EINVAL |
196 | .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71 | |
197 | The caller shares filesystem | |
198 | .RB ( CLONE_FS ) | |
199 | state (in particular, the root directory) | |
200 | with other processes and tried to join a new user namespace. | |
201 | .TP | |
202 | .B EINVAL | |
203 | .\" See kernel/user_namespace.c::userns_install() [kernel 3.15 sources] | |
204 | The caller is multithreaded and tried to join a new user namespace. | |
205 | .TP | |
dfa22c8b EB |
206 | .B ENOMEM |
207 | Cannot allocate sufficient memory to change the specified namespace. | |
dfa22c8b EB |
208 | .TP |
209 | .B EPERM | |
7fc8e5ec | 210 | The calling thread did not have the required capability |
63b6ef41 | 211 | for this operation. |
dfa22c8b EB |
212 | .SH VERSIONS |
213 | The | |
214 | .BR setns () | |
c95b6ae1 | 215 | system call first appeared in Linux in kernel 3.0; |
268a93cb | 216 | library support was added to glibc in version 2.14. |
dfa22c8b EB |
217 | .SH CONFORMING TO |
218 | The | |
219 | .BR setns () | |
220 | system call is Linux-specific. | |
221 | .SH NOTES | |
7aa166c5 MK |
222 | Not all of the attributes that can be shared when |
223 | a new thread is created using | |
dfa22c8b EB |
224 | .BR clone (2) |
225 | can be changed using | |
226 | .BR setns (). | |
8d41e607 MK |
227 | .SH EXAMPLE |
228 | The program below takes two or more arguments. | |
229 | The first argument specifies the pathname of a namespace file in an existing | |
230 | .I /proc/[pid]/ns/ | |
231 | directory. | |
232 | The remaining arguments specify a command and its arguments. | |
233 | The program opens the namespace file, joins that namespace using | |
234 | .BR setns (), | |
235 | and executes the specified command inside that namespace. | |
efeece04 | 236 | .PP |
8d41e607 MK |
237 | The following shell session demonstrates the use of this program |
238 | (compiled as a binary named | |
a1ce9159 | 239 | .IR ns_exec ) |
8d41e607 MK |
240 | in conjunction with the |
241 | .BR CLONE_NEWUTS | |
242 | example program in the | |
243 | .BR clone (2) | |
244 | man page (complied as a binary named | |
245 | .IR newuts ). | |
efeece04 | 246 | .PP |
8d41e607 MK |
247 | We begin by executing the example program in |
248 | .BR clone (2) | |
249 | in the background. | |
250 | That program creates a child in a separate UTS namespace. | |
51ebe080 | 251 | The child changes the hostname in its namespace, |
8d41e607 MK |
252 | and then both processes display the hostnames in their UTS namespaces, |
253 | so that we can see that they are different. | |
efeece04 | 254 | .PP |
8d41e607 | 255 | .in +4n |
b8302363 | 256 | .EX |
8d41e607 | 257 | $ \fBsu\fP # Need privilege for namespace operations |
9f1b9726 | 258 | Password: |
8d41e607 MK |
259 | # \fB./newuts bizarro &\fP |
260 | [1] 3549 | |
261 | clone() returned 3550 | |
262 | uts.nodename in child: bizarro | |
263 | uts.nodename in parent: antero | |
5c977011 | 264 | # \fBuname \-n\fP # Verify hostname in the shell |
8d41e607 | 265 | antero |
b8302363 | 266 | .EE |
e646a1ba | 267 | .in |
efeece04 | 268 | .PP |
8d41e607 MK |
269 | We then run the program shown below, |
270 | using it to execute a shell. | |
271 | Inside that shell, we verify that the hostname is the one | |
272 | set by the child created by the first program: | |
efeece04 | 273 | .PP |
8d41e607 | 274 | .in +4n |
b8302363 | 275 | .EX |
a1ce9159 | 276 | # \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP |
5c977011 | 277 | # \fBuname \-n\fP # Executed in shell started by ns_exec |
8d41e607 | 278 | bizarro |
b8302363 | 279 | .EE |
e646a1ba | 280 | .in |
8d41e607 | 281 | .SS Program source |
e7d0bb47 | 282 | .EX |
8d41e607 MK |
283 | #define _GNU_SOURCE |
284 | #include <fcntl.h> | |
285 | #include <sched.h> | |
286 | #include <unistd.h> | |
287 | #include <stdlib.h> | |
288 | #include <stdio.h> | |
289 | ||
290 | #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\ | |
291 | } while (0) | |
292 | ||
293 | int | |
294 | main(int argc, char *argv[]) | |
295 | { | |
296 | int fd; | |
297 | ||
298 | if (argc < 3) { | |
299 | fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\\n", argv[0]); | |
300 | exit(EXIT_FAILURE); | |
301 | } | |
302 | ||
e2b63503 | 303 | fd = open(argv[1], O_RDONLY); /* Get file descriptor for namespace */ |
8d41e607 MK |
304 | if (fd == \-1) |
305 | errExit("open"); | |
306 | ||
e2b63503 | 307 | if (setns(fd, 0) == \-1) /* Join that namespace */ |
8d41e607 MK |
308 | errExit("setns"); |
309 | ||
e2b63503 | 310 | execvp(argv[2], &argv[2]); /* Execute a command in namespace */ |
8d41e607 MK |
311 | errExit("execvp"); |
312 | } | |
e7d0bb47 | 313 | .EE |
dfa22c8b | 314 | .SH SEE ALSO |
7680cb3e | 315 | .BR nsenter (1), |
dfa22c8b EB |
316 | .BR clone (2), |
317 | .BR fork (2), | |
2a9f74a9 | 318 | .BR unshare (2), |
b93a34b5 | 319 | .BR vfork (2), |
41096af1 | 320 | .BR namespaces (7), |
92dcb220 | 321 | .BR unix (7) |