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/cleanup.h>
14 #include <linux/compiler_types.h>
15 #include <linux/dcache.h>
16 #include <linux/err.h>
17 #include <linux/errno.h>
19 #include <linux/limits.h>
20 #include <linux/mount.h>
21 #include <linux/path.h>
22 #include <linux/sched.h>
23 #include <linux/security.h>
24 #include <linux/stddef.h>
25 #include <linux/syscalls.h>
26 #include <linux/types.h>
27 #include <linux/uaccess.h>
28 #include <uapi/linux/landlock.h>
37 static bool is_initialized(void)
39 if (likely(landlock_initialized
))
43 "Disabled but requested by user space. "
44 "You should enable Landlock at boot time: "
45 "https://docs.kernel.org/userspace-api/landlock.html#boot-time-configuration\n");
50 * copy_min_struct_from_user - Safe future-proof argument copying
52 * Extend copy_struct_from_user() to check for consistent user buffer.
54 * @dst: Kernel space pointer or NULL.
55 * @ksize: Actual size of the data pointed to by @dst.
56 * @ksize_min: Minimal required size to be copied.
57 * @src: User space pointer or NULL.
58 * @usize: (Alleged) size of the data pointed to by @src.
60 static __always_inline
int
61 copy_min_struct_from_user(void *const dst
, const size_t ksize
,
62 const size_t ksize_min
, const void __user
*const src
,
65 /* Checks buffer inconsistencies. */
70 /* Checks size ranges. */
71 BUILD_BUG_ON(ksize
<= 0);
72 BUILD_BUG_ON(ksize
< ksize_min
);
73 if (usize
< ksize_min
)
75 if (usize
> PAGE_SIZE
)
78 /* Copies user buffer and fills with zeros. */
79 return copy_struct_from_user(dst
, ksize
, src
, usize
);
83 * This function only contains arithmetic operations with constants, leading to
84 * BUILD_BUG_ON(). The related code is evaluated and checked at build time,
85 * but it is then ignored thanks to compiler optimizations.
87 static void build_check_abi(void)
89 struct landlock_ruleset_attr ruleset_attr
;
90 struct landlock_path_beneath_attr path_beneath_attr
;
91 struct landlock_net_port_attr net_port_attr
;
92 size_t ruleset_size
, path_beneath_size
, net_port_size
;
95 * For each user space ABI structures, first checks that there is no
96 * hole in them, then checks that all architectures have the same
99 ruleset_size
= sizeof(ruleset_attr
.handled_access_fs
);
100 ruleset_size
+= sizeof(ruleset_attr
.handled_access_net
);
101 ruleset_size
+= sizeof(ruleset_attr
.scoped
);
102 BUILD_BUG_ON(sizeof(ruleset_attr
) != ruleset_size
);
103 BUILD_BUG_ON(sizeof(ruleset_attr
) != 24);
105 path_beneath_size
= sizeof(path_beneath_attr
.allowed_access
);
106 path_beneath_size
+= sizeof(path_beneath_attr
.parent_fd
);
107 BUILD_BUG_ON(sizeof(path_beneath_attr
) != path_beneath_size
);
108 BUILD_BUG_ON(sizeof(path_beneath_attr
) != 12);
110 net_port_size
= sizeof(net_port_attr
.allowed_access
);
111 net_port_size
+= sizeof(net_port_attr
.port
);
112 BUILD_BUG_ON(sizeof(net_port_attr
) != net_port_size
);
113 BUILD_BUG_ON(sizeof(net_port_attr
) != 16);
116 /* Ruleset handling */
118 static int fop_ruleset_release(struct inode
*const inode
,
119 struct file
*const filp
)
121 struct landlock_ruleset
*ruleset
= filp
->private_data
;
123 landlock_put_ruleset(ruleset
);
127 static ssize_t
fop_dummy_read(struct file
*const filp
, char __user
*const buf
,
128 const size_t size
, loff_t
*const ppos
)
130 /* Dummy handler to enable FMODE_CAN_READ. */
134 static ssize_t
fop_dummy_write(struct file
*const filp
,
135 const char __user
*const buf
, const size_t size
,
138 /* Dummy handler to enable FMODE_CAN_WRITE. */
143 * A ruleset file descriptor enables to build a ruleset by adding (i.e.
144 * writing) rule after rule, without relying on the task's context. This
145 * reentrant design is also used in a read way to enforce the ruleset on the
148 static const struct file_operations ruleset_fops
= {
149 .release
= fop_ruleset_release
,
150 .read
= fop_dummy_read
,
151 .write
= fop_dummy_write
,
154 #define LANDLOCK_ABI_VERSION 6
157 * sys_landlock_create_ruleset - Create a new ruleset
159 * @attr: Pointer to a &struct landlock_ruleset_attr identifying the scope of
161 * @size: Size of the pointed &struct landlock_ruleset_attr (needed for
162 * backward and forward compatibility).
163 * @flags: Supported value: %LANDLOCK_CREATE_RULESET_VERSION.
165 * This system call enables to create a new Landlock ruleset, and returns the
166 * related file descriptor on success.
168 * If @flags is %LANDLOCK_CREATE_RULESET_VERSION and @attr is NULL and @size is
169 * 0, then the returned value is the highest supported Landlock ABI version
172 * Possible returned errors are:
174 * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
175 * - %EINVAL: unknown @flags, or unknown access, or unknown scope, or too small @size;
176 * - %E2BIG: @attr or @size inconsistencies;
177 * - %EFAULT: @attr or @size inconsistencies;
178 * - %ENOMSG: empty &landlock_ruleset_attr.handled_access_fs.
180 SYSCALL_DEFINE3(landlock_create_ruleset
,
181 const struct landlock_ruleset_attr __user
*const, attr
,
182 const size_t, size
, const __u32
, flags
)
184 struct landlock_ruleset_attr ruleset_attr
;
185 struct landlock_ruleset
*ruleset
;
188 /* Build-time checks. */
191 if (!is_initialized())
195 if ((flags
== LANDLOCK_CREATE_RULESET_VERSION
) && !attr
&&
197 return LANDLOCK_ABI_VERSION
;
201 /* Copies raw user space buffer. */
202 err
= copy_min_struct_from_user(&ruleset_attr
, sizeof(ruleset_attr
),
203 offsetofend(typeof(ruleset_attr
),
209 /* Checks content (and 32-bits cast). */
210 if ((ruleset_attr
.handled_access_fs
| LANDLOCK_MASK_ACCESS_FS
) !=
211 LANDLOCK_MASK_ACCESS_FS
)
214 /* Checks network content (and 32-bits cast). */
215 if ((ruleset_attr
.handled_access_net
| LANDLOCK_MASK_ACCESS_NET
) !=
216 LANDLOCK_MASK_ACCESS_NET
)
219 /* Checks IPC scoping content (and 32-bits cast). */
220 if ((ruleset_attr
.scoped
| LANDLOCK_MASK_SCOPE
) != LANDLOCK_MASK_SCOPE
)
223 /* Checks arguments and transforms to kernel struct. */
224 ruleset
= landlock_create_ruleset(ruleset_attr
.handled_access_fs
,
225 ruleset_attr
.handled_access_net
,
226 ruleset_attr
.scoped
);
228 return PTR_ERR(ruleset
);
230 /* Creates anonymous FD referring to the ruleset. */
231 ruleset_fd
= anon_inode_getfd("[landlock-ruleset]", &ruleset_fops
,
232 ruleset
, O_RDWR
| O_CLOEXEC
);
234 landlock_put_ruleset(ruleset
);
239 * Returns an owned ruleset from a FD. It is thus needed to call
240 * landlock_put_ruleset() on the return value.
242 static struct landlock_ruleset
*get_ruleset_from_fd(const int fd
,
245 CLASS(fd
, ruleset_f
)(fd
);
246 struct landlock_ruleset
*ruleset
;
248 if (fd_empty(ruleset_f
))
249 return ERR_PTR(-EBADF
);
251 /* Checks FD type and access right. */
252 if (fd_file(ruleset_f
)->f_op
!= &ruleset_fops
)
253 return ERR_PTR(-EBADFD
);
254 if (!(fd_file(ruleset_f
)->f_mode
& mode
))
255 return ERR_PTR(-EPERM
);
256 ruleset
= fd_file(ruleset_f
)->private_data
;
257 if (WARN_ON_ONCE(ruleset
->num_layers
!= 1))
258 return ERR_PTR(-EINVAL
);
259 landlock_get_ruleset(ruleset
);
266 * @path: Must call put_path(@path) after the call if it succeeded.
268 static int get_path_from_fd(const s32 fd
, struct path
*const path
)
270 CLASS(fd_raw
, f
)(fd
);
272 BUILD_BUG_ON(!__same_type(
273 fd
, ((struct landlock_path_beneath_attr
*)NULL
)->parent_fd
));
278 * Forbids ruleset FDs, internal filesystems (e.g. nsfs), including
279 * pseudo filesystems that will never be mountable (e.g. sockfs,
282 if ((fd_file(f
)->f_op
== &ruleset_fops
) ||
283 (fd_file(f
)->f_path
.mnt
->mnt_flags
& MNT_INTERNAL
) ||
284 (fd_file(f
)->f_path
.dentry
->d_sb
->s_flags
& SB_NOUSER
) ||
285 d_is_negative(fd_file(f
)->f_path
.dentry
) ||
286 IS_PRIVATE(d_backing_inode(fd_file(f
)->f_path
.dentry
)))
289 *path
= fd_file(f
)->f_path
;
294 static int add_rule_path_beneath(struct landlock_ruleset
*const ruleset
,
295 const void __user
*const rule_attr
)
297 struct landlock_path_beneath_attr path_beneath_attr
;
302 /* Copies raw user space buffer. */
303 res
= copy_from_user(&path_beneath_attr
, rule_attr
,
304 sizeof(path_beneath_attr
));
309 * Informs about useless rule: empty allowed_access (i.e. deny rules)
310 * are ignored in path walks.
312 if (!path_beneath_attr
.allowed_access
)
315 /* Checks that allowed_access matches the @ruleset constraints. */
316 mask
= ruleset
->access_masks
[0].fs
;
317 if ((path_beneath_attr
.allowed_access
| mask
) != mask
)
320 /* Gets and checks the new rule. */
321 err
= get_path_from_fd(path_beneath_attr
.parent_fd
, &path
);
325 /* Imports the new rule. */
326 err
= landlock_append_fs_rule(ruleset
, &path
,
327 path_beneath_attr
.allowed_access
);
332 static int add_rule_net_port(struct landlock_ruleset
*ruleset
,
333 const void __user
*const rule_attr
)
335 struct landlock_net_port_attr net_port_attr
;
339 /* Copies raw user space buffer. */
340 res
= copy_from_user(&net_port_attr
, rule_attr
, sizeof(net_port_attr
));
345 * Informs about useless rule: empty allowed_access (i.e. deny rules)
346 * are ignored by network actions.
348 if (!net_port_attr
.allowed_access
)
351 /* Checks that allowed_access matches the @ruleset constraints. */
352 mask
= landlock_get_net_access_mask(ruleset
, 0);
353 if ((net_port_attr
.allowed_access
| mask
) != mask
)
356 /* Denies inserting a rule with port greater than 65535. */
357 if (net_port_attr
.port
> U16_MAX
)
360 /* Imports the new rule. */
361 return landlock_append_net_rule(ruleset
, net_port_attr
.port
,
362 net_port_attr
.allowed_access
);
366 * sys_landlock_add_rule - Add a new rule to a ruleset
368 * @ruleset_fd: File descriptor tied to the ruleset that should be extended
370 * @rule_type: Identify the structure type pointed to by @rule_attr:
371 * %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.
372 * @rule_attr: Pointer to a rule (matching the @rule_type).
375 * This system call enables to define a new rule and add it to an existing
378 * Possible returned errors are:
380 * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
381 * - %EAFNOSUPPORT: @rule_type is %LANDLOCK_RULE_NET_PORT but TCP/IP is not
382 * supported by the running kernel;
383 * - %EINVAL: @flags is not 0;
384 * - %EINVAL: The rule accesses are inconsistent (i.e.
385 * &landlock_path_beneath_attr.allowed_access or
386 * &landlock_net_port_attr.allowed_access is not a subset of the ruleset
388 * - %EINVAL: &landlock_net_port_attr.port is greater than 65535;
389 * - %ENOMSG: Empty accesses (e.g. &landlock_path_beneath_attr.allowed_access is
391 * - %EBADF: @ruleset_fd is not a file descriptor for the current thread, or a
392 * member of @rule_attr is not a file descriptor as expected;
393 * - %EBADFD: @ruleset_fd is not a ruleset file descriptor, or a member of
394 * @rule_attr is not the expected file descriptor type;
395 * - %EPERM: @ruleset_fd has no write access to the underlying ruleset;
396 * - %EFAULT: @rule_attr was not a valid address.
398 SYSCALL_DEFINE4(landlock_add_rule
, const int, ruleset_fd
,
399 const enum landlock_rule_type
, rule_type
,
400 const void __user
*const, rule_attr
, const __u32
, flags
)
402 struct landlock_ruleset
*ruleset
__free(landlock_put_ruleset
) = NULL
;
404 if (!is_initialized())
407 /* No flag for now. */
411 /* Gets and checks the ruleset. */
412 ruleset
= get_ruleset_from_fd(ruleset_fd
, FMODE_CAN_WRITE
);
414 return PTR_ERR(ruleset
);
417 case LANDLOCK_RULE_PATH_BENEATH
:
418 return add_rule_path_beneath(ruleset
, rule_attr
);
419 case LANDLOCK_RULE_NET_PORT
:
420 return add_rule_net_port(ruleset
, rule_attr
);
429 * sys_landlock_restrict_self - Enforce a ruleset on the calling thread
431 * @ruleset_fd: File descriptor tied to the ruleset to merge with the target.
434 * This system call enables to enforce a Landlock ruleset on the current
435 * thread. Enforcing a ruleset requires that the task has %CAP_SYS_ADMIN in its
436 * namespace or is running with no_new_privs. This avoids scenarios where
437 * unprivileged tasks can affect the behavior of privileged children.
439 * Possible returned errors are:
441 * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
442 * - %EINVAL: @flags is not 0.
443 * - %EBADF: @ruleset_fd is not a file descriptor for the current thread;
444 * - %EBADFD: @ruleset_fd is not a ruleset file descriptor;
445 * - %EPERM: @ruleset_fd has no read access to the underlying ruleset, or the
446 * current thread is not running with no_new_privs, or it doesn't have
447 * %CAP_SYS_ADMIN in its namespace.
448 * - %E2BIG: The maximum number of stacked rulesets is reached for the current
451 SYSCALL_DEFINE2(landlock_restrict_self
, const int, ruleset_fd
, const __u32
,
454 struct landlock_ruleset
*new_dom
,
455 *ruleset
__free(landlock_put_ruleset
) = NULL
;
456 struct cred
*new_cred
;
457 struct landlock_cred_security
*new_llcred
;
459 if (!is_initialized())
463 * Similar checks as for seccomp(2), except that an -EPERM may be
466 if (!task_no_new_privs(current
) &&
467 !ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN
))
470 /* No flag for now. */
474 /* Gets and checks the ruleset. */
475 ruleset
= get_ruleset_from_fd(ruleset_fd
, FMODE_CAN_READ
);
477 return PTR_ERR(ruleset
);
479 /* Prepares new credentials. */
480 new_cred
= prepare_creds();
484 new_llcred
= landlock_cred(new_cred
);
487 * There is no possible race condition while copying and manipulating
488 * the current credentials because they are dedicated per thread.
490 new_dom
= landlock_merge_ruleset(new_llcred
->domain
, ruleset
);
491 if (IS_ERR(new_dom
)) {
492 abort_creds(new_cred
);
493 return PTR_ERR(new_dom
);
496 /* Replaces the old (prepared) domain. */
497 landlock_put_ruleset(new_llcred
->domain
);
498 new_llcred
->domain
= new_dom
;
499 return commit_creds(new_cred
);