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 #include <sys/ioctl.h>
35 #ifdef HAVE_LINUX_NSFS_H
36 # include <linux/nsfs.h>
39 # define NS_GET_USERNS _IO(0xb7, 0x1)
42 #ifdef HAVE_LIBSELINUX
43 # include <selinux/selinux.h>
49 #include "closestream.h"
50 #include "namespace.h"
51 #include "exec_shell.h"
58 static struct namespace_file
{
62 } namespace_files
[] = {
63 /* Careful the order is significant in this array.
65 * The user namespace comes either first or last: first if
66 * you're using it to increase your privilege and last if
67 * you're using it to decrease. We enter the namespaces in
68 * two passes starting initially from offset 1 and then offset
71 { .nstype
= CLONE_NEWUSER
, .name
= "ns/user", .fd
= -1 },
72 { .nstype
= CLONE_NEWCGROUP
,.name
= "ns/cgroup", .fd
= -1 },
73 { .nstype
= CLONE_NEWIPC
, .name
= "ns/ipc", .fd
= -1 },
74 { .nstype
= CLONE_NEWUTS
, .name
= "ns/uts", .fd
= -1 },
75 { .nstype
= CLONE_NEWNET
, .name
= "ns/net", .fd
= -1 },
76 { .nstype
= CLONE_NEWPID
, .name
= "ns/pid", .fd
= -1 },
77 { .nstype
= CLONE_NEWNS
, .name
= "ns/mnt", .fd
= -1 },
78 { .nstype
= CLONE_NEWTIME
, .name
= "ns/time", .fd
= -1 },
79 { .nstype
= 0, .name
= NULL
, .fd
= -1 }
82 static void __attribute__((__noreturn__
)) usage(void)
86 fputs(USAGE_HEADER
, out
);
87 fprintf(out
, _(" %s [options] [<program> [<argument>...]]\n"),
88 program_invocation_short_name
);
90 fputs(USAGE_SEPARATOR
, out
);
91 fputs(_("Run a program with namespaces of other processes.\n"), out
);
93 fputs(USAGE_OPTIONS
, out
);
94 fputs(_(" -a, --all enter all namespaces\n"), out
);
95 fputs(_(" -t, --target <pid> target process to get namespaces from\n"), out
);
96 fputs(_(" -m, --mount[=<file>] enter mount namespace\n"), out
);
97 fputs(_(" -u, --uts[=<file>] enter UTS namespace (hostname etc)\n"), out
);
98 fputs(_(" -i, --ipc[=<file>] enter System V IPC namespace\n"), out
);
99 fputs(_(" -n, --net[=<file>] enter network namespace\n"), out
);
100 fputs(_(" -p, --pid[=<file>] enter pid namespace\n"), out
);
101 fputs(_(" -C, --cgroup[=<file>] enter cgroup namespace\n"), out
);
102 fputs(_(" -U, --user[=<file>] enter user namespace\n"), out
);
103 fputs(_(" --user-parent enter parent user namespace\n"), out
);
104 fputs(_(" -T, --time[=<file>] enter time namespace\n"), out
);
105 fputs(_(" -S, --setuid[=<uid>] set uid in entered namespace\n"), out
);
106 fputs(_(" -G, --setgid[=<gid>] set gid in entered namespace\n"), out
);
107 fputs(_(" --preserve-credentials do not touch uids or gids\n"), out
);
108 fputs(_(" --keep-caps retain capabilities granted in user namespaces\n"), out
);
109 fputs(_(" -r, --root[=<dir>] set the root directory\n"), out
);
110 fputs(_(" -w, --wd[=<dir>] set the working directory\n"), out
);
111 fputs(_(" -W, --wdns <dir> set the working directory in namespace\n"), out
);
112 fputs(_(" -e, --env inherit environment variables from target process\n"), out
);
113 fputs(_(" -F, --no-fork do not fork before exec'ing <program>\n"), out
);
114 #ifdef HAVE_LIBSELINUX
115 fputs(_(" -Z, --follow-context set SELinux context according to --target PID\n"), out
);
118 fputs(USAGE_SEPARATOR
, out
);
119 printf(USAGE_HELP_OPTIONS(24));
120 printf(USAGE_MAN_TAIL("nsenter(1)"));
125 static pid_t namespace_target_pid
= 0;
126 static int root_fd
= -1;
127 static int wd_fd
= -1;
128 static int env_fd
= -1;
129 static int uid_gid_fd
= -1;
131 static void set_parent_user_ns_fd(void)
133 struct namespace_file
*nsfile
= NULL
;
134 struct namespace_file
*user_nsfile
= NULL
;
137 for (nsfile
= namespace_files
; nsfile
->nstype
; nsfile
++) {
138 if (nsfile
->nstype
== CLONE_NEWUSER
)
139 user_nsfile
= nsfile
;
141 if (nsfile
->fd
== -1)
144 parent_ns
= ioctl(nsfile
->fd
, NS_GET_USERNS
);
146 err(EXIT_FAILURE
, _("failed to open parent ns of %s"), nsfile
->name
);
152 errx(EXIT_FAILURE
, _("no namespaces to get parent of"));
154 user_nsfile
->fd
= parent_ns
;
158 static void open_target_fd(int *fd
, const char *type
, const char *path
)
160 char pathbuf
[PATH_MAX
];
162 if (!path
&& namespace_target_pid
) {
163 snprintf(pathbuf
, sizeof(pathbuf
), "/proc/%u/%s",
164 namespace_target_pid
, type
);
169 _("neither filename nor target pid supplied for %s"),
175 *fd
= open(path
, O_RDONLY
);
177 err(EXIT_FAILURE
, _("cannot open %s"), path
);
180 static void open_namespace_fd(int nstype
, const char *path
)
182 struct namespace_file
*nsfile
;
184 for (nsfile
= namespace_files
; nsfile
->nstype
; nsfile
++) {
185 if (nstype
!= nsfile
->nstype
)
188 open_target_fd(&nsfile
->fd
, nsfile
->name
, path
);
191 /* This should never happen */
192 assert(nsfile
->nstype
);
195 static int get_ns_ino(const char *path
, ino_t
*ino
)
199 if (stat(path
, &st
) != 0)
205 static int is_usable_namespace(pid_t target
, const struct namespace_file
*nsfile
)
211 /* Check NS accessibility */
212 snprintf(path
, sizeof(path
), "/proc/%u/%s", getpid(), nsfile
->name
);
213 rc
= get_ns_ino(path
, &my_ino
);
215 return false; /* Unsupported NS */
217 /* It is not permitted to use setns(2) to reenter the caller's
218 * current user namespace; see setns(2) man page for more details.
220 if (nsfile
->nstype
& CLONE_NEWUSER
) {
221 ino_t target_ino
= 0;
223 snprintf(path
, sizeof(path
), "/proc/%u/%s", target
, nsfile
->name
);
224 if (get_ns_ino(path
, &target_ino
) != 0)
225 err(EXIT_FAILURE
, _("stat of %s failed"), path
);
227 if (my_ino
== target_ino
)
231 return true; /* All pass */
234 static void continue_as_child(void)
240 /* Clear any inherited settings */
241 signal(SIGCHLD
, SIG_DFL
);
245 err(EXIT_FAILURE
, _("fork failed"));
247 /* Only the child returns */
252 ret
= waitpid(child
, &status
, WUNTRACED
);
253 if ((ret
== child
) && (WIFSTOPPED(status
))) {
254 /* The child suspended so suspend us as well */
255 kill(getpid(), SIGSTOP
);
256 kill(child
, SIGCONT
);
261 /* Return the child's exit code if possible */
262 if (WIFEXITED(status
)) {
263 exit(WEXITSTATUS(status
));
264 } else if (WIFSIGNALED(status
)) {
265 kill(getpid(), WTERMSIG(status
));
270 int main(int argc
, char *argv
[])
273 OPT_PRESERVE_CRED
= CHAR_MAX
+ 1,
277 static const struct option longopts
[] = {
278 { "all", no_argument
, NULL
, 'a' },
279 { "help", no_argument
, NULL
, 'h' },
280 { "version", no_argument
, NULL
, 'V'},
281 { "target", required_argument
, NULL
, 't' },
282 { "mount", optional_argument
, NULL
, 'm' },
283 { "uts", optional_argument
, NULL
, 'u' },
284 { "ipc", optional_argument
, NULL
, 'i' },
285 { "net", optional_argument
, NULL
, 'n' },
286 { "pid", optional_argument
, NULL
, 'p' },
287 { "user", optional_argument
, NULL
, 'U' },
288 { "cgroup", optional_argument
, NULL
, 'C' },
289 { "time", optional_argument
, NULL
, 'T' },
290 { "setuid", required_argument
, NULL
, 'S' },
291 { "setgid", required_argument
, NULL
, 'G' },
292 { "root", optional_argument
, NULL
, 'r' },
293 { "wd", optional_argument
, NULL
, 'w' },
294 { "wdns", optional_argument
, NULL
, 'W' },
295 { "env", no_argument
, NULL
, 'e' },
296 { "no-fork", no_argument
, NULL
, 'F' },
297 { "preserve-credentials", no_argument
, NULL
, OPT_PRESERVE_CRED
},
298 { "keep-caps", no_argument
, NULL
, OPT_KEEPCAPS
},
299 { "user-parent", no_argument
, NULL
, OPT_USER_PARENT
},
300 #ifdef HAVE_LIBSELINUX
301 { "follow-context", no_argument
, NULL
, 'Z' },
305 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
309 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
311 struct namespace_file
*nsfile
;
312 int c
, pass
, namespaces
= 0, setgroups_nerrs
= 0, preserve_cred
= 0;
313 bool do_rd
= false, do_wd
= false, do_uid
= false, force_uid
= false,
314 do_gid
= false, force_gid
= false, do_env
= false, do_all
= false,
315 do_user_parent
= false;
316 int do_fork
= -1; /* unknown yet */
321 struct ul_env_list
*envls
;
322 #ifdef HAVE_LIBSELINUX
326 setlocale(LC_ALL
, "");
327 bindtextdomain(PACKAGE
, LOCALEDIR
);
329 close_stdout_atexit();
332 getopt_long(argc
, argv
, "+ahVt:m::u::i::n::p::C::U::T::S:G:r::w::W::eFZ",
333 longopts
, NULL
)) != -1) {
335 err_exclusive_options(c
, longopts
, excl
, excl_st
);
342 namespace_target_pid
=
343 strtoul_or_err(optarg
, _("failed to parse pid"));
347 open_namespace_fd(CLONE_NEWNS
, optarg
);
349 namespaces
|= CLONE_NEWNS
;
353 open_namespace_fd(CLONE_NEWUTS
, optarg
);
355 namespaces
|= CLONE_NEWUTS
;
359 open_namespace_fd(CLONE_NEWIPC
, optarg
);
361 namespaces
|= CLONE_NEWIPC
;
365 open_namespace_fd(CLONE_NEWNET
, optarg
);
367 namespaces
|= CLONE_NEWNET
;
371 open_namespace_fd(CLONE_NEWPID
, optarg
);
373 namespaces
|= CLONE_NEWPID
;
377 open_namespace_fd(CLONE_NEWCGROUP
, optarg
);
379 namespaces
|= CLONE_NEWCGROUP
;
383 open_namespace_fd(CLONE_NEWUSER
, optarg
);
385 namespaces
|= CLONE_NEWUSER
;
389 open_namespace_fd(CLONE_NEWTIME
, optarg
);
391 namespaces
|= CLONE_NEWTIME
;
394 if (strcmp(optarg
, "follow") == 0)
397 uid
= strtoul_or_err(optarg
, _("failed to parse uid"));
401 if (strcmp(optarg
, "follow") == 0)
404 gid
= strtoul_or_err(optarg
, _("failed to parse gid"));
412 open_target_fd(&root_fd
, "root", optarg
);
418 open_target_fd(&wd_fd
, "cwd", optarg
);
428 case OPT_PRESERVE_CRED
:
434 case OPT_USER_PARENT
:
435 do_user_parent
= true;
437 #ifdef HAVE_LIBSELINUX
445 print_version(EXIT_SUCCESS
);
447 errtryhelp(EXIT_FAILURE
);
451 #ifdef HAVE_LIBSELINUX
452 if (selinux
&& is_selinux_enabled() > 0) {
455 if (!namespace_target_pid
)
456 errx(EXIT_FAILURE
, _("no target PID specified for --follow-context"));
457 if (getpidcon(namespace_target_pid
, &scon
) < 0)
458 errx(EXIT_FAILURE
, _("failed to get %d SELinux context"),
459 (int) namespace_target_pid
);
460 if (setexeccon(scon
) < 0)
461 errx(EXIT_FAILURE
, _("failed to set exec context to '%s'"), scon
);
467 if (!namespace_target_pid
)
468 errx(EXIT_FAILURE
, _("no target PID specified for --all"));
469 for (nsfile
= namespace_files
; nsfile
->nstype
; nsfile
++) {
471 continue; /* namespace already specified */
473 if (!is_usable_namespace(namespace_target_pid
, nsfile
))
476 namespaces
|= nsfile
->nstype
;
481 * Open remaining namespace and directory descriptors.
483 for (nsfile
= namespace_files
; nsfile
->nstype
; nsfile
++)
484 if (nsfile
->nstype
& namespaces
)
485 open_namespace_fd(nsfile
->nstype
, NULL
);
487 open_target_fd(&root_fd
, "root", NULL
);
489 open_target_fd(&wd_fd
, "cwd", NULL
);
491 open_target_fd(&env_fd
, "environ", NULL
);
492 if (do_uid
|| do_gid
)
493 open_target_fd(&uid_gid_fd
, "", NULL
);
496 * Get parent userns from any available ns.
499 set_parent_user_ns_fd();
502 * Update namespaces variable to contain all requested namespaces
504 for (nsfile
= namespace_files
; nsfile
->nstype
; nsfile
++) {
507 namespaces
|= nsfile
->nstype
;
510 /* for user namespaces we always set UID and GID (default is 0)
511 * and clear root's groups if --preserve-credentials is no specified */
512 if ((namespaces
& CLONE_NEWUSER
) && !preserve_cred
) {
513 force_uid
= true, force_gid
= true;
515 /* We call setgroups() before and after we enter user namespace,
516 * let's complain only if both fail */
517 if (setgroups(0, NULL
) != 0)
522 * Now that we know which namespaces we want to enter, enter
523 * them. Do this in two passes, not entering the user
524 * namespace on the first pass. So if we're deprivileging the
525 * container we'll enter the user namespace last and if we're
526 * privileging it then we enter the user namespace first
527 * (because the initial setns will fail).
529 for (pass
= 0; pass
< 2; pass
++) {
530 for (nsfile
= namespace_files
+ 1 - pass
; nsfile
->nstype
; nsfile
++) {
533 if (nsfile
->nstype
== CLONE_NEWPID
&& do_fork
== -1)
535 if (setns(nsfile
->fd
, nsfile
->nstype
)) {
538 _("reassociate to namespace '%s' failed"),
549 /* Remember the current working directory if I'm not changing it */
550 if (root_fd
>= 0 && wd_fd
< 0 && wdns
== NULL
) {
551 wd_fd
= open(".", O_RDONLY
);
554 _("cannot open current working directory"));
557 /* Change the root directory */
559 if (fchdir(root_fd
) < 0)
561 _("change directory by root file descriptor failed"));
564 err(EXIT_FAILURE
, _("chroot failed"));
566 err(EXIT_FAILURE
, _("cannot change directory to %s"), "/");
572 /* working directory specified as in-namespace path */
574 wd_fd
= open(wdns
, O_RDONLY
);
577 _("cannot open current working directory"));
580 /* Change the working directory */
582 if (fchdir(wd_fd
) < 0)
584 _("change directory by working directory file descriptor failed"));
590 /* Pass environment variables of the target process to the spawned process */
592 if ((envls
= env_from_fd(env_fd
)) == NULL
)
593 err(EXIT_FAILURE
, _("failed to get environment variables"));
595 if (env_list_setenv(envls
) < 0)
596 err(EXIT_FAILURE
, _("failed to set environment variables"));
597 env_list_free(envls
);
601 if (uid_gid_fd
>= 0) {
604 if (fstat(uid_gid_fd
, &st
) > 0)
605 err(EXIT_FAILURE
, _("can not get process stat"));
619 if (force_uid
|| force_gid
) {
620 if (force_gid
&& setgroups(0, NULL
) != 0 && setgroups_nerrs
) /* drop supplementary groups */
621 err(EXIT_FAILURE
, _("setgroups failed"));
622 if (force_gid
&& setgid(gid
) < 0) /* change GID */
623 err(EXIT_FAILURE
, _("setgid failed"));
624 if (force_uid
&& setuid(uid
) < 0) /* change UID */
625 err(EXIT_FAILURE
, _("setuid failed"));
628 if (keepcaps
&& (namespaces
& CLONE_NEWUSER
))
629 cap_permitted_to_ambient();
632 execvp(argv
[optind
], argv
+ optind
);
633 errexec(argv
[optind
]);