xref: /linux/samples/landlock/sandboxer.c (revision 8faab14922b75c3c0bbc0769eedef85d4609a85a)
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 10
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 	int set_restrict_flags = 0;
458 
459 	if (argc < 2) {
460 		fprintf(stderr, help, argv[0]);
461 		return 1;
462 	}
463 
464 	abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
465 	if (abi < 0) {
466 		const int err = errno;
467 
468 		perror("Failed to check Landlock compatibility");
469 		switch (err) {
470 		case ENOSYS:
471 			fprintf(stderr,
472 				"Hint: Landlock is not supported by the current kernel. "
473 				"To support it, build the kernel with "
474 				"CONFIG_SECURITY_LANDLOCK=y and prepend "
475 				"\"landlock,\" to the content of CONFIG_LSM.\n");
476 			break;
477 		case EOPNOTSUPP:
478 			fprintf(stderr,
479 				"Hint: Landlock is currently disabled. "
480 				"It can be enabled in the kernel configuration by "
481 				"prepending \"landlock,\" to the content of CONFIG_LSM, "
482 				"or at boot time by setting the same content to the "
483 				"\"lsm\" kernel parameter.\n");
484 			break;
485 		}
486 		return 1;
487 	}
488 
489 	/* Best-effort security. */
490 	switch (abi) {
491 	case 1:
492 		/*
493 		 * Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2
494 		 *
495 		 * Note: The "refer" operations (file renaming and linking
496 		 * across different directories) are always forbidden when using
497 		 * Landlock with ABI 1.
498 		 *
499 		 * If only ABI 1 is available, this sandboxer knowingly forbids
500 		 * refer operations.
501 		 *
502 		 * If a program *needs* to do refer operations after enabling
503 		 * Landlock, it can not use Landlock at ABI level 1.  To be
504 		 * compatible with different kernel versions, such programs
505 		 * should then fall back to not restrict themselves at all if
506 		 * the running kernel only supports ABI 1.
507 		 */
508 		ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
509 		__attribute__((fallthrough));
510 	case 2:
511 		/* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
512 		ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
513 		__attribute__((fallthrough));
514 	case 3:
515 		/* Removes network support for ABI < 4 */
516 		ruleset_attr.handled_access_net &=
517 			~(LANDLOCK_ACCESS_NET_BIND_TCP |
518 			  LANDLOCK_ACCESS_NET_CONNECT_TCP);
519 		__attribute__((fallthrough));
520 	case 4:
521 		/* Removes LANDLOCK_ACCESS_FS_IOCTL_DEV for ABI < 5 */
522 		ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
523 
524 		__attribute__((fallthrough));
525 	case 5:
526 		/* Removes LANDLOCK_SCOPE_* for ABI < 6 */
527 		ruleset_attr.scoped &= ~(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
528 					 LANDLOCK_SCOPE_SIGNAL);
529 		__attribute__((fallthrough));
530 	case 6:
531 		/* Removes LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON for ABI < 7 */
532 		supported_restrict_flags &=
533 			~LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON;
534 		__attribute__((fallthrough));
535 	case 7:
536 	case 8:
537 		/* Removes LANDLOCK_ACCESS_FS_RESOLVE_UNIX for ABI < 9 */
538 		ruleset_attr.handled_access_fs &=
539 			~LANDLOCK_ACCESS_FS_RESOLVE_UNIX;
540 		__attribute__((fallthrough));
541 	case 9:
542 		/* Removes UDP support for ABI < 10 */
543 		ruleset_attr.handled_access_net &=
544 			~(LANDLOCK_ACCESS_NET_BIND_UDP |
545 			  LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP);
546 		/* Removes quiet flags for ABI < 10 later on. */
547 		quiet_supported = false;
548 
549 		/* Must be printed for any ABI < LANDLOCK_ABI_LAST. */
550 		fprintf(stderr,
551 			"Hint: You should update the running kernel "
552 			"to leverage Landlock features "
553 			"provided by ABI version %d (instead of %d).\n",
554 			LANDLOCK_ABI_LAST, abi);
555 		__attribute__((fallthrough));
556 	case LANDLOCK_ABI_LAST:
557 		break;
558 	default:
559 		fprintf(stderr,
560 			"Hint: You should update this sandboxer "
561 			"to leverage Landlock features "
562 			"provided by ABI version %d (instead of %d).\n",
563 			abi, LANDLOCK_ABI_LAST);
564 	}
565 	access_fs_ro &= ruleset_attr.handled_access_fs;
566 	access_fs_rw &= ruleset_attr.handled_access_fs;
567 
568 	/* Removes bind access attribute if not supported by a user. */
569 	env_port_name = getenv(ENV_TCP_BIND_NAME);
570 	if (!env_port_name) {
571 		ruleset_attr.handled_access_net &=
572 			~LANDLOCK_ACCESS_NET_BIND_TCP;
573 	}
574 	/* Removes connect access attribute if not supported by a user. */
575 	env_port_name = getenv(ENV_TCP_CONNECT_NAME);
576 	if (!env_port_name) {
577 		ruleset_attr.handled_access_net &=
578 			~LANDLOCK_ACCESS_NET_CONNECT_TCP;
579 	}
580 	/* Removes UDP bind access control if not supported by a user. */
581 	env_port_name = getenv(ENV_UDP_BIND_NAME);
582 	if (!env_port_name) {
583 		ruleset_attr.handled_access_net &=
584 			~LANDLOCK_ACCESS_NET_BIND_UDP;
585 	}
586 	/* Removes UDP connect/send access control if not supported by a user. */
587 	env_port_name = getenv(ENV_UDP_CONNECT_SEND_NAME);
588 	if (!env_port_name) {
589 		ruleset_attr.handled_access_net &=
590 			~LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP;
591 	}
592 
593 	if (check_ruleset_scope(ENV_SCOPED_NAME, &ruleset_attr))
594 		return 1;
595 
596 	/* Enables optional logs. */
597 	env_force_log = getenv(ENV_FORCE_LOG_NAME);
598 	if (env_force_log) {
599 		if (strcmp(env_force_log, "1") != 0) {
600 			fprintf(stderr, "Unknown value for " ENV_FORCE_LOG_NAME
601 					" (only \"1\" is handled)\n");
602 			return 1;
603 		}
604 		if (!(supported_restrict_flags &
605 		      LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON)) {
606 			fprintf(stderr,
607 				"Audit logs not supported by current kernel\n");
608 			return 1;
609 		}
610 		set_restrict_flags |= LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON;
611 		unsetenv(ENV_FORCE_LOG_NAME);
612 	}
613 
614 	/* Set the quiet access masks. */
615 	if (quiet_supported) {
616 		if ((getenv(ENV_FS_QUIET_NAME) || getenv(ENV_NET_QUIET_NAME)) &&
617 		    !getenv(ENV_QUIET_ACCESS_NAME)) {
618 			fprintf(stderr,
619 				"%s must be set (e.g. to \"all\") when %s or %s is used\n",
620 				ENV_QUIET_ACCESS_NAME, ENV_FS_QUIET_NAME,
621 				ENV_NET_QUIET_NAME);
622 			return 1;
623 		}
624 		if (add_quiet_access(ENV_QUIET_ACCESS_NAME, &ruleset_attr))
625 			return 1;
626 	} else if (getenv(ENV_FS_QUIET_NAME) || getenv(ENV_NET_QUIET_NAME) ||
627 		   getenv(ENV_QUIET_ACCESS_NAME)) {
628 		fprintf(stderr,
629 			"Quiet flags not supported by current kernel\n");
630 		return 1;
631 	}
632 
633 	ruleset_fd =
634 		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
635 	if (ruleset_fd < 0) {
636 		perror("Failed to create a ruleset");
637 		return 1;
638 	}
639 
640 	if (populate_ruleset_fs(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro, 0))
641 		goto err_close_ruleset;
642 	if (populate_ruleset_fs(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw, 0))
643 		goto err_close_ruleset;
644 
645 	/* Don't require this env to be present. */
646 	if (quiet_supported && getenv(ENV_FS_QUIET_NAME)) {
647 		if (populate_ruleset_fs(ENV_FS_QUIET_NAME, ruleset_fd, 0,
648 					LANDLOCK_ADD_RULE_QUIET))
649 			goto err_close_ruleset;
650 	}
651 
652 	if (populate_ruleset_net(ENV_TCP_BIND_NAME, ruleset_fd,
653 				 LANDLOCK_ACCESS_NET_BIND_TCP, 0)) {
654 		goto err_close_ruleset;
655 	}
656 	if (populate_ruleset_net(ENV_TCP_CONNECT_NAME, ruleset_fd,
657 				 LANDLOCK_ACCESS_NET_CONNECT_TCP, 0)) {
658 		goto err_close_ruleset;
659 	}
660 	if (populate_ruleset_net(ENV_UDP_BIND_NAME, ruleset_fd,
661 				 LANDLOCK_ACCESS_NET_BIND_UDP, 0)) {
662 		goto err_close_ruleset;
663 	}
664 	if (populate_ruleset_net(ENV_UDP_CONNECT_SEND_NAME, ruleset_fd,
665 				 LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP, 0)) {
666 		goto err_close_ruleset;
667 	}
668 
669 	if (quiet_supported) {
670 		if (populate_ruleset_net(ENV_NET_QUIET_NAME, ruleset_fd, 0,
671 					 LANDLOCK_ADD_RULE_QUIET)) {
672 			goto err_close_ruleset;
673 		}
674 	}
675 
676 	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
677 		perror("Failed to restrict privileges");
678 		goto err_close_ruleset;
679 	}
680 	if (landlock_restrict_self(ruleset_fd, set_restrict_flags)) {
681 		perror("Failed to enforce ruleset");
682 		goto err_close_ruleset;
683 	}
684 	close(ruleset_fd);
685 
686 	cmd_path = argv[1];
687 	cmd_argv = argv + 1;
688 	fprintf(stderr, "Executing the sandboxed command...\n");
689 	execvpe(cmd_path, cmd_argv, envp);
690 	fprintf(stderr, "Failed to execute \"%s\": %s\n", cmd_path,
691 		strerror(errno));
692 	fprintf(stderr, "Hint: access to the binary, the interpreter or "
693 			"shared libraries may be denied.\n");
694 	return 1;
695 
696 err_close_ruleset:
697 	close(ruleset_fd);
698 	return 1;
699 }
700