2 * Yama Linux Security Module
4 * Author: Kees Cook <keescook@chromium.org>
6 * Copyright (C) 2010 Canonical, Ltd.
7 * Copyright (C) 2011 The Chromium OS Authors.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2, as
11 * published by the Free Software Foundation.
15 #include <linux/lsm_hooks.h>
16 #include <linux/sysctl.h>
17 #include <linux/ptrace.h>
18 #include <linux/prctl.h>
19 #include <linux/ratelimit.h>
20 #include <linux/workqueue.h>
21 #include <linux/string_helpers.h>
23 #define YAMA_SCOPE_DISABLED 0
24 #define YAMA_SCOPE_RELATIONAL 1
25 #define YAMA_SCOPE_CAPABILITY 2
26 #define YAMA_SCOPE_NO_ATTACH 3
28 static int ptrace_scope = YAMA_SCOPE_RELATIONAL;
30 /* describe a ptrace relationship for potential exception */
31 struct ptrace_relation {
32 struct task_struct *tracer;
33 struct task_struct *tracee;
35 struct list_head node;
39 static LIST_HEAD(ptracer_relations);
40 static DEFINE_SPINLOCK(ptracer_relations_lock);
42 static void yama_relation_cleanup(struct work_struct *work);
43 static DECLARE_WORK(yama_relation_work, yama_relation_cleanup);
45 static void report_access(const char *access, struct task_struct *target,
46 struct task_struct *agent)
48 char *target_cmd, *agent_cmd;
50 target_cmd = kstrdup_quotable_cmdline(target, GFP_ATOMIC);
51 agent_cmd = kstrdup_quotable_cmdline(agent, GFP_ATOMIC);
53 pr_notice_ratelimited(
54 "ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n",
55 access, target_cmd, target->pid, agent_cmd, agent->pid);
62 * yama_relation_cleanup - remove invalid entries from the relation list
65 static void yama_relation_cleanup(struct work_struct *work)
67 struct ptrace_relation *relation;
69 spin_lock(&ptracer_relations_lock);
71 list_for_each_entry_rcu(relation, &ptracer_relations, node) {
72 if (relation->invalid) {
73 list_del_rcu(&relation->node);
74 kfree_rcu(relation, rcu);
78 spin_unlock(&ptracer_relations_lock);
82 * yama_ptracer_add - add/replace an exception for this tracer/tracee pair
83 * @tracer: the task_struct of the process doing the ptrace
84 * @tracee: the task_struct of the process to be ptraced
86 * Each tracee can have, at most, one tracer registered. Each time this
87 * is called, the prior registered tracer will be replaced for the tracee.
89 * Returns 0 if relationship was added, -ve on error.
91 static int yama_ptracer_add(struct task_struct *tracer,
92 struct task_struct *tracee)
94 struct ptrace_relation *relation, *added;
96 added = kmalloc(sizeof(*added), GFP_KERNEL);
100 added->tracee = tracee;
101 added->tracer = tracer;
102 added->invalid = false;
104 spin_lock(&ptracer_relations_lock);
106 list_for_each_entry_rcu(relation, &ptracer_relations, node) {
107 if (relation->invalid)
109 if (relation->tracee == tracee) {
110 list_replace_rcu(&relation->node, &added->node);
111 kfree_rcu(relation, rcu);
116 list_add_rcu(&added->node, &ptracer_relations);
120 spin_unlock(&ptracer_relations_lock);
125 * yama_ptracer_del - remove exceptions related to the given tasks
126 * @tracer: remove any relation where tracer task matches
127 * @tracee: remove any relation where tracee task matches
129 static void yama_ptracer_del(struct task_struct *tracer,
130 struct task_struct *tracee)
132 struct ptrace_relation *relation;
136 list_for_each_entry_rcu(relation, &ptracer_relations, node) {
137 if (relation->invalid)
139 if (relation->tracee == tracee ||
140 (tracer && relation->tracer == tracer)) {
141 relation->invalid = true;
148 schedule_work(&yama_relation_work);
152 * yama_task_free - check for task_pid to remove from exception list
153 * @task: task being removed
155 void yama_task_free(struct task_struct *task)
157 yama_ptracer_del(task, task);
161 * yama_task_prctl - check for Yama-specific prctl operations
168 * Return 0 on success, -ve on error. -ENOSYS is returned when Yama
169 * does not handle the given option.
171 int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
172 unsigned long arg4, unsigned long arg5)
175 struct task_struct *myself = current;
179 /* Since a thread can call prctl(), find the group leader
180 * before calling _add() or _del() on it, since we want
181 * process-level granularity of control. The tracer group
182 * leader checking is handled later when walking the ancestry
183 * at the time of PTRACE_ATTACH check.
186 if (!thread_group_leader(myself))
187 myself = rcu_dereference(myself->group_leader);
188 get_task_struct(myself);
192 yama_ptracer_del(NULL, myself);
194 } else if (arg2 == PR_SET_PTRACER_ANY || (int)arg2 == -1) {
195 rc = yama_ptracer_add(NULL, myself);
197 struct task_struct *tracer;
200 tracer = find_task_by_vpid(arg2);
202 get_task_struct(tracer);
208 rc = yama_ptracer_add(tracer, myself);
209 put_task_struct(tracer);
213 put_task_struct(myself);
221 * task_is_descendant - walk up a process family tree looking for a match
222 * @parent: the process to compare against while walking up from child
223 * @child: the process to start from while looking upwards for parent
225 * Returns 1 if child is a descendant of parent, 0 if not.
227 static int task_is_descendant(struct task_struct *parent,
228 struct task_struct *child)
231 struct task_struct *walker = child;
233 if (!parent || !child)
237 if (!thread_group_leader(parent))
238 parent = rcu_dereference(parent->group_leader);
239 while (walker->pid > 0) {
240 if (!thread_group_leader(walker))
241 walker = rcu_dereference(walker->group_leader);
242 if (walker == parent) {
246 walker = rcu_dereference(walker->real_parent);
254 * ptracer_exception_found - tracer registered as exception for this tracee
255 * @tracer: the task_struct of the process attempting ptrace
256 * @tracee: the task_struct of the process to be ptraced
258 * Returns 1 if tracer has is ptracer exception ancestor for tracee.
260 static int ptracer_exception_found(struct task_struct *tracer,
261 struct task_struct *tracee)
264 struct ptrace_relation *relation;
265 struct task_struct *parent = NULL;
269 if (!thread_group_leader(tracee))
270 tracee = rcu_dereference(tracee->group_leader);
271 list_for_each_entry_rcu(relation, &ptracer_relations, node) {
272 if (relation->invalid)
274 if (relation->tracee == tracee) {
275 parent = relation->tracer;
281 if (found && (parent == NULL || task_is_descendant(parent, tracer)))
289 * yama_ptrace_access_check - validate PTRACE_ATTACH calls
290 * @child: task that current task is attempting to ptrace
291 * @mode: ptrace attach mode
293 * Returns 0 if following the ptrace is allowed, -ve on error.
295 static int yama_ptrace_access_check(struct task_struct *child,
300 /* require ptrace target be a child of ptracer on attach */
301 if (mode & PTRACE_MODE_ATTACH) {
302 switch (ptrace_scope) {
303 case YAMA_SCOPE_DISABLED:
304 /* No additional restrictions. */
306 case YAMA_SCOPE_RELATIONAL:
308 if (!task_is_descendant(current, child) &&
309 !ptracer_exception_found(current, child) &&
310 !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
314 case YAMA_SCOPE_CAPABILITY:
316 if (!ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
320 case YAMA_SCOPE_NO_ATTACH:
327 if (rc && (mode & PTRACE_MODE_NOAUDIT) == 0)
328 report_access("attach", child, current);
334 * yama_ptrace_traceme - validate PTRACE_TRACEME calls
335 * @parent: task that will become the ptracer of the current task
337 * Returns 0 if following the ptrace is allowed, -ve on error.
339 int yama_ptrace_traceme(struct task_struct *parent)
343 /* Only disallow PTRACE_TRACEME on more aggressive settings. */
344 switch (ptrace_scope) {
345 case YAMA_SCOPE_CAPABILITY:
346 if (!has_ns_capability(parent, current_user_ns(), CAP_SYS_PTRACE))
349 case YAMA_SCOPE_NO_ATTACH:
355 report_access("traceme", current, parent);
360 static struct security_hook_list yama_hooks[] = {
361 LSM_HOOK_INIT(ptrace_access_check, yama_ptrace_access_check),
362 LSM_HOOK_INIT(ptrace_traceme, yama_ptrace_traceme),
363 LSM_HOOK_INIT(task_prctl, yama_task_prctl),
364 LSM_HOOK_INIT(task_free, yama_task_free),
368 static int yama_dointvec_minmax(struct ctl_table *table, int write,
369 void __user *buffer, size_t *lenp, loff_t *ppos)
371 struct ctl_table table_copy;
373 if (write && !capable(CAP_SYS_PTRACE))
376 /* Lock the max value if it ever gets set. */
378 if (*(int *)table_copy.data == *(int *)table_copy.extra2)
379 table_copy.extra1 = table_copy.extra2;
381 return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos);
385 static int max_scope = YAMA_SCOPE_NO_ATTACH;
387 struct ctl_path yama_sysctl_path[] = {
388 { .procname = "kernel", },
389 { .procname = "yama", },
393 static struct ctl_table yama_sysctl_table[] = {
395 .procname = "ptrace_scope",
396 .data = &ptrace_scope,
397 .maxlen = sizeof(int),
399 .proc_handler = yama_dointvec_minmax,
401 .extra2 = &max_scope,
405 static void __init yama_init_sysctl(void)
407 if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
408 panic("Yama: sysctl registration failed.\n");
411 static inline void yama_init_sysctl(void) { }
412 #endif /* CONFIG_SYSCTL */
414 void __init yama_add_hooks(void)
416 pr_info("Yama: becoming mindful.\n");
417 security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks));