xref: /openbmc/linux/security/yama/yama_lsm.c (revision d69dece5f5b6bc7a5e39d2b6136ddc69469331fe)
12d514487SKees Cook /*
22d514487SKees Cook  * Yama Linux Security Module
32d514487SKees Cook  *
42d514487SKees Cook  * Author: Kees Cook <keescook@chromium.org>
52d514487SKees Cook  *
62d514487SKees Cook  * Copyright (C) 2010 Canonical, Ltd.
72d514487SKees Cook  * Copyright (C) 2011 The Chromium OS Authors.
82d514487SKees Cook  *
92d514487SKees Cook  * This program is free software; you can redistribute it and/or modify
102d514487SKees Cook  * it under the terms of the GNU General Public License version 2, as
112d514487SKees Cook  * published by the Free Software Foundation.
122d514487SKees Cook  *
132d514487SKees Cook  */
142d514487SKees Cook 
153c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h>
162d514487SKees Cook #include <linux/sysctl.h>
172d514487SKees Cook #include <linux/ptrace.h>
182d514487SKees Cook #include <linux/prctl.h>
192d514487SKees Cook #include <linux/ratelimit.h>
20235e7527SKees Cook #include <linux/workqueue.h>
218a56038cSKees Cook #include <linux/string_helpers.h>
22dca6b414SJann Horn #include <linux/task_work.h>
23dca6b414SJann Horn #include <linux/sched.h>
24dca6b414SJann Horn #include <linux/spinlock.h>
252d514487SKees Cook 
26389da25fSKees Cook #define YAMA_SCOPE_DISABLED	0
27389da25fSKees Cook #define YAMA_SCOPE_RELATIONAL	1
28389da25fSKees Cook #define YAMA_SCOPE_CAPABILITY	2
29389da25fSKees Cook #define YAMA_SCOPE_NO_ATTACH	3
30389da25fSKees Cook 
31389da25fSKees Cook static int ptrace_scope = YAMA_SCOPE_RELATIONAL;
322d514487SKees Cook 
332d514487SKees Cook /* describe a ptrace relationship for potential exception */
342d514487SKees Cook struct ptrace_relation {
352d514487SKees Cook 	struct task_struct *tracer;
362d514487SKees Cook 	struct task_struct *tracee;
37235e7527SKees Cook 	bool invalid;
382d514487SKees Cook 	struct list_head node;
3993b69d43SKees Cook 	struct rcu_head rcu;
402d514487SKees Cook };
412d514487SKees Cook 
422d514487SKees Cook static LIST_HEAD(ptracer_relations);
432d514487SKees Cook static DEFINE_SPINLOCK(ptracer_relations_lock);
442d514487SKees Cook 
45235e7527SKees Cook static void yama_relation_cleanup(struct work_struct *work);
46235e7527SKees Cook static DECLARE_WORK(yama_relation_work, yama_relation_cleanup);
47235e7527SKees Cook 
48dca6b414SJann Horn struct access_report_info {
49dca6b414SJann Horn 	struct callback_head work;
50dca6b414SJann Horn 	const char *access;
51dca6b414SJann Horn 	struct task_struct *target;
52dca6b414SJann Horn 	struct task_struct *agent;
53dca6b414SJann Horn };
54dca6b414SJann Horn 
55dca6b414SJann Horn static void __report_access(struct callback_head *work)
568a56038cSKees Cook {
57dca6b414SJann Horn 	struct access_report_info *info =
58dca6b414SJann Horn 		container_of(work, struct access_report_info, work);
598a56038cSKees Cook 	char *target_cmd, *agent_cmd;
608a56038cSKees Cook 
61dca6b414SJann Horn 	target_cmd = kstrdup_quotable_cmdline(info->target, GFP_KERNEL);
62dca6b414SJann Horn 	agent_cmd = kstrdup_quotable_cmdline(info->agent, GFP_KERNEL);
638a56038cSKees Cook 
648a56038cSKees Cook 	pr_notice_ratelimited(
658a56038cSKees Cook 		"ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n",
66dca6b414SJann Horn 		info->access, target_cmd, info->target->pid, agent_cmd,
67dca6b414SJann Horn 		info->agent->pid);
688a56038cSKees Cook 
698a56038cSKees Cook 	kfree(agent_cmd);
708a56038cSKees Cook 	kfree(target_cmd);
71dca6b414SJann Horn 
72dca6b414SJann Horn 	put_task_struct(info->agent);
73dca6b414SJann Horn 	put_task_struct(info->target);
74dca6b414SJann Horn 	kfree(info);
75dca6b414SJann Horn }
76dca6b414SJann Horn 
77dca6b414SJann Horn /* defers execution because cmdline access can sleep */
78dca6b414SJann Horn static void report_access(const char *access, struct task_struct *target,
79dca6b414SJann Horn 				struct task_struct *agent)
80dca6b414SJann Horn {
81dca6b414SJann Horn 	struct access_report_info *info;
82dca6b414SJann Horn 	char agent_comm[sizeof(agent->comm)];
83dca6b414SJann Horn 
84dca6b414SJann Horn 	assert_spin_locked(&target->alloc_lock); /* for target->comm */
85dca6b414SJann Horn 
86dca6b414SJann Horn 	if (current->flags & PF_KTHREAD) {
87dca6b414SJann Horn 		/* I don't think kthreads call task_work_run() before exiting.
88dca6b414SJann Horn 		 * Imagine angry ranting about procfs here.
89dca6b414SJann Horn 		 */
90dca6b414SJann Horn 		pr_notice_ratelimited(
91dca6b414SJann Horn 		    "ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n",
92dca6b414SJann Horn 		    access, target->comm, target->pid,
93dca6b414SJann Horn 		    get_task_comm(agent_comm, agent), agent->pid);
94dca6b414SJann Horn 		return;
95dca6b414SJann Horn 	}
96dca6b414SJann Horn 
97dca6b414SJann Horn 	info = kmalloc(sizeof(*info), GFP_ATOMIC);
98dca6b414SJann Horn 	if (!info)
99dca6b414SJann Horn 		return;
100dca6b414SJann Horn 	init_task_work(&info->work, __report_access);
101dca6b414SJann Horn 	get_task_struct(target);
102dca6b414SJann Horn 	get_task_struct(agent);
103dca6b414SJann Horn 	info->access = access;
104dca6b414SJann Horn 	info->target = target;
105dca6b414SJann Horn 	info->agent = agent;
106dca6b414SJann Horn 	if (task_work_add(current, &info->work, true) == 0)
107dca6b414SJann Horn 		return; /* success */
108dca6b414SJann Horn 
109dca6b414SJann Horn 	WARN(1, "report_access called from exiting task");
110dca6b414SJann Horn 	put_task_struct(target);
111dca6b414SJann Horn 	put_task_struct(agent);
112dca6b414SJann Horn 	kfree(info);
1138a56038cSKees Cook }
1148a56038cSKees Cook 
115235e7527SKees Cook /**
116235e7527SKees Cook  * yama_relation_cleanup - remove invalid entries from the relation list
117235e7527SKees Cook  *
118235e7527SKees Cook  */
119235e7527SKees Cook static void yama_relation_cleanup(struct work_struct *work)
120235e7527SKees Cook {
121235e7527SKees Cook 	struct ptrace_relation *relation;
122235e7527SKees Cook 
123235e7527SKees Cook 	spin_lock(&ptracer_relations_lock);
124235e7527SKees Cook 	rcu_read_lock();
125235e7527SKees Cook 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
126235e7527SKees Cook 		if (relation->invalid) {
127235e7527SKees Cook 			list_del_rcu(&relation->node);
128235e7527SKees Cook 			kfree_rcu(relation, rcu);
129235e7527SKees Cook 		}
130235e7527SKees Cook 	}
131235e7527SKees Cook 	rcu_read_unlock();
132235e7527SKees Cook 	spin_unlock(&ptracer_relations_lock);
133235e7527SKees Cook }
134235e7527SKees Cook 
1352d514487SKees Cook /**
1362d514487SKees Cook  * yama_ptracer_add - add/replace an exception for this tracer/tracee pair
1372d514487SKees Cook  * @tracer: the task_struct of the process doing the ptrace
1382d514487SKees Cook  * @tracee: the task_struct of the process to be ptraced
1392d514487SKees Cook  *
1402d514487SKees Cook  * Each tracee can have, at most, one tracer registered. Each time this
1412d514487SKees Cook  * is called, the prior registered tracer will be replaced for the tracee.
1422d514487SKees Cook  *
1432d514487SKees Cook  * Returns 0 if relationship was added, -ve on error.
1442d514487SKees Cook  */
1452d514487SKees Cook static int yama_ptracer_add(struct task_struct *tracer,
1462d514487SKees Cook 			    struct task_struct *tracee)
1472d514487SKees Cook {
14893b69d43SKees Cook 	struct ptrace_relation *relation, *added;
1492d514487SKees Cook 
1502d514487SKees Cook 	added = kmalloc(sizeof(*added), GFP_KERNEL);
1512d514487SKees Cook 	if (!added)
1522d514487SKees Cook 		return -ENOMEM;
1532d514487SKees Cook 
15493b69d43SKees Cook 	added->tracee = tracee;
15593b69d43SKees Cook 	added->tracer = tracer;
156235e7527SKees Cook 	added->invalid = false;
15793b69d43SKees Cook 
158235e7527SKees Cook 	spin_lock(&ptracer_relations_lock);
15993b69d43SKees Cook 	rcu_read_lock();
16093b69d43SKees Cook 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
161235e7527SKees Cook 		if (relation->invalid)
162235e7527SKees Cook 			continue;
16393b69d43SKees Cook 		if (relation->tracee == tracee) {
16493b69d43SKees Cook 			list_replace_rcu(&relation->node, &added->node);
16593b69d43SKees Cook 			kfree_rcu(relation, rcu);
16693b69d43SKees Cook 			goto out;
1672d514487SKees Cook 		}
1682d514487SKees Cook 	}
1692d514487SKees Cook 
17093b69d43SKees Cook 	list_add_rcu(&added->node, &ptracer_relations);
17193b69d43SKees Cook 
17293b69d43SKees Cook out:
17393b69d43SKees Cook 	rcu_read_unlock();
174235e7527SKees Cook 	spin_unlock(&ptracer_relations_lock);
17593b69d43SKees Cook 	return 0;
1762d514487SKees Cook }
1772d514487SKees Cook 
1782d514487SKees Cook /**
1792d514487SKees Cook  * yama_ptracer_del - remove exceptions related to the given tasks
1802d514487SKees Cook  * @tracer: remove any relation where tracer task matches
1812d514487SKees Cook  * @tracee: remove any relation where tracee task matches
1822d514487SKees Cook  */
1832d514487SKees Cook static void yama_ptracer_del(struct task_struct *tracer,
1842d514487SKees Cook 			     struct task_struct *tracee)
1852d514487SKees Cook {
18693b69d43SKees Cook 	struct ptrace_relation *relation;
187235e7527SKees Cook 	bool marked = false;
1882d514487SKees Cook 
18993b69d43SKees Cook 	rcu_read_lock();
19093b69d43SKees Cook 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
191235e7527SKees Cook 		if (relation->invalid)
192235e7527SKees Cook 			continue;
1932d514487SKees Cook 		if (relation->tracee == tracee ||
194bf06189eSKees Cook 		    (tracer && relation->tracer == tracer)) {
195235e7527SKees Cook 			relation->invalid = true;
196235e7527SKees Cook 			marked = true;
1972d514487SKees Cook 		}
19893b69d43SKees Cook 	}
19993b69d43SKees Cook 	rcu_read_unlock();
200235e7527SKees Cook 
201235e7527SKees Cook 	if (marked)
202235e7527SKees Cook 		schedule_work(&yama_relation_work);
2032d514487SKees Cook }
2042d514487SKees Cook 
2052d514487SKees Cook /**
2062d514487SKees Cook  * yama_task_free - check for task_pid to remove from exception list
2072d514487SKees Cook  * @task: task being removed
2082d514487SKees Cook  */
209c6993e4aSKees Cook void yama_task_free(struct task_struct *task)
2102d514487SKees Cook {
2112d514487SKees Cook 	yama_ptracer_del(task, task);
2122d514487SKees Cook }
2132d514487SKees Cook 
2142d514487SKees Cook /**
2152d514487SKees Cook  * yama_task_prctl - check for Yama-specific prctl operations
2162d514487SKees Cook  * @option: operation
2172d514487SKees Cook  * @arg2: argument
2182d514487SKees Cook  * @arg3: argument
2192d514487SKees Cook  * @arg4: argument
2202d514487SKees Cook  * @arg5: argument
2212d514487SKees Cook  *
2222d514487SKees Cook  * Return 0 on success, -ve on error.  -ENOSYS is returned when Yama
2232d514487SKees Cook  * does not handle the given option.
2242d514487SKees Cook  */
225c6993e4aSKees Cook int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
2262d514487SKees Cook 			   unsigned long arg4, unsigned long arg5)
2272d514487SKees Cook {
228b1d9e6b0SCasey Schaufler 	int rc = -ENOSYS;
2292d514487SKees Cook 	struct task_struct *myself = current;
2302d514487SKees Cook 
2312d514487SKees Cook 	switch (option) {
2322d514487SKees Cook 	case PR_SET_PTRACER:
2332d514487SKees Cook 		/* Since a thread can call prctl(), find the group leader
2342d514487SKees Cook 		 * before calling _add() or _del() on it, since we want
2352d514487SKees Cook 		 * process-level granularity of control. The tracer group
2362d514487SKees Cook 		 * leader checking is handled later when walking the ancestry
2372d514487SKees Cook 		 * at the time of PTRACE_ATTACH check.
2382d514487SKees Cook 		 */
2392d514487SKees Cook 		rcu_read_lock();
2402d514487SKees Cook 		if (!thread_group_leader(myself))
2412d514487SKees Cook 			myself = rcu_dereference(myself->group_leader);
2422d514487SKees Cook 		get_task_struct(myself);
2432d514487SKees Cook 		rcu_read_unlock();
2442d514487SKees Cook 
2452d514487SKees Cook 		if (arg2 == 0) {
2462d514487SKees Cook 			yama_ptracer_del(NULL, myself);
2472d514487SKees Cook 			rc = 0;
2482e4930ebSKees Cook 		} else if (arg2 == PR_SET_PTRACER_ANY || (int)arg2 == -1) {
249bf06189eSKees Cook 			rc = yama_ptracer_add(NULL, myself);
2502d514487SKees Cook 		} else {
2512d514487SKees Cook 			struct task_struct *tracer;
2522d514487SKees Cook 
2532d514487SKees Cook 			rcu_read_lock();
2542d514487SKees Cook 			tracer = find_task_by_vpid(arg2);
2552d514487SKees Cook 			if (tracer)
2562d514487SKees Cook 				get_task_struct(tracer);
2572d514487SKees Cook 			else
2582d514487SKees Cook 				rc = -EINVAL;
2592d514487SKees Cook 			rcu_read_unlock();
2602d514487SKees Cook 
2612d514487SKees Cook 			if (tracer) {
2622d514487SKees Cook 				rc = yama_ptracer_add(tracer, myself);
2632d514487SKees Cook 				put_task_struct(tracer);
2642d514487SKees Cook 			}
2652d514487SKees Cook 		}
2662d514487SKees Cook 
2672d514487SKees Cook 		put_task_struct(myself);
2682d514487SKees Cook 		break;
2692d514487SKees Cook 	}
2702d514487SKees Cook 
2712d514487SKees Cook 	return rc;
2722d514487SKees Cook }
2732d514487SKees Cook 
2742d514487SKees Cook /**
2752d514487SKees Cook  * task_is_descendant - walk up a process family tree looking for a match
2762d514487SKees Cook  * @parent: the process to compare against while walking up from child
2772d514487SKees Cook  * @child: the process to start from while looking upwards for parent
2782d514487SKees Cook  *
2792d514487SKees Cook  * Returns 1 if child is a descendant of parent, 0 if not.
2802d514487SKees Cook  */
2812d514487SKees Cook static int task_is_descendant(struct task_struct *parent,
2822d514487SKees Cook 			      struct task_struct *child)
2832d514487SKees Cook {
2842d514487SKees Cook 	int rc = 0;
2852d514487SKees Cook 	struct task_struct *walker = child;
2862d514487SKees Cook 
2872d514487SKees Cook 	if (!parent || !child)
2882d514487SKees Cook 		return 0;
2892d514487SKees Cook 
2902d514487SKees Cook 	rcu_read_lock();
2912d514487SKees Cook 	if (!thread_group_leader(parent))
2922d514487SKees Cook 		parent = rcu_dereference(parent->group_leader);
2932d514487SKees Cook 	while (walker->pid > 0) {
2942d514487SKees Cook 		if (!thread_group_leader(walker))
2952d514487SKees Cook 			walker = rcu_dereference(walker->group_leader);
2962d514487SKees Cook 		if (walker == parent) {
2972d514487SKees Cook 			rc = 1;
2982d514487SKees Cook 			break;
2992d514487SKees Cook 		}
3002d514487SKees Cook 		walker = rcu_dereference(walker->real_parent);
3012d514487SKees Cook 	}
3022d514487SKees Cook 	rcu_read_unlock();
3032d514487SKees Cook 
3042d514487SKees Cook 	return rc;
3052d514487SKees Cook }
3062d514487SKees Cook 
3072d514487SKees Cook /**
3082d514487SKees Cook  * ptracer_exception_found - tracer registered as exception for this tracee
3092d514487SKees Cook  * @tracer: the task_struct of the process attempting ptrace
3102d514487SKees Cook  * @tracee: the task_struct of the process to be ptraced
3112d514487SKees Cook  *
31250523a29SJosh Stone  * Returns 1 if tracer has a ptracer exception ancestor for tracee.
3132d514487SKees Cook  */
3142d514487SKees Cook static int ptracer_exception_found(struct task_struct *tracer,
3152d514487SKees Cook 				   struct task_struct *tracee)
3162d514487SKees Cook {
3172d514487SKees Cook 	int rc = 0;
3182d514487SKees Cook 	struct ptrace_relation *relation;
3192d514487SKees Cook 	struct task_struct *parent = NULL;
320bf06189eSKees Cook 	bool found = false;
3212d514487SKees Cook 
3222d514487SKees Cook 	rcu_read_lock();
32350523a29SJosh Stone 
32450523a29SJosh Stone 	/*
32550523a29SJosh Stone 	 * If there's already an active tracing relationship, then make an
32650523a29SJosh Stone 	 * exception for the sake of other accesses, like process_vm_rw().
32750523a29SJosh Stone 	 */
32850523a29SJosh Stone 	parent = ptrace_parent(tracee);
32950523a29SJosh Stone 	if (parent != NULL && same_thread_group(parent, tracer)) {
33050523a29SJosh Stone 		rc = 1;
33150523a29SJosh Stone 		goto unlock;
33250523a29SJosh Stone 	}
33350523a29SJosh Stone 
33450523a29SJosh Stone 	/* Look for a PR_SET_PTRACER relationship. */
3352d514487SKees Cook 	if (!thread_group_leader(tracee))
3362d514487SKees Cook 		tracee = rcu_dereference(tracee->group_leader);
337235e7527SKees Cook 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
338235e7527SKees Cook 		if (relation->invalid)
339235e7527SKees Cook 			continue;
3402d514487SKees Cook 		if (relation->tracee == tracee) {
3412d514487SKees Cook 			parent = relation->tracer;
342bf06189eSKees Cook 			found = true;
3432d514487SKees Cook 			break;
3442d514487SKees Cook 		}
345235e7527SKees Cook 	}
3462d514487SKees Cook 
347bf06189eSKees Cook 	if (found && (parent == NULL || task_is_descendant(parent, tracer)))
3482d514487SKees Cook 		rc = 1;
34950523a29SJosh Stone 
35050523a29SJosh Stone unlock:
3512d514487SKees Cook 	rcu_read_unlock();
3522d514487SKees Cook 
3532d514487SKees Cook 	return rc;
3542d514487SKees Cook }
3552d514487SKees Cook 
3562d514487SKees Cook /**
3572d514487SKees Cook  * yama_ptrace_access_check - validate PTRACE_ATTACH calls
3582d514487SKees Cook  * @child: task that current task is attempting to ptrace
3592d514487SKees Cook  * @mode: ptrace attach mode
3602d514487SKees Cook  *
3612d514487SKees Cook  * Returns 0 if following the ptrace is allowed, -ve on error.
3622d514487SKees Cook  */
363b1d9e6b0SCasey Schaufler static int yama_ptrace_access_check(struct task_struct *child,
3642d514487SKees Cook 				    unsigned int mode)
3652d514487SKees Cook {
366b1d9e6b0SCasey Schaufler 	int rc = 0;
3672d514487SKees Cook 
3682d514487SKees Cook 	/* require ptrace target be a child of ptracer on attach */
3693dfb7d8cSJann Horn 	if (mode & PTRACE_MODE_ATTACH) {
370389da25fSKees Cook 		switch (ptrace_scope) {
371389da25fSKees Cook 		case YAMA_SCOPE_DISABLED:
372389da25fSKees Cook 			/* No additional restrictions. */
373389da25fSKees Cook 			break;
374389da25fSKees Cook 		case YAMA_SCOPE_RELATIONAL:
3754c44aaafSEric W. Biederman 			rcu_read_lock();
376389da25fSKees Cook 			if (!task_is_descendant(current, child) &&
3772d514487SKees Cook 			    !ptracer_exception_found(current, child) &&
3784c44aaafSEric W. Biederman 			    !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
3792d514487SKees Cook 				rc = -EPERM;
3804c44aaafSEric W. Biederman 			rcu_read_unlock();
381389da25fSKees Cook 			break;
382389da25fSKees Cook 		case YAMA_SCOPE_CAPABILITY:
3834c44aaafSEric W. Biederman 			rcu_read_lock();
3844c44aaafSEric W. Biederman 			if (!ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
385389da25fSKees Cook 				rc = -EPERM;
3864c44aaafSEric W. Biederman 			rcu_read_unlock();
387389da25fSKees Cook 			break;
388389da25fSKees Cook 		case YAMA_SCOPE_NO_ATTACH:
389389da25fSKees Cook 		default:
390389da25fSKees Cook 			rc = -EPERM;
391389da25fSKees Cook 			break;
392389da25fSKees Cook 		}
393389da25fSKees Cook 	}
3942d514487SKees Cook 
3958a56038cSKees Cook 	if (rc && (mode & PTRACE_MODE_NOAUDIT) == 0)
3968a56038cSKees Cook 		report_access("attach", child, current);
3972d514487SKees Cook 
3982d514487SKees Cook 	return rc;
3992d514487SKees Cook }
4002d514487SKees Cook 
4019d8dad74SKees Cook /**
4029d8dad74SKees Cook  * yama_ptrace_traceme - validate PTRACE_TRACEME calls
4039d8dad74SKees Cook  * @parent: task that will become the ptracer of the current task
4049d8dad74SKees Cook  *
4059d8dad74SKees Cook  * Returns 0 if following the ptrace is allowed, -ve on error.
4069d8dad74SKees Cook  */
407c6993e4aSKees Cook int yama_ptrace_traceme(struct task_struct *parent)
4089d8dad74SKees Cook {
409b1d9e6b0SCasey Schaufler 	int rc = 0;
4109d8dad74SKees Cook 
4119d8dad74SKees Cook 	/* Only disallow PTRACE_TRACEME on more aggressive settings. */
4129d8dad74SKees Cook 	switch (ptrace_scope) {
4139d8dad74SKees Cook 	case YAMA_SCOPE_CAPABILITY:
414eddc0a3aSEric W. Biederman 		if (!has_ns_capability(parent, current_user_ns(), CAP_SYS_PTRACE))
4159d8dad74SKees Cook 			rc = -EPERM;
4169d8dad74SKees Cook 		break;
4179d8dad74SKees Cook 	case YAMA_SCOPE_NO_ATTACH:
4189d8dad74SKees Cook 		rc = -EPERM;
4199d8dad74SKees Cook 		break;
4209d8dad74SKees Cook 	}
4219d8dad74SKees Cook 
422dca6b414SJann Horn 	if (rc) {
423dca6b414SJann Horn 		task_lock(current);
4248a56038cSKees Cook 		report_access("traceme", current, parent);
425dca6b414SJann Horn 		task_unlock(current);
426dca6b414SJann Horn 	}
4279d8dad74SKees Cook 
4289d8dad74SKees Cook 	return rc;
4299d8dad74SKees Cook }
4309d8dad74SKees Cook 
431b1d9e6b0SCasey Schaufler static struct security_hook_list yama_hooks[] = {
432e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, yama_ptrace_access_check),
433e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, yama_ptrace_traceme),
434e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_prctl, yama_task_prctl),
435e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_free, yama_task_free),
4362d514487SKees Cook };
437b1d9e6b0SCasey Schaufler 
4382d514487SKees Cook #ifdef CONFIG_SYSCTL
439389da25fSKees Cook static int yama_dointvec_minmax(struct ctl_table *table, int write,
440389da25fSKees Cook 				void __user *buffer, size_t *lenp, loff_t *ppos)
441389da25fSKees Cook {
44241a4695cSKees Cook 	struct ctl_table table_copy;
443389da25fSKees Cook 
444389da25fSKees Cook 	if (write && !capable(CAP_SYS_PTRACE))
445389da25fSKees Cook 		return -EPERM;
446389da25fSKees Cook 
447389da25fSKees Cook 	/* Lock the max value if it ever gets set. */
44841a4695cSKees Cook 	table_copy = *table;
44941a4695cSKees Cook 	if (*(int *)table_copy.data == *(int *)table_copy.extra2)
45041a4695cSKees Cook 		table_copy.extra1 = table_copy.extra2;
451389da25fSKees Cook 
45241a4695cSKees Cook 	return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos);
453389da25fSKees Cook }
454389da25fSKees Cook 
4552d514487SKees Cook static int zero;
456389da25fSKees Cook static int max_scope = YAMA_SCOPE_NO_ATTACH;
4572d514487SKees Cook 
4582d514487SKees Cook struct ctl_path yama_sysctl_path[] = {
4592d514487SKees Cook 	{ .procname = "kernel", },
4602d514487SKees Cook 	{ .procname = "yama", },
4612d514487SKees Cook 	{ }
4622d514487SKees Cook };
4632d514487SKees Cook 
4642d514487SKees Cook static struct ctl_table yama_sysctl_table[] = {
4652d514487SKees Cook 	{
4662d514487SKees Cook 		.procname       = "ptrace_scope",
4672d514487SKees Cook 		.data           = &ptrace_scope,
4682d514487SKees Cook 		.maxlen         = sizeof(int),
4692d514487SKees Cook 		.mode           = 0644,
470389da25fSKees Cook 		.proc_handler   = yama_dointvec_minmax,
4712d514487SKees Cook 		.extra1         = &zero,
472389da25fSKees Cook 		.extra2         = &max_scope,
4732d514487SKees Cook 	},
4742d514487SKees Cook 	{ }
4752d514487SKees Cook };
476730daa16SKees Cook static void __init yama_init_sysctl(void)
4772d514487SKees Cook {
4782d514487SKees Cook 	if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
4792d514487SKees Cook 		panic("Yama: sysctl registration failed.\n");
4802d514487SKees Cook }
481730daa16SKees Cook #else
482730daa16SKees Cook static inline void yama_init_sysctl(void) { }
483730daa16SKees Cook #endif /* CONFIG_SYSCTL */
4842d514487SKees Cook 
485730daa16SKees Cook void __init yama_add_hooks(void)
486730daa16SKees Cook {
487730daa16SKees Cook 	pr_info("Yama: becoming mindful.\n");
488*d69dece5SCasey Schaufler 	security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks), "yama");
489730daa16SKees Cook 	yama_init_sysctl();
490730daa16SKees Cook }
491