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