1 // SPDX-License-Identifier: BSD-3-Clause
3 * Simple Landlock sandbox manager able to launch a process restricted by a
4 * user-defined filesystem access control policy.
6 * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
7 * Copyright © 2020 ANSSI
13 #include <linux/landlock.h>
14 #include <linux/prctl.h>
19 #include <sys/prctl.h>
21 #include <sys/syscall.h>
24 #ifndef landlock_create_ruleset
25 static inline int landlock_create_ruleset(
26 const struct landlock_ruleset_attr *const attr,
27 const size_t size, const __u32 flags)
29 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
33 #ifndef landlock_add_rule
34 static inline int landlock_add_rule(const int ruleset_fd,
35 const enum landlock_rule_type rule_type,
36 const void *const rule_attr, const __u32 flags)
38 return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type,
43 #ifndef landlock_restrict_self
44 static inline int landlock_restrict_self(const int ruleset_fd,
47 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
51 #define ENV_FS_RO_NAME "LL_FS_RO"
52 #define ENV_FS_RW_NAME "LL_FS_RW"
53 #define ENV_PATH_TOKEN ":"
55 static int parse_path(char *env_path, const char ***const path_list)
61 for (i = 0; env_path[i]; i++) {
62 if (env_path[i] == ENV_PATH_TOKEN[0])
66 *path_list = malloc(num_paths * sizeof(**path_list));
67 for (i = 0; i < num_paths; i++)
68 (*path_list)[i] = strsep(&env_path, ENV_PATH_TOKEN);
73 #define ACCESS_FILE ( \
74 LANDLOCK_ACCESS_FS_EXECUTE | \
75 LANDLOCK_ACCESS_FS_WRITE_FILE | \
76 LANDLOCK_ACCESS_FS_READ_FILE)
78 static int populate_ruleset(
79 const char *const env_var, const int ruleset_fd,
80 const __u64 allowed_access)
82 int num_paths, i, ret = 1;
84 const char **path_list = NULL;
85 struct landlock_path_beneath_attr path_beneath = {
89 env_path_name = getenv(env_var);
91 /* Prevents users to forget a setting. */
92 fprintf(stderr, "Missing environment variable %s\n", env_var);
95 env_path_name = strdup(env_path_name);
97 num_paths = parse_path(env_path_name, &path_list);
98 if (num_paths == 1 && path_list[0][0] == '\0') {
100 * Allows to not use all possible restrictions (e.g. use
101 * LL_FS_RO without LL_FS_RW).
107 for (i = 0; i < num_paths; i++) {
110 path_beneath.parent_fd = open(path_list[i], O_PATH |
112 if (path_beneath.parent_fd < 0) {
113 fprintf(stderr, "Failed to open \"%s\": %s\n",
118 if (fstat(path_beneath.parent_fd, &statbuf)) {
119 close(path_beneath.parent_fd);
122 path_beneath.allowed_access = allowed_access;
123 if (!S_ISDIR(statbuf.st_mode))
124 path_beneath.allowed_access &= ACCESS_FILE;
125 if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
127 fprintf(stderr, "Failed to update the ruleset with \"%s\": %s\n",
128 path_list[i], strerror(errno));
129 close(path_beneath.parent_fd);
132 close(path_beneath.parent_fd);
141 #define ACCESS_FS_ROUGHLY_READ ( \
142 LANDLOCK_ACCESS_FS_EXECUTE | \
143 LANDLOCK_ACCESS_FS_READ_FILE | \
144 LANDLOCK_ACCESS_FS_READ_DIR)
146 #define ACCESS_FS_ROUGHLY_WRITE ( \
147 LANDLOCK_ACCESS_FS_WRITE_FILE | \
148 LANDLOCK_ACCESS_FS_REMOVE_DIR | \
149 LANDLOCK_ACCESS_FS_REMOVE_FILE | \
150 LANDLOCK_ACCESS_FS_MAKE_CHAR | \
151 LANDLOCK_ACCESS_FS_MAKE_DIR | \
152 LANDLOCK_ACCESS_FS_MAKE_REG | \
153 LANDLOCK_ACCESS_FS_MAKE_SOCK | \
154 LANDLOCK_ACCESS_FS_MAKE_FIFO | \
155 LANDLOCK_ACCESS_FS_MAKE_BLOCK | \
156 LANDLOCK_ACCESS_FS_MAKE_SYM)
158 int main(const int argc, char *const argv[], char *const *const envp)
160 const char *cmd_path;
161 char *const *cmd_argv;
163 struct landlock_ruleset_attr ruleset_attr = {
164 .handled_access_fs = ACCESS_FS_ROUGHLY_READ |
165 ACCESS_FS_ROUGHLY_WRITE,
169 fprintf(stderr, "usage: %s=\"...\" %s=\"...\" %s <cmd> [args]...\n\n",
170 ENV_FS_RO_NAME, ENV_FS_RW_NAME, argv[0]);
171 fprintf(stderr, "Launch a command in a restricted environment.\n\n");
172 fprintf(stderr, "Environment variables containing paths, "
173 "each separated by a colon:\n");
174 fprintf(stderr, "* %s: list of paths allowed to be used in a read-only way.\n",
176 fprintf(stderr, "* %s: list of paths allowed to be used in a read-write way.\n",
178 fprintf(stderr, "\nexample:\n"
179 "%s=\"/bin:/lib:/usr:/proc:/etc:/dev/urandom\" "
180 "%s=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
182 ENV_FS_RO_NAME, ENV_FS_RW_NAME, argv[0]);
186 ruleset_fd = landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
187 if (ruleset_fd < 0) {
188 const int err = errno;
190 perror("Failed to create a ruleset");
193 fprintf(stderr, "Hint: Landlock is not supported by the current kernel. "
194 "To support it, build the kernel with "
195 "CONFIG_SECURITY_LANDLOCK=y and prepend "
196 "\"landlock,\" to the content of CONFIG_LSM.\n");
199 fprintf(stderr, "Hint: Landlock is currently disabled. "
200 "It can be enabled in the kernel configuration by "
201 "prepending \"landlock,\" to the content of CONFIG_LSM, "
202 "or at boot time by setting the same content to the "
203 "\"lsm\" kernel parameter.\n");
208 if (populate_ruleset(ENV_FS_RO_NAME, ruleset_fd,
209 ACCESS_FS_ROUGHLY_READ)) {
210 goto err_close_ruleset;
212 if (populate_ruleset(ENV_FS_RW_NAME, ruleset_fd,
213 ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE)) {
214 goto err_close_ruleset;
216 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
217 perror("Failed to restrict privileges");
218 goto err_close_ruleset;
220 if (landlock_restrict_self(ruleset_fd, 0)) {
221 perror("Failed to enforce ruleset");
222 goto err_close_ruleset;
228 execvpe(cmd_path, cmd_argv, envp);
229 fprintf(stderr, "Failed to execute \"%s\": %s\n", cmd_path,
231 fprintf(stderr, "Hint: access to the binary, the interpreter or "
232 "shared libraries may be denied.\n");