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