1 // SPDX-License-Identifier: GPL-2.0-only
3 * Landlock LSM - Ptrace hooks
5 * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
6 * Copyright © 2019-2020 ANSSI
9 #include <asm/current.h>
10 #include <linux/cred.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/lsm_hooks.h>
14 #include <linux/rcupdate.h>
15 #include <linux/sched.h>
24 * domain_scope_le - Checks domain ordering for scoped ptrace
26 * @parent: Parent domain.
27 * @child: Potential child of @parent.
29 * Checks if the @parent domain is less or equal to (i.e. an ancestor, which
30 * means a subset of) the @child domain.
32 static bool domain_scope_le(const struct landlock_ruleset *const parent,
33 const struct landlock_ruleset *const child)
35 const struct landlock_hierarchy *walker;
41 for (walker = child->hierarchy; walker; walker = walker->parent) {
42 if (walker == parent->hierarchy)
43 /* @parent is in the scoped hierarchy of @child. */
46 /* There is no relationship between @parent and @child. */
50 static bool task_is_scoped(const struct task_struct *const parent,
51 const struct task_struct *const child)
54 const struct landlock_ruleset *dom_parent, *dom_child;
57 dom_parent = landlock_get_task_domain(parent);
58 dom_child = landlock_get_task_domain(child);
59 is_scoped = domain_scope_le(dom_parent, dom_child);
64 static int task_ptrace(const struct task_struct *const parent,
65 const struct task_struct *const child)
67 /* Quick return for non-landlocked tasks. */
68 if (!landlocked(parent))
70 if (task_is_scoped(parent, child))
76 * hook_ptrace_access_check - Determines whether the current process may access
79 * @child: Process to be accessed.
80 * @mode: Mode of attachment.
82 * If the current task has Landlock rules, then the child must have at least
83 * the same rules. Else denied.
85 * Determines whether a process may access another, returning 0 if permission
86 * granted, -errno if denied.
88 static int hook_ptrace_access_check(struct task_struct *const child,
89 const unsigned int mode)
91 return task_ptrace(current, child);
95 * hook_ptrace_traceme - Determines whether another process may trace the
98 * @parent: Task proposed to be the tracer.
100 * If the parent has Landlock rules, then the current task must have the same
101 * or more rules. Else denied.
103 * Determines whether the nominated task is permitted to trace the current
104 * process, returning 0 if permission is granted, -errno if denied.
106 static int hook_ptrace_traceme(struct task_struct *const parent)
108 return task_ptrace(parent, current);
111 static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = {
112 LSM_HOOK_INIT(ptrace_access_check, hook_ptrace_access_check),
113 LSM_HOOK_INIT(ptrace_traceme, hook_ptrace_traceme),
116 __init void landlock_add_ptrace_hooks(void)
118 security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),