1 // SPDX-License-Identifier: BSD-3-Clause
3 * Simple Landlock sandbox manager able to execute a process restricted by
4 * user-defined file system and network access control policies.
6 * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
7 * Copyright © 2020 ANSSI
11 #define __SANE_USERSPACE_TYPES__
12 #include <arpa/inet.h>
15 #include <linux/landlock.h>
16 #include <linux/prctl.h>
17 #include <linux/socket.h>
22 #include <sys/prctl.h>
24 #include <sys/syscall.h>
28 #ifndef landlock_create_ruleset
30 landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
31 const size_t size, const __u32 flags)
33 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
37 #ifndef landlock_add_rule
38 static inline int landlock_add_rule(const int ruleset_fd,
39 const enum landlock_rule_type rule_type,
40 const void *const rule_attr,
43 return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr,
48 #ifndef landlock_restrict_self
49 static inline int landlock_restrict_self(const int ruleset_fd,
52 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
56 #define ENV_FS_RO_NAME "LL_FS_RO"
57 #define ENV_FS_RW_NAME "LL_FS_RW"
58 #define ENV_TCP_BIND_NAME "LL_TCP_BIND"
59 #define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT"
60 #define ENV_SCOPED_NAME "LL_SCOPED"
61 #define ENV_DELIMITER ":"
63 static int parse_path(char *env_path, const char ***const path_list)
69 for (i = 0; env_path[i]; i++) {
70 if (env_path[i] == ENV_DELIMITER[0])
74 *path_list = malloc(num_paths * sizeof(**path_list));
75 for (i = 0; i < num_paths; i++)
76 (*path_list)[i] = strsep(&env_path, ENV_DELIMITER);
81 /* clang-format off */
83 #define ACCESS_FILE ( \
84 LANDLOCK_ACCESS_FS_EXECUTE | \
85 LANDLOCK_ACCESS_FS_WRITE_FILE | \
86 LANDLOCK_ACCESS_FS_READ_FILE | \
87 LANDLOCK_ACCESS_FS_TRUNCATE | \
88 LANDLOCK_ACCESS_FS_IOCTL_DEV)
92 static int populate_ruleset_fs(const char *const env_var, const int ruleset_fd,
93 const __u64 allowed_access)
95 int num_paths, i, ret = 1;
97 const char **path_list = NULL;
98 struct landlock_path_beneath_attr path_beneath = {
102 env_path_name = getenv(env_var);
103 if (!env_path_name) {
104 /* Prevents users to forget a setting. */
105 fprintf(stderr, "Missing environment variable %s\n", env_var);
108 env_path_name = strdup(env_path_name);
110 num_paths = parse_path(env_path_name, &path_list);
111 if (num_paths == 1 && path_list[0][0] == '\0') {
113 * Allows to not use all possible restrictions (e.g. use
114 * LL_FS_RO without LL_FS_RW).
120 for (i = 0; i < num_paths; i++) {
123 path_beneath.parent_fd = open(path_list[i], O_PATH | O_CLOEXEC);
124 if (path_beneath.parent_fd < 0) {
125 fprintf(stderr, "Failed to open \"%s\": %s\n",
126 path_list[i], strerror(errno));
129 if (fstat(path_beneath.parent_fd, &statbuf)) {
130 fprintf(stderr, "Failed to stat \"%s\": %s\n",
131 path_list[i], strerror(errno));
132 close(path_beneath.parent_fd);
135 path_beneath.allowed_access = allowed_access;
136 if (!S_ISDIR(statbuf.st_mode))
137 path_beneath.allowed_access &= ACCESS_FILE;
138 if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
141 "Failed to update the ruleset with \"%s\": %s\n",
142 path_list[i], strerror(errno));
143 close(path_beneath.parent_fd);
146 close(path_beneath.parent_fd);
156 static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
157 const __u64 allowed_access)
160 char *env_port_name, *env_port_name_next, *strport;
161 struct landlock_net_port_attr net_port = {
162 .allowed_access = allowed_access,
166 env_port_name = getenv(env_var);
169 env_port_name = strdup(env_port_name);
172 env_port_name_next = env_port_name;
173 while ((strport = strsep(&env_port_name_next, ENV_DELIMITER))) {
174 net_port.port = atoi(strport);
175 if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
178 "Failed to update the ruleset with port \"%llu\": %s\n",
179 net_port.port, strerror(errno));
190 /* Returns true on error, false otherwise. */
191 static bool check_ruleset_scope(const char *const env_var,
192 struct landlock_ruleset_attr *ruleset_attr)
194 char *env_type_scope, *env_type_scope_next, *ipc_scoping_name;
196 bool abstract_scoping = false;
197 bool signal_scoping = false;
199 /* Scoping is not supported by Landlock ABI */
200 if (!(ruleset_attr->scoped &
201 (LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | LANDLOCK_SCOPE_SIGNAL)))
204 env_type_scope = getenv(env_var);
205 /* Scoping is not supported by the user */
206 if (!env_type_scope || strcmp("", env_type_scope) == 0)
209 env_type_scope = strdup(env_type_scope);
210 env_type_scope_next = env_type_scope;
211 while ((ipc_scoping_name =
212 strsep(&env_type_scope_next, ENV_DELIMITER))) {
213 if (strcmp("a", ipc_scoping_name) == 0 && !abstract_scoping) {
214 abstract_scoping = true;
215 } else if (strcmp("s", ipc_scoping_name) == 0 &&
217 signal_scoping = true;
219 fprintf(stderr, "Unknown or duplicate scope \"%s\"\n",
227 free(env_type_scope);
230 if (!abstract_scoping)
231 ruleset_attr->scoped &= ~LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET;
233 ruleset_attr->scoped &= ~LANDLOCK_SCOPE_SIGNAL;
239 /* clang-format off */
241 #define ACCESS_FS_ROUGHLY_READ ( \
242 LANDLOCK_ACCESS_FS_EXECUTE | \
243 LANDLOCK_ACCESS_FS_READ_FILE | \
244 LANDLOCK_ACCESS_FS_READ_DIR)
246 #define ACCESS_FS_ROUGHLY_WRITE ( \
247 LANDLOCK_ACCESS_FS_WRITE_FILE | \
248 LANDLOCK_ACCESS_FS_REMOVE_DIR | \
249 LANDLOCK_ACCESS_FS_REMOVE_FILE | \
250 LANDLOCK_ACCESS_FS_MAKE_CHAR | \
251 LANDLOCK_ACCESS_FS_MAKE_DIR | \
252 LANDLOCK_ACCESS_FS_MAKE_REG | \
253 LANDLOCK_ACCESS_FS_MAKE_SOCK | \
254 LANDLOCK_ACCESS_FS_MAKE_FIFO | \
255 LANDLOCK_ACCESS_FS_MAKE_BLOCK | \
256 LANDLOCK_ACCESS_FS_MAKE_SYM | \
257 LANDLOCK_ACCESS_FS_REFER | \
258 LANDLOCK_ACCESS_FS_TRUNCATE | \
259 LANDLOCK_ACCESS_FS_IOCTL_DEV)
261 /* clang-format on */
263 #define LANDLOCK_ABI_LAST 6
265 int main(const int argc, char *const argv[], char *const *const envp)
267 const char *cmd_path;
268 char *const *cmd_argv;
271 __u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ,
272 access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE;
274 struct landlock_ruleset_attr ruleset_attr = {
275 .handled_access_fs = access_fs_rw,
276 .handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
277 LANDLOCK_ACCESS_NET_CONNECT_TCP,
278 .scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
279 LANDLOCK_SCOPE_SIGNAL,
284 "usage: %s=\"...\" %s=\"...\" %s=\"...\" %s=\"...\" %s=\"...\" %s "
285 "<cmd> [args]...\n\n",
286 ENV_FS_RO_NAME, ENV_FS_RW_NAME, ENV_TCP_BIND_NAME,
287 ENV_TCP_CONNECT_NAME, ENV_SCOPED_NAME, argv[0]);
289 "Execute a command in a restricted environment.\n\n");
291 "Environment variables containing paths and ports "
292 "each separated by a colon:\n");
294 "* %s: list of paths allowed to be used in a read-only way.\n",
297 "* %s: list of paths allowed to be used in a read-write way.\n\n",
300 "Environment variables containing ports are optional "
301 "and could be skipped.\n");
303 "* %s: list of ports allowed to bind (server).\n",
306 "* %s: list of ports allowed to connect (client).\n",
307 ENV_TCP_CONNECT_NAME);
308 fprintf(stderr, "* %s: list of scoped IPCs.\n",
312 "%s=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" "
313 "%s=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
318 ENV_FS_RO_NAME, ENV_FS_RW_NAME, ENV_TCP_BIND_NAME,
319 ENV_TCP_CONNECT_NAME, ENV_SCOPED_NAME, argv[0]);
321 "This sandboxer can use Landlock features "
322 "up to ABI version %d.\n",
327 abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
329 const int err = errno;
331 perror("Failed to check Landlock compatibility");
335 "Hint: Landlock is not supported by the current kernel. "
336 "To support it, build the kernel with "
337 "CONFIG_SECURITY_LANDLOCK=y and prepend "
338 "\"landlock,\" to the content of CONFIG_LSM.\n");
342 "Hint: Landlock is currently disabled. "
343 "It can be enabled in the kernel configuration by "
344 "prepending \"landlock,\" to the content of CONFIG_LSM, "
345 "or at boot time by setting the same content to the "
346 "\"lsm\" kernel parameter.\n");
352 /* Best-effort security. */
356 * Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2
358 * Note: The "refer" operations (file renaming and linking
359 * across different directories) are always forbidden when using
360 * Landlock with ABI 1.
362 * If only ABI 1 is available, this sandboxer knowingly forbids
365 * If a program *needs* to do refer operations after enabling
366 * Landlock, it can not use Landlock at ABI level 1. To be
367 * compatible with different kernel versions, such programs
368 * should then fall back to not restrict themselves at all if
369 * the running kernel only supports ABI 1.
371 ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
372 __attribute__((fallthrough));
374 /* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
375 ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
376 __attribute__((fallthrough));
378 /* Removes network support for ABI < 4 */
379 ruleset_attr.handled_access_net &=
380 ~(LANDLOCK_ACCESS_NET_BIND_TCP |
381 LANDLOCK_ACCESS_NET_CONNECT_TCP);
382 __attribute__((fallthrough));
384 /* Removes LANDLOCK_ACCESS_FS_IOCTL_DEV for ABI < 5 */
385 ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
387 __attribute__((fallthrough));
389 /* Removes LANDLOCK_SCOPE_* for ABI < 6 */
390 ruleset_attr.scoped &= ~(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
391 LANDLOCK_SCOPE_SIGNAL);
393 "Hint: You should update the running kernel "
394 "to leverage Landlock features "
395 "provided by ABI version %d (instead of %d).\n",
396 LANDLOCK_ABI_LAST, abi);
397 __attribute__((fallthrough));
398 case LANDLOCK_ABI_LAST:
402 "Hint: You should update this sandboxer "
403 "to leverage Landlock features "
404 "provided by ABI version %d (instead of %d).\n",
405 abi, LANDLOCK_ABI_LAST);
407 access_fs_ro &= ruleset_attr.handled_access_fs;
408 access_fs_rw &= ruleset_attr.handled_access_fs;
410 /* Removes bind access attribute if not supported by a user. */
411 env_port_name = getenv(ENV_TCP_BIND_NAME);
412 if (!env_port_name) {
413 ruleset_attr.handled_access_net &=
414 ~LANDLOCK_ACCESS_NET_BIND_TCP;
416 /* Removes connect access attribute if not supported by a user. */
417 env_port_name = getenv(ENV_TCP_CONNECT_NAME);
418 if (!env_port_name) {
419 ruleset_attr.handled_access_net &=
420 ~LANDLOCK_ACCESS_NET_CONNECT_TCP;
423 if (check_ruleset_scope(ENV_SCOPED_NAME, &ruleset_attr))
427 landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
428 if (ruleset_fd < 0) {
429 perror("Failed to create a ruleset");
433 if (populate_ruleset_fs(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro)) {
434 goto err_close_ruleset;
436 if (populate_ruleset_fs(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw)) {
437 goto err_close_ruleset;
440 if (populate_ruleset_net(ENV_TCP_BIND_NAME, ruleset_fd,
441 LANDLOCK_ACCESS_NET_BIND_TCP)) {
442 goto err_close_ruleset;
444 if (populate_ruleset_net(ENV_TCP_CONNECT_NAME, ruleset_fd,
445 LANDLOCK_ACCESS_NET_CONNECT_TCP)) {
446 goto err_close_ruleset;
449 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
450 perror("Failed to restrict privileges");
451 goto err_close_ruleset;
453 if (landlock_restrict_self(ruleset_fd, 0)) {
454 perror("Failed to enforce ruleset");
455 goto err_close_ruleset;
461 fprintf(stderr, "Executing the sandboxed command...\n");
462 execvpe(cmd_path, cmd_argv, envp);
463 fprintf(stderr, "Failed to execute \"%s\": %s\n", cmd_path,
465 fprintf(stderr, "Hint: access to the binary, the interpreter or "
466 "shared libraries may be denied.\n");