blob: 277c2224ab8efe735faf1a886a838711b9587aaf [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 Frysinger5ef22ca2018-01-20 13:42:10 -0500379 if (!strcmp(profile, "minimalistic-mountns")) {
380 minijail_namespace_vfs(j);
381 if (minijail_bind(j, "/", "/", 0)) {
Jorge Lucangeli Obes7394b902019-03-14 12:43:26 -0400382 fprintf(stderr, "minijail_bind(/) failed.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500383 exit(1);
384 }
385 if (minijail_bind(j, "/proc", "/proc", 0)) {
Jorge Lucangeli Obes7394b902019-03-14 12:43:26 -0400386 fprintf(stderr, "minijail_bind(/proc) failed.\n");
387 exit(1);
388 }
389 if (minijail_bind(j, "/dev/log", "/dev/log", 0)) {
390 fprintf(stderr, "minijail_bind(/dev/log) failed.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500391 exit(1);
392 }
393 minijail_mount_dev(j);
394 if (!*tmp_size) {
395 /* Avoid clobbering |tmp_size| if it was already set. */
396 *tmp_size = DEFAULT_TMP_SIZE;
397 }
398 minijail_remount_proc_readonly(j);
Allen Webbee876072019-02-21 10:56:21 -0800399 use_pivot_root(j, DEFAULT_PIVOT_ROOT, pivot_root, chroot);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500400 } else {
401 fprintf(stderr, "Unrecognized profile name '%s'\n", profile);
402 exit(1);
403 }
404}
405
Mike Frysinger785b1c32018-02-23 15:47:24 -0500406static void set_remount_mode(struct minijail *j, const char *mode)
407{
408 unsigned long msmode;
409 if (!strcmp(mode, "shared"))
410 msmode = MS_SHARED;
411 else if (!strcmp(mode, "private"))
412 msmode = MS_PRIVATE;
413 else if (!strcmp(mode, "slave"))
414 msmode = MS_SLAVE;
415 else if (!strcmp(mode, "unbindable"))
416 msmode = MS_UNBINDABLE;
417 else {
418 fprintf(stderr, "Unknown remount mode: '%s'\n", mode);
419 exit(1);
420 }
421 minijail_remount_mode(j, msmode);
422}
423
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700424static void read_seccomp_filter(const char *filter_path,
425 struct sock_fprog *filter)
426{
427 FILE *f = fopen(filter_path, "re");
428 if (!f) {
429 fprintf(stderr, "failed to open %s: %m", filter_path);
430 exit(1);
431 }
432 off_t filter_size = 0;
433 if (fseeko(f, 0, SEEK_END) == -1 || (filter_size = ftello(f)) == -1) {
434 fclose(f);
435 fprintf(stderr, "failed to get file size of %s: %m",
436 filter_path);
437 exit(1);
438 }
439 if (filter_size % sizeof(struct sock_filter) != 0) {
440 fclose(f);
441 fprintf(stderr,
442 "filter size (%" PRId64
443 ") of %s is not a multiple of %zu: %m",
444 filter_size, filter_path, sizeof(struct sock_filter));
445 exit(1);
446 }
447 rewind(f);
448
449 filter->len = filter_size / sizeof(struct sock_filter);
450 filter->filter = malloc(filter_size);
451 if (!filter->filter) {
452 fclose(f);
453 fprintf(stderr, "failed to allocate memory for filter: %m");
454 exit(1);
455 }
456 if (fread(filter->filter, sizeof(struct sock_filter), filter->len, f) !=
457 filter->len) {
458 fclose(f);
459 fprintf(stderr, "failed read %s: %m", filter_path);
460 exit(1);
461 }
462 fclose(f);
463}
464
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500465static void usage(const char *progn)
466{
467 size_t i;
468 /* clang-format off */
469 printf("Usage: %s [-dGhHiIKlLnNprRstUvyYz]\n"
470 " [-a <table>]\n"
471 " [-b <src>[,<dest>[,<writeable>]]] [-k <src>,<dest>,<type>[,<flags>[,<data>]]]\n"
472 " [-c <caps>] [-C <dir>] [-P <dir>] [-e[file]] [-f <file>] [-g <group>]\n"
473 " [-m[<uid> <loweruid> <count>]*] [-M[<gid> <lowergid> <count>]*] [--profile <name>]\n"
474 " [-R <type,cur,max>] [-S <file>] [-t[size]] [-T <type>] [-u <user>] [-V <file>]\n"
475 " <program> [args...]\n"
476 " -a <table>: Use alternate syscall table <table>.\n"
477 " -b <...>: Bind <src> to <dest> in chroot.\n"
478 " Multiple instances allowed.\n"
479 " -B <mask>: Skip setting securebits in <mask> when restricting capabilities (-c).\n"
480 " By default, SECURE_NOROOT, SECURE_NO_SETUID_FIXUP, and \n"
481 " SECURE_KEEP_CAPS (together with their respective locks) are set.\n"
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -0400482 " There are eight securebits in total.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500483 " -k <...>: Mount <src> at <dest> in chroot.\n"
484 " <flags> and <data> can be specified as in mount(2).\n"
485 " Multiple instances allowed.\n"
486 " -c <caps>: Restrict caps to <caps>.\n"
487 " -C <dir>: chroot(2) to <dir>.\n"
488 " Not compatible with -P.\n"
489 " -P <dir>: pivot_root(2) to <dir> (implies -v).\n"
490 " Not compatible with -C.\n"
491 " --mount-dev, Create a new /dev with a minimal set of device nodes (implies -v).\n"
492 " -d: See the minijail0(1) man page for the exact set.\n"
493 " -e[file]: Enter new network namespace, or existing one if |file| is provided.\n"
494 " -f <file>: Write the pid of the jailed process to <file>.\n"
495 " -g <group>: Change gid to <group>.\n"
496 " -G: Inherit supplementary groups from uid.\n"
497 " Not compatible with -y.\n"
498 " -y: Keep uid's supplementary groups.\n"
499 " Not compatible with -G.\n"
500 " -h: Help (this message).\n"
501 " -H: Seccomp filter help message.\n"
Luis Hector Chavez9dd13fd2018-04-19 20:14:47 -0700502 " -i: Exit immediately after fork(2). The jailed process will run\n"
503 " in the background.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500504 " -I: Run <program> as init (pid 1) inside a new pid namespace (implies -p).\n"
Mike Frysinger785b1c32018-02-23 15:47:24 -0500505 " -K: Do not change share mode of any existing mounts.\n"
506 " -K<mode>: Mark all existing mounts as <mode> instead of MS_PRIVATE.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500507 " -l: Enter new IPC namespace.\n"
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400508 " -L: Report blocked syscalls when using seccomp filter.\n"
509 " If the kernel does not support SECCOMP_RET_LOG,\n"
510 " forces the following syscalls to be allowed:\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500511 " ", progn);
512 /* clang-format on */
513 for (i = 0; i < log_syscalls_len; i++)
514 printf("%s ", log_syscalls[i]);
515
516 /* clang-format off */
517 printf("\n"
518 " -m[map]: Set the uid map of a user namespace (implies -pU).\n"
519 " Same arguments as newuidmap(1), multiple mappings should be separated by ',' (comma).\n"
520 " With no mapping, map the current uid to root inside the user namespace.\n"
521 " Not compatible with -b without the 'writable' option.\n"
522 " -M[map]: Set the gid map of a user namespace (implies -pU).\n"
523 " Same arguments as newgidmap(1), multiple mappings should be separated by ',' (comma).\n"
524 " With no mapping, map the current gid to root inside the user namespace.\n"
525 " Not compatible with -b without the 'writable' option.\n"
526 " -n: Set no_new_privs.\n"
527 " -N: Enter a new cgroup namespace.\n"
528 " -p: Enter new pid namespace (implies -vr).\n"
529 " -r: Remount /proc read-only (implies -v).\n"
530 " -R: Set rlimits, can be specified multiple times.\n"
531 " -s: Use seccomp mode 1 (not the same as -S).\n"
532 " -S <file>: Set seccomp filter using <file>.\n"
533 " E.g., '-S /usr/share/filters/<prog>.$(uname -m)'.\n"
534 " Requires -n when not running as root.\n"
535 " -t[size]: Mount tmpfs at /tmp (implies -v).\n"
536 " Optional argument specifies size (default \"64M\").\n"
537 " -T <type>: Assume <program> is a <type> ELF binary; <type> can be 'static' or 'dynamic'.\n"
538 " This will avoid accessing <program> binary before execve(2).\n"
539 " Type 'static' will avoid preload hooking.\n"
540 " -u <user>: Change uid to <user>.\n"
541 " -U: Enter new user namespace (implies -p).\n"
542 " -v: Enter new mount namespace.\n"
543 " -V <file>: Enter specified mount namespace.\n"
544 " -w: Create and join a new anonymous session keyring.\n"
545 " -Y: Synchronize seccomp filters across thread group.\n"
546 " -z: Don't forward signals to jailed process.\n"
547 " --ambient: Raise ambient capabilities. Requires -c.\n"
548 " --uts[=name]: Enter a new UTS namespace (and set hostname).\n"
549 " --logging=<s>:Use <s> as the logging system.\n"
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400550 " <s> must be 'auto' (default), 'syslog', or 'stderr'.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700551 " --profile <p>:Configure minijail0 to run with the <p> sandboxing profile,\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500552 " which is a convenient way to express multiple flags\n"
553 " that are typically used together.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700554 " See the minijail0(1) man page for the full list.\n"
555 " --preload-library=<f>:Overrides the path to \"" PRELOADPATH "\".\n"
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700556 " This is only really useful for local testing.\n"
557 " --seccomp-bpf-binary=<f>:Set a pre-compiled seccomp filter using <f>.\n"
558 " E.g., '-S /usr/share/filters/<prog>.$(uname -m).bpf'.\n"
559 " Requires -n when not running as root.\n"
560 " The user is responsible for ensuring that the binary\n"
561 " was compiled for the correct architecture / kernel version.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500562 /* clang-format on */
563}
564
565static void seccomp_filter_usage(const char *progn)
566{
567 const struct syscall_entry *entry = syscall_table;
568 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
569 "System call names supported:\n",
570 progn);
571 for (; entry->name && entry->nr >= 0; ++entry)
572 printf(" %s [%d]\n", entry->name, entry->nr);
573 printf("\nSee minijail0(5) for example policies.\n");
574}
575
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700576int parse_args(struct minijail *j, int argc, char *const argv[],
577 int *exit_immediately, ElfType *elftype,
578 const char **preload_path)
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500579{
580 int opt;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700581 int use_seccomp_filter = 0, use_seccomp_filter_binary = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500582 int forward = 1;
583 int binding = 0;
584 int chroot = 0, pivot_root = 0;
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400585 int mount_ns = 0, change_remount = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500586 int inherit_suppl_gids = 0, keep_suppl_gids = 0;
587 int caps = 0, ambient_caps = 0;
588 int seccomp = -1;
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800589 bool use_uid = false, use_gid = false;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500590 uid_t uid = 0;
591 gid_t gid = 0;
592 char *uidmap = NULL, *gidmap = NULL;
593 int set_uidmap = 0, set_gidmap = 0;
594 size_t tmp_size = 0;
595 const char *filter_path = NULL;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400596 int log_to_stderr = -1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500597
598 const char *optstring =
Mike Frysinger785b1c32018-02-23 15:47:24 -0500599 "+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 -0500600 /* clang-format off */
601 const struct option long_options[] = {
602 {"help", no_argument, 0, 'h'},
603 {"mount-dev", no_argument, 0, 'd'},
604 {"ambient", no_argument, 0, 128},
605 {"uts", optional_argument, 0, 129},
606 {"logging", required_argument, 0, 130},
607 {"profile", required_argument, 0, 131},
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700608 {"preload-library", required_argument, 0, 132},
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700609 {"seccomp-bpf-binary", required_argument, 0, 133},
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500610 {0, 0, 0, 0},
611 };
612 /* clang-format on */
613
614 while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) !=
615 -1) {
616 switch (opt) {
617 case 'u':
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800618 if (use_uid) {
619 fprintf(stderr,
620 "-u provided multiple times.\n");
621 exit(1);
622 }
623 use_uid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500624 set_user(j, optarg, &uid, &gid);
625 break;
626 case 'g':
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800627 if (use_gid) {
628 fprintf(stderr,
629 "-g provided multiple times.\n");
630 exit(1);
631 }
632 use_gid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500633 set_group(j, optarg, &gid);
634 break;
635 case 'n':
636 minijail_no_new_privs(j);
637 break;
638 case 's':
639 if (seccomp != -1 && seccomp != 1) {
640 fprintf(stderr,
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700641 "Do not use -s, -S, or "
642 "--seccomp-bpf-binary together.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500643 exit(1);
644 }
645 seccomp = 1;
646 minijail_use_seccomp(j);
647 break;
648 case 'S':
649 if (seccomp != -1 && seccomp != 2) {
650 fprintf(stderr,
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700651 "Do not use -s, -S, or "
652 "--seccomp-bpf-binary together.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500653 exit(1);
654 }
655 seccomp = 2;
656 minijail_use_seccomp_filter(j);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700657 filter_path = optarg;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500658 use_seccomp_filter = 1;
659 break;
660 case 'l':
661 minijail_namespace_ipc(j);
662 break;
663 case 'L':
664 minijail_log_seccomp_filter_failures(j);
665 break;
666 case 'b':
667 add_binding(j, optarg);
668 binding = 1;
669 break;
670 case 'B':
671 skip_securebits(j, optarg);
672 break;
673 case 'c':
674 caps = 1;
675 use_caps(j, optarg);
676 break;
677 case 'C':
678 use_chroot(j, optarg, &chroot, pivot_root);
679 break;
680 case 'k':
681 add_mount(j, optarg);
682 break;
683 case 'K':
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400684 if (optarg) {
Mike Frysinger785b1c32018-02-23 15:47:24 -0500685 set_remount_mode(j, optarg);
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400686 } else {
Mike Frysinger785b1c32018-02-23 15:47:24 -0500687 minijail_skip_remount_private(j);
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400688 }
689 change_remount = 1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500690 break;
691 case 'P':
692 use_pivot_root(j, optarg, &pivot_root, chroot);
693 break;
694 case 'f':
695 if (0 != minijail_write_pid_file(j, optarg)) {
696 fprintf(stderr,
697 "Could not prepare pid file path.\n");
698 exit(1);
699 }
700 break;
701 case 't':
702 minijail_namespace_vfs(j);
703 if (!tmp_size) {
704 /*
705 * Avoid clobbering |tmp_size| if it was already
706 * set.
707 */
708 tmp_size = DEFAULT_TMP_SIZE;
709 }
710 if (optarg != NULL &&
711 0 != parse_size(&tmp_size, optarg)) {
712 fprintf(stderr, "Invalid /tmp tmpfs size.\n");
713 exit(1);
714 }
715 break;
716 case 'v':
717 minijail_namespace_vfs(j);
718 mount_ns = 1;
719 break;
720 case 'V':
721 minijail_namespace_enter_vfs(j, optarg);
722 break;
723 case 'r':
724 minijail_remount_proc_readonly(j);
725 break;
726 case 'G':
727 if (keep_suppl_gids) {
728 fprintf(stderr,
729 "-y and -G are not compatible.\n");
730 exit(1);
731 }
732 minijail_inherit_usergroups(j);
733 inherit_suppl_gids = 1;
734 break;
735 case 'y':
736 if (inherit_suppl_gids) {
737 fprintf(stderr,
738 "-y and -G are not compatible.\n");
739 exit(1);
740 }
741 minijail_keep_supplementary_gids(j);
742 keep_suppl_gids = 1;
743 break;
744 case 'N':
745 minijail_namespace_cgroups(j);
746 break;
747 case 'p':
748 minijail_namespace_pids(j);
749 break;
750 case 'e':
751 if (optarg)
752 minijail_namespace_enter_net(j, optarg);
753 else
754 minijail_namespace_net(j);
755 break;
756 case 'i':
757 *exit_immediately = 1;
758 break;
759 case 'H':
760 seccomp_filter_usage(argv[0]);
761 exit(0);
762 case 'I':
763 minijail_namespace_pids(j);
764 minijail_run_as_init(j);
765 break;
766 case 'U':
767 minijail_namespace_user(j);
768 minijail_namespace_pids(j);
769 break;
770 case 'm':
771 set_uidmap = 1;
772 if (uidmap) {
773 free(uidmap);
774 uidmap = NULL;
775 }
776 if (optarg)
777 uidmap = strdup(optarg);
778 break;
779 case 'M':
780 set_gidmap = 1;
781 if (gidmap) {
782 free(gidmap);
783 gidmap = NULL;
784 }
785 if (optarg)
786 gidmap = strdup(optarg);
787 break;
788 case 'a':
789 if (0 != minijail_use_alt_syscall(j, optarg)) {
790 fprintf(stderr,
791 "Could not set alt-syscall table.\n");
792 exit(1);
793 }
794 break;
795 case 'R':
796 add_rlimit(j, optarg);
797 break;
798 case 'T':
799 if (!strcmp(optarg, "static"))
800 *elftype = ELFSTATIC;
801 else if (!strcmp(optarg, "dynamic"))
802 *elftype = ELFDYNAMIC;
803 else {
804 fprintf(stderr, "ELF type must be 'static' or "
805 "'dynamic'.\n");
806 exit(1);
807 }
808 break;
809 case 'w':
810 minijail_new_session_keyring(j);
811 break;
812 case 'Y':
813 minijail_set_seccomp_filter_tsync(j);
814 break;
815 case 'z':
816 forward = 0;
817 break;
818 case 'd':
819 minijail_namespace_vfs(j);
820 minijail_mount_dev(j);
821 break;
822 /* Long options. */
823 case 128: /* Ambient caps. */
824 ambient_caps = 1;
825 minijail_set_ambient_caps(j);
826 break;
827 case 129: /* UTS/hostname namespace. */
828 minijail_namespace_uts(j);
829 if (optarg)
830 minijail_namespace_set_hostname(j, optarg);
831 break;
832 case 130: /* Logging. */
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400833 if (!strcmp(optarg, "auto")) {
834 log_to_stderr = -1;
835 } else if (!strcmp(optarg, "syslog")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500836 log_to_stderr = 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400837 } else if (!strcmp(optarg, "stderr")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500838 log_to_stderr = 1;
839 } else {
840 fprintf(stderr, "--logger must be 'syslog' or "
841 "'stderr'.\n");
842 exit(1);
843 }
844 break;
845 case 131: /* Profile */
846 use_profile(j, optarg, &pivot_root, chroot, &tmp_size);
847 break;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700848 case 132: /* PRELOADPATH */
849 *preload_path = optarg;
850 break;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700851 case 133: /* seccomp-bpf binary. */
852 if (seccomp != -1 && seccomp != 3) {
853 fprintf(stderr,
854 "Do not use -s, -S, or "
855 "--seccomp-bpf-binary together.\n");
856 exit(1);
857 }
858 seccomp = 3;
859 minijail_use_seccomp_filter(j);
860 filter_path = optarg;
861 use_seccomp_filter_binary = 1;
862 break;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500863 default:
864 usage(argv[0]);
865 exit(opt == 'h' ? 0 : 1);
866 }
867 }
868
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400869 if (log_to_stderr == -1) {
870 /* Autodetect default logging output. */
Mike Frysinger056955c2019-09-24 16:07:05 -0400871 log_to_stderr = isatty(STDIN_FILENO) ? 1 : 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400872 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500873 if (log_to_stderr) {
874 init_logging(LOG_TO_FD, STDERR_FILENO, LOG_INFO);
875 /*
876 * When logging to stderr, ensure the FD survives the jailing.
877 */
878 if (0 !=
879 minijail_preserve_fd(j, STDERR_FILENO, STDERR_FILENO)) {
880 fprintf(stderr, "Could not preserve stderr.\n");
881 exit(1);
882 }
883 }
884
885 /* Set up uid/gid mapping. */
886 if (set_uidmap || set_gidmap) {
887 set_ugid_mapping(j, set_uidmap, uid, uidmap, set_gidmap, gid,
888 gidmap);
889 }
890
891 /* Can only set ambient caps when using regular caps. */
892 if (ambient_caps && !caps) {
893 fprintf(stderr, "Can't set ambient capabilities (--ambient) "
894 "without actually using capabilities (-c).\n");
895 exit(1);
896 }
897
898 /* Set up signal handlers in minijail unless asked not to. */
899 if (forward)
900 minijail_forward_signals(j);
901
902 /*
903 * Only allow bind mounts when entering a chroot, using pivot_root, or
904 * a new mount namespace.
905 */
906 if (binding && !(chroot || pivot_root || mount_ns)) {
907 fprintf(stderr, "Bind mounts require a chroot, pivot_root, or "
908 " new mount namespace.\n");
909 exit(1);
910 }
911
912 /*
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400913 * / is only remounted when entering a new mount namespace, so unless
914 * that's set there is no need for the -K/-K<mode> flags.
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500915 */
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400916 if (change_remount && !mount_ns) {
917 fprintf(stderr, "No need to use -K (skip remounting '/') or "
918 "-K<mode> (remount '/' as <mode>)\n"
919 "without -v (new mount namespace).\n"
920 "Do you need to add '-v' explicitly?\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500921 exit(1);
922 }
923
924 /*
925 * We parse seccomp filters here to make sure we've collected all
926 * cmdline options.
927 */
928 if (use_seccomp_filter) {
929 minijail_parse_seccomp_filters(j, filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700930 } else if (use_seccomp_filter_binary) {
931 struct sock_fprog filter;
932 read_seccomp_filter(filter_path, &filter);
933 minijail_set_seccomp_filters(j, &filter);
934 free((void *)filter.filter);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500935 }
936
937 /* Mount a tmpfs under /tmp and set its size. */
938 if (tmp_size)
939 minijail_mount_tmp_size(j, tmp_size);
940
941 /*
942 * There should be at least one additional unparsed argument: the
943 * executable name.
944 */
945 if (argc == optind) {
946 usage(argv[0]);
947 exit(1);
948 }
949
950 if (*elftype == ELFERROR) {
951 /*
952 * -T was not specified.
953 * Get the path to the program adjusted for changing root.
954 */
955 char *program_path =
956 minijail_get_original_path(j, argv[optind]);
957
958 /* Check that we can access the target program. */
959 if (access(program_path, X_OK)) {
960 fprintf(stderr,
961 "Target program '%s' is not accessible.\n",
962 argv[optind]);
963 exit(1);
964 }
965
966 /* Check if target is statically or dynamically linked. */
967 *elftype = get_elf_linkage(program_path);
968 free(program_path);
969 }
970
971 /*
972 * Setting capabilities need either a dynamically-linked binary, or the
973 * use of ambient capabilities for them to be able to survive an
974 * execve(2).
975 */
976 if (caps && *elftype == ELFSTATIC && !ambient_caps) {
977 fprintf(stderr, "Can't run statically-linked binaries with "
978 "capabilities (-c) without also setting "
979 "ambient capabilities. Try passing "
980 "--ambient.\n");
981 exit(1);
982 }
983
984 return optind;
985}