blob: e1730998614cdac819e135d0b3e660effa4a5f00 [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 */
125 *suppl_gids = realloc(*suppl_gids,
126 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 }
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500165 err(1, "Could not get the value of the %d-th "
Zi Lina9e72262022-01-11 03:22:21 +0000166 "capability",
167 i);
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700168 }
169 if (cap_value == CAP_SET)
170 caps |= (one << i);
171 }
172 cap_free(parsed_caps);
173 } else {
174 char *end = NULL;
175 caps = strtoull(arg, &end, 16);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500176 if (*end)
177 errx(1, "Invalid cap set: '%s'", arg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500178 }
Luis Hector Chavezdabc4302018-09-21 09:21:47 -0700179
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500180 minijail_use_caps(j, caps);
181}
182
183static void add_binding(struct minijail *j, char *arg)
184{
185 char *src = tokenize(&arg, ",");
186 char *dest = tokenize(&arg, ",");
187 char *flags = tokenize(&arg, ",");
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500188 if (!src || src[0] == '\0' || arg != NULL)
189 errx(1, "Bad binding: %s %s", src, dest);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500190 if (dest == NULL || dest[0] == '\0')
191 dest = src;
David Coles87ec5cd2019-06-13 17:20:10 -0700192 int writable;
193 if (flags == NULL || flags[0] == '\0' || !strcmp(flags, "0"))
194 writable = 0;
195 else if (!strcmp(flags, "1"))
196 writable = 1;
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500197 else
198 errx(1, "Bad value for <writable>: %s", flags);
199 if (minijail_bind(j, src, dest, writable))
200 errx(1, "minijail_bind failed");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500201}
202
203static void add_rlimit(struct minijail *j, char *arg)
204{
205 char *type = tokenize(&arg, ",");
206 char *cur = tokenize(&arg, ",");
207 char *max = tokenize(&arg, ",");
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800208 char *end;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500209 if (!type || type[0] == '\0' || !cur || cur[0] == '\0' ||
210 !max || max[0] == '\0' || arg != NULL) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500211 errx(1, "Bad rlimit '%s'", arg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500212 }
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800213 rlim_t cur_rlim;
214 rlim_t max_rlim;
215 if (!strcmp(cur, "unlimited")) {
216 cur_rlim = RLIM_INFINITY;
217 } else {
218 end = NULL;
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400219 cur_rlim = strtoul(cur, &end, 0);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500220 if (*end)
221 errx(1, "Bad soft limit: '%s'", cur);
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800222 }
223 if (!strcmp(max, "unlimited")) {
224 max_rlim = RLIM_INFINITY;
225 } else {
226 end = NULL;
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400227 max_rlim = strtoul(max, &end, 0);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500228 if (*end)
229 errx(1, "Bad hard limit: '%s'", max);
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800230 }
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400231
232 end = NULL;
233 int resource = parse_single_constant(type, &end);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500234 if (type == end)
235 errx(1, "Bad rlimit: '%s'", type);
Mike Frysingere34d7fe2018-05-23 04:18:30 -0400236
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500237 if (minijail_rlimit(j, resource, cur_rlim, max_rlim))
238 errx(1, "minijail_rlimit '%s,%s,%s' failed", type, cur, max);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500239}
240
241static void add_mount(struct minijail *j, char *arg)
242{
243 char *src = tokenize(&arg, ",");
244 char *dest = tokenize(&arg, ",");
245 char *type = tokenize(&arg, ",");
246 char *flags = tokenize(&arg, ",");
247 char *data = tokenize(&arg, ",");
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400248 char *end;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500249 if (!src || src[0] == '\0' || !dest || dest[0] == '\0' ||
Mike Frysinger4f3e09f2018-01-24 18:01:16 -0500250 !type || type[0] == '\0') {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500251 errx(1, "Bad mount: %s %s %s", src, dest, type);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500252 }
Mike Frysinger4f3e09f2018-01-24 18:01:16 -0500253
254 /*
255 * Fun edge case: the data option itself is comma delimited. If there
256 * were no more options, then arg would be set to NULL. But if we had
257 * more pending, it'll be pointing to the next token. Back up and undo
258 * the null byte so it'll be merged back.
259 * An example:
260 * none,/tmp,tmpfs,0xe,mode=0755,uid=10,gid=10
261 * The tokenize calls above will turn this memory into:
262 * none\0/tmp\0tmpfs\00xe\0mode=0755\0uid=10,gid=10
263 * With data pointing at mode=0755 and arg pointing at uid=10,gid=10.
264 */
265 if (arg != NULL)
266 arg[-1] = ',';
267
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400268 unsigned long mountflags;
269 if (flags == NULL || flags[0] == '\0') {
270 mountflags = 0;
271 } else {
272 end = NULL;
273 mountflags = parse_constant(flags, &end);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500274 if (flags == end)
275 errx(1, "Bad mount flags: %s", flags);
Mike Frysinger6f4e93d2018-05-23 05:05:35 -0400276 }
277
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500278 if (minijail_mount_with_data(j, src, dest, type, mountflags, data))
279 errx(1, "minijail_mount failed");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500280}
281
282static char *build_idmap(id_t id, id_t lowerid)
283{
284 int ret;
Mike Frysinger1036cd82020-08-28 00:15:59 -0400285 char *idmap = xmalloc(IDMAP_LEN);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500286 ret = snprintf(idmap, IDMAP_LEN, "%d %d 1", id, lowerid);
287 if (ret < 0 || (size_t)ret >= IDMAP_LEN) {
288 free(idmap);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500289 errx(1, "Could not build id map");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500290 }
291 return idmap;
292}
293
294static int has_cap_setgid(void)
295{
296 cap_t caps;
297 cap_flag_value_t cap_value;
298
299 if (!CAP_IS_SUPPORTED(CAP_SETGID))
300 return 0;
301
302 caps = cap_get_proc();
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500303 if (!caps)
304 err(1, "Could not get process' capabilities");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500305
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500306 if (cap_get_flag(caps, CAP_SETGID, CAP_EFFECTIVE, &cap_value))
307 err(1, "Could not get the value of CAP_SETGID");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500308
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500309 if (cap_free(caps))
310 err(1, "Could not free capabilities");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500311
312 return cap_value == CAP_SET;
313}
314
315static void set_ugid_mapping(struct minijail *j, int set_uidmap, uid_t uid,
316 char *uidmap, int set_gidmap, gid_t gid,
317 char *gidmap)
318{
319 if (set_uidmap) {
320 minijail_namespace_user(j);
321 minijail_namespace_pids(j);
322
323 if (!uidmap) {
324 /*
325 * If no map is passed, map the current uid to the
326 * chosen uid in the target namespace (or root, if none
327 * was chosen).
328 */
329 uidmap = build_idmap(uid, getuid());
330 }
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500331 if (0 != minijail_uidmap(j, uidmap))
332 errx(1, "Could not set uid map");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500333 free(uidmap);
334 }
335 if (set_gidmap) {
336 minijail_namespace_user(j);
337 minijail_namespace_pids(j);
338
339 if (!gidmap) {
340 /*
341 * If no map is passed, map the current gid to the
342 * chosen gid in the target namespace.
343 */
344 gidmap = build_idmap(gid, getgid());
345 }
346 if (!has_cap_setgid()) {
347 /*
348 * This means that we are not running as root,
349 * so we also have to disable setgroups(2) to
350 * be able to set the gid map.
351 * See
352 * http://man7.org/linux/man-pages/man7/user_namespaces.7.html
353 */
354 minijail_namespace_user_disable_setgroups(j);
355 }
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500356 if (0 != minijail_gidmap(j, gidmap))
357 errx(1, "Could not set gid map");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500358 free(gidmap);
359 }
360}
361
362static void use_chroot(struct minijail *j, const char *path, int *chroot,
363 int pivot_root)
364{
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500365 if (pivot_root)
366 errx(1, "Could not set chroot because -P was specified");
367 if (minijail_enter_chroot(j, path))
368 errx(1, "Could not set chroot");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500369 *chroot = 1;
370}
371
372static void use_pivot_root(struct minijail *j, const char *path,
373 int *pivot_root, int chroot)
374{
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500375 if (chroot)
376 errx(1, "Could not set pivot_root because -C was specified");
377 if (minijail_enter_pivot_root(j, path))
378 errx(1, "Could not set pivot_root");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500379 minijail_namespace_vfs(j);
380 *pivot_root = 1;
381}
382
383static void use_profile(struct minijail *j, const char *profile,
384 int *pivot_root, int chroot, size_t *tmp_size)
385{
Mike Frysinger4d2a81e2018-01-22 16:43:33 -0500386 /* Note: New profiles should be added in minijail0_cli_unittest.cc. */
387
Mike Frysingercc5917c2020-02-03 12:34:14 -0500388 if (!strcmp(profile, "minimalistic-mountns") ||
389 !strcmp(profile, "minimalistic-mountns-nodev")) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500390 minijail_namespace_vfs(j);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500391 if (minijail_bind(j, "/", "/", 0))
392 errx(1, "minijail_bind(/) failed");
393 if (minijail_bind(j, "/proc", "/proc", 0))
394 errx(1, "minijail_bind(/proc) failed");
Mike Frysingercc5917c2020-02-03 12:34:14 -0500395 if (!strcmp(profile, "minimalistic-mountns")) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500396 if (minijail_bind(j, "/dev/log", "/dev/log", 0))
397 errx(1, "minijail_bind(/dev/log) failed");
Mike Frysingercc5917c2020-02-03 12:34:14 -0500398 minijail_mount_dev(j);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500399 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500400 if (!*tmp_size) {
401 /* Avoid clobbering |tmp_size| if it was already set. */
402 *tmp_size = DEFAULT_TMP_SIZE;
403 }
404 minijail_remount_proc_readonly(j);
Allen Webbee876072019-02-21 10:56:21 -0800405 use_pivot_root(j, DEFAULT_PIVOT_ROOT, pivot_root, chroot);
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500406 } else
407 errx(1, "Unrecognized profile name '%s'", profile);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500408}
409
Nicole Anderson-Auafa54be2021-03-09 23:00:49 +0000410static void set_remount_mode(struct minijail *j, const char *mode)
Mike Frysinger785b1c32018-02-23 15:47:24 -0500411{
412 unsigned long msmode;
413 if (!strcmp(mode, "shared"))
414 msmode = MS_SHARED;
415 else if (!strcmp(mode, "private"))
416 msmode = MS_PRIVATE;
417 else if (!strcmp(mode, "slave"))
418 msmode = MS_SLAVE;
419 else if (!strcmp(mode, "unbindable"))
420 msmode = MS_UNBINDABLE;
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500421 else
422 errx(1, "Unknown remount mode: '%s'", mode);
Nicole Anderson-Auafa54be2021-03-09 23:00:49 +0000423 minijail_remount_mode(j, msmode);
Mike Frysinger785b1c32018-02-23 15:47:24 -0500424}
425
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700426static void read_seccomp_filter(const char *filter_path,
427 struct sock_fprog *filter)
428{
Mike Frysingerdebdf5d2021-06-21 09:52:06 -0400429 attribute_cleanup_fp FILE *f = fopen(filter_path, "re");
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500430 if (!f)
431 err(1, "failed to open %s", filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700432 off_t filter_size = 0;
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500433 if (fseeko(f, 0, SEEK_END) == -1 || (filter_size = ftello(f)) == -1)
434 err(1, "failed to get file size of %s", filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700435 if (filter_size % sizeof(struct sock_filter) != 0) {
Zi Lina9e72262022-01-11 03:22:21 +0000436 errx(1,
437 "filter size (%" PRId64 ") of %s is not a multiple of"
438 " %zu",
439 filter_size, filter_path, sizeof(struct sock_filter));
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700440 }
441 rewind(f);
442
443 filter->len = filter_size / sizeof(struct sock_filter);
Mike Frysinger1036cd82020-08-28 00:15:59 -0400444 filter->filter = xmalloc(filter_size);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700445 if (fread(filter->filter, sizeof(struct sock_filter), filter->len, f) !=
446 filter->len) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500447 err(1, "failed read %s", filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700448 }
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700449}
450
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500451static void usage(const char *progn)
452{
453 size_t i;
454 /* clang-format off */
455 printf("Usage: %s [-dGhHiIKlLnNprRstUvyYz]\n"
456 " [-a <table>]\n"
Mike Frysingerc9b07992020-09-01 05:04:48 -0400457 " [-b <src>[,[dest][,<writeable>]]] [-k <src>,<dest>,<type>[,<flags>[,<data>]]]\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500458 " [-c <caps>] [-C <dir>] [-P <dir>] [-e[file]] [-f <file>] [-g <group>]\n"
459 " [-m[<uid> <loweruid> <count>]*] [-M[<gid> <lowergid> <count>]*] [--profile <name>]\n"
460 " [-R <type,cur,max>] [-S <file>] [-t[size]] [-T <type>] [-u <user>] [-V <file>]\n"
461 " <program> [args...]\n"
462 " -a <table>: Use alternate syscall table <table>.\n"
Zi Lin44461c72021-11-16 18:37:27 +0000463 " --bind-mount <...>, Bind <src> to <dest> in chroot.\n"
464 " -b <...>: Multiple instances allowed.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500465 " -B <mask>: Skip setting securebits in <mask> when restricting capabilities (-c).\n"
466 " By default, SECURE_NOROOT, SECURE_NO_SETUID_FIXUP, and \n"
467 " SECURE_KEEP_CAPS (together with their respective locks) are set.\n"
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -0400468 " There are eight securebits in total.\n"
Zi Lin44461c72021-11-16 18:37:27 +0000469 " --mount <...>,Mount <src> at <dest> in chroot.\n"
470 " -k <...>:<flags> and <data> can be specified as in mount(2).\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500471 " Multiple instances allowed.\n"
472 " -c <caps>: Restrict caps to <caps>.\n"
473 " -C <dir>: chroot(2) to <dir>.\n"
474 " Not compatible with -P.\n"
475 " -P <dir>: pivot_root(2) to <dir> (implies -v).\n"
476 " Not compatible with -C.\n"
477 " --mount-dev, Create a new /dev with a minimal set of device nodes (implies -v).\n"
478 " -d: See the minijail0(1) man page for the exact set.\n"
479 " -e[file]: Enter new network namespace, or existing one if |file| is provided.\n"
480 " -f <file>: Write the pid of the jailed process to <file>.\n"
481 " -g <group>: Change gid to <group>.\n"
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100482 " -G: Inherit supplementary groups from new uid.\n"
483 " Not compatible with -y or --add-suppl-group.\n"
484 " -y: Keep original uid's supplementary groups.\n"
485 " Not compatible with -G or --add-suppl-group.\n"
486 " --add-suppl-group <g>:Add <g> to the proccess' supplementary groups,\n"
487 " can be specified multiple times to add several groups.\n"
488 " Not compatible with -y or -G.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500489 " -h: Help (this message).\n"
490 " -H: Seccomp filter help message.\n"
Luis Hector Chavez9dd13fd2018-04-19 20:14:47 -0700491 " -i: Exit immediately after fork(2). The jailed process will run\n"
492 " in the background.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500493 " -I: Run <program> as init (pid 1) inside a new pid namespace (implies -p).\n"
Mike Frysinger785b1c32018-02-23 15:47:24 -0500494 " -K: Do not change share mode of any existing mounts.\n"
495 " -K<mode>: Mark all existing mounts as <mode> instead of MS_PRIVATE.\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500496 " -l: Enter new IPC namespace.\n"
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400497 " -L: Report blocked syscalls when using seccomp filter.\n"
498 " If the kernel does not support SECCOMP_RET_LOG,\n"
499 " forces the following syscalls to be allowed:\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500500 " ", progn);
501 /* clang-format on */
502 for (i = 0; i < log_syscalls_len; i++)
503 printf("%s ", log_syscalls[i]);
504
505 /* clang-format off */
506 printf("\n"
507 " -m[map]: Set the uid map of a user namespace (implies -pU).\n"
508 " Same arguments as newuidmap(1), multiple mappings should be separated by ',' (comma).\n"
509 " With no mapping, map the current uid to root inside the user namespace.\n"
510 " Not compatible with -b without the 'writable' option.\n"
511 " -M[map]: Set the gid map of a user namespace (implies -pU).\n"
512 " Same arguments as newgidmap(1), multiple mappings should be separated by ',' (comma).\n"
513 " With no mapping, map the current gid to root inside the user namespace.\n"
514 " Not compatible with -b without the 'writable' option.\n"
515 " -n: Set no_new_privs.\n"
516 " -N: Enter a new cgroup namespace.\n"
517 " -p: Enter new pid namespace (implies -vr).\n"
518 " -r: Remount /proc read-only (implies -v).\n"
519 " -R: Set rlimits, can be specified multiple times.\n"
520 " -s: Use seccomp mode 1 (not the same as -S).\n"
521 " -S <file>: Set seccomp filter using <file>.\n"
522 " E.g., '-S /usr/share/filters/<prog>.$(uname -m)'.\n"
523 " Requires -n when not running as root.\n"
524 " -t[size]: Mount tmpfs at /tmp (implies -v).\n"
525 " Optional argument specifies size (default \"64M\").\n"
526 " -T <type>: Assume <program> is a <type> ELF binary; <type> can be 'static' or 'dynamic'.\n"
527 " This will avoid accessing <program> binary before execve(2).\n"
528 " Type 'static' will avoid preload hooking.\n"
529 " -u <user>: Change uid to <user>.\n"
530 " -U: Enter new user namespace (implies -p).\n"
531 " -v: Enter new mount namespace.\n"
532 " -V <file>: Enter specified mount namespace.\n"
533 " -w: Create and join a new anonymous session keyring.\n"
534 " -Y: Synchronize seccomp filters across thread group.\n"
535 " -z: Don't forward signals to jailed process.\n"
536 " --ambient: Raise ambient capabilities. Requires -c.\n"
537 " --uts[=name]: Enter a new UTS namespace (and set hostname).\n"
538 " --logging=<s>:Use <s> as the logging system.\n"
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400539 " <s> must be 'auto' (default), 'syslog', or 'stderr'.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700540 " --profile <p>:Configure minijail0 to run with the <p> sandboxing profile,\n"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500541 " which is a convenient way to express multiple flags\n"
542 " that are typically used together.\n"
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700543 " See the minijail0(1) man page for the full list.\n"
544 " --preload-library=<f>:Overrides the path to \"" PRELOADPATH "\".\n"
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700545 " This is only really useful for local testing.\n"
546 " --seccomp-bpf-binary=<f>:Set a pre-compiled seccomp filter using <f>.\n"
547 " E.g., '-S /usr/share/filters/<prog>.$(uname -m).bpf'.\n"
548 " Requires -n when not running as root.\n"
549 " The user is responsible for ensuring that the binary\n"
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100550 " was compiled for the correct architecture / kernel version.\n"
Jorge Lucangeli Obes031568c2021-01-06 10:10:38 -0500551 " --allow-speculative-execution:Allow speculative execution and disable\n"
Zi Lin44461c72021-11-16 18:37:27 +0000552 " mitigations for speculative execution attacks.\n"
553 " --config <file>:Load the Minijail configuration file <file>.\n"
554 " If used, must be specified ahead of other options.\n");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500555 /* clang-format on */
556}
557
558static void seccomp_filter_usage(const char *progn)
559{
560 const struct syscall_entry *entry = syscall_table;
561 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
562 "System call names supported:\n",
563 progn);
564 for (; entry->name && entry->nr >= 0; ++entry)
565 printf(" %s [%d]\n", entry->name, entry->nr);
566 printf("\nSee minijail0(5) for example policies.\n");
567}
568
Zi Lin44461c72021-11-16 18:37:27 +0000569/*
570 * Return the next unconsumed option char/value parsed from
571 * |*conf_entry_list|. |optarg| is updated to point to an argument from
572 * the entry value. If all options have been consumed, |*conf_entry_list|
573 * will be freed and -1 will be returned.
574 */
575static int getopt_from_conf(const struct option *longopts,
576 struct config_entry_list **conf_entry_list,
577 size_t *conf_index)
578{
579 int opt = -1;
580 /* If we've consumed all the options in the this config, reset it. */
581 if (*conf_index >= (*conf_entry_list)->num_entries) {
582 free_config_entry_list(*conf_entry_list);
583 *conf_entry_list = NULL;
584 *conf_index = 0;
585 return opt;
586 }
587
588 struct config_entry *entry = &(*conf_entry_list)->entries[*conf_index];
589 /* Look up a matching long option. */
590 size_t i = 0;
591 const struct option *curr_opt;
592 for (curr_opt = &longopts[0]; curr_opt->name != NULL;
593 curr_opt = &longopts[++i])
594 if (strcmp(entry->key, curr_opt->name) == 0)
595 break;
596 if (curr_opt->name == NULL) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500597 errx(1,
598 "Unable to recognize '%s' as Minijail conf entry key, "
Mike Frysingerdac801f2022-01-14 13:31:11 -0500599 "please refer to minijail0(5) for syntax and examples.",
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500600 entry->key);
Zi Lin44461c72021-11-16 18:37:27 +0000601 }
602 opt = curr_opt->val;
603 optarg = (char *)entry->value;
604 (*conf_index)++;
605 return opt;
606}
607
608/*
609 * Similar to getopt(3), return the next option char/value as it
610 * parses through the CLI argument list. Config entries in
611 * |*conf_entry_list| will be parsed with precendences over cli options.
612 * Same as getopt(3), |optarg| is pointing to the option argument.
613 */
614static int getopt_conf_or_cli(int argc, char *const argv[],
615 struct config_entry_list **conf_entry_list,
616 size_t *conf_index)
617{
618 int opt = -1;
619 static const char optstring[] =
620 "+u:g:sS:c:C:P:b:B:V:f:m::M::k:a:e::R:T:vrGhHinNplLt::IUK::wyYzd";
621 /* clang-format off */
622 static const struct option long_options[] = {
623 {"help", no_argument, 0, 'h'},
624 {"mount-dev", no_argument, 0, 'd'},
625 {"ambient", no_argument, 0, 128},
626 {"uts", optional_argument, 0, 129},
627 {"logging", required_argument, 0, 130},
628 {"profile", required_argument, 0, 131},
629 {"preload-library", required_argument, 0, 132},
630 {"seccomp-bpf-binary", required_argument, 0, 133},
631 {"add-suppl-group", required_argument, 0, 134},
632 {"allow-speculative-execution", no_argument, 0, 135},
633 {"config", required_argument, 0, 136},
634 {"mount", required_argument, 0, 'k'},
635 {"bind-mount", required_argument, 0, 'b'},
636 {0, 0, 0, 0},
637 };
638 /* clang-format on */
639 if (*conf_entry_list != NULL)
640 opt =
641 getopt_from_conf(long_options, conf_entry_list, conf_index);
642 if (opt == -1)
643 opt = getopt_long(argc, argv, optstring, long_options, NULL);
644 return opt;
645}
646
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700647int parse_args(struct minijail *j, int argc, char *const argv[],
648 int *exit_immediately, ElfType *elftype,
649 const char **preload_path)
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500650{
Zi Lin44461c72021-11-16 18:37:27 +0000651 enum seccomp_type { None, Strict, Filter, BpfBinaryFilter };
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000652 enum seccomp_type seccomp = None;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500653 int opt;
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000654 int use_seccomp_filter = 0;
655 int use_seccomp_filter_binary = 0;
656 int use_seccomp_log = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500657 int forward = 1;
658 int binding = 0;
659 int chroot = 0, pivot_root = 0;
Nicole Anderson-Auafa54be2021-03-09 23:00:49 +0000660 int mount_ns = 0, change_remount = 0;
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -0500661 const char *remount_mode = NULL;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500662 int inherit_suppl_gids = 0, keep_suppl_gids = 0;
663 int caps = 0, ambient_caps = 0;
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800664 bool use_uid = false, use_gid = false;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500665 uid_t uid = 0;
666 gid_t gid = 0;
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100667 gid_t *suppl_gids = NULL;
668 size_t suppl_gids_count = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500669 char *uidmap = NULL, *gidmap = NULL;
670 int set_uidmap = 0, set_gidmap = 0;
671 size_t tmp_size = 0;
672 const char *filter_path = NULL;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400673 int log_to_stderr = -1;
Zi Lin44461c72021-11-16 18:37:27 +0000674 struct config_entry_list *conf_entry_list = NULL;
675 size_t conf_index = 0;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500676
Zi Lin44461c72021-11-16 18:37:27 +0000677 while ((opt = getopt_conf_or_cli(argc, argv, &conf_entry_list,
678 &conf_index)) != -1) {
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500679 switch (opt) {
680 case 'u':
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500681 if (use_uid)
682 errx(1, "-u provided multiple times.");
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800683 use_uid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500684 set_user(j, optarg, &uid, &gid);
685 break;
686 case 'g':
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500687 if (use_gid)
688 errx(1, "-g provided multiple times.");
Luis Hector Chavez8ddef8f2019-01-02 08:40:54 -0800689 use_gid = true;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500690 set_group(j, optarg, &gid);
691 break;
692 case 'n':
693 minijail_no_new_privs(j);
694 break;
695 case 's':
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000696 if (seccomp != None && seccomp != Strict) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500697 errx(1, "Do not use -s, -S, or "
698 "--seccomp-bpf-binary together");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500699 }
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000700 seccomp = Strict;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500701 minijail_use_seccomp(j);
702 break;
703 case 'S':
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000704 if (seccomp != None && seccomp != Filter) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500705 errx(1, "Do not use -s, -S, or "
706 "--seccomp-bpf-binary together");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500707 }
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000708 seccomp = Filter;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500709 minijail_use_seccomp_filter(j);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700710 filter_path = optarg;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500711 use_seccomp_filter = 1;
712 break;
713 case 'l':
714 minijail_namespace_ipc(j);
715 break;
716 case 'L':
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000717 if (seccomp == BpfBinaryFilter) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500718 errx(1, "-L does not work with "
719 "--seccomp-bpf-binary");
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000720 }
721 use_seccomp_log = 1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500722 minijail_log_seccomp_filter_failures(j);
723 break;
724 case 'b':
725 add_binding(j, optarg);
726 binding = 1;
727 break;
728 case 'B':
729 skip_securebits(j, optarg);
730 break;
731 case 'c':
732 caps = 1;
733 use_caps(j, optarg);
734 break;
735 case 'C':
736 use_chroot(j, optarg, &chroot, pivot_root);
737 break;
738 case 'k':
739 add_mount(j, optarg);
740 break;
741 case 'K':
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -0500742 remount_mode = optarg;
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -0400743 change_remount = 1;
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500744 break;
745 case 'P':
746 use_pivot_root(j, optarg, &pivot_root, chroot);
747 break;
748 case 'f':
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500749 if (0 != minijail_write_pid_file(j, optarg))
750 errx(1, "Could not prepare pid file path");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500751 break;
752 case 't':
753 minijail_namespace_vfs(j);
754 if (!tmp_size) {
755 /*
756 * Avoid clobbering |tmp_size| if it was already
757 * set.
758 */
759 tmp_size = DEFAULT_TMP_SIZE;
760 }
761 if (optarg != NULL &&
762 0 != parse_size(&tmp_size, optarg)) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500763 errx(1, "Invalid /tmp tmpfs size");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500764 }
765 break;
766 case 'v':
767 minijail_namespace_vfs(j);
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -0500768 /*
769 * Set the default mount propagation in the command-line
770 * tool to MS_SLAVE.
771 *
772 * When executing the sandboxed program in a new mount
773 * namespace the Minijail library will by default
774 * remount all mounts with the MS_PRIVATE flag. While
775 * this is an appropriate, safe default for the library,
776 * MS_PRIVATE can be problematic: unmount events will
777 * not propagate into mountpoints marked as MS_PRIVATE.
778 * This means that if a mount is unmounted in the root
779 * mount namespace, it will not be unmounted in the
780 * non-root mount namespace.
781 * This in turn can be problematic because activity in
782 * the non-root mount namespace can now directly
783 * influence the root mount namespace (e.g. preventing
784 * re-mounts of said mount), which would be a privilege
785 * inversion.
786 *
787 * Setting the default in the command-line to MS_SLAVE
788 * will still prevent mounts from leaking out of the
789 * non-root mount namespace but avoid these
790 * privilege-inversion issues.
791 * For cases where mounts should not flow *into* the
792 * namespace either, the user can pass -Kprivate.
793 * Note that mounts are marked as MS_PRIVATE by default
794 * by the kernel, so unless the init process (like
795 * systemd) or something else marks them as shared, this
796 * won't do anything.
797 */
798 minijail_remount_mode(j, MS_SLAVE);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500799 mount_ns = 1;
800 break;
801 case 'V':
802 minijail_namespace_enter_vfs(j, optarg);
803 break;
804 case 'r':
805 minijail_remount_proc_readonly(j);
806 break;
807 case 'G':
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500808 if (keep_suppl_gids)
809 errx(1, "-y and -G are not compatible");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500810 minijail_inherit_usergroups(j);
811 inherit_suppl_gids = 1;
812 break;
813 case 'y':
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500814 if (inherit_suppl_gids)
815 errx(1, "-y and -G are not compatible");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500816 minijail_keep_supplementary_gids(j);
817 keep_suppl_gids = 1;
818 break;
819 case 'N':
820 minijail_namespace_cgroups(j);
821 break;
822 case 'p':
823 minijail_namespace_pids(j);
824 break;
825 case 'e':
826 if (optarg)
827 minijail_namespace_enter_net(j, optarg);
828 else
829 minijail_namespace_net(j);
830 break;
831 case 'i':
832 *exit_immediately = 1;
833 break;
834 case 'H':
835 seccomp_filter_usage(argv[0]);
836 exit(0);
837 case 'I':
838 minijail_namespace_pids(j);
839 minijail_run_as_init(j);
840 break;
841 case 'U':
842 minijail_namespace_user(j);
843 minijail_namespace_pids(j);
844 break;
845 case 'm':
846 set_uidmap = 1;
847 if (uidmap) {
848 free(uidmap);
849 uidmap = NULL;
850 }
851 if (optarg)
Mike Frysinger1036cd82020-08-28 00:15:59 -0400852 uidmap = xstrdup(optarg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500853 break;
854 case 'M':
855 set_gidmap = 1;
856 if (gidmap) {
857 free(gidmap);
858 gidmap = NULL;
859 }
860 if (optarg)
Mike Frysinger1036cd82020-08-28 00:15:59 -0400861 gidmap = xstrdup(optarg);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500862 break;
863 case 'a':
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500864 if (0 != minijail_use_alt_syscall(j, optarg))
865 errx(1, "Could not set alt-syscall table");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500866 break;
867 case 'R':
868 add_rlimit(j, optarg);
869 break;
870 case 'T':
871 if (!strcmp(optarg, "static"))
872 *elftype = ELFSTATIC;
873 else if (!strcmp(optarg, "dynamic"))
874 *elftype = ELFDYNAMIC;
875 else {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500876 errx(1, "ELF type must be 'static' or "
877 "'dynamic'");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500878 }
879 break;
880 case 'w':
881 minijail_new_session_keyring(j);
882 break;
883 case 'Y':
884 minijail_set_seccomp_filter_tsync(j);
885 break;
886 case 'z':
887 forward = 0;
888 break;
889 case 'd':
890 minijail_namespace_vfs(j);
891 minijail_mount_dev(j);
892 break;
893 /* Long options. */
894 case 128: /* Ambient caps. */
895 ambient_caps = 1;
896 minijail_set_ambient_caps(j);
897 break;
898 case 129: /* UTS/hostname namespace. */
899 minijail_namespace_uts(j);
900 if (optarg)
901 minijail_namespace_set_hostname(j, optarg);
902 break;
903 case 130: /* Logging. */
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500904 if (!strcmp(optarg, "auto"))
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400905 log_to_stderr = -1;
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500906 else if (!strcmp(optarg, "syslog"))
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500907 log_to_stderr = 0;
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500908 else if (!strcmp(optarg, "stderr"))
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500909 log_to_stderr = 1;
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500910 else
Zi Lina9e72262022-01-11 03:22:21 +0000911 errx(1,
912 "--logger must be 'syslog' or 'stderr'");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500913 break;
914 case 131: /* Profile */
915 use_profile(j, optarg, &pivot_root, chroot, &tmp_size);
916 break;
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700917 case 132: /* PRELOADPATH */
918 *preload_path = optarg;
919 break;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700920 case 133: /* seccomp-bpf binary. */
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000921 if (seccomp != None && seccomp != BpfBinaryFilter) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500922 errx(1, "Do not use -s, -S, or "
923 "--seccomp-bpf-binary together");
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700924 }
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500925 if (use_seccomp_log == 1)
926 errx(1, "-L does not work with --seccomp-bpf-binary");
Nicole Anderson-Auc1118f62021-09-14 22:20:23 +0000927 seccomp = BpfBinaryFilter;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700928 minijail_use_seccomp_filter(j);
929 filter_path = optarg;
930 use_seccomp_filter_binary = 1;
931 break;
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +0100932 case 134:
933 suppl_group_add(&suppl_gids_count, &suppl_gids,
934 optarg);
935 break;
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100936 case 135:
937 minijail_set_seccomp_filter_allow_speculation(j);
938 break;
Zi Lin44461c72021-11-16 18:37:27 +0000939 case 136: {
940 if (conf_entry_list != NULL) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500941 errx(1,
942 "Nested config file specification is "
Mike Frysingerdac801f2022-01-14 13:31:11 -0500943 "not allowed.");
Zi Lin44461c72021-11-16 18:37:27 +0000944 }
945 conf_entry_list = new_config_entry_list();
946 conf_index = 0;
Zi Lina9e72262022-01-11 03:22:21 +0000947#if defined(BLOCK_NOEXEC_CONF)
948 /*
949 * Check the conf file is in a exec mount.
950 * With a W^X invariant, it excludes writable
951 * mounts.
952 */
953 struct statfs conf_statfs;
954 if (statfs(optarg, &conf_statfs) != 0)
955 err(1, "statfs(%s) failed.", optarg);
956 if ((conf_statfs.f_flags & MS_NOEXEC) != 0)
957 errx(1,
958 "Conf file must be in a exec "
959 "mount: %s",
960 optarg);
961#endif
962#if defined(ENFORCE_ROOTFS_CONF)
963 /* Make sure the conf file is in the same device as the rootfs. */
964 struct stat root_stat;
965 struct stat conf_stat;
966 if (stat("/", &root_stat) != 0)
967 err(1, "stat(/) failed.");
968 if (stat(optarg, &conf_stat) != 0)
969 err(1, "stat(%s) failed.", optarg);
970 if (root_stat.st_dev != conf_stat.st_dev)
971 errx(1, "Conf file must be in the rootfs.");
972#endif
Zi Lin44461c72021-11-16 18:37:27 +0000973 attribute_cleanup_fp FILE *config_file =
974 fopen(optarg, "re");
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500975 if (!config_file)
Zi Lina9e72262022-01-11 03:22:21 +0000976 err(1, "Failed to open %s", optarg);
Zi Lin44461c72021-11-16 18:37:27 +0000977 if (!parse_config_file(config_file, conf_entry_list)) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500978 errx(1,
979 "Unable to parse %s as Minijail conf file, "
980 "please refer to minijail0(5) for syntax "
Mike Frysingerdac801f2022-01-14 13:31:11 -0500981 "and examples.",
Mike Frysinger68f7ccd2021-11-24 22:06:51 -0500982 optarg);
Zi Lin44461c72021-11-16 18:37:27 +0000983 }
984 break;
985 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500986 default:
987 usage(argv[0]);
988 exit(opt == 'h' ? 0 : 1);
989 }
990 }
991
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400992 if (log_to_stderr == -1) {
993 /* Autodetect default logging output. */
Mike Frysinger056955c2019-09-24 16:07:05 -0400994 log_to_stderr = isatty(STDIN_FILENO) ? 1 : 0;
Mike Frysinger3e6a12c2019-09-24 12:50:55 -0400995 }
Mike Frysinger5ef22ca2018-01-20 13:42:10 -0500996 if (log_to_stderr) {
997 init_logging(LOG_TO_FD, STDERR_FILENO, LOG_INFO);
998 /*
999 * When logging to stderr, ensure the FD survives the jailing.
1000 */
1001 if (0 !=
1002 minijail_preserve_fd(j, STDERR_FILENO, STDERR_FILENO)) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001003 errx(1, "Could not preserve stderr");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001004 }
1005 }
1006
1007 /* Set up uid/gid mapping. */
1008 if (set_uidmap || set_gidmap) {
1009 set_ugid_mapping(j, set_uidmap, uid, uidmap, set_gidmap, gid,
1010 gidmap);
1011 }
1012
1013 /* Can only set ambient caps when using regular caps. */
1014 if (ambient_caps && !caps) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001015 errx(1, "Can't set ambient capabilities (--ambient) "
1016 "without actually using capabilities (-c)");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001017 }
1018
1019 /* Set up signal handlers in minijail unless asked not to. */
1020 if (forward)
1021 minijail_forward_signals(j);
1022
1023 /*
1024 * Only allow bind mounts when entering a chroot, using pivot_root, or
1025 * a new mount namespace.
1026 */
1027 if (binding && !(chroot || pivot_root || mount_ns)) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001028 errx(1, "Bind mounts require a chroot, pivot_root, or "
1029 " new mount namespace");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001030 }
1031
1032 /*
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001033 * / is only remounted when entering a new mount namespace, so unless
1034 * that's set there is no need for the -K/-K<mode> flags.
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001035 */
Jorge Lucangeli Obes93418062019-09-27 10:59:45 -04001036 if (change_remount && !mount_ns) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001037 errx(1, "No need to use -K (skip remounting '/') or "
Mike Frysingerdac801f2022-01-14 13:31:11 -05001038 "-K<mode> (remount '/' as <mode>) "
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001039 "without -v (new mount namespace).\n"
Mike Frysingerdac801f2022-01-14 13:31:11 -05001040 "Do you need to add '-v' explicitly?");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001041 }
1042
Jorge Lucangeli Obes9e1ac372020-01-23 14:36:50 -05001043 /* Configure the remount flag here to avoid having -v override it. */
1044 if (change_remount) {
1045 if (remount_mode != NULL) {
1046 set_remount_mode(j, remount_mode);
1047 } else {
1048 minijail_skip_remount_private(j);
1049 }
1050 }
1051
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001052 /*
Stéphane Lesimple8d7174b2020-02-07 20:51:08 +01001053 * Proceed in setting the supplementary gids specified on the
1054 * cmdline options.
1055 */
1056 if (suppl_gids_count) {
1057 minijail_set_supplementary_gids(j, suppl_gids_count,
1058 suppl_gids);
1059 free(suppl_gids);
1060 }
1061
1062 /*
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001063 * We parse seccomp filters here to make sure we've collected all
1064 * cmdline options.
1065 */
1066 if (use_seccomp_filter) {
1067 minijail_parse_seccomp_filters(j, filter_path);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -07001068 } else if (use_seccomp_filter_binary) {
1069 struct sock_fprog filter;
1070 read_seccomp_filter(filter_path, &filter);
1071 minijail_set_seccomp_filters(j, &filter);
1072 free((void *)filter.filter);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001073 }
1074
1075 /* Mount a tmpfs under /tmp and set its size. */
1076 if (tmp_size)
1077 minijail_mount_tmp_size(j, tmp_size);
1078
1079 /*
1080 * There should be at least one additional unparsed argument: the
1081 * executable name.
1082 */
1083 if (argc == optind) {
1084 usage(argv[0]);
1085 exit(1);
1086 }
1087
1088 if (*elftype == ELFERROR) {
1089 /*
1090 * -T was not specified.
1091 * Get the path to the program adjusted for changing root.
1092 */
1093 char *program_path =
1094 minijail_get_original_path(j, argv[optind]);
1095
1096 /* Check that we can access the target program. */
1097 if (access(program_path, X_OK)) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001098 errx(1, "Target program '%s' is not accessible",
Zi Lina9e72262022-01-11 03:22:21 +00001099 argv[optind]);
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001100 }
1101
1102 /* Check if target is statically or dynamically linked. */
1103 *elftype = get_elf_linkage(program_path);
1104 free(program_path);
1105 }
1106
1107 /*
1108 * Setting capabilities need either a dynamically-linked binary, or the
1109 * use of ambient capabilities for them to be able to survive an
1110 * execve(2).
1111 */
1112 if (caps && *elftype == ELFSTATIC && !ambient_caps) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05001113 errx(1, "Can't run statically-linked binaries with capabilities"
1114 " (-c) without also setting ambient capabilities. "
1115 "Try passing --ambient.");
Mike Frysinger5ef22ca2018-01-20 13:42:10 -05001116 }
1117
1118 return optind;
1119}