1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Yama Linux Security Module
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Author: Kees Cook <keescook@chromium.org>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright (C) 2010 Canonical, Ltd.
8*4882a593Smuzhiyun * Copyright (C) 2011 The Chromium OS Authors.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/lsm_hooks.h>
12*4882a593Smuzhiyun #include <linux/sysctl.h>
13*4882a593Smuzhiyun #include <linux/ptrace.h>
14*4882a593Smuzhiyun #include <linux/prctl.h>
15*4882a593Smuzhiyun #include <linux/ratelimit.h>
16*4882a593Smuzhiyun #include <linux/workqueue.h>
17*4882a593Smuzhiyun #include <linux/string_helpers.h>
18*4882a593Smuzhiyun #include <linux/task_work.h>
19*4882a593Smuzhiyun #include <linux/sched.h>
20*4882a593Smuzhiyun #include <linux/spinlock.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #define YAMA_SCOPE_DISABLED 0
23*4882a593Smuzhiyun #define YAMA_SCOPE_RELATIONAL 1
24*4882a593Smuzhiyun #define YAMA_SCOPE_CAPABILITY 2
25*4882a593Smuzhiyun #define YAMA_SCOPE_NO_ATTACH 3
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun static int ptrace_scope = YAMA_SCOPE_RELATIONAL;
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /* describe a ptrace relationship for potential exception */
30*4882a593Smuzhiyun struct ptrace_relation {
31*4882a593Smuzhiyun struct task_struct *tracer;
32*4882a593Smuzhiyun struct task_struct *tracee;
33*4882a593Smuzhiyun bool invalid;
34*4882a593Smuzhiyun struct list_head node;
35*4882a593Smuzhiyun struct rcu_head rcu;
36*4882a593Smuzhiyun };
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun static LIST_HEAD(ptracer_relations);
39*4882a593Smuzhiyun static DEFINE_SPINLOCK(ptracer_relations_lock);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun static void yama_relation_cleanup(struct work_struct *work);
42*4882a593Smuzhiyun static DECLARE_WORK(yama_relation_work, yama_relation_cleanup);
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun struct access_report_info {
45*4882a593Smuzhiyun struct callback_head work;
46*4882a593Smuzhiyun const char *access;
47*4882a593Smuzhiyun struct task_struct *target;
48*4882a593Smuzhiyun struct task_struct *agent;
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun
__report_access(struct callback_head * work)51*4882a593Smuzhiyun static void __report_access(struct callback_head *work)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun struct access_report_info *info =
54*4882a593Smuzhiyun container_of(work, struct access_report_info, work);
55*4882a593Smuzhiyun char *target_cmd, *agent_cmd;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun target_cmd = kstrdup_quotable_cmdline(info->target, GFP_KERNEL);
58*4882a593Smuzhiyun agent_cmd = kstrdup_quotable_cmdline(info->agent, GFP_KERNEL);
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun pr_notice_ratelimited(
61*4882a593Smuzhiyun "ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n",
62*4882a593Smuzhiyun info->access, target_cmd, info->target->pid, agent_cmd,
63*4882a593Smuzhiyun info->agent->pid);
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun kfree(agent_cmd);
66*4882a593Smuzhiyun kfree(target_cmd);
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun put_task_struct(info->agent);
69*4882a593Smuzhiyun put_task_struct(info->target);
70*4882a593Smuzhiyun kfree(info);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* defers execution because cmdline access can sleep */
report_access(const char * access,struct task_struct * target,struct task_struct * agent)74*4882a593Smuzhiyun static void report_access(const char *access, struct task_struct *target,
75*4882a593Smuzhiyun struct task_struct *agent)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun struct access_report_info *info;
78*4882a593Smuzhiyun char agent_comm[sizeof(agent->comm)];
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun assert_spin_locked(&target->alloc_lock); /* for target->comm */
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun if (current->flags & PF_KTHREAD) {
83*4882a593Smuzhiyun /* I don't think kthreads call task_work_run() before exiting.
84*4882a593Smuzhiyun * Imagine angry ranting about procfs here.
85*4882a593Smuzhiyun */
86*4882a593Smuzhiyun pr_notice_ratelimited(
87*4882a593Smuzhiyun "ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n",
88*4882a593Smuzhiyun access, target->comm, target->pid,
89*4882a593Smuzhiyun get_task_comm(agent_comm, agent), agent->pid);
90*4882a593Smuzhiyun return;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun info = kmalloc(sizeof(*info), GFP_ATOMIC);
94*4882a593Smuzhiyun if (!info)
95*4882a593Smuzhiyun return;
96*4882a593Smuzhiyun init_task_work(&info->work, __report_access);
97*4882a593Smuzhiyun get_task_struct(target);
98*4882a593Smuzhiyun get_task_struct(agent);
99*4882a593Smuzhiyun info->access = access;
100*4882a593Smuzhiyun info->target = target;
101*4882a593Smuzhiyun info->agent = agent;
102*4882a593Smuzhiyun if (task_work_add(current, &info->work, TWA_RESUME) == 0)
103*4882a593Smuzhiyun return; /* success */
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun WARN(1, "report_access called from exiting task");
106*4882a593Smuzhiyun put_task_struct(target);
107*4882a593Smuzhiyun put_task_struct(agent);
108*4882a593Smuzhiyun kfree(info);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /**
112*4882a593Smuzhiyun * yama_relation_cleanup - remove invalid entries from the relation list
113*4882a593Smuzhiyun *
114*4882a593Smuzhiyun */
yama_relation_cleanup(struct work_struct * work)115*4882a593Smuzhiyun static void yama_relation_cleanup(struct work_struct *work)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun struct ptrace_relation *relation;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun spin_lock(&ptracer_relations_lock);
120*4882a593Smuzhiyun rcu_read_lock();
121*4882a593Smuzhiyun list_for_each_entry_rcu(relation, &ptracer_relations, node) {
122*4882a593Smuzhiyun if (relation->invalid) {
123*4882a593Smuzhiyun list_del_rcu(&relation->node);
124*4882a593Smuzhiyun kfree_rcu(relation, rcu);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun rcu_read_unlock();
128*4882a593Smuzhiyun spin_unlock(&ptracer_relations_lock);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /**
132*4882a593Smuzhiyun * yama_ptracer_add - add/replace an exception for this tracer/tracee pair
133*4882a593Smuzhiyun * @tracer: the task_struct of the process doing the ptrace
134*4882a593Smuzhiyun * @tracee: the task_struct of the process to be ptraced
135*4882a593Smuzhiyun *
136*4882a593Smuzhiyun * Each tracee can have, at most, one tracer registered. Each time this
137*4882a593Smuzhiyun * is called, the prior registered tracer will be replaced for the tracee.
138*4882a593Smuzhiyun *
139*4882a593Smuzhiyun * Returns 0 if relationship was added, -ve on error.
140*4882a593Smuzhiyun */
yama_ptracer_add(struct task_struct * tracer,struct task_struct * tracee)141*4882a593Smuzhiyun static int yama_ptracer_add(struct task_struct *tracer,
142*4882a593Smuzhiyun struct task_struct *tracee)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun struct ptrace_relation *relation, *added;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun added = kmalloc(sizeof(*added), GFP_KERNEL);
147*4882a593Smuzhiyun if (!added)
148*4882a593Smuzhiyun return -ENOMEM;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun added->tracee = tracee;
151*4882a593Smuzhiyun added->tracer = tracer;
152*4882a593Smuzhiyun added->invalid = false;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun spin_lock(&ptracer_relations_lock);
155*4882a593Smuzhiyun rcu_read_lock();
156*4882a593Smuzhiyun list_for_each_entry_rcu(relation, &ptracer_relations, node) {
157*4882a593Smuzhiyun if (relation->invalid)
158*4882a593Smuzhiyun continue;
159*4882a593Smuzhiyun if (relation->tracee == tracee) {
160*4882a593Smuzhiyun list_replace_rcu(&relation->node, &added->node);
161*4882a593Smuzhiyun kfree_rcu(relation, rcu);
162*4882a593Smuzhiyun goto out;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun list_add_rcu(&added->node, &ptracer_relations);
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun out:
169*4882a593Smuzhiyun rcu_read_unlock();
170*4882a593Smuzhiyun spin_unlock(&ptracer_relations_lock);
171*4882a593Smuzhiyun return 0;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /**
175*4882a593Smuzhiyun * yama_ptracer_del - remove exceptions related to the given tasks
176*4882a593Smuzhiyun * @tracer: remove any relation where tracer task matches
177*4882a593Smuzhiyun * @tracee: remove any relation where tracee task matches
178*4882a593Smuzhiyun */
yama_ptracer_del(struct task_struct * tracer,struct task_struct * tracee)179*4882a593Smuzhiyun static void yama_ptracer_del(struct task_struct *tracer,
180*4882a593Smuzhiyun struct task_struct *tracee)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun struct ptrace_relation *relation;
183*4882a593Smuzhiyun bool marked = false;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun rcu_read_lock();
186*4882a593Smuzhiyun list_for_each_entry_rcu(relation, &ptracer_relations, node) {
187*4882a593Smuzhiyun if (relation->invalid)
188*4882a593Smuzhiyun continue;
189*4882a593Smuzhiyun if (relation->tracee == tracee ||
190*4882a593Smuzhiyun (tracer && relation->tracer == tracer)) {
191*4882a593Smuzhiyun relation->invalid = true;
192*4882a593Smuzhiyun marked = true;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun rcu_read_unlock();
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun if (marked)
198*4882a593Smuzhiyun schedule_work(&yama_relation_work);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun /**
202*4882a593Smuzhiyun * yama_task_free - check for task_pid to remove from exception list
203*4882a593Smuzhiyun * @task: task being removed
204*4882a593Smuzhiyun */
yama_task_free(struct task_struct * task)205*4882a593Smuzhiyun static void yama_task_free(struct task_struct *task)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun yama_ptracer_del(task, task);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun /**
211*4882a593Smuzhiyun * yama_task_prctl - check for Yama-specific prctl operations
212*4882a593Smuzhiyun * @option: operation
213*4882a593Smuzhiyun * @arg2: argument
214*4882a593Smuzhiyun * @arg3: argument
215*4882a593Smuzhiyun * @arg4: argument
216*4882a593Smuzhiyun * @arg5: argument
217*4882a593Smuzhiyun *
218*4882a593Smuzhiyun * Return 0 on success, -ve on error. -ENOSYS is returned when Yama
219*4882a593Smuzhiyun * does not handle the given option.
220*4882a593Smuzhiyun */
yama_task_prctl(int option,unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5)221*4882a593Smuzhiyun static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
222*4882a593Smuzhiyun unsigned long arg4, unsigned long arg5)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun int rc = -ENOSYS;
225*4882a593Smuzhiyun struct task_struct *myself = current;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun switch (option) {
228*4882a593Smuzhiyun case PR_SET_PTRACER:
229*4882a593Smuzhiyun /* Since a thread can call prctl(), find the group leader
230*4882a593Smuzhiyun * before calling _add() or _del() on it, since we want
231*4882a593Smuzhiyun * process-level granularity of control. The tracer group
232*4882a593Smuzhiyun * leader checking is handled later when walking the ancestry
233*4882a593Smuzhiyun * at the time of PTRACE_ATTACH check.
234*4882a593Smuzhiyun */
235*4882a593Smuzhiyun rcu_read_lock();
236*4882a593Smuzhiyun if (!thread_group_leader(myself))
237*4882a593Smuzhiyun myself = rcu_dereference(myself->group_leader);
238*4882a593Smuzhiyun get_task_struct(myself);
239*4882a593Smuzhiyun rcu_read_unlock();
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun if (arg2 == 0) {
242*4882a593Smuzhiyun yama_ptracer_del(NULL, myself);
243*4882a593Smuzhiyun rc = 0;
244*4882a593Smuzhiyun } else if (arg2 == PR_SET_PTRACER_ANY || (int)arg2 == -1) {
245*4882a593Smuzhiyun rc = yama_ptracer_add(NULL, myself);
246*4882a593Smuzhiyun } else {
247*4882a593Smuzhiyun struct task_struct *tracer;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun tracer = find_get_task_by_vpid(arg2);
250*4882a593Smuzhiyun if (!tracer) {
251*4882a593Smuzhiyun rc = -EINVAL;
252*4882a593Smuzhiyun } else {
253*4882a593Smuzhiyun rc = yama_ptracer_add(tracer, myself);
254*4882a593Smuzhiyun put_task_struct(tracer);
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun put_task_struct(myself);
259*4882a593Smuzhiyun break;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun return rc;
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun /**
266*4882a593Smuzhiyun * task_is_descendant - walk up a process family tree looking for a match
267*4882a593Smuzhiyun * @parent: the process to compare against while walking up from child
268*4882a593Smuzhiyun * @child: the process to start from while looking upwards for parent
269*4882a593Smuzhiyun *
270*4882a593Smuzhiyun * Returns 1 if child is a descendant of parent, 0 if not.
271*4882a593Smuzhiyun */
task_is_descendant(struct task_struct * parent,struct task_struct * child)272*4882a593Smuzhiyun static int task_is_descendant(struct task_struct *parent,
273*4882a593Smuzhiyun struct task_struct *child)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun int rc = 0;
276*4882a593Smuzhiyun struct task_struct *walker = child;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun if (!parent || !child)
279*4882a593Smuzhiyun return 0;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun rcu_read_lock();
282*4882a593Smuzhiyun if (!thread_group_leader(parent))
283*4882a593Smuzhiyun parent = rcu_dereference(parent->group_leader);
284*4882a593Smuzhiyun while (walker->pid > 0) {
285*4882a593Smuzhiyun if (!thread_group_leader(walker))
286*4882a593Smuzhiyun walker = rcu_dereference(walker->group_leader);
287*4882a593Smuzhiyun if (walker == parent) {
288*4882a593Smuzhiyun rc = 1;
289*4882a593Smuzhiyun break;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun walker = rcu_dereference(walker->real_parent);
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun rcu_read_unlock();
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun return rc;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /**
299*4882a593Smuzhiyun * ptracer_exception_found - tracer registered as exception for this tracee
300*4882a593Smuzhiyun * @tracer: the task_struct of the process attempting ptrace
301*4882a593Smuzhiyun * @tracee: the task_struct of the process to be ptraced
302*4882a593Smuzhiyun *
303*4882a593Smuzhiyun * Returns 1 if tracer has a ptracer exception ancestor for tracee.
304*4882a593Smuzhiyun */
ptracer_exception_found(struct task_struct * tracer,struct task_struct * tracee)305*4882a593Smuzhiyun static int ptracer_exception_found(struct task_struct *tracer,
306*4882a593Smuzhiyun struct task_struct *tracee)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun int rc = 0;
309*4882a593Smuzhiyun struct ptrace_relation *relation;
310*4882a593Smuzhiyun struct task_struct *parent = NULL;
311*4882a593Smuzhiyun bool found = false;
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun rcu_read_lock();
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /*
316*4882a593Smuzhiyun * If there's already an active tracing relationship, then make an
317*4882a593Smuzhiyun * exception for the sake of other accesses, like process_vm_rw().
318*4882a593Smuzhiyun */
319*4882a593Smuzhiyun parent = ptrace_parent(tracee);
320*4882a593Smuzhiyun if (parent != NULL && same_thread_group(parent, tracer)) {
321*4882a593Smuzhiyun rc = 1;
322*4882a593Smuzhiyun goto unlock;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun /* Look for a PR_SET_PTRACER relationship. */
326*4882a593Smuzhiyun if (!thread_group_leader(tracee))
327*4882a593Smuzhiyun tracee = rcu_dereference(tracee->group_leader);
328*4882a593Smuzhiyun list_for_each_entry_rcu(relation, &ptracer_relations, node) {
329*4882a593Smuzhiyun if (relation->invalid)
330*4882a593Smuzhiyun continue;
331*4882a593Smuzhiyun if (relation->tracee == tracee) {
332*4882a593Smuzhiyun parent = relation->tracer;
333*4882a593Smuzhiyun found = true;
334*4882a593Smuzhiyun break;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun if (found && (parent == NULL || task_is_descendant(parent, tracer)))
339*4882a593Smuzhiyun rc = 1;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun unlock:
342*4882a593Smuzhiyun rcu_read_unlock();
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun return rc;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun /**
348*4882a593Smuzhiyun * yama_ptrace_access_check - validate PTRACE_ATTACH calls
349*4882a593Smuzhiyun * @child: task that current task is attempting to ptrace
350*4882a593Smuzhiyun * @mode: ptrace attach mode
351*4882a593Smuzhiyun *
352*4882a593Smuzhiyun * Returns 0 if following the ptrace is allowed, -ve on error.
353*4882a593Smuzhiyun */
yama_ptrace_access_check(struct task_struct * child,unsigned int mode)354*4882a593Smuzhiyun static int yama_ptrace_access_check(struct task_struct *child,
355*4882a593Smuzhiyun unsigned int mode)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun int rc = 0;
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun /* require ptrace target be a child of ptracer on attach */
360*4882a593Smuzhiyun if (mode & PTRACE_MODE_ATTACH) {
361*4882a593Smuzhiyun switch (ptrace_scope) {
362*4882a593Smuzhiyun case YAMA_SCOPE_DISABLED:
363*4882a593Smuzhiyun /* No additional restrictions. */
364*4882a593Smuzhiyun break;
365*4882a593Smuzhiyun case YAMA_SCOPE_RELATIONAL:
366*4882a593Smuzhiyun rcu_read_lock();
367*4882a593Smuzhiyun if (!pid_alive(child))
368*4882a593Smuzhiyun rc = -EPERM;
369*4882a593Smuzhiyun if (!rc && !task_is_descendant(current, child) &&
370*4882a593Smuzhiyun !ptracer_exception_found(current, child) &&
371*4882a593Smuzhiyun !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
372*4882a593Smuzhiyun rc = -EPERM;
373*4882a593Smuzhiyun rcu_read_unlock();
374*4882a593Smuzhiyun break;
375*4882a593Smuzhiyun case YAMA_SCOPE_CAPABILITY:
376*4882a593Smuzhiyun rcu_read_lock();
377*4882a593Smuzhiyun if (!ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
378*4882a593Smuzhiyun rc = -EPERM;
379*4882a593Smuzhiyun rcu_read_unlock();
380*4882a593Smuzhiyun break;
381*4882a593Smuzhiyun case YAMA_SCOPE_NO_ATTACH:
382*4882a593Smuzhiyun default:
383*4882a593Smuzhiyun rc = -EPERM;
384*4882a593Smuzhiyun break;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun if (rc && (mode & PTRACE_MODE_NOAUDIT) == 0)
389*4882a593Smuzhiyun report_access("attach", child, current);
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun return rc;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun /**
395*4882a593Smuzhiyun * yama_ptrace_traceme - validate PTRACE_TRACEME calls
396*4882a593Smuzhiyun * @parent: task that will become the ptracer of the current task
397*4882a593Smuzhiyun *
398*4882a593Smuzhiyun * Returns 0 if following the ptrace is allowed, -ve on error.
399*4882a593Smuzhiyun */
yama_ptrace_traceme(struct task_struct * parent)400*4882a593Smuzhiyun static int yama_ptrace_traceme(struct task_struct *parent)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun int rc = 0;
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun /* Only disallow PTRACE_TRACEME on more aggressive settings. */
405*4882a593Smuzhiyun switch (ptrace_scope) {
406*4882a593Smuzhiyun case YAMA_SCOPE_CAPABILITY:
407*4882a593Smuzhiyun if (!has_ns_capability(parent, current_user_ns(), CAP_SYS_PTRACE))
408*4882a593Smuzhiyun rc = -EPERM;
409*4882a593Smuzhiyun break;
410*4882a593Smuzhiyun case YAMA_SCOPE_NO_ATTACH:
411*4882a593Smuzhiyun rc = -EPERM;
412*4882a593Smuzhiyun break;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun if (rc) {
416*4882a593Smuzhiyun task_lock(current);
417*4882a593Smuzhiyun report_access("traceme", current, parent);
418*4882a593Smuzhiyun task_unlock(current);
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun return rc;
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun static struct security_hook_list yama_hooks[] __lsm_ro_after_init = {
425*4882a593Smuzhiyun LSM_HOOK_INIT(ptrace_access_check, yama_ptrace_access_check),
426*4882a593Smuzhiyun LSM_HOOK_INIT(ptrace_traceme, yama_ptrace_traceme),
427*4882a593Smuzhiyun LSM_HOOK_INIT(task_prctl, yama_task_prctl),
428*4882a593Smuzhiyun LSM_HOOK_INIT(task_free, yama_task_free),
429*4882a593Smuzhiyun };
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun #ifdef CONFIG_SYSCTL
yama_dointvec_minmax(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)432*4882a593Smuzhiyun static int yama_dointvec_minmax(struct ctl_table *table, int write,
433*4882a593Smuzhiyun void *buffer, size_t *lenp, loff_t *ppos)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun struct ctl_table table_copy;
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun if (write && !capable(CAP_SYS_PTRACE))
438*4882a593Smuzhiyun return -EPERM;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun /* Lock the max value if it ever gets set. */
441*4882a593Smuzhiyun table_copy = *table;
442*4882a593Smuzhiyun if (*(int *)table_copy.data == *(int *)table_copy.extra2)
443*4882a593Smuzhiyun table_copy.extra1 = table_copy.extra2;
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos);
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun static int max_scope = YAMA_SCOPE_NO_ATTACH;
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun static struct ctl_path yama_sysctl_path[] = {
451*4882a593Smuzhiyun { .procname = "kernel", },
452*4882a593Smuzhiyun { .procname = "yama", },
453*4882a593Smuzhiyun { }
454*4882a593Smuzhiyun };
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun static struct ctl_table yama_sysctl_table[] = {
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun .procname = "ptrace_scope",
459*4882a593Smuzhiyun .data = &ptrace_scope,
460*4882a593Smuzhiyun .maxlen = sizeof(int),
461*4882a593Smuzhiyun .mode = 0644,
462*4882a593Smuzhiyun .proc_handler = yama_dointvec_minmax,
463*4882a593Smuzhiyun .extra1 = SYSCTL_ZERO,
464*4882a593Smuzhiyun .extra2 = &max_scope,
465*4882a593Smuzhiyun },
466*4882a593Smuzhiyun { }
467*4882a593Smuzhiyun };
yama_init_sysctl(void)468*4882a593Smuzhiyun static void __init yama_init_sysctl(void)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
471*4882a593Smuzhiyun panic("Yama: sysctl registration failed.\n");
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun #else
yama_init_sysctl(void)474*4882a593Smuzhiyun static inline void yama_init_sysctl(void) { }
475*4882a593Smuzhiyun #endif /* CONFIG_SYSCTL */
476*4882a593Smuzhiyun
yama_init(void)477*4882a593Smuzhiyun static int __init yama_init(void)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun pr_info("Yama: becoming mindful.\n");
480*4882a593Smuzhiyun security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks), "yama");
481*4882a593Smuzhiyun yama_init_sysctl();
482*4882a593Smuzhiyun return 0;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun DEFINE_LSM(yama) = {
486*4882a593Smuzhiyun .name = "yama",
487*4882a593Smuzhiyun .init = yama_init,
488*4882a593Smuzhiyun };
489