1SETNS(2) Linux Programmer's Manual SETNS(2)
2
3
4
6 setns - reassociate thread with a namespace
7
9 #define _GNU_SOURCE /* See feature_test_macros(7) */
10 #include <sched.h>
11
12 int setns(int fd, int nstype);
13
15 Given a file descriptor referring to a namespace, reassociate the call‐
16 ing thread with that namespace.
17
18 The fd argument is a file descriptor referring to one of the namespace
19 entries in a /proc/[pid]/ns/ directory; see namespaces(7) for further
20 information on /proc/[pid]/ns/. The calling thread will be reassoci‐
21 ated with the corresponding namespace, subject to any constraints
22 imposed by the nstype argument.
23
24 The nstype argument specifies which type of namespace the calling
25 thread may be reassociated with. This argument can have one of the
26 following values:
27
28 0 Allow any type of namespace to be joined.
29
30 CLONE_NEWCGROUP (since Linux 4.6)
31 fd must refer to a cgroup namespace.
32
33 CLONE_NEWIPC (since Linux 3.0)
34 fd must refer to an IPC namespace.
35
36 CLONE_NEWNET (since Linux 3.0)
37 fd must refer to a network namespace.
38
39 CLONE_NEWNS (since Linux 3.8)
40 fd must refer to a mount namespace.
41
42 CLONE_NEWPID (since Linux 3.8)
43 fd must refer to a descendant PID namespace.
44
45 CLONE_NEWUSER (since Linux 3.8)
46 fd must refer to a user namespace.
47
48 CLONE_NEWUTS (since Linux 3.0)
49 fd must refer to a UTS namespace.
50
51 Specifying nstype as 0 suffices if the caller knows (or does not care)
52 what type of namespace is referred to by fd. Specifying a nonzero
53 value for nstype is useful if the caller does not know what type of
54 namespace is referred to by fd and wants to ensure that the namespace
55 is of a particular type. (The caller might not know the type of the
56 namespace referred to by fd if the file descriptor was opened by
57 another process and, for example, passed to the caller via a UNIX
58 domain socket.)
59
60 Details for specific namespace types
61 Note the following details and restrictions when reassociating with
62 specific namespace types:
63
64 User namespaces
65 A process reassociating itself with a user namespace must have
66 the CAP_SYS_ADMIN capability in the target user namespace.
67 (This necessarily implies that it is only possible to join a
68 descendant user namespace.) Upon successfully joining a user
69 namespace, a process is granted all capabilities in that names‐
70 pace, regardless of its user and group IDs.
71
72 A multithreaded process may not change user namespace with
73 setns().
74
75 It is not permitted to use setns() to reenter the caller's cur‐
76 rent user namespace. This prevents a caller that has dropped
77 capabilities from regaining those capabilities via a call to
78 setns().
79
80 For security reasons, a process can't join a new user namespace
81 if it is sharing filesystem-related attributes (the attributes
82 whose sharing is controlled by the clone(2) CLONE_FS flag) with
83 another process.
84
85 For further details on user namespaces, see user_namespaces(7).
86
87 Mount namespaces
88 Changing the mount namespace requires that the caller possess
89 both CAP_SYS_CHROOT and CAP_SYS_ADMIN capabilities in its own
90 user namespace and CAP_SYS_ADMIN in the user namespace that owns
91 the target mount namespace.
92
93 A process can't join a new mount namespace if it is sharing
94 filesystem-related attributes (the attributes whose sharing is
95 controlled by the clone(2) CLONE_FS flag) with another process.
96
97 See user_namespaces(7) for details on the interaction of user
98 namespaces and mount namespaces.
99
100 PID namespaces
101 In order to reassociate itself with a new PID namespace, the
102 caller must have the CAP_SYS_ADMIN capability both in its own
103 user namespace and in the user namespace that owns the target
104 PID namespace.
105
106 If fd refers to a PID namespace, the semantics are somewhat dif‐
107 ferent from other namespace types: reassociating the calling
108 thread with a PID namespace changes only the PID namespace that
109 subsequently created child processes of the caller will be
110 placed in; it does not change the PID namespace of the caller
111 itself.
112
113 Reassociating with a PID namespace is allowed only if the PID
114 namespace specified by fd is a descendant (child, grandchild,
115 etc.) of the PID namespace of the caller.
116
117 For further details on PID namespaces, see pid_namespaces(7).
118
119 Cgroup namespaces
120 In order to reassociate itself with a new cgroup namespace, the
121 caller must have the CAP_SYS_ADMIN capability both in its own
122 user namespace and in the user namespace that owns the target
123 cgroup namespace.
124
125 Using setns() to change the caller's cgroup namespace does not
126 change the caller's cgroup memberships.
127
128 Network, IPC, and UTS namespaces
129 In order to reassociate itself with a new network, IPC, or UTS
130 namespace, the caller must have the CAP_SYS_ADMIN capability
131 both in its own user namespace and in the user namespace that
132 owns the target namespace.
133
135 On success, setns() returns 0. On failure, -1 is returned and errno is
136 set to indicate the error.
137
139 EBADF fd is not a valid file descriptor.
140
141 EINVAL fd refers to a namespace whose type does not match that speci‐
142 fied in nstype.
143
144 EINVAL There is problem with reassociating the thread with the speci‐
145 fied namespace.
146
147 EINVAL The caller tried to join an ancestor (parent, grandparent, and
148 so on) PID namespace.
149
150 EINVAL The caller attempted to join the user namespace in which it is
151 already a member.
152
153 EINVAL The caller shares filesystem (CLONE_FS) state (in particular,
154 the root directory) with other processes and tried to join a new
155 user namespace.
156
157 EINVAL The caller is multithreaded and tried to join a new user names‐
158 pace.
159
160 ENOMEM Cannot allocate sufficient memory to change the specified names‐
161 pace.
162
163 EPERM The calling thread did not have the required capability for this
164 operation.
165
167 The setns() system call first appeared in Linux in kernel 3.0; library
168 support was added to glibc in version 2.14.
169
171 The setns() system call is Linux-specific.
172
174 Not all of the attributes that can be shared when a new thread is cre‐
175 ated using clone(2) can be changed using setns().
176
178 The program below takes two or more arguments. The first argument
179 specifies the pathname of a namespace file in an existing
180 /proc/[pid]/ns/ directory. The remaining arguments specify a command
181 and its arguments. The program opens the namespace file, joins that
182 namespace using setns(), and executes the specified command inside that
183 namespace.
184
185 The following shell session demonstrates the use of this program (com‐
186 piled as a binary named ns_exec) in conjunction with the CLONE_NEWUTS
187 example program in the clone(2) man page (complied as a binary named
188 newuts).
189
190 We begin by executing the example program in clone(2) in the back‐
191 ground. That program creates a child in a separate UTS namespace. The
192 child changes the hostname in its namespace, and then both processes
193 display the hostnames in their UTS namespaces, so that we can see that
194 they are different.
195
196 $ su # Need privilege for namespace operations
197 Password:
198 # ./newuts bizarro &
199 [1] 3549
200 clone() returned 3550
201 uts.nodename in child: bizarro
202 uts.nodename in parent: antero
203 # uname -n # Verify hostname in the shell
204 antero
205
206 We then run the program shown below, using it to execute a shell.
207 Inside that shell, we verify that the hostname is the one set by the
208 child created by the first program:
209
210 # ./ns_exec /proc/3550/ns/uts /bin/bash
211 # uname -n # Executed in shell started by ns_exec
212 bizarro
213
214 Program source
215 #define _GNU_SOURCE
216 #include <fcntl.h>
217 #include <sched.h>
218 #include <unistd.h>
219 #include <stdlib.h>
220 #include <stdio.h>
221
222 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
223 } while (0)
224
225 int
226 main(int argc, char *argv[])
227 {
228 int fd;
229
230 if (argc < 3) {
231 fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\n", argv[0]);
232 exit(EXIT_FAILURE);
233 }
234
235 fd = open(argv[1], O_RDONLY); /* Get file descriptor for namespace */
236 if (fd == -1)
237 errExit("open");
238
239 if (setns(fd, 0) == -1) /* Join that namespace */
240 errExit("setns");
241
242 execvp(argv[2], &argv[2]); /* Execute a command in namespace */
243 errExit("execvp");
244 }
245
247 nsenter(1), clone(2), fork(2), unshare(2), vfork(2), namespaces(7),
248 unix(7)
249
251 This page is part of release 5.04 of the Linux man-pages project. A
252 description of the project, information about reporting bugs, and the
253 latest version of this page, can be found at
254 https://www.kernel.org/doc/man-pages/.
255
256
257
258Linux 2019-10-10 SETNS(2)