blob: d4ed38b01e3d7e3e87e4b74cf2bf8da893c712a7 [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
Zi Lin44461c72021-11-16 18:37:27 +000024#include "config_parser.h"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050025#include "elfparse.h"
26#include "minijail0_cli.h"
27#include "system.h"
28#include "util.h"
29
30#define IDMAP_LEN 32U
31#define DEFAULT_TMP_SIZE (64 * 1024 * 1024)
32
Mike Frysinger1036cd82020-08-28 00:15:59 -040033/*
34 * A malloc() that aborts on failure. We only implement this in the CLI as
35 * the library should return ENOMEM errors when allocations fail.
36 */
37static void *xmalloc(size_t size)
38{
39 void *ret = malloc(size);
40 if (!ret) {
41 perror("malloc() failed");
42 exit(1);
43 }
44 return ret;
45}
46
47static char *xstrdup(const char *s)
48{
49 char *ret = strdup(s);
50 if (!ret) {
51 perror("strdup() failed");
52 exit(1);
53 }
54 return ret;
55}
56
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050057static void set_user(struct minijail *j, const char *arg, uid_t *out_uid,
58 gid_t *out_gid)
59{
60 char *end = NULL;
Stéphane Lesimpled4911dd2022-01-10 10:52:30 +010061 uid_t uid = strtoul(arg, &end, 10);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050062 if (!*end && *arg) {
63 *out_uid = uid;
64 minijail_change_uid(j, uid);
65 return;
66 }
67
Mattias Nissler160d58f2020-02-25 11:01:30 +010068 int ret = lookup_user(arg, out_uid, out_gid);
69 if (ret) {
70 fprintf(stderr, "Bad user '%s': %s\n", arg, strerror(-ret));
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050071 exit(1);
72 }
73
Mattias Nissler160d58f2020-02-25 11:01:30 +010074 ret = minijail_change_user(j, arg);
75 if (ret) {
76 fprintf(stderr, "minijail_change_user('%s') failed: %s\n", arg,
77 strerror(-ret));
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050078 exit(1);
79 }
80}
81
82static void set_group(struct minijail *j, const char *arg, gid_t *out_gid)
83{
84 char *end = NULL;
Stéphane Lesimpled4911dd2022-01-10 10:52:30 +010085 gid_t gid = strtoul(arg, &end, 10);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050086 if (!*end && *arg) {
87 *out_gid = gid;
88 minijail_change_gid(j, gid);
89 return;
90 }
91
Mattias Nissler160d58f2020-02-25 11:01:30 +010092 int ret = lookup_group(arg, out_gid);
93 if (ret) {
94 fprintf(stderr, "Bad group '%s': %s\n", arg, strerror(-ret));
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050095 exit(1);
96 }
97
Mattias Nissler160d58f2020-02-25 11:01:30 +010098 minijail_change_gid(j, *out_gid);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050099}
100
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100101/*
102 * Helper function used by --add-suppl-group (possibly more than once),
103 * to build the supplementary gids array.
104 */
105static void suppl_group_add(size_t *suppl_gids_count, gid_t **suppl_gids,
106 char *arg) {
107 char *end = NULL;
Stéphane Lesimpled4911dd2022-01-10 10:52:30 +0100108 gid_t gid = strtoul(arg, &end, 10);
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. */
Mattias Nissler160d58f2020-02-25 11:01:30 +0100112 } else if ((ret = lookup_group(arg, &gid))) {
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100113 /*
114 * A group name has been specified,
115 * but doesn't exist: we bail out.
116 */
Mattias Nissler160d58f2020-02-25 11:01:30 +0100117 fprintf(stderr, "Bad group '%s': %s\n", arg, strerror(-ret));
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100118 exit(1);
119 }
120
121 /*
122 * From here, gid is guaranteed to be set and valid,
123 * we add it to our supplementary gids array.
124 */
125 *suppl_gids = realloc(*suppl_gids,
126 sizeof(gid_t) * ++(*suppl_gids_count));
127 if (!suppl_gids) {
128 fprintf(stderr, "failed to allocate memory.\n");
129 exit(1);
130 }
131
132 (*suppl_gids)[*suppl_gids_count - 1] = gid;
133}
134
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500135static void skip_securebits(struct minijail *j, const char *arg)
136{
137 uint64_t securebits_skip_mask;
138 char *end = NULL;
139 securebits_skip_mask = strtoull(arg, &end, 16);
140 if (*end) {
141 fprintf(stderr, "Invalid securebit mask: '%s'\n", arg);
142 exit(1);
143 }
144 minijail_skip_setting_securebits(j, securebits_skip_mask);
145}
146
147static void use_caps(struct minijail *j, const char *arg)
148{
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700149 uint64_t caps = 0;
150 cap_t parsed_caps = cap_from_text(arg);
151
152 if (parsed_caps != NULL) {
153 unsigned int i;
154 const uint64_t one = 1;
155 cap_flag_value_t cap_value;
156 unsigned int last_valid_cap = get_last_valid_cap();
157
158 for (i = 0; i <= last_valid_cap; ++i) {
159 if (cap_get_flag(parsed_caps, i, CAP_EFFECTIVE,
160 &cap_value)) {
Luis Hector Chavez677900f2018-09-24 09:13:26 -0700161 if (errno == EINVAL) {
162 /*
163 * Some versions of libcap reject any
164 * capabilities they were not compiled
165 * with by returning EINVAL.
166 */
167 continue;
168 }
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700169 fprintf(stderr,
170 "Could not get the value of "
171 "the %d-th capability: %m\n",
172 i);
173 exit(1);
174 }
175 if (cap_value == CAP_SET)
176 caps |= (one << i);
177 }
178 cap_free(parsed_caps);
179 } else {
180 char *end = NULL;
181 caps = strtoull(arg, &end, 16);
182 if (*end) {
183 fprintf(stderr, "Invalid cap set: '%s'\n", arg);
184 exit(1);
185 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500186 }
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700187
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500188 minijail_use_caps(j, caps);
189}
190
191static void add_binding(struct minijail *j, char *arg)
192{
193 char *src = tokenize(&arg, ",");
194 char *dest = tokenize(&arg, ",");
195 char *flags = tokenize(&arg, ",");
196 if (!src || src[0] == '\0' || arg != NULL) {
197 fprintf(stderr, "Bad binding: %s %s\n", src, dest);
198 exit(1);
199 }
200 if (dest == NULL || dest[0] == '\0')
201 dest = src;
David Coles87ec5cd2019-06-13 17:20:10 -0700202 int writable;
203 if (flags == NULL || flags[0] == '\0' || !strcmp(flags, "0"))
204 writable = 0;
205 else if (!strcmp(flags, "1"))
206 writable = 1;
207 else {
208 fprintf(stderr, "Bad value for <writable>: %s\n", flags);
209 exit(1);
210 }
211 if (minijail_bind(j, src, dest, writable)) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500212 fprintf(stderr, "minijail_bind failed.\n");
213 exit(1);
214 }
215}
216
217static void add_rlimit(struct minijail *j, char *arg)
218{
219 char *type = tokenize(&arg, ",");
220 char *cur = tokenize(&arg, ",");
221 char *max = tokenize(&arg, ",");
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800222 char *end;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500223 if (!type || type[0] == '\0' || !cur || cur[0] == '\0' ||
224 !max || max[0] == '\0' || arg != NULL) {
225 fprintf(stderr, "Bad rlimit '%s'.\n", arg);
226 exit(1);
227 }
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800228 rlim_t cur_rlim;
229 rlim_t max_rlim;
230 if (!strcmp(cur, "unlimited")) {
231 cur_rlim = RLIM_INFINITY;
232 } else {
233 end = NULL;
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400234 cur_rlim = strtoul(cur, &end, 0);
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800235 if (*end) {
236 fprintf(stderr, "Bad soft limit: '%s'.\n", cur);
237 exit(1);
238 }
239 }
240 if (!strcmp(max, "unlimited")) {
241 max_rlim = RLIM_INFINITY;
242 } else {
243 end = NULL;
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400244 max_rlim = strtoul(max, &end, 0);
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800245 if (*end) {
246 fprintf(stderr, "Bad hard limit: '%s'.\n", max);
247 exit(1);
248 }
249 }
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400250
251 end = NULL;
252 int resource = parse_single_constant(type, &end);
253 if (type == end) {
254 fprintf(stderr, "Bad rlimit: '%s'.\n", type);
255 exit(1);
256 }
257
258 if (minijail_rlimit(j, resource, cur_rlim, max_rlim)) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500259 fprintf(stderr, "minijail_rlimit '%s,%s,%s' failed.\n", type,
260 cur, max);
261 exit(1);
262 }
263}
264
265static void add_mount(struct minijail *j, char *arg)
266{
267 char *src = tokenize(&arg, ",");
268 char *dest = tokenize(&arg, ",");
269 char *type = tokenize(&arg, ",");
270 char *flags = tokenize(&arg, ",");
271 char *data = tokenize(&arg, ",");
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400272 char *end;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500273 if (!src || src[0] == '\0' || !dest || dest[0] == '\0' ||
Mike Frysinger4f3e09f2018-01-24 18:01:16 -0500274 !type || type[0] == '\0') {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500275 fprintf(stderr, "Bad mount: %s %s %s\n", src, dest, type);
276 exit(1);
277 }
Mike Frysinger4f3e09f2018-01-24 18:01:16 -0500278
279 /*
280 * Fun edge case: the data option itself is comma delimited. If there
281 * were no more options, then arg would be set to NULL. But if we had
282 * more pending, it'll be pointing to the next token. Back up and undo
283 * the null byte so it'll be merged back.
284 * An example:
285 * none,/tmp,tmpfs,0xe,mode=0755,uid=10,gid=10
286 * The tokenize calls above will turn this memory into:
287 * none\0/tmp\0tmpfs\00xe\0mode=0755\0uid=10,gid=10
288 * With data pointing at mode=0755 and arg pointing at uid=10,gid=10.
289 */
290 if (arg != NULL)
291 arg[-1] = ',';
292
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400293 unsigned long mountflags;
294 if (flags == NULL || flags[0] == '\0') {
295 mountflags = 0;
296 } else {
297 end = NULL;
298 mountflags = parse_constant(flags, &end);
299 if (flags == end) {
300 fprintf(stderr, "Bad mount flags: %s\n", flags);
301 exit(1);
302 }
303 }
304
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500305 if (minijail_mount_with_data(j, src, dest, type,
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400306 mountflags, data)) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500307 fprintf(stderr, "minijail_mount failed.\n");
308 exit(1);
309 }
310}
311
312static char *build_idmap(id_t id, id_t lowerid)
313{
314 int ret;
Mike Frysinger1036cd82020-08-28 00:15:59 -0400315 char *idmap = xmalloc(IDMAP_LEN);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500316 ret = snprintf(idmap, IDMAP_LEN, "%d %d 1", id, lowerid);
317 if (ret < 0 || (size_t)ret >= IDMAP_LEN) {
318 free(idmap);
319 fprintf(stderr, "Could not build id map.\n");
320 exit(1);
321 }
322 return idmap;
323}
324
325static int has_cap_setgid(void)
326{
327 cap_t caps;
328 cap_flag_value_t cap_value;
329
330 if (!CAP_IS_SUPPORTED(CAP_SETGID))
331 return 0;
332
333 caps = cap_get_proc();
334 if (!caps) {
335 fprintf(stderr, "Could not get process' capabilities: %m\n");
336 exit(1);
337 }
338
339 if (cap_get_flag(caps, CAP_SETGID, CAP_EFFECTIVE, &cap_value)) {
340 fprintf(stderr, "Could not get the value of CAP_SETGID: %m\n");
341 exit(1);
342 }
343
344 if (cap_free(caps)) {
345 fprintf(stderr, "Could not free capabilities: %m\n");
346 exit(1);
347 }
348
349 return cap_value == CAP_SET;
350}
351
352static void set_ugid_mapping(struct minijail *j, int set_uidmap, uid_t uid,
353 char *uidmap, int set_gidmap, gid_t gid,
354 char *gidmap)
355{
356 if (set_uidmap) {
357 minijail_namespace_user(j);
358 minijail_namespace_pids(j);
359
360 if (!uidmap) {
361 /*
362 * If no map is passed, map the current uid to the
363 * chosen uid in the target namespace (or root, if none
364 * was chosen).
365 */
366 uidmap = build_idmap(uid, getuid());
367 }
368 if (0 != minijail_uidmap(j, uidmap)) {
369 fprintf(stderr, "Could not set uid map.\n");
370 exit(1);
371 }
372 free(uidmap);
373 }
374 if (set_gidmap) {
375 minijail_namespace_user(j);
376 minijail_namespace_pids(j);
377
378 if (!gidmap) {
379 /*
380 * If no map is passed, map the current gid to the
381 * chosen gid in the target namespace.
382 */
383 gidmap = build_idmap(gid, getgid());
384 }
385 if (!has_cap_setgid()) {
386 /*
387 * This means that we are not running as root,
388 * so we also have to disable setgroups(2) to
389 * be able to set the gid map.
390 * See
391 * http://man7.org/linux/man-pages/man7/user_namespaces.7.html
392 */
393 minijail_namespace_user_disable_setgroups(j);
394 }
395 if (0 != minijail_gidmap(j, gidmap)) {
396 fprintf(stderr, "Could not set gid map.\n");
397 exit(1);
398 }
399 free(gidmap);
400 }
401}
402
403static void use_chroot(struct minijail *j, const char *path, int *chroot,
404 int pivot_root)
405{
406 if (pivot_root) {
407 fprintf(stderr, "Could not set chroot because "
408 "'-P' was specified.\n");
409 exit(1);
410 }
411 if (minijail_enter_chroot(j, path)) {
412 fprintf(stderr, "Could not set chroot.\n");
413 exit(1);
414 }
415 *chroot = 1;
416}
417
418static void use_pivot_root(struct minijail *j, const char *path,
419 int *pivot_root, int chroot)
420{
421 if (chroot) {
422 fprintf(stderr, "Could not set pivot_root because "
423 "'-C' was specified.\n");
424 exit(1);
425 }
426 if (minijail_enter_pivot_root(j, path)) {
427 fprintf(stderr, "Could not set pivot_root.\n");
428 exit(1);
429 }
430 minijail_namespace_vfs(j);
431 *pivot_root = 1;
432}
433
434static void use_profile(struct minijail *j, const char *profile,
435 int *pivot_root, int chroot, size_t *tmp_size)
436{
Mike Frysinger4d2a81e2018-01-22 16:43:33 -0500437 /* Note: New profiles should be added in minijail0_cli_unittest.cc. */
438
Mike Frysingercc5917c2020-02-03 12:34:14 -0500439 if (!strcmp(profile, "minimalistic-mountns") ||
440 !strcmp(profile, "minimalistic-mountns-nodev")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500441 minijail_namespace_vfs(j);
442 if (minijail_bind(j, "/", "/", 0)) {
Jorge Lucangeli Obes7394b902019-03-14 12:43:26 -0400443 fprintf(stderr, "minijail_bind(/) failed.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500444 exit(1);
445 }
446 if (minijail_bind(j, "/proc", "/proc", 0)) {
Jorge Lucangeli Obes7394b902019-03-14 12:43:26 -0400447 fprintf(stderr, "minijail_bind(/proc) failed.\n");
448 exit(1);
449 }
Mike Frysingercc5917c2020-02-03 12:34:14 -0500450 if (!strcmp(profile, "minimalistic-mountns")) {
451 if (minijail_bind(j, "/dev/log", "/dev/log", 0)) {
Zi Lin44461c72021-11-16 18:37:27 +0000452 fprintf(stderr,
453 "minijail_bind(/dev/log) failed.\n");
Mike Frysingercc5917c2020-02-03 12:34:14 -0500454 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
Nicole Anderson-Auafa54be2021-03-09 23:00:49 +0000470static void set_remount_mode(struct minijail *j, const char *mode)
Mike Frysinger785b1c32018-02-23 15:47:24 -0500471{
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 }
Nicole Anderson-Auafa54be2021-03-09 23:00:49 +0000485 minijail_remount_mode(j, msmode);
Mike Frysinger785b1c32018-02-23 15:47:24 -0500486}
487
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700488static void read_seccomp_filter(const char *filter_path,
489 struct sock_fprog *filter)
490{
Mike Frysingerdebdf5d2021-06-21 09:52:06 -0400491 attribute_cleanup_fp FILE *f = fopen(filter_path, "re");
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700492 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) {
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700498 fprintf(stderr, "failed to get file size of %s: %m",
499 filter_path);
500 exit(1);
501 }
502 if (filter_size % sizeof(struct sock_filter) != 0) {
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700503 fprintf(stderr,
504 "filter size (%" PRId64
505 ") of %s is not a multiple of %zu: %m",
506 filter_size, filter_path, sizeof(struct sock_filter));
507 exit(1);
508 }
509 rewind(f);
510
511 filter->len = filter_size / sizeof(struct sock_filter);
Mike Frysinger1036cd82020-08-28 00:15:59 -0400512 filter->filter = xmalloc(filter_size);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700513 if (fread(filter->filter, sizeof(struct sock_filter), filter->len, f) !=
514 filter->len) {
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700515 fprintf(stderr, "failed read %s: %m", filter_path);
516 exit(1);
517 }
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700518}
519
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500520static void usage(const char *progn)
521{
522 size_t i;
523 /* clang-format off */
524 printf("Usage: %s [-dGhHiIKlLnNprRstUvyYz]\n"
525 " [-a <table>]\n"
Mike Frysingerc9b07992020-09-01 05:04:48 -0400526 " [-b <src>[,[dest][,<writeable>]]] [-k <src>,<dest>,<type>[,<flags>[,<data>]]]\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500527 " [-c <caps>] [-C <dir>] [-P <dir>] [-e[file]] [-f <file>] [-g <group>]\n"
528 " [-m[<uid> <loweruid> <count>]*] [-M[<gid> <lowergid> <count>]*] [--profile <name>]\n"
529 " [-R <type,cur,max>] [-S <file>] [-t[size]] [-T <type>] [-u <user>] [-V <file>]\n"
530 " <program> [args...]\n"
531 " -a <table>: Use alternate syscall table <table>.\n"
Zi Lin44461c72021-11-16 18:37:27 +0000532 " --bind-mount <...>, Bind <src> to <dest> in chroot.\n"
533 " -b <...>: Multiple instances allowed.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500534 " -B <mask>: Skip setting securebits in <mask> when restricting capabilities (-c).\n"
535 " By default, SECURE_NOROOT, SECURE_NO_SETUID_FIXUP, and \n"
536 " SECURE_KEEP_CAPS (together with their respective locks) are set.\n"
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -0400537 " There are eight securebits in total.\n"
Zi Lin44461c72021-11-16 18:37:27 +0000538 " --mount <...>,Mount <src> at <dest> in chroot.\n"
539 " -k <...>:<flags> and <data> can be specified as in mount(2).\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500540 " Multiple instances allowed.\n"
541 " -c <caps>: Restrict caps to <caps>.\n"
542 " -C <dir>: chroot(2) to <dir>.\n"
543 " Not compatible with -P.\n"
544 " -P <dir>: pivot_root(2) to <dir> (implies -v).\n"
545 " Not compatible with -C.\n"
546 " --mount-dev, Create a new /dev with a minimal set of device nodes (implies -v).\n"
547 " -d: See the minijail0(1) man page for the exact set.\n"
548 " -e[file]: Enter new network namespace, or existing one if |file| is provided.\n"
549 " -f <file>: Write the pid of the jailed process to <file>.\n"
550 " -g <group>: Change gid to <group>.\n"
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100551 " -G: Inherit supplementary groups from new uid.\n"
552 " Not compatible with -y or --add-suppl-group.\n"
553 " -y: Keep original uid's supplementary groups.\n"
554 " Not compatible with -G or --add-suppl-group.\n"
555 " --add-suppl-group <g>:Add <g> to the proccess' supplementary groups,\n"
556 " can be specified multiple times to add several groups.\n"
557 " Not compatible with -y or -G.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500558 " -h: Help (this message).\n"
559 " -H: Seccomp filter help message.\n"
Luis Hector Chavez9dd13fd2018-04-19 20:14:47 -0700560 " -i: Exit immediately after fork(2). The jailed process will run\n"
561 " in the background.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500562 " -I: Run <program> as init (pid 1) inside a new pid namespace (implies -p).\n"
Mike Frysinger785b1c32018-02-23 15:47:24 -0500563 " -K: Do not change share mode of any existing mounts.\n"
564 " -K<mode>: Mark all existing mounts as <mode> instead of MS_PRIVATE.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500565 " -l: Enter new IPC namespace.\n"
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400566 " -L: Report blocked syscalls when using seccomp filter.\n"
567 " If the kernel does not support SECCOMP_RET_LOG,\n"
568 " forces the following syscalls to be allowed:\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500569 " ", progn);
570 /* clang-format on */
571 for (i = 0; i < log_syscalls_len; i++)
572 printf("%s ", log_syscalls[i]);
573
574 /* clang-format off */
575 printf("\n"
576 " -m[map]: Set the uid map of a user namespace (implies -pU).\n"
577 " Same arguments as newuidmap(1), multiple mappings should be separated by ',' (comma).\n"
578 " With no mapping, map the current uid to root inside the user namespace.\n"
579 " Not compatible with -b without the 'writable' option.\n"
580 " -M[map]: Set the gid map of a user namespace (implies -pU).\n"
581 " Same arguments as newgidmap(1), multiple mappings should be separated by ',' (comma).\n"
582 " With no mapping, map the current gid to root inside the user namespace.\n"
583 " Not compatible with -b without the 'writable' option.\n"
584 " -n: Set no_new_privs.\n"
585 " -N: Enter a new cgroup namespace.\n"
586 " -p: Enter new pid namespace (implies -vr).\n"
587 " -r: Remount /proc read-only (implies -v).\n"
588 " -R: Set rlimits, can be specified multiple times.\n"
589 " -s: Use seccomp mode 1 (not the same as -S).\n"
590 " -S <file>: Set seccomp filter using <file>.\n"
591 " E.g., '-S /usr/share/filters/<prog>.$(uname -m)'.\n"
592 " Requires -n when not running as root.\n"
593 " -t[size]: Mount tmpfs at /tmp (implies -v).\n"
594 " Optional argument specifies size (default \"64M\").\n"
595 " -T <type>: Assume <program> is a <type> ELF binary; <type> can be 'static' or 'dynamic'.\n"
596 " This will avoid accessing <program> binary before execve(2).\n"
597 " Type 'static' will avoid preload hooking.\n"
598 " -u <user>: Change uid to <user>.\n"
599 " -U: Enter new user namespace (implies -p).\n"
600 " -v: Enter new mount namespace.\n"
601 " -V <file>: Enter specified mount namespace.\n"
602 " -w: Create and join a new anonymous session keyring.\n"
603 " -Y: Synchronize seccomp filters across thread group.\n"
604 " -z: Don't forward signals to jailed process.\n"
605 " --ambient: Raise ambient capabilities. Requires -c.\n"
606 " --uts[=name]: Enter a new UTS namespace (and set hostname).\n"
607 " --logging=<s>:Use <s> as the logging system.\n"
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400608 " <s> must be 'auto' (default), 'syslog', or 'stderr'.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700609 " --profile <p>:Configure minijail0 to run with the <p> sandboxing profile,\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500610 " which is a convenient way to express multiple flags\n"
611 " that are typically used together.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700612 " See the minijail0(1) man page for the full list.\n"
613 " --preload-library=<f>:Overrides the path to \"" PRELOADPATH "\".\n"
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700614 " This is only really useful for local testing.\n"
615 " --seccomp-bpf-binary=<f>:Set a pre-compiled seccomp filter using <f>.\n"
616 " E.g., '-S /usr/share/filters/<prog>.$(uname -m).bpf'.\n"
617 " Requires -n when not running as root.\n"
618 " The user is responsible for ensuring that the binary\n"
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100619 " was compiled for the correct architecture / kernel version.\n"
Jorge Lucangeli Obes031568c2021-01-06 10:10:38 -0500620 " --allow-speculative-execution:Allow speculative execution and disable\n"
Zi Lin44461c72021-11-16 18:37:27 +0000621 " mitigations for speculative execution attacks.\n"
622 " --config <file>:Load the Minijail configuration file <file>.\n"
623 " If used, must be specified ahead of other options.\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
Zi Lin44461c72021-11-16 18:37:27 +0000638/*
639 * Return the next unconsumed option char/value parsed from
640 * |*conf_entry_list|. |optarg| is updated to point to an argument from
641 * the entry value. If all options have been consumed, |*conf_entry_list|
642 * will be freed and -1 will be returned.
643 */
644static int getopt_from_conf(const struct option *longopts,
645 struct config_entry_list **conf_entry_list,
646 size_t *conf_index)
647{
648 int opt = -1;
649 /* If we've consumed all the options in the this config, reset it. */
650 if (*conf_index >= (*conf_entry_list)->num_entries) {
651 free_config_entry_list(*conf_entry_list);
652 *conf_entry_list = NULL;
653 *conf_index = 0;
654 return opt;
655 }
656
657 struct config_entry *entry = &(*conf_entry_list)->entries[*conf_index];
658 /* Look up a matching long option. */
659 size_t i = 0;
660 const struct option *curr_opt;
661 for (curr_opt = &longopts[0]; curr_opt->name != NULL;
662 curr_opt = &longopts[++i])
663 if (strcmp(entry->key, curr_opt->name) == 0)
664 break;
665 if (curr_opt->name == NULL) {
666 fprintf(
667 stderr,
668 "Unable to recognize '%s' as Minijail conf entry key, "
669 "please refer to minijail0(5) for syntax and examples.\n",
670 entry->key);
671 exit(1);
672 }
673 opt = curr_opt->val;
674 optarg = (char *)entry->value;
675 (*conf_index)++;
676 return opt;
677}
678
679/*
680 * Similar to getopt(3), return the next option char/value as it
681 * parses through the CLI argument list. Config entries in
682 * |*conf_entry_list| will be parsed with precendences over cli options.
683 * Same as getopt(3), |optarg| is pointing to the option argument.
684 */
685static int getopt_conf_or_cli(int argc, char *const argv[],
686 struct config_entry_list **conf_entry_list,
687 size_t *conf_index)
688{
689 int opt = -1;
690 static const char optstring[] =
691 "+u:g:sS:c:C:P:b:B:V:f:m::M::k:a:e::R:T:vrGhHinNplLt::IUK::wyYzd";
692 /* clang-format off */
693 static const struct option long_options[] = {
694 {"help", no_argument, 0, 'h'},
695 {"mount-dev", no_argument, 0, 'd'},
696 {"ambient", no_argument, 0, 128},
697 {"uts", optional_argument, 0, 129},
698 {"logging", required_argument, 0, 130},
699 {"profile", required_argument, 0, 131},
700 {"preload-library", required_argument, 0, 132},
701 {"seccomp-bpf-binary", required_argument, 0, 133},
702 {"add-suppl-group", required_argument, 0, 134},
703 {"allow-speculative-execution", no_argument, 0, 135},
704 {"config", required_argument, 0, 136},
705 {"mount", required_argument, 0, 'k'},
706 {"bind-mount", required_argument, 0, 'b'},
707 {0, 0, 0, 0},
708 };
709 /* clang-format on */
710 if (*conf_entry_list != NULL)
711 opt =
712 getopt_from_conf(long_options, conf_entry_list, conf_index);
713 if (opt == -1)
714 opt = getopt_long(argc, argv, optstring, long_options, NULL);
715 return opt;
716}
717
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700718int parse_args(struct minijail *j, int argc, char *const argv[],
719 int *exit_immediately, ElfType *elftype,
720 const char **preload_path)
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500721{
Zi Lin44461c72021-11-16 18:37:27 +0000722 enum seccomp_type { None, Strict, Filter, BpfBinaryFilter };
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000723 enum seccomp_type seccomp = None;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500724 int opt;
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000725 int use_seccomp_filter = 0;
726 int use_seccomp_filter_binary = 0;
727 int use_seccomp_log = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500728 int forward = 1;
729 int binding = 0;
730 int chroot = 0, pivot_root = 0;
Nicole Anderson-Auafa54be2021-03-09 23:00:49 +0000731 int mount_ns = 0, change_remount = 0;
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -0500732 const char *remount_mode = NULL;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500733 int inherit_suppl_gids = 0, keep_suppl_gids = 0;
734 int caps = 0, ambient_caps = 0;
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800735 bool use_uid = false, use_gid = false;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500736 uid_t uid = 0;
737 gid_t gid = 0;
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100738 gid_t *suppl_gids = NULL;
739 size_t suppl_gids_count = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500740 char *uidmap = NULL, *gidmap = NULL;
741 int set_uidmap = 0, set_gidmap = 0;
742 size_t tmp_size = 0;
743 const char *filter_path = NULL;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400744 int log_to_stderr = -1;
Zi Lin44461c72021-11-16 18:37:27 +0000745 struct config_entry_list *conf_entry_list = NULL;
746 size_t conf_index = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500747
Zi Lin44461c72021-11-16 18:37:27 +0000748 while ((opt = getopt_conf_or_cli(argc, argv, &conf_entry_list,
749 &conf_index)) != -1) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500750 switch (opt) {
751 case 'u':
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800752 if (use_uid) {
753 fprintf(stderr,
754 "-u provided multiple times.\n");
755 exit(1);
756 }
757 use_uid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500758 set_user(j, optarg, &uid, &gid);
759 break;
760 case 'g':
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800761 if (use_gid) {
762 fprintf(stderr,
763 "-g provided multiple times.\n");
764 exit(1);
765 }
766 use_gid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500767 set_group(j, optarg, &gid);
768 break;
769 case 'n':
770 minijail_no_new_privs(j);
771 break;
772 case 's':
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000773 if (seccomp != None && seccomp != Strict) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500774 fprintf(stderr,
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700775 "Do not use -s, -S, or "
776 "--seccomp-bpf-binary together.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500777 exit(1);
778 }
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000779 seccomp = Strict;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500780 minijail_use_seccomp(j);
781 break;
782 case 'S':
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000783 if (seccomp != None && seccomp != Filter) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500784 fprintf(stderr,
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700785 "Do not use -s, -S, or "
786 "--seccomp-bpf-binary together.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500787 exit(1);
788 }
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000789 seccomp = Filter;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500790 minijail_use_seccomp_filter(j);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700791 filter_path = optarg;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500792 use_seccomp_filter = 1;
793 break;
794 case 'l':
795 minijail_namespace_ipc(j);
796 break;
797 case 'L':
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000798 if (seccomp == BpfBinaryFilter) {
Zi Lin44461c72021-11-16 18:37:27 +0000799 fprintf(stderr, "-L does not work with "
800 "--seccomp-bpf-binary.\n");
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000801 exit(1);
802 }
803 use_seccomp_log = 1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500804 minijail_log_seccomp_filter_failures(j);
805 break;
806 case 'b':
807 add_binding(j, optarg);
808 binding = 1;
809 break;
810 case 'B':
811 skip_securebits(j, optarg);
812 break;
813 case 'c':
814 caps = 1;
815 use_caps(j, optarg);
816 break;
817 case 'C':
818 use_chroot(j, optarg, &chroot, pivot_root);
819 break;
820 case 'k':
821 add_mount(j, optarg);
822 break;
823 case 'K':
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -0500824 remount_mode = optarg;
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400825 change_remount = 1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500826 break;
827 case 'P':
828 use_pivot_root(j, optarg, &pivot_root, chroot);
829 break;
830 case 'f':
831 if (0 != minijail_write_pid_file(j, optarg)) {
832 fprintf(stderr,
833 "Could not prepare pid file path.\n");
834 exit(1);
835 }
836 break;
837 case 't':
838 minijail_namespace_vfs(j);
839 if (!tmp_size) {
840 /*
841 * Avoid clobbering |tmp_size| if it was already
842 * set.
843 */
844 tmp_size = DEFAULT_TMP_SIZE;
845 }
846 if (optarg != NULL &&
847 0 != parse_size(&tmp_size, optarg)) {
848 fprintf(stderr, "Invalid /tmp tmpfs size.\n");
849 exit(1);
850 }
851 break;
852 case 'v':
853 minijail_namespace_vfs(j);
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -0500854 /*
855 * Set the default mount propagation in the command-line
856 * tool to MS_SLAVE.
857 *
858 * When executing the sandboxed program in a new mount
859 * namespace the Minijail library will by default
860 * remount all mounts with the MS_PRIVATE flag. While
861 * this is an appropriate, safe default for the library,
862 * MS_PRIVATE can be problematic: unmount events will
863 * not propagate into mountpoints marked as MS_PRIVATE.
864 * This means that if a mount is unmounted in the root
865 * mount namespace, it will not be unmounted in the
866 * non-root mount namespace.
867 * This in turn can be problematic because activity in
868 * the non-root mount namespace can now directly
869 * influence the root mount namespace (e.g. preventing
870 * re-mounts of said mount), which would be a privilege
871 * inversion.
872 *
873 * Setting the default in the command-line to MS_SLAVE
874 * will still prevent mounts from leaking out of the
875 * non-root mount namespace but avoid these
876 * privilege-inversion issues.
877 * For cases where mounts should not flow *into* the
878 * namespace either, the user can pass -Kprivate.
879 * Note that mounts are marked as MS_PRIVATE by default
880 * by the kernel, so unless the init process (like
881 * systemd) or something else marks them as shared, this
882 * won't do anything.
883 */
884 minijail_remount_mode(j, MS_SLAVE);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500885 mount_ns = 1;
886 break;
887 case 'V':
888 minijail_namespace_enter_vfs(j, optarg);
889 break;
890 case 'r':
891 minijail_remount_proc_readonly(j);
892 break;
893 case 'G':
894 if (keep_suppl_gids) {
895 fprintf(stderr,
896 "-y and -G are not compatible.\n");
897 exit(1);
898 }
899 minijail_inherit_usergroups(j);
900 inherit_suppl_gids = 1;
901 break;
902 case 'y':
903 if (inherit_suppl_gids) {
904 fprintf(stderr,
905 "-y and -G are not compatible.\n");
906 exit(1);
907 }
908 minijail_keep_supplementary_gids(j);
909 keep_suppl_gids = 1;
910 break;
911 case 'N':
912 minijail_namespace_cgroups(j);
913 break;
914 case 'p':
915 minijail_namespace_pids(j);
916 break;
917 case 'e':
918 if (optarg)
919 minijail_namespace_enter_net(j, optarg);
920 else
921 minijail_namespace_net(j);
922 break;
923 case 'i':
924 *exit_immediately = 1;
925 break;
926 case 'H':
927 seccomp_filter_usage(argv[0]);
928 exit(0);
929 case 'I':
930 minijail_namespace_pids(j);
931 minijail_run_as_init(j);
932 break;
933 case 'U':
934 minijail_namespace_user(j);
935 minijail_namespace_pids(j);
936 break;
937 case 'm':
938 set_uidmap = 1;
939 if (uidmap) {
940 free(uidmap);
941 uidmap = NULL;
942 }
943 if (optarg)
Mike Frysinger1036cd82020-08-28 00:15:59 -0400944 uidmap = xstrdup(optarg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500945 break;
946 case 'M':
947 set_gidmap = 1;
948 if (gidmap) {
949 free(gidmap);
950 gidmap = NULL;
951 }
952 if (optarg)
Mike Frysinger1036cd82020-08-28 00:15:59 -0400953 gidmap = xstrdup(optarg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500954 break;
955 case 'a':
956 if (0 != minijail_use_alt_syscall(j, optarg)) {
957 fprintf(stderr,
958 "Could not set alt-syscall table.\n");
959 exit(1);
960 }
961 break;
962 case 'R':
963 add_rlimit(j, optarg);
964 break;
965 case 'T':
966 if (!strcmp(optarg, "static"))
967 *elftype = ELFSTATIC;
968 else if (!strcmp(optarg, "dynamic"))
969 *elftype = ELFDYNAMIC;
970 else {
971 fprintf(stderr, "ELF type must be 'static' or "
972 "'dynamic'.\n");
973 exit(1);
974 }
975 break;
976 case 'w':
977 minijail_new_session_keyring(j);
978 break;
979 case 'Y':
980 minijail_set_seccomp_filter_tsync(j);
981 break;
982 case 'z':
983 forward = 0;
984 break;
985 case 'd':
986 minijail_namespace_vfs(j);
987 minijail_mount_dev(j);
988 break;
989 /* Long options. */
990 case 128: /* Ambient caps. */
991 ambient_caps = 1;
992 minijail_set_ambient_caps(j);
993 break;
994 case 129: /* UTS/hostname namespace. */
995 minijail_namespace_uts(j);
996 if (optarg)
997 minijail_namespace_set_hostname(j, optarg);
998 break;
999 case 130: /* Logging. */
Mike Frysinger3e6a12c2019-09-24 12:50:55 -04001000 if (!strcmp(optarg, "auto")) {
1001 log_to_stderr = -1;
1002 } else if (!strcmp(optarg, "syslog")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001003 log_to_stderr = 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -04001004 } else if (!strcmp(optarg, "stderr")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001005 log_to_stderr = 1;
1006 } else {
1007 fprintf(stderr, "--logger must be 'syslog' or "
1008 "'stderr'.\n");
1009 exit(1);
1010 }
1011 break;
1012 case 131: /* Profile */
1013 use_profile(j, optarg, &pivot_root, chroot, &tmp_size);
1014 break;
Luis Hector Chavez9acba452018-10-11 10:13:25 -07001015 case 132: /* PRELOADPATH */
1016 *preload_path = optarg;
1017 break;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001018 case 133: /* seccomp-bpf binary. */
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +00001019 if (seccomp != None && seccomp != BpfBinaryFilter) {
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001020 fprintf(stderr,
1021 "Do not use -s, -S, or "
1022 "--seccomp-bpf-binary together.\n");
1023 exit(1);
1024 }
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +00001025 if (use_seccomp_log == 1) {
1026 fprintf(stderr,
1027 "-L does not work with --seccomp-bpf-binary.\n");
1028 exit(1);
1029 }
1030 seccomp = BpfBinaryFilter;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001031 minijail_use_seccomp_filter(j);
1032 filter_path = optarg;
1033 use_seccomp_filter_binary = 1;
1034 break;
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +01001035 case 134:
1036 suppl_group_add(&suppl_gids_count, &suppl_gids,
1037 optarg);
1038 break;
Anand K Mistry31adc6c2020-11-26 11:39:46 +11001039 case 135:
1040 minijail_set_seccomp_filter_allow_speculation(j);
1041 break;
Zi Lin44461c72021-11-16 18:37:27 +00001042 case 136: {
1043 if (conf_entry_list != NULL) {
1044 fprintf(stderr,
1045 "Nested config file specification is "
1046 "not allowed.\n");
1047 exit(1);
1048 }
1049 conf_entry_list = new_config_entry_list();
1050 conf_index = 0;
1051 attribute_cleanup_fp FILE *config_file =
1052 fopen(optarg, "re");
1053 if (!config_file) {
1054 fprintf(stderr, "failed to open %s: %m",
1055 optarg);
1056 exit(1);
1057 }
1058 if (!parse_config_file(config_file, conf_entry_list)) {
1059 fprintf(
1060 stderr,
1061 "Unable to parse %s as Minijail conf file, "
1062 "please refer to minijail0(5) for syntax "
1063 "and examples.\n",
1064 optarg);
1065 exit(1);
1066 }
1067 break;
1068 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001069 default:
1070 usage(argv[0]);
1071 exit(opt == 'h' ? 0 : 1);
1072 }
1073 }
1074
Mike Frysinger3e6a12c2019-09-24 12:50:55 -04001075 if (log_to_stderr == -1) {
1076 /* Autodetect default logging output. */
Mike Frysinger056955c2019-09-24 16:07:05 -04001077 log_to_stderr = isatty(STDIN_FILENO) ? 1 : 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -04001078 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001079 if (log_to_stderr) {
1080 init_logging(LOG_TO_FD, STDERR_FILENO, LOG_INFO);
1081 /*
1082 * When logging to stderr, ensure the FD survives the jailing.
1083 */
1084 if (0 !=
1085 minijail_preserve_fd(j, STDERR_FILENO, STDERR_FILENO)) {
1086 fprintf(stderr, "Could not preserve stderr.\n");
1087 exit(1);
1088 }
1089 }
1090
1091 /* Set up uid/gid mapping. */
1092 if (set_uidmap || set_gidmap) {
1093 set_ugid_mapping(j, set_uidmap, uid, uidmap, set_gidmap, gid,
1094 gidmap);
1095 }
1096
1097 /* Can only set ambient caps when using regular caps. */
1098 if (ambient_caps && !caps) {
1099 fprintf(stderr, "Can't set ambient capabilities (--ambient) "
1100 "without actually using capabilities (-c).\n");
1101 exit(1);
1102 }
1103
1104 /* Set up signal handlers in minijail unless asked not to. */
1105 if (forward)
1106 minijail_forward_signals(j);
1107
1108 /*
1109 * Only allow bind mounts when entering a chroot, using pivot_root, or
1110 * a new mount namespace.
1111 */
1112 if (binding && !(chroot || pivot_root || mount_ns)) {
1113 fprintf(stderr, "Bind mounts require a chroot, pivot_root, or "
1114 " new mount namespace.\n");
1115 exit(1);
1116 }
1117
1118 /*
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001119 * / is only remounted when entering a new mount namespace, so unless
1120 * that's set there is no need for the -K/-K<mode> flags.
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001121 */
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001122 if (change_remount && !mount_ns) {
1123 fprintf(stderr, "No need to use -K (skip remounting '/') or "
1124 "-K<mode> (remount '/' as <mode>)\n"
1125 "without -v (new mount namespace).\n"
1126 "Do you need to add '-v' explicitly?\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001127 exit(1);
1128 }
1129
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -05001130 /* Configure the remount flag here to avoid having -v override it. */
1131 if (change_remount) {
1132 if (remount_mode != NULL) {
1133 set_remount_mode(j, remount_mode);
1134 } else {
1135 minijail_skip_remount_private(j);
1136 }
1137 }
1138
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001139 /*
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +01001140 * Proceed in setting the supplementary gids specified on the
1141 * cmdline options.
1142 */
1143 if (suppl_gids_count) {
1144 minijail_set_supplementary_gids(j, suppl_gids_count,
1145 suppl_gids);
1146 free(suppl_gids);
1147 }
1148
1149 /*
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001150 * We parse seccomp filters here to make sure we've collected all
1151 * cmdline options.
1152 */
1153 if (use_seccomp_filter) {
1154 minijail_parse_seccomp_filters(j, filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001155 } else if (use_seccomp_filter_binary) {
1156 struct sock_fprog filter;
1157 read_seccomp_filter(filter_path, &filter);
1158 minijail_set_seccomp_filters(j, &filter);
1159 free((void *)filter.filter);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001160 }
1161
1162 /* Mount a tmpfs under /tmp and set its size. */
1163 if (tmp_size)
1164 minijail_mount_tmp_size(j, tmp_size);
1165
1166 /*
1167 * There should be at least one additional unparsed argument: the
1168 * executable name.
1169 */
1170 if (argc == optind) {
1171 usage(argv[0]);
1172 exit(1);
1173 }
1174
1175 if (*elftype == ELFERROR) {
1176 /*
1177 * -T was not specified.
1178 * Get the path to the program adjusted for changing root.
1179 */
1180 char *program_path =
1181 minijail_get_original_path(j, argv[optind]);
1182
1183 /* Check that we can access the target program. */
1184 if (access(program_path, X_OK)) {
1185 fprintf(stderr,
1186 "Target program '%s' is not accessible.\n",
1187 argv[optind]);
1188 exit(1);
1189 }
1190
1191 /* Check if target is statically or dynamically linked. */
1192 *elftype = get_elf_linkage(program_path);
1193 free(program_path);
1194 }
1195
1196 /*
1197 * Setting capabilities need either a dynamically-linked binary, or the
1198 * use of ambient capabilities for them to be able to survive an
1199 * execve(2).
1200 */
1201 if (caps && *elftype == ELFSTATIC && !ambient_caps) {
1202 fprintf(stderr, "Can't run statically-linked binaries with "
1203 "capabilities (-c) without also setting "
1204 "ambient capabilities. Try passing "
1205 "--ambient.\n");
1206 exit(1);
1207 }
1208
1209 return optind;
1210}