1 // SPDX-License-Identifier: GPL-2.0-only
3 * Landlock LSM - System call implementations and user space interfaces
5 * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
6 * Copyright © 2018-2020 ANSSI
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>
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>
35 static bool is_initialized(void)
37 if (likely(landlock_initialized))
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");
48 * copy_min_struct_from_user - Safe future-proof argument copying
50 * Extend copy_struct_from_user() to check for consistent user buffer.
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.
58 static __always_inline int
59 copy_min_struct_from_user(void *const dst, const size_t ksize,
60 const size_t ksize_min, const void __user *const src,
63 /* Checks buffer inconsistencies. */
68 /* Checks size ranges. */
69 BUILD_BUG_ON(ksize <= 0);
70 BUILD_BUG_ON(ksize < ksize_min);
71 if (usize < ksize_min)
73 if (usize > PAGE_SIZE)
76 /* Copies user buffer and fills with zeros. */
77 return copy_struct_from_user(dst, ksize, src, usize);
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.
85 static void build_check_abi(void)
87 struct landlock_ruleset_attr ruleset_attr;
88 struct landlock_path_beneath_attr path_beneath_attr;
89 size_t ruleset_size, path_beneath_size;
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
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);
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);
106 /* Ruleset handling */
108 static int fop_ruleset_release(struct inode *const inode,
109 struct file *const filp)
111 struct landlock_ruleset *ruleset = filp->private_data;
113 landlock_put_ruleset(ruleset);
117 static ssize_t fop_dummy_read(struct file *const filp, char __user *const buf,
118 const size_t size, loff_t *const ppos)
120 /* Dummy handler to enable FMODE_CAN_READ. */
124 static ssize_t fop_dummy_write(struct file *const filp,
125 const char __user *const buf, const size_t size,
128 /* Dummy handler to enable FMODE_CAN_WRITE. */
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
138 static const struct file_operations ruleset_fops = {
139 .release = fop_ruleset_release,
140 .read = fop_dummy_read,
141 .write = fop_dummy_write,
144 #define LANDLOCK_ABI_VERSION 3
147 * sys_landlock_create_ruleset - Create a new ruleset
149 * @attr: Pointer to a &struct landlock_ruleset_attr identifying the scope of
151 * @size: Size of the pointed &struct landlock_ruleset_attr (needed for
152 * backward and forward compatibility).
153 * @flags: Supported value: %LANDLOCK_CREATE_RULESET_VERSION.
155 * This system call enables to create a new Landlock ruleset, and returns the
156 * related file descriptor on success.
158 * If @flags is %LANDLOCK_CREATE_RULESET_VERSION and @attr is NULL and @size is
159 * 0, then the returned value is the highest supported Landlock ABI version
162 * Possible returned errors are:
164 * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
165 * - %EINVAL: unknown @flags, or unknown access, or too small @size;
166 * - %E2BIG or %EFAULT: @attr or @size inconsistencies;
167 * - %ENOMSG: empty &landlock_ruleset_attr.handled_access_fs.
169 SYSCALL_DEFINE3(landlock_create_ruleset,
170 const struct landlock_ruleset_attr __user *const, attr,
171 const size_t, size, const __u32, flags)
173 struct landlock_ruleset_attr ruleset_attr;
174 struct landlock_ruleset *ruleset;
177 /* Build-time checks. */
180 if (!is_initialized())
184 if ((flags == LANDLOCK_CREATE_RULESET_VERSION) && !attr &&
186 return LANDLOCK_ABI_VERSION;
190 /* Copies raw user space buffer. */
191 err = copy_min_struct_from_user(&ruleset_attr, sizeof(ruleset_attr),
192 offsetofend(typeof(ruleset_attr),
198 /* Checks content (and 32-bits cast). */
199 if ((ruleset_attr.handled_access_fs | LANDLOCK_MASK_ACCESS_FS) !=
200 LANDLOCK_MASK_ACCESS_FS)
203 /* Checks arguments and transforms to kernel struct. */
204 ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs);
206 return PTR_ERR(ruleset);
208 /* Creates anonymous FD referring to the ruleset. */
209 ruleset_fd = anon_inode_getfd("[landlock-ruleset]", &ruleset_fops,
210 ruleset, O_RDWR | O_CLOEXEC);
212 landlock_put_ruleset(ruleset);
217 * Returns an owned ruleset from a FD. It is thus needed to call
218 * landlock_put_ruleset() on the return value.
220 static struct landlock_ruleset *get_ruleset_from_fd(const int fd,
224 struct landlock_ruleset *ruleset;
226 ruleset_f = fdget(fd);
228 return ERR_PTR(-EBADF);
230 /* Checks FD type and access right. */
231 if (ruleset_f.file->f_op != &ruleset_fops) {
232 ruleset = ERR_PTR(-EBADFD);
235 if (!(ruleset_f.file->f_mode & mode)) {
236 ruleset = ERR_PTR(-EPERM);
239 ruleset = ruleset_f.file->private_data;
240 if (WARN_ON_ONCE(ruleset->num_layers != 1)) {
241 ruleset = ERR_PTR(-EINVAL);
244 landlock_get_ruleset(ruleset);
254 * @path: Must call put_path(@path) after the call if it succeeded.
256 static int get_path_from_fd(const s32 fd, struct path *const path)
261 BUILD_BUG_ON(!__same_type(
262 fd, ((struct landlock_path_beneath_attr *)NULL)->parent_fd));
264 /* Handles O_PATH. */
269 * Forbids ruleset FDs, internal filesystems (e.g. nsfs), including
270 * pseudo filesystems that will never be mountable (e.g. sockfs,
273 if ((f.file->f_op == &ruleset_fops) ||
274 (f.file->f_path.mnt->mnt_flags & MNT_INTERNAL) ||
275 (f.file->f_path.dentry->d_sb->s_flags & SB_NOUSER) ||
276 d_is_negative(f.file->f_path.dentry) ||
277 IS_PRIVATE(d_backing_inode(f.file->f_path.dentry))) {
281 *path = f.file->f_path;
290 * sys_landlock_add_rule - Add a new rule to a ruleset
292 * @ruleset_fd: File descriptor tied to the ruleset that should be extended
294 * @rule_type: Identify the structure type pointed to by @rule_attr (only
295 * %LANDLOCK_RULE_PATH_BENEATH for now).
296 * @rule_attr: Pointer to a rule (only of type &struct
297 * landlock_path_beneath_attr for now).
300 * This system call enables to define a new rule and add it to an existing
303 * Possible returned errors are:
305 * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
306 * - %EINVAL: @flags is not 0, or inconsistent access in the rule (i.e.
307 * &landlock_path_beneath_attr.allowed_access is not a subset of the
308 * ruleset handled accesses);
309 * - %ENOMSG: Empty accesses (e.g. &landlock_path_beneath_attr.allowed_access);
310 * - %EBADF: @ruleset_fd is not a file descriptor for the current thread, or a
311 * member of @rule_attr is not a file descriptor as expected;
312 * - %EBADFD: @ruleset_fd is not a ruleset file descriptor, or a member of
313 * @rule_attr is not the expected file descriptor type;
314 * - %EPERM: @ruleset_fd has no write access to the underlying ruleset;
315 * - %EFAULT: @rule_attr inconsistency.
317 SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
318 const enum landlock_rule_type, rule_type,
319 const void __user *const, rule_attr, const __u32, flags)
321 struct landlock_path_beneath_attr path_beneath_attr;
323 struct landlock_ruleset *ruleset;
326 if (!is_initialized())
329 /* No flag for now. */
333 /* Gets and checks the ruleset. */
334 ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_WRITE);
336 return PTR_ERR(ruleset);
338 if (rule_type != LANDLOCK_RULE_PATH_BENEATH) {
340 goto out_put_ruleset;
343 /* Copies raw user space buffer, only one type for now. */
344 res = copy_from_user(&path_beneath_attr, rule_attr,
345 sizeof(path_beneath_attr));
348 goto out_put_ruleset;
352 * Informs about useless rule: empty allowed_access (i.e. deny rules)
353 * are ignored in path walks.
355 if (!path_beneath_attr.allowed_access) {
357 goto out_put_ruleset;
360 * Checks that allowed_access matches the @ruleset constraints
361 * (ruleset->fs_access_masks[0] is automatically upgraded to 64-bits).
363 if ((path_beneath_attr.allowed_access | ruleset->fs_access_masks[0]) !=
364 ruleset->fs_access_masks[0]) {
366 goto out_put_ruleset;
369 /* Gets and checks the new rule. */
370 err = get_path_from_fd(path_beneath_attr.parent_fd, &path);
372 goto out_put_ruleset;
374 /* Imports the new rule. */
375 err = landlock_append_fs_rule(ruleset, &path,
376 path_beneath_attr.allowed_access);
380 landlock_put_ruleset(ruleset);
387 * sys_landlock_restrict_self - Enforce a ruleset on the calling thread
389 * @ruleset_fd: File descriptor tied to the ruleset to merge with the target.
392 * This system call enables to enforce a Landlock ruleset on the current
393 * thread. Enforcing a ruleset requires that the task has %CAP_SYS_ADMIN in its
394 * namespace or is running with no_new_privs. This avoids scenarios where
395 * unprivileged tasks can affect the behavior of privileged children.
397 * Possible returned errors are:
399 * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
400 * - %EINVAL: @flags is not 0.
401 * - %EBADF: @ruleset_fd is not a file descriptor for the current thread;
402 * - %EBADFD: @ruleset_fd is not a ruleset file descriptor;
403 * - %EPERM: @ruleset_fd has no read access to the underlying ruleset, or the
404 * current thread is not running with no_new_privs, or it doesn't have
405 * %CAP_SYS_ADMIN in its namespace.
406 * - %E2BIG: The maximum number of stacked rulesets is reached for the current
409 SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32,
412 struct landlock_ruleset *new_dom, *ruleset;
413 struct cred *new_cred;
414 struct landlock_cred_security *new_llcred;
417 if (!is_initialized())
421 * Similar checks as for seccomp(2), except that an -EPERM may be
424 if (!task_no_new_privs(current) &&
425 !ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN))
428 /* No flag for now. */
432 /* Gets and checks the ruleset. */
433 ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_READ);
435 return PTR_ERR(ruleset);
437 /* Prepares new credentials. */
438 new_cred = prepare_creds();
441 goto out_put_ruleset;
443 new_llcred = landlock_cred(new_cred);
446 * There is no possible race condition while copying and manipulating
447 * the current credentials because they are dedicated per thread.
449 new_dom = landlock_merge_ruleset(new_llcred->domain, ruleset);
450 if (IS_ERR(new_dom)) {
451 err = PTR_ERR(new_dom);
455 /* Replaces the old (prepared) domain. */
456 landlock_put_ruleset(new_llcred->domain);
457 new_llcred->domain = new_dom;
459 landlock_put_ruleset(ruleset);
460 return commit_creds(new_cred);
463 abort_creds(new_cred);
466 landlock_put_ruleset(ruleset);