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
11 #define __SANE_USERSPACE_TYPES__
12 #include <arpa/inet.h>
15 #include <linux/landlock.h>
16 #include <linux/prctl.h>
21 #include <sys/prctl.h>
23 #include <sys/syscall.h>
26 #ifndef landlock_create_ruleset
28 landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
29 const size_t size, const __u32 flags)
31 return syscall(__NR_landlock_create_ruleset, attr, size, flags);
35 #ifndef landlock_add_rule
36 static inline int landlock_add_rule(const int ruleset_fd,
37 const enum landlock_rule_type rule_type,
38 const void *const rule_attr,
41 return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr,
46 #ifndef landlock_restrict_self
47 static inline int landlock_restrict_self(const int ruleset_fd,
50 return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
54 #define ENV_FS_RO_NAME "LL_FS_RO"
55 #define ENV_FS_RW_NAME "LL_FS_RW"
56 #define ENV_TCP_BIND_NAME "LL_TCP_BIND"
57 #define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT"
58 #define ENV_DELIMITER ":"
60 static int parse_path(char *env_path, const char ***const path_list)
66 for (i = 0; env_path[i]; i++) {
67 if (env_path[i] == ENV_DELIMITER[0])
71 *path_list = malloc(num_paths * sizeof(**path_list));
72 for (i = 0; i < num_paths; i++)
73 (*path_list)[i] = strsep(&env_path, ENV_DELIMITER);
78 /* clang-format off */
80 #define ACCESS_FILE ( \
81 LANDLOCK_ACCESS_FS_EXECUTE | \
82 LANDLOCK_ACCESS_FS_WRITE_FILE | \
83 LANDLOCK_ACCESS_FS_READ_FILE | \
84 LANDLOCK_ACCESS_FS_TRUNCATE)
88 static int populate_ruleset_fs(const char *const env_var, const int ruleset_fd,
89 const __u64 allowed_access)
91 int num_paths, i, ret = 1;
93 const char **path_list = NULL;
94 struct landlock_path_beneath_attr path_beneath = {
98 env_path_name = getenv(env_var);
100 /* Prevents users to forget a setting. */
101 fprintf(stderr, "Missing environment variable %s\n", env_var);
104 env_path_name = strdup(env_path_name);
106 num_paths = parse_path(env_path_name, &path_list);
107 if (num_paths == 1 && path_list[0][0] == '\0') {
109 * Allows to not use all possible restrictions (e.g. use
110 * LL_FS_RO without LL_FS_RW).
116 for (i = 0; i < num_paths; i++) {
119 path_beneath.parent_fd = open(path_list[i], O_PATH | O_CLOEXEC);
120 if (path_beneath.parent_fd < 0) {
121 fprintf(stderr, "Failed to open \"%s\": %s\n",
122 path_list[i], strerror(errno));
125 if (fstat(path_beneath.parent_fd, &statbuf)) {
126 close(path_beneath.parent_fd);
129 path_beneath.allowed_access = allowed_access;
130 if (!S_ISDIR(statbuf.st_mode))
131 path_beneath.allowed_access &= ACCESS_FILE;
132 if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
135 "Failed to update the ruleset with \"%s\": %s\n",
136 path_list[i], strerror(errno));
137 close(path_beneath.parent_fd);
140 close(path_beneath.parent_fd);
150 static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
151 const __u64 allowed_access)
154 char *env_port_name, *strport;
155 struct landlock_net_port_attr net_port = {
156 .allowed_access = allowed_access,
160 env_port_name = getenv(env_var);
163 env_port_name = strdup(env_port_name);
166 while ((strport = strsep(&env_port_name, ENV_DELIMITER))) {
167 net_port.port = atoi(strport);
168 if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
171 "Failed to update the ruleset with port \"%llu\": %s\n",
172 net_port.port, strerror(errno));
183 /* clang-format off */
185 #define ACCESS_FS_ROUGHLY_READ ( \
186 LANDLOCK_ACCESS_FS_EXECUTE | \
187 LANDLOCK_ACCESS_FS_READ_FILE | \
188 LANDLOCK_ACCESS_FS_READ_DIR)
190 #define ACCESS_FS_ROUGHLY_WRITE ( \
191 LANDLOCK_ACCESS_FS_WRITE_FILE | \
192 LANDLOCK_ACCESS_FS_REMOVE_DIR | \
193 LANDLOCK_ACCESS_FS_REMOVE_FILE | \
194 LANDLOCK_ACCESS_FS_MAKE_CHAR | \
195 LANDLOCK_ACCESS_FS_MAKE_DIR | \
196 LANDLOCK_ACCESS_FS_MAKE_REG | \
197 LANDLOCK_ACCESS_FS_MAKE_SOCK | \
198 LANDLOCK_ACCESS_FS_MAKE_FIFO | \
199 LANDLOCK_ACCESS_FS_MAKE_BLOCK | \
200 LANDLOCK_ACCESS_FS_MAKE_SYM | \
201 LANDLOCK_ACCESS_FS_REFER | \
202 LANDLOCK_ACCESS_FS_TRUNCATE)
204 /* clang-format on */
206 #define LANDLOCK_ABI_LAST 4
208 int main(const int argc, char *const argv[], char *const *const envp)
210 const char *cmd_path;
211 char *const *cmd_argv;
214 __u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ,
215 access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE;
217 struct landlock_ruleset_attr ruleset_attr = {
218 .handled_access_fs = access_fs_rw,
219 .handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
220 LANDLOCK_ACCESS_NET_CONNECT_TCP,
225 "usage: %s=\"...\" %s=\"...\" %s=\"...\" %s=\"...\"%s "
226 "<cmd> [args]...\n\n",
227 ENV_FS_RO_NAME, ENV_FS_RW_NAME, ENV_TCP_BIND_NAME,
228 ENV_TCP_CONNECT_NAME, argv[0]);
230 "Launch a command in a restricted environment.\n\n");
232 "Environment variables containing paths and ports "
233 "each separated by a colon:\n");
235 "* %s: list of paths allowed to be used in a read-only way.\n",
238 "* %s: list of paths allowed to be used in a read-write way.\n\n",
241 "Environment variables containing ports are optional "
242 "and could be skipped.\n");
244 "* %s: list of ports allowed to bind (server).\n",
247 "* %s: list of ports allowed to connect (client).\n",
248 ENV_TCP_CONNECT_NAME);
251 "%s=\"/bin:/lib:/usr:/proc:/etc:/dev/urandom\" "
252 "%s=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
256 ENV_FS_RO_NAME, ENV_FS_RW_NAME, ENV_TCP_BIND_NAME,
257 ENV_TCP_CONNECT_NAME, argv[0]);
259 "This sandboxer can use Landlock features "
260 "up to ABI version %d.\n",
265 abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
267 const int err = errno;
269 perror("Failed to check Landlock compatibility");
273 "Hint: Landlock is not supported by the current kernel. "
274 "To support it, build the kernel with "
275 "CONFIG_SECURITY_LANDLOCK=y and prepend "
276 "\"landlock,\" to the content of CONFIG_LSM.\n");
280 "Hint: Landlock is currently disabled. "
281 "It can be enabled in the kernel configuration by "
282 "prepending \"landlock,\" to the content of CONFIG_LSM, "
283 "or at boot time by setting the same content to the "
284 "\"lsm\" kernel parameter.\n");
290 /* Best-effort security. */
294 * Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2
296 * Note: The "refer" operations (file renaming and linking
297 * across different directories) are always forbidden when using
298 * Landlock with ABI 1.
300 * If only ABI 1 is available, this sandboxer knowingly forbids
303 * If a program *needs* to do refer operations after enabling
304 * Landlock, it can not use Landlock at ABI level 1. To be
305 * compatible with different kernel versions, such programs
306 * should then fall back to not restrict themselves at all if
307 * the running kernel only supports ABI 1.
309 ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
310 __attribute__((fallthrough));
312 /* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
313 ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
314 __attribute__((fallthrough));
316 /* Removes network support for ABI < 4 */
317 ruleset_attr.handled_access_net &=
318 ~(LANDLOCK_ACCESS_NET_BIND_TCP |
319 LANDLOCK_ACCESS_NET_CONNECT_TCP);
321 "Hint: You should update the running kernel "
322 "to leverage Landlock features "
323 "provided by ABI version %d (instead of %d).\n",
324 LANDLOCK_ABI_LAST, abi);
325 __attribute__((fallthrough));
326 case LANDLOCK_ABI_LAST:
330 "Hint: You should update this sandboxer "
331 "to leverage Landlock features "
332 "provided by ABI version %d (instead of %d).\n",
333 abi, LANDLOCK_ABI_LAST);
335 access_fs_ro &= ruleset_attr.handled_access_fs;
336 access_fs_rw &= ruleset_attr.handled_access_fs;
338 /* Removes bind access attribute if not supported by a user. */
339 env_port_name = getenv(ENV_TCP_BIND_NAME);
340 if (!env_port_name) {
341 ruleset_attr.handled_access_net &=
342 ~LANDLOCK_ACCESS_NET_BIND_TCP;
344 /* Removes connect access attribute if not supported by a user. */
345 env_port_name = getenv(ENV_TCP_CONNECT_NAME);
346 if (!env_port_name) {
347 ruleset_attr.handled_access_net &=
348 ~LANDLOCK_ACCESS_NET_CONNECT_TCP;
352 landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
353 if (ruleset_fd < 0) {
354 perror("Failed to create a ruleset");
358 if (populate_ruleset_fs(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro)) {
359 goto err_close_ruleset;
361 if (populate_ruleset_fs(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw)) {
362 goto err_close_ruleset;
365 if (populate_ruleset_net(ENV_TCP_BIND_NAME, ruleset_fd,
366 LANDLOCK_ACCESS_NET_BIND_TCP)) {
367 goto err_close_ruleset;
369 if (populate_ruleset_net(ENV_TCP_CONNECT_NAME, ruleset_fd,
370 LANDLOCK_ACCESS_NET_CONNECT_TCP)) {
371 goto err_close_ruleset;
374 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
375 perror("Failed to restrict privileges");
376 goto err_close_ruleset;
378 if (landlock_restrict_self(ruleset_fd, 0)) {
379 perror("Failed to enforce ruleset");
380 goto err_close_ruleset;
386 execvpe(cmd_path, cmd_argv, envp);
387 fprintf(stderr, "Failed to execute \"%s\": %s\n", cmd_path,
389 fprintf(stderr, "Hint: access to the binary, the interpreter or "
390 "shared libraries may be denied.\n");