blob: ea4b6cc8cd200049e677e7321c7a91d2614f7cef [file] [log] [blame]
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001/* Copyright 2018 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6#include <dlfcn.h>
7#include <errno.h>
8#include <getopt.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <sys/capability.h>
13#include <sys/types.h>
14#include <unistd.h>
15
16#include "libminijail.h"
17#include "libsyscalls.h"
18
19#include "elfparse.h"
20#include "minijail0_cli.h"
21#include "system.h"
22#include "util.h"
23
24#define IDMAP_LEN 32U
25#define DEFAULT_TMP_SIZE (64 * 1024 * 1024)
26
27static void set_user(struct minijail *j, const char *arg, uid_t *out_uid,
28 gid_t *out_gid)
29{
30 char *end = NULL;
31 int uid = strtod(arg, &end);
32 if (!*end && *arg) {
33 *out_uid = uid;
34 minijail_change_uid(j, uid);
35 return;
36 }
37
38 if (lookup_user(arg, out_uid, out_gid)) {
39 fprintf(stderr, "Bad user: '%s'\n", arg);
40 exit(1);
41 }
42
43 if (minijail_change_user(j, arg)) {
44 fprintf(stderr, "Bad user: '%s'\n", arg);
45 exit(1);
46 }
47}
48
49static void set_group(struct minijail *j, const char *arg, gid_t *out_gid)
50{
51 char *end = NULL;
52 int gid = strtod(arg, &end);
53 if (!*end && *arg) {
54 *out_gid = gid;
55 minijail_change_gid(j, gid);
56 return;
57 }
58
59 if (lookup_group(arg, out_gid)) {
60 fprintf(stderr, "Bad group: '%s'\n", arg);
61 exit(1);
62 }
63
64 if (minijail_change_group(j, arg)) {
65 fprintf(stderr, "Bad group: '%s'\n", arg);
66 exit(1);
67 }
68}
69
70static void skip_securebits(struct minijail *j, const char *arg)
71{
72 uint64_t securebits_skip_mask;
73 char *end = NULL;
74 securebits_skip_mask = strtoull(arg, &end, 16);
75 if (*end) {
76 fprintf(stderr, "Invalid securebit mask: '%s'\n", arg);
77 exit(1);
78 }
79 minijail_skip_setting_securebits(j, securebits_skip_mask);
80}
81
82static void use_caps(struct minijail *j, const char *arg)
83{
84 uint64_t caps;
85 char *end = NULL;
86 caps = strtoull(arg, &end, 16);
87 if (*end) {
88 fprintf(stderr, "Invalid cap set: '%s'\n", arg);
89 exit(1);
90 }
91 minijail_use_caps(j, caps);
92}
93
94static void add_binding(struct minijail *j, char *arg)
95{
96 char *src = tokenize(&arg, ",");
97 char *dest = tokenize(&arg, ",");
98 char *flags = tokenize(&arg, ",");
99 if (!src || src[0] == '\0' || arg != NULL) {
100 fprintf(stderr, "Bad binding: %s %s\n", src, dest);
101 exit(1);
102 }
103 if (dest == NULL || dest[0] == '\0')
104 dest = src;
105 if (flags == NULL || flags[0] == '\0')
106 flags = "0";
107 if (minijail_bind(j, src, dest, atoi(flags))) {
108 fprintf(stderr, "minijail_bind failed.\n");
109 exit(1);
110 }
111}
112
113static void add_rlimit(struct minijail *j, char *arg)
114{
115 char *type = tokenize(&arg, ",");
116 char *cur = tokenize(&arg, ",");
117 char *max = tokenize(&arg, ",");
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800118 char *end;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500119 if (!type || type[0] == '\0' || !cur || cur[0] == '\0' ||
120 !max || max[0] == '\0' || arg != NULL) {
121 fprintf(stderr, "Bad rlimit '%s'.\n", arg);
122 exit(1);
123 }
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800124 rlim_t cur_rlim;
125 rlim_t max_rlim;
126 if (!strcmp(cur, "unlimited")) {
127 cur_rlim = RLIM_INFINITY;
128 } else {
129 end = NULL;
130 cur_rlim = strtoul(cur, &end, 10);
131 if (*end) {
132 fprintf(stderr, "Bad soft limit: '%s'.\n", cur);
133 exit(1);
134 }
135 }
136 if (!strcmp(max, "unlimited")) {
137 max_rlim = RLIM_INFINITY;
138 } else {
139 end = NULL;
140 max_rlim = strtoul(max, &end, 10);
141 if (*end) {
142 fprintf(stderr, "Bad hard limit: '%s'.\n", max);
143 exit(1);
144 }
145 }
146 if (minijail_rlimit(j, atoi(type), cur_rlim, max_rlim)) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500147 fprintf(stderr, "minijail_rlimit '%s,%s,%s' failed.\n", type,
148 cur, max);
149 exit(1);
150 }
151}
152
153static void add_mount(struct minijail *j, char *arg)
154{
155 char *src = tokenize(&arg, ",");
156 char *dest = tokenize(&arg, ",");
157 char *type = tokenize(&arg, ",");
158 char *flags = tokenize(&arg, ",");
159 char *data = tokenize(&arg, ",");
160 if (!src || src[0] == '\0' || !dest || dest[0] == '\0' ||
161 !type || type[0] == '\0' || arg != NULL) {
162 fprintf(stderr, "Bad mount: %s %s %s\n", src, dest, type);
163 exit(1);
164 }
165 if (minijail_mount_with_data(j, src, dest, type,
166 flags ? strtoul(flags, NULL, 16) : 0,
167 data)) {
168 fprintf(stderr, "minijail_mount failed.\n");
169 exit(1);
170 }
171}
172
173static char *build_idmap(id_t id, id_t lowerid)
174{
175 int ret;
176 char *idmap = malloc(IDMAP_LEN);
177 ret = snprintf(idmap, IDMAP_LEN, "%d %d 1", id, lowerid);
178 if (ret < 0 || (size_t)ret >= IDMAP_LEN) {
179 free(idmap);
180 fprintf(stderr, "Could not build id map.\n");
181 exit(1);
182 }
183 return idmap;
184}
185
186static int has_cap_setgid(void)
187{
188 cap_t caps;
189 cap_flag_value_t cap_value;
190
191 if (!CAP_IS_SUPPORTED(CAP_SETGID))
192 return 0;
193
194 caps = cap_get_proc();
195 if (!caps) {
196 fprintf(stderr, "Could not get process' capabilities: %m\n");
197 exit(1);
198 }
199
200 if (cap_get_flag(caps, CAP_SETGID, CAP_EFFECTIVE, &cap_value)) {
201 fprintf(stderr, "Could not get the value of CAP_SETGID: %m\n");
202 exit(1);
203 }
204
205 if (cap_free(caps)) {
206 fprintf(stderr, "Could not free capabilities: %m\n");
207 exit(1);
208 }
209
210 return cap_value == CAP_SET;
211}
212
213static void set_ugid_mapping(struct minijail *j, int set_uidmap, uid_t uid,
214 char *uidmap, int set_gidmap, gid_t gid,
215 char *gidmap)
216{
217 if (set_uidmap) {
218 minijail_namespace_user(j);
219 minijail_namespace_pids(j);
220
221 if (!uidmap) {
222 /*
223 * If no map is passed, map the current uid to the
224 * chosen uid in the target namespace (or root, if none
225 * was chosen).
226 */
227 uidmap = build_idmap(uid, getuid());
228 }
229 if (0 != minijail_uidmap(j, uidmap)) {
230 fprintf(stderr, "Could not set uid map.\n");
231 exit(1);
232 }
233 free(uidmap);
234 }
235 if (set_gidmap) {
236 minijail_namespace_user(j);
237 minijail_namespace_pids(j);
238
239 if (!gidmap) {
240 /*
241 * If no map is passed, map the current gid to the
242 * chosen gid in the target namespace.
243 */
244 gidmap = build_idmap(gid, getgid());
245 }
246 if (!has_cap_setgid()) {
247 /*
248 * This means that we are not running as root,
249 * so we also have to disable setgroups(2) to
250 * be able to set the gid map.
251 * See
252 * http://man7.org/linux/man-pages/man7/user_namespaces.7.html
253 */
254 minijail_namespace_user_disable_setgroups(j);
255 }
256 if (0 != minijail_gidmap(j, gidmap)) {
257 fprintf(stderr, "Could not set gid map.\n");
258 exit(1);
259 }
260 free(gidmap);
261 }
262}
263
264static void use_chroot(struct minijail *j, const char *path, int *chroot,
265 int pivot_root)
266{
267 if (pivot_root) {
268 fprintf(stderr, "Could not set chroot because "
269 "'-P' was specified.\n");
270 exit(1);
271 }
272 if (minijail_enter_chroot(j, path)) {
273 fprintf(stderr, "Could not set chroot.\n");
274 exit(1);
275 }
276 *chroot = 1;
277}
278
279static void use_pivot_root(struct minijail *j, const char *path,
280 int *pivot_root, int chroot)
281{
282 if (chroot) {
283 fprintf(stderr, "Could not set pivot_root because "
284 "'-C' was specified.\n");
285 exit(1);
286 }
287 if (minijail_enter_pivot_root(j, path)) {
288 fprintf(stderr, "Could not set pivot_root.\n");
289 exit(1);
290 }
291 minijail_namespace_vfs(j);
292 *pivot_root = 1;
293}
294
295static void use_profile(struct minijail *j, const char *profile,
296 int *pivot_root, int chroot, size_t *tmp_size)
297{
Mike Frysinger4d2a81e2018-01-22 16:43:33 -0500298 /* Note: New profiles should be added in minijail0_cli_unittest.cc. */
299
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500300 if (!strcmp(profile, "minimalistic-mountns")) {
301 minijail_namespace_vfs(j);
302 if (minijail_bind(j, "/", "/", 0)) {
303 fprintf(stderr, "minijail_bind failed.\n");
304 exit(1);
305 }
306 if (minijail_bind(j, "/proc", "/proc", 0)) {
307 fprintf(stderr, "minijail_bind failed.\n");
308 exit(1);
309 }
310 minijail_mount_dev(j);
311 if (!*tmp_size) {
312 /* Avoid clobbering |tmp_size| if it was already set. */
313 *tmp_size = DEFAULT_TMP_SIZE;
314 }
315 minijail_remount_proc_readonly(j);
316 use_pivot_root(j, "/var/empty", pivot_root, chroot);
317 } else {
318 fprintf(stderr, "Unrecognized profile name '%s'\n", profile);
319 exit(1);
320 }
321}
322
323static void usage(const char *progn)
324{
325 size_t i;
326 /* clang-format off */
327 printf("Usage: %s [-dGhHiIKlLnNprRstUvyYz]\n"
328 " [-a <table>]\n"
329 " [-b <src>[,<dest>[,<writeable>]]] [-k <src>,<dest>,<type>[,<flags>[,<data>]]]\n"
330 " [-c <caps>] [-C <dir>] [-P <dir>] [-e[file]] [-f <file>] [-g <group>]\n"
331 " [-m[<uid> <loweruid> <count>]*] [-M[<gid> <lowergid> <count>]*] [--profile <name>]\n"
332 " [-R <type,cur,max>] [-S <file>] [-t[size]] [-T <type>] [-u <user>] [-V <file>]\n"
333 " <program> [args...]\n"
334 " -a <table>: Use alternate syscall table <table>.\n"
335 " -b <...>: Bind <src> to <dest> in chroot.\n"
336 " Multiple instances allowed.\n"
337 " -B <mask>: Skip setting securebits in <mask> when restricting capabilities (-c).\n"
338 " By default, SECURE_NOROOT, SECURE_NO_SETUID_FIXUP, and \n"
339 " SECURE_KEEP_CAPS (together with their respective locks) are set.\n"
340 " -k <...>: Mount <src> at <dest> in chroot.\n"
341 " <flags> and <data> can be specified as in mount(2).\n"
342 " Multiple instances allowed.\n"
343 " -c <caps>: Restrict caps to <caps>.\n"
344 " -C <dir>: chroot(2) to <dir>.\n"
345 " Not compatible with -P.\n"
346 " -P <dir>: pivot_root(2) to <dir> (implies -v).\n"
347 " Not compatible with -C.\n"
348 " --mount-dev, Create a new /dev with a minimal set of device nodes (implies -v).\n"
349 " -d: See the minijail0(1) man page for the exact set.\n"
350 " -e[file]: Enter new network namespace, or existing one if |file| is provided.\n"
351 " -f <file>: Write the pid of the jailed process to <file>.\n"
352 " -g <group>: Change gid to <group>.\n"
353 " -G: Inherit supplementary groups from uid.\n"
354 " Not compatible with -y.\n"
355 " -y: Keep uid's supplementary groups.\n"
356 " Not compatible with -G.\n"
357 " -h: Help (this message).\n"
358 " -H: Seccomp filter help message.\n"
359 " -i: Exit immediately after fork (do not act as init).\n"
360 " -I: Run <program> as init (pid 1) inside a new pid namespace (implies -p).\n"
361 " -K: Don't mark all existing mounts as MS_PRIVATE.\n"
362 " -l: Enter new IPC namespace.\n"
363 " -L: Report blocked syscalls to syslog when using seccomp filter.\n"
364 " Forces the following syscalls to be allowed:\n"
365 " ", progn);
366 /* clang-format on */
367 for (i = 0; i < log_syscalls_len; i++)
368 printf("%s ", log_syscalls[i]);
369
370 /* clang-format off */
371 printf("\n"
372 " -m[map]: Set the uid map of a user namespace (implies -pU).\n"
373 " Same arguments as newuidmap(1), multiple mappings should be separated by ',' (comma).\n"
374 " With no mapping, map the current uid to root inside the user namespace.\n"
375 " Not compatible with -b without the 'writable' option.\n"
376 " -M[map]: Set the gid map of a user namespace (implies -pU).\n"
377 " Same arguments as newgidmap(1), multiple mappings should be separated by ',' (comma).\n"
378 " With no mapping, map the current gid to root inside the user namespace.\n"
379 " Not compatible with -b without the 'writable' option.\n"
380 " -n: Set no_new_privs.\n"
381 " -N: Enter a new cgroup namespace.\n"
382 " -p: Enter new pid namespace (implies -vr).\n"
383 " -r: Remount /proc read-only (implies -v).\n"
384 " -R: Set rlimits, can be specified multiple times.\n"
385 " -s: Use seccomp mode 1 (not the same as -S).\n"
386 " -S <file>: Set seccomp filter using <file>.\n"
387 " E.g., '-S /usr/share/filters/<prog>.$(uname -m)'.\n"
388 " Requires -n when not running as root.\n"
389 " -t[size]: Mount tmpfs at /tmp (implies -v).\n"
390 " Optional argument specifies size (default \"64M\").\n"
391 " -T <type>: Assume <program> is a <type> ELF binary; <type> can be 'static' or 'dynamic'.\n"
392 " This will avoid accessing <program> binary before execve(2).\n"
393 " Type 'static' will avoid preload hooking.\n"
394 " -u <user>: Change uid to <user>.\n"
395 " -U: Enter new user namespace (implies -p).\n"
396 " -v: Enter new mount namespace.\n"
397 " -V <file>: Enter specified mount namespace.\n"
398 " -w: Create and join a new anonymous session keyring.\n"
399 " -Y: Synchronize seccomp filters across thread group.\n"
400 " -z: Don't forward signals to jailed process.\n"
401 " --ambient: Raise ambient capabilities. Requires -c.\n"
402 " --uts[=name]: Enter a new UTS namespace (and set hostname).\n"
403 " --logging=<s>:Use <s> as the logging system.\n"
404 " <s> must be 'syslog' (default) or 'stderr'.\n"
405 " --profile <p>,Configure minijail0 to run with the <p> sandboxing profile,\n"
406 " which is a convenient way to express multiple flags\n"
407 " that are typically used together.\n"
408 " See the minijail0(1) man page for the full list.\n");
409 /* clang-format on */
410}
411
412static void seccomp_filter_usage(const char *progn)
413{
414 const struct syscall_entry *entry = syscall_table;
415 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
416 "System call names supported:\n",
417 progn);
418 for (; entry->name && entry->nr >= 0; ++entry)
419 printf(" %s [%d]\n", entry->name, entry->nr);
420 printf("\nSee minijail0(5) for example policies.\n");
421}
422
423int parse_args(struct minijail *j, int argc, char * const argv[],
424 int *exit_immediately, ElfType *elftype)
425{
426 int opt;
427 int use_seccomp_filter = 0;
428 int forward = 1;
429 int binding = 0;
430 int chroot = 0, pivot_root = 0;
431 int mount_ns = 0, skip_remount = 0;
432 int inherit_suppl_gids = 0, keep_suppl_gids = 0;
433 int caps = 0, ambient_caps = 0;
434 int seccomp = -1;
435 const size_t path_max = 4096;
436 uid_t uid = 0;
437 gid_t gid = 0;
438 char *uidmap = NULL, *gidmap = NULL;
439 int set_uidmap = 0, set_gidmap = 0;
440 size_t tmp_size = 0;
441 const char *filter_path = NULL;
442 int log_to_stderr = 0;
443
444 const char *optstring =
445 "+u:g:sS:c:C:P:b:B:V:f:m::M::k:a:e::R:T:vrGhHinNplLt::IUKwyYzd";
446 /* clang-format off */
447 const struct option long_options[] = {
448 {"help", no_argument, 0, 'h'},
449 {"mount-dev", no_argument, 0, 'd'},
450 {"ambient", no_argument, 0, 128},
451 {"uts", optional_argument, 0, 129},
452 {"logging", required_argument, 0, 130},
453 {"profile", required_argument, 0, 131},
454 {0, 0, 0, 0},
455 };
456 /* clang-format on */
457
458 while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) !=
459 -1) {
460 switch (opt) {
461 case 'u':
462 set_user(j, optarg, &uid, &gid);
463 break;
464 case 'g':
465 set_group(j, optarg, &gid);
466 break;
467 case 'n':
468 minijail_no_new_privs(j);
469 break;
470 case 's':
471 if (seccomp != -1 && seccomp != 1) {
472 fprintf(stderr,
473 "Do not use -s & -S together.\n");
474 exit(1);
475 }
476 seccomp = 1;
477 minijail_use_seccomp(j);
478 break;
479 case 'S':
480 if (seccomp != -1 && seccomp != 2) {
481 fprintf(stderr,
482 "Do not use -s & -S together.\n");
483 exit(1);
484 }
485 seccomp = 2;
486 minijail_use_seccomp_filter(j);
487 if (strlen(optarg) >= path_max) {
488 fprintf(stderr, "Filter path is too long.\n");
489 exit(1);
490 }
491 filter_path = strndup(optarg, path_max);
492 if (!filter_path) {
493 fprintf(stderr,
494 "Could not strndup(3) filter path.\n");
495 exit(1);
496 }
497 use_seccomp_filter = 1;
498 break;
499 case 'l':
500 minijail_namespace_ipc(j);
501 break;
502 case 'L':
503 minijail_log_seccomp_filter_failures(j);
504 break;
505 case 'b':
506 add_binding(j, optarg);
507 binding = 1;
508 break;
509 case 'B':
510 skip_securebits(j, optarg);
511 break;
512 case 'c':
513 caps = 1;
514 use_caps(j, optarg);
515 break;
516 case 'C':
517 use_chroot(j, optarg, &chroot, pivot_root);
518 break;
519 case 'k':
520 add_mount(j, optarg);
521 break;
522 case 'K':
523 minijail_skip_remount_private(j);
524 skip_remount = 1;
525 break;
526 case 'P':
527 use_pivot_root(j, optarg, &pivot_root, chroot);
528 break;
529 case 'f':
530 if (0 != minijail_write_pid_file(j, optarg)) {
531 fprintf(stderr,
532 "Could not prepare pid file path.\n");
533 exit(1);
534 }
535 break;
536 case 't':
537 minijail_namespace_vfs(j);
538 if (!tmp_size) {
539 /*
540 * Avoid clobbering |tmp_size| if it was already
541 * set.
542 */
543 tmp_size = DEFAULT_TMP_SIZE;
544 }
545 if (optarg != NULL &&
546 0 != parse_size(&tmp_size, optarg)) {
547 fprintf(stderr, "Invalid /tmp tmpfs size.\n");
548 exit(1);
549 }
550 break;
551 case 'v':
552 minijail_namespace_vfs(j);
553 mount_ns = 1;
554 break;
555 case 'V':
556 minijail_namespace_enter_vfs(j, optarg);
557 break;
558 case 'r':
559 minijail_remount_proc_readonly(j);
560 break;
561 case 'G':
562 if (keep_suppl_gids) {
563 fprintf(stderr,
564 "-y and -G are not compatible.\n");
565 exit(1);
566 }
567 minijail_inherit_usergroups(j);
568 inherit_suppl_gids = 1;
569 break;
570 case 'y':
571 if (inherit_suppl_gids) {
572 fprintf(stderr,
573 "-y and -G are not compatible.\n");
574 exit(1);
575 }
576 minijail_keep_supplementary_gids(j);
577 keep_suppl_gids = 1;
578 break;
579 case 'N':
580 minijail_namespace_cgroups(j);
581 break;
582 case 'p':
583 minijail_namespace_pids(j);
584 break;
585 case 'e':
586 if (optarg)
587 minijail_namespace_enter_net(j, optarg);
588 else
589 minijail_namespace_net(j);
590 break;
591 case 'i':
592 *exit_immediately = 1;
593 break;
594 case 'H':
595 seccomp_filter_usage(argv[0]);
596 exit(0);
597 case 'I':
598 minijail_namespace_pids(j);
599 minijail_run_as_init(j);
600 break;
601 case 'U':
602 minijail_namespace_user(j);
603 minijail_namespace_pids(j);
604 break;
605 case 'm':
606 set_uidmap = 1;
607 if (uidmap) {
608 free(uidmap);
609 uidmap = NULL;
610 }
611 if (optarg)
612 uidmap = strdup(optarg);
613 break;
614 case 'M':
615 set_gidmap = 1;
616 if (gidmap) {
617 free(gidmap);
618 gidmap = NULL;
619 }
620 if (optarg)
621 gidmap = strdup(optarg);
622 break;
623 case 'a':
624 if (0 != minijail_use_alt_syscall(j, optarg)) {
625 fprintf(stderr,
626 "Could not set alt-syscall table.\n");
627 exit(1);
628 }
629 break;
630 case 'R':
631 add_rlimit(j, optarg);
632 break;
633 case 'T':
634 if (!strcmp(optarg, "static"))
635 *elftype = ELFSTATIC;
636 else if (!strcmp(optarg, "dynamic"))
637 *elftype = ELFDYNAMIC;
638 else {
639 fprintf(stderr, "ELF type must be 'static' or "
640 "'dynamic'.\n");
641 exit(1);
642 }
643 break;
644 case 'w':
645 minijail_new_session_keyring(j);
646 break;
647 case 'Y':
648 minijail_set_seccomp_filter_tsync(j);
649 break;
650 case 'z':
651 forward = 0;
652 break;
653 case 'd':
654 minijail_namespace_vfs(j);
655 minijail_mount_dev(j);
656 break;
657 /* Long options. */
658 case 128: /* Ambient caps. */
659 ambient_caps = 1;
660 minijail_set_ambient_caps(j);
661 break;
662 case 129: /* UTS/hostname namespace. */
663 minijail_namespace_uts(j);
664 if (optarg)
665 minijail_namespace_set_hostname(j, optarg);
666 break;
667 case 130: /* Logging. */
668 if (!strcmp(optarg, "syslog"))
669 log_to_stderr = 0;
670 else if (!strcmp(optarg, "stderr")) {
671 log_to_stderr = 1;
672 } else {
673 fprintf(stderr, "--logger must be 'syslog' or "
674 "'stderr'.\n");
675 exit(1);
676 }
677 break;
678 case 131: /* Profile */
679 use_profile(j, optarg, &pivot_root, chroot, &tmp_size);
680 break;
681 default:
682 usage(argv[0]);
683 exit(opt == 'h' ? 0 : 1);
684 }
685 }
686
687 if (log_to_stderr) {
688 init_logging(LOG_TO_FD, STDERR_FILENO, LOG_INFO);
689 /*
690 * When logging to stderr, ensure the FD survives the jailing.
691 */
692 if (0 !=
693 minijail_preserve_fd(j, STDERR_FILENO, STDERR_FILENO)) {
694 fprintf(stderr, "Could not preserve stderr.\n");
695 exit(1);
696 }
697 }
698
699 /* Set up uid/gid mapping. */
700 if (set_uidmap || set_gidmap) {
701 set_ugid_mapping(j, set_uidmap, uid, uidmap, set_gidmap, gid,
702 gidmap);
703 }
704
705 /* Can only set ambient caps when using regular caps. */
706 if (ambient_caps && !caps) {
707 fprintf(stderr, "Can't set ambient capabilities (--ambient) "
708 "without actually using capabilities (-c).\n");
709 exit(1);
710 }
711
712 /* Set up signal handlers in minijail unless asked not to. */
713 if (forward)
714 minijail_forward_signals(j);
715
716 /*
717 * Only allow bind mounts when entering a chroot, using pivot_root, or
718 * a new mount namespace.
719 */
720 if (binding && !(chroot || pivot_root || mount_ns)) {
721 fprintf(stderr, "Bind mounts require a chroot, pivot_root, or "
722 " new mount namespace.\n");
723 exit(1);
724 }
725
726 /*
727 * Remounting / as MS_PRIVATE only happens when entering a new mount
728 * namespace, so skipping it only applies in that case.
729 */
730 if (skip_remount && !mount_ns) {
731 fprintf(stderr, "Can't skip marking mounts as MS_PRIVATE"
732 " without mount namespaces.\n");
733 exit(1);
734 }
735
736 /*
737 * We parse seccomp filters here to make sure we've collected all
738 * cmdline options.
739 */
740 if (use_seccomp_filter) {
741 minijail_parse_seccomp_filters(j, filter_path);
742 free((void *)filter_path);
743 }
744
745 /* Mount a tmpfs under /tmp and set its size. */
746 if (tmp_size)
747 minijail_mount_tmp_size(j, tmp_size);
748
749 /*
750 * There should be at least one additional unparsed argument: the
751 * executable name.
752 */
753 if (argc == optind) {
754 usage(argv[0]);
755 exit(1);
756 }
757
758 if (*elftype == ELFERROR) {
759 /*
760 * -T was not specified.
761 * Get the path to the program adjusted for changing root.
762 */
763 char *program_path =
764 minijail_get_original_path(j, argv[optind]);
765
766 /* Check that we can access the target program. */
767 if (access(program_path, X_OK)) {
768 fprintf(stderr,
769 "Target program '%s' is not accessible.\n",
770 argv[optind]);
771 exit(1);
772 }
773
774 /* Check if target is statically or dynamically linked. */
775 *elftype = get_elf_linkage(program_path);
776 free(program_path);
777 }
778
779 /*
780 * Setting capabilities need either a dynamically-linked binary, or the
781 * use of ambient capabilities for them to be able to survive an
782 * execve(2).
783 */
784 if (caps && *elftype == ELFSTATIC && !ambient_caps) {
785 fprintf(stderr, "Can't run statically-linked binaries with "
786 "capabilities (-c) without also setting "
787 "ambient capabilities. Try passing "
788 "--ambient.\n");
789 exit(1);
790 }
791
792 return optind;
793}