xref: /linux/samples/landlock/sandboxer.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Simple Landlock sandbox manager able to execute a process restricted by
4  * user-defined file system and network access control policies.
5  *
6  * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
7  * Copyright © 2020 ANSSI
8  */
9 
10 #define _GNU_SOURCE
11 #define __SANE_USERSPACE_TYPES__
12 #include <arpa/inet.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <linux/landlock.h>
16 #include <linux/socket.h>
17 #include <stddef.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/prctl.h>
22 #include <sys/stat.h>
23 #include <sys/syscall.h>
24 #include <unistd.h>
25 #include <stdbool.h>
26 
27 #if defined(__GLIBC__)
28 #include <linux/prctl.h>
29 #endif
30 
31 #ifndef landlock_create_ruleset
32 static inline int
33 landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
34 			const size_t size, const __u32 flags)
35 {
36 	return syscall(__NR_landlock_create_ruleset, attr, size, flags);
37 }
38 #endif
39 
40 #ifndef landlock_add_rule
41 static inline int landlock_add_rule(const int ruleset_fd,
42 				    const enum landlock_rule_type rule_type,
43 				    const void *const rule_attr,
44 				    const __u32 flags)
45 {
46 	return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr,
47 		       flags);
48 }
49 #endif
50 
51 #ifndef landlock_restrict_self
52 static inline int landlock_restrict_self(const int ruleset_fd,
53 					 const __u32 flags)
54 {
55 	return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
56 }
57 #endif
58 
59 #define ENV_FS_RO_NAME "LL_FS_RO"
60 #define ENV_FS_RW_NAME "LL_FS_RW"
61 #define ENV_FS_QUIET_NAME "LL_FS_QUIET"
62 #define ENV_TCP_BIND_NAME "LL_TCP_BIND"
63 #define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT"
64 #define ENV_NET_QUIET_NAME "LL_NET_QUIET"
65 #define ENV_SCOPED_NAME "LL_SCOPED"
66 #define ENV_QUIET_ACCESS_NAME "LL_QUIET_ACCESS"
67 #define ENV_FORCE_LOG_NAME "LL_FORCE_LOG"
68 #define ENV_UDP_BIND_NAME "LL_UDP_BIND"
69 #define ENV_UDP_CONNECT_SEND_NAME "LL_UDP_CONNECT_SEND"
70 #define ENV_DELIMITER ":"
71 
72 static int str2num(const char *numstr, __u64 *num_dst)
73 {
74 	char *endptr = NULL;
75 	int err = 0;
76 	__u64 num;
77 
78 	errno = 0;
79 	num = strtoull(numstr, &endptr, 10);
80 	if (errno != 0)
81 		err = errno;
82 	/* Was the string empty, or not entirely parsed successfully? */
83 	else if ((*numstr == '\0') || (*endptr != '\0'))
84 		err = EINVAL;
85 	else
86 		*num_dst = num;
87 
88 	return err;
89 }
90 
91 static int parse_path(char *env_path, const char ***const path_list)
92 {
93 	int i, num_paths = 0;
94 
95 	if (env_path) {
96 		num_paths++;
97 		for (i = 0; env_path[i]; i++) {
98 			if (env_path[i] == ENV_DELIMITER[0])
99 				num_paths++;
100 		}
101 	}
102 	*path_list = malloc(num_paths * sizeof(**path_list));
103 	if (!*path_list)
104 		return -1;
105 
106 	for (i = 0; i < num_paths; i++)
107 		(*path_list)[i] = strsep(&env_path, ENV_DELIMITER);
108 
109 	return num_paths;
110 }
111 
112 /* clang-format off */
113 
114 #define ACCESS_FILE ( \
115 	LANDLOCK_ACCESS_FS_EXECUTE | \
116 	LANDLOCK_ACCESS_FS_WRITE_FILE | \
117 	LANDLOCK_ACCESS_FS_READ_FILE | \
118 	LANDLOCK_ACCESS_FS_TRUNCATE | \
119 	LANDLOCK_ACCESS_FS_IOCTL_DEV | \
120 	LANDLOCK_ACCESS_FS_RESOLVE_UNIX)
121 
122 /* clang-format on */
123 
124 static int populate_ruleset_fs(const char *const env_var, const int ruleset_fd,
125 			       const __u64 allowed_access, __u32 flags)
126 {
127 	int num_paths, i, ret = 1;
128 	char *env_path_name;
129 	const char **path_list = NULL;
130 	struct landlock_path_beneath_attr path_beneath = {
131 		.parent_fd = -1,
132 	};
133 
134 	env_path_name = getenv(env_var);
135 	if (!env_path_name) {
136 		/* Prevents users to forget a setting. */
137 		fprintf(stderr, "Missing environment variable %s\n", env_var);
138 		return 1;
139 	}
140 	env_path_name = strdup(env_path_name);
141 	unsetenv(env_var);
142 	num_paths = parse_path(env_path_name, &path_list);
143 	if (num_paths < 0) {
144 		fprintf(stderr, "Failed to allocate memory\n");
145 		goto out_free_name;
146 	}
147 	if (num_paths == 1 && path_list[0][0] == '\0') {
148 		/*
149 		 * Allows to not use all possible restrictions (e.g. use
150 		 * LL_FS_RO without LL_FS_RW).
151 		 */
152 		ret = 0;
153 		goto out_free_name;
154 	}
155 
156 	for (i = 0; i < num_paths; i++) {
157 		struct stat statbuf;
158 
159 		path_beneath.parent_fd = open(path_list[i], O_PATH | O_CLOEXEC);
160 		if (path_beneath.parent_fd < 0) {
161 			fprintf(stderr, "Failed to open \"%s\": %s\n",
162 				path_list[i], strerror(errno));
163 			continue;
164 		}
165 		if (fstat(path_beneath.parent_fd, &statbuf)) {
166 			fprintf(stderr, "Failed to stat \"%s\": %s\n",
167 				path_list[i], strerror(errno));
168 			close(path_beneath.parent_fd);
169 			goto out_free_name;
170 		}
171 		path_beneath.allowed_access = allowed_access;
172 		if (!S_ISDIR(statbuf.st_mode))
173 			path_beneath.allowed_access &= ACCESS_FILE;
174 		if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
175 				      &path_beneath, flags)) {
176 			fprintf(stderr,
177 				"Failed to update the ruleset with \"%s\": %s\n",
178 				path_list[i], strerror(errno));
179 			close(path_beneath.parent_fd);
180 			goto out_free_name;
181 		}
182 		close(path_beneath.parent_fd);
183 	}
184 	ret = 0;
185 
186 out_free_name:
187 	free(path_list);
188 	free(env_path_name);
189 	return ret;
190 }
191 
192 static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
193 				const __u64 allowed_access, __u32 flags)
194 {
195 	int ret = 1;
196 	char *env_port_name, *env_port_name_next, *strport;
197 	struct landlock_net_port_attr net_port = {
198 		.allowed_access = allowed_access,
199 	};
200 
201 	env_port_name = getenv(env_var);
202 	if (!env_port_name)
203 		return 0;
204 	env_port_name = strdup(env_port_name);
205 	unsetenv(env_var);
206 
207 	env_port_name_next = env_port_name;
208 	while ((strport = strsep(&env_port_name_next, ENV_DELIMITER))) {
209 		__u64 port;
210 
211 		if (strcmp(strport, "") == 0)
212 			continue;
213 
214 		if (str2num(strport, &port)) {
215 			fprintf(stderr, "Failed to parse port at \"%s\"\n",
216 				strport);
217 			goto out_free_name;
218 		}
219 		net_port.port = port;
220 		if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
221 				      &net_port, flags)) {
222 			fprintf(stderr,
223 				"Failed to update the ruleset with port \"%llu\": %s\n",
224 				net_port.port, strerror(errno));
225 			goto out_free_name;
226 		}
227 	}
228 	ret = 0;
229 
230 out_free_name:
231 	free(env_port_name);
232 	return ret;
233 }
234 
235 /* Returns true on error, false otherwise. */
236 static bool check_ruleset_scope(const char *const env_var,
237 				struct landlock_ruleset_attr *ruleset_attr)
238 {
239 	char *env_type_scope, *env_type_scope_next, *ipc_scoping_name;
240 	bool error = false;
241 	bool abstract_scoping = false;
242 	bool signal_scoping = false;
243 
244 	/* Scoping is not supported by Landlock ABI */
245 	if (!(ruleset_attr->scoped &
246 	      (LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | LANDLOCK_SCOPE_SIGNAL)))
247 		goto out_unset;
248 
249 	env_type_scope = getenv(env_var);
250 	/* Scoping is not supported by the user */
251 	if (!env_type_scope || strcmp("", env_type_scope) == 0)
252 		goto out_unset;
253 
254 	env_type_scope = strdup(env_type_scope);
255 	env_type_scope_next = env_type_scope;
256 	while ((ipc_scoping_name =
257 			strsep(&env_type_scope_next, ENV_DELIMITER))) {
258 		if (strcmp("a", ipc_scoping_name) == 0 && !abstract_scoping) {
259 			abstract_scoping = true;
260 		} else if (strcmp("s", ipc_scoping_name) == 0 &&
261 			   !signal_scoping) {
262 			signal_scoping = true;
263 		} else {
264 			fprintf(stderr, "Unknown or duplicate scope \"%s\"\n",
265 				ipc_scoping_name);
266 			error = true;
267 			goto out_free_name;
268 		}
269 	}
270 
271 out_free_name:
272 	free(env_type_scope);
273 
274 out_unset:
275 	if (!abstract_scoping)
276 		ruleset_attr->scoped &= ~LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET;
277 	if (!signal_scoping)
278 		ruleset_attr->scoped &= ~LANDLOCK_SCOPE_SIGNAL;
279 
280 	unsetenv(env_var);
281 	return error;
282 }
283 
284 /* clang-format off */
285 
286 #define ACCESS_FS_ROUGHLY_READ ( \
287 	LANDLOCK_ACCESS_FS_EXECUTE | \
288 	LANDLOCK_ACCESS_FS_READ_FILE | \
289 	LANDLOCK_ACCESS_FS_READ_DIR)
290 
291 #define ACCESS_FS_ROUGHLY_WRITE ( \
292 	LANDLOCK_ACCESS_FS_WRITE_FILE | \
293 	LANDLOCK_ACCESS_FS_REMOVE_DIR | \
294 	LANDLOCK_ACCESS_FS_REMOVE_FILE | \
295 	LANDLOCK_ACCESS_FS_MAKE_CHAR | \
296 	LANDLOCK_ACCESS_FS_MAKE_DIR | \
297 	LANDLOCK_ACCESS_FS_MAKE_REG | \
298 	LANDLOCK_ACCESS_FS_MAKE_SOCK | \
299 	LANDLOCK_ACCESS_FS_MAKE_FIFO | \
300 	LANDLOCK_ACCESS_FS_MAKE_BLOCK | \
301 	LANDLOCK_ACCESS_FS_MAKE_SYM | \
302 	LANDLOCK_ACCESS_FS_REFER | \
303 	LANDLOCK_ACCESS_FS_TRUNCATE | \
304 	LANDLOCK_ACCESS_FS_IOCTL_DEV | \
305 	LANDLOCK_ACCESS_FS_RESOLVE_UNIX)
306 
307 /* clang-format on */
308 
309 /*
310  * Parses ENV_QUIET_ACCESS_NAME and sets the quiet_access_fs, quiet_access_net
311  * and quiet_scoped masks of @ruleset_attr accordingly.
312  */
313 static int add_quiet_access(const char *const env_var,
314 			    struct landlock_ruleset_attr *const ruleset_attr)
315 {
316 	char *env_quiet_access, *env_quiet_access_next, *str_access;
317 
318 	env_quiet_access = getenv(env_var);
319 	if (!env_quiet_access)
320 		return 0;
321 
322 	env_quiet_access = strdup(env_quiet_access);
323 	env_quiet_access_next = env_quiet_access;
324 	unsetenv(env_var);
325 
326 	while ((str_access = strsep(&env_quiet_access_next, ENV_DELIMITER))) {
327 		if (strcmp(str_access, "") == 0)
328 			continue;
329 		else if (strcmp(str_access, "all") == 0) {
330 			ruleset_attr->quiet_access_fs =
331 				ruleset_attr->handled_access_fs;
332 			ruleset_attr->quiet_access_net =
333 				ruleset_attr->handled_access_net;
334 			ruleset_attr->quiet_scoped = ruleset_attr->scoped;
335 		} else if (strcmp(str_access, "read") == 0)
336 			ruleset_attr->quiet_access_fs |= ACCESS_FS_ROUGHLY_READ;
337 		else if (strcmp(str_access, "write") == 0)
338 			ruleset_attr->quiet_access_fs |=
339 				ACCESS_FS_ROUGHLY_WRITE;
340 		else if (strcmp(str_access, "tcp_bind") == 0)
341 			ruleset_attr->quiet_access_net |=
342 				LANDLOCK_ACCESS_NET_BIND_TCP;
343 		else if (strcmp(str_access, "tcp_connect") == 0)
344 			ruleset_attr->quiet_access_net |=
345 				LANDLOCK_ACCESS_NET_CONNECT_TCP;
346 		else if (strcmp(str_access, "udp_bind") == 0)
347 			ruleset_attr->quiet_access_net |=
348 				LANDLOCK_ACCESS_NET_BIND_UDP;
349 		else if (strcmp(str_access, "udp_connect") == 0)
350 			ruleset_attr->quiet_access_net |=
351 				LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
352 		else if (strcmp(str_access, "abstract_unix_socket") == 0)
353 			ruleset_attr->quiet_scoped |=
354 				LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET;
355 		else if (strcmp(str_access, "signal") == 0)
356 			ruleset_attr->quiet_scoped |= LANDLOCK_SCOPE_SIGNAL;
357 		else {
358 			fprintf(stderr, "Unknown quiet access \"%s\"\n",
359 				str_access);
360 			free(env_quiet_access);
361 			return -1;
362 		}
363 	}
364 
365 	free(env_quiet_access);
366 	ruleset_attr->quiet_access_fs &= ruleset_attr->handled_access_fs;
367 	ruleset_attr->quiet_access_net &= ruleset_attr->handled_access_net;
368 	ruleset_attr->quiet_scoped &= ruleset_attr->scoped;
369 	return 0;
370 }
371 
372 #define LANDLOCK_ABI_LAST 11
373 
374 #define XSTR(s) #s
375 #define STR(s) XSTR(s)
376 
377 /* clang-format off */
378 
379 static const char help[] =
380 	"usage: " ENV_FS_RO_NAME "=\"...\" " ENV_FS_RW_NAME "=\"...\" "
381 	"[other environment variables] %1$s <cmd> [args]...\n"
382 	"\n"
383 	"Execute the given command in a restricted environment.\n"
384 	"Multi-valued settings (lists of ports, paths, scopes) are colon-delimited.\n"
385 	"\n"
386 	"Mandatory settings:\n"
387 	"* " ENV_FS_RO_NAME ": paths allowed to be used in a read-only way\n"
388 	"* " ENV_FS_RW_NAME ": paths allowed to be used in a read-write way\n"
389 	"\n"
390 	"Optional settings (when not set, their associated access check "
391 	"is always allowed, which is different from an empty string which "
392 	"means an empty list):\n"
393 	"* " ENV_TCP_BIND_NAME ": ports allowed to bind (server)\n"
394 	"* " ENV_TCP_CONNECT_NAME ": ports allowed to connect (client)\n"
395 	"* " ENV_UDP_BIND_NAME ": local UDP ports allowed to bind (server: "
396 	"prepare to receive on port / client: set as source port)\n"
397 	"* " ENV_UDP_CONNECT_SEND_NAME ": remote UDP ports allowed to connect "
398 	"or send to (client: use as destination port / server: receive only from it)\n"
399 	"(caution: sending requires being able to bind to a local source port)\n"
400 	"* " ENV_SCOPED_NAME ": actions denied on the outside of the landlock domain\n"
401 	"  - \"a\" to restrict opening abstract unix sockets\n"
402 	"  - \"s\" to restrict sending signals\n"
403 	"\n"
404 	"A sandboxer should not log denied access requests to avoid spamming logs, "
405 	"but to test audit we can set " ENV_FORCE_LOG_NAME "=1\n"
406 	ENV_FS_QUIET_NAME " and " ENV_NET_QUIET_NAME ", both optional, can then be used "
407 	"to make access to some denied paths or network ports not trigger audit logging.\n"
408 	ENV_QUIET_ACCESS_NAME " can be used to specify which accesses should be quieted "
409 	"(required when " ENV_FS_QUIET_NAME " or " ENV_NET_QUIET_NAME " is set):\n"
410 	"  - \"all\" to quiet all of the accesses below\n"
411 	"  - \"read\" to quiet all file/dir read accesses\n"
412 	"  - \"write\" to quiet all file/dir write accesses\n"
413 	"  - \"tcp_bind\" to quiet tcp bind denials\n"
414 	"  - \"tcp_connect\" to quiet tcp connect denials\n"
415 	"  - \"udp_bind\" to quiet udp bind denials\n"
416 	"  - \"udp_connect\" to quiet udp connect / send denials\n"
417 	"  - \"abstract_unix_socket\" to quiet abstract unix socket denials\n"
418 	"  - \"signal\" to quiet signal denials\n"
419 	"\n"
420 	"Example:\n"
421 	ENV_FS_RO_NAME "=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" "
422 	ENV_FS_RW_NAME "=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
423 	ENV_TCP_BIND_NAME "=\"9418\" "
424 	ENV_TCP_CONNECT_NAME "=\"80:443\" "
425 	ENV_UDP_CONNECT_SEND_NAME "=\"53\" "
426 	ENV_SCOPED_NAME "=\"a:s\" "
427 	"%1$s bash -i\n"
428 	"\n"
429 	"This sandboxer can use Landlock features up to ABI version "
430 	STR(LANDLOCK_ABI_LAST) ".\n";
431 
432 /* clang-format on */
433 
434 int main(const int argc, char *const argv[], char *const *const envp)
435 {
436 	const char *cmd_path;
437 	char *const *cmd_argv;
438 	int ruleset_fd, abi;
439 	char *env_port_name, *env_force_log;
440 	__u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ,
441 	      access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE;
442 
443 	struct landlock_ruleset_attr ruleset_attr = {
444 		.handled_access_fs = access_fs_rw,
445 		.handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
446 				      LANDLOCK_ACCESS_NET_CONNECT_TCP |
447 				      LANDLOCK_ACCESS_NET_BIND_UDP |
448 				      LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP,
449 		.scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
450 			  LANDLOCK_SCOPE_SIGNAL,
451 		.quiet_access_fs = 0,
452 		.quiet_access_net = 0,
453 		.quiet_scoped = 0,
454 	};
455 	bool quiet_supported = true;
456 	int supported_restrict_flags = LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON |
457 				       LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS;
458 	int set_restrict_flags = LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS;
459 
460 	if (argc < 2) {
461 		fprintf(stderr, help, argv[0]);
462 		return 1;
463 	}
464 
465 	abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
466 	if (abi < 0) {
467 		const int err = errno;
468 
469 		perror("Failed to check Landlock compatibility");
470 		switch (err) {
471 		case ENOSYS:
472 			fprintf(stderr,
473 				"Hint: Landlock is not supported by the current kernel. "
474 				"To support it, build the kernel with "
475 				"CONFIG_SECURITY_LANDLOCK=y and prepend "
476 				"\"landlock,\" to the content of CONFIG_LSM.\n");
477 			break;
478 		case EOPNOTSUPP:
479 			fprintf(stderr,
480 				"Hint: Landlock is currently disabled. "
481 				"It can be enabled in the kernel configuration by "
482 				"prepending \"landlock,\" to the content of CONFIG_LSM, "
483 				"or at boot time by setting the same content to the "
484 				"\"lsm\" kernel parameter.\n");
485 			break;
486 		}
487 		return 1;
488 	}
489 
490 	/* Best-effort security. */
491 	switch (abi) {
492 	case 1:
493 		/*
494 		 * Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2
495 		 *
496 		 * Note: The "refer" operations (file renaming and linking
497 		 * across different directories) are always forbidden when using
498 		 * Landlock with ABI 1.
499 		 *
500 		 * If only ABI 1 is available, this sandboxer knowingly forbids
501 		 * refer operations.
502 		 *
503 		 * If a program *needs* to do refer operations after enabling
504 		 * Landlock, it can not use Landlock at ABI level 1.  To be
505 		 * compatible with different kernel versions, such programs
506 		 * should then fall back to not restrict themselves at all if
507 		 * the running kernel only supports ABI 1.
508 		 */
509 		ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
510 		__attribute__((fallthrough));
511 	case 2:
512 		/* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
513 		ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
514 		__attribute__((fallthrough));
515 	case 3:
516 		/* Removes network support for ABI < 4 */
517 		ruleset_attr.handled_access_net &=
518 			~(LANDLOCK_ACCESS_NET_BIND_TCP |
519 			  LANDLOCK_ACCESS_NET_CONNECT_TCP);
520 		__attribute__((fallthrough));
521 	case 4:
522 		/* Removes LANDLOCK_ACCESS_FS_IOCTL_DEV for ABI < 5 */
523 		ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
524 
525 		__attribute__((fallthrough));
526 	case 5:
527 		/* Removes LANDLOCK_SCOPE_* for ABI < 6 */
528 		ruleset_attr.scoped &= ~(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
529 					 LANDLOCK_SCOPE_SIGNAL);
530 		__attribute__((fallthrough));
531 	case 6:
532 		/* Removes LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON for ABI < 7 */
533 		supported_restrict_flags &=
534 			~LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON;
535 		__attribute__((fallthrough));
536 	case 7:
537 	case 8:
538 		/* Removes LANDLOCK_ACCESS_FS_RESOLVE_UNIX for ABI < 9 */
539 		ruleset_attr.handled_access_fs &=
540 			~LANDLOCK_ACCESS_FS_RESOLVE_UNIX;
541 		__attribute__((fallthrough));
542 	case 9:
543 		/* Removes UDP support for ABI < 10 */
544 		ruleset_attr.handled_access_net &=
545 			~(LANDLOCK_ACCESS_NET_BIND_UDP |
546 			  LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP);
547 		/* Removes quiet flags for ABI < 10 later on. */
548 		quiet_supported = false;
549 		__attribute__((fallthrough));
550 	case 10:
551 		/* Removes LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS for ABI < 11 */
552 		supported_restrict_flags &=
553 			~LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS;
554 		set_restrict_flags &= ~LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS;
555 
556 		/* Must be printed for any ABI < LANDLOCK_ABI_LAST. */
557 		fprintf(stderr,
558 			"Hint: You should update the running kernel "
559 			"to leverage Landlock features "
560 			"provided by ABI version %d (instead of %d).\n",
561 			LANDLOCK_ABI_LAST, abi);
562 		__attribute__((fallthrough));
563 	case LANDLOCK_ABI_LAST:
564 		break;
565 	default:
566 		fprintf(stderr,
567 			"Hint: You should update this sandboxer "
568 			"to leverage Landlock features "
569 			"provided by ABI version %d (instead of %d).\n",
570 			abi, LANDLOCK_ABI_LAST);
571 	}
572 	access_fs_ro &= ruleset_attr.handled_access_fs;
573 	access_fs_rw &= ruleset_attr.handled_access_fs;
574 
575 	/* Removes bind access attribute if not supported by a user. */
576 	env_port_name = getenv(ENV_TCP_BIND_NAME);
577 	if (!env_port_name) {
578 		ruleset_attr.handled_access_net &=
579 			~LANDLOCK_ACCESS_NET_BIND_TCP;
580 	}
581 	/* Removes connect access attribute if not supported by a user. */
582 	env_port_name = getenv(ENV_TCP_CONNECT_NAME);
583 	if (!env_port_name) {
584 		ruleset_attr.handled_access_net &=
585 			~LANDLOCK_ACCESS_NET_CONNECT_TCP;
586 	}
587 	/* Removes UDP bind access control if not supported by a user. */
588 	env_port_name = getenv(ENV_UDP_BIND_NAME);
589 	if (!env_port_name) {
590 		ruleset_attr.handled_access_net &=
591 			~LANDLOCK_ACCESS_NET_BIND_UDP;
592 	}
593 	/* Removes UDP connect/send access control if not supported by a user. */
594 	env_port_name = getenv(ENV_UDP_CONNECT_SEND_NAME);
595 	if (!env_port_name) {
596 		ruleset_attr.handled_access_net &=
597 			~LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
598 	}
599 
600 	if (check_ruleset_scope(ENV_SCOPED_NAME, &ruleset_attr))
601 		return 1;
602 
603 	/* Enables optional logs. */
604 	env_force_log = getenv(ENV_FORCE_LOG_NAME);
605 	if (env_force_log) {
606 		if (strcmp(env_force_log, "1") != 0) {
607 			fprintf(stderr, "Unknown value for " ENV_FORCE_LOG_NAME
608 					" (only \"1\" is handled)\n");
609 			return 1;
610 		}
611 		if (!(supported_restrict_flags &
612 		      LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON)) {
613 			fprintf(stderr,
614 				"Audit logs not supported by current kernel\n");
615 			return 1;
616 		}
617 		set_restrict_flags |= LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON;
618 		unsetenv(ENV_FORCE_LOG_NAME);
619 	}
620 
621 	/* Set the quiet access masks. */
622 	if (quiet_supported) {
623 		if ((getenv(ENV_FS_QUIET_NAME) || getenv(ENV_NET_QUIET_NAME)) &&
624 		    !getenv(ENV_QUIET_ACCESS_NAME)) {
625 			fprintf(stderr,
626 				"%s must be set (e.g. to \"all\") when %s or %s is used\n",
627 				ENV_QUIET_ACCESS_NAME, ENV_FS_QUIET_NAME,
628 				ENV_NET_QUIET_NAME);
629 			return 1;
630 		}
631 		if (add_quiet_access(ENV_QUIET_ACCESS_NAME, &ruleset_attr))
632 			return 1;
633 	} else if (getenv(ENV_FS_QUIET_NAME) || getenv(ENV_NET_QUIET_NAME) ||
634 		   getenv(ENV_QUIET_ACCESS_NAME)) {
635 		fprintf(stderr,
636 			"Quiet flags not supported by current kernel\n");
637 		return 1;
638 	}
639 
640 	ruleset_fd =
641 		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
642 	if (ruleset_fd < 0) {
643 		perror("Failed to create a ruleset");
644 		return 1;
645 	}
646 
647 	if (populate_ruleset_fs(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro, 0))
648 		goto err_close_ruleset;
649 	if (populate_ruleset_fs(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw, 0))
650 		goto err_close_ruleset;
651 
652 	/* Don't require this env to be present. */
653 	if (quiet_supported && getenv(ENV_FS_QUIET_NAME)) {
654 		if (populate_ruleset_fs(ENV_FS_QUIET_NAME, ruleset_fd, 0,
655 					LANDLOCK_ADD_RULE_QUIET))
656 			goto err_close_ruleset;
657 	}
658 
659 	if (populate_ruleset_net(ENV_TCP_BIND_NAME, ruleset_fd,
660 				 LANDLOCK_ACCESS_NET_BIND_TCP, 0)) {
661 		goto err_close_ruleset;
662 	}
663 	if (populate_ruleset_net(ENV_TCP_CONNECT_NAME, ruleset_fd,
664 				 LANDLOCK_ACCESS_NET_CONNECT_TCP, 0)) {
665 		goto err_close_ruleset;
666 	}
667 	if (populate_ruleset_net(ENV_UDP_BIND_NAME, ruleset_fd,
668 				 LANDLOCK_ACCESS_NET_BIND_UDP, 0)) {
669 		goto err_close_ruleset;
670 	}
671 	if (populate_ruleset_net(ENV_UDP_CONNECT_SEND_NAME, ruleset_fd,
672 				 LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP, 0)) {
673 		goto err_close_ruleset;
674 	}
675 
676 	if (quiet_supported) {
677 		if (populate_ruleset_net(ENV_NET_QUIET_NAME, ruleset_fd, 0,
678 					 LANDLOCK_ADD_RULE_QUIET)) {
679 			goto err_close_ruleset;
680 		}
681 	}
682 
683 	if (!(set_restrict_flags & LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS) &&
684 	    prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
685 		perror("Failed to restrict privileges");
686 		goto err_close_ruleset;
687 	}
688 	if (landlock_restrict_self(ruleset_fd, set_restrict_flags)) {
689 		perror("Failed to enforce ruleset");
690 		goto err_close_ruleset;
691 	}
692 	close(ruleset_fd);
693 
694 	cmd_path = argv[1];
695 	cmd_argv = argv + 1;
696 	fprintf(stderr, "Executing the sandboxed command...\n");
697 	execvpe(cmd_path, cmd_argv, envp);
698 	fprintf(stderr, "Failed to execute \"%s\": %s\n", cmd_path,
699 		strerror(errno));
700 	fprintf(stderr, "Hint: access to the binary, the interpreter or "
701 			"shared libraries may be denied.\n");
702 	return 1;
703 
704 err_close_ruleset:
705 	close(ruleset_fd);
706 	return 1;
707 }
708