]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/nsenter.c
misc: consolidate version printing and close_stdout()
[thirdparty/util-linux.git] / sys-utils / nsenter.c
1 /*
2 * nsenter(1) - command-line interface for setns(2)
3 *
4 * Copyright (C) 2012-2013 Eric Biederman <ebiederm@xmission.com>
5 *
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.
9 *
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.
14 *
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.
18 */
19
20 #include <dirent.h>
21 #include <errno.h>
22 #include <getopt.h>
23 #include <sched.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdbool.h>
27 #include <unistd.h>
28 #include <assert.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <grp.h>
32 #include <sys/stat.h>
33
34 #ifdef HAVE_LIBSELINUX
35 # include <selinux/selinux.h>
36 #endif
37
38 #include "strutils.h"
39 #include "nls.h"
40 #include "c.h"
41 #include "closestream.h"
42 #include "namespace.h"
43 #include "exec_shell.h"
44
45 static struct namespace_file {
46 int nstype;
47 const char *name;
48 int fd;
49 } namespace_files[] = {
50 /* Careful the order is significant in this array.
51 *
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
56 * 0 if that fails.
57 */
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 }
66 };
67
68 static void __attribute__((__noreturn__)) usage(void)
69 {
70 FILE *out = stdout;
71
72 fputs(USAGE_HEADER, out);
73 fprintf(out, _(" %s [options] [<program> [<argument>...]]\n"),
74 program_invocation_short_name);
75
76 fputs(USAGE_SEPARATOR, out);
77 fputs(_("Run a program with namespaces of other processes.\n"), out);
78
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);
97 #endif
98
99 fputs(USAGE_SEPARATOR, out);
100 printf(USAGE_HELP_OPTIONS(24));
101 printf(USAGE_MAN_TAIL("nsenter(1)"));
102
103 exit(EXIT_SUCCESS);
104 }
105
106 static pid_t namespace_target_pid = 0;
107 static int root_fd = -1;
108 static int wd_fd = -1;
109
110 static void open_target_fd(int *fd, const char *type, const char *path)
111 {
112 char pathbuf[PATH_MAX];
113
114 if (!path && namespace_target_pid) {
115 snprintf(pathbuf, sizeof(pathbuf), "/proc/%u/%s",
116 namespace_target_pid, type);
117 path = pathbuf;
118 }
119 if (!path)
120 errx(EXIT_FAILURE,
121 _("neither filename nor target pid supplied for %s"),
122 type);
123
124 if (*fd >= 0)
125 close(*fd);
126
127 *fd = open(path, O_RDONLY);
128 if (*fd < 0)
129 err(EXIT_FAILURE, _("cannot open %s"), path);
130 }
131
132 static void open_namespace_fd(int nstype, const char *path)
133 {
134 struct namespace_file *nsfile;
135
136 for (nsfile = namespace_files; nsfile->nstype; nsfile++) {
137 if (nstype != nsfile->nstype)
138 continue;
139
140 open_target_fd(&nsfile->fd, nsfile->name, path);
141 return;
142 }
143 /* This should never happen */
144 assert(nsfile->nstype);
145 }
146
147 static int get_ns_ino(const char *path, ino_t *ino)
148 {
149 struct stat st;
150
151 if (stat(path, &st) != 0)
152 return -errno;
153 *ino = st.st_ino;
154 return 0;
155 }
156
157 static int is_same_namespace(pid_t a, pid_t b, const char *type)
158 {
159 char path[PATH_MAX];
160 ino_t a_ino = 0, b_ino = 0;
161
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);
165
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);
169
170 return a_ino == b_ino;
171 }
172
173 static void continue_as_child(void)
174 {
175 pid_t child = fork();
176 int status;
177 pid_t ret;
178
179 if (child < 0)
180 err(EXIT_FAILURE, _("fork failed"));
181
182 /* Only the child returns */
183 if (child == 0)
184 return;
185
186 for (;;) {
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);
192 } else {
193 break;
194 }
195 }
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));
201 }
202 exit(EXIT_FAILURE);
203 }
204
205 int main(int argc, char *argv[])
206 {
207 enum {
208 OPT_PRESERVE_CRED = CHAR_MAX + 1
209 };
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' },
230 #endif
231 { NULL, 0, NULL, 0 }
232 };
233
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;
237 bool do_all = false;
238 int do_fork = -1; /* unknown yet */
239 uid_t uid = 0;
240 gid_t gid = 0;
241 #ifdef HAVE_LIBSELINUX
242 bool selinux = 0;
243 #endif
244
245 setlocale(LC_ALL, "");
246 bindtextdomain(PACKAGE, LOCALEDIR);
247 textdomain(PACKAGE);
248 close_stdout_atexit();
249
250 while ((c =
251 getopt_long(argc, argv, "+ahVt:m::u::i::n::p::C::U::S:G:r::w::FZ",
252 longopts, NULL)) != -1) {
253 switch (c) {
254 case 'a':
255 do_all = true;
256 break;
257 case 't':
258 namespace_target_pid =
259 strtoul_or_err(optarg, _("failed to parse pid"));
260 break;
261 case 'm':
262 if (optarg)
263 open_namespace_fd(CLONE_NEWNS, optarg);
264 else
265 namespaces |= CLONE_NEWNS;
266 break;
267 case 'u':
268 if (optarg)
269 open_namespace_fd(CLONE_NEWUTS, optarg);
270 else
271 namespaces |= CLONE_NEWUTS;
272 break;
273 case 'i':
274 if (optarg)
275 open_namespace_fd(CLONE_NEWIPC, optarg);
276 else
277 namespaces |= CLONE_NEWIPC;
278 break;
279 case 'n':
280 if (optarg)
281 open_namespace_fd(CLONE_NEWNET, optarg);
282 else
283 namespaces |= CLONE_NEWNET;
284 break;
285 case 'p':
286 if (optarg)
287 open_namespace_fd(CLONE_NEWPID, optarg);
288 else
289 namespaces |= CLONE_NEWPID;
290 break;
291 case 'C':
292 if (optarg)
293 open_namespace_fd(CLONE_NEWCGROUP, optarg);
294 else
295 namespaces |= CLONE_NEWCGROUP;
296 break;
297 case 'U':
298 if (optarg)
299 open_namespace_fd(CLONE_NEWUSER, optarg);
300 else
301 namespaces |= CLONE_NEWUSER;
302 break;
303 case 'S':
304 uid = strtoul_or_err(optarg, _("failed to parse uid"));
305 force_uid = true;
306 break;
307 case 'G':
308 gid = strtoul_or_err(optarg, _("failed to parse gid"));
309 force_gid = true;
310 break;
311 case 'F':
312 do_fork = 0;
313 break;
314 case 'r':
315 if (optarg)
316 open_target_fd(&root_fd, "root", optarg);
317 else
318 do_rd = true;
319 break;
320 case 'w':
321 if (optarg)
322 open_target_fd(&wd_fd, "cwd", optarg);
323 else
324 do_wd = true;
325 break;
326 case OPT_PRESERVE_CRED:
327 preserve_cred = 1;
328 break;
329 #ifdef HAVE_LIBSELINUX
330 case 'Z':
331 selinux = 1;
332 break;
333 #endif
334 case 'h':
335 usage();
336 case 'V':
337 print_version(EXIT_SUCCESS);
338 default:
339 errtryhelp(EXIT_FAILURE);
340 }
341 }
342
343 #ifdef HAVE_LIBSELINUX
344 if (selinux && is_selinux_enabled() > 0) {
345 char *scon = NULL;
346
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);
354 freecon(scon);
355 }
356 #endif
357
358 if (do_all) {
359 if (!namespace_target_pid)
360 errx(EXIT_FAILURE, _("no target PID specified for --all"));
361 for (nsfile = namespace_files; nsfile->nstype; nsfile++) {
362 if (nsfile->fd >= 0)
363 continue; /* namespace already specified */
364
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.
367 */
368 if (nsfile->nstype & CLONE_NEWUSER
369 && is_same_namespace(getpid(), namespace_target_pid, nsfile->name))
370 continue;
371
372 namespaces |= nsfile->nstype;
373 }
374 }
375
376 /*
377 * Open remaining namespace and directory descriptors.
378 */
379 for (nsfile = namespace_files; nsfile->nstype; nsfile++)
380 if (nsfile->nstype & namespaces)
381 open_namespace_fd(nsfile->nstype, NULL);
382 if (do_rd)
383 open_target_fd(&root_fd, "root", NULL);
384 if (do_wd)
385 open_target_fd(&wd_fd, "cwd", NULL);
386
387 /*
388 * Update namespaces variable to contain all requested namespaces
389 */
390 for (nsfile = namespace_files; nsfile->nstype; nsfile++) {
391 if (nsfile->fd < 0)
392 continue;
393 namespaces |= nsfile->nstype;
394 }
395
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;
400
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)
404 setgroups_nerrs++;
405 }
406
407 /*
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).
414 */
415 for (pass = 0; pass < 2; pass ++) {
416 for (nsfile = namespace_files + 1 - pass; nsfile->nstype; nsfile++) {
417 if (nsfile->fd < 0)
418 continue;
419 if (nsfile->nstype == CLONE_NEWPID && do_fork == -1)
420 do_fork = 1;
421 if (setns(nsfile->fd, nsfile->nstype)) {
422 if (pass != 0)
423 err(EXIT_FAILURE,
424 _("reassociate to namespace '%s' failed"),
425 nsfile->name);
426 else
427 continue;
428 }
429
430 close(nsfile->fd);
431 nsfile->fd = -1;
432 }
433 }
434
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);
438 if (wd_fd < 0)
439 err(EXIT_FAILURE,
440 _("cannot open current working directory"));
441 }
442
443 /* Change the root directory */
444 if (root_fd >= 0) {
445 if (fchdir(root_fd) < 0)
446 err(EXIT_FAILURE,
447 _("change directory by root file descriptor failed"));
448
449 if (chroot(".") < 0)
450 err(EXIT_FAILURE, _("chroot failed"));
451
452 close(root_fd);
453 root_fd = -1;
454 }
455
456 /* Change the working directory */
457 if (wd_fd >= 0) {
458 if (fchdir(wd_fd) < 0)
459 err(EXIT_FAILURE,
460 _("change directory by working directory file descriptor failed"));
461
462 close(wd_fd);
463 wd_fd = -1;
464 }
465
466 if (do_fork == 1)
467 continue_as_child();
468
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"));
476 }
477
478 if (optind < argc) {
479 execvp(argv[optind], argv + optind);
480 errexec(argv[optind]);
481 }
482 exec_shell();
483 }