2021-04-22 15:41:21 +00:00
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
/*
|
2024-03-07 14:38:49 +00:00
|
|
|
* Simple Landlock sandbox manager able to execute a process restricted by
|
|
|
|
* user-defined file system and network access control policies.
|
2021-04-22 15:41:21 +00:00
|
|
|
*
|
|
|
|
* Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
|
|
|
|
* Copyright © 2020 ANSSI
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
2023-10-26 01:47:50 +00:00
|
|
|
#define __SANE_USERSPACE_TYPES__
|
|
|
|
#include <arpa/inet.h>
|
2021-04-22 15:41:21 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <linux/landlock.h>
|
|
|
|
#include <linux/prctl.h>
|
2024-09-05 00:14:01 +00:00
|
|
|
#include <linux/socket.h>
|
2021-04-22 15:41:21 +00:00
|
|
|
#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>
|
2024-09-05 00:14:01 +00:00
|
|
|
#include <stdbool.h>
|
2021-04-22 15:41:21 +00:00
|
|
|
|
|
|
|
#ifndef landlock_create_ruleset
|
2022-05-06 16:05:13 +00:00
|
|
|
static inline int
|
|
|
|
landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
|
|
|
|
const size_t size, const __u32 flags)
|
2021-04-22 15:41:21 +00:00
|
|
|
{
|
|
|
|
return syscall(__NR_landlock_create_ruleset, attr, size, flags);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef landlock_add_rule
|
|
|
|
static inline int landlock_add_rule(const int ruleset_fd,
|
2022-05-06 16:05:13 +00:00
|
|
|
const enum landlock_rule_type rule_type,
|
|
|
|
const void *const rule_attr,
|
|
|
|
const __u32 flags)
|
2021-04-22 15:41:21 +00:00
|
|
|
{
|
2022-05-06 16:05:13 +00:00
|
|
|
return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr,
|
|
|
|
flags);
|
2021-04-22 15:41:21 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef landlock_restrict_self
|
|
|
|
static inline int landlock_restrict_self(const int ruleset_fd,
|
2022-05-06 16:05:13 +00:00
|
|
|
const __u32 flags)
|
2021-04-22 15:41:21 +00:00
|
|
|
{
|
|
|
|
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"
|
2023-10-26 01:47:50 +00:00
|
|
|
#define ENV_TCP_BIND_NAME "LL_TCP_BIND"
|
|
|
|
#define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT"
|
2024-09-05 00:14:01 +00:00
|
|
|
#define ENV_SCOPED_NAME "LL_SCOPED"
|
2023-10-26 01:47:50 +00:00
|
|
|
#define ENV_DELIMITER ":"
|
2021-04-22 15:41:21 +00:00
|
|
|
|
|
|
|
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++) {
|
2023-10-26 01:47:50 +00:00
|
|
|
if (env_path[i] == ENV_DELIMITER[0])
|
2021-04-22 15:41:21 +00:00
|
|
|
num_paths++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*path_list = malloc(num_paths * sizeof(**path_list));
|
|
|
|
for (i = 0; i < num_paths; i++)
|
2023-10-26 01:47:50 +00:00
|
|
|
(*path_list)[i] = strsep(&env_path, ENV_DELIMITER);
|
2021-04-22 15:41:21 +00:00
|
|
|
|
|
|
|
return num_paths;
|
|
|
|
}
|
|
|
|
|
2022-05-06 16:05:12 +00:00
|
|
|
/* clang-format off */
|
|
|
|
|
2021-04-22 15:41:21 +00:00
|
|
|
#define ACCESS_FILE ( \
|
|
|
|
LANDLOCK_ACCESS_FS_EXECUTE | \
|
|
|
|
LANDLOCK_ACCESS_FS_WRITE_FILE | \
|
2022-10-18 18:22:15 +00:00
|
|
|
LANDLOCK_ACCESS_FS_READ_FILE | \
|
2024-04-19 16:11:19 +00:00
|
|
|
LANDLOCK_ACCESS_FS_TRUNCATE | \
|
|
|
|
LANDLOCK_ACCESS_FS_IOCTL_DEV)
|
2021-04-22 15:41:21 +00:00
|
|
|
|
2022-05-06 16:05:12 +00:00
|
|
|
/* clang-format on */
|
|
|
|
|
2023-10-26 01:47:50 +00:00
|
|
|
static int populate_ruleset_fs(const char *const env_var, const int ruleset_fd,
|
|
|
|
const __u64 allowed_access)
|
2021-04-22 15:41:21 +00:00
|
|
|
{
|
|
|
|
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 == 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;
|
|
|
|
|
2022-05-06 16:05:13 +00:00
|
|
|
path_beneath.parent_fd = open(path_list[i], O_PATH | O_CLOEXEC);
|
2021-04-22 15:41:21 +00:00
|
|
|
if (path_beneath.parent_fd < 0) {
|
|
|
|
fprintf(stderr, "Failed to open \"%s\": %s\n",
|
2022-05-06 16:05:13 +00:00
|
|
|
path_list[i], strerror(errno));
|
2024-03-07 14:38:49 +00:00
|
|
|
continue;
|
2021-04-22 15:41:21 +00:00
|
|
|
}
|
|
|
|
if (fstat(path_beneath.parent_fd, &statbuf)) {
|
2024-03-07 14:38:49 +00:00
|
|
|
fprintf(stderr, "Failed to stat \"%s\": %s\n",
|
|
|
|
path_list[i], strerror(errno));
|
2021-04-22 15:41:21 +00:00
|
|
|
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,
|
2022-05-06 16:05:13 +00:00
|
|
|
&path_beneath, 0)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Failed to update the ruleset with \"%s\": %s\n",
|
|
|
|
path_list[i], strerror(errno));
|
2021-04-22 15:41:21 +00:00
|
|
|
close(path_beneath.parent_fd);
|
|
|
|
goto out_free_name;
|
|
|
|
}
|
|
|
|
close(path_beneath.parent_fd);
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out_free_name:
|
2021-04-28 21:38:52 +00:00
|
|
|
free(path_list);
|
2021-04-22 15:41:21 +00:00
|
|
|
free(env_path_name);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-10-26 01:47:50 +00:00
|
|
|
static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
|
|
|
|
const __u64 allowed_access)
|
|
|
|
{
|
|
|
|
int ret = 1;
|
2024-03-26 09:56:25 +00:00
|
|
|
char *env_port_name, *env_port_name_next, *strport;
|
2023-10-26 01:47:50 +00:00
|
|
|
struct landlock_net_port_attr net_port = {
|
|
|
|
.allowed_access = allowed_access,
|
|
|
|
.port = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
env_port_name = getenv(env_var);
|
|
|
|
if (!env_port_name)
|
|
|
|
return 0;
|
|
|
|
env_port_name = strdup(env_port_name);
|
|
|
|
unsetenv(env_var);
|
|
|
|
|
2024-03-26 09:56:25 +00:00
|
|
|
env_port_name_next = env_port_name;
|
|
|
|
while ((strport = strsep(&env_port_name_next, ENV_DELIMITER))) {
|
2023-10-26 01:47:50 +00:00
|
|
|
net_port.port = atoi(strport);
|
|
|
|
if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
|
|
|
|
&net_port, 0)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-09-05 00:14:01 +00:00
|
|
|
/* 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;
|
2024-09-06 21:30:07 +00:00
|
|
|
bool signal_scoping = false;
|
2024-09-05 00:14:01 +00:00
|
|
|
|
|
|
|
/* Scoping is not supported by Landlock ABI */
|
2024-09-06 21:30:07 +00:00
|
|
|
if (!(ruleset_attr->scoped &
|
|
|
|
(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | LANDLOCK_SCOPE_SIGNAL)))
|
2024-09-05 00:14:01 +00:00
|
|
|
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;
|
2024-09-06 21:30:07 +00:00
|
|
|
} else if (strcmp("s", ipc_scoping_name) == 0 &&
|
|
|
|
!signal_scoping) {
|
|
|
|
signal_scoping = true;
|
2024-09-05 00:14:01 +00:00
|
|
|
} 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;
|
2024-09-06 21:30:07 +00:00
|
|
|
if (!signal_scoping)
|
|
|
|
ruleset_attr->scoped &= ~LANDLOCK_SCOPE_SIGNAL;
|
2024-09-05 00:14:01 +00:00
|
|
|
|
|
|
|
unsetenv(env_var);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2022-05-06 16:05:12 +00:00
|
|
|
/* clang-format off */
|
|
|
|
|
2021-04-22 15:41:21 +00:00
|
|
|
#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 | \
|
2022-05-06 16:10:59 +00:00
|
|
|
LANDLOCK_ACCESS_FS_MAKE_SYM | \
|
2022-10-18 18:22:15 +00:00
|
|
|
LANDLOCK_ACCESS_FS_REFER | \
|
2024-04-19 16:11:19 +00:00
|
|
|
LANDLOCK_ACCESS_FS_TRUNCATE | \
|
|
|
|
LANDLOCK_ACCESS_FS_IOCTL_DEV)
|
2022-05-06 16:10:59 +00:00
|
|
|
|
2022-05-06 16:05:12 +00:00
|
|
|
/* clang-format on */
|
|
|
|
|
2024-09-05 00:14:01 +00:00
|
|
|
#define LANDLOCK_ABI_LAST 6
|
2022-09-23 15:42:05 +00:00
|
|
|
|
2021-04-22 15:41:21 +00:00
|
|
|
int main(const int argc, char *const argv[], char *const *const envp)
|
|
|
|
{
|
|
|
|
const char *cmd_path;
|
|
|
|
char *const *cmd_argv;
|
2022-05-06 16:10:59 +00:00
|
|
|
int ruleset_fd, abi;
|
2023-10-26 01:47:50 +00:00
|
|
|
char *env_port_name;
|
2022-05-06 16:10:59 +00:00
|
|
|
__u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ,
|
|
|
|
access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE;
|
2023-10-26 01:47:50 +00:00
|
|
|
|
2021-04-22 15:41:21 +00:00
|
|
|
struct landlock_ruleset_attr ruleset_attr = {
|
2022-05-06 16:10:59 +00:00
|
|
|
.handled_access_fs = access_fs_rw,
|
2023-10-26 01:47:50 +00:00
|
|
|
.handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
|
|
|
|
LANDLOCK_ACCESS_NET_CONNECT_TCP,
|
2024-09-06 21:30:07 +00:00
|
|
|
.scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
|
|
|
|
LANDLOCK_SCOPE_SIGNAL,
|
2021-04-22 15:41:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (argc < 2) {
|
2022-05-06 16:05:13 +00:00
|
|
|
fprintf(stderr,
|
2024-09-05 00:14:01 +00:00
|
|
|
"usage: %s=\"...\" %s=\"...\" %s=\"...\" %s=\"...\" %s=\"...\" %s "
|
2023-10-26 01:47:50 +00:00
|
|
|
"<cmd> [args]...\n\n",
|
|
|
|
ENV_FS_RO_NAME, ENV_FS_RW_NAME, ENV_TCP_BIND_NAME,
|
2024-09-05 00:14:01 +00:00
|
|
|
ENV_TCP_CONNECT_NAME, ENV_SCOPED_NAME, argv[0]);
|
2022-05-06 16:05:13 +00:00
|
|
|
fprintf(stderr,
|
2024-03-07 14:38:49 +00:00
|
|
|
"Execute a command in a restricted environment.\n\n");
|
2023-10-26 01:47:50 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Environment variables containing paths and ports "
|
|
|
|
"each separated by a colon:\n");
|
2022-05-06 16:05:13 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"* %s: list of paths allowed to be used in a read-only way.\n",
|
|
|
|
ENV_FS_RO_NAME);
|
|
|
|
fprintf(stderr,
|
2023-10-26 01:47:50 +00:00
|
|
|
"* %s: list of paths allowed to be used in a read-write way.\n\n",
|
2022-05-06 16:05:13 +00:00
|
|
|
ENV_FS_RW_NAME);
|
2023-10-26 01:47:50 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Environment variables containing ports are optional "
|
|
|
|
"and could be skipped.\n");
|
|
|
|
fprintf(stderr,
|
|
|
|
"* %s: list of ports allowed to bind (server).\n",
|
|
|
|
ENV_TCP_BIND_NAME);
|
|
|
|
fprintf(stderr,
|
|
|
|
"* %s: list of ports allowed to connect (client).\n",
|
|
|
|
ENV_TCP_CONNECT_NAME);
|
2024-09-05 00:14:01 +00:00
|
|
|
fprintf(stderr, "* %s: list of scoped IPCs.\n",
|
|
|
|
ENV_SCOPED_NAME);
|
2022-05-06 16:05:13 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"\nexample:\n"
|
2024-03-07 14:38:49 +00:00
|
|
|
"%s=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" "
|
2022-05-06 16:05:13 +00:00
|
|
|
"%s=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
|
2023-10-26 01:47:50 +00:00
|
|
|
"%s=\"9418\" "
|
|
|
|
"%s=\"80:443\" "
|
2024-09-06 21:30:07 +00:00
|
|
|
"%s=\"a:s\" "
|
2022-09-23 15:42:05 +00:00
|
|
|
"%s bash -i\n\n",
|
2023-10-26 01:47:50 +00:00
|
|
|
ENV_FS_RO_NAME, ENV_FS_RW_NAME, ENV_TCP_BIND_NAME,
|
2024-09-05 00:14:01 +00:00
|
|
|
ENV_TCP_CONNECT_NAME, ENV_SCOPED_NAME, argv[0]);
|
2022-09-23 15:42:05 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"This sandboxer can use Landlock features "
|
|
|
|
"up to ABI version %d.\n",
|
|
|
|
LANDLOCK_ABI_LAST);
|
2021-04-22 15:41:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-05-06 16:10:59 +00:00
|
|
|
abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
|
|
|
|
if (abi < 0) {
|
2021-04-22 15:41:21 +00:00
|
|
|
const int err = errno;
|
|
|
|
|
2022-05-06 16:10:59 +00:00
|
|
|
perror("Failed to check Landlock compatibility");
|
2021-04-22 15:41:21 +00:00
|
|
|
switch (err) {
|
|
|
|
case ENOSYS:
|
2022-05-06 16:05:13 +00:00
|
|
|
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");
|
2021-04-22 15:41:21 +00:00
|
|
|
break;
|
|
|
|
case EOPNOTSUPP:
|
2022-05-06 16:05:13 +00:00
|
|
|
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");
|
2021-04-22 15:41:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2022-09-23 15:42:05 +00:00
|
|
|
|
2022-05-06 16:10:59 +00:00
|
|
|
/* Best-effort security. */
|
2022-09-23 15:42:05 +00:00
|
|
|
switch (abi) {
|
|
|
|
case 1:
|
2022-11-07 18:16:51 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2022-09-23 15:42:05 +00:00
|
|
|
ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
|
2022-10-18 18:22:15 +00:00
|
|
|
__attribute__((fallthrough));
|
|
|
|
case 2:
|
|
|
|
/* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
|
|
|
|
ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
|
2023-10-26 01:47:50 +00:00
|
|
|
__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);
|
2024-04-19 16:11:19 +00:00
|
|
|
__attribute__((fallthrough));
|
|
|
|
case 4:
|
|
|
|
/* Removes LANDLOCK_ACCESS_FS_IOCTL_DEV for ABI < 5 */
|
|
|
|
ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_IOCTL_DEV;
|
|
|
|
|
2024-09-05 00:14:01 +00:00
|
|
|
__attribute__((fallthrough));
|
|
|
|
case 5:
|
2024-09-06 21:30:07 +00:00
|
|
|
/* Removes LANDLOCK_SCOPE_* for ABI < 6 */
|
|
|
|
ruleset_attr.scoped &= ~(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
|
|
|
|
LANDLOCK_SCOPE_SIGNAL);
|
2022-09-23 15:42:05 +00:00
|
|
|
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);
|
2022-05-06 16:10:59 +00:00
|
|
|
}
|
2022-09-23 15:42:05 +00:00
|
|
|
access_fs_ro &= ruleset_attr.handled_access_fs;
|
|
|
|
access_fs_rw &= ruleset_attr.handled_access_fs;
|
2022-05-06 16:10:59 +00:00
|
|
|
|
2023-10-26 01:47:50 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2024-09-05 00:14:01 +00:00
|
|
|
if (check_ruleset_scope(ENV_SCOPED_NAME, &ruleset_attr))
|
|
|
|
return 1;
|
|
|
|
|
2022-05-06 16:10:59 +00:00
|
|
|
ruleset_fd =
|
|
|
|
landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
|
|
|
|
if (ruleset_fd < 0) {
|
|
|
|
perror("Failed to create a ruleset");
|
|
|
|
return 1;
|
|
|
|
}
|
2023-10-26 01:47:50 +00:00
|
|
|
|
|
|
|
if (populate_ruleset_fs(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro)) {
|
|
|
|
goto err_close_ruleset;
|
|
|
|
}
|
|
|
|
if (populate_ruleset_fs(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw)) {
|
2021-04-22 15:41:21 +00:00
|
|
|
goto err_close_ruleset;
|
|
|
|
}
|
2023-10-26 01:47:50 +00:00
|
|
|
|
|
|
|
if (populate_ruleset_net(ENV_TCP_BIND_NAME, ruleset_fd,
|
|
|
|
LANDLOCK_ACCESS_NET_BIND_TCP)) {
|
|
|
|
goto err_close_ruleset;
|
|
|
|
}
|
|
|
|
if (populate_ruleset_net(ENV_TCP_CONNECT_NAME, ruleset_fd,
|
|
|
|
LANDLOCK_ACCESS_NET_CONNECT_TCP)) {
|
2021-04-22 15:41:21 +00:00
|
|
|
goto err_close_ruleset;
|
|
|
|
}
|
2023-10-26 01:47:50 +00:00
|
|
|
|
2021-04-22 15:41:21 +00:00
|
|
|
if (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, 0)) {
|
|
|
|
perror("Failed to enforce ruleset");
|
|
|
|
goto err_close_ruleset;
|
|
|
|
}
|
|
|
|
close(ruleset_fd);
|
|
|
|
|
|
|
|
cmd_path = argv[1];
|
|
|
|
cmd_argv = argv + 1;
|
2024-03-07 14:38:49 +00:00
|
|
|
fprintf(stderr, "Executing the sandboxed command...\n");
|
2021-04-22 15:41:21 +00:00
|
|
|
execvpe(cmd_path, cmd_argv, envp);
|
|
|
|
fprintf(stderr, "Failed to execute \"%s\": %s\n", cmd_path,
|
2022-05-06 16:05:13 +00:00
|
|
|
strerror(errno));
|
2021-04-22 15:41:21 +00:00
|
|
|
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;
|
|
|
|
}
|