blob: cb6ed10816d49ac963a65816a92ca738a7fb42d0 [file] [log] [blame]
Kees Cook2d514482011-12-21 12:17:04 -08001/*
2 * Yama Linux Security Module
3 *
4 * Author: Kees Cook <keescook@chromium.org>
5 *
6 * Copyright (C) 2010 Canonical, Ltd.
7 * Copyright (C) 2011 The Chromium OS Authors.
8 *
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.
12 *
13 */
14
Casey Schaufler3c4ed7b2015-05-02 15:10:46 -070015#include <linux/lsm_hooks.h>
Kees Cook2d514482011-12-21 12:17:04 -080016#include <linux/sysctl.h>
17#include <linux/ptrace.h>
18#include <linux/prctl.h>
19#include <linux/ratelimit.h>
Kees Cook235e7522012-11-19 15:21:26 -080020#include <linux/workqueue.h>
Kees Cook2d514482011-12-21 12:17:04 -080021
Kees Cook389da252012-04-16 11:56:45 -070022#define YAMA_SCOPE_DISABLED 0
23#define YAMA_SCOPE_RELATIONAL 1
24#define YAMA_SCOPE_CAPABILITY 2
25#define YAMA_SCOPE_NO_ATTACH 3
26
27static int ptrace_scope = YAMA_SCOPE_RELATIONAL;
Kees Cook2d514482011-12-21 12:17:04 -080028
29/* describe a ptrace relationship for potential exception */
30struct ptrace_relation {
31 struct task_struct *tracer;
32 struct task_struct *tracee;
Kees Cook235e7522012-11-19 15:21:26 -080033 bool invalid;
Kees Cook2d514482011-12-21 12:17:04 -080034 struct list_head node;
Kees Cook93b69d42012-10-18 14:53:58 -070035 struct rcu_head rcu;
Kees Cook2d514482011-12-21 12:17:04 -080036};
37
38static LIST_HEAD(ptracer_relations);
39static DEFINE_SPINLOCK(ptracer_relations_lock);
40
Kees Cook235e7522012-11-19 15:21:26 -080041static void yama_relation_cleanup(struct work_struct *work);
42static DECLARE_WORK(yama_relation_work, yama_relation_cleanup);
43
44/**
45 * yama_relation_cleanup - remove invalid entries from the relation list
46 *
47 */
48static void yama_relation_cleanup(struct work_struct *work)
49{
50 struct ptrace_relation *relation;
51
52 spin_lock(&ptracer_relations_lock);
53 rcu_read_lock();
54 list_for_each_entry_rcu(relation, &ptracer_relations, node) {
55 if (relation->invalid) {
56 list_del_rcu(&relation->node);
57 kfree_rcu(relation, rcu);
58 }
59 }
60 rcu_read_unlock();
61 spin_unlock(&ptracer_relations_lock);
62}
63
Kees Cook2d514482011-12-21 12:17:04 -080064/**
65 * yama_ptracer_add - add/replace an exception for this tracer/tracee pair
66 * @tracer: the task_struct of the process doing the ptrace
67 * @tracee: the task_struct of the process to be ptraced
68 *
69 * Each tracee can have, at most, one tracer registered. Each time this
70 * is called, the prior registered tracer will be replaced for the tracee.
71 *
72 * Returns 0 if relationship was added, -ve on error.
73 */
74static int yama_ptracer_add(struct task_struct *tracer,
75 struct task_struct *tracee)
76{
Kees Cook93b69d42012-10-18 14:53:58 -070077 struct ptrace_relation *relation, *added;
Kees Cook2d514482011-12-21 12:17:04 -080078
79 added = kmalloc(sizeof(*added), GFP_KERNEL);
80 if (!added)
81 return -ENOMEM;
82
Kees Cook93b69d42012-10-18 14:53:58 -070083 added->tracee = tracee;
84 added->tracer = tracer;
Kees Cook235e7522012-11-19 15:21:26 -080085 added->invalid = false;
Kees Cook93b69d42012-10-18 14:53:58 -070086
Kees Cook235e7522012-11-19 15:21:26 -080087 spin_lock(&ptracer_relations_lock);
Kees Cook93b69d42012-10-18 14:53:58 -070088 rcu_read_lock();
89 list_for_each_entry_rcu(relation, &ptracer_relations, node) {
Kees Cook235e7522012-11-19 15:21:26 -080090 if (relation->invalid)
91 continue;
Kees Cook93b69d42012-10-18 14:53:58 -070092 if (relation->tracee == tracee) {
93 list_replace_rcu(&relation->node, &added->node);
94 kfree_rcu(relation, rcu);
95 goto out;
Kees Cook2d514482011-12-21 12:17:04 -080096 }
Kees Cook2d514482011-12-21 12:17:04 -080097 }
Kees Cook2d514482011-12-21 12:17:04 -080098
Kees Cook93b69d42012-10-18 14:53:58 -070099 list_add_rcu(&added->node, &ptracer_relations);
100
101out:
102 rcu_read_unlock();
Kees Cook235e7522012-11-19 15:21:26 -0800103 spin_unlock(&ptracer_relations_lock);
Kees Cook93b69d42012-10-18 14:53:58 -0700104 return 0;
Kees Cook2d514482011-12-21 12:17:04 -0800105}
106
107/**
108 * yama_ptracer_del - remove exceptions related to the given tasks
109 * @tracer: remove any relation where tracer task matches
110 * @tracee: remove any relation where tracee task matches
111 */
112static void yama_ptracer_del(struct task_struct *tracer,
113 struct task_struct *tracee)
114{
Kees Cook93b69d42012-10-18 14:53:58 -0700115 struct ptrace_relation *relation;
Kees Cook235e7522012-11-19 15:21:26 -0800116 bool marked = false;
Kees Cook2d514482011-12-21 12:17:04 -0800117
Kees Cook93b69d42012-10-18 14:53:58 -0700118 rcu_read_lock();
119 list_for_each_entry_rcu(relation, &ptracer_relations, node) {
Kees Cook235e7522012-11-19 15:21:26 -0800120 if (relation->invalid)
121 continue;
Kees Cook2d514482011-12-21 12:17:04 -0800122 if (relation->tracee == tracee ||
Kees Cookbf061892012-02-14 16:48:09 -0800123 (tracer && relation->tracer == tracer)) {
Kees Cook235e7522012-11-19 15:21:26 -0800124 relation->invalid = true;
125 marked = true;
Kees Cook2d514482011-12-21 12:17:04 -0800126 }
Kees Cook93b69d42012-10-18 14:53:58 -0700127 }
128 rcu_read_unlock();
Kees Cook235e7522012-11-19 15:21:26 -0800129
130 if (marked)
131 schedule_work(&yama_relation_work);
Kees Cook2d514482011-12-21 12:17:04 -0800132}
133
134/**
135 * yama_task_free - check for task_pid to remove from exception list
136 * @task: task being removed
137 */
Kees Cookc6993e42012-09-04 13:32:13 -0700138void yama_task_free(struct task_struct *task)
Kees Cook2d514482011-12-21 12:17:04 -0800139{
140 yama_ptracer_del(task, task);
141}
142
143/**
144 * yama_task_prctl - check for Yama-specific prctl operations
145 * @option: operation
146 * @arg2: argument
147 * @arg3: argument
148 * @arg4: argument
149 * @arg5: argument
150 *
151 * Return 0 on success, -ve on error. -ENOSYS is returned when Yama
152 * does not handle the given option.
153 */
Kees Cookc6993e42012-09-04 13:32:13 -0700154int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
Kees Cook2d514482011-12-21 12:17:04 -0800155 unsigned long arg4, unsigned long arg5)
156{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700157 int rc = -ENOSYS;
Kees Cook2d514482011-12-21 12:17:04 -0800158 struct task_struct *myself = current;
159
Kees Cook2d514482011-12-21 12:17:04 -0800160 switch (option) {
161 case PR_SET_PTRACER:
162 /* Since a thread can call prctl(), find the group leader
163 * before calling _add() or _del() on it, since we want
164 * process-level granularity of control. The tracer group
165 * leader checking is handled later when walking the ancestry
166 * at the time of PTRACE_ATTACH check.
167 */
168 rcu_read_lock();
169 if (!thread_group_leader(myself))
170 myself = rcu_dereference(myself->group_leader);
171 get_task_struct(myself);
172 rcu_read_unlock();
173
174 if (arg2 == 0) {
175 yama_ptracer_del(NULL, myself);
176 rc = 0;
Kees Cook2e4930e2012-08-27 11:38:13 -0700177 } else if (arg2 == PR_SET_PTRACER_ANY || (int)arg2 == -1) {
Kees Cookbf061892012-02-14 16:48:09 -0800178 rc = yama_ptracer_add(NULL, myself);
Kees Cook2d514482011-12-21 12:17:04 -0800179 } else {
180 struct task_struct *tracer;
181
182 rcu_read_lock();
183 tracer = find_task_by_vpid(arg2);
184 if (tracer)
185 get_task_struct(tracer);
186 else
187 rc = -EINVAL;
188 rcu_read_unlock();
189
190 if (tracer) {
191 rc = yama_ptracer_add(tracer, myself);
192 put_task_struct(tracer);
193 }
194 }
195
196 put_task_struct(myself);
197 break;
198 }
199
200 return rc;
201}
202
203/**
204 * task_is_descendant - walk up a process family tree looking for a match
205 * @parent: the process to compare against while walking up from child
206 * @child: the process to start from while looking upwards for parent
207 *
208 * Returns 1 if child is a descendant of parent, 0 if not.
209 */
210static int task_is_descendant(struct task_struct *parent,
211 struct task_struct *child)
212{
213 int rc = 0;
214 struct task_struct *walker = child;
215
216 if (!parent || !child)
217 return 0;
218
219 rcu_read_lock();
220 if (!thread_group_leader(parent))
221 parent = rcu_dereference(parent->group_leader);
222 while (walker->pid > 0) {
223 if (!thread_group_leader(walker))
224 walker = rcu_dereference(walker->group_leader);
225 if (walker == parent) {
226 rc = 1;
227 break;
228 }
229 walker = rcu_dereference(walker->real_parent);
230 }
231 rcu_read_unlock();
232
233 return rc;
234}
235
236/**
237 * ptracer_exception_found - tracer registered as exception for this tracee
238 * @tracer: the task_struct of the process attempting ptrace
239 * @tracee: the task_struct of the process to be ptraced
240 *
241 * Returns 1 if tracer has is ptracer exception ancestor for tracee.
242 */
243static int ptracer_exception_found(struct task_struct *tracer,
244 struct task_struct *tracee)
245{
246 int rc = 0;
247 struct ptrace_relation *relation;
248 struct task_struct *parent = NULL;
Kees Cookbf061892012-02-14 16:48:09 -0800249 bool found = false;
Kees Cook2d514482011-12-21 12:17:04 -0800250
Kees Cook2d514482011-12-21 12:17:04 -0800251 rcu_read_lock();
252 if (!thread_group_leader(tracee))
253 tracee = rcu_dereference(tracee->group_leader);
Kees Cook235e7522012-11-19 15:21:26 -0800254 list_for_each_entry_rcu(relation, &ptracer_relations, node) {
255 if (relation->invalid)
256 continue;
Kees Cook2d514482011-12-21 12:17:04 -0800257 if (relation->tracee == tracee) {
258 parent = relation->tracer;
Kees Cookbf061892012-02-14 16:48:09 -0800259 found = true;
Kees Cook2d514482011-12-21 12:17:04 -0800260 break;
261 }
Kees Cook235e7522012-11-19 15:21:26 -0800262 }
Kees Cook2d514482011-12-21 12:17:04 -0800263
Kees Cookbf061892012-02-14 16:48:09 -0800264 if (found && (parent == NULL || task_is_descendant(parent, tracer)))
Kees Cook2d514482011-12-21 12:17:04 -0800265 rc = 1;
266 rcu_read_unlock();
Kees Cook2d514482011-12-21 12:17:04 -0800267
268 return rc;
269}
270
271/**
272 * yama_ptrace_access_check - validate PTRACE_ATTACH calls
273 * @child: task that current task is attempting to ptrace
274 * @mode: ptrace attach mode
275 *
276 * Returns 0 if following the ptrace is allowed, -ve on error.
277 */
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700278static int yama_ptrace_access_check(struct task_struct *child,
Kees Cook2d514482011-12-21 12:17:04 -0800279 unsigned int mode)
280{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700281 int rc = 0;
Kees Cook2d514482011-12-21 12:17:04 -0800282
283 /* require ptrace target be a child of ptracer on attach */
Jann Horn3dfb7d82016-01-20 15:00:01 -0800284 if (mode & PTRACE_MODE_ATTACH) {
Kees Cook389da252012-04-16 11:56:45 -0700285 switch (ptrace_scope) {
286 case YAMA_SCOPE_DISABLED:
287 /* No additional restrictions. */
288 break;
289 case YAMA_SCOPE_RELATIONAL:
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -0700290 rcu_read_lock();
Kees Cook389da252012-04-16 11:56:45 -0700291 if (!task_is_descendant(current, child) &&
292 !ptracer_exception_found(current, child) &&
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -0700293 !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
Kees Cook389da252012-04-16 11:56:45 -0700294 rc = -EPERM;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -0700295 rcu_read_unlock();
Kees Cook389da252012-04-16 11:56:45 -0700296 break;
297 case YAMA_SCOPE_CAPABILITY:
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -0700298 rcu_read_lock();
299 if (!ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
Kees Cook389da252012-04-16 11:56:45 -0700300 rc = -EPERM;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -0700301 rcu_read_unlock();
Kees Cook389da252012-04-16 11:56:45 -0700302 break;
303 case YAMA_SCOPE_NO_ATTACH:
304 default:
305 rc = -EPERM;
306 break;
307 }
308 }
Kees Cook2d514482011-12-21 12:17:04 -0800309
Jann Horn3dfb7d82016-01-20 15:00:01 -0800310 if (rc && (mode & PTRACE_MODE_NOAUDIT) == 0) {
Kees Cook389da252012-04-16 11:56:45 -0700311 printk_ratelimited(KERN_NOTICE
312 "ptrace of pid %d was attempted by: %s (pid %d)\n",
Kees Cook7612bfe2012-08-15 11:41:55 -0700313 child->pid, current->comm, current->pid);
Kees Cook2d514482011-12-21 12:17:04 -0800314 }
315
316 return rc;
317}
318
Kees Cook9d8dad72012-08-09 19:01:26 -0700319/**
320 * yama_ptrace_traceme - validate PTRACE_TRACEME calls
321 * @parent: task that will become the ptracer of the current task
322 *
323 * Returns 0 if following the ptrace is allowed, -ve on error.
324 */
Kees Cookc6993e42012-09-04 13:32:13 -0700325int yama_ptrace_traceme(struct task_struct *parent)
Kees Cook9d8dad72012-08-09 19:01:26 -0700326{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700327 int rc = 0;
Kees Cook9d8dad72012-08-09 19:01:26 -0700328
329 /* Only disallow PTRACE_TRACEME on more aggressive settings. */
330 switch (ptrace_scope) {
331 case YAMA_SCOPE_CAPABILITY:
Eric W. Biedermaneddc0a32013-03-21 02:30:41 -0700332 if (!has_ns_capability(parent, current_user_ns(), CAP_SYS_PTRACE))
Kees Cook9d8dad72012-08-09 19:01:26 -0700333 rc = -EPERM;
334 break;
335 case YAMA_SCOPE_NO_ATTACH:
336 rc = -EPERM;
337 break;
338 }
339
340 if (rc) {
Kees Cook9d8dad72012-08-09 19:01:26 -0700341 printk_ratelimited(KERN_NOTICE
342 "ptraceme of pid %d was attempted by: %s (pid %d)\n",
Kees Cook7612bfe2012-08-15 11:41:55 -0700343 current->pid, parent->comm, parent->pid);
Kees Cook9d8dad72012-08-09 19:01:26 -0700344 }
345
346 return rc;
347}
348
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700349static struct security_hook_list yama_hooks[] = {
Casey Schauflere20b0432015-05-02 15:11:36 -0700350 LSM_HOOK_INIT(ptrace_access_check, yama_ptrace_access_check),
351 LSM_HOOK_INIT(ptrace_traceme, yama_ptrace_traceme),
352 LSM_HOOK_INIT(task_prctl, yama_task_prctl),
353 LSM_HOOK_INIT(task_free, yama_task_free),
Kees Cook2d514482011-12-21 12:17:04 -0800354};
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700355
Kees Cook2d514482011-12-21 12:17:04 -0800356#ifdef CONFIG_SYSCTL
Kees Cook389da252012-04-16 11:56:45 -0700357static int yama_dointvec_minmax(struct ctl_table *table, int write,
358 void __user *buffer, size_t *lenp, loff_t *ppos)
359{
Kees Cook41a4695c2013-02-27 08:37:56 -0800360 struct ctl_table table_copy;
Kees Cook389da252012-04-16 11:56:45 -0700361
362 if (write && !capable(CAP_SYS_PTRACE))
363 return -EPERM;
364
Kees Cook389da252012-04-16 11:56:45 -0700365 /* Lock the max value if it ever gets set. */
Kees Cook41a4695c2013-02-27 08:37:56 -0800366 table_copy = *table;
367 if (*(int *)table_copy.data == *(int *)table_copy.extra2)
368 table_copy.extra1 = table_copy.extra2;
Kees Cook389da252012-04-16 11:56:45 -0700369
Kees Cook41a4695c2013-02-27 08:37:56 -0800370 return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos);
Kees Cook389da252012-04-16 11:56:45 -0700371}
372
Kees Cook2d514482011-12-21 12:17:04 -0800373static int zero;
Kees Cook389da252012-04-16 11:56:45 -0700374static int max_scope = YAMA_SCOPE_NO_ATTACH;
Kees Cook2d514482011-12-21 12:17:04 -0800375
376struct ctl_path yama_sysctl_path[] = {
377 { .procname = "kernel", },
378 { .procname = "yama", },
379 { }
380};
381
382static struct ctl_table yama_sysctl_table[] = {
383 {
384 .procname = "ptrace_scope",
385 .data = &ptrace_scope,
386 .maxlen = sizeof(int),
387 .mode = 0644,
Kees Cook389da252012-04-16 11:56:45 -0700388 .proc_handler = yama_dointvec_minmax,
Kees Cook2d514482011-12-21 12:17:04 -0800389 .extra1 = &zero,
Kees Cook389da252012-04-16 11:56:45 -0700390 .extra2 = &max_scope,
Kees Cook2d514482011-12-21 12:17:04 -0800391 },
392 { }
393};
Kees Cook730daa12015-07-23 18:02:48 -0700394static void __init yama_init_sysctl(void)
Kees Cook2d514482011-12-21 12:17:04 -0800395{
Kees Cook2d514482011-12-21 12:17:04 -0800396 if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
397 panic("Yama: sysctl registration failed.\n");
Kees Cook2d514482011-12-21 12:17:04 -0800398}
Kees Cook730daa12015-07-23 18:02:48 -0700399#else
400static inline void yama_init_sysctl(void) { }
401#endif /* CONFIG_SYSCTL */
Kees Cook2d514482011-12-21 12:17:04 -0800402
Kees Cook730daa12015-07-23 18:02:48 -0700403void __init yama_add_hooks(void)
404{
405 pr_info("Yama: becoming mindful.\n");
406 security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks));
407 yama_init_sysctl();
408}