Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  1// SPDX-License-Identifier: BSD-3-Clause
  2/*
  3 * Simple Landlock sandbox manager able to execute a process restricted by
  4 * user-defined file system and network access control policies.
  5 *
  6 * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
  7 * Copyright © 2020 ANSSI
  8 */
  9
 10#define _GNU_SOURCE
 11#define __SANE_USERSPACE_TYPES__
 12#include <arpa/inet.h>
 13#include <errno.h>
 14#include <fcntl.h>
 15#include <linux/landlock.h>
 16#include <linux/prctl.h>
 17#include <stddef.h>
 18#include <stdio.h>
 19#include <stdlib.h>
 20#include <string.h>
 21#include <sys/prctl.h>
 22#include <sys/stat.h>
 23#include <sys/syscall.h>
 24#include <unistd.h>
 25
 26#ifndef landlock_create_ruleset
 27static inline int
 28landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
 29			const size_t size, const __u32 flags)
 30{
 31	return syscall(__NR_landlock_create_ruleset, attr, size, flags);
 32}
 33#endif
 34
 35#ifndef landlock_add_rule
 36static inline int landlock_add_rule(const int ruleset_fd,
 37				    const enum landlock_rule_type rule_type,
 38				    const void *const rule_attr,
 39				    const __u32 flags)
 40{
 41	return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr,
 42		       flags);
 43}
 44#endif
 45
 46#ifndef landlock_restrict_self
 47static inline int landlock_restrict_self(const int ruleset_fd,
 48					 const __u32 flags)
 49{
 50	return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
 51}
 52#endif
 53
 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 ":"
 59
 60static int parse_path(char *env_path, const char ***const path_list)
 61{
 62	int i, num_paths = 0;
 63
 64	if (env_path) {
 65		num_paths++;
 66		for (i = 0; env_path[i]; i++) {
 67			if (env_path[i] == ENV_DELIMITER[0])
 68				num_paths++;
 69		}
 70	}
 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);
 74
 75	return num_paths;
 76}
 77
 78/* clang-format off */
 79
 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)
 85
 86/* clang-format on */
 87
 88static int populate_ruleset_fs(const char *const env_var, const int ruleset_fd,
 89			       const __u64 allowed_access)
 90{
 91	int num_paths, i, ret = 1;
 92	char *env_path_name;
 93	const char **path_list = NULL;
 94	struct landlock_path_beneath_attr path_beneath = {
 95		.parent_fd = -1,
 96	};
 97
 98	env_path_name = getenv(env_var);
 99	if (!env_path_name) {
100		/* Prevents users to forget a setting. */
101		fprintf(stderr, "Missing environment variable %s\n", env_var);
102		return 1;
103	}
104	env_path_name = strdup(env_path_name);
105	unsetenv(env_var);
106	num_paths = parse_path(env_path_name, &path_list);
107	if (num_paths == 1 && path_list[0][0] == '\0') {
108		/*
109		 * Allows to not use all possible restrictions (e.g. use
110		 * LL_FS_RO without LL_FS_RW).
111		 */
112		ret = 0;
113		goto out_free_name;
114	}
115
116	for (i = 0; i < num_paths; i++) {
117		struct stat statbuf;
118
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));
123			continue;
124		}
125		if (fstat(path_beneath.parent_fd, &statbuf)) {
126			fprintf(stderr, "Failed to stat \"%s\": %s\n",
127				path_list[i], strerror(errno));
128			close(path_beneath.parent_fd);
129			goto out_free_name;
130		}
131		path_beneath.allowed_access = allowed_access;
132		if (!S_ISDIR(statbuf.st_mode))
133			path_beneath.allowed_access &= ACCESS_FILE;
134		if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
135				      &path_beneath, 0)) {
136			fprintf(stderr,
137				"Failed to update the ruleset with \"%s\": %s\n",
138				path_list[i], strerror(errno));
139			close(path_beneath.parent_fd);
140			goto out_free_name;
141		}
142		close(path_beneath.parent_fd);
143	}
144	ret = 0;
145
146out_free_name:
147	free(path_list);
148	free(env_path_name);
149	return ret;
150}
151
152static int populate_ruleset_net(const char *const env_var, const int ruleset_fd,
153				const __u64 allowed_access)
154{
155	int ret = 1;
156	char *env_port_name, *env_port_name_next, *strport;
157	struct landlock_net_port_attr net_port = {
158		.allowed_access = allowed_access,
159		.port = 0,
160	};
161
162	env_port_name = getenv(env_var);
163	if (!env_port_name)
164		return 0;
165	env_port_name = strdup(env_port_name);
166	unsetenv(env_var);
167
168	env_port_name_next = env_port_name;
169	while ((strport = strsep(&env_port_name_next, ENV_DELIMITER))) {
170		net_port.port = atoi(strport);
171		if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
172				      &net_port, 0)) {
173			fprintf(stderr,
174				"Failed to update the ruleset with port \"%llu\": %s\n",
175				net_port.port, strerror(errno));
176			goto out_free_name;
177		}
178	}
179	ret = 0;
180
181out_free_name:
182	free(env_port_name);
183	return ret;
184}
185
186/* clang-format off */
187
188#define ACCESS_FS_ROUGHLY_READ ( \
189	LANDLOCK_ACCESS_FS_EXECUTE | \
190	LANDLOCK_ACCESS_FS_READ_FILE | \
191	LANDLOCK_ACCESS_FS_READ_DIR)
192
193#define ACCESS_FS_ROUGHLY_WRITE ( \
194	LANDLOCK_ACCESS_FS_WRITE_FILE | \
195	LANDLOCK_ACCESS_FS_REMOVE_DIR | \
196	LANDLOCK_ACCESS_FS_REMOVE_FILE | \
197	LANDLOCK_ACCESS_FS_MAKE_CHAR | \
198	LANDLOCK_ACCESS_FS_MAKE_DIR | \
199	LANDLOCK_ACCESS_FS_MAKE_REG | \
200	LANDLOCK_ACCESS_FS_MAKE_SOCK | \
201	LANDLOCK_ACCESS_FS_MAKE_FIFO | \
202	LANDLOCK_ACCESS_FS_MAKE_BLOCK | \
203	LANDLOCK_ACCESS_FS_MAKE_SYM | \
204	LANDLOCK_ACCESS_FS_REFER | \
205	LANDLOCK_ACCESS_FS_TRUNCATE)
206
207/* clang-format on */
208
209#define LANDLOCK_ABI_LAST 4
210
211int main(const int argc, char *const argv[], char *const *const envp)
212{
213	const char *cmd_path;
214	char *const *cmd_argv;
215	int ruleset_fd, abi;
216	char *env_port_name;
217	__u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ,
218	      access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE;
219
220	struct landlock_ruleset_attr ruleset_attr = {
221		.handled_access_fs = access_fs_rw,
222		.handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP |
223				      LANDLOCK_ACCESS_NET_CONNECT_TCP,
224	};
225
226	if (argc < 2) {
227		fprintf(stderr,
228			"usage: %s=\"...\" %s=\"...\" %s=\"...\" %s=\"...\"%s "
229			"<cmd> [args]...\n\n",
230			ENV_FS_RO_NAME, ENV_FS_RW_NAME, ENV_TCP_BIND_NAME,
231			ENV_TCP_CONNECT_NAME, argv[0]);
232		fprintf(stderr,
233			"Execute a command in a restricted environment.\n\n");
234		fprintf(stderr,
235			"Environment variables containing paths and ports "
236			"each separated by a colon:\n");
237		fprintf(stderr,
238			"* %s: list of paths allowed to be used in a read-only way.\n",
239			ENV_FS_RO_NAME);
240		fprintf(stderr,
241			"* %s: list of paths allowed to be used in a read-write way.\n\n",
242			ENV_FS_RW_NAME);
243		fprintf(stderr,
244			"Environment variables containing ports are optional "
245			"and could be skipped.\n");
246		fprintf(stderr,
247			"* %s: list of ports allowed to bind (server).\n",
248			ENV_TCP_BIND_NAME);
249		fprintf(stderr,
250			"* %s: list of ports allowed to connect (client).\n",
251			ENV_TCP_CONNECT_NAME);
252		fprintf(stderr,
253			"\nexample:\n"
254			"%s=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" "
255			"%s=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
256			"%s=\"9418\" "
257			"%s=\"80:443\" "
258			"%s bash -i\n\n",
259			ENV_FS_RO_NAME, ENV_FS_RW_NAME, ENV_TCP_BIND_NAME,
260			ENV_TCP_CONNECT_NAME, argv[0]);
261		fprintf(stderr,
262			"This sandboxer can use Landlock features "
263			"up to ABI version %d.\n",
264			LANDLOCK_ABI_LAST);
265		return 1;
266	}
267
268	abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
269	if (abi < 0) {
270		const int err = errno;
271
272		perror("Failed to check Landlock compatibility");
273		switch (err) {
274		case ENOSYS:
275			fprintf(stderr,
276				"Hint: Landlock is not supported by the current kernel. "
277				"To support it, build the kernel with "
278				"CONFIG_SECURITY_LANDLOCK=y and prepend "
279				"\"landlock,\" to the content of CONFIG_LSM.\n");
280			break;
281		case EOPNOTSUPP:
282			fprintf(stderr,
283				"Hint: Landlock is currently disabled. "
284				"It can be enabled in the kernel configuration by "
285				"prepending \"landlock,\" to the content of CONFIG_LSM, "
286				"or at boot time by setting the same content to the "
287				"\"lsm\" kernel parameter.\n");
288			break;
289		}
290		return 1;
291	}
292
293	/* Best-effort security. */
294	switch (abi) {
295	case 1:
296		/*
297		 * Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2
298		 *
299		 * Note: The "refer" operations (file renaming and linking
300		 * across different directories) are always forbidden when using
301		 * Landlock with ABI 1.
302		 *
303		 * If only ABI 1 is available, this sandboxer knowingly forbids
304		 * refer operations.
305		 *
306		 * If a program *needs* to do refer operations after enabling
307		 * Landlock, it can not use Landlock at ABI level 1.  To be
308		 * compatible with different kernel versions, such programs
309		 * should then fall back to not restrict themselves at all if
310		 * the running kernel only supports ABI 1.
311		 */
312		ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
313		__attribute__((fallthrough));
314	case 2:
315		/* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
316		ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
317		__attribute__((fallthrough));
318	case 3:
319		/* Removes network support for ABI < 4 */
320		ruleset_attr.handled_access_net &=
321			~(LANDLOCK_ACCESS_NET_BIND_TCP |
322			  LANDLOCK_ACCESS_NET_CONNECT_TCP);
323		fprintf(stderr,
324			"Hint: You should update the running kernel "
325			"to leverage Landlock features "
326			"provided by ABI version %d (instead of %d).\n",
327			LANDLOCK_ABI_LAST, abi);
328		__attribute__((fallthrough));
329	case LANDLOCK_ABI_LAST:
330		break;
331	default:
332		fprintf(stderr,
333			"Hint: You should update this sandboxer "
334			"to leverage Landlock features "
335			"provided by ABI version %d (instead of %d).\n",
336			abi, LANDLOCK_ABI_LAST);
337	}
338	access_fs_ro &= ruleset_attr.handled_access_fs;
339	access_fs_rw &= ruleset_attr.handled_access_fs;
340
341	/* Removes bind access attribute if not supported by a user. */
342	env_port_name = getenv(ENV_TCP_BIND_NAME);
343	if (!env_port_name) {
344		ruleset_attr.handled_access_net &=
345			~LANDLOCK_ACCESS_NET_BIND_TCP;
346	}
347	/* Removes connect access attribute if not supported by a user. */
348	env_port_name = getenv(ENV_TCP_CONNECT_NAME);
349	if (!env_port_name) {
350		ruleset_attr.handled_access_net &=
351			~LANDLOCK_ACCESS_NET_CONNECT_TCP;
352	}
353
354	ruleset_fd =
355		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
356	if (ruleset_fd < 0) {
357		perror("Failed to create a ruleset");
358		return 1;
359	}
360
361	if (populate_ruleset_fs(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro)) {
362		goto err_close_ruleset;
363	}
364	if (populate_ruleset_fs(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw)) {
365		goto err_close_ruleset;
366	}
367
368	if (populate_ruleset_net(ENV_TCP_BIND_NAME, ruleset_fd,
369				 LANDLOCK_ACCESS_NET_BIND_TCP)) {
370		goto err_close_ruleset;
371	}
372	if (populate_ruleset_net(ENV_TCP_CONNECT_NAME, ruleset_fd,
373				 LANDLOCK_ACCESS_NET_CONNECT_TCP)) {
374		goto err_close_ruleset;
375	}
376
377	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
378		perror("Failed to restrict privileges");
379		goto err_close_ruleset;
380	}
381	if (landlock_restrict_self(ruleset_fd, 0)) {
382		perror("Failed to enforce ruleset");
383		goto err_close_ruleset;
384	}
385	close(ruleset_fd);
386
387	cmd_path = argv[1];
388	cmd_argv = argv + 1;
389	fprintf(stderr, "Executing the sandboxed command...\n");
390	execvpe(cmd_path, cmd_argv, envp);
391	fprintf(stderr, "Failed to execute \"%s\": %s\n", cmd_path,
392		strerror(errno));
393	fprintf(stderr, "Hint: access to the binary, the interpreter or "
394			"shared libraries may be denied.\n");
395	return 1;
396
397err_close_ruleset:
398	close(ruleset_fd);
399	return 1;
400}