blob: e07fc564ce461de4d9c99512845775be923a560f [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
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"
532 " -b <...>: Bind <src> to <dest> in chroot.\n"
533 " Multiple instances allowed.\n"
534 " -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"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500538 " -k <...>: Mount <src> at <dest> in chroot.\n"
539 " <flags> and <data> can be specified as in mount(2).\n"
540 " 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"
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100621 " mitigations for speculative execution attacks.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500622 /* clang-format on */
623}
624
625static void seccomp_filter_usage(const char *progn)
626{
627 const struct syscall_entry *entry = syscall_table;
628 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
629 "System call names supported:\n",
630 progn);
631 for (; entry->name && entry->nr >= 0; ++entry)
632 printf(" %s [%d]\n", entry->name, entry->nr);
633 printf("\nSee minijail0(5) for example policies.\n");
634}
635
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700636int parse_args(struct minijail *j, int argc, char *const argv[],
637 int *exit_immediately, ElfType *elftype,
638 const char **preload_path)
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500639{
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000640 enum seccomp_type{None, Strict, Filter, BpfBinaryFilter};
641 enum seccomp_type seccomp = None;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500642 int opt;
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000643 int use_seccomp_filter = 0;
644 int use_seccomp_filter_binary = 0;
645 int use_seccomp_log = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500646 int forward = 1;
647 int binding = 0;
648 int chroot = 0, pivot_root = 0;
Nicole Anderson-Auafa54be2021-03-09 23:00:49 +0000649 int mount_ns = 0, change_remount = 0;
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -0500650 const char *remount_mode = NULL;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500651 int inherit_suppl_gids = 0, keep_suppl_gids = 0;
652 int caps = 0, ambient_caps = 0;
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800653 bool use_uid = false, use_gid = false;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500654 uid_t uid = 0;
655 gid_t gid = 0;
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100656 gid_t *suppl_gids = NULL;
657 size_t suppl_gids_count = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500658 char *uidmap = NULL, *gidmap = NULL;
659 int set_uidmap = 0, set_gidmap = 0;
660 size_t tmp_size = 0;
661 const char *filter_path = NULL;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400662 int log_to_stderr = -1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500663
664 const char *optstring =
Mike Frysinger785b1c32018-02-23 15:47:24 -0500665 "+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 -0500666 /* clang-format off */
667 const struct option long_options[] = {
668 {"help", no_argument, 0, 'h'},
669 {"mount-dev", no_argument, 0, 'd'},
670 {"ambient", no_argument, 0, 128},
671 {"uts", optional_argument, 0, 129},
672 {"logging", required_argument, 0, 130},
673 {"profile", required_argument, 0, 131},
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700674 {"preload-library", required_argument, 0, 132},
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700675 {"seccomp-bpf-binary", required_argument, 0, 133},
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100676 {"add-suppl-group", required_argument, 0, 134},
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100677 {"allow-speculative-execution", no_argument, 0, 135},
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500678 {0, 0, 0, 0},
679 };
680 /* clang-format on */
681
682 while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) !=
683 -1) {
684 switch (opt) {
685 case 'u':
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800686 if (use_uid) {
687 fprintf(stderr,
688 "-u provided multiple times.\n");
689 exit(1);
690 }
691 use_uid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500692 set_user(j, optarg, &uid, &gid);
693 break;
694 case 'g':
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800695 if (use_gid) {
696 fprintf(stderr,
697 "-g provided multiple times.\n");
698 exit(1);
699 }
700 use_gid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500701 set_group(j, optarg, &gid);
702 break;
703 case 'n':
704 minijail_no_new_privs(j);
705 break;
706 case 's':
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000707 if (seccomp != None && seccomp != Strict) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500708 fprintf(stderr,
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700709 "Do not use -s, -S, or "
710 "--seccomp-bpf-binary together.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500711 exit(1);
712 }
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000713 seccomp = Strict;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500714 minijail_use_seccomp(j);
715 break;
716 case 'S':
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000717 if (seccomp != None && seccomp != Filter) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500718 fprintf(stderr,
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700719 "Do not use -s, -S, or "
720 "--seccomp-bpf-binary together.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500721 exit(1);
722 }
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000723 seccomp = Filter;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500724 minijail_use_seccomp_filter(j);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700725 filter_path = optarg;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500726 use_seccomp_filter = 1;
727 break;
728 case 'l':
729 minijail_namespace_ipc(j);
730 break;
731 case 'L':
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000732 if (seccomp == BpfBinaryFilter) {
733 fprintf(stderr,
734 "-L does not work with "
735 "--seccomp-bpf-binary.\n");
736 exit(1);
737 }
738 use_seccomp_log = 1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500739 minijail_log_seccomp_filter_failures(j);
740 break;
741 case 'b':
742 add_binding(j, optarg);
743 binding = 1;
744 break;
745 case 'B':
746 skip_securebits(j, optarg);
747 break;
748 case 'c':
749 caps = 1;
750 use_caps(j, optarg);
751 break;
752 case 'C':
753 use_chroot(j, optarg, &chroot, pivot_root);
754 break;
755 case 'k':
756 add_mount(j, optarg);
757 break;
758 case 'K':
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -0500759 remount_mode = optarg;
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400760 change_remount = 1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500761 break;
762 case 'P':
763 use_pivot_root(j, optarg, &pivot_root, chroot);
764 break;
765 case 'f':
766 if (0 != minijail_write_pid_file(j, optarg)) {
767 fprintf(stderr,
768 "Could not prepare pid file path.\n");
769 exit(1);
770 }
771 break;
772 case 't':
773 minijail_namespace_vfs(j);
774 if (!tmp_size) {
775 /*
776 * Avoid clobbering |tmp_size| if it was already
777 * set.
778 */
779 tmp_size = DEFAULT_TMP_SIZE;
780 }
781 if (optarg != NULL &&
782 0 != parse_size(&tmp_size, optarg)) {
783 fprintf(stderr, "Invalid /tmp tmpfs size.\n");
784 exit(1);
785 }
786 break;
787 case 'v':
788 minijail_namespace_vfs(j);
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -0500789 /*
790 * Set the default mount propagation in the command-line
791 * tool to MS_SLAVE.
792 *
793 * When executing the sandboxed program in a new mount
794 * namespace the Minijail library will by default
795 * remount all mounts with the MS_PRIVATE flag. While
796 * this is an appropriate, safe default for the library,
797 * MS_PRIVATE can be problematic: unmount events will
798 * not propagate into mountpoints marked as MS_PRIVATE.
799 * This means that if a mount is unmounted in the root
800 * mount namespace, it will not be unmounted in the
801 * non-root mount namespace.
802 * This in turn can be problematic because activity in
803 * the non-root mount namespace can now directly
804 * influence the root mount namespace (e.g. preventing
805 * re-mounts of said mount), which would be a privilege
806 * inversion.
807 *
808 * Setting the default in the command-line to MS_SLAVE
809 * will still prevent mounts from leaking out of the
810 * non-root mount namespace but avoid these
811 * privilege-inversion issues.
812 * For cases where mounts should not flow *into* the
813 * namespace either, the user can pass -Kprivate.
814 * Note that mounts are marked as MS_PRIVATE by default
815 * by the kernel, so unless the init process (like
816 * systemd) or something else marks them as shared, this
817 * won't do anything.
818 */
819 minijail_remount_mode(j, MS_SLAVE);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500820 mount_ns = 1;
821 break;
822 case 'V':
823 minijail_namespace_enter_vfs(j, optarg);
824 break;
825 case 'r':
826 minijail_remount_proc_readonly(j);
827 break;
828 case 'G':
829 if (keep_suppl_gids) {
830 fprintf(stderr,
831 "-y and -G are not compatible.\n");
832 exit(1);
833 }
834 minijail_inherit_usergroups(j);
835 inherit_suppl_gids = 1;
836 break;
837 case 'y':
838 if (inherit_suppl_gids) {
839 fprintf(stderr,
840 "-y and -G are not compatible.\n");
841 exit(1);
842 }
843 minijail_keep_supplementary_gids(j);
844 keep_suppl_gids = 1;
845 break;
846 case 'N':
847 minijail_namespace_cgroups(j);
848 break;
849 case 'p':
850 minijail_namespace_pids(j);
851 break;
852 case 'e':
853 if (optarg)
854 minijail_namespace_enter_net(j, optarg);
855 else
856 minijail_namespace_net(j);
857 break;
858 case 'i':
859 *exit_immediately = 1;
860 break;
861 case 'H':
862 seccomp_filter_usage(argv[0]);
863 exit(0);
864 case 'I':
865 minijail_namespace_pids(j);
866 minijail_run_as_init(j);
867 break;
868 case 'U':
869 minijail_namespace_user(j);
870 minijail_namespace_pids(j);
871 break;
872 case 'm':
873 set_uidmap = 1;
874 if (uidmap) {
875 free(uidmap);
876 uidmap = NULL;
877 }
878 if (optarg)
Mike Frysinger1036cd82020-08-28 00:15:59 -0400879 uidmap = xstrdup(optarg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500880 break;
881 case 'M':
882 set_gidmap = 1;
883 if (gidmap) {
884 free(gidmap);
885 gidmap = NULL;
886 }
887 if (optarg)
Mike Frysinger1036cd82020-08-28 00:15:59 -0400888 gidmap = xstrdup(optarg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500889 break;
890 case 'a':
891 if (0 != minijail_use_alt_syscall(j, optarg)) {
892 fprintf(stderr,
893 "Could not set alt-syscall table.\n");
894 exit(1);
895 }
896 break;
897 case 'R':
898 add_rlimit(j, optarg);
899 break;
900 case 'T':
901 if (!strcmp(optarg, "static"))
902 *elftype = ELFSTATIC;
903 else if (!strcmp(optarg, "dynamic"))
904 *elftype = ELFDYNAMIC;
905 else {
906 fprintf(stderr, "ELF type must be 'static' or "
907 "'dynamic'.\n");
908 exit(1);
909 }
910 break;
911 case 'w':
912 minijail_new_session_keyring(j);
913 break;
914 case 'Y':
915 minijail_set_seccomp_filter_tsync(j);
916 break;
917 case 'z':
918 forward = 0;
919 break;
920 case 'd':
921 minijail_namespace_vfs(j);
922 minijail_mount_dev(j);
923 break;
924 /* Long options. */
925 case 128: /* Ambient caps. */
926 ambient_caps = 1;
927 minijail_set_ambient_caps(j);
928 break;
929 case 129: /* UTS/hostname namespace. */
930 minijail_namespace_uts(j);
931 if (optarg)
932 minijail_namespace_set_hostname(j, optarg);
933 break;
934 case 130: /* Logging. */
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400935 if (!strcmp(optarg, "auto")) {
936 log_to_stderr = -1;
937 } else if (!strcmp(optarg, "syslog")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500938 log_to_stderr = 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400939 } else if (!strcmp(optarg, "stderr")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500940 log_to_stderr = 1;
941 } else {
942 fprintf(stderr, "--logger must be 'syslog' or "
943 "'stderr'.\n");
944 exit(1);
945 }
946 break;
947 case 131: /* Profile */
948 use_profile(j, optarg, &pivot_root, chroot, &tmp_size);
949 break;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700950 case 132: /* PRELOADPATH */
951 *preload_path = optarg;
952 break;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700953 case 133: /* seccomp-bpf binary. */
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000954 if (seccomp != None && seccomp != BpfBinaryFilter) {
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700955 fprintf(stderr,
956 "Do not use -s, -S, or "
957 "--seccomp-bpf-binary together.\n");
958 exit(1);
959 }
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000960 if (use_seccomp_log == 1) {
961 fprintf(stderr,
962 "-L does not work with --seccomp-bpf-binary.\n");
963 exit(1);
964 }
965 seccomp = BpfBinaryFilter;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700966 minijail_use_seccomp_filter(j);
967 filter_path = optarg;
968 use_seccomp_filter_binary = 1;
969 break;
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100970 case 134:
971 suppl_group_add(&suppl_gids_count, &suppl_gids,
972 optarg);
973 break;
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100974 case 135:
975 minijail_set_seccomp_filter_allow_speculation(j);
976 break;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500977 default:
978 usage(argv[0]);
979 exit(opt == 'h' ? 0 : 1);
980 }
981 }
982
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400983 if (log_to_stderr == -1) {
984 /* Autodetect default logging output. */
Mike Frysinger056955c2019-09-24 16:07:05 -0400985 log_to_stderr = isatty(STDIN_FILENO) ? 1 : 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400986 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500987 if (log_to_stderr) {
988 init_logging(LOG_TO_FD, STDERR_FILENO, LOG_INFO);
989 /*
990 * When logging to stderr, ensure the FD survives the jailing.
991 */
992 if (0 !=
993 minijail_preserve_fd(j, STDERR_FILENO, STDERR_FILENO)) {
994 fprintf(stderr, "Could not preserve stderr.\n");
995 exit(1);
996 }
997 }
998
999 /* Set up uid/gid mapping. */
1000 if (set_uidmap || set_gidmap) {
1001 set_ugid_mapping(j, set_uidmap, uid, uidmap, set_gidmap, gid,
1002 gidmap);
1003 }
1004
1005 /* Can only set ambient caps when using regular caps. */
1006 if (ambient_caps && !caps) {
1007 fprintf(stderr, "Can't set ambient capabilities (--ambient) "
1008 "without actually using capabilities (-c).\n");
1009 exit(1);
1010 }
1011
1012 /* Set up signal handlers in minijail unless asked not to. */
1013 if (forward)
1014 minijail_forward_signals(j);
1015
1016 /*
1017 * Only allow bind mounts when entering a chroot, using pivot_root, or
1018 * a new mount namespace.
1019 */
1020 if (binding && !(chroot || pivot_root || mount_ns)) {
1021 fprintf(stderr, "Bind mounts require a chroot, pivot_root, or "
1022 " new mount namespace.\n");
1023 exit(1);
1024 }
1025
1026 /*
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001027 * / is only remounted when entering a new mount namespace, so unless
1028 * that's set there is no need for the -K/-K<mode> flags.
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001029 */
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001030 if (change_remount && !mount_ns) {
1031 fprintf(stderr, "No need to use -K (skip remounting '/') or "
1032 "-K<mode> (remount '/' as <mode>)\n"
1033 "without -v (new mount namespace).\n"
1034 "Do you need to add '-v' explicitly?\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001035 exit(1);
1036 }
1037
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -05001038 /* Configure the remount flag here to avoid having -v override it. */
1039 if (change_remount) {
1040 if (remount_mode != NULL) {
1041 set_remount_mode(j, remount_mode);
1042 } else {
1043 minijail_skip_remount_private(j);
1044 }
1045 }
1046
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001047 /*
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +01001048 * Proceed in setting the supplementary gids specified on the
1049 * cmdline options.
1050 */
1051 if (suppl_gids_count) {
1052 minijail_set_supplementary_gids(j, suppl_gids_count,
1053 suppl_gids);
1054 free(suppl_gids);
1055 }
1056
1057 /*
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001058 * We parse seccomp filters here to make sure we've collected all
1059 * cmdline options.
1060 */
1061 if (use_seccomp_filter) {
1062 minijail_parse_seccomp_filters(j, filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001063 } else if (use_seccomp_filter_binary) {
1064 struct sock_fprog filter;
1065 read_seccomp_filter(filter_path, &filter);
1066 minijail_set_seccomp_filters(j, &filter);
1067 free((void *)filter.filter);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001068 }
1069
1070 /* Mount a tmpfs under /tmp and set its size. */
1071 if (tmp_size)
1072 minijail_mount_tmp_size(j, tmp_size);
1073
1074 /*
1075 * There should be at least one additional unparsed argument: the
1076 * executable name.
1077 */
1078 if (argc == optind) {
1079 usage(argv[0]);
1080 exit(1);
1081 }
1082
1083 if (*elftype == ELFERROR) {
1084 /*
1085 * -T was not specified.
1086 * Get the path to the program adjusted for changing root.
1087 */
1088 char *program_path =
1089 minijail_get_original_path(j, argv[optind]);
1090
1091 /* Check that we can access the target program. */
1092 if (access(program_path, X_OK)) {
1093 fprintf(stderr,
1094 "Target program '%s' is not accessible.\n",
1095 argv[optind]);
1096 exit(1);
1097 }
1098
1099 /* Check if target is statically or dynamically linked. */
1100 *elftype = get_elf_linkage(program_path);
1101 free(program_path);
1102 }
1103
1104 /*
1105 * Setting capabilities need either a dynamically-linked binary, or the
1106 * use of ambient capabilities for them to be able to survive an
1107 * execve(2).
1108 */
1109 if (caps && *elftype == ELFSTATIC && !ambient_caps) {
1110 fprintf(stderr, "Can't run statically-linked binaries with "
1111 "capabilities (-c) without also setting "
1112 "ambient capabilities. Try passing "
1113 "--ambient.\n");
1114 exit(1);
1115 }
1116
1117 return optind;
1118}