2 * nsenter(1) - command-line interface for setns(2)
4 * Copyright (C) 2012-2013 Eric Biederman <ebiederm@xmission.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/types.h>
34 #ifdef HAVE_LIBSELINUX
35 # include <selinux/selinux.h>
41 #include "closestream.h"
42 #include "namespace.h"
43 #include "exec_shell.h"
45 static struct namespace_file
{
49 } namespace_files
[] = {
50 /* Careful the order is significant in this array.
52 * The user namespace comes either first or last: first if
53 * you're using it to increase your privilege and last if
54 * you're using it to decrease. We enter the namespaces in
55 * two passes starting initially from offset 1 and then offset
58 { .nstype
= CLONE_NEWUSER
, .name
= "ns/user", .fd
= -1 },
59 { .nstype
= CLONE_NEWCGROUP
,.name
= "ns/cgroup", .fd
= -1 },
60 { .nstype
= CLONE_NEWIPC
, .name
= "ns/ipc", .fd
= -1 },
61 { .nstype
= CLONE_NEWUTS
, .name
= "ns/uts", .fd
= -1 },
62 { .nstype
= CLONE_NEWNET
, .name
= "ns/net", .fd
= -1 },
63 { .nstype
= CLONE_NEWPID
, .name
= "ns/pid", .fd
= -1 },
64 { .nstype
= CLONE_NEWNS
, .name
= "ns/mnt", .fd
= -1 },
65 { .nstype
= 0, .name
= NULL
, .fd
= -1 }
68 static void __attribute__((__noreturn__
)) usage(void)
72 fputs(USAGE_HEADER
, out
);
73 fprintf(out
, _(" %s [options] [<program> [<argument>...]]\n"),
74 program_invocation_short_name
);
76 fputs(USAGE_SEPARATOR
, out
);
77 fputs(_("Run a program with namespaces of other processes.\n"), out
);
79 fputs(USAGE_OPTIONS
, out
);
80 fputs(_(" -a, --all enter all namespaces\n"), out
);
81 fputs(_(" -t, --target <pid> target process to get namespaces from\n"), out
);
82 fputs(_(" -m, --mount[=<file>] enter mount namespace\n"), out
);
83 fputs(_(" -u, --uts[=<file>] enter UTS namespace (hostname etc)\n"), out
);
84 fputs(_(" -i, --ipc[=<file>] enter System V IPC namespace\n"), out
);
85 fputs(_(" -n, --net[=<file>] enter network namespace\n"), out
);
86 fputs(_(" -p, --pid[=<file>] enter pid namespace\n"), out
);
87 fputs(_(" -C, --cgroup[=<file>] enter cgroup namespace\n"), out
);
88 fputs(_(" -U, --user[=<file>] enter user namespace\n"), out
);
89 fputs(_(" -S, --setuid <uid> set uid in entered namespace\n"), out
);
90 fputs(_(" -G, --setgid <gid> set gid in entered namespace\n"), out
);
91 fputs(_(" --preserve-credentials do not touch uids or gids\n"), out
);
92 fputs(_(" -r, --root[=<dir>] set the root directory\n"), out
);
93 fputs(_(" -w, --wd[=<dir>] set the working directory\n"), out
);
94 fputs(_(" -F, --no-fork do not fork before exec'ing <program>\n"), out
);
95 #ifdef HAVE_LIBSELINUX
96 fputs(_(" -Z, --follow-context set SELinux context according to --target PID\n"), out
);
99 fputs(USAGE_SEPARATOR
, out
);
100 printf(USAGE_HELP_OPTIONS(24));
101 printf(USAGE_MAN_TAIL("nsenter(1)"));
106 static pid_t namespace_target_pid
= 0;
107 static int root_fd
= -1;
108 static int wd_fd
= -1;
110 static void open_target_fd(int *fd
, const char *type
, const char *path
)
112 char pathbuf
[PATH_MAX
];
114 if (!path
&& namespace_target_pid
) {
115 snprintf(pathbuf
, sizeof(pathbuf
), "/proc/%u/%s",
116 namespace_target_pid
, type
);
121 _("neither filename nor target pid supplied for %s"),
127 *fd
= open(path
, O_RDONLY
);
129 err(EXIT_FAILURE
, _("cannot open %s"), path
);
132 static void open_namespace_fd(int nstype
, const char *path
)
134 struct namespace_file
*nsfile
;
136 for (nsfile
= namespace_files
; nsfile
->nstype
; nsfile
++) {
137 if (nstype
!= nsfile
->nstype
)
140 open_target_fd(&nsfile
->fd
, nsfile
->name
, path
);
143 /* This should never happen */
144 assert(nsfile
->nstype
);
147 static int get_ns_ino(const char *path
, ino_t
*ino
)
151 if (stat(path
, &st
) != 0)
157 static int is_same_namespace(pid_t a
, pid_t b
, const char *type
)
160 ino_t a_ino
= 0, b_ino
= 0;
162 snprintf(path
, sizeof(path
), "/proc/%u/%s", a
, type
);
163 if (get_ns_ino(path
, &a_ino
) != 0)
164 err(EXIT_FAILURE
, _("stat of %s failed"), path
);
166 snprintf(path
, sizeof(path
), "/proc/%u/%s", b
, type
);
167 if (get_ns_ino(path
, &b_ino
) != 0)
168 err(EXIT_FAILURE
, _("stat of %s failed"), path
);
170 return a_ino
== b_ino
;
173 static void continue_as_child(void)
175 pid_t child
= fork();
180 err(EXIT_FAILURE
, _("fork failed"));
182 /* Only the child returns */
187 ret
= waitpid(child
, &status
, WUNTRACED
);
188 if ((ret
== child
) && (WIFSTOPPED(status
))) {
189 /* The child suspended so suspend us as well */
190 kill(getpid(), SIGSTOP
);
191 kill(child
, SIGCONT
);
196 /* Return the child's exit code if possible */
197 if (WIFEXITED(status
)) {
198 exit(WEXITSTATUS(status
));
199 } else if (WIFSIGNALED(status
)) {
200 kill(getpid(), WTERMSIG(status
));
205 int main(int argc
, char *argv
[])
208 OPT_PRESERVE_CRED
= CHAR_MAX
+ 1
210 static const struct option longopts
[] = {
211 { "all", no_argument
, NULL
, 'a' },
212 { "help", no_argument
, NULL
, 'h' },
213 { "version", no_argument
, NULL
, 'V'},
214 { "target", required_argument
, NULL
, 't' },
215 { "mount", optional_argument
, NULL
, 'm' },
216 { "uts", optional_argument
, NULL
, 'u' },
217 { "ipc", optional_argument
, NULL
, 'i' },
218 { "net", optional_argument
, NULL
, 'n' },
219 { "pid", optional_argument
, NULL
, 'p' },
220 { "user", optional_argument
, NULL
, 'U' },
221 { "cgroup", optional_argument
, NULL
, 'C' },
222 { "setuid", required_argument
, NULL
, 'S' },
223 { "setgid", required_argument
, NULL
, 'G' },
224 { "root", optional_argument
, NULL
, 'r' },
225 { "wd", optional_argument
, NULL
, 'w' },
226 { "no-fork", no_argument
, NULL
, 'F' },
227 { "preserve-credentials", no_argument
, NULL
, OPT_PRESERVE_CRED
},
228 #ifdef HAVE_LIBSELINUX
229 { "follow-context", no_argument
, NULL
, 'Z' },
234 struct namespace_file
*nsfile
;
235 int c
, pass
, namespaces
= 0, setgroups_nerrs
= 0, preserve_cred
= 0;
236 bool do_rd
= false, do_wd
= false, force_uid
= false, force_gid
= false;
238 int do_fork
= -1; /* unknown yet */
241 #ifdef HAVE_LIBSELINUX
245 setlocale(LC_ALL
, "");
246 bindtextdomain(PACKAGE
, LOCALEDIR
);
248 close_stdout_atexit();
251 getopt_long(argc
, argv
, "+ahVt:m::u::i::n::p::C::U::S:G:r::w::FZ",
252 longopts
, NULL
)) != -1) {
258 namespace_target_pid
=
259 strtoul_or_err(optarg
, _("failed to parse pid"));
263 open_namespace_fd(CLONE_NEWNS
, optarg
);
265 namespaces
|= CLONE_NEWNS
;
269 open_namespace_fd(CLONE_NEWUTS
, optarg
);
271 namespaces
|= CLONE_NEWUTS
;
275 open_namespace_fd(CLONE_NEWIPC
, optarg
);
277 namespaces
|= CLONE_NEWIPC
;
281 open_namespace_fd(CLONE_NEWNET
, optarg
);
283 namespaces
|= CLONE_NEWNET
;
287 open_namespace_fd(CLONE_NEWPID
, optarg
);
289 namespaces
|= CLONE_NEWPID
;
293 open_namespace_fd(CLONE_NEWCGROUP
, optarg
);
295 namespaces
|= CLONE_NEWCGROUP
;
299 open_namespace_fd(CLONE_NEWUSER
, optarg
);
301 namespaces
|= CLONE_NEWUSER
;
304 uid
= strtoul_or_err(optarg
, _("failed to parse uid"));
308 gid
= strtoul_or_err(optarg
, _("failed to parse gid"));
316 open_target_fd(&root_fd
, "root", optarg
);
322 open_target_fd(&wd_fd
, "cwd", optarg
);
326 case OPT_PRESERVE_CRED
:
329 #ifdef HAVE_LIBSELINUX
337 print_version(EXIT_SUCCESS
);
339 errtryhelp(EXIT_FAILURE
);
343 #ifdef HAVE_LIBSELINUX
344 if (selinux
&& is_selinux_enabled() > 0) {
347 if (!namespace_target_pid
)
348 errx(EXIT_FAILURE
, _("no target PID specified for --follow-context"));
349 if (getpidcon(namespace_target_pid
, &scon
) < 0)
350 errx(EXIT_FAILURE
, _("failed to get %d SELinux context"),
351 (int) namespace_target_pid
);
352 if (setexeccon(scon
) < 0)
353 errx(EXIT_FAILURE
, _("failed to set exec context to '%s'"), scon
);
359 if (!namespace_target_pid
)
360 errx(EXIT_FAILURE
, _("no target PID specified for --all"));
361 for (nsfile
= namespace_files
; nsfile
->nstype
; nsfile
++) {
363 continue; /* namespace already specified */
365 /* It is not permitted to use setns(2) to reenter the caller's
366 * current user namespace; see setns(2) man page for more details.
368 if (nsfile
->nstype
& CLONE_NEWUSER
369 && is_same_namespace(getpid(), namespace_target_pid
, nsfile
->name
))
372 namespaces
|= nsfile
->nstype
;
377 * Open remaining namespace and directory descriptors.
379 for (nsfile
= namespace_files
; nsfile
->nstype
; nsfile
++)
380 if (nsfile
->nstype
& namespaces
)
381 open_namespace_fd(nsfile
->nstype
, NULL
);
383 open_target_fd(&root_fd
, "root", NULL
);
385 open_target_fd(&wd_fd
, "cwd", NULL
);
388 * Update namespaces variable to contain all requested namespaces
390 for (nsfile
= namespace_files
; nsfile
->nstype
; nsfile
++) {
393 namespaces
|= nsfile
->nstype
;
396 /* for user namespaces we always set UID and GID (default is 0)
397 * and clear root's groups if --preserve-credentials is no specified */
398 if ((namespaces
& CLONE_NEWUSER
) && !preserve_cred
) {
399 force_uid
= true, force_gid
= true;
401 /* We call setgroups() before and after we enter user namespace,
402 * let's complain only if both fail */
403 if (setgroups(0, NULL
) != 0)
408 * Now that we know which namespaces we want to enter, enter
409 * them. Do this in two passes, not entering the user
410 * namespace on the first pass. So if we're deprivileging the
411 * container we'll enter the user namespace last and if we're
412 * privileging it then we enter the user namespace first
413 * (because the initial setns will fail).
415 for (pass
= 0; pass
< 2; pass
++) {
416 for (nsfile
= namespace_files
+ 1 - pass
; nsfile
->nstype
; nsfile
++) {
419 if (nsfile
->nstype
== CLONE_NEWPID
&& do_fork
== -1)
421 if (setns(nsfile
->fd
, nsfile
->nstype
)) {
424 _("reassociate to namespace '%s' failed"),
435 /* Remember the current working directory if I'm not changing it */
436 if (root_fd
>= 0 && wd_fd
< 0) {
437 wd_fd
= open(".", O_RDONLY
);
440 _("cannot open current working directory"));
443 /* Change the root directory */
445 if (fchdir(root_fd
) < 0)
447 _("change directory by root file descriptor failed"));
450 err(EXIT_FAILURE
, _("chroot failed"));
456 /* Change the working directory */
458 if (fchdir(wd_fd
) < 0)
460 _("change directory by working directory file descriptor failed"));
469 if (force_uid
|| force_gid
) {
470 if (force_gid
&& setgroups(0, NULL
) != 0 && setgroups_nerrs
) /* drop supplementary groups */
471 err(EXIT_FAILURE
, _("setgroups failed"));
472 if (force_gid
&& setgid(gid
) < 0) /* change GID */
473 err(EXIT_FAILURE
, _("setgid failed"));
474 if (force_uid
&& setuid(uid
) < 0) /* change UID */
475 err(EXIT_FAILURE
, _("setuid failed"));
479 execvp(argv
[optind
], argv
+ optind
);
480 errexec(argv
[optind
]);