xref: /openbmc/linux/samples/landlock/sandboxer.c (revision 060f35a317ef09101b128f399dce7ed13d019461)
1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Simple Landlock sandbox manager able to launch a process restricted by a
4  * user-defined filesystem access control policy.
5  *
6  * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
7  * Copyright © 2020 ANSSI
8  */
9 
10 #define _GNU_SOURCE
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <linux/landlock.h>
14 #include <linux/prctl.h>
15 #include <stddef.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/prctl.h>
20 #include <sys/stat.h>
21 #include <sys/syscall.h>
22 #include <unistd.h>
23 
24 #ifndef landlock_create_ruleset
25 static inline int
landlock_create_ruleset(const struct landlock_ruleset_attr * const attr,const size_t size,const __u32 flags)26 landlock_create_ruleset(const struct landlock_ruleset_attr *const attr,
27 			const size_t size, const __u32 flags)
28 {
29 	return syscall(__NR_landlock_create_ruleset, attr, size, flags);
30 }
31 #endif
32 
33 #ifndef landlock_add_rule
landlock_add_rule(const int ruleset_fd,const enum landlock_rule_type rule_type,const void * const rule_attr,const __u32 flags)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,
37 				    const __u32 flags)
38 {
39 	return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr,
40 		       flags);
41 }
42 #endif
43 
44 #ifndef landlock_restrict_self
landlock_restrict_self(const int ruleset_fd,const __u32 flags)45 static inline int landlock_restrict_self(const int ruleset_fd,
46 					 const __u32 flags)
47 {
48 	return syscall(__NR_landlock_restrict_self, ruleset_fd, flags);
49 }
50 #endif
51 
52 #define ENV_FS_RO_NAME "LL_FS_RO"
53 #define ENV_FS_RW_NAME "LL_FS_RW"
54 #define ENV_PATH_TOKEN ":"
55 
parse_path(char * env_path,const char *** const path_list)56 static int parse_path(char *env_path, const char ***const path_list)
57 {
58 	int i, num_paths = 0;
59 
60 	if (env_path) {
61 		num_paths++;
62 		for (i = 0; env_path[i]; i++) {
63 			if (env_path[i] == ENV_PATH_TOKEN[0])
64 				num_paths++;
65 		}
66 	}
67 	*path_list = malloc(num_paths * sizeof(**path_list));
68 	if (!*path_list)
69 		return -1;
70 
71 	for (i = 0; i < num_paths; i++)
72 		(*path_list)[i] = strsep(&env_path, ENV_PATH_TOKEN);
73 
74 	return num_paths;
75 }
76 
77 /* clang-format off */
78 
79 #define ACCESS_FILE ( \
80 	LANDLOCK_ACCESS_FS_EXECUTE | \
81 	LANDLOCK_ACCESS_FS_WRITE_FILE | \
82 	LANDLOCK_ACCESS_FS_READ_FILE | \
83 	LANDLOCK_ACCESS_FS_TRUNCATE)
84 
85 /* clang-format on */
86 
populate_ruleset(const char * const env_var,const int ruleset_fd,const __u64 allowed_access)87 static int populate_ruleset(const char *const env_var, const int ruleset_fd,
88 			    const __u64 allowed_access)
89 {
90 	int num_paths, i, ret = 1;
91 	char *env_path_name;
92 	const char **path_list = NULL;
93 	struct landlock_path_beneath_attr path_beneath = {
94 		.parent_fd = -1,
95 	};
96 
97 	env_path_name = getenv(env_var);
98 	if (!env_path_name) {
99 		/* Prevents users to forget a setting. */
100 		fprintf(stderr, "Missing environment variable %s\n", env_var);
101 		return 1;
102 	}
103 	env_path_name = strdup(env_path_name);
104 	unsetenv(env_var);
105 	num_paths = parse_path(env_path_name, &path_list);
106 	if (num_paths < 0) {
107 		fprintf(stderr, "Failed to allocate memory\n");
108 		goto out_free_name;
109 	}
110 	if (num_paths == 1 && path_list[0][0] == '\0') {
111 		/*
112 		 * Allows to not use all possible restrictions (e.g. use
113 		 * LL_FS_RO without LL_FS_RW).
114 		 */
115 		ret = 0;
116 		goto out_free_name;
117 	}
118 
119 	for (i = 0; i < num_paths; i++) {
120 		struct stat statbuf;
121 
122 		path_beneath.parent_fd = open(path_list[i], O_PATH | O_CLOEXEC);
123 		if (path_beneath.parent_fd < 0) {
124 			fprintf(stderr, "Failed to open \"%s\": %s\n",
125 				path_list[i], strerror(errno));
126 			goto out_free_name;
127 		}
128 		if (fstat(path_beneath.parent_fd, &statbuf)) {
129 			close(path_beneath.parent_fd);
130 			goto out_free_name;
131 		}
132 		path_beneath.allowed_access = allowed_access;
133 		if (!S_ISDIR(statbuf.st_mode))
134 			path_beneath.allowed_access &= ACCESS_FILE;
135 		if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
136 				      &path_beneath, 0)) {
137 			fprintf(stderr,
138 				"Failed to update the ruleset with \"%s\": %s\n",
139 				path_list[i], strerror(errno));
140 			close(path_beneath.parent_fd);
141 			goto out_free_name;
142 		}
143 		close(path_beneath.parent_fd);
144 	}
145 	ret = 0;
146 
147 out_free_name:
148 	free(path_list);
149 	free(env_path_name);
150 	return ret;
151 }
152 
153 /* clang-format off */
154 
155 #define ACCESS_FS_ROUGHLY_READ ( \
156 	LANDLOCK_ACCESS_FS_EXECUTE | \
157 	LANDLOCK_ACCESS_FS_READ_FILE | \
158 	LANDLOCK_ACCESS_FS_READ_DIR)
159 
160 #define ACCESS_FS_ROUGHLY_WRITE ( \
161 	LANDLOCK_ACCESS_FS_WRITE_FILE | \
162 	LANDLOCK_ACCESS_FS_REMOVE_DIR | \
163 	LANDLOCK_ACCESS_FS_REMOVE_FILE | \
164 	LANDLOCK_ACCESS_FS_MAKE_CHAR | \
165 	LANDLOCK_ACCESS_FS_MAKE_DIR | \
166 	LANDLOCK_ACCESS_FS_MAKE_REG | \
167 	LANDLOCK_ACCESS_FS_MAKE_SOCK | \
168 	LANDLOCK_ACCESS_FS_MAKE_FIFO | \
169 	LANDLOCK_ACCESS_FS_MAKE_BLOCK | \
170 	LANDLOCK_ACCESS_FS_MAKE_SYM | \
171 	LANDLOCK_ACCESS_FS_REFER | \
172 	LANDLOCK_ACCESS_FS_TRUNCATE)
173 
174 /* clang-format on */
175 
176 #define LANDLOCK_ABI_LAST 3
177 
main(const int argc,char * const argv[],char * const * const envp)178 int main(const int argc, char *const argv[], char *const *const envp)
179 {
180 	const char *cmd_path;
181 	char *const *cmd_argv;
182 	int ruleset_fd, abi;
183 	__u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ,
184 	      access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE;
185 	struct landlock_ruleset_attr ruleset_attr = {
186 		.handled_access_fs = access_fs_rw,
187 	};
188 
189 	if (argc < 2) {
190 		fprintf(stderr,
191 			"usage: %s=\"...\" %s=\"...\" %s <cmd> [args]...\n\n",
192 			ENV_FS_RO_NAME, ENV_FS_RW_NAME, argv[0]);
193 		fprintf(stderr,
194 			"Launch a command in a restricted environment.\n\n");
195 		fprintf(stderr, "Environment variables containing paths, "
196 				"each separated by a colon:\n");
197 		fprintf(stderr,
198 			"* %s: list of paths allowed to be used in a read-only way.\n",
199 			ENV_FS_RO_NAME);
200 		fprintf(stderr,
201 			"* %s: list of paths allowed to be used in a read-write way.\n",
202 			ENV_FS_RW_NAME);
203 		fprintf(stderr,
204 			"\nexample:\n"
205 			"%s=\"/bin:/lib:/usr:/proc:/etc:/dev/urandom\" "
206 			"%s=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
207 			"%s bash -i\n\n",
208 			ENV_FS_RO_NAME, ENV_FS_RW_NAME, argv[0]);
209 		fprintf(stderr,
210 			"This sandboxer can use Landlock features "
211 			"up to ABI version %d.\n",
212 			LANDLOCK_ABI_LAST);
213 		return 1;
214 	}
215 
216 	abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
217 	if (abi < 0) {
218 		const int err = errno;
219 
220 		perror("Failed to check Landlock compatibility");
221 		switch (err) {
222 		case ENOSYS:
223 			fprintf(stderr,
224 				"Hint: Landlock is not supported by the current kernel. "
225 				"To support it, build the kernel with "
226 				"CONFIG_SECURITY_LANDLOCK=y and prepend "
227 				"\"landlock,\" to the content of CONFIG_LSM.\n");
228 			break;
229 		case EOPNOTSUPP:
230 			fprintf(stderr,
231 				"Hint: Landlock is currently disabled. "
232 				"It can be enabled in the kernel configuration by "
233 				"prepending \"landlock,\" to the content of CONFIG_LSM, "
234 				"or at boot time by setting the same content to the "
235 				"\"lsm\" kernel parameter.\n");
236 			break;
237 		}
238 		return 1;
239 	}
240 
241 	/* Best-effort security. */
242 	switch (abi) {
243 	case 1:
244 		/*
245 		 * Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2
246 		 *
247 		 * Note: The "refer" operations (file renaming and linking
248 		 * across different directories) are always forbidden when using
249 		 * Landlock with ABI 1.
250 		 *
251 		 * If only ABI 1 is available, this sandboxer knowingly forbids
252 		 * refer operations.
253 		 *
254 		 * If a program *needs* to do refer operations after enabling
255 		 * Landlock, it can not use Landlock at ABI level 1.  To be
256 		 * compatible with different kernel versions, such programs
257 		 * should then fall back to not restrict themselves at all if
258 		 * the running kernel only supports ABI 1.
259 		 */
260 		ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER;
261 		__attribute__((fallthrough));
262 	case 2:
263 		/* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
264 		ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
265 
266 		fprintf(stderr,
267 			"Hint: You should update the running kernel "
268 			"to leverage Landlock features "
269 			"provided by ABI version %d (instead of %d).\n",
270 			LANDLOCK_ABI_LAST, abi);
271 		__attribute__((fallthrough));
272 	case LANDLOCK_ABI_LAST:
273 		break;
274 	default:
275 		fprintf(stderr,
276 			"Hint: You should update this sandboxer "
277 			"to leverage Landlock features "
278 			"provided by ABI version %d (instead of %d).\n",
279 			abi, LANDLOCK_ABI_LAST);
280 	}
281 	access_fs_ro &= ruleset_attr.handled_access_fs;
282 	access_fs_rw &= ruleset_attr.handled_access_fs;
283 
284 	ruleset_fd =
285 		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
286 	if (ruleset_fd < 0) {
287 		perror("Failed to create a ruleset");
288 		return 1;
289 	}
290 	if (populate_ruleset(ENV_FS_RO_NAME, ruleset_fd, access_fs_ro)) {
291 		goto err_close_ruleset;
292 	}
293 	if (populate_ruleset(ENV_FS_RW_NAME, ruleset_fd, access_fs_rw)) {
294 		goto err_close_ruleset;
295 	}
296 	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
297 		perror("Failed to restrict privileges");
298 		goto err_close_ruleset;
299 	}
300 	if (landlock_restrict_self(ruleset_fd, 0)) {
301 		perror("Failed to enforce ruleset");
302 		goto err_close_ruleset;
303 	}
304 	close(ruleset_fd);
305 
306 	cmd_path = argv[1];
307 	cmd_argv = argv + 1;
308 	execvpe(cmd_path, cmd_argv, envp);
309 	fprintf(stderr, "Failed to execute \"%s\": %s\n", cmd_path,
310 		strerror(errno));
311 	fprintf(stderr, "Hint: access to the binary, the interpreter or "
312 			"shared libraries may be denied.\n");
313 	return 1;
314 
315 err_close_ruleset:
316 	close(ruleset_fd);
317 	return 1;
318 }
319