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.
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
26 landlock_create_ruleset(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,
39 return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr,
44 #ifndef landlock_restrict_self
45 static inline int landlock_restrict_self(const int ruleset_fd,
48 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
52 #define ENV_FS_RO_NAME "LL_FS_RO"
53 #define ENV_FS_RW_NAME "LL_FS_RW"
54 #define ENV_PATH_TOKEN ":"
56 static int parse_path(char *env_path, const char ***const path_list)
62 for (i = 0; env_path[i]; i++) {
63 if (env_path[i] == ENV_PATH_TOKEN[0])
67 *path_list = malloc(num_paths * sizeof(**path_list));
68 for (i = 0; i < num_paths; i++)
69 (*path_list)[i] = strsep(&env_path, ENV_PATH_TOKEN);
74 /* clang-format off */
76 #define ACCESS_FILE ( \
77 LANDLOCK_ACCESS_FS_EXECUTE | \
78 LANDLOCK_ACCESS_FS_WRITE_FILE | \
79 LANDLOCK_ACCESS_FS_READ_FILE)
83 static int populate_ruleset(const char *const env_var, const int ruleset_fd,
84 const __u64 allowed_access)
86 int num_paths, i, ret = 1;
88 const char **path_list = NULL;
89 struct landlock_path_beneath_attr path_beneath = {
93 env_path_name = getenv(env_var);
95 /* Prevents users to forget a setting. */
96 fprintf(stderr, "Missing environment variable %s\n", env_var);
99 env_path_name = strdup(env_path_name);
101 num_paths = parse_path(env_path_name, &path_list);
102 if (num_paths == 1 && path_list[0][0] == '\0') {
104 * Allows to not use all possible restrictions (e.g. use
105 * LL_FS_RO without LL_FS_RW).
111 for (i = 0; i < num_paths; i++) {
114 path_beneath.parent_fd = open(path_list[i], O_PATH | O_CLOEXEC);
115 if (path_beneath.parent_fd < 0) {
116 fprintf(stderr, "Failed to open \"%s\": %s\n",
117 path_list[i], strerror(errno));
120 if (fstat(path_beneath.parent_fd, &statbuf)) {
121 close(path_beneath.parent_fd);
124 path_beneath.allowed_access = allowed_access;
125 if (!S_ISDIR(statbuf.st_mode))
126 path_beneath.allowed_access &= ACCESS_FILE;
127 if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
130 "Failed to update the ruleset with \"%s\": %s\n",
131 path_list[i], strerror(errno));
132 close(path_beneath.parent_fd);
135 close(path_beneath.parent_fd);
145 /* clang-format off */
147 #define ACCESS_FS_ROUGHLY_READ ( \
148 LANDLOCK_ACCESS_FS_EXECUTE | \
149 LANDLOCK_ACCESS_FS_READ_FILE | \
150 LANDLOCK_ACCESS_FS_READ_DIR)
152 #define ACCESS_FS_ROUGHLY_WRITE ( \
153 LANDLOCK_ACCESS_FS_WRITE_FILE | \
154 LANDLOCK_ACCESS_FS_REMOVE_DIR | \
155 LANDLOCK_ACCESS_FS_REMOVE_FILE | \
156 LANDLOCK_ACCESS_FS_MAKE_CHAR | \
157 LANDLOCK_ACCESS_FS_MAKE_DIR | \
158 LANDLOCK_ACCESS_FS_MAKE_REG | \
159 LANDLOCK_ACCESS_FS_MAKE_SOCK | \
160 LANDLOCK_ACCESS_FS_MAKE_FIFO | \
161 LANDLOCK_ACCESS_FS_MAKE_BLOCK | \
162 LANDLOCK_ACCESS_FS_MAKE_SYM | \
163 LANDLOCK_ACCESS_FS_REFER)
165 /* clang-format on */
167 #define LANDLOCK_ABI_LAST 2
169 int main(const int argc, char *const argv[], char *const *const envp)
171 const char *cmd_path;
172 char *const *cmd_argv;
174 __u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ,
175 access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE;
176 struct landlock_ruleset_attr ruleset_attr = {
177 .handled_access_fs = access_fs_rw,
182 "usage: %s=\"...\" %s=\"...\" %s <cmd> [args]...\n\n",
183 ENV_FS_RO_NAME, ENV_FS_RW_NAME, argv[0]);
185 "Launch a command in a restricted environment.\n\n");
186 fprintf(stderr, "Environment variables containing paths, "
187 "each separated by a colon:\n");
189 "* %s: list of paths allowed to be used in a read-only way.\n",
192 "* %s: list of paths allowed to be used in a read-write way.\n",
196 "%s=\"/bin:/lib:/usr:/proc:/etc:/dev/urandom\" "
197 "%s=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
199 ENV_FS_RO_NAME, ENV_FS_RW_NAME, argv[0]);
201 "This sandboxer can use Landlock features "
202 "up to ABI version %d.\n",
207 abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
209 const int err = errno;
211 perror("Failed to check Landlock compatibility");
215 "Hint: Landlock is not supported by the current kernel. "
216 "To support it, build the kernel with "
217 "CONFIG_SECURITY_LANDLOCK=y and prepend "
218 "\"landlock,\" to the content of CONFIG_LSM.\n");
222 "Hint: Landlock is currently disabled. "
223 "It can be enabled in the kernel configuration by "
224 "prepending \"landlock,\" to the content of CONFIG_LSM, "
225 "or at boot time by setting the same content to the "
226 "\"lsm\" kernel parameter.\n");
232 /* Best-effort security. */
235 /* Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2 */
236 ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
239 "Hint: You should update the running kernel "
240 "to leverage Landlock features "
241 "provided by ABI version %d (instead of %d).\n",
242 LANDLOCK_ABI_LAST, abi);
243 __attribute__((fallthrough));
244 case LANDLOCK_ABI_LAST:
248 "Hint: You should update this sandboxer "
249 "to leverage Landlock features "
250 "provided by ABI version %d (instead of %d).\n",
251 abi, LANDLOCK_ABI_LAST);
253 access_fs_ro &= ruleset_attr.handled_access_fs;
254 access_fs_rw &= ruleset_attr.handled_access_fs;
257 landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
258 if (ruleset_fd < 0) {
259 perror("Failed to create a ruleset");
262 if (populate_ruleset(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro)) {
263 goto err_close_ruleset;
265 if (populate_ruleset(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw)) {
266 goto err_close_ruleset;
268 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
269 perror("Failed to restrict privileges");
270 goto err_close_ruleset;
272 if (landlock_restrict_self(ruleset_fd, 0)) {
273 perror("Failed to enforce ruleset");
274 goto err_close_ruleset;
280 execvpe(cmd_path, cmd_argv, envp);
281 fprintf(stderr, "Failed to execute \"%s\": %s\n", cmd_path,
283 fprintf(stderr, "Hint: access to the binary, the interpreter or "
284 "shared libraries may be denied.\n");