| // SPDX-License-Identifier: BSD-3-Clause | |
| /* | |
| * Simple Landlock sandbox manager able to execute a process restricted by | |
| * user-defined file system and network access control policies. | |
| * | |
| * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net> | |
| * Copyright © 2020 ANSSI | |
| */ | |
| #define _GNU_SOURCE | |
| #define __SANE_USERSPACE_TYPES__ | |
| #include <arpa/inet.h> | |
| #include <errno.h> | |
| #include <fcntl.h> | |
| #include <limits.h> | |
| #include <linux/landlock.h> | |
| #include <linux/sched.h> | |
| #include <linux/socket.h> | |
| #include <stdbool.h> | |
| #include <stddef.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <sys/prctl.h> | |
| #include <sys/stat.h> | |
| #include <sys/syscall.h> | |
| #include <unistd.h> | |
| #ifdef USE_LIBCAP | |
| #include <sys/capability.h> | |
| #endif | |
| #if defined(__GLIBC__) | |
| #include <linux/prctl.h> | |
| #endif | |
| #ifndef landlock_create_ruleset | |
| static inline int | |
| landlock_create_ruleset(const struct landlock_ruleset_attr *const attr, | |
| const size_t size, const __u32 flags) | |
| { | |
| return syscall(__NR_landlock_create_ruleset, attr, size, flags); | |
| } | |
| #endif | |
| #ifndef landlock_add_rule | |
| static inline int landlock_add_rule(const int ruleset_fd, | |
| const enum landlock_rule_type rule_type, | |
| const void *const rule_attr, | |
| const __u32 flags) | |
| { | |
| return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr, | |
| flags); | |
| } | |
| #endif | |
| #ifndef landlock_restrict_self | |
| static inline int landlock_restrict_self(const int ruleset_fd, | |
| const __u32 flags) | |
| { | |
| return syscall(__NR_landlock_restrict_self, ruleset_fd, flags); | |
| } | |
| #endif | |
| #define ENV_FS_RO_NAME "LL_FS_RO" | |
| #define ENV_FS_RW_NAME "LL_FS_RW" | |
| #define ENV_FS_QUIET_NAME "LL_FS_QUIET" | |
| #define ENV_TCP_BIND_NAME "LL_TCP_BIND" | |
| #define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT" | |
| #define ENV_NET_QUIET_NAME "LL_NET_QUIET" | |
| #define ENV_NS_NAME "LL_NS" | |
| #define ENV_NS_QUIET_NAME "LL_NS_QUIET" | |
| #define ENV_CAP_NAME "LL_CAP" | |
| #define ENV_CAP_QUIET_NAME "LL_CAP_QUIET" | |
| #define ENV_SCOPED_NAME "LL_SCOPED" | |
| #define ENV_QUIET_ACCESS_NAME "LL_QUIET_ACCESS" | |
| #define ENV_FORCE_LOG_NAME "LL_FORCE_LOG" | |
| #define ENV_UDP_BIND_NAME "LL_UDP_BIND" | |
| #define ENV_UDP_CONNECT_SEND_NAME "LL_UDP_CONNECT_SEND" | |
| #define ENV_DELIMITER ":" | |
| #ifdef USE_LIBCAP | |
| #define CAP_VALUE_DESCRIPTION "names or numbers" | |
| #define CAP_VALUE_EXAMPLES "cap_net_bind_service, cap_sys_admin, 18" | |
| #define CAP_VALUE_EXAMPLE "cap_sys_admin" | |
| #else | |
| #define CAP_VALUE_DESCRIPTION "numbers" | |
| #define CAP_VALUE_EXAMPLES "10, 18, 21" | |
| #define CAP_VALUE_EXAMPLE "21" | |
| #endif | |
| #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) | |
| static int str2num(const char *numstr, __u64 *num_dst) | |
| { | |
| char *endptr = NULL; | |
| int err = 0; | |
| __u64 num; | |
| errno = 0; | |
| num = strtoull(numstr, &endptr, 10); | |
| if (errno != 0) | |
| err = errno; | |
| /* Was the string empty, or not entirely parsed successfully? */ | |
| else if ((*numstr == '\0') || (*endptr != '\0')) | |
| err = EINVAL; | |
| else | |
| *num_dst = num; | |
| return err; | |
| } | |
| static int parse_path(char *env_path, const char ***const path_list) | |
| { | |
| int i, num_paths = 0; | |
| if (env_path) { | |
| num_paths++; | |
| for (i = 0; env_path[i]; i++) { | |
| if (env_path[i] == ENV_DELIMITER[0]) | |
| num_paths++; | |
| } | |
| } | |
| *path_list = malloc(num_paths * sizeof(**path_list)); | |
| if (!*path_list) | |
| return -1; | |
| for (i = 0; i < num_paths; i++) | |
| (*path_list)[i] = strsep(&env_path, ENV_DELIMITER); | |
| return num_paths; | |
| } | |
| /* clang-format off */ | |
| #define ACCESS_FILE ( \ | |
| LANDLOCK_ACCESS_FS_EXECUTE | \ | |
| LANDLOCK_ACCESS_FS_WRITE_FILE | \ | |
| LANDLOCK_ACCESS_FS_READ_FILE | \ | |
| LANDLOCK_ACCESS_FS_TRUNCATE | \ | |
| LANDLOCK_ACCESS_FS_IOCTL_DEV | \ | |
| LANDLOCK_ACCESS_FS_RESOLVE_UNIX) | |
| /* clang-format on */ | |
| static int populate_ruleset_fs(const char *const env_var, const int ruleset_fd, | |
| const __u64 allowed_access, __u32 flags) | |
| { | |
| int num_paths, i, ret = 1; | |
| char *env_path_name; | |
| const char **path_list = NULL; | |
| struct landlock_path_beneath_attr path_beneath = { | |
| .parent_fd = -1, | |
| }; | |
| env_path_name = getenv(env_var); | |
| if (!env_path_name) { | |
| /* Prevents users to forget a setting. */ | |
| fprintf(stderr, "Missing environment variable %s\n", env_var); | |
| return 1; | |
| } | |
| env_path_name = strdup(env_path_name); | |
| unsetenv(env_var); | |
| num_paths = parse_path(env_path_name, &path_list); | |
| if (num_paths < 0) { | |
| fprintf(stderr, "Failed to allocate memory\n"); | |
| goto out_free_name; | |
| } | |
| if (num_paths == 1 && path_list[0][0] == '\0') { | |
| /* | |
| * Allows to not use all possible restrictions (e.g. use | |
| * LL_FS_RO without LL_FS_RW). | |
| */ | |
| ret = 0; | |
| goto out_free_name; | |
| } | |
| for (i = 0; i < num_paths; i++) { | |
| struct stat statbuf; | |
| path_beneath.parent_fd = open(path_list[i], O_PATH | O_CLOEXEC); | |
| if (path_beneath.parent_fd < 0) { | |
| fprintf(stderr, "Failed to open \"%s\": %s\n", | |
| path_list[i], strerror(errno)); | |
| continue; | |
| } | |
| if (fstat(path_beneath.parent_fd, &statbuf)) { | |
| fprintf(stderr, "Failed to stat \"%s\": %s\n", | |
| path_list[i], strerror(errno)); | |
| close(path_beneath.parent_fd); | |
| goto out_free_name; | |
| } | |
| path_beneath.allowed_access = allowed_access; | |
| if (!S_ISDIR(statbuf.st_mode)) | |
| path_beneath.allowed_access &= ACCESS_FILE; | |
| if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, | |
| &path_beneath, flags)) { | |
| fprintf(stderr, | |
| "Failed to update the ruleset with \"%s\": %s\n", | |
| path_list[i], strerror(errno)); | |
| close(path_beneath.parent_fd); | |
| goto out_free_name; | |
| } | |
| close(path_beneath.parent_fd); | |
| } | |
| ret = 0; | |
| out_free_name: | |
| free(path_list); | |
| free(env_path_name); | |
| return ret; | |
| } | |
| static int populate_ruleset_net(const char *const env_var, const int ruleset_fd, | |
| const __u64 allowed_access, __u32 flags) | |
| { | |
| int ret = 1; | |
| char *env_port_name, *env_port_name_next, *strport; | |
| struct landlock_net_port_attr net_port = { | |
| .allowed_access = allowed_access, | |
| }; | |
| env_port_name = getenv(env_var); | |
| if (!env_port_name) | |
| return 0; | |
| env_port_name = strdup(env_port_name); | |
| unsetenv(env_var); | |
| env_port_name_next = env_port_name; | |
| while ((strport = strsep(&env_port_name_next, ENV_DELIMITER))) { | |
| __u64 port; | |
| if (strcmp(strport, "") == 0) | |
| continue; | |
| if (str2num(strport, &port)) { | |
| fprintf(stderr, "Failed to parse port at \"%s\"\n", | |
| strport); | |
| goto out_free_name; | |
| } | |
| net_port.port = port; | |
| if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT, | |
| &net_port, flags)) { | |
| fprintf(stderr, | |
| "Failed to update the ruleset with port \"%llu\": %s\n", | |
| net_port.port, strerror(errno)); | |
| goto out_free_name; | |
| } | |
| } | |
| ret = 0; | |
| out_free_name: | |
| free(env_port_name); | |
| return ret; | |
| } | |
| static __u64 str2ns(const char *const name) | |
| { | |
| static const struct { | |
| const char *name; | |
| __u64 value; | |
| } ns_map[] = { | |
| /* clang-format off */ | |
| { "cgroup", CLONE_NEWCGROUP }, | |
| { "ipc", CLONE_NEWIPC }, | |
| { "mnt", CLONE_NEWNS }, | |
| { "net", CLONE_NEWNET }, | |
| { "pid", CLONE_NEWPID }, | |
| { "time", CLONE_NEWTIME }, | |
| { "user", CLONE_NEWUSER }, | |
| { "uts", CLONE_NEWUTS }, | |
| /* clang-format on */ | |
| }; | |
| size_t i; | |
| for (i = 0; i < ARRAY_SIZE(ns_map); i++) { | |
| if (strcmp(name, ns_map[i].name) == 0) | |
| return ns_map[i].value; | |
| } | |
| return 0; | |
| } | |
| /* | |
| * Parses a colon-delimited list of namespace type names into a bitmask. | |
| * Returns 0 on success (mask 0 when the variable is unset or empty), or 1 on a | |
| * parse error. | |
| */ | |
| static int parse_ns_list(const char *const env_var, __u64 *const mask) | |
| { | |
| int ret = 1; | |
| char *env_ns_name, *env_ns_name_next, *strns; | |
| *mask = 0; | |
| env_ns_name = getenv(env_var); | |
| if (!env_ns_name) | |
| return 0; | |
| env_ns_name = strdup(env_ns_name); | |
| unsetenv(env_var); | |
| env_ns_name_next = env_ns_name; | |
| while ((strns = strsep(&env_ns_name_next, ENV_DELIMITER))) { | |
| __u64 ns_type; | |
| if (strcmp(strns, "") == 0) | |
| continue; | |
| ns_type = str2ns(strns); | |
| if (!ns_type) { | |
| fprintf(stderr, "Unknown namespace type \"%s\"\n", | |
| strns); | |
| goto out_free_name; | |
| } | |
| *mask |= ns_type; | |
| } | |
| ret = 0; | |
| out_free_name: | |
| free(env_ns_name); | |
| return ret; | |
| } | |
| static int populate_ruleset_ns(const char *const allowed_env, | |
| const char *const quiet_env, | |
| const int ruleset_fd) | |
| { | |
| struct landlock_namespace_attr ns_attr = { | |
| .permissions = LANDLOCK_PERMISSION_NAMESPACE_USE, | |
| }; | |
| if (parse_ns_list(allowed_env, &ns_attr.allowed_namespace_types)) | |
| return 1; | |
| if (parse_ns_list(quiet_env, &ns_attr.quiet_namespace_types)) | |
| return 1; | |
| if (!ns_attr.allowed_namespace_types && !ns_attr.quiet_namespace_types) | |
| return 0; | |
| if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NAMESPACE, &ns_attr, | |
| 0)) { | |
| fprintf(stderr, | |
| "Failed to update the ruleset with namespace types: %s\n", | |
| strerror(errno)); | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| static int str2cap(const char *const strcap, __u64 *const cap) | |
| { | |
| #ifdef USE_LIBCAP | |
| cap_value_t value; | |
| #endif | |
| if (!str2num(strcap, cap)) | |
| return 0; | |
| #ifdef USE_LIBCAP | |
| if (cap_from_name(strcap, &value)) | |
| return 1; | |
| *cap = value; | |
| return 0; | |
| #else | |
| return 1; | |
| #endif | |
| } | |
| /* | |
| * Parses a colon-delimited list of capability values into a bitmask. Returns 0 | |
| * on success (mask 0 when the variable is unset or empty), or 1 on a parse | |
| * error. | |
| */ | |
| static int parse_cap_list(const char *const env_var, __u64 *const mask) | |
| { | |
| int ret = 1; | |
| char *env_cap_name, *env_cap_name_next, *strcap; | |
| *mask = 0; | |
| env_cap_name = getenv(env_var); | |
| if (!env_cap_name) | |
| return 0; | |
| env_cap_name = strdup(env_cap_name); | |
| unsetenv(env_var); | |
| env_cap_name_next = env_cap_name; | |
| while ((strcap = strsep(&env_cap_name_next, ENV_DELIMITER))) { | |
| __u64 cap; | |
| if (strcmp(strcap, "") == 0) | |
| continue; | |
| if (str2cap(strcap, &cap)) { | |
| fprintf(stderr, "Failed to parse capability \"%s\"\n", | |
| strcap); | |
| goto out_free_name; | |
| } | |
| if (cap >= sizeof(*mask) * CHAR_BIT) { | |
| fprintf(stderr, "Capability \"%s\" is out of range\n", | |
| strcap); | |
| goto out_free_name; | |
| } | |
| *mask |= 1ULL << cap; | |
| } | |
| ret = 0; | |
| out_free_name: | |
| free(env_cap_name); | |
| return ret; | |
| } | |
| static int populate_ruleset_cap(const char *const allowed_env, | |
| const char *const quiet_env, | |
| const int ruleset_fd) | |
| { | |
| struct landlock_capability_attr cap_attr = { | |
| .permissions = LANDLOCK_PERMISSION_CAPABILITY_USE, | |
| }; | |
| if (parse_cap_list(allowed_env, &cap_attr.allowed_capabilities)) | |
| return 1; | |
| if (parse_cap_list(quiet_env, &cap_attr.quiet_capabilities)) | |
| return 1; | |
| if (!cap_attr.allowed_capabilities && !cap_attr.quiet_capabilities) | |
| return 0; | |
| if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_CAPABILITY, &cap_attr, | |
| 0)) { | |
| fprintf(stderr, | |
| "Failed to update the ruleset with capabilities: %s\n", | |
| strerror(errno)); | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| /* Returns true on error, false otherwise. */ | |
| static bool check_ruleset_scope(const char *const env_var, | |
| struct landlock_ruleset_attr *ruleset_attr) | |
| { | |
| char *env_type_scope, *env_type_scope_next, *ipc_scoping_name; | |
| bool error = false; | |
| bool abstract_scoping = false; | |
| bool signal_scoping = false; | |
| /* Scoping is not supported by Landlock ABI */ | |
| if (!(ruleset_attr->scoped & | |
| (LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | LANDLOCK_SCOPE_SIGNAL))) | |
| goto out_unset; | |
| env_type_scope = getenv(env_var); | |
| /* Scoping is not supported by the user */ | |
| if (!env_type_scope || strcmp("", env_type_scope) == 0) | |
| goto out_unset; | |
| env_type_scope = strdup(env_type_scope); | |
| env_type_scope_next = env_type_scope; | |
| while ((ipc_scoping_name = | |
| strsep(&env_type_scope_next, ENV_DELIMITER))) { | |
| if (strcmp("a", ipc_scoping_name) == 0 && !abstract_scoping) { | |
| abstract_scoping = true; | |
| } else if (strcmp("s", ipc_scoping_name) == 0 && | |
| !signal_scoping) { | |
| signal_scoping = true; | |
| } else { | |
| fprintf(stderr, "Unknown or duplicate scope \"%s\"\n", | |
| ipc_scoping_name); | |
| error = true; | |
| goto out_free_name; | |
| } | |
| } | |
| out_free_name: | |
| free(env_type_scope); | |
| out_unset: | |
| if (!abstract_scoping) | |
| ruleset_attr->scoped &= ~LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET; | |
| if (!signal_scoping) | |
| ruleset_attr->scoped &= ~LANDLOCK_SCOPE_SIGNAL; | |
| unsetenv(env_var); | |
| return error; | |
| } | |
| /* clang-format off */ | |
| #define ACCESS_FS_ROUGHLY_READ ( \ | |
| LANDLOCK_ACCESS_FS_EXECUTE | \ | |
| LANDLOCK_ACCESS_FS_READ_FILE | \ | |
| LANDLOCK_ACCESS_FS_READ_DIR) | |
| #define ACCESS_FS_ROUGHLY_WRITE ( \ | |
| LANDLOCK_ACCESS_FS_WRITE_FILE | \ | |
| LANDLOCK_ACCESS_FS_REMOVE_DIR | \ | |
| LANDLOCK_ACCESS_FS_REMOVE_FILE | \ | |
| LANDLOCK_ACCESS_FS_MAKE_CHAR | \ | |
| LANDLOCK_ACCESS_FS_MAKE_DIR | \ | |
| LANDLOCK_ACCESS_FS_MAKE_REG | \ | |
| LANDLOCK_ACCESS_FS_MAKE_SOCK | \ | |
| LANDLOCK_ACCESS_FS_MAKE_FIFO | \ | |
| LANDLOCK_ACCESS_FS_MAKE_BLOCK | \ | |
| LANDLOCK_ACCESS_FS_MAKE_SYM | \ | |
| LANDLOCK_ACCESS_FS_REFER | \ | |
| LANDLOCK_ACCESS_FS_TRUNCATE | \ | |
| LANDLOCK_ACCESS_FS_IOCTL_DEV | \ | |
| LANDLOCK_ACCESS_FS_RESOLVE_UNIX) | |
| /* clang-format on */ | |
| /* | |
| * Parses ENV_QUIET_ACCESS_NAME and sets the quiet_access_fs, quiet_access_net | |
| * and quiet_scoped masks of @ruleset_attr accordingly. | |
| */ | |
| static int add_quiet_access(const char *const env_var, | |
| struct landlock_ruleset_attr *const ruleset_attr) | |
| { | |
| char *env_quiet_access, *env_quiet_access_next, *str_access; | |
| env_quiet_access = getenv(env_var); | |
| if (!env_quiet_access) | |
| return 0; | |
| env_quiet_access = strdup(env_quiet_access); | |
| env_quiet_access_next = env_quiet_access; | |
| unsetenv(env_var); | |
| while ((str_access = strsep(&env_quiet_access_next, ENV_DELIMITER))) { | |
| if (strcmp(str_access, "") == 0) | |
| continue; | |
| else if (strcmp(str_access, "all") == 0) { | |
| ruleset_attr->quiet_access_fs = | |
| ruleset_attr->handled_access_fs; | |
| ruleset_attr->quiet_access_net = | |
| ruleset_attr->handled_access_net; | |
| ruleset_attr->quiet_scoped = ruleset_attr->scoped; | |
| } else if (strcmp(str_access, "read") == 0) | |
| ruleset_attr->quiet_access_fs |= ACCESS_FS_ROUGHLY_READ; | |
| else if (strcmp(str_access, "write") == 0) | |
| ruleset_attr->quiet_access_fs |= | |
| ACCESS_FS_ROUGHLY_WRITE; | |
| else if (strcmp(str_access, "tcp_bind") == 0) | |
| ruleset_attr->quiet_access_net |= | |
| LANDLOCK_ACCESS_NET_BIND_TCP; | |
| else if (strcmp(str_access, "tcp_connect") == 0) | |
| ruleset_attr->quiet_access_net |= | |
| LANDLOCK_ACCESS_NET_CONNECT_TCP; | |
| else if (strcmp(str_access, "udp_bind") == 0) | |
| ruleset_attr->quiet_access_net |= | |
| LANDLOCK_ACCESS_NET_BIND_UDP; | |
| else if (strcmp(str_access, "udp_connect") == 0) | |
| ruleset_attr->quiet_access_net |= | |
| LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP; | |
| else if (strcmp(str_access, "abstract_unix_socket") == 0) | |
| ruleset_attr->quiet_scoped |= | |
| LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET; | |
| else if (strcmp(str_access, "signal") == 0) | |
| ruleset_attr->quiet_scoped |= LANDLOCK_SCOPE_SIGNAL; | |
| else { | |
| fprintf(stderr, "Unknown quiet access \"%s\"\n", | |
| str_access); | |
| free(env_quiet_access); | |
| return -1; | |
| } | |
| } | |
| free(env_quiet_access); | |
| ruleset_attr->quiet_access_fs &= ruleset_attr->handled_access_fs; | |
| ruleset_attr->quiet_access_net &= ruleset_attr->handled_access_net; | |
| ruleset_attr->quiet_scoped &= ruleset_attr->scoped; | |
| return 0; | |
| } | |
| #define LANDLOCK_ABI_LAST 12 | |
| #define XSTR(s) #s | |
| #define STR(s) XSTR(s) | |
| /* clang-format off */ | |
| static const char help[] = | |
| "usage: " ENV_FS_RO_NAME "=\"...\" " ENV_FS_RW_NAME "=\"...\" " | |
| "[other environment variables] %1$s <cmd> [args]...\n" | |
| "\n" | |
| "Execute the given command in a restricted environment.\n" | |
| "Multi-valued settings (lists of ports, paths, scopes) are colon-delimited.\n" | |
| "\n" | |
| "Mandatory settings:\n" | |
| "* " ENV_FS_RO_NAME ": paths allowed to be used in a read-only way\n" | |
| "* " ENV_FS_RW_NAME ": paths allowed to be used in a read-write way\n" | |
| "\n" | |
| "Optional settings (when not set, their associated access check " | |
| "is always allowed, which is different from an empty string which " | |
| "means an empty list):\n" | |
| "* " ENV_TCP_BIND_NAME ": ports allowed to bind (server)\n" | |
| "* " ENV_TCP_CONNECT_NAME ": ports allowed to connect (client)\n" | |
| "* " ENV_UDP_BIND_NAME ": local UDP ports allowed to bind (server: " | |
| "prepare to receive on port / client: set as source port)\n" | |
| "* " ENV_UDP_CONNECT_SEND_NAME ": remote UDP ports allowed to connect " | |
| "or send to (client: use as destination port / server: receive only from it)\n" | |
| "(caution: sending requires being able to bind to a local source port)\n" | |
| "* " ENV_NS_NAME ": namespace types allowed to use\n" | |
| " (cgroup, ipc, mnt, net, pid, time, user, uts)\n" | |
| "* " ENV_NS_QUIET_NAME | |
| ": namespace types whose denial should not be audit logged\n" | |
| " (same value format as " ENV_NS_NAME | |
| "; quieting an allowed member is inert,\n" | |
| " as an allowed member is never denied; setting it alone still\n" | |
| " restricts namespace use, denying every type)\n" | |
| "* " ENV_CAP_NAME ": capabilities allowed to use, as " | |
| CAP_VALUE_DESCRIPTION "\n" | |
| " (e.g. " CAP_VALUE_EXAMPLES ")\n" | |
| "* " ENV_CAP_QUIET_NAME | |
| ": capabilities whose denial should not be audit logged\n" | |
| " (same value format as " ENV_CAP_NAME | |
| "; quieting an allowed member is inert,\n" | |
| " as an allowed member is never denied; setting it alone still\n" | |
| " restricts capability use, denying every capability)\n" | |
| "* " ENV_SCOPED_NAME ": actions denied on the outside of the landlock domain\n" | |
| " - \"a\" to restrict opening abstract unix sockets\n" | |
| " - \"s\" to restrict sending signals\n" | |
| "\n" | |
| "A sandboxer should not log denied access requests to avoid spamming logs, " | |
| "but to test audit we can set " ENV_FORCE_LOG_NAME "=1\n" | |
| ENV_FS_QUIET_NAME " and " ENV_NET_QUIET_NAME ", both optional, can then be used " | |
| "to make access to some denied paths or network ports not trigger audit logging.\n" | |
| ENV_QUIET_ACCESS_NAME " can be used to specify which accesses should be quieted " | |
| "(required when " ENV_FS_QUIET_NAME " or " ENV_NET_QUIET_NAME " is set):\n" | |
| " - \"all\" to quiet all of the accesses below\n" | |
| " - \"read\" to quiet all file/dir read accesses\n" | |
| " - \"write\" to quiet all file/dir write accesses\n" | |
| " - \"tcp_bind\" to quiet tcp bind denials\n" | |
| " - \"tcp_connect\" to quiet tcp connect denials\n" | |
| " - \"udp_bind\" to quiet udp bind denials\n" | |
| " - \"udp_connect\" to quiet udp connect / send denials\n" | |
| " - \"abstract_unix_socket\" to quiet abstract unix socket denials\n" | |
| " - \"signal\" to quiet signal denials\n" | |
| "\n" | |
| "Example:\n" | |
| ENV_FS_RO_NAME "=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" " | |
| ENV_FS_RW_NAME "=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" " | |
| ENV_TCP_BIND_NAME "=\"9418\" " | |
| ENV_TCP_CONNECT_NAME "=\"80:443\" " | |
| ENV_UDP_CONNECT_SEND_NAME "=\"53\" " | |
| ENV_NS_NAME "=\"user:uts:net\" " | |
| ENV_CAP_NAME "=\"" CAP_VALUE_EXAMPLE "\" " | |
| ENV_SCOPED_NAME "=\"a:s\" " | |
| "%1$s bash -i\n" | |
| "\n" | |
| "This sandboxer can use Landlock features up to ABI version " | |
| STR(LANDLOCK_ABI_LAST) ".\n"; | |
| /* clang-format on */ | |
| int main(const int argc, char *const argv[], char *const *const envp) | |
| { | |
| const char *cmd_path; | |
| char *const *cmd_argv; | |
| int ruleset_fd, abi; | |
| char *env_port_name, *env_force_log; | |
| __u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ, | |
| access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE; | |
| struct landlock_ruleset_attr ruleset_attr = { | |
| .handled_access_fs = access_fs_rw, | |
| .handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP | | |
| LANDLOCK_ACCESS_NET_CONNECT_TCP | | |
| LANDLOCK_ACCESS_NET_BIND_UDP | | |
| LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP, | |
| .scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | | |
| LANDLOCK_SCOPE_SIGNAL, | |
| .quiet_access_fs = 0, | |
| .quiet_access_net = 0, | |
| .quiet_scoped = 0, | |
| .handled_permissions = LANDLOCK_PERMISSION_NAMESPACE_USE | | |
| LANDLOCK_PERMISSION_CAPABILITY_USE, | |
| }; | |
| bool quiet_supported = true; | |
| int supported_restrict_flags = LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON | | |
| LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS; | |
| int set_restrict_flags = LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS; | |
| if (argc < 2) { | |
| fprintf(stderr, help, argv[0]); | |
| return 1; | |
| } | |
| abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION); | |
| if (abi < 0) { | |
| const int err = errno; | |
| perror("Failed to check Landlock compatibility"); | |
| switch (err) { | |
| case ENOSYS: | |
| fprintf(stderr, | |
| "Hint: Landlock is not supported by the current kernel. " | |
| "To support it, build the kernel with " | |
| "CONFIG_SECURITY_LANDLOCK=y and prepend " | |
| "\"landlock,\" to the content of CONFIG_LSM.\n"); | |
| break; | |
| case EOPNOTSUPP: | |
| fprintf(stderr, | |
| "Hint: Landlock is currently disabled. " | |
| "It can be enabled in the kernel configuration by " | |
| "prepending \"landlock,\" to the content of CONFIG_LSM, " | |
| "or at boot time by setting the same content to the " | |
| "\"lsm\" kernel parameter.\n"); | |
| break; | |
| } | |
| return 1; | |
| } | |
| /* Best-effort security. */ | |
| switch (abi) { | |
| case 1: | |
| /* | |
| * Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2 | |
| * | |
| * Note: The "refer" operations (file renaming and linking | |
| * across different directories) are always forbidden when using | |
| * Landlock with ABI 1. | |
| * | |
| * If only ABI 1 is available, this sandboxer knowingly forbids | |
| * refer operations. | |
| * | |
| * If a program *needs* to do refer operations after enabling | |
| * Landlock, it can not use Landlock at ABI level 1. To be | |
| * compatible with different kernel versions, such programs | |
| * should then fall back to not restrict themselves at all if | |
| * the running kernel only supports ABI 1. | |
| */ | |
| ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER; | |
| __attribute__((fallthrough)); | |
| case 2: | |
| /* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */ | |
| ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE; | |
| __attribute__((fallthrough)); | |
| case 3: | |
| /* Removes network support for ABI < 4 */ | |
| ruleset_attr.handled_access_net &= | |
| ~(LANDLOCK_ACCESS_NET_BIND_TCP | | |
| LANDLOCK_ACCESS_NET_CONNECT_TCP); | |
| __attribute__((fallthrough)); | |
| case 4: | |
| /* Removes LANDLOCK_ACCESS_FS_IOCTL_DEV for ABI < 5 */ | |
| ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV; | |
| __attribute__((fallthrough)); | |
| case 5: | |
| /* Removes LANDLOCK_SCOPE_* for ABI < 6 */ | |
| ruleset_attr.scoped &= ~(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | | |
| LANDLOCK_SCOPE_SIGNAL); | |
| __attribute__((fallthrough)); | |
| case 6: | |
| /* Removes LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON for ABI < 7 */ | |
| supported_restrict_flags &= | |
| ~LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON; | |
| __attribute__((fallthrough)); | |
| case 7: | |
| case 8: | |
| /* Removes LANDLOCK_ACCESS_FS_RESOLVE_UNIX for ABI < 9 */ | |
| ruleset_attr.handled_access_fs &= | |
| ~LANDLOCK_ACCESS_FS_RESOLVE_UNIX; | |
| __attribute__((fallthrough)); | |
| case 9: | |
| /* Removes UDP support for ABI < 10 */ | |
| ruleset_attr.handled_access_net &= | |
| ~(LANDLOCK_ACCESS_NET_BIND_UDP | | |
| LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP); | |
| /* Removes quiet flags for ABI < 10 later on. */ | |
| quiet_supported = false; | |
| __attribute__((fallthrough)); | |
| case 10: | |
| /* Removes LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS for ABI < 11 */ | |
| supported_restrict_flags &= | |
| ~LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS; | |
| set_restrict_flags &= ~LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS; | |
| __attribute__((fallthrough)); | |
| case 11: | |
| /* Removes LANDLOCK_PERMISSION_* for ABI < 12 */ | |
| ruleset_attr.handled_permissions &= | |
| ~(LANDLOCK_PERMISSION_NAMESPACE_USE | | |
| LANDLOCK_PERMISSION_CAPABILITY_USE); | |
| /* Must be printed for any ABI < LANDLOCK_ABI_LAST. */ | |
| fprintf(stderr, | |
| "Hint: You should update the running kernel " | |
| "to leverage Landlock features " | |
| "provided by ABI version %d (instead of %d).\n", | |
| LANDLOCK_ABI_LAST, abi); | |
| __attribute__((fallthrough)); | |
| case LANDLOCK_ABI_LAST: | |
| break; | |
| default: | |
| fprintf(stderr, | |
| "Hint: You should update this sandboxer " | |
| "to leverage Landlock features " | |
| "provided by ABI version %d (instead of %d).\n", | |
| abi, LANDLOCK_ABI_LAST); | |
| } | |
| access_fs_ro &= ruleset_attr.handled_access_fs; | |
| access_fs_rw &= ruleset_attr.handled_access_fs; | |
| /* Removes bind access attribute if not supported by a user. */ | |
| env_port_name = getenv(ENV_TCP_BIND_NAME); | |
| if (!env_port_name) { | |
| ruleset_attr.handled_access_net &= | |
| ~LANDLOCK_ACCESS_NET_BIND_TCP; | |
| } | |
| /* Removes connect access attribute if not supported by a user. */ | |
| env_port_name = getenv(ENV_TCP_CONNECT_NAME); | |
| if (!env_port_name) { | |
| ruleset_attr.handled_access_net &= | |
| ~LANDLOCK_ACCESS_NET_CONNECT_TCP; | |
| } | |
| /* Removes UDP bind access control if not supported by a user. */ | |
| env_port_name = getenv(ENV_UDP_BIND_NAME); | |
| if (!env_port_name) { | |
| ruleset_attr.handled_access_net &= | |
| ~LANDLOCK_ACCESS_NET_BIND_UDP; | |
| } | |
| /* Removes UDP connect/send access control if not supported by a user. */ | |
| env_port_name = getenv(ENV_UDP_CONNECT_SEND_NAME); | |
| if (!env_port_name) { | |
| ruleset_attr.handled_access_net &= | |
| ~LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP; | |
| } | |
| /* Removes namespace handling if not set by a user. */ | |
| if (!getenv(ENV_NS_NAME) && !getenv(ENV_NS_QUIET_NAME)) | |
| ruleset_attr.handled_permissions &= | |
| ~LANDLOCK_PERMISSION_NAMESPACE_USE; | |
| if (!(ruleset_attr.handled_permissions & | |
| LANDLOCK_PERMISSION_NAMESPACE_USE)) { | |
| unsetenv(ENV_NS_NAME); | |
| unsetenv(ENV_NS_QUIET_NAME); | |
| } | |
| #ifndef USE_LIBCAP | |
| if (getenv(ENV_CAP_NAME) || getenv(ENV_CAP_QUIET_NAME)) | |
| fprintf(stderr, | |
| "Warning: Built without libcap; use capability numbers, not names.\n"); | |
| #endif | |
| /* Removes capability handling if not set by a user. */ | |
| if (!getenv(ENV_CAP_NAME) && !getenv(ENV_CAP_QUIET_NAME)) | |
| ruleset_attr.handled_permissions &= | |
| ~LANDLOCK_PERMISSION_CAPABILITY_USE; | |
| if (!(ruleset_attr.handled_permissions & | |
| LANDLOCK_PERMISSION_CAPABILITY_USE)) { | |
| unsetenv(ENV_CAP_NAME); | |
| unsetenv(ENV_CAP_QUIET_NAME); | |
| } | |
| if (check_ruleset_scope(ENV_SCOPED_NAME, &ruleset_attr)) | |
| return 1; | |
| /* Enables optional logs. */ | |
| env_force_log = getenv(ENV_FORCE_LOG_NAME); | |
| if (env_force_log) { | |
| if (strcmp(env_force_log, "1") != 0) { | |
| fprintf(stderr, "Unknown value for " ENV_FORCE_LOG_NAME | |
| " (only \"1\" is handled)\n"); | |
| return 1; | |
| } | |
| if (!(supported_restrict_flags & | |
| LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON)) { | |
| fprintf(stderr, | |
| "Audit logs not supported by current kernel\n"); | |
| return 1; | |
| } | |
| set_restrict_flags |= LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON; | |
| unsetenv(ENV_FORCE_LOG_NAME); | |
| } | |
| /* Set the quiet access masks. */ | |
| if (quiet_supported) { | |
| if ((getenv(ENV_FS_QUIET_NAME) || getenv(ENV_NET_QUIET_NAME)) && | |
| !getenv(ENV_QUIET_ACCESS_NAME)) { | |
| fprintf(stderr, | |
| "%s must be set (e.g. to \"all\") when %s or %s is used\n", | |
| ENV_QUIET_ACCESS_NAME, ENV_FS_QUIET_NAME, | |
| ENV_NET_QUIET_NAME); | |
| return 1; | |
| } | |
| if (add_quiet_access(ENV_QUIET_ACCESS_NAME, &ruleset_attr)) | |
| return 1; | |
| } else if (getenv(ENV_FS_QUIET_NAME) || getenv(ENV_NET_QUIET_NAME) || | |
| getenv(ENV_QUIET_ACCESS_NAME)) { | |
| fprintf(stderr, | |
| "Quiet flags not supported by current kernel\n"); | |
| return 1; | |
| } | |
| ruleset_fd = | |
| landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); | |
| if (ruleset_fd < 0) { | |
| perror("Failed to create a ruleset"); | |
| return 1; | |
| } | |
| if (populate_ruleset_fs(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro, 0)) | |
| goto err_close_ruleset; | |
| if (populate_ruleset_fs(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw, 0)) | |
| goto err_close_ruleset; | |
| /* Don't require this env to be present. */ | |
| if (quiet_supported && getenv(ENV_FS_QUIET_NAME)) { | |
| if (populate_ruleset_fs(ENV_FS_QUIET_NAME, ruleset_fd, 0, | |
| LANDLOCK_ADD_RULE_QUIET)) | |
| goto err_close_ruleset; | |
| } | |
| if (populate_ruleset_net(ENV_TCP_BIND_NAME, ruleset_fd, | |
| LANDLOCK_ACCESS_NET_BIND_TCP, 0)) { | |
| goto err_close_ruleset; | |
| } | |
| if (populate_ruleset_net(ENV_TCP_CONNECT_NAME, ruleset_fd, | |
| LANDLOCK_ACCESS_NET_CONNECT_TCP, 0)) { | |
| goto err_close_ruleset; | |
| } | |
| if (populate_ruleset_net(ENV_UDP_BIND_NAME, ruleset_fd, | |
| LANDLOCK_ACCESS_NET_BIND_UDP, 0)) { | |
| goto err_close_ruleset; | |
| } | |
| if (populate_ruleset_net(ENV_UDP_CONNECT_SEND_NAME, ruleset_fd, | |
| LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP, 0)) { | |
| goto err_close_ruleset; | |
| } | |
| if (quiet_supported) { | |
| if (populate_ruleset_net(ENV_NET_QUIET_NAME, ruleset_fd, 0, | |
| LANDLOCK_ADD_RULE_QUIET)) { | |
| goto err_close_ruleset; | |
| } | |
| } | |
| if ((ruleset_attr.handled_permissions & | |
| LANDLOCK_PERMISSION_NAMESPACE_USE) && | |
| populate_ruleset_ns(ENV_NS_NAME, ENV_NS_QUIET_NAME, ruleset_fd)) | |
| goto err_close_ruleset; | |
| if ((ruleset_attr.handled_permissions & | |
| LANDLOCK_PERMISSION_CAPABILITY_USE) && | |
| populate_ruleset_cap(ENV_CAP_NAME, ENV_CAP_QUIET_NAME, ruleset_fd)) | |
| goto err_close_ruleset; | |
| if (!(set_restrict_flags & LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS) && | |
| prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { | |
| perror("Failed to restrict privileges"); | |
| goto err_close_ruleset; | |
| } | |
| if (landlock_restrict_self(ruleset_fd, set_restrict_flags)) { | |
| perror("Failed to enforce ruleset"); | |
| goto err_close_ruleset; | |
| } | |
| close(ruleset_fd); | |
| cmd_path = argv[1]; | |
| cmd_argv = argv + 1; | |
| fprintf(stderr, "Executing the sandboxed command...\n"); | |
| execvpe(cmd_path, cmd_argv, envp); | |
| fprintf(stderr, "Failed to execute \"%s\": %s\n", cmd_path, | |
| strerror(errno)); | |
| fprintf(stderr, "Hint: access to the binary, the interpreter or " | |
| "shared libraries may be denied.\n"); | |
| return 1; | |
| err_close_ruleset: | |
| close(ruleset_fd); | |
| return 1; | |
| } |