blob: f19a053a674b082d325bee65f2e6dfe3533dbc0a [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>
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07009#include <inttypes.h>
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -080010#include <stdbool.h>
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050011#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/capability.h>
Mike Frysinger785b1c32018-02-23 15:47:24 -050015#include <sys/mount.h>
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050016#include <sys/types.h>
17#include <unistd.h>
18
Luis Hector Chavezc3e17722018-10-16 20:43:12 -070019#include <linux/filter.h>
20
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050021#include "libminijail.h"
22#include "libsyscalls.h"
23
24#include "elfparse.h"
25#include "minijail0_cli.h"
26#include "system.h"
27#include "util.h"
28
29#define IDMAP_LEN 32U
30#define DEFAULT_TMP_SIZE (64 * 1024 * 1024)
31
32static void set_user(struct minijail *j, const char *arg, uid_t *out_uid,
33 gid_t *out_gid)
34{
35 char *end = NULL;
36 int uid = strtod(arg, &end);
37 if (!*end && *arg) {
38 *out_uid = uid;
39 minijail_change_uid(j, uid);
40 return;
41 }
42
43 if (lookup_user(arg, out_uid, out_gid)) {
44 fprintf(stderr, "Bad user: '%s'\n", arg);
45 exit(1);
46 }
47
48 if (minijail_change_user(j, arg)) {
49 fprintf(stderr, "Bad user: '%s'\n", arg);
50 exit(1);
51 }
52}
53
54static void set_group(struct minijail *j, const char *arg, gid_t *out_gid)
55{
56 char *end = NULL;
57 int gid = strtod(arg, &end);
58 if (!*end && *arg) {
59 *out_gid = gid;
60 minijail_change_gid(j, gid);
61 return;
62 }
63
64 if (lookup_group(arg, out_gid)) {
65 fprintf(stderr, "Bad group: '%s'\n", arg);
66 exit(1);
67 }
68
69 if (minijail_change_group(j, arg)) {
70 fprintf(stderr, "Bad group: '%s'\n", arg);
71 exit(1);
72 }
73}
74
75static void skip_securebits(struct minijail *j, const char *arg)
76{
77 uint64_t securebits_skip_mask;
78 char *end = NULL;
79 securebits_skip_mask = strtoull(arg, &end, 16);
80 if (*end) {
81 fprintf(stderr, "Invalid securebit mask: '%s'\n", arg);
82 exit(1);
83 }
84 minijail_skip_setting_securebits(j, securebits_skip_mask);
85}
86
87static void use_caps(struct minijail *j, const char *arg)
88{
Luis Hector Chavezdabc4302018-09-21 09:21:47 -070089 uint64_t caps = 0;
90 cap_t parsed_caps = cap_from_text(arg);
91
92 if (parsed_caps != NULL) {
93 unsigned int i;
94 const uint64_t one = 1;
95 cap_flag_value_t cap_value;
96 unsigned int last_valid_cap = get_last_valid_cap();
97
98 for (i = 0; i <= last_valid_cap; ++i) {
99 if (cap_get_flag(parsed_caps, i, CAP_EFFECTIVE,
100 &cap_value)) {
Luis Hector Chavez677900f2018-09-24 09:13:26 -0700101 if (errno == EINVAL) {
102 /*
103 * Some versions of libcap reject any
104 * capabilities they were not compiled
105 * with by returning EINVAL.
106 */
107 continue;
108 }
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700109 fprintf(stderr,
110 "Could not get the value of "
111 "the %d-th capability: %m\n",
112 i);
113 exit(1);
114 }
115 if (cap_value == CAP_SET)
116 caps |= (one << i);
117 }
118 cap_free(parsed_caps);
119 } else {
120 char *end = NULL;
121 caps = strtoull(arg, &end, 16);
122 if (*end) {
123 fprintf(stderr, "Invalid cap set: '%s'\n", arg);
124 exit(1);
125 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500126 }
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700127
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500128 minijail_use_caps(j, caps);
129}
130
131static void add_binding(struct minijail *j, char *arg)
132{
133 char *src = tokenize(&arg, ",");
134 char *dest = tokenize(&arg, ",");
135 char *flags = tokenize(&arg, ",");
136 if (!src || src[0] == '\0' || arg != NULL) {
137 fprintf(stderr, "Bad binding: %s %s\n", src, dest);
138 exit(1);
139 }
140 if (dest == NULL || dest[0] == '\0')
141 dest = src;
David Coles87ec5cd2019-06-13 17:20:10 -0700142 int writable;
143 if (flags == NULL || flags[0] == '\0' || !strcmp(flags, "0"))
144 writable = 0;
145 else if (!strcmp(flags, "1"))
146 writable = 1;
147 else {
148 fprintf(stderr, "Bad value for <writable>: %s\n", flags);
149 exit(1);
150 }
151 if (minijail_bind(j, src, dest, writable)) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500152 fprintf(stderr, "minijail_bind failed.\n");
153 exit(1);
154 }
155}
156
157static void add_rlimit(struct minijail *j, char *arg)
158{
159 char *type = tokenize(&arg, ",");
160 char *cur = tokenize(&arg, ",");
161 char *max = tokenize(&arg, ",");
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800162 char *end;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500163 if (!type || type[0] == '\0' || !cur || cur[0] == '\0' ||
164 !max || max[0] == '\0' || arg != NULL) {
165 fprintf(stderr, "Bad rlimit '%s'.\n", arg);
166 exit(1);
167 }
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800168 rlim_t cur_rlim;
169 rlim_t max_rlim;
170 if (!strcmp(cur, "unlimited")) {
171 cur_rlim = RLIM_INFINITY;
172 } else {
173 end = NULL;
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400174 cur_rlim = strtoul(cur, &end, 0);
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800175 if (*end) {
176 fprintf(stderr, "Bad soft limit: '%s'.\n", cur);
177 exit(1);
178 }
179 }
180 if (!strcmp(max, "unlimited")) {
181 max_rlim = RLIM_INFINITY;
182 } else {
183 end = NULL;
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400184 max_rlim = strtoul(max, &end, 0);
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800185 if (*end) {
186 fprintf(stderr, "Bad hard limit: '%s'.\n", max);
187 exit(1);
188 }
189 }
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400190
191 end = NULL;
192 int resource = parse_single_constant(type, &end);
193 if (type == end) {
194 fprintf(stderr, "Bad rlimit: '%s'.\n", type);
195 exit(1);
196 }
197
198 if (minijail_rlimit(j, resource, cur_rlim, max_rlim)) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500199 fprintf(stderr, "minijail_rlimit '%s,%s,%s' failed.\n", type,
200 cur, max);
201 exit(1);
202 }
203}
204
205static void add_mount(struct minijail *j, char *arg)
206{
207 char *src = tokenize(&arg, ",");
208 char *dest = tokenize(&arg, ",");
209 char *type = tokenize(&arg, ",");
210 char *flags = tokenize(&arg, ",");
211 char *data = tokenize(&arg, ",");
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400212 char *end;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500213 if (!src || src[0] == '\0' || !dest || dest[0] == '\0' ||
Mike Frysinger4f3e09f2018-01-24 18:01:16 -0500214 !type || type[0] == '\0') {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500215 fprintf(stderr, "Bad mount: %s %s %s\n", src, dest, type);
216 exit(1);
217 }
Mike Frysinger4f3e09f2018-01-24 18:01:16 -0500218
219 /*
220 * Fun edge case: the data option itself is comma delimited. If there
221 * were no more options, then arg would be set to NULL. But if we had
222 * more pending, it'll be pointing to the next token. Back up and undo
223 * the null byte so it'll be merged back.
224 * An example:
225 * none,/tmp,tmpfs,0xe,mode=0755,uid=10,gid=10
226 * The tokenize calls above will turn this memory into:
227 * none\0/tmp\0tmpfs\00xe\0mode=0755\0uid=10,gid=10
228 * With data pointing at mode=0755 and arg pointing at uid=10,gid=10.
229 */
230 if (arg != NULL)
231 arg[-1] = ',';
232
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400233 unsigned long mountflags;
234 if (flags == NULL || flags[0] == '\0') {
235 mountflags = 0;
236 } else {
237 end = NULL;
238 mountflags = parse_constant(flags, &end);
239 if (flags == end) {
240 fprintf(stderr, "Bad mount flags: %s\n", flags);
241 exit(1);
242 }
243 }
244
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500245 if (minijail_mount_with_data(j, src, dest, type,
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400246 mountflags, data)) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500247 fprintf(stderr, "minijail_mount failed.\n");
248 exit(1);
249 }
250}
251
252static char *build_idmap(id_t id, id_t lowerid)
253{
254 int ret;
255 char *idmap = malloc(IDMAP_LEN);
256 ret = snprintf(idmap, IDMAP_LEN, "%d %d 1", id, lowerid);
257 if (ret < 0 || (size_t)ret >= IDMAP_LEN) {
258 free(idmap);
259 fprintf(stderr, "Could not build id map.\n");
260 exit(1);
261 }
262 return idmap;
263}
264
265static int has_cap_setgid(void)
266{
267 cap_t caps;
268 cap_flag_value_t cap_value;
269
270 if (!CAP_IS_SUPPORTED(CAP_SETGID))
271 return 0;
272
273 caps = cap_get_proc();
274 if (!caps) {
275 fprintf(stderr, "Could not get process' capabilities: %m\n");
276 exit(1);
277 }
278
279 if (cap_get_flag(caps, CAP_SETGID, CAP_EFFECTIVE, &cap_value)) {
280 fprintf(stderr, "Could not get the value of CAP_SETGID: %m\n");
281 exit(1);
282 }
283
284 if (cap_free(caps)) {
285 fprintf(stderr, "Could not free capabilities: %m\n");
286 exit(1);
287 }
288
289 return cap_value == CAP_SET;
290}
291
292static void set_ugid_mapping(struct minijail *j, int set_uidmap, uid_t uid,
293 char *uidmap, int set_gidmap, gid_t gid,
294 char *gidmap)
295{
296 if (set_uidmap) {
297 minijail_namespace_user(j);
298 minijail_namespace_pids(j);
299
300 if (!uidmap) {
301 /*
302 * If no map is passed, map the current uid to the
303 * chosen uid in the target namespace (or root, if none
304 * was chosen).
305 */
306 uidmap = build_idmap(uid, getuid());
307 }
308 if (0 != minijail_uidmap(j, uidmap)) {
309 fprintf(stderr, "Could not set uid map.\n");
310 exit(1);
311 }
312 free(uidmap);
313 }
314 if (set_gidmap) {
315 minijail_namespace_user(j);
316 minijail_namespace_pids(j);
317
318 if (!gidmap) {
319 /*
320 * If no map is passed, map the current gid to the
321 * chosen gid in the target namespace.
322 */
323 gidmap = build_idmap(gid, getgid());
324 }
325 if (!has_cap_setgid()) {
326 /*
327 * This means that we are not running as root,
328 * so we also have to disable setgroups(2) to
329 * be able to set the gid map.
330 * See
331 * http://man7.org/linux/man-pages/man7/user_namespaces.7.html
332 */
333 minijail_namespace_user_disable_setgroups(j);
334 }
335 if (0 != minijail_gidmap(j, gidmap)) {
336 fprintf(stderr, "Could not set gid map.\n");
337 exit(1);
338 }
339 free(gidmap);
340 }
341}
342
343static void use_chroot(struct minijail *j, const char *path, int *chroot,
344 int pivot_root)
345{
346 if (pivot_root) {
347 fprintf(stderr, "Could not set chroot because "
348 "'-P' was specified.\n");
349 exit(1);
350 }
351 if (minijail_enter_chroot(j, path)) {
352 fprintf(stderr, "Could not set chroot.\n");
353 exit(1);
354 }
355 *chroot = 1;
356}
357
358static void use_pivot_root(struct minijail *j, const char *path,
359 int *pivot_root, int chroot)
360{
361 if (chroot) {
362 fprintf(stderr, "Could not set pivot_root because "
363 "'-C' was specified.\n");
364 exit(1);
365 }
366 if (minijail_enter_pivot_root(j, path)) {
367 fprintf(stderr, "Could not set pivot_root.\n");
368 exit(1);
369 }
370 minijail_namespace_vfs(j);
371 *pivot_root = 1;
372}
373
374static void use_profile(struct minijail *j, const char *profile,
375 int *pivot_root, int chroot, size_t *tmp_size)
376{
Mike Frysinger4d2a81e2018-01-22 16:43:33 -0500377 /* Note: New profiles should be added in minijail0_cli_unittest.cc. */
378
Mike Frysingercc5917c2020-02-03 12:34:14 -0500379 if (!strcmp(profile, "minimalistic-mountns") ||
380 !strcmp(profile, "minimalistic-mountns-nodev")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500381 minijail_namespace_vfs(j);
382 if (minijail_bind(j, "/", "/", 0)) {
Jorge Lucangeli Obes7394b902019-03-14 12:43:26 -0400383 fprintf(stderr, "minijail_bind(/) failed.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500384 exit(1);
385 }
386 if (minijail_bind(j, "/proc", "/proc", 0)) {
Jorge Lucangeli Obes7394b902019-03-14 12:43:26 -0400387 fprintf(stderr, "minijail_bind(/proc) failed.\n");
388 exit(1);
389 }
Mike Frysingercc5917c2020-02-03 12:34:14 -0500390 if (!strcmp(profile, "minimalistic-mountns")) {
391 if (minijail_bind(j, "/dev/log", "/dev/log", 0)) {
392 fprintf(stderr, "minijail_bind(/dev/log) failed.\n");
393 exit(1);
394 }
395 minijail_mount_dev(j);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500396 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500397 if (!*tmp_size) {
398 /* Avoid clobbering |tmp_size| if it was already set. */
399 *tmp_size = DEFAULT_TMP_SIZE;
400 }
401 minijail_remount_proc_readonly(j);
Allen Webbee876072019-02-21 10:56:21 -0800402 use_pivot_root(j, DEFAULT_PIVOT_ROOT, pivot_root, chroot);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500403 } else {
404 fprintf(stderr, "Unrecognized profile name '%s'\n", profile);
405 exit(1);
406 }
407}
408
Mike Frysinger785b1c32018-02-23 15:47:24 -0500409static void set_remount_mode(struct minijail *j, const char *mode)
410{
411 unsigned long msmode;
412 if (!strcmp(mode, "shared"))
413 msmode = MS_SHARED;
414 else if (!strcmp(mode, "private"))
415 msmode = MS_PRIVATE;
416 else if (!strcmp(mode, "slave"))
417 msmode = MS_SLAVE;
418 else if (!strcmp(mode, "unbindable"))
419 msmode = MS_UNBINDABLE;
420 else {
421 fprintf(stderr, "Unknown remount mode: '%s'\n", mode);
422 exit(1);
423 }
424 minijail_remount_mode(j, msmode);
425}
426
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700427static void read_seccomp_filter(const char *filter_path,
428 struct sock_fprog *filter)
429{
430 FILE *f = fopen(filter_path, "re");
431 if (!f) {
432 fprintf(stderr, "failed to open %s: %m", filter_path);
433 exit(1);
434 }
435 off_t filter_size = 0;
436 if (fseeko(f, 0, SEEK_END) == -1 || (filter_size = ftello(f)) == -1) {
437 fclose(f);
438 fprintf(stderr, "failed to get file size of %s: %m",
439 filter_path);
440 exit(1);
441 }
442 if (filter_size % sizeof(struct sock_filter) != 0) {
443 fclose(f);
444 fprintf(stderr,
445 "filter size (%" PRId64
446 ") of %s is not a multiple of %zu: %m",
447 filter_size, filter_path, sizeof(struct sock_filter));
448 exit(1);
449 }
450 rewind(f);
451
452 filter->len = filter_size / sizeof(struct sock_filter);
453 filter->filter = malloc(filter_size);
454 if (!filter->filter) {
455 fclose(f);
456 fprintf(stderr, "failed to allocate memory for filter: %m");
457 exit(1);
458 }
459 if (fread(filter->filter, sizeof(struct sock_filter), filter->len, f) !=
460 filter->len) {
461 fclose(f);
462 fprintf(stderr, "failed read %s: %m", filter_path);
463 exit(1);
464 }
465 fclose(f);
466}
467
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500468static void usage(const char *progn)
469{
470 size_t i;
471 /* clang-format off */
472 printf("Usage: %s [-dGhHiIKlLnNprRstUvyYz]\n"
473 " [-a <table>]\n"
474 " [-b <src>[,<dest>[,<writeable>]]] [-k <src>,<dest>,<type>[,<flags>[,<data>]]]\n"
475 " [-c <caps>] [-C <dir>] [-P <dir>] [-e[file]] [-f <file>] [-g <group>]\n"
476 " [-m[<uid> <loweruid> <count>]*] [-M[<gid> <lowergid> <count>]*] [--profile <name>]\n"
477 " [-R <type,cur,max>] [-S <file>] [-t[size]] [-T <type>] [-u <user>] [-V <file>]\n"
478 " <program> [args...]\n"
479 " -a <table>: Use alternate syscall table <table>.\n"
480 " -b <...>: Bind <src> to <dest> in chroot.\n"
481 " Multiple instances allowed.\n"
482 " -B <mask>: Skip setting securebits in <mask> when restricting capabilities (-c).\n"
483 " By default, SECURE_NOROOT, SECURE_NO_SETUID_FIXUP, and \n"
484 " SECURE_KEEP_CAPS (together with their respective locks) are set.\n"
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -0400485 " There are eight securebits in total.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500486 " -k <...>: Mount <src> at <dest> in chroot.\n"
487 " <flags> and <data> can be specified as in mount(2).\n"
488 " Multiple instances allowed.\n"
489 " -c <caps>: Restrict caps to <caps>.\n"
490 " -C <dir>: chroot(2) to <dir>.\n"
491 " Not compatible with -P.\n"
492 " -P <dir>: pivot_root(2) to <dir> (implies -v).\n"
493 " Not compatible with -C.\n"
494 " --mount-dev, Create a new /dev with a minimal set of device nodes (implies -v).\n"
495 " -d: See the minijail0(1) man page for the exact set.\n"
496 " -e[file]: Enter new network namespace, or existing one if |file| is provided.\n"
497 " -f <file>: Write the pid of the jailed process to <file>.\n"
498 " -g <group>: Change gid to <group>.\n"
499 " -G: Inherit supplementary groups from uid.\n"
500 " Not compatible with -y.\n"
501 " -y: Keep uid's supplementary groups.\n"
502 " Not compatible with -G.\n"
503 " -h: Help (this message).\n"
504 " -H: Seccomp filter help message.\n"
Luis Hector Chavez9dd13fd2018-04-19 20:14:47 -0700505 " -i: Exit immediately after fork(2). The jailed process will run\n"
506 " in the background.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500507 " -I: Run <program> as init (pid 1) inside a new pid namespace (implies -p).\n"
Mike Frysinger785b1c32018-02-23 15:47:24 -0500508 " -K: Do not change share mode of any existing mounts.\n"
509 " -K<mode>: Mark all existing mounts as <mode> instead of MS_PRIVATE.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500510 " -l: Enter new IPC namespace.\n"
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400511 " -L: Report blocked syscalls when using seccomp filter.\n"
512 " If the kernel does not support SECCOMP_RET_LOG,\n"
513 " forces the following syscalls to be allowed:\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500514 " ", progn);
515 /* clang-format on */
516 for (i = 0; i < log_syscalls_len; i++)
517 printf("%s ", log_syscalls[i]);
518
519 /* clang-format off */
520 printf("\n"
521 " -m[map]: Set the uid map of a user namespace (implies -pU).\n"
522 " Same arguments as newuidmap(1), multiple mappings should be separated by ',' (comma).\n"
523 " With no mapping, map the current uid to root inside the user namespace.\n"
524 " Not compatible with -b without the 'writable' option.\n"
525 " -M[map]: Set the gid map of a user namespace (implies -pU).\n"
526 " Same arguments as newgidmap(1), multiple mappings should be separated by ',' (comma).\n"
527 " With no mapping, map the current gid to root inside the user namespace.\n"
528 " Not compatible with -b without the 'writable' option.\n"
529 " -n: Set no_new_privs.\n"
530 " -N: Enter a new cgroup namespace.\n"
531 " -p: Enter new pid namespace (implies -vr).\n"
532 " -r: Remount /proc read-only (implies -v).\n"
533 " -R: Set rlimits, can be specified multiple times.\n"
534 " -s: Use seccomp mode 1 (not the same as -S).\n"
535 " -S <file>: Set seccomp filter using <file>.\n"
536 " E.g., '-S /usr/share/filters/<prog>.$(uname -m)'.\n"
537 " Requires -n when not running as root.\n"
538 " -t[size]: Mount tmpfs at /tmp (implies -v).\n"
539 " Optional argument specifies size (default \"64M\").\n"
540 " -T <type>: Assume <program> is a <type> ELF binary; <type> can be 'static' or 'dynamic'.\n"
541 " This will avoid accessing <program> binary before execve(2).\n"
542 " Type 'static' will avoid preload hooking.\n"
543 " -u <user>: Change uid to <user>.\n"
544 " -U: Enter new user namespace (implies -p).\n"
545 " -v: Enter new mount namespace.\n"
546 " -V <file>: Enter specified mount namespace.\n"
547 " -w: Create and join a new anonymous session keyring.\n"
548 " -Y: Synchronize seccomp filters across thread group.\n"
549 " -z: Don't forward signals to jailed process.\n"
550 " --ambient: Raise ambient capabilities. Requires -c.\n"
551 " --uts[=name]: Enter a new UTS namespace (and set hostname).\n"
552 " --logging=<s>:Use <s> as the logging system.\n"
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400553 " <s> must be 'auto' (default), 'syslog', or 'stderr'.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700554 " --profile <p>:Configure minijail0 to run with the <p> sandboxing profile,\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500555 " which is a convenient way to express multiple flags\n"
556 " that are typically used together.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700557 " See the minijail0(1) man page for the full list.\n"
558 " --preload-library=<f>:Overrides the path to \"" PRELOADPATH "\".\n"
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700559 " This is only really useful for local testing.\n"
560 " --seccomp-bpf-binary=<f>:Set a pre-compiled seccomp filter using <f>.\n"
561 " E.g., '-S /usr/share/filters/<prog>.$(uname -m).bpf'.\n"
562 " Requires -n when not running as root.\n"
563 " The user is responsible for ensuring that the binary\n"
564 " was compiled for the correct architecture / kernel version.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500565 /* clang-format on */
566}
567
568static void seccomp_filter_usage(const char *progn)
569{
570 const struct syscall_entry *entry = syscall_table;
571 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
572 "System call names supported:\n",
573 progn);
574 for (; entry->name && entry->nr >= 0; ++entry)
575 printf(" %s [%d]\n", entry->name, entry->nr);
576 printf("\nSee minijail0(5) for example policies.\n");
577}
578
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700579int parse_args(struct minijail *j, int argc, char *const argv[],
580 int *exit_immediately, ElfType *elftype,
581 const char **preload_path)
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500582{
583 int opt;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700584 int use_seccomp_filter = 0, use_seccomp_filter_binary = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500585 int forward = 1;
586 int binding = 0;
587 int chroot = 0, pivot_root = 0;
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400588 int mount_ns = 0, change_remount = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500589 int inherit_suppl_gids = 0, keep_suppl_gids = 0;
590 int caps = 0, ambient_caps = 0;
591 int seccomp = -1;
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800592 bool use_uid = false, use_gid = false;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500593 uid_t uid = 0;
594 gid_t gid = 0;
595 char *uidmap = NULL, *gidmap = NULL;
596 int set_uidmap = 0, set_gidmap = 0;
597 size_t tmp_size = 0;
598 const char *filter_path = NULL;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400599 int log_to_stderr = -1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500600
601 const char *optstring =
Mike Frysinger785b1c32018-02-23 15:47:24 -0500602 "+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 -0500603 /* clang-format off */
604 const struct option long_options[] = {
605 {"help", no_argument, 0, 'h'},
606 {"mount-dev", no_argument, 0, 'd'},
607 {"ambient", no_argument, 0, 128},
608 {"uts", optional_argument, 0, 129},
609 {"logging", required_argument, 0, 130},
610 {"profile", required_argument, 0, 131},
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700611 {"preload-library", required_argument, 0, 132},
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700612 {"seccomp-bpf-binary", required_argument, 0, 133},
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500613 {0, 0, 0, 0},
614 };
615 /* clang-format on */
616
617 while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) !=
618 -1) {
619 switch (opt) {
620 case 'u':
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800621 if (use_uid) {
622 fprintf(stderr,
623 "-u provided multiple times.\n");
624 exit(1);
625 }
626 use_uid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500627 set_user(j, optarg, &uid, &gid);
628 break;
629 case 'g':
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800630 if (use_gid) {
631 fprintf(stderr,
632 "-g provided multiple times.\n");
633 exit(1);
634 }
635 use_gid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500636 set_group(j, optarg, &gid);
637 break;
638 case 'n':
639 minijail_no_new_privs(j);
640 break;
641 case 's':
642 if (seccomp != -1 && seccomp != 1) {
643 fprintf(stderr,
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700644 "Do not use -s, -S, or "
645 "--seccomp-bpf-binary together.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500646 exit(1);
647 }
648 seccomp = 1;
649 minijail_use_seccomp(j);
650 break;
651 case 'S':
652 if (seccomp != -1 && seccomp != 2) {
653 fprintf(stderr,
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700654 "Do not use -s, -S, or "
655 "--seccomp-bpf-binary together.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500656 exit(1);
657 }
658 seccomp = 2;
659 minijail_use_seccomp_filter(j);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700660 filter_path = optarg;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500661 use_seccomp_filter = 1;
662 break;
663 case 'l':
664 minijail_namespace_ipc(j);
665 break;
666 case 'L':
667 minijail_log_seccomp_filter_failures(j);
668 break;
669 case 'b':
670 add_binding(j, optarg);
671 binding = 1;
672 break;
673 case 'B':
674 skip_securebits(j, optarg);
675 break;
676 case 'c':
677 caps = 1;
678 use_caps(j, optarg);
679 break;
680 case 'C':
681 use_chroot(j, optarg, &chroot, pivot_root);
682 break;
683 case 'k':
684 add_mount(j, optarg);
685 break;
686 case 'K':
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400687 if (optarg) {
Mike Frysinger785b1c32018-02-23 15:47:24 -0500688 set_remount_mode(j, optarg);
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400689 } else {
Mike Frysinger785b1c32018-02-23 15:47:24 -0500690 minijail_skip_remount_private(j);
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400691 }
692 change_remount = 1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500693 break;
694 case 'P':
695 use_pivot_root(j, optarg, &pivot_root, chroot);
696 break;
697 case 'f':
698 if (0 != minijail_write_pid_file(j, optarg)) {
699 fprintf(stderr,
700 "Could not prepare pid file path.\n");
701 exit(1);
702 }
703 break;
704 case 't':
705 minijail_namespace_vfs(j);
706 if (!tmp_size) {
707 /*
708 * Avoid clobbering |tmp_size| if it was already
709 * set.
710 */
711 tmp_size = DEFAULT_TMP_SIZE;
712 }
713 if (optarg != NULL &&
714 0 != parse_size(&tmp_size, optarg)) {
715 fprintf(stderr, "Invalid /tmp tmpfs size.\n");
716 exit(1);
717 }
718 break;
719 case 'v':
720 minijail_namespace_vfs(j);
721 mount_ns = 1;
722 break;
723 case 'V':
724 minijail_namespace_enter_vfs(j, optarg);
725 break;
726 case 'r':
727 minijail_remount_proc_readonly(j);
728 break;
729 case 'G':
730 if (keep_suppl_gids) {
731 fprintf(stderr,
732 "-y and -G are not compatible.\n");
733 exit(1);
734 }
735 minijail_inherit_usergroups(j);
736 inherit_suppl_gids = 1;
737 break;
738 case 'y':
739 if (inherit_suppl_gids) {
740 fprintf(stderr,
741 "-y and -G are not compatible.\n");
742 exit(1);
743 }
744 minijail_keep_supplementary_gids(j);
745 keep_suppl_gids = 1;
746 break;
747 case 'N':
748 minijail_namespace_cgroups(j);
749 break;
750 case 'p':
751 minijail_namespace_pids(j);
752 break;
753 case 'e':
754 if (optarg)
755 minijail_namespace_enter_net(j, optarg);
756 else
757 minijail_namespace_net(j);
758 break;
759 case 'i':
760 *exit_immediately = 1;
761 break;
762 case 'H':
763 seccomp_filter_usage(argv[0]);
764 exit(0);
765 case 'I':
766 minijail_namespace_pids(j);
767 minijail_run_as_init(j);
768 break;
769 case 'U':
770 minijail_namespace_user(j);
771 minijail_namespace_pids(j);
772 break;
773 case 'm':
774 set_uidmap = 1;
775 if (uidmap) {
776 free(uidmap);
777 uidmap = NULL;
778 }
779 if (optarg)
780 uidmap = strdup(optarg);
781 break;
782 case 'M':
783 set_gidmap = 1;
784 if (gidmap) {
785 free(gidmap);
786 gidmap = NULL;
787 }
788 if (optarg)
789 gidmap = strdup(optarg);
790 break;
791 case 'a':
792 if (0 != minijail_use_alt_syscall(j, optarg)) {
793 fprintf(stderr,
794 "Could not set alt-syscall table.\n");
795 exit(1);
796 }
797 break;
798 case 'R':
799 add_rlimit(j, optarg);
800 break;
801 case 'T':
802 if (!strcmp(optarg, "static"))
803 *elftype = ELFSTATIC;
804 else if (!strcmp(optarg, "dynamic"))
805 *elftype = ELFDYNAMIC;
806 else {
807 fprintf(stderr, "ELF type must be 'static' or "
808 "'dynamic'.\n");
809 exit(1);
810 }
811 break;
812 case 'w':
813 minijail_new_session_keyring(j);
814 break;
815 case 'Y':
816 minijail_set_seccomp_filter_tsync(j);
817 break;
818 case 'z':
819 forward = 0;
820 break;
821 case 'd':
822 minijail_namespace_vfs(j);
823 minijail_mount_dev(j);
824 break;
825 /* Long options. */
826 case 128: /* Ambient caps. */
827 ambient_caps = 1;
828 minijail_set_ambient_caps(j);
829 break;
830 case 129: /* UTS/hostname namespace. */
831 minijail_namespace_uts(j);
832 if (optarg)
833 minijail_namespace_set_hostname(j, optarg);
834 break;
835 case 130: /* Logging. */
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400836 if (!strcmp(optarg, "auto")) {
837 log_to_stderr = -1;
838 } else if (!strcmp(optarg, "syslog")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500839 log_to_stderr = 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400840 } else if (!strcmp(optarg, "stderr")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500841 log_to_stderr = 1;
842 } else {
843 fprintf(stderr, "--logger must be 'syslog' or "
844 "'stderr'.\n");
845 exit(1);
846 }
847 break;
848 case 131: /* Profile */
849 use_profile(j, optarg, &pivot_root, chroot, &tmp_size);
850 break;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700851 case 132: /* PRELOADPATH */
852 *preload_path = optarg;
853 break;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700854 case 133: /* seccomp-bpf binary. */
855 if (seccomp != -1 && seccomp != 3) {
856 fprintf(stderr,
857 "Do not use -s, -S, or "
858 "--seccomp-bpf-binary together.\n");
859 exit(1);
860 }
861 seccomp = 3;
862 minijail_use_seccomp_filter(j);
863 filter_path = optarg;
864 use_seccomp_filter_binary = 1;
865 break;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500866 default:
867 usage(argv[0]);
868 exit(opt == 'h' ? 0 : 1);
869 }
870 }
871
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400872 if (log_to_stderr == -1) {
873 /* Autodetect default logging output. */
Mike Frysinger056955c2019-09-24 16:07:05 -0400874 log_to_stderr = isatty(STDIN_FILENO) ? 1 : 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400875 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500876 if (log_to_stderr) {
877 init_logging(LOG_TO_FD, STDERR_FILENO, LOG_INFO);
878 /*
879 * When logging to stderr, ensure the FD survives the jailing.
880 */
881 if (0 !=
882 minijail_preserve_fd(j, STDERR_FILENO, STDERR_FILENO)) {
883 fprintf(stderr, "Could not preserve stderr.\n");
884 exit(1);
885 }
886 }
887
888 /* Set up uid/gid mapping. */
889 if (set_uidmap || set_gidmap) {
890 set_ugid_mapping(j, set_uidmap, uid, uidmap, set_gidmap, gid,
891 gidmap);
892 }
893
894 /* Can only set ambient caps when using regular caps. */
895 if (ambient_caps && !caps) {
896 fprintf(stderr, "Can't set ambient capabilities (--ambient) "
897 "without actually using capabilities (-c).\n");
898 exit(1);
899 }
900
901 /* Set up signal handlers in minijail unless asked not to. */
902 if (forward)
903 minijail_forward_signals(j);
904
905 /*
906 * Only allow bind mounts when entering a chroot, using pivot_root, or
907 * a new mount namespace.
908 */
909 if (binding && !(chroot || pivot_root || mount_ns)) {
910 fprintf(stderr, "Bind mounts require a chroot, pivot_root, or "
911 " new mount namespace.\n");
912 exit(1);
913 }
914
915 /*
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400916 * / is only remounted when entering a new mount namespace, so unless
917 * that's set there is no need for the -K/-K<mode> flags.
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500918 */
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400919 if (change_remount && !mount_ns) {
920 fprintf(stderr, "No need to use -K (skip remounting '/') or "
921 "-K<mode> (remount '/' as <mode>)\n"
922 "without -v (new mount namespace).\n"
923 "Do you need to add '-v' explicitly?\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500924 exit(1);
925 }
926
927 /*
928 * We parse seccomp filters here to make sure we've collected all
929 * cmdline options.
930 */
931 if (use_seccomp_filter) {
932 minijail_parse_seccomp_filters(j, filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700933 } else if (use_seccomp_filter_binary) {
934 struct sock_fprog filter;
935 read_seccomp_filter(filter_path, &filter);
936 minijail_set_seccomp_filters(j, &filter);
937 free((void *)filter.filter);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500938 }
939
940 /* Mount a tmpfs under /tmp and set its size. */
941 if (tmp_size)
942 minijail_mount_tmp_size(j, tmp_size);
943
944 /*
945 * There should be at least one additional unparsed argument: the
946 * executable name.
947 */
948 if (argc == optind) {
949 usage(argv[0]);
950 exit(1);
951 }
952
953 if (*elftype == ELFERROR) {
954 /*
955 * -T was not specified.
956 * Get the path to the program adjusted for changing root.
957 */
958 char *program_path =
959 minijail_get_original_path(j, argv[optind]);
960
961 /* Check that we can access the target program. */
962 if (access(program_path, X_OK)) {
963 fprintf(stderr,
964 "Target program '%s' is not accessible.\n",
965 argv[optind]);
966 exit(1);
967 }
968
969 /* Check if target is statically or dynamically linked. */
970 *elftype = get_elf_linkage(program_path);
971 free(program_path);
972 }
973
974 /*
975 * Setting capabilities need either a dynamically-linked binary, or the
976 * use of ambient capabilities for them to be able to survive an
977 * execve(2).
978 */
979 if (caps && *elftype == ELFSTATIC && !ambient_caps) {
980 fprintf(stderr, "Can't run statically-linked binaries with "
981 "capabilities (-c) without also setting "
982 "ambient capabilities. Try passing "
983 "--ambient.\n");
984 exit(1);
985 }
986
987 return optind;
988}