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