xref: /aosp_15_r20/external/minijail/minijail0_cli.c (revision 4b9c6d91573e8b3a96609339b46361b5476dd0f9)
1*4b9c6d91SCole Faust /* Copyright 2018 The ChromiumOS Authors
2*4b9c6d91SCole Faust  * Use of this source code is governed by a BSD-style license that can be
3*4b9c6d91SCole Faust  * found in the LICENSE file.
4*4b9c6d91SCole Faust  */
5*4b9c6d91SCole Faust 
6*4b9c6d91SCole Faust #include <dlfcn.h>
7*4b9c6d91SCole Faust #include <err.h>
8*4b9c6d91SCole Faust #include <errno.h>
9*4b9c6d91SCole Faust #include <fcntl.h>
10*4b9c6d91SCole Faust #include <getopt.h>
11*4b9c6d91SCole Faust #include <inttypes.h>
12*4b9c6d91SCole Faust #include <stdbool.h>
13*4b9c6d91SCole Faust #include <stdio.h>
14*4b9c6d91SCole Faust #include <stdlib.h>
15*4b9c6d91SCole Faust #include <string.h>
16*4b9c6d91SCole Faust #include <sys/capability.h>
17*4b9c6d91SCole Faust #include <sys/mount.h>
18*4b9c6d91SCole Faust #include <sys/stat.h>
19*4b9c6d91SCole Faust #include <sys/types.h>
20*4b9c6d91SCole Faust #include <sys/vfs.h>
21*4b9c6d91SCole Faust #include <unistd.h>
22*4b9c6d91SCole Faust 
23*4b9c6d91SCole Faust #include <linux/filter.h>
24*4b9c6d91SCole Faust 
25*4b9c6d91SCole Faust #include "libminijail.h"
26*4b9c6d91SCole Faust #include "libsyscalls.h"
27*4b9c6d91SCole Faust 
28*4b9c6d91SCole Faust #include "config_parser.h"
29*4b9c6d91SCole Faust #include "elfparse.h"
30*4b9c6d91SCole Faust #include "minijail0_cli.h"
31*4b9c6d91SCole Faust #include "system.h"
32*4b9c6d91SCole Faust #include "util.h"
33*4b9c6d91SCole Faust 
34*4b9c6d91SCole Faust #define IDMAP_LEN 32U
35*4b9c6d91SCole Faust #define DEFAULT_TMP_SIZE (64 * 1024 * 1024)
36*4b9c6d91SCole Faust 
37*4b9c6d91SCole Faust /*
38*4b9c6d91SCole Faust  * A malloc() that aborts on failure.  We only implement this in the CLI as
39*4b9c6d91SCole Faust  * the library should return ENOMEM errors when allocations fail.
40*4b9c6d91SCole Faust  */
xmalloc(size_t size)41*4b9c6d91SCole Faust static void *xmalloc(size_t size)
42*4b9c6d91SCole Faust {
43*4b9c6d91SCole Faust 	void *ret = malloc(size);
44*4b9c6d91SCole Faust 	if (!ret)
45*4b9c6d91SCole Faust 		err(1, "malloc() failed");
46*4b9c6d91SCole Faust 	return ret;
47*4b9c6d91SCole Faust }
48*4b9c6d91SCole Faust 
xstrdup(const char * s)49*4b9c6d91SCole Faust static char *xstrdup(const char *s)
50*4b9c6d91SCole Faust {
51*4b9c6d91SCole Faust 	char *ret = strdup(s);
52*4b9c6d91SCole Faust 	if (!ret)
53*4b9c6d91SCole Faust 		err(1, "strdup() failed");
54*4b9c6d91SCole Faust 	return ret;
55*4b9c6d91SCole Faust }
56*4b9c6d91SCole Faust 
set_user(struct minijail * j,const char * arg,uid_t * out_uid,gid_t * out_gid)57*4b9c6d91SCole Faust static void set_user(struct minijail *j, const char *arg, uid_t *out_uid,
58*4b9c6d91SCole Faust 		     gid_t *out_gid)
59*4b9c6d91SCole Faust {
60*4b9c6d91SCole Faust 	char *end = NULL;
61*4b9c6d91SCole Faust 	uid_t uid = strtoul(arg, &end, 10);
62*4b9c6d91SCole Faust 	if (!*end && *arg) {
63*4b9c6d91SCole Faust 		*out_uid = uid;
64*4b9c6d91SCole Faust 		minijail_change_uid(j, uid);
65*4b9c6d91SCole Faust 		return;
66*4b9c6d91SCole Faust 	}
67*4b9c6d91SCole Faust 
68*4b9c6d91SCole Faust 	int ret = lookup_user(arg, out_uid, out_gid);
69*4b9c6d91SCole Faust 	if (ret) {
70*4b9c6d91SCole Faust 		errno = -ret;
71*4b9c6d91SCole Faust 		err(1, "Bad user '%s'", arg);
72*4b9c6d91SCole Faust 	}
73*4b9c6d91SCole Faust 
74*4b9c6d91SCole Faust 	ret = minijail_change_user(j, arg);
75*4b9c6d91SCole Faust 	if (ret) {
76*4b9c6d91SCole Faust 		errno = -ret;
77*4b9c6d91SCole Faust 		err(1, "minijail_change_user('%s') failed", arg);
78*4b9c6d91SCole Faust 	}
79*4b9c6d91SCole Faust }
80*4b9c6d91SCole Faust 
set_group(struct minijail * j,const char * arg,gid_t * out_gid)81*4b9c6d91SCole Faust static void set_group(struct minijail *j, const char *arg, gid_t *out_gid)
82*4b9c6d91SCole Faust {
83*4b9c6d91SCole Faust 	char *end = NULL;
84*4b9c6d91SCole Faust 	gid_t gid = strtoul(arg, &end, 10);
85*4b9c6d91SCole Faust 	if (!*end && *arg) {
86*4b9c6d91SCole Faust 		*out_gid = gid;
87*4b9c6d91SCole Faust 		minijail_change_gid(j, gid);
88*4b9c6d91SCole Faust 		return;
89*4b9c6d91SCole Faust 	}
90*4b9c6d91SCole Faust 
91*4b9c6d91SCole Faust 	int ret = lookup_group(arg, out_gid);
92*4b9c6d91SCole Faust 	if (ret) {
93*4b9c6d91SCole Faust 		errno = -ret;
94*4b9c6d91SCole Faust 		err(1, "Bad group '%s'", arg);
95*4b9c6d91SCole Faust 	}
96*4b9c6d91SCole Faust 
97*4b9c6d91SCole Faust 	minijail_change_gid(j, *out_gid);
98*4b9c6d91SCole Faust }
99*4b9c6d91SCole Faust 
100*4b9c6d91SCole Faust /*
101*4b9c6d91SCole Faust  * Helper function used by --add-suppl-group (possibly more than once),
102*4b9c6d91SCole Faust  * to build the supplementary gids array.
103*4b9c6d91SCole Faust  */
suppl_group_add(size_t * suppl_gids_count,gid_t ** suppl_gids,char * arg)104*4b9c6d91SCole Faust static void suppl_group_add(size_t *suppl_gids_count, gid_t **suppl_gids,
105*4b9c6d91SCole Faust 			    char *arg)
106*4b9c6d91SCole Faust {
107*4b9c6d91SCole Faust 	char *end = NULL;
108*4b9c6d91SCole Faust 	gid_t gid = strtoul(arg, &end, 10);
109*4b9c6d91SCole Faust 	int ret;
110*4b9c6d91SCole Faust 	if (!*end && *arg) {
111*4b9c6d91SCole Faust 		/* A gid number has been specified, proceed. */
112*4b9c6d91SCole Faust 	} else if ((ret = lookup_group(arg, &gid))) {
113*4b9c6d91SCole Faust 		/*
114*4b9c6d91SCole Faust 		 * A group name has been specified,
115*4b9c6d91SCole Faust 		 * but doesn't exist: we bail out.
116*4b9c6d91SCole Faust 		 */
117*4b9c6d91SCole Faust 		errno = -ret;
118*4b9c6d91SCole Faust 		err(1, "Bad group '%s'", arg);
119*4b9c6d91SCole Faust 	}
120*4b9c6d91SCole Faust 
121*4b9c6d91SCole Faust 	/*
122*4b9c6d91SCole Faust 	 * From here, gid is guaranteed to be set and valid,
123*4b9c6d91SCole Faust 	 * we add it to our supplementary gids array.
124*4b9c6d91SCole Faust 	 */
125*4b9c6d91SCole Faust 	*suppl_gids =
126*4b9c6d91SCole Faust 	    realloc(*suppl_gids, sizeof(gid_t) * ++(*suppl_gids_count));
127*4b9c6d91SCole Faust 	if (!suppl_gids)
128*4b9c6d91SCole Faust 		err(1, "failed to allocate memory");
129*4b9c6d91SCole Faust 
130*4b9c6d91SCole Faust 	(*suppl_gids)[*suppl_gids_count - 1] = gid;
131*4b9c6d91SCole Faust }
132*4b9c6d91SCole Faust 
skip_securebits(struct minijail * j,const char * arg)133*4b9c6d91SCole Faust static void skip_securebits(struct minijail *j, const char *arg)
134*4b9c6d91SCole Faust {
135*4b9c6d91SCole Faust 	uint64_t securebits_skip_mask;
136*4b9c6d91SCole Faust 	char *end = NULL;
137*4b9c6d91SCole Faust 	securebits_skip_mask = strtoull(arg, &end, 16);
138*4b9c6d91SCole Faust 	if (*end)
139*4b9c6d91SCole Faust 		errx(1, "Invalid securebit mask: '%s'", arg);
140*4b9c6d91SCole Faust 	minijail_skip_setting_securebits(j, securebits_skip_mask);
141*4b9c6d91SCole Faust }
142*4b9c6d91SCole Faust 
use_caps(struct minijail * j,const char * arg)143*4b9c6d91SCole Faust static void use_caps(struct minijail *j, const char *arg)
144*4b9c6d91SCole Faust {
145*4b9c6d91SCole Faust 	uint64_t caps = 0;
146*4b9c6d91SCole Faust 	cap_t parsed_caps = cap_from_text(arg);
147*4b9c6d91SCole Faust 
148*4b9c6d91SCole Faust 	if (parsed_caps != NULL) {
149*4b9c6d91SCole Faust 		unsigned int i;
150*4b9c6d91SCole Faust 		const uint64_t one = 1;
151*4b9c6d91SCole Faust 		cap_flag_value_t cap_value;
152*4b9c6d91SCole Faust 		unsigned int last_valid_cap = get_last_valid_cap();
153*4b9c6d91SCole Faust 
154*4b9c6d91SCole Faust 		for (i = 0; i <= last_valid_cap; ++i) {
155*4b9c6d91SCole Faust 			if (cap_get_flag(parsed_caps, i, CAP_EFFECTIVE,
156*4b9c6d91SCole Faust 					 &cap_value)) {
157*4b9c6d91SCole Faust 				if (errno == EINVAL) {
158*4b9c6d91SCole Faust 					/*
159*4b9c6d91SCole Faust 					 * Some versions of libcap reject any
160*4b9c6d91SCole Faust 					 * capabilities they were not compiled
161*4b9c6d91SCole Faust 					 * with by returning EINVAL.
162*4b9c6d91SCole Faust 					 */
163*4b9c6d91SCole Faust 					continue;
164*4b9c6d91SCole Faust 				}
165*4b9c6d91SCole Faust 				err(1,
166*4b9c6d91SCole Faust 				    "Could not get the value of the %d-th "
167*4b9c6d91SCole Faust 				    "capability",
168*4b9c6d91SCole Faust 				    i);
169*4b9c6d91SCole Faust 			}
170*4b9c6d91SCole Faust 			if (cap_value == CAP_SET)
171*4b9c6d91SCole Faust 				caps |= (one << i);
172*4b9c6d91SCole Faust 		}
173*4b9c6d91SCole Faust 		cap_free(parsed_caps);
174*4b9c6d91SCole Faust 	} else {
175*4b9c6d91SCole Faust 		char *end = NULL;
176*4b9c6d91SCole Faust 		caps = strtoull(arg, &end, 16);
177*4b9c6d91SCole Faust 		if (*end)
178*4b9c6d91SCole Faust 			errx(1, "Invalid cap set: '%s'", arg);
179*4b9c6d91SCole Faust 	}
180*4b9c6d91SCole Faust 
181*4b9c6d91SCole Faust 	minijail_use_caps(j, caps);
182*4b9c6d91SCole Faust }
183*4b9c6d91SCole Faust 
add_binding(struct minijail * j,char * arg)184*4b9c6d91SCole Faust static void add_binding(struct minijail *j, char *arg)
185*4b9c6d91SCole Faust {
186*4b9c6d91SCole Faust 	char *src = tokenize(&arg, ",");
187*4b9c6d91SCole Faust 	char *dest = tokenize(&arg, ",");
188*4b9c6d91SCole Faust 	char *flags = tokenize(&arg, ",");
189*4b9c6d91SCole Faust 	if (!src || src[0] == '\0' || arg != NULL)
190*4b9c6d91SCole Faust 		errx(1, "Bad binding: %s %s", src, dest);
191*4b9c6d91SCole Faust 	if (dest == NULL || dest[0] == '\0')
192*4b9c6d91SCole Faust 		dest = src;
193*4b9c6d91SCole Faust 	int writable;
194*4b9c6d91SCole Faust 	if (flags == NULL || flags[0] == '\0' || streq(flags, "0"))
195*4b9c6d91SCole Faust 		writable = 0;
196*4b9c6d91SCole Faust 	else if (streq(flags, "1"))
197*4b9c6d91SCole Faust 		writable = 1;
198*4b9c6d91SCole Faust 	else
199*4b9c6d91SCole Faust 		errx(1, "Bad value for <writable>: %s", flags);
200*4b9c6d91SCole Faust 	if (minijail_bind(j, src, dest, writable))
201*4b9c6d91SCole Faust 		errx(1, "minijail_bind failed");
202*4b9c6d91SCole Faust }
203*4b9c6d91SCole Faust 
add_rlimit(struct minijail * j,char * arg)204*4b9c6d91SCole Faust static void add_rlimit(struct minijail *j, char *arg)
205*4b9c6d91SCole Faust {
206*4b9c6d91SCole Faust 	char *type = tokenize(&arg, ",");
207*4b9c6d91SCole Faust 	char *cur = tokenize(&arg, ",");
208*4b9c6d91SCole Faust 	char *max = tokenize(&arg, ",");
209*4b9c6d91SCole Faust 	char *end;
210*4b9c6d91SCole Faust 	if (!type || type[0] == '\0' || !cur || cur[0] == '\0' || !max ||
211*4b9c6d91SCole Faust 	    max[0] == '\0' || arg != NULL) {
212*4b9c6d91SCole Faust 		errx(1, "Bad rlimit '%s'", arg);
213*4b9c6d91SCole Faust 	}
214*4b9c6d91SCole Faust 	rlim_t cur_rlim;
215*4b9c6d91SCole Faust 	rlim_t max_rlim;
216*4b9c6d91SCole Faust 	if (streq(cur, "unlimited")) {
217*4b9c6d91SCole Faust 		cur_rlim = RLIM_INFINITY;
218*4b9c6d91SCole Faust 	} else {
219*4b9c6d91SCole Faust 		end = NULL;
220*4b9c6d91SCole Faust 		cur_rlim = strtoul(cur, &end, 0);
221*4b9c6d91SCole Faust 		if (*end)
222*4b9c6d91SCole Faust 			errx(1, "Bad soft limit: '%s'", cur);
223*4b9c6d91SCole Faust 	}
224*4b9c6d91SCole Faust 	if (streq(max, "unlimited")) {
225*4b9c6d91SCole Faust 		max_rlim = RLIM_INFINITY;
226*4b9c6d91SCole Faust 	} else {
227*4b9c6d91SCole Faust 		end = NULL;
228*4b9c6d91SCole Faust 		max_rlim = strtoul(max, &end, 0);
229*4b9c6d91SCole Faust 		if (*end)
230*4b9c6d91SCole Faust 			errx(1, "Bad hard limit: '%s'", max);
231*4b9c6d91SCole Faust 	}
232*4b9c6d91SCole Faust 
233*4b9c6d91SCole Faust 	end = NULL;
234*4b9c6d91SCole Faust 	int resource = parse_single_constant(type, &end);
235*4b9c6d91SCole Faust 	if (type == end)
236*4b9c6d91SCole Faust 		errx(1, "Bad rlimit: '%s'", type);
237*4b9c6d91SCole Faust 
238*4b9c6d91SCole Faust 	if (minijail_rlimit(j, resource, cur_rlim, max_rlim))
239*4b9c6d91SCole Faust 		errx(1, "minijail_rlimit '%s,%s,%s' failed", type, cur, max);
240*4b9c6d91SCole Faust }
241*4b9c6d91SCole Faust 
add_mount(struct minijail * j,char * arg)242*4b9c6d91SCole Faust static void add_mount(struct minijail *j, char *arg)
243*4b9c6d91SCole Faust {
244*4b9c6d91SCole Faust 	char *src = tokenize(&arg, ",");
245*4b9c6d91SCole Faust 	char *dest = tokenize(&arg, ",");
246*4b9c6d91SCole Faust 	char *type = tokenize(&arg, ",");
247*4b9c6d91SCole Faust 	char *flags = tokenize(&arg, ",");
248*4b9c6d91SCole Faust 	char *data = tokenize(&arg, ",");
249*4b9c6d91SCole Faust 	char *end;
250*4b9c6d91SCole Faust 	if (!src || src[0] == '\0' || !dest || dest[0] == '\0' || !type ||
251*4b9c6d91SCole Faust 	    type[0] == '\0') {
252*4b9c6d91SCole Faust 		errx(1, "Bad mount: %s %s %s", src, dest, type);
253*4b9c6d91SCole Faust 	}
254*4b9c6d91SCole Faust 
255*4b9c6d91SCole Faust 	/*
256*4b9c6d91SCole Faust 	 * Fun edge case: the data option itself is comma delimited.  If there
257*4b9c6d91SCole Faust 	 * were no more options, then arg would be set to NULL.  But if we had
258*4b9c6d91SCole Faust 	 * more pending, it'll be pointing to the next token.  Back up and undo
259*4b9c6d91SCole Faust 	 * the null byte so it'll be merged back.
260*4b9c6d91SCole Faust 	 * An example:
261*4b9c6d91SCole Faust 	 *   none,/tmp,tmpfs,0xe,mode=0755,uid=10,gid=10
262*4b9c6d91SCole Faust 	 * The tokenize calls above will turn this memory into:
263*4b9c6d91SCole Faust 	 *   none\0/tmp\0tmpfs\00xe\0mode=0755\0uid=10,gid=10
264*4b9c6d91SCole Faust 	 * With data pointing at mode=0755 and arg pointing at uid=10,gid=10.
265*4b9c6d91SCole Faust 	 */
266*4b9c6d91SCole Faust 	if (arg != NULL)
267*4b9c6d91SCole Faust 		arg[-1] = ',';
268*4b9c6d91SCole Faust 
269*4b9c6d91SCole Faust 	unsigned long mountflags;
270*4b9c6d91SCole Faust 	if (flags == NULL || flags[0] == '\0') {
271*4b9c6d91SCole Faust 		mountflags = 0;
272*4b9c6d91SCole Faust 	} else {
273*4b9c6d91SCole Faust 		end = NULL;
274*4b9c6d91SCole Faust 		mountflags = parse_constant(flags, &end);
275*4b9c6d91SCole Faust 		if (flags == end)
276*4b9c6d91SCole Faust 			errx(1, "Bad mount flags: %s", flags);
277*4b9c6d91SCole Faust 	}
278*4b9c6d91SCole Faust 
279*4b9c6d91SCole Faust 	if (minijail_mount_with_data(j, src, dest, type, mountflags, data))
280*4b9c6d91SCole Faust 		errx(1, "minijail_mount failed");
281*4b9c6d91SCole Faust }
282*4b9c6d91SCole Faust 
build_idmap(id_t id,id_t lowerid)283*4b9c6d91SCole Faust static char *build_idmap(id_t id, id_t lowerid)
284*4b9c6d91SCole Faust {
285*4b9c6d91SCole Faust 	int ret;
286*4b9c6d91SCole Faust 	char *idmap = xmalloc(IDMAP_LEN);
287*4b9c6d91SCole Faust 	ret = snprintf(idmap, IDMAP_LEN, "%d %d 1", id, lowerid);
288*4b9c6d91SCole Faust 	if (ret < 0 || (size_t)ret >= IDMAP_LEN) {
289*4b9c6d91SCole Faust 		free(idmap);
290*4b9c6d91SCole Faust 		errx(1, "Could not build id map");
291*4b9c6d91SCole Faust 	}
292*4b9c6d91SCole Faust 	return idmap;
293*4b9c6d91SCole Faust }
294*4b9c6d91SCole Faust 
has_cap_setgid(void)295*4b9c6d91SCole Faust static int has_cap_setgid(void)
296*4b9c6d91SCole Faust {
297*4b9c6d91SCole Faust 	cap_t caps;
298*4b9c6d91SCole Faust 	cap_flag_value_t cap_value;
299*4b9c6d91SCole Faust 
300*4b9c6d91SCole Faust 	if (!CAP_IS_SUPPORTED(CAP_SETGID))
301*4b9c6d91SCole Faust 		return 0;
302*4b9c6d91SCole Faust 
303*4b9c6d91SCole Faust 	caps = cap_get_proc();
304*4b9c6d91SCole Faust 	if (!caps)
305*4b9c6d91SCole Faust 		err(1, "Could not get process' capabilities");
306*4b9c6d91SCole Faust 
307*4b9c6d91SCole Faust 	if (cap_get_flag(caps, CAP_SETGID, CAP_EFFECTIVE, &cap_value))
308*4b9c6d91SCole Faust 		err(1, "Could not get the value of CAP_SETGID");
309*4b9c6d91SCole Faust 
310*4b9c6d91SCole Faust 	if (cap_free(caps))
311*4b9c6d91SCole Faust 		err(1, "Could not free capabilities");
312*4b9c6d91SCole Faust 
313*4b9c6d91SCole Faust 	return cap_value == CAP_SET;
314*4b9c6d91SCole Faust }
315*4b9c6d91SCole Faust 
set_ugid_mapping(struct minijail * j,int set_uidmap,uid_t uid,char * uidmap,int set_gidmap,gid_t gid,char * gidmap)316*4b9c6d91SCole Faust static void set_ugid_mapping(struct minijail *j, int set_uidmap, uid_t uid,
317*4b9c6d91SCole Faust 			     char *uidmap, int set_gidmap, gid_t gid,
318*4b9c6d91SCole Faust 			     char *gidmap)
319*4b9c6d91SCole Faust {
320*4b9c6d91SCole Faust 	if (set_uidmap) {
321*4b9c6d91SCole Faust 		minijail_namespace_user(j);
322*4b9c6d91SCole Faust 		minijail_namespace_pids(j);
323*4b9c6d91SCole Faust 
324*4b9c6d91SCole Faust 		if (!uidmap) {
325*4b9c6d91SCole Faust 			/*
326*4b9c6d91SCole Faust 			 * If no map is passed, map the current uid to the
327*4b9c6d91SCole Faust 			 * chosen uid in the target namespace (or root, if none
328*4b9c6d91SCole Faust 			 * was chosen).
329*4b9c6d91SCole Faust 			 */
330*4b9c6d91SCole Faust 			uidmap = build_idmap(uid, getuid());
331*4b9c6d91SCole Faust 		}
332*4b9c6d91SCole Faust 		if (0 != minijail_uidmap(j, uidmap))
333*4b9c6d91SCole Faust 			errx(1, "Could not set uid map");
334*4b9c6d91SCole Faust 		free(uidmap);
335*4b9c6d91SCole Faust 	}
336*4b9c6d91SCole Faust 	if (set_gidmap) {
337*4b9c6d91SCole Faust 		minijail_namespace_user(j);
338*4b9c6d91SCole Faust 		minijail_namespace_pids(j);
339*4b9c6d91SCole Faust 
340*4b9c6d91SCole Faust 		if (!gidmap) {
341*4b9c6d91SCole Faust 			/*
342*4b9c6d91SCole Faust 			 * If no map is passed, map the current gid to the
343*4b9c6d91SCole Faust 			 * chosen gid in the target namespace.
344*4b9c6d91SCole Faust 			 */
345*4b9c6d91SCole Faust 			gidmap = build_idmap(gid, getgid());
346*4b9c6d91SCole Faust 		}
347*4b9c6d91SCole Faust 		if (!has_cap_setgid()) {
348*4b9c6d91SCole Faust 			/*
349*4b9c6d91SCole Faust 			 * This means that we are not running as root,
350*4b9c6d91SCole Faust 			 * so we also have to disable setgroups(2) to
351*4b9c6d91SCole Faust 			 * be able to set the gid map.
352*4b9c6d91SCole Faust 			 * See
353*4b9c6d91SCole Faust 			 * http://man7.org/linux/man-pages/man7/user_namespaces.7.html
354*4b9c6d91SCole Faust 			 */
355*4b9c6d91SCole Faust 			minijail_namespace_user_disable_setgroups(j);
356*4b9c6d91SCole Faust 		}
357*4b9c6d91SCole Faust 		if (0 != minijail_gidmap(j, gidmap))
358*4b9c6d91SCole Faust 			errx(1, "Could not set gid map");
359*4b9c6d91SCole Faust 		free(gidmap);
360*4b9c6d91SCole Faust 	}
361*4b9c6d91SCole Faust }
362*4b9c6d91SCole Faust 
use_chroot(struct minijail * j,const char * path,int * chroot,int pivot_root)363*4b9c6d91SCole Faust static void use_chroot(struct minijail *j, const char *path, int *chroot,
364*4b9c6d91SCole Faust 		       int pivot_root)
365*4b9c6d91SCole Faust {
366*4b9c6d91SCole Faust 	if (pivot_root)
367*4b9c6d91SCole Faust 		errx(1, "Could not set chroot because -P was specified");
368*4b9c6d91SCole Faust 	if (minijail_enter_chroot(j, path))
369*4b9c6d91SCole Faust 		errx(1, "Could not set chroot");
370*4b9c6d91SCole Faust 	*chroot = 1;
371*4b9c6d91SCole Faust }
372*4b9c6d91SCole Faust 
use_pivot_root(struct minijail * j,const char * path,int * pivot_root,int chroot)373*4b9c6d91SCole Faust static void use_pivot_root(struct minijail *j, const char *path,
374*4b9c6d91SCole Faust 			   int *pivot_root, int chroot)
375*4b9c6d91SCole Faust {
376*4b9c6d91SCole Faust 	if (chroot)
377*4b9c6d91SCole Faust 		errx(1, "Could not set pivot_root because -C was specified");
378*4b9c6d91SCole Faust 	if (minijail_enter_pivot_root(j, path))
379*4b9c6d91SCole Faust 		errx(1, "Could not set pivot_root");
380*4b9c6d91SCole Faust 	minijail_namespace_vfs(j);
381*4b9c6d91SCole Faust 	*pivot_root = 1;
382*4b9c6d91SCole Faust }
383*4b9c6d91SCole Faust 
use_profile(struct minijail * j,const char * profile,int * pivot_root,int chroot,size_t * tmp_size)384*4b9c6d91SCole Faust static void use_profile(struct minijail *j, const char *profile,
385*4b9c6d91SCole Faust 			int *pivot_root, int chroot, size_t *tmp_size)
386*4b9c6d91SCole Faust {
387*4b9c6d91SCole Faust 	/* Note: New profiles should be added in minijail0_cli_unittest.cc. */
388*4b9c6d91SCole Faust 
389*4b9c6d91SCole Faust 	if (streq(profile, "minimalistic-mountns") ||
390*4b9c6d91SCole Faust 	    streq(profile, "minimalistic-mountns-nodev")) {
391*4b9c6d91SCole Faust 		minijail_namespace_vfs(j);
392*4b9c6d91SCole Faust 		if (minijail_bind(j, "/", "/", 0))
393*4b9c6d91SCole Faust 			errx(1, "minijail_bind(/) failed");
394*4b9c6d91SCole Faust 		if (minijail_bind(j, "/proc", "/proc", 0))
395*4b9c6d91SCole Faust 			errx(1, "minijail_bind(/proc) failed");
396*4b9c6d91SCole Faust 		if (streq(profile, "minimalistic-mountns")) {
397*4b9c6d91SCole Faust 			if (minijail_bind(j, "/dev/log", "/dev/log", 0))
398*4b9c6d91SCole Faust 				errx(1, "minijail_bind(/dev/log) failed");
399*4b9c6d91SCole Faust 			minijail_mount_dev(j);
400*4b9c6d91SCole Faust 		}
401*4b9c6d91SCole Faust 		if (!*tmp_size) {
402*4b9c6d91SCole Faust 			/* Avoid clobbering |tmp_size| if it was already set. */
403*4b9c6d91SCole Faust 			*tmp_size = DEFAULT_TMP_SIZE;
404*4b9c6d91SCole Faust 		}
405*4b9c6d91SCole Faust 		minijail_remount_proc_readonly(j);
406*4b9c6d91SCole Faust 		minijail_set_using_minimalistic_mountns(j);
407*4b9c6d91SCole Faust 		use_pivot_root(j, DEFAULT_PIVOT_ROOT, pivot_root, chroot);
408*4b9c6d91SCole Faust 	} else
409*4b9c6d91SCole Faust 		errx(1, "Unrecognized profile name '%s'", profile);
410*4b9c6d91SCole Faust }
411*4b9c6d91SCole Faust 
set_remount_mode(struct minijail * j,const char * mode)412*4b9c6d91SCole Faust static void set_remount_mode(struct minijail *j, const char *mode)
413*4b9c6d91SCole Faust {
414*4b9c6d91SCole Faust 	unsigned long msmode;
415*4b9c6d91SCole Faust 	if (streq(mode, "shared"))
416*4b9c6d91SCole Faust 		msmode = MS_SHARED;
417*4b9c6d91SCole Faust 	else if (streq(mode, "private"))
418*4b9c6d91SCole Faust 		msmode = MS_PRIVATE;
419*4b9c6d91SCole Faust 	else if (streq(mode, "slave"))
420*4b9c6d91SCole Faust 		msmode = MS_SLAVE;
421*4b9c6d91SCole Faust 	else if (streq(mode, "unbindable"))
422*4b9c6d91SCole Faust 		msmode = MS_UNBINDABLE;
423*4b9c6d91SCole Faust 	else
424*4b9c6d91SCole Faust 		errx(1, "Unknown remount mode: '%s'", mode);
425*4b9c6d91SCole Faust 	minijail_remount_mode(j, msmode);
426*4b9c6d91SCole Faust }
427*4b9c6d91SCole Faust 
read_seccomp_filter(const char * filter_path,struct sock_fprog * filter)428*4b9c6d91SCole Faust static void read_seccomp_filter(const char *filter_path,
429*4b9c6d91SCole Faust 				struct sock_fprog *filter)
430*4b9c6d91SCole Faust {
431*4b9c6d91SCole Faust 	attribute_cleanup_fp FILE *f = fopen(filter_path, "re");
432*4b9c6d91SCole Faust 	if (!f)
433*4b9c6d91SCole Faust 		err(1, "failed to open %s", filter_path);
434*4b9c6d91SCole Faust 	off_t filter_size = 0;
435*4b9c6d91SCole Faust 	if (fseeko(f, 0, SEEK_END) == -1 || (filter_size = ftello(f)) == -1)
436*4b9c6d91SCole Faust 		err(1, "failed to get file size of %s", filter_path);
437*4b9c6d91SCole Faust 	if (filter_size % sizeof(struct sock_filter) != 0) {
438*4b9c6d91SCole Faust 		errx(1,
439*4b9c6d91SCole Faust 		     "filter size (%" PRId64 ") of %s is not a multiple of"
440*4b9c6d91SCole Faust 		     " %zu",
441*4b9c6d91SCole Faust 		     filter_size, filter_path, sizeof(struct sock_filter));
442*4b9c6d91SCole Faust 	}
443*4b9c6d91SCole Faust 	rewind(f);
444*4b9c6d91SCole Faust 
445*4b9c6d91SCole Faust 	filter->len = filter_size / sizeof(struct sock_filter);
446*4b9c6d91SCole Faust 	filter->filter = xmalloc(filter_size);
447*4b9c6d91SCole Faust 	if (fread(filter->filter, sizeof(struct sock_filter), filter->len, f) !=
448*4b9c6d91SCole Faust 	    filter->len) {
449*4b9c6d91SCole Faust 		err(1, "failed read %s", filter_path);
450*4b9c6d91SCole Faust 	}
451*4b9c6d91SCole Faust }
452*4b9c6d91SCole Faust 
453*4b9c6d91SCole Faust /*
454*4b9c6d91SCole Faust  * Long options use values starting at 0x100 so that they're out of range of
455*4b9c6d91SCole Faust  * bytes which is how command line options are processed.  Practically speaking,
456*4b9c6d91SCole Faust  * we could get by with the (7-bit) ASCII range, but UTF-8 codepoints would be a
457*4b9c6d91SCole Faust  * bit confusing, and honestly there's no reason to "optimize" here.
458*4b9c6d91SCole Faust  *
459*4b9c6d91SCole Faust  * The long enum values are internal to this file and can freely change at any
460*4b9c6d91SCole Faust  * time without breaking anything.  Please keep alphabetically ordered.
461*4b9c6d91SCole Faust  */
462*4b9c6d91SCole Faust enum {
463*4b9c6d91SCole Faust 	/* Everything after this point only have long options. */
464*4b9c6d91SCole Faust 	LONG_OPTION_BASE = 0x100,
465*4b9c6d91SCole Faust 	OPT_ADD_SUPPL_GROUP,
466*4b9c6d91SCole Faust 	OPT_ALLOW_SPECULATIVE_EXECUTION,
467*4b9c6d91SCole Faust 	OPT_AMBIENT,
468*4b9c6d91SCole Faust 	OPT_CONFIG,
469*4b9c6d91SCole Faust 	OPT_ENV_ADD,
470*4b9c6d91SCole Faust 	OPT_ENV_RESET,
471*4b9c6d91SCole Faust 	OPT_FS_DEFAULT_PATHS,
472*4b9c6d91SCole Faust 	OPT_FS_PATH_RX,
473*4b9c6d91SCole Faust 	OPT_FS_PATH_RO,
474*4b9c6d91SCole Faust 	OPT_FS_PATH_RW,
475*4b9c6d91SCole Faust 	OPT_FS_PATH_ADVANCED_RW,
476*4b9c6d91SCole Faust 	OPT_LOGGING,
477*4b9c6d91SCole Faust 	OPT_PRELOAD_LIBRARY,
478*4b9c6d91SCole Faust 	OPT_PROFILE,
479*4b9c6d91SCole Faust 	OPT_SECCOMP_BPF_BINARY,
480*4b9c6d91SCole Faust 	OPT_UTS,
481*4b9c6d91SCole Faust };
482*4b9c6d91SCole Faust 
483*4b9c6d91SCole Faust /*
484*4b9c6d91SCole Faust  * NB: When adding new options, prefer long-option only.  Add a short option
485*4b9c6d91SCole Faust  * only if its meaning is intuitive/obvious at a glance.
486*4b9c6d91SCole Faust  *
487*4b9c6d91SCole Faust  * Keep this sorted.
488*4b9c6d91SCole Faust  */
489*4b9c6d91SCole Faust static const char optstring[] =
490*4b9c6d91SCole Faust     "+a:b:c:de::f:g:hik:lm::nprst::u:vwyzB:C:GHIK::LM::NP:R:S:T:UV:Y";
491*4b9c6d91SCole Faust 
492*4b9c6d91SCole Faust static const struct option long_options[] = {
493*4b9c6d91SCole Faust     {"help", no_argument, 0, 'h'},
494*4b9c6d91SCole Faust     {"mount-dev", no_argument, 0, 'd'},
495*4b9c6d91SCole Faust     {"ambient", no_argument, 0, OPT_AMBIENT},
496*4b9c6d91SCole Faust     {"uts", optional_argument, 0, OPT_UTS},
497*4b9c6d91SCole Faust     {"logging", required_argument, 0, OPT_LOGGING},
498*4b9c6d91SCole Faust     {"profile", required_argument, 0, OPT_PROFILE},
499*4b9c6d91SCole Faust     {"preload-library", required_argument, 0, OPT_PRELOAD_LIBRARY},
500*4b9c6d91SCole Faust     {"seccomp-bpf-binary", required_argument, 0, OPT_SECCOMP_BPF_BINARY},
501*4b9c6d91SCole Faust     {"add-suppl-group", required_argument, 0, OPT_ADD_SUPPL_GROUP},
502*4b9c6d91SCole Faust     {"allow-speculative-execution", no_argument, 0,
503*4b9c6d91SCole Faust      OPT_ALLOW_SPECULATIVE_EXECUTION},
504*4b9c6d91SCole Faust     {"config", required_argument, 0, OPT_CONFIG},
505*4b9c6d91SCole Faust     {"env-add", required_argument, 0, OPT_ENV_ADD},
506*4b9c6d91SCole Faust     {"env-reset", no_argument, 0, OPT_ENV_RESET},
507*4b9c6d91SCole Faust     {"mount", required_argument, 0, 'k'},
508*4b9c6d91SCole Faust     {"bind-mount", required_argument, 0, 'b'},
509*4b9c6d91SCole Faust     {"ns-mount", no_argument, 0, 'v'},
510*4b9c6d91SCole Faust     {"fs-default-paths", no_argument, 0, OPT_FS_DEFAULT_PATHS},
511*4b9c6d91SCole Faust     {"fs-path-rx", required_argument, 0, OPT_FS_PATH_RX},
512*4b9c6d91SCole Faust     {"fs-path-ro", required_argument, 0, OPT_FS_PATH_RO},
513*4b9c6d91SCole Faust     {"fs-path-rw", required_argument, 0, OPT_FS_PATH_RW},
514*4b9c6d91SCole Faust     {"fs-path-advanced-rw", required_argument, 0, OPT_FS_PATH_ADVANCED_RW},
515*4b9c6d91SCole Faust     {0, 0, 0, 0},
516*4b9c6d91SCole Faust };
517*4b9c6d91SCole Faust 
518*4b9c6d91SCole Faust /*
519*4b9c6d91SCole Faust  * Pull the usage string out into the top-level to help with long-lines.  We
520*4b9c6d91SCole Faust  * want the output to be wrapped at 80 cols when it's shown to the user in the
521*4b9c6d91SCole Faust  * terminal, but we don't want the source wrapped to 80 cols because that will
522*4b9c6d91SCole Faust  * effectively make terminal output wrap to much lower levels (like <70).
523*4b9c6d91SCole Faust  */
524*4b9c6d91SCole Faust /* clang-format off */
525*4b9c6d91SCole Faust static const char help_text[] =
526*4b9c6d91SCole Faust "Account (user/group) options:\n"
527*4b9c6d91SCole Faust "  -u <user>    Change uid to <user>.\n"
528*4b9c6d91SCole Faust "  -g <group>   Change gid to <group>.\n"
529*4b9c6d91SCole Faust "  -G           Inherit supplementary groups from new uid.\n"
530*4b9c6d91SCole Faust "               Incompatible with -y or --add-suppl-group.\n"
531*4b9c6d91SCole Faust "  -y           Keep original uid's supplementary groups.\n"
532*4b9c6d91SCole Faust "               Incompatible with -G or --add-suppl-group.\n"
533*4b9c6d91SCole Faust "  --add-suppl-group <group>\n"
534*4b9c6d91SCole Faust "               Add <group> to the proccess' supplementary groups.\n"
535*4b9c6d91SCole Faust "               Can be specified multiple times to add several groups.\n"
536*4b9c6d91SCole Faust "               Incompatible with -y or -G.\n"
537*4b9c6d91SCole Faust "\n"
538*4b9c6d91SCole Faust "Mount/path options:\n"
539*4b9c6d91SCole Faust "  -b <src[,dst[,writable]]>, --bind-mount <...>\n"
540*4b9c6d91SCole Faust "               Bind <src> to <dst>.\n"
541*4b9c6d91SCole Faust "  -k <src,dst,fstype[,flags[,data]]>, --mount <...>\n"
542*4b9c6d91SCole Faust "               Mount <src> at <dst>. <flags> and <data> can be specified as\n"
543*4b9c6d91SCole Faust "               in mount(2). Multiple instances allowed.\n"
544*4b9c6d91SCole Faust "  -K           Do not change share mode of any existing mounts.\n"
545*4b9c6d91SCole Faust "  -K<mode>     Mark all existing mounts as <mode> instead of MS_PRIVATE.\n"
546*4b9c6d91SCole Faust "  -r           Remount /proc read-only (implies -v).\n"
547*4b9c6d91SCole Faust "  -d, --mount-dev\n"
548*4b9c6d91SCole Faust "               Create a new /dev with a minimal set of device nodes\n"
549*4b9c6d91SCole Faust "               (implies -v). See minijail0(1) for exact list.\n"
550*4b9c6d91SCole Faust "  -t[size]     Mount tmpfs at /tmp (implies -v).\n"
551*4b9c6d91SCole Faust "               Optional argument specifies size (default \"64M\").\n"
552*4b9c6d91SCole Faust "  -C <dir>     chroot(2) to <dir>. Incompatible with -P.\n"
553*4b9c6d91SCole Faust "  -P <dir>     pivot_root(2) to <dir> (implies -v). Incompatible with -C.\n"
554*4b9c6d91SCole Faust "\n"
555*4b9c6d91SCole Faust "Namespace options:\n"
556*4b9c6d91SCole Faust "  -N           Enter a new cgroup namespace.\n"
557*4b9c6d91SCole Faust "  -l           Enter new IPC namespace.\n"
558*4b9c6d91SCole Faust "  -v, --ns-mount\n"
559*4b9c6d91SCole Faust "               Enter new mount namespace.\n"
560*4b9c6d91SCole Faust "  -V <file>    Enter specified mount namespace.\n"
561*4b9c6d91SCole Faust "  -e[file]     Enter new network namespace, or existing |file| if provided.\n"
562*4b9c6d91SCole Faust "  -p           Enter new pid namespace (implies -vr).\n"
563*4b9c6d91SCole Faust "  -I           Run as init (pid 1) inside a new pid namespace (implies -p).\n"
564*4b9c6d91SCole Faust "  -U           Enter new user namespace (implies -p).\n"
565*4b9c6d91SCole Faust "  -m[<uid> <loweruid> <count>]\n"
566*4b9c6d91SCole Faust "               Set the uid map of a user namespace (implies -pU).\n"
567*4b9c6d91SCole Faust "               Same arguments as newuidmap(1); mappings are comma separated.\n"
568*4b9c6d91SCole Faust "               With no mapping, map the current uid to root.\n"
569*4b9c6d91SCole Faust "               Incompatible with -b without the 'writable' option.\n"
570*4b9c6d91SCole Faust "  -M[<gid> <lowergid> <count>]\n"
571*4b9c6d91SCole Faust "               Set the gid map of a user namespace (implies -pU).\n"
572*4b9c6d91SCole Faust "               Same arguments as newgidmap(1); mappings are comma separated.\n"
573*4b9c6d91SCole Faust "               With no mapping, map the current gid to root.\n"
574*4b9c6d91SCole Faust "               Incompatible with -b without the 'writable' option.\n"
575*4b9c6d91SCole Faust "  --uts[=name] Enter a new UTS namespace (and set hostname).\n"
576*4b9c6d91SCole Faust "\n"
577*4b9c6d91SCole Faust "Seccomp options:\n"
578*4b9c6d91SCole Faust "  -S <file>    Set seccomp filter using <file>.\n"
579*4b9c6d91SCole Faust "               E.g., '-S /usr/share/filters/<prog>.$(uname -m)'.\n"
580*4b9c6d91SCole Faust "               Requires -n when not running as root.\n"
581*4b9c6d91SCole Faust "  --seccomp-bpf-binary=<f>\n"
582*4b9c6d91SCole Faust "               Set a pre-compiled seccomp filter using <f>.\n"
583*4b9c6d91SCole Faust "               E.g., '-S /usr/share/filters/<prog>.$(uname -m).bpf'.\n"
584*4b9c6d91SCole Faust "               Requires -n when not running as root.\n"
585*4b9c6d91SCole Faust "               The user is responsible for ensuring that the binary\n"
586*4b9c6d91SCole Faust "               was compiled for the correct architecture / kernel version.\n"
587*4b9c6d91SCole Faust "  -L           Report blocked syscalls when using seccomp filter.\n"
588*4b9c6d91SCole Faust "               If the kernel does not support SECCOMP_RET_LOG, some syscalls\n"
589*4b9c6d91SCole Faust "               will automatically be allowed (see below).\n"
590*4b9c6d91SCole Faust "  -Y           Synchronize seccomp filters across thread group.\n"
591*4b9c6d91SCole Faust "  -a <table>   Use alternate syscall table <table>.\n"
592*4b9c6d91SCole Faust "  -s           Use seccomp mode 1 (not the same as -S).\n"
593*4b9c6d91SCole Faust "\n"
594*4b9c6d91SCole Faust "Other options:\n"
595*4b9c6d91SCole Faust "  --config <file>\n"
596*4b9c6d91SCole Faust "               Load the Minijail configuration file <file>.\n"
597*4b9c6d91SCole Faust "               If used, must be specified ahead of other options.\n"
598*4b9c6d91SCole Faust "  --profile <p>\n"
599*4b9c6d91SCole Faust "               Configure minijail0 to run with the <p> sandboxing profile,\n"
600*4b9c6d91SCole Faust "               which is a convenient way to express multiple flags\n"
601*4b9c6d91SCole Faust "               that are typically used together.\n"
602*4b9c6d91SCole Faust "               See the minijail0(1) man page for the full list.\n"
603*4b9c6d91SCole Faust "  -n           Set no_new_privs. See prctl(2) for details.\n"
604*4b9c6d91SCole Faust "  -c <caps>    Restrict caps to <caps>.\n"
605*4b9c6d91SCole Faust "  --ambient    Raise ambient capabilities. Requires -c.\n"
606*4b9c6d91SCole Faust "  -B <mask>    Skip setting <mask> securebits when restricting caps (-c).\n"
607*4b9c6d91SCole Faust "               By default, SECURE_NOROOT, SECURE_NO_SETUID_FIXUP, and \n"
608*4b9c6d91SCole Faust "               SECURE_KEEP_CAPS (with their respective locks) are set.\n"
609*4b9c6d91SCole Faust "  -f <file>    Write the pid of the jailed process to <file>.\n"
610*4b9c6d91SCole Faust "  -i           Exit immediately after fork(2); i.e. background the program.\n"
611*4b9c6d91SCole Faust "  -z           Don't forward signals to jailed process.\n"
612*4b9c6d91SCole Faust "  -R <type,cur,max>\n"
613*4b9c6d91SCole Faust "               Call setrlimit(3); can be specified multiple times.\n"
614*4b9c6d91SCole Faust "  -T <type>    Assume <program> is a <type> ELF binary;\n"
615*4b9c6d91SCole Faust "               <type> may be 'static' or 'dynamic'.\n"
616*4b9c6d91SCole Faust "               This will avoid accessing <program> binary before execve(2).\n"
617*4b9c6d91SCole Faust "               Type 'static' will avoid preload hooking.\n"
618*4b9c6d91SCole Faust "  -w           Create and join a new anonymous session keyring.\n"
619*4b9c6d91SCole Faust "  --env-reset  Clear the current environment instead of having <program>\n"
620*4b9c6d91SCole Faust "               inherit the active environment. Often used to start <program>\n"
621*4b9c6d91SCole Faust "               with a minimal sanitized environment.\n"
622*4b9c6d91SCole Faust "  --env-add <NAME=value>\n"
623*4b9c6d91SCole Faust "               Sets the specified environment variable <NAME>\n"
624*4b9c6d91SCole Faust "               in the <program>'s environment before starting it.\n"
625*4b9c6d91SCole Faust "\n"
626*4b9c6d91SCole Faust "Uncommon options:\n"
627*4b9c6d91SCole Faust "  --allow-speculative-execution\n"
628*4b9c6d91SCole Faust "               Allow speculative execution by disabling mitigations.\n"
629*4b9c6d91SCole Faust "  --fs-default-paths\n"
630*4b9c6d91SCole Faust "               Adds a set of allowed paths to allow running common system \n"
631*4b9c6d91SCole Faust "               executables.\n"
632*4b9c6d91SCole Faust "  --fs-path-rx\n"
633*4b9c6d91SCole Faust "               Adds an allowed read-execute path.\n"
634*4b9c6d91SCole Faust "  --fs-path-ro\n"
635*4b9c6d91SCole Faust "               Adds an allowed read-only path.\n"
636*4b9c6d91SCole Faust "  --fs-path-rw\n"
637*4b9c6d91SCole Faust "               Adds an allowed read-write path.\n"
638*4b9c6d91SCole Faust "  --fs-path-advanced-rw\n"
639*4b9c6d91SCole Faust "               Adds an allowed advanced read-write path.\n"
640*4b9c6d91SCole Faust "  --preload-library=<file>\n"
641*4b9c6d91SCole Faust "               Overrides the path to \"" PRELOADPATH "\".\n"
642*4b9c6d91SCole Faust "               This is only really useful for local testing.\n"
643*4b9c6d91SCole Faust "  --logging=<output>\n"
644*4b9c6d91SCole Faust "               Set the logging system output: 'auto' (default),\n"
645*4b9c6d91SCole Faust "               'syslog', or 'stderr'.\n"
646*4b9c6d91SCole Faust "  -h           Help (this message).\n"
647*4b9c6d91SCole Faust "  -H           Seccomp filter help message.\n";
648*4b9c6d91SCole Faust /* clang-format on */
649*4b9c6d91SCole Faust 
usage(const char * progn)650*4b9c6d91SCole Faust static void usage(const char *progn)
651*4b9c6d91SCole Faust {
652*4b9c6d91SCole Faust 	printf("Usage: %s [options] [--] <program> [args...]\n\n%s", progn,
653*4b9c6d91SCole Faust 	       help_text);
654*4b9c6d91SCole Faust 
655*4b9c6d91SCole Faust 	printf("\nsyscalls allowed when logging (-L):\n ");
656*4b9c6d91SCole Faust 	for (size_t i = 0; i < log_syscalls_len; ++i)
657*4b9c6d91SCole Faust 		printf(" %s", log_syscalls[i]);
658*4b9c6d91SCole Faust 	printf("\n");
659*4b9c6d91SCole Faust }
660*4b9c6d91SCole Faust 
seccomp_filter_usage(const char * progn)661*4b9c6d91SCole Faust static void seccomp_filter_usage(const char *progn)
662*4b9c6d91SCole Faust {
663*4b9c6d91SCole Faust 	const struct syscall_entry *entry = syscall_table;
664*4b9c6d91SCole Faust 	printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
665*4b9c6d91SCole Faust 	       "System call names supported:\n",
666*4b9c6d91SCole Faust 	       progn);
667*4b9c6d91SCole Faust 	for (; entry->name && entry->nr >= 0; ++entry)
668*4b9c6d91SCole Faust 		printf("  %s [%d]\n", entry->name, entry->nr);
669*4b9c6d91SCole Faust 	printf("\nSee minijail0(5) for example policies.\n");
670*4b9c6d91SCole Faust }
671*4b9c6d91SCole Faust 
672*4b9c6d91SCole Faust /*
673*4b9c6d91SCole Faust  * Return the next unconsumed option char/value parsed from
674*4b9c6d91SCole Faust  * |*conf_entry_list|. |optarg| is updated to point to an argument from
675*4b9c6d91SCole Faust  * the entry value. If all options have been consumed, |*conf_entry_list|
676*4b9c6d91SCole Faust  * will be freed and -1 will be returned.
677*4b9c6d91SCole Faust  */
getopt_from_conf(const struct option * longopts,struct config_entry_list ** conf_entry_list,size_t * conf_index)678*4b9c6d91SCole Faust static int getopt_from_conf(const struct option *longopts,
679*4b9c6d91SCole Faust 			    struct config_entry_list **conf_entry_list,
680*4b9c6d91SCole Faust 			    size_t *conf_index)
681*4b9c6d91SCole Faust {
682*4b9c6d91SCole Faust 	int opt = -1;
683*4b9c6d91SCole Faust 	/* If we've consumed all the options in the this config, reset it. */
684*4b9c6d91SCole Faust 	if (*conf_index >= (*conf_entry_list)->num_entries) {
685*4b9c6d91SCole Faust 		free_config_entry_list(*conf_entry_list);
686*4b9c6d91SCole Faust 		*conf_entry_list = NULL;
687*4b9c6d91SCole Faust 		*conf_index = 0;
688*4b9c6d91SCole Faust 		return opt;
689*4b9c6d91SCole Faust 	}
690*4b9c6d91SCole Faust 
691*4b9c6d91SCole Faust 	struct config_entry *entry = &(*conf_entry_list)->entries[*conf_index];
692*4b9c6d91SCole Faust 	/* Look up a matching long option. */
693*4b9c6d91SCole Faust 	size_t i = 0;
694*4b9c6d91SCole Faust 	const struct option *curr_opt;
695*4b9c6d91SCole Faust 	for (curr_opt = &longopts[0]; curr_opt->name != NULL;
696*4b9c6d91SCole Faust 	     curr_opt = &longopts[++i])
697*4b9c6d91SCole Faust 		if (streq(entry->key, curr_opt->name))
698*4b9c6d91SCole Faust 			break;
699*4b9c6d91SCole Faust 	if (curr_opt->name == NULL) {
700*4b9c6d91SCole Faust 		errx(1,
701*4b9c6d91SCole Faust 		     "Unable to recognize '%s' as Minijail conf entry key, "
702*4b9c6d91SCole Faust 		     "please refer to minijail0(5) for syntax and examples.",
703*4b9c6d91SCole Faust 		     entry->key);
704*4b9c6d91SCole Faust 	}
705*4b9c6d91SCole Faust 	opt = curr_opt->val;
706*4b9c6d91SCole Faust 	optarg = (char *)entry->value;
707*4b9c6d91SCole Faust 	(*conf_index)++;
708*4b9c6d91SCole Faust 	return opt;
709*4b9c6d91SCole Faust }
710*4b9c6d91SCole Faust 
711*4b9c6d91SCole Faust /*
712*4b9c6d91SCole Faust  * Similar to getopt(3), return the next option char/value as it
713*4b9c6d91SCole Faust  * parses through the CLI argument list. Config entries in
714*4b9c6d91SCole Faust  * |*conf_entry_list| will be parsed with precendences over cli options.
715*4b9c6d91SCole Faust  * Same as getopt(3), |optarg| is pointing to the option argument.
716*4b9c6d91SCole Faust  */
getopt_conf_or_cli(int argc,char * const argv[],struct config_entry_list ** conf_entry_list,size_t * conf_index)717*4b9c6d91SCole Faust static int getopt_conf_or_cli(int argc, char *const argv[],
718*4b9c6d91SCole Faust 			      struct config_entry_list **conf_entry_list,
719*4b9c6d91SCole Faust 			      size_t *conf_index)
720*4b9c6d91SCole Faust {
721*4b9c6d91SCole Faust 	int opt = -1;
722*4b9c6d91SCole Faust 	if (*conf_entry_list != NULL)
723*4b9c6d91SCole Faust 		opt =
724*4b9c6d91SCole Faust 		    getopt_from_conf(long_options, conf_entry_list, conf_index);
725*4b9c6d91SCole Faust 	if (opt == -1)
726*4b9c6d91SCole Faust 		opt = getopt_long(argc, argv, optstring, long_options, NULL);
727*4b9c6d91SCole Faust 	return opt;
728*4b9c6d91SCole Faust }
729*4b9c6d91SCole Faust 
set_child_env(char *** envp,char * arg,char * const environ[])730*4b9c6d91SCole Faust static void set_child_env(char ***envp, char *arg, char *const environ[])
731*4b9c6d91SCole Faust {
732*4b9c6d91SCole Faust 	/* We expect VAR=value format for arg. */
733*4b9c6d91SCole Faust 	char *delim = strchr(arg, '=');
734*4b9c6d91SCole Faust 	if (!delim) {
735*4b9c6d91SCole Faust 		errx(1, "Expected an argument of the "
736*4b9c6d91SCole Faust 		        "form VAR=value (got '%s')", arg);
737*4b9c6d91SCole Faust 	}
738*4b9c6d91SCole Faust 	*delim = '\0';
739*4b9c6d91SCole Faust 	const char *env_value = delim + 1;
740*4b9c6d91SCole Faust 	if (!*envp) {
741*4b9c6d91SCole Faust 		/*
742*4b9c6d91SCole Faust 		 * We got our first --env-add. Initialize *envp by
743*4b9c6d91SCole Faust 		 * copying our current env to the future child env.
744*4b9c6d91SCole Faust 		 */
745*4b9c6d91SCole Faust 		*envp = minijail_copy_env(environ);
746*4b9c6d91SCole Faust 		if (!*envp)
747*4b9c6d91SCole Faust 			err(1, "Failed to allocate memory.");
748*4b9c6d91SCole Faust 	}
749*4b9c6d91SCole Faust 	if (minijail_setenv(envp, arg, env_value, 1))
750*4b9c6d91SCole Faust 		err(1, "minijail_setenv() failed.");
751*4b9c6d91SCole Faust }
752*4b9c6d91SCole Faust 
parse_args(struct minijail * j,int argc,char * const argv[],char * const environ[],int * exit_immediately,ElfType * elftype,const char ** preload_path,char *** envp)753*4b9c6d91SCole Faust int parse_args(struct minijail *j, int argc, char *const argv[],
754*4b9c6d91SCole Faust 	       char *const environ[], int *exit_immediately,
755*4b9c6d91SCole Faust 	       ElfType *elftype, const char **preload_path,
756*4b9c6d91SCole Faust 	       char ***envp)
757*4b9c6d91SCole Faust {
758*4b9c6d91SCole Faust 	enum seccomp_type { None, Strict, Filter, BpfBinaryFilter };
759*4b9c6d91SCole Faust 	enum seccomp_type seccomp = None;
760*4b9c6d91SCole Faust 	int opt;
761*4b9c6d91SCole Faust 	int use_seccomp_filter = 0;
762*4b9c6d91SCole Faust 	int use_seccomp_filter_binary = 0;
763*4b9c6d91SCole Faust 	int use_seccomp_log = 0;
764*4b9c6d91SCole Faust 	int forward = 1;
765*4b9c6d91SCole Faust 	int binding = 0;
766*4b9c6d91SCole Faust 	int chroot = 0, pivot_root = 0;
767*4b9c6d91SCole Faust 	int mount_ns = 0, change_remount = 0;
768*4b9c6d91SCole Faust 	const char *remount_mode = NULL;
769*4b9c6d91SCole Faust 	int inherit_suppl_gids = 0, keep_suppl_gids = 0;
770*4b9c6d91SCole Faust 	int caps = 0, ambient_caps = 0;
771*4b9c6d91SCole Faust 	bool use_uid = false, use_gid = false;
772*4b9c6d91SCole Faust 	uid_t uid = 0;
773*4b9c6d91SCole Faust 	gid_t gid = 0;
774*4b9c6d91SCole Faust 	gid_t *suppl_gids = NULL;
775*4b9c6d91SCole Faust 	size_t suppl_gids_count = 0;
776*4b9c6d91SCole Faust 	char *uidmap = NULL, *gidmap = NULL;
777*4b9c6d91SCole Faust 	int set_uidmap = 0, set_gidmap = 0;
778*4b9c6d91SCole Faust 	size_t tmp_size = 0;
779*4b9c6d91SCole Faust 	const char *filter_path = NULL;
780*4b9c6d91SCole Faust 	int log_to_stderr = -1;
781*4b9c6d91SCole Faust 	struct config_entry_list *conf_entry_list = NULL;
782*4b9c6d91SCole Faust 	size_t conf_index = 0;
783*4b9c6d91SCole Faust 
784*4b9c6d91SCole Faust 	while ((opt = getopt_conf_or_cli(argc, argv, &conf_entry_list,
785*4b9c6d91SCole Faust 					 &conf_index)) != -1) {
786*4b9c6d91SCole Faust 		switch (opt) {
787*4b9c6d91SCole Faust 		case 'u':
788*4b9c6d91SCole Faust 			if (use_uid)
789*4b9c6d91SCole Faust 				errx(1, "-u provided multiple times.");
790*4b9c6d91SCole Faust 			use_uid = true;
791*4b9c6d91SCole Faust 			set_user(j, optarg, &uid, &gid);
792*4b9c6d91SCole Faust 			break;
793*4b9c6d91SCole Faust 		case 'g':
794*4b9c6d91SCole Faust 			if (use_gid)
795*4b9c6d91SCole Faust 				errx(1, "-g provided multiple times.");
796*4b9c6d91SCole Faust 			use_gid = true;
797*4b9c6d91SCole Faust 			set_group(j, optarg, &gid);
798*4b9c6d91SCole Faust 			break;
799*4b9c6d91SCole Faust 		case 'n':
800*4b9c6d91SCole Faust 			minijail_no_new_privs(j);
801*4b9c6d91SCole Faust 			break;
802*4b9c6d91SCole Faust 		case 's':
803*4b9c6d91SCole Faust 			if (seccomp != None && seccomp != Strict) {
804*4b9c6d91SCole Faust 				errx(1, "Do not use -s, -S, or "
805*4b9c6d91SCole Faust 					"--seccomp-bpf-binary together");
806*4b9c6d91SCole Faust 			}
807*4b9c6d91SCole Faust 			seccomp = Strict;
808*4b9c6d91SCole Faust 			minijail_use_seccomp(j);
809*4b9c6d91SCole Faust 			break;
810*4b9c6d91SCole Faust 		case 'S':
811*4b9c6d91SCole Faust 			if (seccomp != None && seccomp != Filter) {
812*4b9c6d91SCole Faust 				errx(1, "Do not use -s, -S, or "
813*4b9c6d91SCole Faust 					"--seccomp-bpf-binary together");
814*4b9c6d91SCole Faust 			}
815*4b9c6d91SCole Faust 			seccomp = Filter;
816*4b9c6d91SCole Faust 			minijail_use_seccomp_filter(j);
817*4b9c6d91SCole Faust 			filter_path = optarg;
818*4b9c6d91SCole Faust 			use_seccomp_filter = 1;
819*4b9c6d91SCole Faust 			break;
820*4b9c6d91SCole Faust 		case 'l':
821*4b9c6d91SCole Faust 			minijail_namespace_ipc(j);
822*4b9c6d91SCole Faust 			break;
823*4b9c6d91SCole Faust 		case 'L':
824*4b9c6d91SCole Faust 			if (seccomp == BpfBinaryFilter) {
825*4b9c6d91SCole Faust 				errx(1, "-L does not work with "
826*4b9c6d91SCole Faust 					"--seccomp-bpf-binary");
827*4b9c6d91SCole Faust 			}
828*4b9c6d91SCole Faust 			use_seccomp_log = 1;
829*4b9c6d91SCole Faust 			minijail_log_seccomp_filter_failures(j);
830*4b9c6d91SCole Faust 			break;
831*4b9c6d91SCole Faust 		case 'b':
832*4b9c6d91SCole Faust 			add_binding(j, optarg);
833*4b9c6d91SCole Faust 			binding = 1;
834*4b9c6d91SCole Faust 			break;
835*4b9c6d91SCole Faust 		case 'B':
836*4b9c6d91SCole Faust 			skip_securebits(j, optarg);
837*4b9c6d91SCole Faust 			break;
838*4b9c6d91SCole Faust 		case 'c':
839*4b9c6d91SCole Faust 			caps = 1;
840*4b9c6d91SCole Faust 			use_caps(j, optarg);
841*4b9c6d91SCole Faust 			break;
842*4b9c6d91SCole Faust 		case 'C':
843*4b9c6d91SCole Faust 			use_chroot(j, optarg, &chroot, pivot_root);
844*4b9c6d91SCole Faust 			break;
845*4b9c6d91SCole Faust 		case 'k':
846*4b9c6d91SCole Faust 			add_mount(j, optarg);
847*4b9c6d91SCole Faust 			break;
848*4b9c6d91SCole Faust 		case 'K':
849*4b9c6d91SCole Faust 			remount_mode = optarg;
850*4b9c6d91SCole Faust 			change_remount = 1;
851*4b9c6d91SCole Faust 			break;
852*4b9c6d91SCole Faust 		case 'P':
853*4b9c6d91SCole Faust 			use_pivot_root(j, optarg, &pivot_root, chroot);
854*4b9c6d91SCole Faust 			break;
855*4b9c6d91SCole Faust 		case 'f':
856*4b9c6d91SCole Faust 			if (0 != minijail_write_pid_file(j, optarg))
857*4b9c6d91SCole Faust 				errx(1, "Could not prepare pid file path");
858*4b9c6d91SCole Faust 			break;
859*4b9c6d91SCole Faust 		case 't':
860*4b9c6d91SCole Faust 			minijail_namespace_vfs(j);
861*4b9c6d91SCole Faust 			if (!tmp_size) {
862*4b9c6d91SCole Faust 				/*
863*4b9c6d91SCole Faust 				 * Avoid clobbering |tmp_size| if it was already
864*4b9c6d91SCole Faust 				 * set.
865*4b9c6d91SCole Faust 				 */
866*4b9c6d91SCole Faust 				tmp_size = DEFAULT_TMP_SIZE;
867*4b9c6d91SCole Faust 			}
868*4b9c6d91SCole Faust 			if (optarg != NULL &&
869*4b9c6d91SCole Faust 			    0 != parse_size(&tmp_size, optarg)) {
870*4b9c6d91SCole Faust 				errx(1, "Invalid /tmp tmpfs size");
871*4b9c6d91SCole Faust 			}
872*4b9c6d91SCole Faust 			break;
873*4b9c6d91SCole Faust 		case 'v':
874*4b9c6d91SCole Faust 			minijail_namespace_vfs(j);
875*4b9c6d91SCole Faust 			/*
876*4b9c6d91SCole Faust 			 * Set the default mount propagation in the command-line
877*4b9c6d91SCole Faust 			 * tool to MS_SLAVE.
878*4b9c6d91SCole Faust 			 *
879*4b9c6d91SCole Faust 			 * When executing the sandboxed program in a new mount
880*4b9c6d91SCole Faust 			 * namespace the Minijail library will by default
881*4b9c6d91SCole Faust 			 * remount all mounts with the MS_PRIVATE flag. While
882*4b9c6d91SCole Faust 			 * this is an appropriate, safe default for the library,
883*4b9c6d91SCole Faust 			 * MS_PRIVATE can be problematic: unmount events will
884*4b9c6d91SCole Faust 			 * not propagate into mountpoints marked as MS_PRIVATE.
885*4b9c6d91SCole Faust 			 * This means that if a mount is unmounted in the root
886*4b9c6d91SCole Faust 			 * mount namespace, it will not be unmounted in the
887*4b9c6d91SCole Faust 			 * non-root mount namespace.
888*4b9c6d91SCole Faust 			 * This in turn can be problematic because activity in
889*4b9c6d91SCole Faust 			 * the non-root mount namespace can now directly
890*4b9c6d91SCole Faust 			 * influence the root mount namespace (e.g. preventing
891*4b9c6d91SCole Faust 			 * re-mounts of said mount), which would be a privilege
892*4b9c6d91SCole Faust 			 * inversion.
893*4b9c6d91SCole Faust 			 *
894*4b9c6d91SCole Faust 			 * Setting the default in the command-line to MS_SLAVE
895*4b9c6d91SCole Faust 			 * will still prevent mounts from leaking out of the
896*4b9c6d91SCole Faust 			 * non-root mount namespace but avoid these
897*4b9c6d91SCole Faust 			 * privilege-inversion issues.
898*4b9c6d91SCole Faust 			 * For cases where mounts should not flow *into* the
899*4b9c6d91SCole Faust 			 * namespace either, the user can pass -Kprivate.
900*4b9c6d91SCole Faust 			 * Note that mounts are marked as MS_PRIVATE by default
901*4b9c6d91SCole Faust 			 * by the kernel, so unless the init process (like
902*4b9c6d91SCole Faust 			 * systemd) or something else marks them as shared, this
903*4b9c6d91SCole Faust 			 * won't do anything.
904*4b9c6d91SCole Faust 			 */
905*4b9c6d91SCole Faust 			minijail_remount_mode(j, MS_SLAVE);
906*4b9c6d91SCole Faust 			mount_ns = 1;
907*4b9c6d91SCole Faust 			break;
908*4b9c6d91SCole Faust 		case 'V':
909*4b9c6d91SCole Faust 			minijail_namespace_enter_vfs(j, optarg);
910*4b9c6d91SCole Faust 			break;
911*4b9c6d91SCole Faust 		case 'r':
912*4b9c6d91SCole Faust 			minijail_remount_proc_readonly(j);
913*4b9c6d91SCole Faust 			break;
914*4b9c6d91SCole Faust 		case 'G':
915*4b9c6d91SCole Faust 			if (keep_suppl_gids)
916*4b9c6d91SCole Faust 				errx(1, "-y and -G are not compatible");
917*4b9c6d91SCole Faust 			minijail_inherit_usergroups(j);
918*4b9c6d91SCole Faust 			inherit_suppl_gids = 1;
919*4b9c6d91SCole Faust 			break;
920*4b9c6d91SCole Faust 		case 'y':
921*4b9c6d91SCole Faust 			if (inherit_suppl_gids)
922*4b9c6d91SCole Faust 				errx(1, "-y and -G are not compatible");
923*4b9c6d91SCole Faust 			minijail_keep_supplementary_gids(j);
924*4b9c6d91SCole Faust 			keep_suppl_gids = 1;
925*4b9c6d91SCole Faust 			break;
926*4b9c6d91SCole Faust 		case 'N':
927*4b9c6d91SCole Faust 			minijail_namespace_cgroups(j);
928*4b9c6d91SCole Faust 			break;
929*4b9c6d91SCole Faust 		case 'p':
930*4b9c6d91SCole Faust 			minijail_namespace_pids(j);
931*4b9c6d91SCole Faust 			break;
932*4b9c6d91SCole Faust 		case 'e':
933*4b9c6d91SCole Faust 			if (optarg)
934*4b9c6d91SCole Faust 				minijail_namespace_enter_net(j, optarg);
935*4b9c6d91SCole Faust 			else
936*4b9c6d91SCole Faust 				minijail_namespace_net(j);
937*4b9c6d91SCole Faust 			break;
938*4b9c6d91SCole Faust 		case 'i':
939*4b9c6d91SCole Faust 			*exit_immediately = 1;
940*4b9c6d91SCole Faust 			break;
941*4b9c6d91SCole Faust 		case 'H':
942*4b9c6d91SCole Faust 			seccomp_filter_usage(argv[0]);
943*4b9c6d91SCole Faust 			exit(0);
944*4b9c6d91SCole Faust 		case 'I':
945*4b9c6d91SCole Faust 			minijail_namespace_pids(j);
946*4b9c6d91SCole Faust 			minijail_run_as_init(j);
947*4b9c6d91SCole Faust 			break;
948*4b9c6d91SCole Faust 		case 'U':
949*4b9c6d91SCole Faust 			minijail_namespace_user(j);
950*4b9c6d91SCole Faust 			minijail_namespace_pids(j);
951*4b9c6d91SCole Faust 			break;
952*4b9c6d91SCole Faust 		case 'm':
953*4b9c6d91SCole Faust 			set_uidmap = 1;
954*4b9c6d91SCole Faust 			if (uidmap) {
955*4b9c6d91SCole Faust 				free(uidmap);
956*4b9c6d91SCole Faust 				uidmap = NULL;
957*4b9c6d91SCole Faust 			}
958*4b9c6d91SCole Faust 			if (optarg)
959*4b9c6d91SCole Faust 				uidmap = xstrdup(optarg);
960*4b9c6d91SCole Faust 			break;
961*4b9c6d91SCole Faust 		case 'M':
962*4b9c6d91SCole Faust 			set_gidmap = 1;
963*4b9c6d91SCole Faust 			if (gidmap) {
964*4b9c6d91SCole Faust 				free(gidmap);
965*4b9c6d91SCole Faust 				gidmap = NULL;
966*4b9c6d91SCole Faust 			}
967*4b9c6d91SCole Faust 			if (optarg)
968*4b9c6d91SCole Faust 				gidmap = xstrdup(optarg);
969*4b9c6d91SCole Faust 			break;
970*4b9c6d91SCole Faust 		case 'a':
971*4b9c6d91SCole Faust 			if (0 != minijail_use_alt_syscall(j, optarg))
972*4b9c6d91SCole Faust 				errx(1, "Could not set alt-syscall table");
973*4b9c6d91SCole Faust 			break;
974*4b9c6d91SCole Faust 		case 'R':
975*4b9c6d91SCole Faust 			add_rlimit(j, optarg);
976*4b9c6d91SCole Faust 			break;
977*4b9c6d91SCole Faust 		case 'T':
978*4b9c6d91SCole Faust 			if (streq(optarg, "static"))
979*4b9c6d91SCole Faust 				*elftype = ELFSTATIC;
980*4b9c6d91SCole Faust 			else if (streq(optarg, "dynamic"))
981*4b9c6d91SCole Faust 				*elftype = ELFDYNAMIC;
982*4b9c6d91SCole Faust 			else {
983*4b9c6d91SCole Faust 				errx(1, "ELF type must be 'static' or "
984*4b9c6d91SCole Faust 					"'dynamic'");
985*4b9c6d91SCole Faust 			}
986*4b9c6d91SCole Faust 			break;
987*4b9c6d91SCole Faust 		case 'w':
988*4b9c6d91SCole Faust 			minijail_new_session_keyring(j);
989*4b9c6d91SCole Faust 			break;
990*4b9c6d91SCole Faust 		case 'Y':
991*4b9c6d91SCole Faust 			minijail_set_seccomp_filter_tsync(j);
992*4b9c6d91SCole Faust 			break;
993*4b9c6d91SCole Faust 		case 'z':
994*4b9c6d91SCole Faust 			forward = 0;
995*4b9c6d91SCole Faust 			break;
996*4b9c6d91SCole Faust 		case 'd':
997*4b9c6d91SCole Faust 			minijail_namespace_vfs(j);
998*4b9c6d91SCole Faust 			minijail_mount_dev(j);
999*4b9c6d91SCole Faust 			break;
1000*4b9c6d91SCole Faust 		/* Long options. */
1001*4b9c6d91SCole Faust 		case OPT_AMBIENT:
1002*4b9c6d91SCole Faust 			ambient_caps = 1;
1003*4b9c6d91SCole Faust 			minijail_set_ambient_caps(j);
1004*4b9c6d91SCole Faust 			break;
1005*4b9c6d91SCole Faust 		case OPT_UTS:
1006*4b9c6d91SCole Faust 			minijail_namespace_uts(j);
1007*4b9c6d91SCole Faust 			if (optarg)
1008*4b9c6d91SCole Faust 				minijail_namespace_set_hostname(j, optarg);
1009*4b9c6d91SCole Faust 			break;
1010*4b9c6d91SCole Faust 		case OPT_LOGGING:
1011*4b9c6d91SCole Faust 			if (streq(optarg, "auto"))
1012*4b9c6d91SCole Faust 				log_to_stderr = -1;
1013*4b9c6d91SCole Faust 			else if (streq(optarg, "syslog"))
1014*4b9c6d91SCole Faust 				log_to_stderr = 0;
1015*4b9c6d91SCole Faust 			else if (streq(optarg, "stderr"))
1016*4b9c6d91SCole Faust 				log_to_stderr = 1;
1017*4b9c6d91SCole Faust 			else
1018*4b9c6d91SCole Faust 				errx(1,
1019*4b9c6d91SCole Faust 				     "--logger must be 'syslog' or 'stderr'");
1020*4b9c6d91SCole Faust 			break;
1021*4b9c6d91SCole Faust 		case OPT_PROFILE:
1022*4b9c6d91SCole Faust 			use_profile(j, optarg, &pivot_root, chroot, &tmp_size);
1023*4b9c6d91SCole Faust 			break;
1024*4b9c6d91SCole Faust 		case OPT_PRELOAD_LIBRARY:
1025*4b9c6d91SCole Faust 			*preload_path = optarg;
1026*4b9c6d91SCole Faust 			break;
1027*4b9c6d91SCole Faust 		case OPT_FS_DEFAULT_PATHS:
1028*4b9c6d91SCole Faust 			minijail_enable_default_fs_restrictions(j);
1029*4b9c6d91SCole Faust 			break;
1030*4b9c6d91SCole Faust 		case OPT_FS_PATH_RX:
1031*4b9c6d91SCole Faust 			minijail_add_fs_restriction_rx(j, optarg);
1032*4b9c6d91SCole Faust 			break;
1033*4b9c6d91SCole Faust 		case OPT_FS_PATH_RO:
1034*4b9c6d91SCole Faust 			minijail_add_fs_restriction_ro(j, optarg);
1035*4b9c6d91SCole Faust 			break;
1036*4b9c6d91SCole Faust 		case OPT_FS_PATH_RW:
1037*4b9c6d91SCole Faust 			minijail_add_fs_restriction_rw(j, optarg);
1038*4b9c6d91SCole Faust 			break;
1039*4b9c6d91SCole Faust 		case OPT_FS_PATH_ADVANCED_RW:
1040*4b9c6d91SCole Faust 			minijail_add_fs_restriction_advanced_rw(j, optarg);
1041*4b9c6d91SCole Faust 			break;
1042*4b9c6d91SCole Faust 		case OPT_SECCOMP_BPF_BINARY:
1043*4b9c6d91SCole Faust 			if (seccomp != None && seccomp != BpfBinaryFilter) {
1044*4b9c6d91SCole Faust 				errx(1, "Do not use -s, -S, or "
1045*4b9c6d91SCole Faust 					"--seccomp-bpf-binary together");
1046*4b9c6d91SCole Faust 			}
1047*4b9c6d91SCole Faust 			if (use_seccomp_log == 1)
1048*4b9c6d91SCole Faust 				errx(1, "-L does not work with "
1049*4b9c6d91SCole Faust 					"--seccomp-bpf-binary");
1050*4b9c6d91SCole Faust 			seccomp = BpfBinaryFilter;
1051*4b9c6d91SCole Faust 			minijail_use_seccomp_filter(j);
1052*4b9c6d91SCole Faust 			filter_path = optarg;
1053*4b9c6d91SCole Faust 			use_seccomp_filter_binary = 1;
1054*4b9c6d91SCole Faust 			break;
1055*4b9c6d91SCole Faust 		case OPT_ADD_SUPPL_GROUP:
1056*4b9c6d91SCole Faust 			suppl_group_add(&suppl_gids_count, &suppl_gids, optarg);
1057*4b9c6d91SCole Faust 			break;
1058*4b9c6d91SCole Faust 		case OPT_ALLOW_SPECULATIVE_EXECUTION:
1059*4b9c6d91SCole Faust 			minijail_set_seccomp_filter_allow_speculation(j);
1060*4b9c6d91SCole Faust 			break;
1061*4b9c6d91SCole Faust 		case OPT_CONFIG: {
1062*4b9c6d91SCole Faust 			if (conf_entry_list != NULL) {
1063*4b9c6d91SCole Faust 				errx(1, "Nested config file specification is "
1064*4b9c6d91SCole Faust 					"not allowed.");
1065*4b9c6d91SCole Faust 			}
1066*4b9c6d91SCole Faust 			conf_entry_list = new_config_entry_list();
1067*4b9c6d91SCole Faust 			conf_index = 0;
1068*4b9c6d91SCole Faust #if defined(BLOCK_NOEXEC_CONF)
1069*4b9c6d91SCole Faust 			/*
1070*4b9c6d91SCole Faust 			 * Check the conf file is in a exec mount.
1071*4b9c6d91SCole Faust 			 * With a W^X invariant, it excludes writable
1072*4b9c6d91SCole Faust 			 * mounts.
1073*4b9c6d91SCole Faust 			 */
1074*4b9c6d91SCole Faust 			struct statfs conf_statfs;
1075*4b9c6d91SCole Faust 			if (statfs(optarg, &conf_statfs) != 0)
1076*4b9c6d91SCole Faust 				err(1, "statfs(%s) failed.", optarg);
1077*4b9c6d91SCole Faust 			if ((conf_statfs.f_flags & MS_NOEXEC) != 0)
1078*4b9c6d91SCole Faust 				errx(1,
1079*4b9c6d91SCole Faust 				     "Conf file must be in a exec "
1080*4b9c6d91SCole Faust 				     "mount: %s",
1081*4b9c6d91SCole Faust 				     optarg);
1082*4b9c6d91SCole Faust #endif
1083*4b9c6d91SCole Faust #if defined(ENFORCE_ROOTFS_CONF)
1084*4b9c6d91SCole Faust 			/* Make sure the conf file is in the same device as the
1085*4b9c6d91SCole Faust 			 * rootfs. */
1086*4b9c6d91SCole Faust 			struct stat root_stat;
1087*4b9c6d91SCole Faust 			struct stat conf_stat;
1088*4b9c6d91SCole Faust 			if (stat("/", &root_stat) != 0)
1089*4b9c6d91SCole Faust 				err(1, "stat(/) failed.");
1090*4b9c6d91SCole Faust 			if (stat(optarg, &conf_stat) != 0)
1091*4b9c6d91SCole Faust 				err(1, "stat(%s) failed.", optarg);
1092*4b9c6d91SCole Faust 			if (root_stat.st_dev != conf_stat.st_dev)
1093*4b9c6d91SCole Faust 				errx(1, "Conf file must be in the rootfs.");
1094*4b9c6d91SCole Faust #endif
1095*4b9c6d91SCole Faust 			attribute_cleanup_fp FILE *config_file =
1096*4b9c6d91SCole Faust 			    fopen(optarg, "re");
1097*4b9c6d91SCole Faust 			if (!config_file)
1098*4b9c6d91SCole Faust 				err(1, "Failed to open %s", optarg);
1099*4b9c6d91SCole Faust 			if (!parse_config_file(config_file, conf_entry_list)) {
1100*4b9c6d91SCole Faust 				errx(
1101*4b9c6d91SCole Faust 				    1,
1102*4b9c6d91SCole Faust 				    "Unable to parse %s as Minijail conf file, "
1103*4b9c6d91SCole Faust 				    "please refer to minijail0(5) for syntax "
1104*4b9c6d91SCole Faust 				    "and examples.",
1105*4b9c6d91SCole Faust 				    optarg);
1106*4b9c6d91SCole Faust 			}
1107*4b9c6d91SCole Faust 			break;
1108*4b9c6d91SCole Faust 		}
1109*4b9c6d91SCole Faust 		case OPT_ENV_ADD:
1110*4b9c6d91SCole Faust 			/*
1111*4b9c6d91SCole Faust 			 * We either copy our current env to the child env
1112*4b9c6d91SCole Faust 			 * then add the requested envvar to it, or just
1113*4b9c6d91SCole Faust 			 * add the requested envvar to the already existing
1114*4b9c6d91SCole Faust 			 * envp.
1115*4b9c6d91SCole Faust 			 */
1116*4b9c6d91SCole Faust 			set_child_env(envp, optarg, environ);
1117*4b9c6d91SCole Faust 			break;
1118*4b9c6d91SCole Faust 		case OPT_ENV_RESET:
1119*4b9c6d91SCole Faust 			if (*envp && *envp != environ) {
1120*4b9c6d91SCole Faust 				/*
1121*4b9c6d91SCole Faust 				 * We already started to initialize the future
1122*4b9c6d91SCole Faust 				 * child env, because we got some --env-add
1123*4b9c6d91SCole Faust 				 * earlier on the command-line, so first,
1124*4b9c6d91SCole Faust 				 * free the memory we allocated.
1125*4b9c6d91SCole Faust 				 * If |*envp| happens to point to |environ|,
1126*4b9c6d91SCole Faust 				 * don't attempt to free it.
1127*4b9c6d91SCole Faust 				 */
1128*4b9c6d91SCole Faust 				minijail_free_env(*envp);
1129*4b9c6d91SCole Faust 			}
1130*4b9c6d91SCole Faust 			/* Allocate an empty environment for the child. */
1131*4b9c6d91SCole Faust 			*envp = calloc(1, sizeof(char *));
1132*4b9c6d91SCole Faust 			if (!*envp)
1133*4b9c6d91SCole Faust 				err(1, "Failed to allocate memory.");
1134*4b9c6d91SCole Faust 			break;
1135*4b9c6d91SCole Faust 		default:
1136*4b9c6d91SCole Faust 			usage(argv[0]);
1137*4b9c6d91SCole Faust 			exit(opt == 'h' ? 0 : 1);
1138*4b9c6d91SCole Faust 		}
1139*4b9c6d91SCole Faust 	}
1140*4b9c6d91SCole Faust 
1141*4b9c6d91SCole Faust 	if (log_to_stderr == -1) {
1142*4b9c6d91SCole Faust 		/* Autodetect default logging output. */
1143*4b9c6d91SCole Faust 		log_to_stderr = isatty(STDIN_FILENO) ? 1 : 0;
1144*4b9c6d91SCole Faust 	}
1145*4b9c6d91SCole Faust 	if (log_to_stderr) {
1146*4b9c6d91SCole Faust 		init_logging(LOG_TO_FD, STDERR_FILENO, LOG_INFO);
1147*4b9c6d91SCole Faust 		/*
1148*4b9c6d91SCole Faust 		 * When logging to stderr, ensure the FD survives the jailing.
1149*4b9c6d91SCole Faust 		 */
1150*4b9c6d91SCole Faust 		if (0 !=
1151*4b9c6d91SCole Faust 		    minijail_preserve_fd(j, STDERR_FILENO, STDERR_FILENO)) {
1152*4b9c6d91SCole Faust 			errx(1, "Could not preserve stderr");
1153*4b9c6d91SCole Faust 		}
1154*4b9c6d91SCole Faust 	}
1155*4b9c6d91SCole Faust 
1156*4b9c6d91SCole Faust 	/* Set up uid/gid mapping. */
1157*4b9c6d91SCole Faust 	if (set_uidmap || set_gidmap) {
1158*4b9c6d91SCole Faust 		set_ugid_mapping(j, set_uidmap, uid, uidmap, set_gidmap, gid,
1159*4b9c6d91SCole Faust 				 gidmap);
1160*4b9c6d91SCole Faust 	}
1161*4b9c6d91SCole Faust 
1162*4b9c6d91SCole Faust 	/* Can only set ambient caps when using regular caps. */
1163*4b9c6d91SCole Faust 	if (ambient_caps && !caps) {
1164*4b9c6d91SCole Faust 		errx(1, "Can't set ambient capabilities (--ambient) "
1165*4b9c6d91SCole Faust 			"without actually using capabilities (-c)");
1166*4b9c6d91SCole Faust 	}
1167*4b9c6d91SCole Faust 
1168*4b9c6d91SCole Faust 	/* Set up signal handlers in minijail unless asked not to. */
1169*4b9c6d91SCole Faust 	if (forward)
1170*4b9c6d91SCole Faust 		minijail_forward_signals(j);
1171*4b9c6d91SCole Faust 
1172*4b9c6d91SCole Faust 	/*
1173*4b9c6d91SCole Faust 	 * Only allow bind mounts when entering a chroot, using pivot_root, or
1174*4b9c6d91SCole Faust 	 * a new mount namespace.
1175*4b9c6d91SCole Faust 	 */
1176*4b9c6d91SCole Faust 	if (binding && !(chroot || pivot_root || mount_ns)) {
1177*4b9c6d91SCole Faust 		errx(1, "Bind mounts require a chroot, pivot_root, or "
1178*4b9c6d91SCole Faust 			" new mount namespace");
1179*4b9c6d91SCole Faust 	}
1180*4b9c6d91SCole Faust 
1181*4b9c6d91SCole Faust 	/*
1182*4b9c6d91SCole Faust 	 * / is only remounted when entering a new mount namespace, so unless
1183*4b9c6d91SCole Faust 	 * that's set there is no need for the -K/-K<mode> flags.
1184*4b9c6d91SCole Faust 	 */
1185*4b9c6d91SCole Faust 	if (change_remount && !mount_ns) {
1186*4b9c6d91SCole Faust 		errx(1, "No need to use -K (skip remounting '/') or "
1187*4b9c6d91SCole Faust 			"-K<mode> (remount '/' as <mode>) "
1188*4b9c6d91SCole Faust 			"without -v (new mount namespace).\n"
1189*4b9c6d91SCole Faust 			"Do you need to add '-v' explicitly?");
1190*4b9c6d91SCole Faust 	}
1191*4b9c6d91SCole Faust 
1192*4b9c6d91SCole Faust 	/* Configure the remount flag here to avoid having -v override it. */
1193*4b9c6d91SCole Faust 	if (change_remount) {
1194*4b9c6d91SCole Faust 		if (remount_mode != NULL) {
1195*4b9c6d91SCole Faust 			set_remount_mode(j, remount_mode);
1196*4b9c6d91SCole Faust 		} else {
1197*4b9c6d91SCole Faust 			minijail_skip_remount_private(j);
1198*4b9c6d91SCole Faust 		}
1199*4b9c6d91SCole Faust 	}
1200*4b9c6d91SCole Faust 
1201*4b9c6d91SCole Faust 	/*
1202*4b9c6d91SCole Faust 	 * Proceed in setting the supplementary gids specified on the
1203*4b9c6d91SCole Faust 	 * cmdline options.
1204*4b9c6d91SCole Faust 	 */
1205*4b9c6d91SCole Faust 	if (suppl_gids_count) {
1206*4b9c6d91SCole Faust 		minijail_set_supplementary_gids(j, suppl_gids_count,
1207*4b9c6d91SCole Faust 						suppl_gids);
1208*4b9c6d91SCole Faust 		free(suppl_gids);
1209*4b9c6d91SCole Faust 	}
1210*4b9c6d91SCole Faust 
1211*4b9c6d91SCole Faust 	/*
1212*4b9c6d91SCole Faust 	 * We parse seccomp filters here to make sure we've collected all
1213*4b9c6d91SCole Faust 	 * cmdline options.
1214*4b9c6d91SCole Faust 	 */
1215*4b9c6d91SCole Faust 	if (use_seccomp_filter) {
1216*4b9c6d91SCole Faust 		minijail_parse_seccomp_filters(j, filter_path);
1217*4b9c6d91SCole Faust 	} else if (use_seccomp_filter_binary) {
1218*4b9c6d91SCole Faust 		struct sock_fprog filter;
1219*4b9c6d91SCole Faust 		read_seccomp_filter(filter_path, &filter);
1220*4b9c6d91SCole Faust 		minijail_set_seccomp_filters(j, &filter);
1221*4b9c6d91SCole Faust 		free((void *)filter.filter);
1222*4b9c6d91SCole Faust 	}
1223*4b9c6d91SCole Faust 
1224*4b9c6d91SCole Faust 	/* Mount a tmpfs under /tmp and set its size. */
1225*4b9c6d91SCole Faust 	if (tmp_size)
1226*4b9c6d91SCole Faust 		minijail_mount_tmp_size(j, tmp_size);
1227*4b9c6d91SCole Faust 
1228*4b9c6d91SCole Faust 	/*
1229*4b9c6d91SCole Faust 	 * Copy our current env to the child if its |*envp| has not
1230*4b9c6d91SCole Faust 	 * already been initialized from --env-(reset|add) usage.
1231*4b9c6d91SCole Faust 	 */
1232*4b9c6d91SCole Faust 	if (!*envp) {
1233*4b9c6d91SCole Faust 		*envp = minijail_copy_env(environ);
1234*4b9c6d91SCole Faust 		if (!*envp)
1235*4b9c6d91SCole Faust 			err(1, "Failed to allocate memory.");
1236*4b9c6d91SCole Faust 	}
1237*4b9c6d91SCole Faust 
1238*4b9c6d91SCole Faust 	/*
1239*4b9c6d91SCole Faust 	 * There should be at least one additional unparsed argument: the
1240*4b9c6d91SCole Faust 	 * executable name.
1241*4b9c6d91SCole Faust 	 */
1242*4b9c6d91SCole Faust 	if (argc == optind) {
1243*4b9c6d91SCole Faust 		usage(argv[0]);
1244*4b9c6d91SCole Faust 		exit(1);
1245*4b9c6d91SCole Faust 	}
1246*4b9c6d91SCole Faust 
1247*4b9c6d91SCole Faust 	if (*elftype == ELFERROR) {
1248*4b9c6d91SCole Faust 		/*
1249*4b9c6d91SCole Faust 		 * -T was not specified.
1250*4b9c6d91SCole Faust 		 * Get the path to the program adjusted for changing root.
1251*4b9c6d91SCole Faust 		 */
1252*4b9c6d91SCole Faust 		char *program_path =
1253*4b9c6d91SCole Faust 		    minijail_get_original_path(j, argv[optind]);
1254*4b9c6d91SCole Faust 
1255*4b9c6d91SCole Faust 		/* Check that we can access the target program. */
1256*4b9c6d91SCole Faust 		if (access(program_path, X_OK)) {
1257*4b9c6d91SCole Faust 			errx(1, "Target program '%s' is not accessible",
1258*4b9c6d91SCole Faust 			     argv[optind]);
1259*4b9c6d91SCole Faust 		}
1260*4b9c6d91SCole Faust 
1261*4b9c6d91SCole Faust 		/* Check if target is statically or dynamically linked. */
1262*4b9c6d91SCole Faust 		*elftype = get_elf_linkage(program_path);
1263*4b9c6d91SCole Faust 		free(program_path);
1264*4b9c6d91SCole Faust 	}
1265*4b9c6d91SCole Faust 
1266*4b9c6d91SCole Faust 	/*
1267*4b9c6d91SCole Faust 	 * Setting capabilities need either a dynamically-linked binary, or the
1268*4b9c6d91SCole Faust 	 * use of ambient capabilities for them to be able to survive an
1269*4b9c6d91SCole Faust 	 * execve(2).
1270*4b9c6d91SCole Faust 	 */
1271*4b9c6d91SCole Faust 	if (caps && *elftype == ELFSTATIC && !ambient_caps) {
1272*4b9c6d91SCole Faust 		errx(1, "Can't run statically-linked binaries with capabilities"
1273*4b9c6d91SCole Faust 			" (-c) without also setting ambient capabilities. "
1274*4b9c6d91SCole Faust 			"Try passing --ambient.");
1275*4b9c6d91SCole Faust 	}
1276*4b9c6d91SCole Faust 
1277*4b9c6d91SCole Faust 	return optind;
1278*4b9c6d91SCole Faust }
1279