blob: a43f8f21bdc933c5bb10e192d41f30de413e3413 [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>
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05007#include <err.h>
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05008#include <errno.h>
Zi Lina9e72262022-01-11 03:22:21 +00009#include <fcntl.h>
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050010#include <getopt.h>
Luis Hector Chavezc3e17722018-10-16 20:43:12 -070011#include <inttypes.h>
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -080012#include <stdbool.h>
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050013#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <sys/capability.h>
Mike Frysinger785b1c32018-02-23 15:47:24 -050017#include <sys/mount.h>
Zi Lina9e72262022-01-11 03:22:21 +000018#include <sys/stat.h>
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050019#include <sys/types.h>
Zi Lina9e72262022-01-11 03:22:21 +000020#include <sys/vfs.h>
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050021#include <unistd.h>
22
Luis Hector Chavezc3e17722018-10-16 20:43:12 -070023#include <linux/filter.h>
24
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050025#include "libminijail.h"
26#include "libsyscalls.h"
27
Zi Lin44461c72021-11-16 18:37:27 +000028#include "config_parser.h"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050029#include "elfparse.h"
30#include "minijail0_cli.h"
31#include "system.h"
32#include "util.h"
33
34#define IDMAP_LEN 32U
35#define DEFAULT_TMP_SIZE (64 * 1024 * 1024)
36
Mike Frysinger1036cd82020-08-28 00:15:59 -040037/*
38 * A malloc() that aborts on failure. We only implement this in the CLI as
39 * the library should return ENOMEM errors when allocations fail.
40 */
41static void *xmalloc(size_t size)
42{
43 void *ret = malloc(size);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -050044 if (!ret)
45 err(1, "malloc() failed");
Mike Frysinger1036cd82020-08-28 00:15:59 -040046 return ret;
47}
48
49static char *xstrdup(const char *s)
50{
51 char *ret = strdup(s);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -050052 if (!ret)
53 err(1, "strdup() failed");
Mike Frysinger1036cd82020-08-28 00:15:59 -040054 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) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -050070 errno = -ret;
71 err(1, "Bad user '%s'", arg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050072 }
73
Mattias Nissler160d58f2020-02-25 11:01:30 +010074 ret = minijail_change_user(j, arg);
75 if (ret) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -050076 errno = -ret;
77 err(1, "minijail_change_user('%s') failed", arg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050078 }
79}
80
81static void set_group(struct minijail *j, const char *arg, gid_t *out_gid)
82{
83 char *end = NULL;
Stéphane Lesimpled4911dd2022-01-10 10:52:30 +010084 gid_t gid = strtoul(arg, &end, 10);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050085 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) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -050093 errno = -ret;
94 err(1, "Bad group '%s'", arg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050095 }
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,
Zi Lina9e72262022-01-11 03:22:21 +0000105 char *arg)
106{
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100107 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 */
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500117 errno = -ret;
118 err(1, "Bad group '%s'", arg);
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100119 }
120
121 /*
122 * From here, gid is guaranteed to be set and valid,
123 * we add it to our supplementary gids array.
124 */
Zi Linfdc98d62022-01-19 22:10:29 +0000125 *suppl_gids =
126 realloc(*suppl_gids, sizeof(gid_t) * ++(*suppl_gids_count));
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500127 if (!suppl_gids)
128 err(1, "failed to allocate memory");
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100129
130 (*suppl_gids)[*suppl_gids_count - 1] = gid;
131}
132
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500133static void skip_securebits(struct minijail *j, const char *arg)
134{
135 uint64_t securebits_skip_mask;
136 char *end = NULL;
137 securebits_skip_mask = strtoull(arg, &end, 16);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500138 if (*end)
139 errx(1, "Invalid securebit mask: '%s'", arg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500140 minijail_skip_setting_securebits(j, securebits_skip_mask);
141}
142
143static void use_caps(struct minijail *j, const char *arg)
144{
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700145 uint64_t caps = 0;
146 cap_t parsed_caps = cap_from_text(arg);
147
148 if (parsed_caps != NULL) {
149 unsigned int i;
150 const uint64_t one = 1;
151 cap_flag_value_t cap_value;
152 unsigned int last_valid_cap = get_last_valid_cap();
153
154 for (i = 0; i <= last_valid_cap; ++i) {
155 if (cap_get_flag(parsed_caps, i, CAP_EFFECTIVE,
156 &cap_value)) {
Luis Hector Chavez677900f2018-09-24 09:13:26 -0700157 if (errno == EINVAL) {
158 /*
159 * Some versions of libcap reject any
160 * capabilities they were not compiled
161 * with by returning EINVAL.
162 */
163 continue;
164 }
Zi Linfdc98d62022-01-19 22:10:29 +0000165 err(1,
166 "Could not get the value of the %d-th "
Zi Lina9e72262022-01-11 03:22:21 +0000167 "capability",
168 i);
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700169 }
170 if (cap_value == CAP_SET)
171 caps |= (one << i);
172 }
173 cap_free(parsed_caps);
174 } else {
175 char *end = NULL;
176 caps = strtoull(arg, &end, 16);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500177 if (*end)
178 errx(1, "Invalid cap set: '%s'", arg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500179 }
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700180
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500181 minijail_use_caps(j, caps);
182}
183
184static void add_binding(struct minijail *j, char *arg)
185{
186 char *src = tokenize(&arg, ",");
187 char *dest = tokenize(&arg, ",");
188 char *flags = tokenize(&arg, ",");
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500189 if (!src || src[0] == '\0' || arg != NULL)
190 errx(1, "Bad binding: %s %s", src, dest);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500191 if (dest == NULL || dest[0] == '\0')
192 dest = src;
David Coles87ec5cd2019-06-13 17:20:10 -0700193 int writable;
194 if (flags == NULL || flags[0] == '\0' || !strcmp(flags, "0"))
195 writable = 0;
196 else if (!strcmp(flags, "1"))
197 writable = 1;
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500198 else
199 errx(1, "Bad value for <writable>: %s", flags);
200 if (minijail_bind(j, src, dest, writable))
201 errx(1, "minijail_bind failed");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500202}
203
204static void add_rlimit(struct minijail *j, char *arg)
205{
206 char *type = tokenize(&arg, ",");
207 char *cur = tokenize(&arg, ",");
208 char *max = tokenize(&arg, ",");
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800209 char *end;
Zi Linfdc98d62022-01-19 22:10:29 +0000210 if (!type || type[0] == '\0' || !cur || cur[0] == '\0' || !max ||
211 max[0] == '\0' || arg != NULL) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500212 errx(1, "Bad rlimit '%s'", arg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500213 }
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800214 rlim_t cur_rlim;
215 rlim_t max_rlim;
216 if (!strcmp(cur, "unlimited")) {
217 cur_rlim = RLIM_INFINITY;
218 } else {
219 end = NULL;
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400220 cur_rlim = strtoul(cur, &end, 0);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500221 if (*end)
222 errx(1, "Bad soft limit: '%s'", cur);
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800223 }
224 if (!strcmp(max, "unlimited")) {
225 max_rlim = RLIM_INFINITY;
226 } else {
227 end = NULL;
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400228 max_rlim = strtoul(max, &end, 0);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500229 if (*end)
230 errx(1, "Bad hard limit: '%s'", max);
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800231 }
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400232
233 end = NULL;
234 int resource = parse_single_constant(type, &end);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500235 if (type == end)
236 errx(1, "Bad rlimit: '%s'", type);
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400237
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500238 if (minijail_rlimit(j, resource, cur_rlim, max_rlim))
239 errx(1, "minijail_rlimit '%s,%s,%s' failed", type, cur, max);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500240}
241
242static void add_mount(struct minijail *j, char *arg)
243{
244 char *src = tokenize(&arg, ",");
245 char *dest = tokenize(&arg, ",");
246 char *type = tokenize(&arg, ",");
247 char *flags = tokenize(&arg, ",");
248 char *data = tokenize(&arg, ",");
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400249 char *end;
Zi Linfdc98d62022-01-19 22:10:29 +0000250 if (!src || src[0] == '\0' || !dest || dest[0] == '\0' || !type ||
251 type[0] == '\0') {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500252 errx(1, "Bad mount: %s %s %s", src, dest, type);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500253 }
Mike Frysinger4f3e09f2018-01-24 18:01:16 -0500254
255 /*
256 * Fun edge case: the data option itself is comma delimited. If there
257 * were no more options, then arg would be set to NULL. But if we had
258 * more pending, it'll be pointing to the next token. Back up and undo
259 * the null byte so it'll be merged back.
260 * An example:
261 * none,/tmp,tmpfs,0xe,mode=0755,uid=10,gid=10
262 * The tokenize calls above will turn this memory into:
263 * none\0/tmp\0tmpfs\00xe\0mode=0755\0uid=10,gid=10
264 * With data pointing at mode=0755 and arg pointing at uid=10,gid=10.
265 */
266 if (arg != NULL)
267 arg[-1] = ',';
268
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400269 unsigned long mountflags;
270 if (flags == NULL || flags[0] == '\0') {
271 mountflags = 0;
272 } else {
273 end = NULL;
274 mountflags = parse_constant(flags, &end);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500275 if (flags == end)
276 errx(1, "Bad mount flags: %s", flags);
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400277 }
278
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500279 if (minijail_mount_with_data(j, src, dest, type, mountflags, data))
280 errx(1, "minijail_mount failed");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500281}
282
283static char *build_idmap(id_t id, id_t lowerid)
284{
285 int ret;
Mike Frysinger1036cd82020-08-28 00:15:59 -0400286 char *idmap = xmalloc(IDMAP_LEN);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500287 ret = snprintf(idmap, IDMAP_LEN, "%d %d 1", id, lowerid);
288 if (ret < 0 || (size_t)ret >= IDMAP_LEN) {
289 free(idmap);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500290 errx(1, "Could not build id map");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500291 }
292 return idmap;
293}
294
295static int has_cap_setgid(void)
296{
297 cap_t caps;
298 cap_flag_value_t cap_value;
299
300 if (!CAP_IS_SUPPORTED(CAP_SETGID))
301 return 0;
302
303 caps = cap_get_proc();
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500304 if (!caps)
305 err(1, "Could not get process' capabilities");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500306
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500307 if (cap_get_flag(caps, CAP_SETGID, CAP_EFFECTIVE, &cap_value))
308 err(1, "Could not get the value of CAP_SETGID");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500309
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500310 if (cap_free(caps))
311 err(1, "Could not free capabilities");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500312
313 return cap_value == CAP_SET;
314}
315
316static void set_ugid_mapping(struct minijail *j, int set_uidmap, uid_t uid,
317 char *uidmap, int set_gidmap, gid_t gid,
318 char *gidmap)
319{
320 if (set_uidmap) {
321 minijail_namespace_user(j);
322 minijail_namespace_pids(j);
323
324 if (!uidmap) {
325 /*
326 * If no map is passed, map the current uid to the
327 * chosen uid in the target namespace (or root, if none
328 * was chosen).
329 */
330 uidmap = build_idmap(uid, getuid());
331 }
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500332 if (0 != minijail_uidmap(j, uidmap))
333 errx(1, "Could not set uid map");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500334 free(uidmap);
335 }
336 if (set_gidmap) {
337 minijail_namespace_user(j);
338 minijail_namespace_pids(j);
339
340 if (!gidmap) {
341 /*
342 * If no map is passed, map the current gid to the
343 * chosen gid in the target namespace.
344 */
345 gidmap = build_idmap(gid, getgid());
346 }
347 if (!has_cap_setgid()) {
348 /*
349 * This means that we are not running as root,
350 * so we also have to disable setgroups(2) to
351 * be able to set the gid map.
352 * See
353 * http://man7.org/linux/man-pages/man7/user_namespaces.7.html
354 */
355 minijail_namespace_user_disable_setgroups(j);
356 }
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500357 if (0 != minijail_gidmap(j, gidmap))
358 errx(1, "Could not set gid map");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500359 free(gidmap);
360 }
361}
362
363static void use_chroot(struct minijail *j, const char *path, int *chroot,
364 int pivot_root)
365{
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500366 if (pivot_root)
367 errx(1, "Could not set chroot because -P was specified");
368 if (minijail_enter_chroot(j, path))
369 errx(1, "Could not set chroot");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500370 *chroot = 1;
371}
372
373static void use_pivot_root(struct minijail *j, const char *path,
374 int *pivot_root, int chroot)
375{
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500376 if (chroot)
377 errx(1, "Could not set pivot_root because -C was specified");
378 if (minijail_enter_pivot_root(j, path))
379 errx(1, "Could not set pivot_root");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500380 minijail_namespace_vfs(j);
381 *pivot_root = 1;
382}
383
384static void use_profile(struct minijail *j, const char *profile,
385 int *pivot_root, int chroot, size_t *tmp_size)
386{
Mike Frysinger4d2a81e2018-01-22 16:43:33 -0500387 /* Note: New profiles should be added in minijail0_cli_unittest.cc. */
388
Mike Frysingercc5917c2020-02-03 12:34:14 -0500389 if (!strcmp(profile, "minimalistic-mountns") ||
390 !strcmp(profile, "minimalistic-mountns-nodev")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500391 minijail_namespace_vfs(j);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500392 if (minijail_bind(j, "/", "/", 0))
393 errx(1, "minijail_bind(/) failed");
394 if (minijail_bind(j, "/proc", "/proc", 0))
395 errx(1, "minijail_bind(/proc) failed");
Mike Frysingercc5917c2020-02-03 12:34:14 -0500396 if (!strcmp(profile, "minimalistic-mountns")) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500397 if (minijail_bind(j, "/dev/log", "/dev/log", 0))
398 errx(1, "minijail_bind(/dev/log) failed");
Mike Frysingercc5917c2020-02-03 12:34:14 -0500399 minijail_mount_dev(j);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500400 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500401 if (!*tmp_size) {
402 /* Avoid clobbering |tmp_size| if it was already set. */
403 *tmp_size = DEFAULT_TMP_SIZE;
404 }
405 minijail_remount_proc_readonly(j);
Allen Webbee876072019-02-21 10:56:21 -0800406 use_pivot_root(j, DEFAULT_PIVOT_ROOT, pivot_root, chroot);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500407 } else
408 errx(1, "Unrecognized profile name '%s'", profile);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500409}
410
Nicole Anderson-Auafa54be2021-03-09 23:00:49 +0000411static void set_remount_mode(struct minijail *j, const char *mode)
Mike Frysinger785b1c32018-02-23 15:47:24 -0500412{
413 unsigned long msmode;
414 if (!strcmp(mode, "shared"))
415 msmode = MS_SHARED;
416 else if (!strcmp(mode, "private"))
417 msmode = MS_PRIVATE;
418 else if (!strcmp(mode, "slave"))
419 msmode = MS_SLAVE;
420 else if (!strcmp(mode, "unbindable"))
421 msmode = MS_UNBINDABLE;
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500422 else
423 errx(1, "Unknown remount mode: '%s'", mode);
Nicole Anderson-Auafa54be2021-03-09 23:00:49 +0000424 minijail_remount_mode(j, msmode);
Mike Frysinger785b1c32018-02-23 15:47:24 -0500425}
426
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700427static void read_seccomp_filter(const char *filter_path,
428 struct sock_fprog *filter)
429{
Mike Frysingerdebdf5d2021-06-21 09:52:06 -0400430 attribute_cleanup_fp FILE *f = fopen(filter_path, "re");
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500431 if (!f)
432 err(1, "failed to open %s", filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700433 off_t filter_size = 0;
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500434 if (fseeko(f, 0, SEEK_END) == -1 || (filter_size = ftello(f)) == -1)
435 err(1, "failed to get file size of %s", filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700436 if (filter_size % sizeof(struct sock_filter) != 0) {
Zi Lina9e72262022-01-11 03:22:21 +0000437 errx(1,
438 "filter size (%" PRId64 ") of %s is not a multiple of"
439 " %zu",
440 filter_size, filter_path, sizeof(struct sock_filter));
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700441 }
442 rewind(f);
443
444 filter->len = filter_size / sizeof(struct sock_filter);
Mike Frysinger1036cd82020-08-28 00:15:59 -0400445 filter->filter = xmalloc(filter_size);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700446 if (fread(filter->filter, sizeof(struct sock_filter), filter->len, f) !=
447 filter->len) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500448 err(1, "failed read %s", filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700449 }
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700450}
451
Mike Frysinger4ffd7e52022-01-24 14:07:02 -0500452/*
453 * Long options use values starting at 0x100 so that they're out of range of
454 * bytes which is how command line options are processed. Practically speaking,
455 * we could get by with the (7-bit) ASCII range, but UTF-8 codepoints would be a
456 * bit confusing, and honestly there's no reason to "optimize" here.
457 *
458 * The long enum values are internal to this file and can freely change at any
459 * time without breaking anything. Don't worry about ordering.
460 */
461enum {
462 /* Everything after this point only have long options. */
463 LONG_OPTION_BASE = 0x100,
464 OPT_ADD_SUPPL_GROUP,
465 OPT_ALLOW_SPECULATIVE_EXECUTION,
466 OPT_AMBIENT,
467 OPT_CONFIG,
468 OPT_LOGGING,
469 OPT_PRELOAD_LIBRARY,
470 OPT_PROFILE,
471 OPT_SECCOMP_BPF_BINARY,
472 OPT_UTS,
473};
474
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500475static void usage(const char *progn)
476{
477 size_t i;
478 /* clang-format off */
479 printf("Usage: %s [-dGhHiIKlLnNprRstUvyYz]\n"
480 " [-a <table>]\n"
Mike Frysingerc9b07992020-09-01 05:04:48 -0400481 " [-b <src>[,[dest][,<writeable>]]] [-k <src>,<dest>,<type>[,<flags>[,<data>]]]\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500482 " [-c <caps>] [-C <dir>] [-P <dir>] [-e[file]] [-f <file>] [-g <group>]\n"
483 " [-m[<uid> <loweruid> <count>]*] [-M[<gid> <lowergid> <count>]*] [--profile <name>]\n"
484 " [-R <type,cur,max>] [-S <file>] [-t[size]] [-T <type>] [-u <user>] [-V <file>]\n"
485 " <program> [args...]\n"
486 " -a <table>: Use alternate syscall table <table>.\n"
Zi Lin44461c72021-11-16 18:37:27 +0000487 " --bind-mount <...>, Bind <src> to <dest> in chroot.\n"
488 " -b <...>: Multiple instances allowed.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500489 " -B <mask>: Skip setting securebits in <mask> when restricting capabilities (-c).\n"
490 " By default, SECURE_NOROOT, SECURE_NO_SETUID_FIXUP, and \n"
491 " SECURE_KEEP_CAPS (together with their respective locks) are set.\n"
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -0400492 " There are eight securebits in total.\n"
Zi Lin44461c72021-11-16 18:37:27 +0000493 " --mount <...>,Mount <src> at <dest> in chroot.\n"
494 " -k <...>:<flags> and <data> can be specified as in mount(2).\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500495 " Multiple instances allowed.\n"
496 " -c <caps>: Restrict caps to <caps>.\n"
497 " -C <dir>: chroot(2) to <dir>.\n"
498 " Not compatible with -P.\n"
499 " -P <dir>: pivot_root(2) to <dir> (implies -v).\n"
500 " Not compatible with -C.\n"
501 " --mount-dev, Create a new /dev with a minimal set of device nodes (implies -v).\n"
502 " -d: See the minijail0(1) man page for the exact set.\n"
503 " -e[file]: Enter new network namespace, or existing one if |file| is provided.\n"
504 " -f <file>: Write the pid of the jailed process to <file>.\n"
505 " -g <group>: Change gid to <group>.\n"
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100506 " -G: Inherit supplementary groups from new uid.\n"
507 " Not compatible with -y or --add-suppl-group.\n"
508 " -y: Keep original uid's supplementary groups.\n"
509 " Not compatible with -G or --add-suppl-group.\n"
510 " --add-suppl-group <g>:Add <g> to the proccess' supplementary groups,\n"
511 " can be specified multiple times to add several groups.\n"
512 " Not compatible with -y or -G.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500513 " -h: Help (this message).\n"
514 " -H: Seccomp filter help message.\n"
Luis Hector Chavez9dd13fd2018-04-19 20:14:47 -0700515 " -i: Exit immediately after fork(2). The jailed process will run\n"
516 " in the background.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500517 " -I: Run <program> as init (pid 1) inside a new pid namespace (implies -p).\n"
Mike Frysinger785b1c32018-02-23 15:47:24 -0500518 " -K: Do not change share mode of any existing mounts.\n"
519 " -K<mode>: Mark all existing mounts as <mode> instead of MS_PRIVATE.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500520 " -l: Enter new IPC namespace.\n"
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400521 " -L: Report blocked syscalls when using seccomp filter.\n"
522 " If the kernel does not support SECCOMP_RET_LOG,\n"
523 " forces the following syscalls to be allowed:\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500524 " ", progn);
525 /* clang-format on */
526 for (i = 0; i < log_syscalls_len; i++)
527 printf("%s ", log_syscalls[i]);
528
529 /* clang-format off */
530 printf("\n"
531 " -m[map]: Set the uid map of a user namespace (implies -pU).\n"
532 " Same arguments as newuidmap(1), multiple mappings should be separated by ',' (comma).\n"
533 " With no mapping, map the current uid to root inside the user namespace.\n"
534 " Not compatible with -b without the 'writable' option.\n"
535 " -M[map]: Set the gid map of a user namespace (implies -pU).\n"
536 " Same arguments as newgidmap(1), multiple mappings should be separated by ',' (comma).\n"
537 " With no mapping, map the current gid to root inside the user namespace.\n"
538 " Not compatible with -b without the 'writable' option.\n"
539 " -n: Set no_new_privs.\n"
540 " -N: Enter a new cgroup namespace.\n"
541 " -p: Enter new pid namespace (implies -vr).\n"
542 " -r: Remount /proc read-only (implies -v).\n"
543 " -R: Set rlimits, can be specified multiple times.\n"
544 " -s: Use seccomp mode 1 (not the same as -S).\n"
545 " -S <file>: Set seccomp filter using <file>.\n"
546 " E.g., '-S /usr/share/filters/<prog>.$(uname -m)'.\n"
547 " Requires -n when not running as root.\n"
548 " -t[size]: Mount tmpfs at /tmp (implies -v).\n"
549 " Optional argument specifies size (default \"64M\").\n"
550 " -T <type>: Assume <program> is a <type> ELF binary; <type> can be 'static' or 'dynamic'.\n"
551 " This will avoid accessing <program> binary before execve(2).\n"
552 " Type 'static' will avoid preload hooking.\n"
553 " -u <user>: Change uid to <user>.\n"
554 " -U: Enter new user namespace (implies -p).\n"
555 " -v: Enter new mount namespace.\n"
556 " -V <file>: Enter specified mount namespace.\n"
557 " -w: Create and join a new anonymous session keyring.\n"
558 " -Y: Synchronize seccomp filters across thread group.\n"
559 " -z: Don't forward signals to jailed process.\n"
560 " --ambient: Raise ambient capabilities. Requires -c.\n"
561 " --uts[=name]: Enter a new UTS namespace (and set hostname).\n"
562 " --logging=<s>:Use <s> as the logging system.\n"
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400563 " <s> must be 'auto' (default), 'syslog', or 'stderr'.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700564 " --profile <p>:Configure minijail0 to run with the <p> sandboxing profile,\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500565 " which is a convenient way to express multiple flags\n"
566 " that are typically used together.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700567 " See the minijail0(1) man page for the full list.\n"
568 " --preload-library=<f>:Overrides the path to \"" PRELOADPATH "\".\n"
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700569 " This is only really useful for local testing.\n"
570 " --seccomp-bpf-binary=<f>:Set a pre-compiled seccomp filter using <f>.\n"
571 " E.g., '-S /usr/share/filters/<prog>.$(uname -m).bpf'.\n"
572 " Requires -n when not running as root.\n"
573 " The user is responsible for ensuring that the binary\n"
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100574 " was compiled for the correct architecture / kernel version.\n"
Jorge Lucangeli Obes031568c2021-01-06 10:10:38 -0500575 " --allow-speculative-execution:Allow speculative execution and disable\n"
Zi Lin44461c72021-11-16 18:37:27 +0000576 " mitigations for speculative execution attacks.\n"
577 " --config <file>:Load the Minijail configuration file <file>.\n"
578 " If used, must be specified ahead of other options.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500579 /* clang-format on */
580}
581
582static void seccomp_filter_usage(const char *progn)
583{
584 const struct syscall_entry *entry = syscall_table;
585 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
586 "System call names supported:\n",
587 progn);
588 for (; entry->name && entry->nr >= 0; ++entry)
589 printf(" %s [%d]\n", entry->name, entry->nr);
590 printf("\nSee minijail0(5) for example policies.\n");
591}
592
Zi Lin44461c72021-11-16 18:37:27 +0000593/*
594 * Return the next unconsumed option char/value parsed from
595 * |*conf_entry_list|. |optarg| is updated to point to an argument from
596 * the entry value. If all options have been consumed, |*conf_entry_list|
597 * will be freed and -1 will be returned.
598 */
599static int getopt_from_conf(const struct option *longopts,
600 struct config_entry_list **conf_entry_list,
601 size_t *conf_index)
602{
603 int opt = -1;
604 /* If we've consumed all the options in the this config, reset it. */
605 if (*conf_index >= (*conf_entry_list)->num_entries) {
606 free_config_entry_list(*conf_entry_list);
607 *conf_entry_list = NULL;
608 *conf_index = 0;
609 return opt;
610 }
611
612 struct config_entry *entry = &(*conf_entry_list)->entries[*conf_index];
613 /* Look up a matching long option. */
614 size_t i = 0;
615 const struct option *curr_opt;
616 for (curr_opt = &longopts[0]; curr_opt->name != NULL;
617 curr_opt = &longopts[++i])
618 if (strcmp(entry->key, curr_opt->name) == 0)
619 break;
620 if (curr_opt->name == NULL) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500621 errx(1,
622 "Unable to recognize '%s' as Minijail conf entry key, "
Mike Frysingerdac801f2022-01-14 13:31:11 -0500623 "please refer to minijail0(5) for syntax and examples.",
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500624 entry->key);
Zi Lin44461c72021-11-16 18:37:27 +0000625 }
626 opt = curr_opt->val;
627 optarg = (char *)entry->value;
628 (*conf_index)++;
629 return opt;
630}
631
632/*
633 * Similar to getopt(3), return the next option char/value as it
634 * parses through the CLI argument list. Config entries in
635 * |*conf_entry_list| will be parsed with precendences over cli options.
636 * Same as getopt(3), |optarg| is pointing to the option argument.
637 */
638static int getopt_conf_or_cli(int argc, char *const argv[],
639 struct config_entry_list **conf_entry_list,
640 size_t *conf_index)
641{
642 int opt = -1;
643 static const char optstring[] =
644 "+u:g:sS:c:C:P:b:B:V:f:m::M::k:a:e::R:T:vrGhHinNplLt::IUK::wyYzd";
645 /* clang-format off */
646 static const struct option long_options[] = {
647 {"help", no_argument, 0, 'h'},
648 {"mount-dev", no_argument, 0, 'd'},
Mike Frysinger4ffd7e52022-01-24 14:07:02 -0500649 {"ambient", no_argument, 0, OPT_AMBIENT},
650 {"uts", optional_argument, 0, OPT_UTS},
651 {"logging", required_argument, 0, OPT_LOGGING},
652 {"profile", required_argument, 0, OPT_PROFILE},
653 {"preload-library", required_argument, 0, OPT_PRELOAD_LIBRARY},
654 {"seccomp-bpf-binary", required_argument, 0, OPT_SECCOMP_BPF_BINARY},
655 {"add-suppl-group", required_argument, 0, OPT_ADD_SUPPL_GROUP},
656 {"allow-speculative-execution", no_argument, 0,
657 OPT_ALLOW_SPECULATIVE_EXECUTION},
658 {"config", required_argument, 0, OPT_CONFIG},
Zi Lin44461c72021-11-16 18:37:27 +0000659 {"mount", required_argument, 0, 'k'},
660 {"bind-mount", required_argument, 0, 'b'},
661 {0, 0, 0, 0},
662 };
663 /* clang-format on */
664 if (*conf_entry_list != NULL)
665 opt =
666 getopt_from_conf(long_options, conf_entry_list, conf_index);
667 if (opt == -1)
668 opt = getopt_long(argc, argv, optstring, long_options, NULL);
669 return opt;
670}
671
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700672int parse_args(struct minijail *j, int argc, char *const argv[],
673 int *exit_immediately, ElfType *elftype,
674 const char **preload_path)
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500675{
Zi Lin44461c72021-11-16 18:37:27 +0000676 enum seccomp_type { None, Strict, Filter, BpfBinaryFilter };
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000677 enum seccomp_type seccomp = None;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500678 int opt;
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000679 int use_seccomp_filter = 0;
680 int use_seccomp_filter_binary = 0;
681 int use_seccomp_log = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500682 int forward = 1;
683 int binding = 0;
684 int chroot = 0, pivot_root = 0;
Nicole Anderson-Auafa54be2021-03-09 23:00:49 +0000685 int mount_ns = 0, change_remount = 0;
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -0500686 const char *remount_mode = NULL;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500687 int inherit_suppl_gids = 0, keep_suppl_gids = 0;
688 int caps = 0, ambient_caps = 0;
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800689 bool use_uid = false, use_gid = false;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500690 uid_t uid = 0;
691 gid_t gid = 0;
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100692 gid_t *suppl_gids = NULL;
693 size_t suppl_gids_count = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500694 char *uidmap = NULL, *gidmap = NULL;
695 int set_uidmap = 0, set_gidmap = 0;
696 size_t tmp_size = 0;
697 const char *filter_path = NULL;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400698 int log_to_stderr = -1;
Zi Lin44461c72021-11-16 18:37:27 +0000699 struct config_entry_list *conf_entry_list = NULL;
700 size_t conf_index = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500701
Zi Lin44461c72021-11-16 18:37:27 +0000702 while ((opt = getopt_conf_or_cli(argc, argv, &conf_entry_list,
703 &conf_index)) != -1) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500704 switch (opt) {
705 case 'u':
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500706 if (use_uid)
707 errx(1, "-u provided multiple times.");
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800708 use_uid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500709 set_user(j, optarg, &uid, &gid);
710 break;
711 case 'g':
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500712 if (use_gid)
713 errx(1, "-g provided multiple times.");
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800714 use_gid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500715 set_group(j, optarg, &gid);
716 break;
717 case 'n':
718 minijail_no_new_privs(j);
719 break;
720 case 's':
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000721 if (seccomp != None && seccomp != Strict) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500722 errx(1, "Do not use -s, -S, or "
723 "--seccomp-bpf-binary together");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500724 }
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000725 seccomp = Strict;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500726 minijail_use_seccomp(j);
727 break;
728 case 'S':
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000729 if (seccomp != None && seccomp != Filter) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500730 errx(1, "Do not use -s, -S, or "
731 "--seccomp-bpf-binary together");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500732 }
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000733 seccomp = Filter;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500734 minijail_use_seccomp_filter(j);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700735 filter_path = optarg;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500736 use_seccomp_filter = 1;
737 break;
738 case 'l':
739 minijail_namespace_ipc(j);
740 break;
741 case 'L':
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000742 if (seccomp == BpfBinaryFilter) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500743 errx(1, "-L does not work with "
744 "--seccomp-bpf-binary");
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000745 }
746 use_seccomp_log = 1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500747 minijail_log_seccomp_filter_failures(j);
748 break;
749 case 'b':
750 add_binding(j, optarg);
751 binding = 1;
752 break;
753 case 'B':
754 skip_securebits(j, optarg);
755 break;
756 case 'c':
757 caps = 1;
758 use_caps(j, optarg);
759 break;
760 case 'C':
761 use_chroot(j, optarg, &chroot, pivot_root);
762 break;
763 case 'k':
764 add_mount(j, optarg);
765 break;
766 case 'K':
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -0500767 remount_mode = optarg;
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400768 change_remount = 1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500769 break;
770 case 'P':
771 use_pivot_root(j, optarg, &pivot_root, chroot);
772 break;
773 case 'f':
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500774 if (0 != minijail_write_pid_file(j, optarg))
775 errx(1, "Could not prepare pid file path");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500776 break;
777 case 't':
778 minijail_namespace_vfs(j);
779 if (!tmp_size) {
780 /*
781 * Avoid clobbering |tmp_size| if it was already
782 * set.
783 */
784 tmp_size = DEFAULT_TMP_SIZE;
785 }
786 if (optarg != NULL &&
787 0 != parse_size(&tmp_size, optarg)) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500788 errx(1, "Invalid /tmp tmpfs size");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500789 }
790 break;
791 case 'v':
792 minijail_namespace_vfs(j);
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -0500793 /*
794 * Set the default mount propagation in the command-line
795 * tool to MS_SLAVE.
796 *
797 * When executing the sandboxed program in a new mount
798 * namespace the Minijail library will by default
799 * remount all mounts with the MS_PRIVATE flag. While
800 * this is an appropriate, safe default for the library,
801 * MS_PRIVATE can be problematic: unmount events will
802 * not propagate into mountpoints marked as MS_PRIVATE.
803 * This means that if a mount is unmounted in the root
804 * mount namespace, it will not be unmounted in the
805 * non-root mount namespace.
806 * This in turn can be problematic because activity in
807 * the non-root mount namespace can now directly
808 * influence the root mount namespace (e.g. preventing
809 * re-mounts of said mount), which would be a privilege
810 * inversion.
811 *
812 * Setting the default in the command-line to MS_SLAVE
813 * will still prevent mounts from leaking out of the
814 * non-root mount namespace but avoid these
815 * privilege-inversion issues.
816 * For cases where mounts should not flow *into* the
817 * namespace either, the user can pass -Kprivate.
818 * Note that mounts are marked as MS_PRIVATE by default
819 * by the kernel, so unless the init process (like
820 * systemd) or something else marks them as shared, this
821 * won't do anything.
822 */
823 minijail_remount_mode(j, MS_SLAVE);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500824 mount_ns = 1;
825 break;
826 case 'V':
827 minijail_namespace_enter_vfs(j, optarg);
828 break;
829 case 'r':
830 minijail_remount_proc_readonly(j);
831 break;
832 case 'G':
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500833 if (keep_suppl_gids)
834 errx(1, "-y and -G are not compatible");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500835 minijail_inherit_usergroups(j);
836 inherit_suppl_gids = 1;
837 break;
838 case 'y':
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500839 if (inherit_suppl_gids)
840 errx(1, "-y and -G are not compatible");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500841 minijail_keep_supplementary_gids(j);
842 keep_suppl_gids = 1;
843 break;
844 case 'N':
845 minijail_namespace_cgroups(j);
846 break;
847 case 'p':
848 minijail_namespace_pids(j);
849 break;
850 case 'e':
851 if (optarg)
852 minijail_namespace_enter_net(j, optarg);
853 else
854 minijail_namespace_net(j);
855 break;
856 case 'i':
857 *exit_immediately = 1;
858 break;
859 case 'H':
860 seccomp_filter_usage(argv[0]);
861 exit(0);
862 case 'I':
863 minijail_namespace_pids(j);
864 minijail_run_as_init(j);
865 break;
866 case 'U':
867 minijail_namespace_user(j);
868 minijail_namespace_pids(j);
869 break;
870 case 'm':
871 set_uidmap = 1;
872 if (uidmap) {
873 free(uidmap);
874 uidmap = NULL;
875 }
876 if (optarg)
Mike Frysinger1036cd82020-08-28 00:15:59 -0400877 uidmap = xstrdup(optarg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500878 break;
879 case 'M':
880 set_gidmap = 1;
881 if (gidmap) {
882 free(gidmap);
883 gidmap = NULL;
884 }
885 if (optarg)
Mike Frysinger1036cd82020-08-28 00:15:59 -0400886 gidmap = xstrdup(optarg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500887 break;
888 case 'a':
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500889 if (0 != minijail_use_alt_syscall(j, optarg))
890 errx(1, "Could not set alt-syscall table");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500891 break;
892 case 'R':
893 add_rlimit(j, optarg);
894 break;
895 case 'T':
896 if (!strcmp(optarg, "static"))
897 *elftype = ELFSTATIC;
898 else if (!strcmp(optarg, "dynamic"))
899 *elftype = ELFDYNAMIC;
900 else {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500901 errx(1, "ELF type must be 'static' or "
902 "'dynamic'");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500903 }
904 break;
905 case 'w':
906 minijail_new_session_keyring(j);
907 break;
908 case 'Y':
909 minijail_set_seccomp_filter_tsync(j);
910 break;
911 case 'z':
912 forward = 0;
913 break;
914 case 'd':
915 minijail_namespace_vfs(j);
916 minijail_mount_dev(j);
917 break;
918 /* Long options. */
Mike Frysinger4ffd7e52022-01-24 14:07:02 -0500919 case OPT_AMBIENT:
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500920 ambient_caps = 1;
921 minijail_set_ambient_caps(j);
922 break;
Mike Frysinger4ffd7e52022-01-24 14:07:02 -0500923 case OPT_UTS:
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500924 minijail_namespace_uts(j);
925 if (optarg)
926 minijail_namespace_set_hostname(j, optarg);
927 break;
Mike Frysinger4ffd7e52022-01-24 14:07:02 -0500928 case OPT_LOGGING:
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500929 if (!strcmp(optarg, "auto"))
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400930 log_to_stderr = -1;
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500931 else if (!strcmp(optarg, "syslog"))
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500932 log_to_stderr = 0;
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500933 else if (!strcmp(optarg, "stderr"))
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500934 log_to_stderr = 1;
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500935 else
Zi Lina9e72262022-01-11 03:22:21 +0000936 errx(1,
937 "--logger must be 'syslog' or 'stderr'");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500938 break;
Mike Frysinger4ffd7e52022-01-24 14:07:02 -0500939 case OPT_PROFILE:
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500940 use_profile(j, optarg, &pivot_root, chroot, &tmp_size);
941 break;
Mike Frysinger4ffd7e52022-01-24 14:07:02 -0500942 case OPT_PRELOAD_LIBRARY:
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700943 *preload_path = optarg;
944 break;
Mike Frysinger4ffd7e52022-01-24 14:07:02 -0500945 case OPT_SECCOMP_BPF_BINARY:
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000946 if (seccomp != None && seccomp != BpfBinaryFilter) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500947 errx(1, "Do not use -s, -S, or "
948 "--seccomp-bpf-binary together");
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700949 }
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500950 if (use_seccomp_log == 1)
Zi Linfdc98d62022-01-19 22:10:29 +0000951 errx(1, "-L does not work with "
952 "--seccomp-bpf-binary");
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000953 seccomp = BpfBinaryFilter;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700954 minijail_use_seccomp_filter(j);
955 filter_path = optarg;
956 use_seccomp_filter_binary = 1;
957 break;
Mike Frysinger4ffd7e52022-01-24 14:07:02 -0500958 case OPT_ADD_SUPPL_GROUP:
Zi Linfdc98d62022-01-19 22:10:29 +0000959 suppl_group_add(&suppl_gids_count, &suppl_gids, optarg);
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100960 break;
Mike Frysinger4ffd7e52022-01-24 14:07:02 -0500961 case OPT_ALLOW_SPECULATIVE_EXECUTION:
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100962 minijail_set_seccomp_filter_allow_speculation(j);
963 break;
Mike Frysinger4ffd7e52022-01-24 14:07:02 -0500964 case OPT_CONFIG: {
Zi Lin44461c72021-11-16 18:37:27 +0000965 if (conf_entry_list != NULL) {
Zi Linfdc98d62022-01-19 22:10:29 +0000966 errx(1, "Nested config file specification is "
967 "not allowed.");
Zi Lin44461c72021-11-16 18:37:27 +0000968 }
969 conf_entry_list = new_config_entry_list();
970 conf_index = 0;
Zi Lina9e72262022-01-11 03:22:21 +0000971#if defined(BLOCK_NOEXEC_CONF)
972 /*
Zi Linfdc98d62022-01-19 22:10:29 +0000973 * Check the conf file is in a exec mount.
974 * With a W^X invariant, it excludes writable
975 * mounts.
976 */
Zi Lina9e72262022-01-11 03:22:21 +0000977 struct statfs conf_statfs;
978 if (statfs(optarg, &conf_statfs) != 0)
979 err(1, "statfs(%s) failed.", optarg);
980 if ((conf_statfs.f_flags & MS_NOEXEC) != 0)
981 errx(1,
982 "Conf file must be in a exec "
983 "mount: %s",
984 optarg);
985#endif
986#if defined(ENFORCE_ROOTFS_CONF)
Zi Linfdc98d62022-01-19 22:10:29 +0000987 /* Make sure the conf file is in the same device as the
988 * rootfs. */
Zi Lina9e72262022-01-11 03:22:21 +0000989 struct stat root_stat;
990 struct stat conf_stat;
991 if (stat("/", &root_stat) != 0)
992 err(1, "stat(/) failed.");
993 if (stat(optarg, &conf_stat) != 0)
994 err(1, "stat(%s) failed.", optarg);
995 if (root_stat.st_dev != conf_stat.st_dev)
996 errx(1, "Conf file must be in the rootfs.");
997#endif
Zi Lin44461c72021-11-16 18:37:27 +0000998 attribute_cleanup_fp FILE *config_file =
999 fopen(optarg, "re");
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001000 if (!config_file)
Zi Lina9e72262022-01-11 03:22:21 +00001001 err(1, "Failed to open %s", optarg);
Zi Lin44461c72021-11-16 18:37:27 +00001002 if (!parse_config_file(config_file, conf_entry_list)) {
Zi Linfdc98d62022-01-19 22:10:29 +00001003 errx(
1004 1,
1005 "Unable to parse %s as Minijail conf file, "
1006 "please refer to minijail0(5) for syntax "
1007 "and examples.",
1008 optarg);
Zi Lin44461c72021-11-16 18:37:27 +00001009 }
1010 break;
1011 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001012 default:
1013 usage(argv[0]);
1014 exit(opt == 'h' ? 0 : 1);
1015 }
1016 }
1017
Mike Frysinger3e6a12c2019-09-24 12:50:55 -04001018 if (log_to_stderr == -1) {
1019 /* Autodetect default logging output. */
Mike Frysinger056955c2019-09-24 16:07:05 -04001020 log_to_stderr = isatty(STDIN_FILENO) ? 1 : 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -04001021 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001022 if (log_to_stderr) {
1023 init_logging(LOG_TO_FD, STDERR_FILENO, LOG_INFO);
1024 /*
1025 * When logging to stderr, ensure the FD survives the jailing.
1026 */
1027 if (0 !=
1028 minijail_preserve_fd(j, STDERR_FILENO, STDERR_FILENO)) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001029 errx(1, "Could not preserve stderr");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001030 }
1031 }
1032
1033 /* Set up uid/gid mapping. */
1034 if (set_uidmap || set_gidmap) {
1035 set_ugid_mapping(j, set_uidmap, uid, uidmap, set_gidmap, gid,
1036 gidmap);
1037 }
1038
1039 /* Can only set ambient caps when using regular caps. */
1040 if (ambient_caps && !caps) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001041 errx(1, "Can't set ambient capabilities (--ambient) "
1042 "without actually using capabilities (-c)");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001043 }
1044
1045 /* Set up signal handlers in minijail unless asked not to. */
1046 if (forward)
1047 minijail_forward_signals(j);
1048
1049 /*
1050 * Only allow bind mounts when entering a chroot, using pivot_root, or
1051 * a new mount namespace.
1052 */
1053 if (binding && !(chroot || pivot_root || mount_ns)) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001054 errx(1, "Bind mounts require a chroot, pivot_root, or "
1055 " new mount namespace");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001056 }
1057
1058 /*
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001059 * / is only remounted when entering a new mount namespace, so unless
1060 * that's set there is no need for the -K/-K<mode> flags.
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001061 */
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001062 if (change_remount && !mount_ns) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001063 errx(1, "No need to use -K (skip remounting '/') or "
Mike Frysingerdac801f2022-01-14 13:31:11 -05001064 "-K<mode> (remount '/' as <mode>) "
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001065 "without -v (new mount namespace).\n"
Mike Frysingerdac801f2022-01-14 13:31:11 -05001066 "Do you need to add '-v' explicitly?");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001067 }
1068
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -05001069 /* Configure the remount flag here to avoid having -v override it. */
1070 if (change_remount) {
1071 if (remount_mode != NULL) {
1072 set_remount_mode(j, remount_mode);
1073 } else {
1074 minijail_skip_remount_private(j);
1075 }
1076 }
1077
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001078 /*
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +01001079 * Proceed in setting the supplementary gids specified on the
1080 * cmdline options.
1081 */
1082 if (suppl_gids_count) {
1083 minijail_set_supplementary_gids(j, suppl_gids_count,
Zi Linfdc98d62022-01-19 22:10:29 +00001084 suppl_gids);
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +01001085 free(suppl_gids);
1086 }
1087
1088 /*
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001089 * We parse seccomp filters here to make sure we've collected all
1090 * cmdline options.
1091 */
1092 if (use_seccomp_filter) {
1093 minijail_parse_seccomp_filters(j, filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001094 } else if (use_seccomp_filter_binary) {
1095 struct sock_fprog filter;
1096 read_seccomp_filter(filter_path, &filter);
1097 minijail_set_seccomp_filters(j, &filter);
1098 free((void *)filter.filter);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001099 }
1100
1101 /* Mount a tmpfs under /tmp and set its size. */
1102 if (tmp_size)
1103 minijail_mount_tmp_size(j, tmp_size);
1104
1105 /*
1106 * There should be at least one additional unparsed argument: the
1107 * executable name.
1108 */
1109 if (argc == optind) {
1110 usage(argv[0]);
1111 exit(1);
1112 }
1113
1114 if (*elftype == ELFERROR) {
1115 /*
1116 * -T was not specified.
1117 * Get the path to the program adjusted for changing root.
1118 */
1119 char *program_path =
1120 minijail_get_original_path(j, argv[optind]);
1121
1122 /* Check that we can access the target program. */
1123 if (access(program_path, X_OK)) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001124 errx(1, "Target program '%s' is not accessible",
Zi Lina9e72262022-01-11 03:22:21 +00001125 argv[optind]);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001126 }
1127
1128 /* Check if target is statically or dynamically linked. */
1129 *elftype = get_elf_linkage(program_path);
1130 free(program_path);
1131 }
1132
1133 /*
1134 * Setting capabilities need either a dynamically-linked binary, or the
1135 * use of ambient capabilities for them to be able to survive an
1136 * execve(2).
1137 */
1138 if (caps && *elftype == ELFSTATIC && !ambient_caps) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001139 errx(1, "Can't run statically-linked binaries with capabilities"
1140 " (-c) without also setting ambient capabilities. "
1141 "Try passing --ambient.");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001142 }
1143
1144 return optind;
1145}