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"
49 static struct namespace_file
{
53 } namespace_files
[] = {
54 /* Careful the order is significant in this array.
56 * The user namespace comes either first or last: first if
57 * you're using it to increase your privilege and last if
58 * you're using it to decrease. We enter the namespaces in
59 * two passes starting initially from offset 1 and then offset
62 { .nstype
= CLONE_NEWUSER
, .name
= "ns/user", .fd
= -1 },
63 { .nstype
= CLONE_NEWCGROUP
,.name
= "ns/cgroup", .fd
= -1 },
64 { .nstype
= CLONE_NEWIPC
, .name
= "ns/ipc", .fd
= -1 },
65 { .nstype
= CLONE_NEWUTS
, .name
= "ns/uts", .fd
= -1 },
66 { .nstype
= CLONE_NEWNET
, .name
= "ns/net", .fd
= -1 },
67 { .nstype
= CLONE_NEWPID
, .name
= "ns/pid", .fd
= -1 },
68 { .nstype
= CLONE_NEWNS
, .name
= "ns/mnt", .fd
= -1 },
69 { .nstype
= CLONE_NEWTIME
, .name
= "ns/time", .fd
= -1 },
70 { .nstype
= 0, .name
= NULL
, .fd
= -1 }
73 static void __attribute__((__noreturn__
)) usage(void)
77 fputs(USAGE_HEADER
, out
);
78 fprintf(out
, _(" %s [options] [<program> [<argument>...]]\n"),
79 program_invocation_short_name
);
81 fputs(USAGE_SEPARATOR
, out
);
82 fputs(_("Run a program with namespaces of other processes.\n"), out
);
84 fputs(USAGE_OPTIONS
, out
);
85 fputs(_(" -a, --all enter all namespaces\n"), out
);
86 fputs(_(" -t, --target <pid> target process to get namespaces from\n"), out
);
87 fputs(_(" -m, --mount[=<file>] enter mount namespace\n"), out
);
88 fputs(_(" -u, --uts[=<file>] enter UTS namespace (hostname etc)\n"), out
);
89 fputs(_(" -i, --ipc[=<file>] enter System V IPC namespace\n"), out
);
90 fputs(_(" -n, --net[=<file>] enter network namespace\n"), out
);
91 fputs(_(" -p, --pid[=<file>] enter pid namespace\n"), out
);
92 fputs(_(" -C, --cgroup[=<file>] enter cgroup namespace\n"), out
);
93 fputs(_(" -U, --user[=<file>] enter user namespace\n"), out
);
94 fputs(_(" -T, --time[=<file>] enter time namespace\n"), out
);
95 fputs(_(" -S, --setuid[=<uid>] set uid in entered namespace\n"), out
);
96 fputs(_(" -G, --setgid[=<gid>] set gid in entered namespace\n"), out
);
97 fputs(_(" --preserve-credentials do not touch uids or gids\n"), out
);
98 fputs(_(" -r, --root[=<dir>] set the root directory\n"), out
);
99 fputs(_(" -w, --wd[=<dir>] set the working directory\n"), out
);
100 fputs(_(" -W, --wdns <dir> set the working directory in namespace\n"), out
);
101 fputs(_(" -e, --env inherit environment variables from target process\n"), out
);
102 fputs(_(" -F, --no-fork do not fork before exec'ing <program>\n"), out
);
103 #ifdef HAVE_LIBSELINUX
104 fputs(_(" -Z, --follow-context set SELinux context according to --target PID\n"), out
);
107 fputs(USAGE_SEPARATOR
, out
);
108 printf(USAGE_HELP_OPTIONS(24));
109 printf(USAGE_MAN_TAIL("nsenter(1)"));
114 static pid_t namespace_target_pid
= 0;
115 static int root_fd
= -1;
116 static int wd_fd
= -1;
117 static int env_fd
= -1;
118 static int uid_gid_fd
= -1;
120 static void open_target_fd(int *fd
, const char *type
, const char *path
)
122 char pathbuf
[PATH_MAX
];
124 if (!path
&& namespace_target_pid
) {
125 snprintf(pathbuf
, sizeof(pathbuf
), "/proc/%u/%s",
126 namespace_target_pid
, type
);
131 _("neither filename nor target pid supplied for %s"),
137 *fd
= open(path
, O_RDONLY
);
139 err(EXIT_FAILURE
, _("cannot open %s"), path
);
142 static void open_namespace_fd(int nstype
, const char *path
)
144 struct namespace_file
*nsfile
;
146 for (nsfile
= namespace_files
; nsfile
->nstype
; nsfile
++) {
147 if (nstype
!= nsfile
->nstype
)
150 open_target_fd(&nsfile
->fd
, nsfile
->name
, path
);
153 /* This should never happen */
154 assert(nsfile
->nstype
);
157 static int get_ns_ino(const char *path
, ino_t
*ino
)
161 if (stat(path
, &st
) != 0)
167 static int is_usable_namespace(pid_t target
, const struct namespace_file
*nsfile
)
173 /* Check NS accessibility */
174 snprintf(path
, sizeof(path
), "/proc/%u/%s", getpid(), nsfile
->name
);
175 rc
= get_ns_ino(path
, &my_ino
);
177 return false; /* Unsupported NS */
179 /* It is not permitted to use setns(2) to reenter the caller's
180 * current user namespace; see setns(2) man page for more details.
182 if (nsfile
->nstype
& CLONE_NEWUSER
) {
183 ino_t target_ino
= 0;
185 snprintf(path
, sizeof(path
), "/proc/%u/%s", target
, nsfile
->name
);
186 if (get_ns_ino(path
, &target_ino
) != 0)
187 err(EXIT_FAILURE
, _("stat of %s failed"), path
);
189 if (my_ino
== target_ino
)
193 return true; /* All pass */
196 static void continue_as_child(void)
202 /* Clear any inherited settings */
203 signal(SIGCHLD
, SIG_DFL
);
207 err(EXIT_FAILURE
, _("fork failed"));
209 /* Only the child returns */
214 ret
= waitpid(child
, &status
, WUNTRACED
);
215 if ((ret
== child
) && (WIFSTOPPED(status
))) {
216 /* The child suspended so suspend us as well */
217 kill(getpid(), SIGSTOP
);
218 kill(child
, SIGCONT
);
223 /* Return the child's exit code if possible */
224 if (WIFEXITED(status
)) {
225 exit(WEXITSTATUS(status
));
226 } else if (WIFSIGNALED(status
)) {
227 kill(getpid(), WTERMSIG(status
));
232 int main(int argc
, char *argv
[])
235 OPT_PRESERVE_CRED
= CHAR_MAX
+ 1
237 static const struct option longopts
[] = {
238 { "all", no_argument
, NULL
, 'a' },
239 { "help", no_argument
, NULL
, 'h' },
240 { "version", no_argument
, NULL
, 'V'},
241 { "target", required_argument
, NULL
, 't' },
242 { "mount", optional_argument
, NULL
, 'm' },
243 { "uts", optional_argument
, NULL
, 'u' },
244 { "ipc", optional_argument
, NULL
, 'i' },
245 { "net", optional_argument
, NULL
, 'n' },
246 { "pid", optional_argument
, NULL
, 'p' },
247 { "user", optional_argument
, NULL
, 'U' },
248 { "cgroup", optional_argument
, NULL
, 'C' },
249 { "time", optional_argument
, NULL
, 'T' },
250 { "setuid", optional_argument
, NULL
, 'S' },
251 { "setgid", optional_argument
, NULL
, 'G' },
252 { "root", optional_argument
, NULL
, 'r' },
253 { "wd", optional_argument
, NULL
, 'w' },
254 { "wdns", optional_argument
, NULL
, 'W' },
255 { "env", no_argument
, NULL
, 'e' },
256 { "no-fork", no_argument
, NULL
, 'F' },
257 { "preserve-credentials", no_argument
, NULL
, OPT_PRESERVE_CRED
},
258 #ifdef HAVE_LIBSELINUX
259 { "follow-context", no_argument
, NULL
, 'Z' },
263 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
267 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
269 struct namespace_file
*nsfile
;
270 int c
, pass
, namespaces
= 0, setgroups_nerrs
= 0, preserve_cred
= 0;
271 bool do_rd
= false, do_wd
= false, do_uid
= false, force_uid
= false,
272 do_gid
= false, force_gid
= false, do_env
= false, do_all
= false;
273 int do_fork
= -1; /* unknown yet */
277 struct ul_env_list
*envls
;
278 #ifdef HAVE_LIBSELINUX
282 setlocale(LC_ALL
, "");
283 bindtextdomain(PACKAGE
, LOCALEDIR
);
285 close_stdout_atexit();
288 getopt_long(argc
, argv
, "+ahVt:m::u::i::n::p::C::U::T::S::G::r::w::W::eFZ",
289 longopts
, NULL
)) != -1) {
291 err_exclusive_options(c
, longopts
, excl
, excl_st
);
298 namespace_target_pid
=
299 strtoul_or_err(optarg
, _("failed to parse pid"));
303 open_namespace_fd(CLONE_NEWNS
, optarg
);
305 namespaces
|= CLONE_NEWNS
;
309 open_namespace_fd(CLONE_NEWUTS
, optarg
);
311 namespaces
|= CLONE_NEWUTS
;
315 open_namespace_fd(CLONE_NEWIPC
, optarg
);
317 namespaces
|= CLONE_NEWIPC
;
321 open_namespace_fd(CLONE_NEWNET
, optarg
);
323 namespaces
|= CLONE_NEWNET
;
327 open_namespace_fd(CLONE_NEWPID
, optarg
);
329 namespaces
|= CLONE_NEWPID
;
333 open_namespace_fd(CLONE_NEWCGROUP
, optarg
);
335 namespaces
|= CLONE_NEWCGROUP
;
339 open_namespace_fd(CLONE_NEWUSER
, optarg
);
341 namespaces
|= CLONE_NEWUSER
;
345 open_namespace_fd(CLONE_NEWTIME
, optarg
);
347 namespaces
|= CLONE_NEWTIME
;
351 uid
= strtoul_or_err(optarg
, _("failed to parse uid"));
358 gid
= strtoul_or_err(optarg
, _("failed to parse gid"));
368 open_target_fd(&root_fd
, "root", optarg
);
374 open_target_fd(&wd_fd
, "cwd", optarg
);
384 case OPT_PRESERVE_CRED
:
387 #ifdef HAVE_LIBSELINUX
395 print_version(EXIT_SUCCESS
);
397 errtryhelp(EXIT_FAILURE
);
401 #ifdef HAVE_LIBSELINUX
402 if (selinux
&& is_selinux_enabled() > 0) {
405 if (!namespace_target_pid
)
406 errx(EXIT_FAILURE
, _("no target PID specified for --follow-context"));
407 if (getpidcon(namespace_target_pid
, &scon
) < 0)
408 errx(EXIT_FAILURE
, _("failed to get %d SELinux context"),
409 (int) namespace_target_pid
);
410 if (setexeccon(scon
) < 0)
411 errx(EXIT_FAILURE
, _("failed to set exec context to '%s'"), scon
);
417 if (!namespace_target_pid
)
418 errx(EXIT_FAILURE
, _("no target PID specified for --all"));
419 for (nsfile
= namespace_files
; nsfile
->nstype
; nsfile
++) {
421 continue; /* namespace already specified */
423 if (!is_usable_namespace(namespace_target_pid
, nsfile
))
426 namespaces
|= nsfile
->nstype
;
431 * Open remaining namespace and directory descriptors.
433 for (nsfile
= namespace_files
; nsfile
->nstype
; nsfile
++)
434 if (nsfile
->nstype
& namespaces
)
435 open_namespace_fd(nsfile
->nstype
, NULL
);
437 open_target_fd(&root_fd
, "root", NULL
);
439 open_target_fd(&wd_fd
, "cwd", NULL
);
441 open_target_fd(&env_fd
, "environ", NULL
);
442 if (do_uid
|| do_gid
)
443 open_target_fd(&uid_gid_fd
, "", NULL
);
446 * Update namespaces variable to contain all requested namespaces
448 for (nsfile
= namespace_files
; nsfile
->nstype
; nsfile
++) {
451 namespaces
|= nsfile
->nstype
;
454 /* for user namespaces we always set UID and GID (default is 0)
455 * and clear root's groups if --preserve-credentials is no specified */
456 if ((namespaces
& CLONE_NEWUSER
) && !preserve_cred
) {
457 force_uid
= true, force_gid
= true;
459 /* We call setgroups() before and after we enter user namespace,
460 * let's complain only if both fail */
461 if (setgroups(0, NULL
) != 0)
466 * Now that we know which namespaces we want to enter, enter
467 * them. Do this in two passes, not entering the user
468 * namespace on the first pass. So if we're deprivileging the
469 * container we'll enter the user namespace last and if we're
470 * privileging it then we enter the user namespace first
471 * (because the initial setns will fail).
473 for (pass
= 0; pass
< 2; pass
++) {
474 for (nsfile
= namespace_files
+ 1 - pass
; nsfile
->nstype
; nsfile
++) {
477 if (nsfile
->nstype
== CLONE_NEWPID
&& do_fork
== -1)
479 if (setns(nsfile
->fd
, nsfile
->nstype
)) {
482 _("reassociate to namespace '%s' failed"),
493 /* Remember the current working directory if I'm not changing it */
494 if (root_fd
>= 0 && wd_fd
< 0 && wdns
== NULL
) {
495 wd_fd
= open(".", O_RDONLY
);
498 _("cannot open current working directory"));
501 /* Change the root directory */
503 if (fchdir(root_fd
) < 0)
505 _("change directory by root file descriptor failed"));
508 err(EXIT_FAILURE
, _("chroot failed"));
510 err(EXIT_FAILURE
, _("cannot change directory to %s"), "/");
516 /* working directory specified as in-namespace path */
518 wd_fd
= open(wdns
, O_RDONLY
);
521 _("cannot open current working directory"));
524 /* Change the working directory */
526 if (fchdir(wd_fd
) < 0)
528 _("change directory by working directory file descriptor failed"));
534 /* Pass environment variables of the target process to the spawned process */
536 if ((envls
= env_from_fd(env_fd
)) == NULL
)
537 err(EXIT_FAILURE
, _("failed to get environment variables"));
539 if (env_list_setenv(envls
) < 0)
540 err(EXIT_FAILURE
, _("failed to set environment variables"));
541 env_list_free(envls
);
545 if (uid_gid_fd
>= 0) {
548 if (fstat(uid_gid_fd
, &st
) > 0)
549 err(EXIT_FAILURE
, _("can not get process stat"));
563 if (force_uid
|| force_gid
) {
564 if (force_gid
&& setgroups(0, NULL
) != 0 && setgroups_nerrs
) /* drop supplementary groups */
565 err(EXIT_FAILURE
, _("setgroups failed"));
566 if (force_gid
&& setgid(gid
) < 0) /* change GID */
567 err(EXIT_FAILURE
, _("setgid failed"));
568 if (force_uid
&& setuid(uid
) < 0) /* change UID */
569 err(EXIT_FAILURE
, _("setuid failed"));
573 execvp(argv
[optind
], argv
+ optind
);
574 errexec(argv
[optind
]);