blob: c3da5de9cc8a92929222149fa0e55ff722216a3e [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
Mattias Nissler160d58f2020-02-25 11:01:30 +010043 int ret = lookup_user(arg, out_uid, out_gid);
44 if (ret) {
45 fprintf(stderr, "Bad user '%s': %s\n", arg, strerror(-ret));
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050046 exit(1);
47 }
48
Mattias Nissler160d58f2020-02-25 11:01:30 +010049 ret = minijail_change_user(j, arg);
50 if (ret) {
51 fprintf(stderr, "minijail_change_user('%s') failed: %s\n", arg,
52 strerror(-ret));
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050053 exit(1);
54 }
55}
56
57static void set_group(struct minijail *j, const char *arg, gid_t *out_gid)
58{
59 char *end = NULL;
60 int gid = strtod(arg, &end);
61 if (!*end && *arg) {
62 *out_gid = gid;
63 minijail_change_gid(j, gid);
64 return;
65 }
66
Mattias Nissler160d58f2020-02-25 11:01:30 +010067 int ret = lookup_group(arg, out_gid);
68 if (ret) {
69 fprintf(stderr, "Bad group '%s': %s\n", arg, strerror(-ret));
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050070 exit(1);
71 }
72
Mattias Nissler160d58f2020-02-25 11:01:30 +010073 minijail_change_gid(j, *out_gid);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050074}
75
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +010076/*
77 * Helper function used by --add-suppl-group (possibly more than once),
78 * to build the supplementary gids array.
79 */
80static void suppl_group_add(size_t *suppl_gids_count, gid_t **suppl_gids,
81 char *arg) {
82 char *end = NULL;
83 int groupid = strtod(arg, &end);
84 gid_t gid;
Mattias Nissler160d58f2020-02-25 11:01:30 +010085 int ret;
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +010086 if (!*end && *arg) {
87 /* A gid number has been specified, proceed. */
88 gid = groupid;
Mattias Nissler160d58f2020-02-25 11:01:30 +010089 } else if ((ret = lookup_group(arg, &gid))) {
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +010090 /*
91 * A group name has been specified,
92 * but doesn't exist: we bail out.
93 */
Mattias Nissler160d58f2020-02-25 11:01:30 +010094 fprintf(stderr, "Bad group '%s': %s\n", arg, strerror(-ret));
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +010095 exit(1);
96 }
97
98 /*
99 * From here, gid is guaranteed to be set and valid,
100 * we add it to our supplementary gids array.
101 */
102 *suppl_gids = realloc(*suppl_gids,
103 sizeof(gid_t) * ++(*suppl_gids_count));
104 if (!suppl_gids) {
105 fprintf(stderr, "failed to allocate memory.\n");
106 exit(1);
107 }
108
109 (*suppl_gids)[*suppl_gids_count - 1] = gid;
110}
111
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500112static void skip_securebits(struct minijail *j, const char *arg)
113{
114 uint64_t securebits_skip_mask;
115 char *end = NULL;
116 securebits_skip_mask = strtoull(arg, &end, 16);
117 if (*end) {
118 fprintf(stderr, "Invalid securebit mask: '%s'\n", arg);
119 exit(1);
120 }
121 minijail_skip_setting_securebits(j, securebits_skip_mask);
122}
123
124static void use_caps(struct minijail *j, const char *arg)
125{
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700126 uint64_t caps = 0;
127 cap_t parsed_caps = cap_from_text(arg);
128
129 if (parsed_caps != NULL) {
130 unsigned int i;
131 const uint64_t one = 1;
132 cap_flag_value_t cap_value;
133 unsigned int last_valid_cap = get_last_valid_cap();
134
135 for (i = 0; i <= last_valid_cap; ++i) {
136 if (cap_get_flag(parsed_caps, i, CAP_EFFECTIVE,
137 &cap_value)) {
Luis Hector Chavez677900f2018-09-24 09:13:26 -0700138 if (errno == EINVAL) {
139 /*
140 * Some versions of libcap reject any
141 * capabilities they were not compiled
142 * with by returning EINVAL.
143 */
144 continue;
145 }
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700146 fprintf(stderr,
147 "Could not get the value of "
148 "the %d-th capability: %m\n",
149 i);
150 exit(1);
151 }
152 if (cap_value == CAP_SET)
153 caps |= (one << i);
154 }
155 cap_free(parsed_caps);
156 } else {
157 char *end = NULL;
158 caps = strtoull(arg, &end, 16);
159 if (*end) {
160 fprintf(stderr, "Invalid cap set: '%s'\n", arg);
161 exit(1);
162 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500163 }
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700164
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500165 minijail_use_caps(j, caps);
166}
167
168static void add_binding(struct minijail *j, char *arg)
169{
170 char *src = tokenize(&arg, ",");
171 char *dest = tokenize(&arg, ",");
172 char *flags = tokenize(&arg, ",");
173 if (!src || src[0] == '\0' || arg != NULL) {
174 fprintf(stderr, "Bad binding: %s %s\n", src, dest);
175 exit(1);
176 }
177 if (dest == NULL || dest[0] == '\0')
178 dest = src;
David Coles87ec5cd2019-06-13 17:20:10 -0700179 int writable;
180 if (flags == NULL || flags[0] == '\0' || !strcmp(flags, "0"))
181 writable = 0;
182 else if (!strcmp(flags, "1"))
183 writable = 1;
184 else {
185 fprintf(stderr, "Bad value for <writable>: %s\n", flags);
186 exit(1);
187 }
188 if (minijail_bind(j, src, dest, writable)) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500189 fprintf(stderr, "minijail_bind failed.\n");
190 exit(1);
191 }
192}
193
194static void add_rlimit(struct minijail *j, char *arg)
195{
196 char *type = tokenize(&arg, ",");
197 char *cur = tokenize(&arg, ",");
198 char *max = tokenize(&arg, ",");
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800199 char *end;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500200 if (!type || type[0] == '\0' || !cur || cur[0] == '\0' ||
201 !max || max[0] == '\0' || arg != NULL) {
202 fprintf(stderr, "Bad rlimit '%s'.\n", arg);
203 exit(1);
204 }
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800205 rlim_t cur_rlim;
206 rlim_t max_rlim;
207 if (!strcmp(cur, "unlimited")) {
208 cur_rlim = RLIM_INFINITY;
209 } else {
210 end = NULL;
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400211 cur_rlim = strtoul(cur, &end, 0);
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800212 if (*end) {
213 fprintf(stderr, "Bad soft limit: '%s'.\n", cur);
214 exit(1);
215 }
216 }
217 if (!strcmp(max, "unlimited")) {
218 max_rlim = RLIM_INFINITY;
219 } else {
220 end = NULL;
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400221 max_rlim = strtoul(max, &end, 0);
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800222 if (*end) {
223 fprintf(stderr, "Bad hard limit: '%s'.\n", max);
224 exit(1);
225 }
226 }
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400227
228 end = NULL;
229 int resource = parse_single_constant(type, &end);
230 if (type == end) {
231 fprintf(stderr, "Bad rlimit: '%s'.\n", type);
232 exit(1);
233 }
234
235 if (minijail_rlimit(j, resource, cur_rlim, max_rlim)) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500236 fprintf(stderr, "minijail_rlimit '%s,%s,%s' failed.\n", type,
237 cur, max);
238 exit(1);
239 }
240}
241
242static void add_mount(struct minijail *j, char *arg)
243{
244 char *src = tokenize(&arg, ",");
245 char *dest = tokenize(&arg, ",");
246 char *type = tokenize(&arg, ",");
247 char *flags = tokenize(&arg, ",");
248 char *data = tokenize(&arg, ",");
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400249 char *end;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500250 if (!src || src[0] == '\0' || !dest || dest[0] == '\0' ||
Mike Frysinger4f3e09f2018-01-24 18:01:16 -0500251 !type || type[0] == '\0') {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500252 fprintf(stderr, "Bad mount: %s %s %s\n", src, dest, type);
253 exit(1);
254 }
Mike Frysinger4f3e09f2018-01-24 18:01:16 -0500255
256 /*
257 * Fun edge case: the data option itself is comma delimited. If there
258 * were no more options, then arg would be set to NULL. But if we had
259 * more pending, it'll be pointing to the next token. Back up and undo
260 * the null byte so it'll be merged back.
261 * An example:
262 * none,/tmp,tmpfs,0xe,mode=0755,uid=10,gid=10
263 * The tokenize calls above will turn this memory into:
264 * none\0/tmp\0tmpfs\00xe\0mode=0755\0uid=10,gid=10
265 * With data pointing at mode=0755 and arg pointing at uid=10,gid=10.
266 */
267 if (arg != NULL)
268 arg[-1] = ',';
269
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400270 unsigned long mountflags;
271 if (flags == NULL || flags[0] == '\0') {
272 mountflags = 0;
273 } else {
274 end = NULL;
275 mountflags = parse_constant(flags, &end);
276 if (flags == end) {
277 fprintf(stderr, "Bad mount flags: %s\n", flags);
278 exit(1);
279 }
280 }
281
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500282 if (minijail_mount_with_data(j, src, dest, type,
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400283 mountflags, data)) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500284 fprintf(stderr, "minijail_mount failed.\n");
285 exit(1);
286 }
287}
288
289static char *build_idmap(id_t id, id_t lowerid)
290{
291 int ret;
292 char *idmap = malloc(IDMAP_LEN);
293 ret = snprintf(idmap, IDMAP_LEN, "%d %d 1", id, lowerid);
294 if (ret < 0 || (size_t)ret >= IDMAP_LEN) {
295 free(idmap);
296 fprintf(stderr, "Could not build id map.\n");
297 exit(1);
298 }
299 return idmap;
300}
301
302static int has_cap_setgid(void)
303{
304 cap_t caps;
305 cap_flag_value_t cap_value;
306
307 if (!CAP_IS_SUPPORTED(CAP_SETGID))
308 return 0;
309
310 caps = cap_get_proc();
311 if (!caps) {
312 fprintf(stderr, "Could not get process' capabilities: %m\n");
313 exit(1);
314 }
315
316 if (cap_get_flag(caps, CAP_SETGID, CAP_EFFECTIVE, &cap_value)) {
317 fprintf(stderr, "Could not get the value of CAP_SETGID: %m\n");
318 exit(1);
319 }
320
321 if (cap_free(caps)) {
322 fprintf(stderr, "Could not free capabilities: %m\n");
323 exit(1);
324 }
325
326 return cap_value == CAP_SET;
327}
328
329static void set_ugid_mapping(struct minijail *j, int set_uidmap, uid_t uid,
330 char *uidmap, int set_gidmap, gid_t gid,
331 char *gidmap)
332{
333 if (set_uidmap) {
334 minijail_namespace_user(j);
335 minijail_namespace_pids(j);
336
337 if (!uidmap) {
338 /*
339 * If no map is passed, map the current uid to the
340 * chosen uid in the target namespace (or root, if none
341 * was chosen).
342 */
343 uidmap = build_idmap(uid, getuid());
344 }
345 if (0 != minijail_uidmap(j, uidmap)) {
346 fprintf(stderr, "Could not set uid map.\n");
347 exit(1);
348 }
349 free(uidmap);
350 }
351 if (set_gidmap) {
352 minijail_namespace_user(j);
353 minijail_namespace_pids(j);
354
355 if (!gidmap) {
356 /*
357 * If no map is passed, map the current gid to the
358 * chosen gid in the target namespace.
359 */
360 gidmap = build_idmap(gid, getgid());
361 }
362 if (!has_cap_setgid()) {
363 /*
364 * This means that we are not running as root,
365 * so we also have to disable setgroups(2) to
366 * be able to set the gid map.
367 * See
368 * http://man7.org/linux/man-pages/man7/user_namespaces.7.html
369 */
370 minijail_namespace_user_disable_setgroups(j);
371 }
372 if (0 != minijail_gidmap(j, gidmap)) {
373 fprintf(stderr, "Could not set gid map.\n");
374 exit(1);
375 }
376 free(gidmap);
377 }
378}
379
380static void use_chroot(struct minijail *j, const char *path, int *chroot,
381 int pivot_root)
382{
383 if (pivot_root) {
384 fprintf(stderr, "Could not set chroot because "
385 "'-P' was specified.\n");
386 exit(1);
387 }
388 if (minijail_enter_chroot(j, path)) {
389 fprintf(stderr, "Could not set chroot.\n");
390 exit(1);
391 }
392 *chroot = 1;
393}
394
395static void use_pivot_root(struct minijail *j, const char *path,
396 int *pivot_root, int chroot)
397{
398 if (chroot) {
399 fprintf(stderr, "Could not set pivot_root because "
400 "'-C' was specified.\n");
401 exit(1);
402 }
403 if (minijail_enter_pivot_root(j, path)) {
404 fprintf(stderr, "Could not set pivot_root.\n");
405 exit(1);
406 }
407 minijail_namespace_vfs(j);
408 *pivot_root = 1;
409}
410
411static void use_profile(struct minijail *j, const char *profile,
412 int *pivot_root, int chroot, size_t *tmp_size)
413{
Mike Frysinger4d2a81e2018-01-22 16:43:33 -0500414 /* Note: New profiles should be added in minijail0_cli_unittest.cc. */
415
Mike Frysingercc5917c2020-02-03 12:34:14 -0500416 if (!strcmp(profile, "minimalistic-mountns") ||
417 !strcmp(profile, "minimalistic-mountns-nodev")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500418 minijail_namespace_vfs(j);
419 if (minijail_bind(j, "/", "/", 0)) {
Jorge Lucangeli Obes7394b902019-03-14 12:43:26 -0400420 fprintf(stderr, "minijail_bind(/) failed.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500421 exit(1);
422 }
423 if (minijail_bind(j, "/proc", "/proc", 0)) {
Jorge Lucangeli Obes7394b902019-03-14 12:43:26 -0400424 fprintf(stderr, "minijail_bind(/proc) failed.\n");
425 exit(1);
426 }
Mike Frysingercc5917c2020-02-03 12:34:14 -0500427 if (!strcmp(profile, "minimalistic-mountns")) {
428 if (minijail_bind(j, "/dev/log", "/dev/log", 0)) {
429 fprintf(stderr, "minijail_bind(/dev/log) failed.\n");
430 exit(1);
431 }
432 minijail_mount_dev(j);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500433 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500434 if (!*tmp_size) {
435 /* Avoid clobbering |tmp_size| if it was already set. */
436 *tmp_size = DEFAULT_TMP_SIZE;
437 }
438 minijail_remount_proc_readonly(j);
Allen Webbee876072019-02-21 10:56:21 -0800439 use_pivot_root(j, DEFAULT_PIVOT_ROOT, pivot_root, chroot);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500440 } else {
441 fprintf(stderr, "Unrecognized profile name '%s'\n", profile);
442 exit(1);
443 }
444}
445
Mike Frysinger785b1c32018-02-23 15:47:24 -0500446static void set_remount_mode(struct minijail *j, const char *mode)
447{
448 unsigned long msmode;
449 if (!strcmp(mode, "shared"))
450 msmode = MS_SHARED;
451 else if (!strcmp(mode, "private"))
452 msmode = MS_PRIVATE;
453 else if (!strcmp(mode, "slave"))
454 msmode = MS_SLAVE;
455 else if (!strcmp(mode, "unbindable"))
456 msmode = MS_UNBINDABLE;
457 else {
458 fprintf(stderr, "Unknown remount mode: '%s'\n", mode);
459 exit(1);
460 }
461 minijail_remount_mode(j, msmode);
462}
463
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700464static void read_seccomp_filter(const char *filter_path,
465 struct sock_fprog *filter)
466{
467 FILE *f = fopen(filter_path, "re");
468 if (!f) {
469 fprintf(stderr, "failed to open %s: %m", filter_path);
470 exit(1);
471 }
472 off_t filter_size = 0;
473 if (fseeko(f, 0, SEEK_END) == -1 || (filter_size = ftello(f)) == -1) {
474 fclose(f);
475 fprintf(stderr, "failed to get file size of %s: %m",
476 filter_path);
477 exit(1);
478 }
479 if (filter_size % sizeof(struct sock_filter) != 0) {
480 fclose(f);
481 fprintf(stderr,
482 "filter size (%" PRId64
483 ") of %s is not a multiple of %zu: %m",
484 filter_size, filter_path, sizeof(struct sock_filter));
485 exit(1);
486 }
487 rewind(f);
488
489 filter->len = filter_size / sizeof(struct sock_filter);
490 filter->filter = malloc(filter_size);
491 if (!filter->filter) {
492 fclose(f);
493 fprintf(stderr, "failed to allocate memory for filter: %m");
494 exit(1);
495 }
496 if (fread(filter->filter, sizeof(struct sock_filter), filter->len, f) !=
497 filter->len) {
498 fclose(f);
499 fprintf(stderr, "failed read %s: %m", filter_path);
500 exit(1);
501 }
502 fclose(f);
503}
504
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500505static void usage(const char *progn)
506{
507 size_t i;
508 /* clang-format off */
509 printf("Usage: %s [-dGhHiIKlLnNprRstUvyYz]\n"
510 " [-a <table>]\n"
511 " [-b <src>[,<dest>[,<writeable>]]] [-k <src>,<dest>,<type>[,<flags>[,<data>]]]\n"
512 " [-c <caps>] [-C <dir>] [-P <dir>] [-e[file]] [-f <file>] [-g <group>]\n"
513 " [-m[<uid> <loweruid> <count>]*] [-M[<gid> <lowergid> <count>]*] [--profile <name>]\n"
514 " [-R <type,cur,max>] [-S <file>] [-t[size]] [-T <type>] [-u <user>] [-V <file>]\n"
515 " <program> [args...]\n"
516 " -a <table>: Use alternate syscall table <table>.\n"
517 " -b <...>: Bind <src> to <dest> in chroot.\n"
518 " Multiple instances allowed.\n"
519 " -B <mask>: Skip setting securebits in <mask> when restricting capabilities (-c).\n"
520 " By default, SECURE_NOROOT, SECURE_NO_SETUID_FIXUP, and \n"
521 " SECURE_KEEP_CAPS (together with their respective locks) are set.\n"
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -0400522 " There are eight securebits in total.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500523 " -k <...>: Mount <src> at <dest> in chroot.\n"
524 " <flags> and <data> can be specified as in mount(2).\n"
525 " Multiple instances allowed.\n"
526 " -c <caps>: Restrict caps to <caps>.\n"
527 " -C <dir>: chroot(2) to <dir>.\n"
528 " Not compatible with -P.\n"
529 " -P <dir>: pivot_root(2) to <dir> (implies -v).\n"
530 " Not compatible with -C.\n"
531 " --mount-dev, Create a new /dev with a minimal set of device nodes (implies -v).\n"
532 " -d: See the minijail0(1) man page for the exact set.\n"
533 " -e[file]: Enter new network namespace, or existing one if |file| is provided.\n"
534 " -f <file>: Write the pid of the jailed process to <file>.\n"
535 " -g <group>: Change gid to <group>.\n"
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100536 " -G: Inherit supplementary groups from new uid.\n"
537 " Not compatible with -y or --add-suppl-group.\n"
538 " -y: Keep original uid's supplementary groups.\n"
539 " Not compatible with -G or --add-suppl-group.\n"
540 " --add-suppl-group <g>:Add <g> to the proccess' supplementary groups,\n"
541 " can be specified multiple times to add several groups.\n"
542 " Not compatible with -y or -G.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500543 " -h: Help (this message).\n"
544 " -H: Seccomp filter help message.\n"
Luis Hector Chavez9dd13fd2018-04-19 20:14:47 -0700545 " -i: Exit immediately after fork(2). The jailed process will run\n"
546 " in the background.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500547 " -I: Run <program> as init (pid 1) inside a new pid namespace (implies -p).\n"
Mike Frysinger785b1c32018-02-23 15:47:24 -0500548 " -K: Do not change share mode of any existing mounts.\n"
549 " -K<mode>: Mark all existing mounts as <mode> instead of MS_PRIVATE.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500550 " -l: Enter new IPC namespace.\n"
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400551 " -L: Report blocked syscalls when using seccomp filter.\n"
552 " If the kernel does not support SECCOMP_RET_LOG,\n"
553 " forces the following syscalls to be allowed:\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500554 " ", progn);
555 /* clang-format on */
556 for (i = 0; i < log_syscalls_len; i++)
557 printf("%s ", log_syscalls[i]);
558
559 /* clang-format off */
560 printf("\n"
561 " -m[map]: Set the uid map of a user namespace (implies -pU).\n"
562 " Same arguments as newuidmap(1), multiple mappings should be separated by ',' (comma).\n"
563 " With no mapping, map the current uid to root inside the user namespace.\n"
564 " Not compatible with -b without the 'writable' option.\n"
565 " -M[map]: Set the gid map of a user namespace (implies -pU).\n"
566 " Same arguments as newgidmap(1), multiple mappings should be separated by ',' (comma).\n"
567 " With no mapping, map the current gid to root inside the user namespace.\n"
568 " Not compatible with -b without the 'writable' option.\n"
569 " -n: Set no_new_privs.\n"
570 " -N: Enter a new cgroup namespace.\n"
571 " -p: Enter new pid namespace (implies -vr).\n"
572 " -r: Remount /proc read-only (implies -v).\n"
573 " -R: Set rlimits, can be specified multiple times.\n"
574 " -s: Use seccomp mode 1 (not the same as -S).\n"
575 " -S <file>: Set seccomp filter using <file>.\n"
576 " E.g., '-S /usr/share/filters/<prog>.$(uname -m)'.\n"
577 " Requires -n when not running as root.\n"
578 " -t[size]: Mount tmpfs at /tmp (implies -v).\n"
579 " Optional argument specifies size (default \"64M\").\n"
580 " -T <type>: Assume <program> is a <type> ELF binary; <type> can be 'static' or 'dynamic'.\n"
581 " This will avoid accessing <program> binary before execve(2).\n"
582 " Type 'static' will avoid preload hooking.\n"
583 " -u <user>: Change uid to <user>.\n"
584 " -U: Enter new user namespace (implies -p).\n"
585 " -v: Enter new mount namespace.\n"
586 " -V <file>: Enter specified mount namespace.\n"
587 " -w: Create and join a new anonymous session keyring.\n"
588 " -Y: Synchronize seccomp filters across thread group.\n"
589 " -z: Don't forward signals to jailed process.\n"
590 " --ambient: Raise ambient capabilities. Requires -c.\n"
591 " --uts[=name]: Enter a new UTS namespace (and set hostname).\n"
592 " --logging=<s>:Use <s> as the logging system.\n"
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400593 " <s> must be 'auto' (default), 'syslog', or 'stderr'.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700594 " --profile <p>:Configure minijail0 to run with the <p> sandboxing profile,\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500595 " which is a convenient way to express multiple flags\n"
596 " that are typically used together.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700597 " See the minijail0(1) man page for the full list.\n"
598 " --preload-library=<f>:Overrides the path to \"" PRELOADPATH "\".\n"
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700599 " This is only really useful for local testing.\n"
600 " --seccomp-bpf-binary=<f>:Set a pre-compiled seccomp filter using <f>.\n"
601 " E.g., '-S /usr/share/filters/<prog>.$(uname -m).bpf'.\n"
602 " Requires -n when not running as root.\n"
603 " The user is responsible for ensuring that the binary\n"
604 " was compiled for the correct architecture / kernel version.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500605 /* clang-format on */
606}
607
608static void seccomp_filter_usage(const char *progn)
609{
610 const struct syscall_entry *entry = syscall_table;
611 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
612 "System call names supported:\n",
613 progn);
614 for (; entry->name && entry->nr >= 0; ++entry)
615 printf(" %s [%d]\n", entry->name, entry->nr);
616 printf("\nSee minijail0(5) for example policies.\n");
617}
618
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700619int parse_args(struct minijail *j, int argc, char *const argv[],
620 int *exit_immediately, ElfType *elftype,
621 const char **preload_path)
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500622{
623 int opt;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700624 int use_seccomp_filter = 0, use_seccomp_filter_binary = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500625 int forward = 1;
626 int binding = 0;
627 int chroot = 0, pivot_root = 0;
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400628 int mount_ns = 0, change_remount = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500629 int inherit_suppl_gids = 0, keep_suppl_gids = 0;
630 int caps = 0, ambient_caps = 0;
631 int seccomp = -1;
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800632 bool use_uid = false, use_gid = false;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500633 uid_t uid = 0;
634 gid_t gid = 0;
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100635 gid_t *suppl_gids = NULL;
636 size_t suppl_gids_count = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500637 char *uidmap = NULL, *gidmap = NULL;
638 int set_uidmap = 0, set_gidmap = 0;
639 size_t tmp_size = 0;
640 const char *filter_path = NULL;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400641 int log_to_stderr = -1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500642
643 const char *optstring =
Mike Frysinger785b1c32018-02-23 15:47:24 -0500644 "+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 -0500645 /* clang-format off */
646 const struct option long_options[] = {
647 {"help", no_argument, 0, 'h'},
648 {"mount-dev", no_argument, 0, 'd'},
649 {"ambient", no_argument, 0, 128},
650 {"uts", optional_argument, 0, 129},
651 {"logging", required_argument, 0, 130},
652 {"profile", required_argument, 0, 131},
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700653 {"preload-library", required_argument, 0, 132},
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700654 {"seccomp-bpf-binary", required_argument, 0, 133},
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100655 {"add-suppl-group", required_argument, 0, 134},
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500656 {0, 0, 0, 0},
657 };
658 /* clang-format on */
659
660 while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) !=
661 -1) {
662 switch (opt) {
663 case 'u':
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800664 if (use_uid) {
665 fprintf(stderr,
666 "-u provided multiple times.\n");
667 exit(1);
668 }
669 use_uid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500670 set_user(j, optarg, &uid, &gid);
671 break;
672 case 'g':
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800673 if (use_gid) {
674 fprintf(stderr,
675 "-g provided multiple times.\n");
676 exit(1);
677 }
678 use_gid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500679 set_group(j, optarg, &gid);
680 break;
681 case 'n':
682 minijail_no_new_privs(j);
683 break;
684 case 's':
685 if (seccomp != -1 && seccomp != 1) {
686 fprintf(stderr,
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700687 "Do not use -s, -S, or "
688 "--seccomp-bpf-binary together.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500689 exit(1);
690 }
691 seccomp = 1;
692 minijail_use_seccomp(j);
693 break;
694 case 'S':
695 if (seccomp != -1 && seccomp != 2) {
696 fprintf(stderr,
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700697 "Do not use -s, -S, or "
698 "--seccomp-bpf-binary together.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500699 exit(1);
700 }
701 seccomp = 2;
702 minijail_use_seccomp_filter(j);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700703 filter_path = optarg;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500704 use_seccomp_filter = 1;
705 break;
706 case 'l':
707 minijail_namespace_ipc(j);
708 break;
709 case 'L':
710 minijail_log_seccomp_filter_failures(j);
711 break;
712 case 'b':
713 add_binding(j, optarg);
714 binding = 1;
715 break;
716 case 'B':
717 skip_securebits(j, optarg);
718 break;
719 case 'c':
720 caps = 1;
721 use_caps(j, optarg);
722 break;
723 case 'C':
724 use_chroot(j, optarg, &chroot, pivot_root);
725 break;
726 case 'k':
727 add_mount(j, optarg);
728 break;
729 case 'K':
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400730 if (optarg) {
Mike Frysinger785b1c32018-02-23 15:47:24 -0500731 set_remount_mode(j, optarg);
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400732 } else {
Mike Frysinger785b1c32018-02-23 15:47:24 -0500733 minijail_skip_remount_private(j);
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400734 }
735 change_remount = 1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500736 break;
737 case 'P':
738 use_pivot_root(j, optarg, &pivot_root, chroot);
739 break;
740 case 'f':
741 if (0 != minijail_write_pid_file(j, optarg)) {
742 fprintf(stderr,
743 "Could not prepare pid file path.\n");
744 exit(1);
745 }
746 break;
747 case 't':
748 minijail_namespace_vfs(j);
749 if (!tmp_size) {
750 /*
751 * Avoid clobbering |tmp_size| if it was already
752 * set.
753 */
754 tmp_size = DEFAULT_TMP_SIZE;
755 }
756 if (optarg != NULL &&
757 0 != parse_size(&tmp_size, optarg)) {
758 fprintf(stderr, "Invalid /tmp tmpfs size.\n");
759 exit(1);
760 }
761 break;
762 case 'v':
763 minijail_namespace_vfs(j);
764 mount_ns = 1;
765 break;
766 case 'V':
767 minijail_namespace_enter_vfs(j, optarg);
768 break;
769 case 'r':
770 minijail_remount_proc_readonly(j);
771 break;
772 case 'G':
773 if (keep_suppl_gids) {
774 fprintf(stderr,
775 "-y and -G are not compatible.\n");
776 exit(1);
777 }
778 minijail_inherit_usergroups(j);
779 inherit_suppl_gids = 1;
780 break;
781 case 'y':
782 if (inherit_suppl_gids) {
783 fprintf(stderr,
784 "-y and -G are not compatible.\n");
785 exit(1);
786 }
787 minijail_keep_supplementary_gids(j);
788 keep_suppl_gids = 1;
789 break;
790 case 'N':
791 minijail_namespace_cgroups(j);
792 break;
793 case 'p':
794 minijail_namespace_pids(j);
795 break;
796 case 'e':
797 if (optarg)
798 minijail_namespace_enter_net(j, optarg);
799 else
800 minijail_namespace_net(j);
801 break;
802 case 'i':
803 *exit_immediately = 1;
804 break;
805 case 'H':
806 seccomp_filter_usage(argv[0]);
807 exit(0);
808 case 'I':
809 minijail_namespace_pids(j);
810 minijail_run_as_init(j);
811 break;
812 case 'U':
813 minijail_namespace_user(j);
814 minijail_namespace_pids(j);
815 break;
816 case 'm':
817 set_uidmap = 1;
818 if (uidmap) {
819 free(uidmap);
820 uidmap = NULL;
821 }
822 if (optarg)
823 uidmap = strdup(optarg);
824 break;
825 case 'M':
826 set_gidmap = 1;
827 if (gidmap) {
828 free(gidmap);
829 gidmap = NULL;
830 }
831 if (optarg)
832 gidmap = strdup(optarg);
833 break;
834 case 'a':
835 if (0 != minijail_use_alt_syscall(j, optarg)) {
836 fprintf(stderr,
837 "Could not set alt-syscall table.\n");
838 exit(1);
839 }
840 break;
841 case 'R':
842 add_rlimit(j, optarg);
843 break;
844 case 'T':
845 if (!strcmp(optarg, "static"))
846 *elftype = ELFSTATIC;
847 else if (!strcmp(optarg, "dynamic"))
848 *elftype = ELFDYNAMIC;
849 else {
850 fprintf(stderr, "ELF type must be 'static' or "
851 "'dynamic'.\n");
852 exit(1);
853 }
854 break;
855 case 'w':
856 minijail_new_session_keyring(j);
857 break;
858 case 'Y':
859 minijail_set_seccomp_filter_tsync(j);
860 break;
861 case 'z':
862 forward = 0;
863 break;
864 case 'd':
865 minijail_namespace_vfs(j);
866 minijail_mount_dev(j);
867 break;
868 /* Long options. */
869 case 128: /* Ambient caps. */
870 ambient_caps = 1;
871 minijail_set_ambient_caps(j);
872 break;
873 case 129: /* UTS/hostname namespace. */
874 minijail_namespace_uts(j);
875 if (optarg)
876 minijail_namespace_set_hostname(j, optarg);
877 break;
878 case 130: /* Logging. */
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400879 if (!strcmp(optarg, "auto")) {
880 log_to_stderr = -1;
881 } else if (!strcmp(optarg, "syslog")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500882 log_to_stderr = 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400883 } else if (!strcmp(optarg, "stderr")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500884 log_to_stderr = 1;
885 } else {
886 fprintf(stderr, "--logger must be 'syslog' or "
887 "'stderr'.\n");
888 exit(1);
889 }
890 break;
891 case 131: /* Profile */
892 use_profile(j, optarg, &pivot_root, chroot, &tmp_size);
893 break;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700894 case 132: /* PRELOADPATH */
895 *preload_path = optarg;
896 break;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700897 case 133: /* seccomp-bpf binary. */
898 if (seccomp != -1 && seccomp != 3) {
899 fprintf(stderr,
900 "Do not use -s, -S, or "
901 "--seccomp-bpf-binary together.\n");
902 exit(1);
903 }
904 seccomp = 3;
905 minijail_use_seccomp_filter(j);
906 filter_path = optarg;
907 use_seccomp_filter_binary = 1;
908 break;
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100909 case 134:
910 suppl_group_add(&suppl_gids_count, &suppl_gids,
911 optarg);
912 break;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500913 default:
914 usage(argv[0]);
915 exit(opt == 'h' ? 0 : 1);
916 }
917 }
918
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400919 if (log_to_stderr == -1) {
920 /* Autodetect default logging output. */
Mike Frysinger056955c2019-09-24 16:07:05 -0400921 log_to_stderr = isatty(STDIN_FILENO) ? 1 : 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400922 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500923 if (log_to_stderr) {
924 init_logging(LOG_TO_FD, STDERR_FILENO, LOG_INFO);
925 /*
926 * When logging to stderr, ensure the FD survives the jailing.
927 */
928 if (0 !=
929 minijail_preserve_fd(j, STDERR_FILENO, STDERR_FILENO)) {
930 fprintf(stderr, "Could not preserve stderr.\n");
931 exit(1);
932 }
933 }
934
935 /* Set up uid/gid mapping. */
936 if (set_uidmap || set_gidmap) {
937 set_ugid_mapping(j, set_uidmap, uid, uidmap, set_gidmap, gid,
938 gidmap);
939 }
940
941 /* Can only set ambient caps when using regular caps. */
942 if (ambient_caps && !caps) {
943 fprintf(stderr, "Can't set ambient capabilities (--ambient) "
944 "without actually using capabilities (-c).\n");
945 exit(1);
946 }
947
948 /* Set up signal handlers in minijail unless asked not to. */
949 if (forward)
950 minijail_forward_signals(j);
951
952 /*
953 * Only allow bind mounts when entering a chroot, using pivot_root, or
954 * a new mount namespace.
955 */
956 if (binding && !(chroot || pivot_root || mount_ns)) {
957 fprintf(stderr, "Bind mounts require a chroot, pivot_root, or "
958 " new mount namespace.\n");
959 exit(1);
960 }
961
962 /*
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400963 * / is only remounted when entering a new mount namespace, so unless
964 * that's set there is no need for the -K/-K<mode> flags.
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500965 */
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400966 if (change_remount && !mount_ns) {
967 fprintf(stderr, "No need to use -K (skip remounting '/') or "
968 "-K<mode> (remount '/' as <mode>)\n"
969 "without -v (new mount namespace).\n"
970 "Do you need to add '-v' explicitly?\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500971 exit(1);
972 }
973
974 /*
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100975 * Proceed in setting the supplementary gids specified on the
976 * cmdline options.
977 */
978 if (suppl_gids_count) {
979 minijail_set_supplementary_gids(j, suppl_gids_count,
980 suppl_gids);
981 free(suppl_gids);
982 }
983
984 /*
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500985 * We parse seccomp filters here to make sure we've collected all
986 * cmdline options.
987 */
988 if (use_seccomp_filter) {
989 minijail_parse_seccomp_filters(j, filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700990 } else if (use_seccomp_filter_binary) {
991 struct sock_fprog filter;
992 read_seccomp_filter(filter_path, &filter);
993 minijail_set_seccomp_filters(j, &filter);
994 free((void *)filter.filter);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500995 }
996
997 /* Mount a tmpfs under /tmp and set its size. */
998 if (tmp_size)
999 minijail_mount_tmp_size(j, tmp_size);
1000
1001 /*
1002 * There should be at least one additional unparsed argument: the
1003 * executable name.
1004 */
1005 if (argc == optind) {
1006 usage(argv[0]);
1007 exit(1);
1008 }
1009
1010 if (*elftype == ELFERROR) {
1011 /*
1012 * -T was not specified.
1013 * Get the path to the program adjusted for changing root.
1014 */
1015 char *program_path =
1016 minijail_get_original_path(j, argv[optind]);
1017
1018 /* Check that we can access the target program. */
1019 if (access(program_path, X_OK)) {
1020 fprintf(stderr,
1021 "Target program '%s' is not accessible.\n",
1022 argv[optind]);
1023 exit(1);
1024 }
1025
1026 /* Check if target is statically or dynamically linked. */
1027 *elftype = get_elf_linkage(program_path);
1028 free(program_path);
1029 }
1030
1031 /*
1032 * Setting capabilities need either a dynamically-linked binary, or the
1033 * use of ambient capabilities for them to be able to survive an
1034 * execve(2).
1035 */
1036 if (caps && *elftype == ELFSTATIC && !ambient_caps) {
1037 fprintf(stderr, "Can't run statically-linked binaries with "
1038 "capabilities (-c) without also setting "
1039 "ambient capabilities. Try passing "
1040 "--ambient.\n");
1041 exit(1);
1042 }
1043
1044 return optind;
1045}