1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Landlock LSM - System call implementations and user space interfaces
4 *
5 * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
6 * Copyright © 2018-2020 ANSSI
7 */
8
9 #include <asm/current.h>
10 #include <linux/anon_inodes.h>
11 #include <linux/build_bug.h>
12 #include <linux/capability.h>
13 #include <linux/compiler_types.h>
14 #include <linux/dcache.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
17 #include <linux/fs.h>
18 #include <linux/limits.h>
19 #include <linux/mount.h>
20 #include <linux/path.h>
21 #include <linux/sched.h>
22 #include <linux/security.h>
23 #include <linux/stddef.h>
24 #include <linux/syscalls.h>
25 #include <linux/types.h>
26 #include <linux/uaccess.h>
27 #include <uapi/linux/landlock.h>
28
29 #include "cred.h"
30 #include "fs.h"
31 #include "limits.h"
32 #include "ruleset.h"
33 #include "setup.h"
34
is_initialized(void)35 static bool is_initialized(void)
36 {
37 if (likely(landlock_initialized))
38 return true;
39
40 pr_warn_once(
41 "Disabled but requested by user space. "
42 "You should enable Landlock at boot time: "
43 "https://docs.kernel.org/userspace-api/landlock.html#boot-time-configuration\n");
44 return false;
45 }
46
47 /**
48 * copy_min_struct_from_user - Safe future-proof argument copying
49 *
50 * Extend copy_struct_from_user() to check for consistent user buffer.
51 *
52 * @dst: Kernel space pointer or NULL.
53 * @ksize: Actual size of the data pointed to by @dst.
54 * @ksize_min: Minimal required size to be copied.
55 * @src: User space pointer or NULL.
56 * @usize: (Alleged) size of the data pointed to by @src.
57 */
58 static __always_inline int
copy_min_struct_from_user(void * const dst,const size_t ksize,const size_t ksize_min,const void __user * const src,const size_t usize)59 copy_min_struct_from_user(void *const dst, const size_t ksize,
60 const size_t ksize_min, const void __user *const src,
61 const size_t usize)
62 {
63 /* Checks buffer inconsistencies. */
64 BUILD_BUG_ON(!dst);
65 if (!src)
66 return -EFAULT;
67
68 /* Checks size ranges. */
69 BUILD_BUG_ON(ksize <= 0);
70 BUILD_BUG_ON(ksize < ksize_min);
71 if (usize < ksize_min)
72 return -EINVAL;
73 if (usize > PAGE_SIZE)
74 return -E2BIG;
75
76 /* Copies user buffer and fills with zeros. */
77 return copy_struct_from_user(dst, ksize, src, usize);
78 }
79
80 /*
81 * This function only contains arithmetic operations with constants, leading to
82 * BUILD_BUG_ON(). The related code is evaluated and checked at build time,
83 * but it is then ignored thanks to compiler optimizations.
84 */
build_check_abi(void)85 static void build_check_abi(void)
86 {
87 struct landlock_ruleset_attr ruleset_attr;
88 struct landlock_path_beneath_attr path_beneath_attr;
89 size_t ruleset_size, path_beneath_size;
90
91 /*
92 * For each user space ABI structures, first checks that there is no
93 * hole in them, then checks that all architectures have the same
94 * struct size.
95 */
96 ruleset_size = sizeof(ruleset_attr.handled_access_fs);
97 BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);
98 BUILD_BUG_ON(sizeof(ruleset_attr) != 8);
99
100 path_beneath_size = sizeof(path_beneath_attr.allowed_access);
101 path_beneath_size += sizeof(path_beneath_attr.parent_fd);
102 BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);
103 BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);
104 }
105
106 /* Ruleset handling */
107
fop_ruleset_release(struct inode * const inode,struct file * const filp)108 static int fop_ruleset_release(struct inode *const inode,
109 struct file *const filp)
110 {
111 struct landlock_ruleset *ruleset = filp->private_data;
112
113 landlock_put_ruleset(ruleset);
114 return 0;
115 }
116
fop_dummy_read(struct file * const filp,char __user * const buf,const size_t size,loff_t * const ppos)117 static ssize_t fop_dummy_read(struct file *const filp, char __user *const buf,
118 const size_t size, loff_t *const ppos)
119 {
120 /* Dummy handler to enable FMODE_CAN_READ. */
121 return -EINVAL;
122 }
123
fop_dummy_write(struct file * const filp,const char __user * const buf,const size_t size,loff_t * const ppos)124 static ssize_t fop_dummy_write(struct file *const filp,
125 const char __user *const buf, const size_t size,
126 loff_t *const ppos)
127 {
128 /* Dummy handler to enable FMODE_CAN_WRITE. */
129 return -EINVAL;
130 }
131
132 /*
133 * A ruleset file descriptor enables to build a ruleset by adding (i.e.
134 * writing) rule after rule, without relying on the task's context. This
135 * reentrant design is also used in a read way to enforce the ruleset on the
136 * current task.
137 */
138 static const struct file_operations ruleset_fops = {
139 .release = fop_ruleset_release,
140 .read = fop_dummy_read,
141 .write = fop_dummy_write,
142 };
143
144 #define LANDLOCK_ABI_VERSION 3
145
146 /**
147 * sys_landlock_create_ruleset - Create a new ruleset
148 *
149 * @attr: Pointer to a &struct landlock_ruleset_attr identifying the scope of
150 * the new ruleset.
151 * @size: Size of the pointed &struct landlock_ruleset_attr (needed for
152 * backward and forward compatibility).
153 * @flags: Supported value:
154 * - %LANDLOCK_CREATE_RULESET_VERSION
155 * - %LANDLOCK_CREATE_RULESET_ERRATA
156 *
157 * This system call enables to create a new Landlock ruleset, and returns the
158 * related file descriptor on success.
159 *
160 * If @flags is %LANDLOCK_CREATE_RULESET_VERSION and @attr is NULL and @size is
161 * 0, then the returned value is the highest supported Landlock ABI version
162 * (starting at 1).
163 *
164 * If @flags is %LANDLOCK_CREATE_RULESET_ERRATA and @attr is NULL and @size is
165 * 0, then the returned value is a bitmask of fixed issues for the current
166 * Landlock ABI version.
167 *
168 * Possible returned errors are:
169 *
170 * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
171 * - %EINVAL: unknown @flags, or unknown access, or too small @size;
172 * - %E2BIG or %EFAULT: @attr or @size inconsistencies;
173 * - %ENOMSG: empty &landlock_ruleset_attr.handled_access_fs.
174 */
SYSCALL_DEFINE3(landlock_create_ruleset,const struct landlock_ruleset_attr __user * const,attr,const size_t,size,const __u32,flags)175 SYSCALL_DEFINE3(landlock_create_ruleset,
176 const struct landlock_ruleset_attr __user *const, attr,
177 const size_t, size, const __u32, flags)
178 {
179 struct landlock_ruleset_attr ruleset_attr;
180 struct landlock_ruleset *ruleset;
181 int err, ruleset_fd;
182
183 /* Build-time checks. */
184 build_check_abi();
185
186 if (!is_initialized())
187 return -EOPNOTSUPP;
188
189 if (flags) {
190 if (attr || size)
191 return -EINVAL;
192
193 if (flags == LANDLOCK_CREATE_RULESET_VERSION)
194 return landlock_abi_version;
195
196 if (flags == LANDLOCK_CREATE_RULESET_ERRATA)
197 return landlock_errata;
198
199 return -EINVAL;
200 }
201
202 /* Copies raw user space buffer. */
203 err = copy_min_struct_from_user(&ruleset_attr, sizeof(ruleset_attr),
204 offsetofend(typeof(ruleset_attr),
205 handled_access_fs),
206 attr, size);
207 if (err)
208 return err;
209
210 /* Checks content (and 32-bits cast). */
211 if ((ruleset_attr.handled_access_fs | LANDLOCK_MASK_ACCESS_FS) !=
212 LANDLOCK_MASK_ACCESS_FS)
213 return -EINVAL;
214
215 /* Checks arguments and transforms to kernel struct. */
216 ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs);
217 if (IS_ERR(ruleset))
218 return PTR_ERR(ruleset);
219
220 /* Creates anonymous FD referring to the ruleset. */
221 ruleset_fd = anon_inode_getfd("[landlock-ruleset]", &ruleset_fops,
222 ruleset, O_RDWR | O_CLOEXEC);
223 if (ruleset_fd < 0)
224 landlock_put_ruleset(ruleset);
225 return ruleset_fd;
226 }
227
228 const int landlock_abi_version = LANDLOCK_ABI_VERSION;
229
230 /*
231 * Returns an owned ruleset from a FD. It is thus needed to call
232 * landlock_put_ruleset() on the return value.
233 */
get_ruleset_from_fd(const int fd,const fmode_t mode)234 static struct landlock_ruleset *get_ruleset_from_fd(const int fd,
235 const fmode_t mode)
236 {
237 struct fd ruleset_f;
238 struct landlock_ruleset *ruleset;
239
240 ruleset_f = fdget(fd);
241 if (!ruleset_f.file)
242 return ERR_PTR(-EBADF);
243
244 /* Checks FD type and access right. */
245 if (ruleset_f.file->f_op != &ruleset_fops) {
246 ruleset = ERR_PTR(-EBADFD);
247 goto out_fdput;
248 }
249 if (!(ruleset_f.file->f_mode & mode)) {
250 ruleset = ERR_PTR(-EPERM);
251 goto out_fdput;
252 }
253 ruleset = ruleset_f.file->private_data;
254 if (WARN_ON_ONCE(ruleset->num_layers != 1)) {
255 ruleset = ERR_PTR(-EINVAL);
256 goto out_fdput;
257 }
258 landlock_get_ruleset(ruleset);
259
260 out_fdput:
261 fdput(ruleset_f);
262 return ruleset;
263 }
264
265 /* Path handling */
266
267 /*
268 * @path: Must call put_path(@path) after the call if it succeeded.
269 */
get_path_from_fd(const s32 fd,struct path * const path)270 static int get_path_from_fd(const s32 fd, struct path *const path)
271 {
272 struct fd f;
273 int err = 0;
274
275 BUILD_BUG_ON(!__same_type(
276 fd, ((struct landlock_path_beneath_attr *)NULL)->parent_fd));
277
278 /* Handles O_PATH. */
279 f = fdget_raw(fd);
280 if (!f.file)
281 return -EBADF;
282 /*
283 * Forbids ruleset FDs, internal filesystems (e.g. nsfs), including
284 * pseudo filesystems that will never be mountable (e.g. sockfs,
285 * pipefs).
286 */
287 if ((f.file->f_op == &ruleset_fops) ||
288 (f.file->f_path.mnt->mnt_flags & MNT_INTERNAL) ||
289 (f.file->f_path.dentry->d_sb->s_flags & SB_NOUSER) ||
290 d_is_negative(f.file->f_path.dentry) ||
291 IS_PRIVATE(d_backing_inode(f.file->f_path.dentry))) {
292 err = -EBADFD;
293 goto out_fdput;
294 }
295 *path = f.file->f_path;
296 path_get(path);
297
298 out_fdput:
299 fdput(f);
300 return err;
301 }
302
303 /**
304 * sys_landlock_add_rule - Add a new rule to a ruleset
305 *
306 * @ruleset_fd: File descriptor tied to the ruleset that should be extended
307 * with the new rule.
308 * @rule_type: Identify the structure type pointed to by @rule_attr (only
309 * %LANDLOCK_RULE_PATH_BENEATH for now).
310 * @rule_attr: Pointer to a rule (only of type &struct
311 * landlock_path_beneath_attr for now).
312 * @flags: Must be 0.
313 *
314 * This system call enables to define a new rule and add it to an existing
315 * ruleset.
316 *
317 * Possible returned errors are:
318 *
319 * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
320 * - %EINVAL: @flags is not 0, or inconsistent access in the rule (i.e.
321 * &landlock_path_beneath_attr.allowed_access is not a subset of the
322 * ruleset handled accesses);
323 * - %ENOMSG: Empty accesses (e.g. &landlock_path_beneath_attr.allowed_access);
324 * - %EBADF: @ruleset_fd is not a file descriptor for the current thread, or a
325 * member of @rule_attr is not a file descriptor as expected;
326 * - %EBADFD: @ruleset_fd is not a ruleset file descriptor, or a member of
327 * @rule_attr is not the expected file descriptor type;
328 * - %EPERM: @ruleset_fd has no write access to the underlying ruleset;
329 * - %EFAULT: @rule_attr inconsistency.
330 */
SYSCALL_DEFINE4(landlock_add_rule,const int,ruleset_fd,const enum landlock_rule_type,rule_type,const void __user * const,rule_attr,const __u32,flags)331 SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
332 const enum landlock_rule_type, rule_type,
333 const void __user *const, rule_attr, const __u32, flags)
334 {
335 struct landlock_path_beneath_attr path_beneath_attr;
336 struct path path;
337 struct landlock_ruleset *ruleset;
338 int res, err;
339
340 if (!is_initialized())
341 return -EOPNOTSUPP;
342
343 /* No flag for now. */
344 if (flags)
345 return -EINVAL;
346
347 /* Gets and checks the ruleset. */
348 ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_WRITE);
349 if (IS_ERR(ruleset))
350 return PTR_ERR(ruleset);
351
352 if (rule_type != LANDLOCK_RULE_PATH_BENEATH) {
353 err = -EINVAL;
354 goto out_put_ruleset;
355 }
356
357 /* Copies raw user space buffer, only one type for now. */
358 res = copy_from_user(&path_beneath_attr, rule_attr,
359 sizeof(path_beneath_attr));
360 if (res) {
361 err = -EFAULT;
362 goto out_put_ruleset;
363 }
364
365 /*
366 * Informs about useless rule: empty allowed_access (i.e. deny rules)
367 * are ignored in path walks.
368 */
369 if (!path_beneath_attr.allowed_access) {
370 err = -ENOMSG;
371 goto out_put_ruleset;
372 }
373 /*
374 * Checks that allowed_access matches the @ruleset constraints
375 * (ruleset->fs_access_masks[0] is automatically upgraded to 64-bits).
376 */
377 if ((path_beneath_attr.allowed_access | ruleset->fs_access_masks[0]) !=
378 ruleset->fs_access_masks[0]) {
379 err = -EINVAL;
380 goto out_put_ruleset;
381 }
382
383 /* Gets and checks the new rule. */
384 err = get_path_from_fd(path_beneath_attr.parent_fd, &path);
385 if (err)
386 goto out_put_ruleset;
387
388 /* Imports the new rule. */
389 err = landlock_append_fs_rule(ruleset, &path,
390 path_beneath_attr.allowed_access);
391 path_put(&path);
392
393 out_put_ruleset:
394 landlock_put_ruleset(ruleset);
395 return err;
396 }
397
398 /* Enforcement */
399
400 /**
401 * sys_landlock_restrict_self - Enforce a ruleset on the calling thread
402 *
403 * @ruleset_fd: File descriptor tied to the ruleset to merge with the target.
404 * @flags: Must be 0.
405 *
406 * This system call enables to enforce a Landlock ruleset on the current
407 * thread. Enforcing a ruleset requires that the task has %CAP_SYS_ADMIN in its
408 * namespace or is running with no_new_privs. This avoids scenarios where
409 * unprivileged tasks can affect the behavior of privileged children.
410 *
411 * Possible returned errors are:
412 *
413 * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
414 * - %EINVAL: @flags is not 0.
415 * - %EBADF: @ruleset_fd is not a file descriptor for the current thread;
416 * - %EBADFD: @ruleset_fd is not a ruleset file descriptor;
417 * - %EPERM: @ruleset_fd has no read access to the underlying ruleset, or the
418 * current thread is not running with no_new_privs, or it doesn't have
419 * %CAP_SYS_ADMIN in its namespace.
420 * - %E2BIG: The maximum number of stacked rulesets is reached for the current
421 * thread.
422 */
SYSCALL_DEFINE2(landlock_restrict_self,const int,ruleset_fd,const __u32,flags)423 SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32,
424 flags)
425 {
426 struct landlock_ruleset *new_dom, *ruleset;
427 struct cred *new_cred;
428 struct landlock_cred_security *new_llcred;
429 int err;
430
431 if (!is_initialized())
432 return -EOPNOTSUPP;
433
434 /*
435 * Similar checks as for seccomp(2), except that an -EPERM may be
436 * returned.
437 */
438 if (!task_no_new_privs(current) &&
439 !ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN))
440 return -EPERM;
441
442 /* No flag for now. */
443 if (flags)
444 return -EINVAL;
445
446 /* Gets and checks the ruleset. */
447 ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_READ);
448 if (IS_ERR(ruleset))
449 return PTR_ERR(ruleset);
450
451 /* Prepares new credentials. */
452 new_cred = prepare_creds();
453 if (!new_cred) {
454 err = -ENOMEM;
455 goto out_put_ruleset;
456 }
457 new_llcred = landlock_cred(new_cred);
458
459 /*
460 * There is no possible race condition while copying and manipulating
461 * the current credentials because they are dedicated per thread.
462 */
463 new_dom = landlock_merge_ruleset(new_llcred->domain, ruleset);
464 if (IS_ERR(new_dom)) {
465 err = PTR_ERR(new_dom);
466 goto out_put_creds;
467 }
468
469 /* Replaces the old (prepared) domain. */
470 landlock_put_ruleset(new_llcred->domain);
471 new_llcred->domain = new_dom;
472
473 landlock_put_ruleset(ruleset);
474 return commit_creds(new_cred);
475
476 out_put_creds:
477 abort_creds(new_cred);
478
479 out_put_ruleset:
480 landlock_put_ruleset(ruleset);
481 return err;
482 }
483