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