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