blob: 22da7fd72a975d121003aabdbb9993c640c392cc [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
Mike Frysinger1036cd82020-08-28 00:15:59 -040032/*
33 * A malloc() that aborts on failure. We only implement this in the CLI as
34 * the library should return ENOMEM errors when allocations fail.
35 */
36static void *xmalloc(size_t size)
37{
38 void *ret = malloc(size);
39 if (!ret) {
40 perror("malloc() failed");
41 exit(1);
42 }
43 return ret;
44}
45
46static char *xstrdup(const char *s)
47{
48 char *ret = strdup(s);
49 if (!ret) {
50 perror("strdup() failed");
51 exit(1);
52 }
53 return ret;
54}
55
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050056static void set_user(struct minijail *j, const char *arg, uid_t *out_uid,
57 gid_t *out_gid)
58{
59 char *end = NULL;
60 int uid = strtod(arg, &end);
61 if (!*end && *arg) {
62 *out_uid = uid;
63 minijail_change_uid(j, uid);
64 return;
65 }
66
Mattias Nissler160d58f2020-02-25 11:01:30 +010067 int ret = lookup_user(arg, out_uid, out_gid);
68 if (ret) {
69 fprintf(stderr, "Bad user '%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 ret = minijail_change_user(j, arg);
74 if (ret) {
75 fprintf(stderr, "minijail_change_user('%s') failed: %s\n", arg,
76 strerror(-ret));
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050077 exit(1);
78 }
79}
80
81static void set_group(struct minijail *j, const char *arg, gid_t *out_gid)
82{
83 char *end = NULL;
84 int gid = strtod(arg, &end);
85 if (!*end && *arg) {
86 *out_gid = gid;
87 minijail_change_gid(j, gid);
88 return;
89 }
90
Mattias Nissler160d58f2020-02-25 11:01:30 +010091 int ret = lookup_group(arg, out_gid);
92 if (ret) {
93 fprintf(stderr, "Bad group '%s': %s\n", arg, strerror(-ret));
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050094 exit(1);
95 }
96
Mattias Nissler160d58f2020-02-25 11:01:30 +010097 minijail_change_gid(j, *out_gid);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050098}
99
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100100/*
101 * Helper function used by --add-suppl-group (possibly more than once),
102 * to build the supplementary gids array.
103 */
104static void suppl_group_add(size_t *suppl_gids_count, gid_t **suppl_gids,
105 char *arg) {
106 char *end = NULL;
107 int groupid = strtod(arg, &end);
108 gid_t gid;
Mattias Nissler160d58f2020-02-25 11:01:30 +0100109 int ret;
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100110 if (!*end && *arg) {
111 /* A gid number has been specified, proceed. */
112 gid = groupid;
Mattias Nissler160d58f2020-02-25 11:01:30 +0100113 } else if ((ret = lookup_group(arg, &gid))) {
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100114 /*
115 * A group name has been specified,
116 * but doesn't exist: we bail out.
117 */
Mattias Nissler160d58f2020-02-25 11:01:30 +0100118 fprintf(stderr, "Bad group '%s': %s\n", arg, strerror(-ret));
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100119 exit(1);
120 }
121
122 /*
123 * From here, gid is guaranteed to be set and valid,
124 * we add it to our supplementary gids array.
125 */
126 *suppl_gids = realloc(*suppl_gids,
127 sizeof(gid_t) * ++(*suppl_gids_count));
128 if (!suppl_gids) {
129 fprintf(stderr, "failed to allocate memory.\n");
130 exit(1);
131 }
132
133 (*suppl_gids)[*suppl_gids_count - 1] = gid;
134}
135
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500136static void skip_securebits(struct minijail *j, const char *arg)
137{
138 uint64_t securebits_skip_mask;
139 char *end = NULL;
140 securebits_skip_mask = strtoull(arg, &end, 16);
141 if (*end) {
142 fprintf(stderr, "Invalid securebit mask: '%s'\n", arg);
143 exit(1);
144 }
145 minijail_skip_setting_securebits(j, securebits_skip_mask);
146}
147
148static void use_caps(struct minijail *j, const char *arg)
149{
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700150 uint64_t caps = 0;
151 cap_t parsed_caps = cap_from_text(arg);
152
153 if (parsed_caps != NULL) {
154 unsigned int i;
155 const uint64_t one = 1;
156 cap_flag_value_t cap_value;
157 unsigned int last_valid_cap = get_last_valid_cap();
158
159 for (i = 0; i <= last_valid_cap; ++i) {
160 if (cap_get_flag(parsed_caps, i, CAP_EFFECTIVE,
161 &cap_value)) {
Luis Hector Chavez677900f2018-09-24 09:13:26 -0700162 if (errno == EINVAL) {
163 /*
164 * Some versions of libcap reject any
165 * capabilities they were not compiled
166 * with by returning EINVAL.
167 */
168 continue;
169 }
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700170 fprintf(stderr,
171 "Could not get the value of "
172 "the %d-th capability: %m\n",
173 i);
174 exit(1);
175 }
176 if (cap_value == CAP_SET)
177 caps |= (one << i);
178 }
179 cap_free(parsed_caps);
180 } else {
181 char *end = NULL;
182 caps = strtoull(arg, &end, 16);
183 if (*end) {
184 fprintf(stderr, "Invalid cap set: '%s'\n", arg);
185 exit(1);
186 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500187 }
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700188
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500189 minijail_use_caps(j, caps);
190}
191
192static void add_binding(struct minijail *j, char *arg)
193{
194 char *src = tokenize(&arg, ",");
195 char *dest = tokenize(&arg, ",");
196 char *flags = tokenize(&arg, ",");
197 if (!src || src[0] == '\0' || arg != NULL) {
198 fprintf(stderr, "Bad binding: %s %s\n", src, dest);
199 exit(1);
200 }
201 if (dest == NULL || dest[0] == '\0')
202 dest = src;
David Coles87ec5cd2019-06-13 17:20:10 -0700203 int writable;
204 if (flags == NULL || flags[0] == '\0' || !strcmp(flags, "0"))
205 writable = 0;
206 else if (!strcmp(flags, "1"))
207 writable = 1;
208 else {
209 fprintf(stderr, "Bad value for <writable>: %s\n", flags);
210 exit(1);
211 }
212 if (minijail_bind(j, src, dest, writable)) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500213 fprintf(stderr, "minijail_bind failed.\n");
214 exit(1);
215 }
216}
217
218static void add_rlimit(struct minijail *j, char *arg)
219{
220 char *type = tokenize(&arg, ",");
221 char *cur = tokenize(&arg, ",");
222 char *max = tokenize(&arg, ",");
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800223 char *end;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500224 if (!type || type[0] == '\0' || !cur || cur[0] == '\0' ||
225 !max || max[0] == '\0' || arg != NULL) {
226 fprintf(stderr, "Bad rlimit '%s'.\n", arg);
227 exit(1);
228 }
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800229 rlim_t cur_rlim;
230 rlim_t max_rlim;
231 if (!strcmp(cur, "unlimited")) {
232 cur_rlim = RLIM_INFINITY;
233 } else {
234 end = NULL;
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400235 cur_rlim = strtoul(cur, &end, 0);
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800236 if (*end) {
237 fprintf(stderr, "Bad soft limit: '%s'.\n", cur);
238 exit(1);
239 }
240 }
241 if (!strcmp(max, "unlimited")) {
242 max_rlim = RLIM_INFINITY;
243 } else {
244 end = NULL;
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400245 max_rlim = strtoul(max, &end, 0);
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800246 if (*end) {
247 fprintf(stderr, "Bad hard limit: '%s'.\n", max);
248 exit(1);
249 }
250 }
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400251
252 end = NULL;
253 int resource = parse_single_constant(type, &end);
254 if (type == end) {
255 fprintf(stderr, "Bad rlimit: '%s'.\n", type);
256 exit(1);
257 }
258
259 if (minijail_rlimit(j, resource, cur_rlim, max_rlim)) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500260 fprintf(stderr, "minijail_rlimit '%s,%s,%s' failed.\n", type,
261 cur, max);
262 exit(1);
263 }
264}
265
266static void add_mount(struct minijail *j, char *arg)
267{
268 char *src = tokenize(&arg, ",");
269 char *dest = tokenize(&arg, ",");
270 char *type = tokenize(&arg, ",");
271 char *flags = tokenize(&arg, ",");
272 char *data = tokenize(&arg, ",");
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400273 char *end;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500274 if (!src || src[0] == '\0' || !dest || dest[0] == '\0' ||
Mike Frysinger4f3e09f2018-01-24 18:01:16 -0500275 !type || type[0] == '\0') {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500276 fprintf(stderr, "Bad mount: %s %s %s\n", src, dest, type);
277 exit(1);
278 }
Mike Frysinger4f3e09f2018-01-24 18:01:16 -0500279
280 /*
281 * Fun edge case: the data option itself is comma delimited. If there
282 * were no more options, then arg would be set to NULL. But if we had
283 * more pending, it'll be pointing to the next token. Back up and undo
284 * the null byte so it'll be merged back.
285 * An example:
286 * none,/tmp,tmpfs,0xe,mode=0755,uid=10,gid=10
287 * The tokenize calls above will turn this memory into:
288 * none\0/tmp\0tmpfs\00xe\0mode=0755\0uid=10,gid=10
289 * With data pointing at mode=0755 and arg pointing at uid=10,gid=10.
290 */
291 if (arg != NULL)
292 arg[-1] = ',';
293
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400294 unsigned long mountflags;
295 if (flags == NULL || flags[0] == '\0') {
296 mountflags = 0;
297 } else {
298 end = NULL;
299 mountflags = parse_constant(flags, &end);
300 if (flags == end) {
301 fprintf(stderr, "Bad mount flags: %s\n", flags);
302 exit(1);
303 }
304 }
305
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500306 if (minijail_mount_with_data(j, src, dest, type,
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400307 mountflags, data)) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500308 fprintf(stderr, "minijail_mount failed.\n");
309 exit(1);
310 }
311}
312
313static char *build_idmap(id_t id, id_t lowerid)
314{
315 int ret;
Mike Frysinger1036cd82020-08-28 00:15:59 -0400316 char *idmap = xmalloc(IDMAP_LEN);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500317 ret = snprintf(idmap, IDMAP_LEN, "%d %d 1", id, lowerid);
318 if (ret < 0 || (size_t)ret >= IDMAP_LEN) {
319 free(idmap);
320 fprintf(stderr, "Could not build id map.\n");
321 exit(1);
322 }
323 return idmap;
324}
325
326static int has_cap_setgid(void)
327{
328 cap_t caps;
329 cap_flag_value_t cap_value;
330
331 if (!CAP_IS_SUPPORTED(CAP_SETGID))
332 return 0;
333
334 caps = cap_get_proc();
335 if (!caps) {
336 fprintf(stderr, "Could not get process' capabilities: %m\n");
337 exit(1);
338 }
339
340 if (cap_get_flag(caps, CAP_SETGID, CAP_EFFECTIVE, &cap_value)) {
341 fprintf(stderr, "Could not get the value of CAP_SETGID: %m\n");
342 exit(1);
343 }
344
345 if (cap_free(caps)) {
346 fprintf(stderr, "Could not free capabilities: %m\n");
347 exit(1);
348 }
349
350 return cap_value == CAP_SET;
351}
352
353static void set_ugid_mapping(struct minijail *j, int set_uidmap, uid_t uid,
354 char *uidmap, int set_gidmap, gid_t gid,
355 char *gidmap)
356{
357 if (set_uidmap) {
358 minijail_namespace_user(j);
359 minijail_namespace_pids(j);
360
361 if (!uidmap) {
362 /*
363 * If no map is passed, map the current uid to the
364 * chosen uid in the target namespace (or root, if none
365 * was chosen).
366 */
367 uidmap = build_idmap(uid, getuid());
368 }
369 if (0 != minijail_uidmap(j, uidmap)) {
370 fprintf(stderr, "Could not set uid map.\n");
371 exit(1);
372 }
373 free(uidmap);
374 }
375 if (set_gidmap) {
376 minijail_namespace_user(j);
377 minijail_namespace_pids(j);
378
379 if (!gidmap) {
380 /*
381 * If no map is passed, map the current gid to the
382 * chosen gid in the target namespace.
383 */
384 gidmap = build_idmap(gid, getgid());
385 }
386 if (!has_cap_setgid()) {
387 /*
388 * This means that we are not running as root,
389 * so we also have to disable setgroups(2) to
390 * be able to set the gid map.
391 * See
392 * http://man7.org/linux/man-pages/man7/user_namespaces.7.html
393 */
394 minijail_namespace_user_disable_setgroups(j);
395 }
396 if (0 != minijail_gidmap(j, gidmap)) {
397 fprintf(stderr, "Could not set gid map.\n");
398 exit(1);
399 }
400 free(gidmap);
401 }
402}
403
404static void use_chroot(struct minijail *j, const char *path, int *chroot,
405 int pivot_root)
406{
407 if (pivot_root) {
408 fprintf(stderr, "Could not set chroot because "
409 "'-P' was specified.\n");
410 exit(1);
411 }
412 if (minijail_enter_chroot(j, path)) {
413 fprintf(stderr, "Could not set chroot.\n");
414 exit(1);
415 }
416 *chroot = 1;
417}
418
419static void use_pivot_root(struct minijail *j, const char *path,
420 int *pivot_root, int chroot)
421{
422 if (chroot) {
423 fprintf(stderr, "Could not set pivot_root because "
424 "'-C' was specified.\n");
425 exit(1);
426 }
427 if (minijail_enter_pivot_root(j, path)) {
428 fprintf(stderr, "Could not set pivot_root.\n");
429 exit(1);
430 }
431 minijail_namespace_vfs(j);
432 *pivot_root = 1;
433}
434
435static void use_profile(struct minijail *j, const char *profile,
436 int *pivot_root, int chroot, size_t *tmp_size)
437{
Mike Frysinger4d2a81e2018-01-22 16:43:33 -0500438 /* Note: New profiles should be added in minijail0_cli_unittest.cc. */
439
Mike Frysingercc5917c2020-02-03 12:34:14 -0500440 if (!strcmp(profile, "minimalistic-mountns") ||
441 !strcmp(profile, "minimalistic-mountns-nodev")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500442 minijail_namespace_vfs(j);
443 if (minijail_bind(j, "/", "/", 0)) {
Jorge Lucangeli Obes7394b902019-03-14 12:43:26 -0400444 fprintf(stderr, "minijail_bind(/) failed.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500445 exit(1);
446 }
447 if (minijail_bind(j, "/proc", "/proc", 0)) {
Jorge Lucangeli Obes7394b902019-03-14 12:43:26 -0400448 fprintf(stderr, "minijail_bind(/proc) failed.\n");
449 exit(1);
450 }
Mike Frysingercc5917c2020-02-03 12:34:14 -0500451 if (!strcmp(profile, "minimalistic-mountns")) {
452 if (minijail_bind(j, "/dev/log", "/dev/log", 0)) {
453 fprintf(stderr, "minijail_bind(/dev/log) failed.\n");
454 exit(1);
455 }
456 minijail_mount_dev(j);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500457 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500458 if (!*tmp_size) {
459 /* Avoid clobbering |tmp_size| if it was already set. */
460 *tmp_size = DEFAULT_TMP_SIZE;
461 }
462 minijail_remount_proc_readonly(j);
Allen Webbee876072019-02-21 10:56:21 -0800463 use_pivot_root(j, DEFAULT_PIVOT_ROOT, pivot_root, chroot);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500464 } else {
465 fprintf(stderr, "Unrecognized profile name '%s'\n", profile);
466 exit(1);
467 }
468}
469
Mike Frysinger785b1c32018-02-23 15:47:24 -0500470static void set_remount_mode(struct minijail *j, const char *mode)
471{
472 unsigned long msmode;
473 if (!strcmp(mode, "shared"))
474 msmode = MS_SHARED;
475 else if (!strcmp(mode, "private"))
476 msmode = MS_PRIVATE;
477 else if (!strcmp(mode, "slave"))
478 msmode = MS_SLAVE;
479 else if (!strcmp(mode, "unbindable"))
480 msmode = MS_UNBINDABLE;
481 else {
482 fprintf(stderr, "Unknown remount mode: '%s'\n", mode);
483 exit(1);
484 }
485 minijail_remount_mode(j, msmode);
486}
487
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700488static void read_seccomp_filter(const char *filter_path,
489 struct sock_fprog *filter)
490{
491 FILE *f = fopen(filter_path, "re");
492 if (!f) {
493 fprintf(stderr, "failed to open %s: %m", filter_path);
494 exit(1);
495 }
496 off_t filter_size = 0;
497 if (fseeko(f, 0, SEEK_END) == -1 || (filter_size = ftello(f)) == -1) {
498 fclose(f);
499 fprintf(stderr, "failed to get file size of %s: %m",
500 filter_path);
501 exit(1);
502 }
503 if (filter_size % sizeof(struct sock_filter) != 0) {
504 fclose(f);
505 fprintf(stderr,
506 "filter size (%" PRId64
507 ") of %s is not a multiple of %zu: %m",
508 filter_size, filter_path, sizeof(struct sock_filter));
509 exit(1);
510 }
511 rewind(f);
512
513 filter->len = filter_size / sizeof(struct sock_filter);
Mike Frysinger1036cd82020-08-28 00:15:59 -0400514 filter->filter = xmalloc(filter_size);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700515 if (fread(filter->filter, sizeof(struct sock_filter), filter->len, f) !=
516 filter->len) {
517 fclose(f);
518 fprintf(stderr, "failed read %s: %m", filter_path);
519 exit(1);
520 }
521 fclose(f);
522}
523
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500524static void usage(const char *progn)
525{
526 size_t i;
527 /* clang-format off */
528 printf("Usage: %s [-dGhHiIKlLnNprRstUvyYz]\n"
529 " [-a <table>]\n"
530 " [-b <src>[,<dest>[,<writeable>]]] [-k <src>,<dest>,<type>[,<flags>[,<data>]]]\n"
531 " [-c <caps>] [-C <dir>] [-P <dir>] [-e[file]] [-f <file>] [-g <group>]\n"
532 " [-m[<uid> <loweruid> <count>]*] [-M[<gid> <lowergid> <count>]*] [--profile <name>]\n"
533 " [-R <type,cur,max>] [-S <file>] [-t[size]] [-T <type>] [-u <user>] [-V <file>]\n"
534 " <program> [args...]\n"
535 " -a <table>: Use alternate syscall table <table>.\n"
536 " -b <...>: Bind <src> to <dest> in chroot.\n"
537 " Multiple instances allowed.\n"
538 " -B <mask>: Skip setting securebits in <mask> when restricting capabilities (-c).\n"
539 " By default, SECURE_NOROOT, SECURE_NO_SETUID_FIXUP, and \n"
540 " SECURE_KEEP_CAPS (together with their respective locks) are set.\n"
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -0400541 " There are eight securebits in total.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500542 " -k <...>: Mount <src> at <dest> in chroot.\n"
543 " <flags> and <data> can be specified as in mount(2).\n"
544 " Multiple instances allowed.\n"
545 " -c <caps>: Restrict caps to <caps>.\n"
546 " -C <dir>: chroot(2) to <dir>.\n"
547 " Not compatible with -P.\n"
548 " -P <dir>: pivot_root(2) to <dir> (implies -v).\n"
549 " Not compatible with -C.\n"
550 " --mount-dev, Create a new /dev with a minimal set of device nodes (implies -v).\n"
551 " -d: See the minijail0(1) man page for the exact set.\n"
552 " -e[file]: Enter new network namespace, or existing one if |file| is provided.\n"
553 " -f <file>: Write the pid of the jailed process to <file>.\n"
554 " -g <group>: Change gid to <group>.\n"
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100555 " -G: Inherit supplementary groups from new uid.\n"
556 " Not compatible with -y or --add-suppl-group.\n"
557 " -y: Keep original uid's supplementary groups.\n"
558 " Not compatible with -G or --add-suppl-group.\n"
559 " --add-suppl-group <g>:Add <g> to the proccess' supplementary groups,\n"
560 " can be specified multiple times to add several groups.\n"
561 " Not compatible with -y or -G.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500562 " -h: Help (this message).\n"
563 " -H: Seccomp filter help message.\n"
Luis Hector Chavez9dd13fd2018-04-19 20:14:47 -0700564 " -i: Exit immediately after fork(2). The jailed process will run\n"
565 " in the background.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500566 " -I: Run <program> as init (pid 1) inside a new pid namespace (implies -p).\n"
Mike Frysinger785b1c32018-02-23 15:47:24 -0500567 " -K: Do not change share mode of any existing mounts.\n"
568 " -K<mode>: Mark all existing mounts as <mode> instead of MS_PRIVATE.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500569 " -l: Enter new IPC namespace.\n"
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400570 " -L: Report blocked syscalls when using seccomp filter.\n"
571 " If the kernel does not support SECCOMP_RET_LOG,\n"
572 " forces the following syscalls to be allowed:\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500573 " ", progn);
574 /* clang-format on */
575 for (i = 0; i < log_syscalls_len; i++)
576 printf("%s ", log_syscalls[i]);
577
578 /* clang-format off */
579 printf("\n"
580 " -m[map]: Set the uid map of a user namespace (implies -pU).\n"
581 " Same arguments as newuidmap(1), multiple mappings should be separated by ',' (comma).\n"
582 " With no mapping, map the current uid to root inside the user namespace.\n"
583 " Not compatible with -b without the 'writable' option.\n"
584 " -M[map]: Set the gid map of a user namespace (implies -pU).\n"
585 " Same arguments as newgidmap(1), multiple mappings should be separated by ',' (comma).\n"
586 " With no mapping, map the current gid to root inside the user namespace.\n"
587 " Not compatible with -b without the 'writable' option.\n"
588 " -n: Set no_new_privs.\n"
589 " -N: Enter a new cgroup namespace.\n"
590 " -p: Enter new pid namespace (implies -vr).\n"
591 " -r: Remount /proc read-only (implies -v).\n"
592 " -R: Set rlimits, can be specified multiple times.\n"
593 " -s: Use seccomp mode 1 (not the same as -S).\n"
594 " -S <file>: Set seccomp filter using <file>.\n"
595 " E.g., '-S /usr/share/filters/<prog>.$(uname -m)'.\n"
596 " Requires -n when not running as root.\n"
597 " -t[size]: Mount tmpfs at /tmp (implies -v).\n"
598 " Optional argument specifies size (default \"64M\").\n"
599 " -T <type>: Assume <program> is a <type> ELF binary; <type> can be 'static' or 'dynamic'.\n"
600 " This will avoid accessing <program> binary before execve(2).\n"
601 " Type 'static' will avoid preload hooking.\n"
602 " -u <user>: Change uid to <user>.\n"
603 " -U: Enter new user namespace (implies -p).\n"
604 " -v: Enter new mount namespace.\n"
605 " -V <file>: Enter specified mount namespace.\n"
606 " -w: Create and join a new anonymous session keyring.\n"
607 " -Y: Synchronize seccomp filters across thread group.\n"
608 " -z: Don't forward signals to jailed process.\n"
609 " --ambient: Raise ambient capabilities. Requires -c.\n"
610 " --uts[=name]: Enter a new UTS namespace (and set hostname).\n"
611 " --logging=<s>:Use <s> as the logging system.\n"
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400612 " <s> must be 'auto' (default), 'syslog', or 'stderr'.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700613 " --profile <p>:Configure minijail0 to run with the <p> sandboxing profile,\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500614 " which is a convenient way to express multiple flags\n"
615 " that are typically used together.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700616 " See the minijail0(1) man page for the full list.\n"
617 " --preload-library=<f>:Overrides the path to \"" PRELOADPATH "\".\n"
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700618 " This is only really useful for local testing.\n"
619 " --seccomp-bpf-binary=<f>:Set a pre-compiled seccomp filter using <f>.\n"
620 " E.g., '-S /usr/share/filters/<prog>.$(uname -m).bpf'.\n"
621 " Requires -n when not running as root.\n"
622 " The user is responsible for ensuring that the binary\n"
623 " was compiled for the correct architecture / kernel version.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500624 /* clang-format on */
625}
626
627static void seccomp_filter_usage(const char *progn)
628{
629 const struct syscall_entry *entry = syscall_table;
630 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
631 "System call names supported:\n",
632 progn);
633 for (; entry->name && entry->nr >= 0; ++entry)
634 printf(" %s [%d]\n", entry->name, entry->nr);
635 printf("\nSee minijail0(5) for example policies.\n");
636}
637
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700638int parse_args(struct minijail *j, int argc, char *const argv[],
639 int *exit_immediately, ElfType *elftype,
640 const char **preload_path)
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500641{
642 int opt;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700643 int use_seccomp_filter = 0, use_seccomp_filter_binary = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500644 int forward = 1;
645 int binding = 0;
646 int chroot = 0, pivot_root = 0;
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400647 int mount_ns = 0, change_remount = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500648 int inherit_suppl_gids = 0, keep_suppl_gids = 0;
649 int caps = 0, ambient_caps = 0;
650 int seccomp = -1;
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800651 bool use_uid = false, use_gid = false;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500652 uid_t uid = 0;
653 gid_t gid = 0;
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100654 gid_t *suppl_gids = NULL;
655 size_t suppl_gids_count = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500656 char *uidmap = NULL, *gidmap = NULL;
657 int set_uidmap = 0, set_gidmap = 0;
658 size_t tmp_size = 0;
659 const char *filter_path = NULL;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400660 int log_to_stderr = -1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500661
662 const char *optstring =
Mike Frysinger785b1c32018-02-23 15:47:24 -0500663 "+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 -0500664 /* clang-format off */
665 const struct option long_options[] = {
666 {"help", no_argument, 0, 'h'},
667 {"mount-dev", no_argument, 0, 'd'},
668 {"ambient", no_argument, 0, 128},
669 {"uts", optional_argument, 0, 129},
670 {"logging", required_argument, 0, 130},
671 {"profile", required_argument, 0, 131},
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700672 {"preload-library", required_argument, 0, 132},
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700673 {"seccomp-bpf-binary", required_argument, 0, 133},
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100674 {"add-suppl-group", required_argument, 0, 134},
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500675 {0, 0, 0, 0},
676 };
677 /* clang-format on */
678
679 while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) !=
680 -1) {
681 switch (opt) {
682 case 'u':
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800683 if (use_uid) {
684 fprintf(stderr,
685 "-u provided multiple times.\n");
686 exit(1);
687 }
688 use_uid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500689 set_user(j, optarg, &uid, &gid);
690 break;
691 case 'g':
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800692 if (use_gid) {
693 fprintf(stderr,
694 "-g provided multiple times.\n");
695 exit(1);
696 }
697 use_gid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500698 set_group(j, optarg, &gid);
699 break;
700 case 'n':
701 minijail_no_new_privs(j);
702 break;
703 case 's':
704 if (seccomp != -1 && seccomp != 1) {
705 fprintf(stderr,
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700706 "Do not use -s, -S, or "
707 "--seccomp-bpf-binary together.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500708 exit(1);
709 }
710 seccomp = 1;
711 minijail_use_seccomp(j);
712 break;
713 case 'S':
714 if (seccomp != -1 && seccomp != 2) {
715 fprintf(stderr,
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700716 "Do not use -s, -S, or "
717 "--seccomp-bpf-binary together.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500718 exit(1);
719 }
720 seccomp = 2;
721 minijail_use_seccomp_filter(j);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700722 filter_path = optarg;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500723 use_seccomp_filter = 1;
724 break;
725 case 'l':
726 minijail_namespace_ipc(j);
727 break;
728 case 'L':
729 minijail_log_seccomp_filter_failures(j);
730 break;
731 case 'b':
732 add_binding(j, optarg);
733 binding = 1;
734 break;
735 case 'B':
736 skip_securebits(j, optarg);
737 break;
738 case 'c':
739 caps = 1;
740 use_caps(j, optarg);
741 break;
742 case 'C':
743 use_chroot(j, optarg, &chroot, pivot_root);
744 break;
745 case 'k':
746 add_mount(j, optarg);
747 break;
748 case 'K':
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400749 if (optarg) {
Mike Frysinger785b1c32018-02-23 15:47:24 -0500750 set_remount_mode(j, optarg);
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400751 } else {
Mike Frysinger785b1c32018-02-23 15:47:24 -0500752 minijail_skip_remount_private(j);
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400753 }
754 change_remount = 1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500755 break;
756 case 'P':
757 use_pivot_root(j, optarg, &pivot_root, chroot);
758 break;
759 case 'f':
760 if (0 != minijail_write_pid_file(j, optarg)) {
761 fprintf(stderr,
762 "Could not prepare pid file path.\n");
763 exit(1);
764 }
765 break;
766 case 't':
767 minijail_namespace_vfs(j);
768 if (!tmp_size) {
769 /*
770 * Avoid clobbering |tmp_size| if it was already
771 * set.
772 */
773 tmp_size = DEFAULT_TMP_SIZE;
774 }
775 if (optarg != NULL &&
776 0 != parse_size(&tmp_size, optarg)) {
777 fprintf(stderr, "Invalid /tmp tmpfs size.\n");
778 exit(1);
779 }
780 break;
781 case 'v':
782 minijail_namespace_vfs(j);
783 mount_ns = 1;
784 break;
785 case 'V':
786 minijail_namespace_enter_vfs(j, optarg);
787 break;
788 case 'r':
789 minijail_remount_proc_readonly(j);
790 break;
791 case 'G':
792 if (keep_suppl_gids) {
793 fprintf(stderr,
794 "-y and -G are not compatible.\n");
795 exit(1);
796 }
797 minijail_inherit_usergroups(j);
798 inherit_suppl_gids = 1;
799 break;
800 case 'y':
801 if (inherit_suppl_gids) {
802 fprintf(stderr,
803 "-y and -G are not compatible.\n");
804 exit(1);
805 }
806 minijail_keep_supplementary_gids(j);
807 keep_suppl_gids = 1;
808 break;
809 case 'N':
810 minijail_namespace_cgroups(j);
811 break;
812 case 'p':
813 minijail_namespace_pids(j);
814 break;
815 case 'e':
816 if (optarg)
817 minijail_namespace_enter_net(j, optarg);
818 else
819 minijail_namespace_net(j);
820 break;
821 case 'i':
822 *exit_immediately = 1;
823 break;
824 case 'H':
825 seccomp_filter_usage(argv[0]);
826 exit(0);
827 case 'I':
828 minijail_namespace_pids(j);
829 minijail_run_as_init(j);
830 break;
831 case 'U':
832 minijail_namespace_user(j);
833 minijail_namespace_pids(j);
834 break;
835 case 'm':
836 set_uidmap = 1;
837 if (uidmap) {
838 free(uidmap);
839 uidmap = NULL;
840 }
841 if (optarg)
Mike Frysinger1036cd82020-08-28 00:15:59 -0400842 uidmap = xstrdup(optarg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500843 break;
844 case 'M':
845 set_gidmap = 1;
846 if (gidmap) {
847 free(gidmap);
848 gidmap = NULL;
849 }
850 if (optarg)
Mike Frysinger1036cd82020-08-28 00:15:59 -0400851 gidmap = xstrdup(optarg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500852 break;
853 case 'a':
854 if (0 != minijail_use_alt_syscall(j, optarg)) {
855 fprintf(stderr,
856 "Could not set alt-syscall table.\n");
857 exit(1);
858 }
859 break;
860 case 'R':
861 add_rlimit(j, optarg);
862 break;
863 case 'T':
864 if (!strcmp(optarg, "static"))
865 *elftype = ELFSTATIC;
866 else if (!strcmp(optarg, "dynamic"))
867 *elftype = ELFDYNAMIC;
868 else {
869 fprintf(stderr, "ELF type must be 'static' or "
870 "'dynamic'.\n");
871 exit(1);
872 }
873 break;
874 case 'w':
875 minijail_new_session_keyring(j);
876 break;
877 case 'Y':
878 minijail_set_seccomp_filter_tsync(j);
879 break;
880 case 'z':
881 forward = 0;
882 break;
883 case 'd':
884 minijail_namespace_vfs(j);
885 minijail_mount_dev(j);
886 break;
887 /* Long options. */
888 case 128: /* Ambient caps. */
889 ambient_caps = 1;
890 minijail_set_ambient_caps(j);
891 break;
892 case 129: /* UTS/hostname namespace. */
893 minijail_namespace_uts(j);
894 if (optarg)
895 minijail_namespace_set_hostname(j, optarg);
896 break;
897 case 130: /* Logging. */
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400898 if (!strcmp(optarg, "auto")) {
899 log_to_stderr = -1;
900 } else if (!strcmp(optarg, "syslog")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500901 log_to_stderr = 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400902 } else if (!strcmp(optarg, "stderr")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500903 log_to_stderr = 1;
904 } else {
905 fprintf(stderr, "--logger must be 'syslog' or "
906 "'stderr'.\n");
907 exit(1);
908 }
909 break;
910 case 131: /* Profile */
911 use_profile(j, optarg, &pivot_root, chroot, &tmp_size);
912 break;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700913 case 132: /* PRELOADPATH */
914 *preload_path = optarg;
915 break;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700916 case 133: /* seccomp-bpf binary. */
917 if (seccomp != -1 && seccomp != 3) {
918 fprintf(stderr,
919 "Do not use -s, -S, or "
920 "--seccomp-bpf-binary together.\n");
921 exit(1);
922 }
923 seccomp = 3;
924 minijail_use_seccomp_filter(j);
925 filter_path = optarg;
926 use_seccomp_filter_binary = 1;
927 break;
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100928 case 134:
929 suppl_group_add(&suppl_gids_count, &suppl_gids,
930 optarg);
931 break;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500932 default:
933 usage(argv[0]);
934 exit(opt == 'h' ? 0 : 1);
935 }
936 }
937
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400938 if (log_to_stderr == -1) {
939 /* Autodetect default logging output. */
Mike Frysinger056955c2019-09-24 16:07:05 -0400940 log_to_stderr = isatty(STDIN_FILENO) ? 1 : 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400941 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500942 if (log_to_stderr) {
943 init_logging(LOG_TO_FD, STDERR_FILENO, LOG_INFO);
944 /*
945 * When logging to stderr, ensure the FD survives the jailing.
946 */
947 if (0 !=
948 minijail_preserve_fd(j, STDERR_FILENO, STDERR_FILENO)) {
949 fprintf(stderr, "Could not preserve stderr.\n");
950 exit(1);
951 }
952 }
953
954 /* Set up uid/gid mapping. */
955 if (set_uidmap || set_gidmap) {
956 set_ugid_mapping(j, set_uidmap, uid, uidmap, set_gidmap, gid,
957 gidmap);
958 }
959
960 /* Can only set ambient caps when using regular caps. */
961 if (ambient_caps && !caps) {
962 fprintf(stderr, "Can't set ambient capabilities (--ambient) "
963 "without actually using capabilities (-c).\n");
964 exit(1);
965 }
966
967 /* Set up signal handlers in minijail unless asked not to. */
968 if (forward)
969 minijail_forward_signals(j);
970
971 /*
972 * Only allow bind mounts when entering a chroot, using pivot_root, or
973 * a new mount namespace.
974 */
975 if (binding && !(chroot || pivot_root || mount_ns)) {
976 fprintf(stderr, "Bind mounts require a chroot, pivot_root, or "
977 " new mount namespace.\n");
978 exit(1);
979 }
980
981 /*
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400982 * / is only remounted when entering a new mount namespace, so unless
983 * that's set there is no need for the -K/-K<mode> flags.
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500984 */
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400985 if (change_remount && !mount_ns) {
986 fprintf(stderr, "No need to use -K (skip remounting '/') or "
987 "-K<mode> (remount '/' as <mode>)\n"
988 "without -v (new mount namespace).\n"
989 "Do you need to add '-v' explicitly?\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500990 exit(1);
991 }
992
993 /*
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100994 * Proceed in setting the supplementary gids specified on the
995 * cmdline options.
996 */
997 if (suppl_gids_count) {
998 minijail_set_supplementary_gids(j, suppl_gids_count,
999 suppl_gids);
1000 free(suppl_gids);
1001 }
1002
1003 /*
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001004 * We parse seccomp filters here to make sure we've collected all
1005 * cmdline options.
1006 */
1007 if (use_seccomp_filter) {
1008 minijail_parse_seccomp_filters(j, filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001009 } else if (use_seccomp_filter_binary) {
1010 struct sock_fprog filter;
1011 read_seccomp_filter(filter_path, &filter);
1012 minijail_set_seccomp_filters(j, &filter);
1013 free((void *)filter.filter);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001014 }
1015
1016 /* Mount a tmpfs under /tmp and set its size. */
1017 if (tmp_size)
1018 minijail_mount_tmp_size(j, tmp_size);
1019
1020 /*
1021 * There should be at least one additional unparsed argument: the
1022 * executable name.
1023 */
1024 if (argc == optind) {
1025 usage(argv[0]);
1026 exit(1);
1027 }
1028
1029 if (*elftype == ELFERROR) {
1030 /*
1031 * -T was not specified.
1032 * Get the path to the program adjusted for changing root.
1033 */
1034 char *program_path =
1035 minijail_get_original_path(j, argv[optind]);
1036
1037 /* Check that we can access the target program. */
1038 if (access(program_path, X_OK)) {
1039 fprintf(stderr,
1040 "Target program '%s' is not accessible.\n",
1041 argv[optind]);
1042 exit(1);
1043 }
1044
1045 /* Check if target is statically or dynamically linked. */
1046 *elftype = get_elf_linkage(program_path);
1047 free(program_path);
1048 }
1049
1050 /*
1051 * Setting capabilities need either a dynamically-linked binary, or the
1052 * use of ambient capabilities for them to be able to survive an
1053 * execve(2).
1054 */
1055 if (caps && *elftype == ELFSTATIC && !ambient_caps) {
1056 fprintf(stderr, "Can't run statically-linked binaries with "
1057 "capabilities (-c) without also setting "
1058 "ambient capabilities. Try passing "
1059 "--ambient.\n");
1060 exit(1);
1061 }
1062
1063 return optind;
1064}