xref: /openbmc/linux/security/yama/yama_lsm.c (revision 2ee0826085d1c0281cb60c1f4bc3e0c27efeedc3)
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 
253*2ee08260SMike Rapoport 			tracer = find_get_task_by_vpid(arg2);
254*2ee08260SMike Rapoport 			if (!tracer) {
2552d514487SKees Cook 				rc = -EINVAL;
256*2ee08260SMike Rapoport 			} else {
2572d514487SKees Cook 				rc = yama_ptracer_add(tracer, myself);
2582d514487SKees Cook 				put_task_struct(tracer);
2592d514487SKees Cook 			}
2602d514487SKees Cook 		}
2612d514487SKees Cook 
2622d514487SKees Cook 		put_task_struct(myself);
2632d514487SKees Cook 		break;
2642d514487SKees Cook 	}
2652d514487SKees Cook 
2662d514487SKees Cook 	return rc;
2672d514487SKees Cook }
2682d514487SKees Cook 
2692d514487SKees Cook /**
2702d514487SKees Cook  * task_is_descendant - walk up a process family tree looking for a match
2712d514487SKees Cook  * @parent: the process to compare against while walking up from child
2722d514487SKees Cook  * @child: the process to start from while looking upwards for parent
2732d514487SKees Cook  *
2742d514487SKees Cook  * Returns 1 if child is a descendant of parent, 0 if not.
2752d514487SKees Cook  */
2762d514487SKees Cook static int task_is_descendant(struct task_struct *parent,
2772d514487SKees Cook 			      struct task_struct *child)
2782d514487SKees Cook {
2792d514487SKees Cook 	int rc = 0;
2802d514487SKees Cook 	struct task_struct *walker = child;
2812d514487SKees Cook 
2822d514487SKees Cook 	if (!parent || !child)
2832d514487SKees Cook 		return 0;
2842d514487SKees Cook 
2852d514487SKees Cook 	rcu_read_lock();
2862d514487SKees Cook 	if (!thread_group_leader(parent))
2872d514487SKees Cook 		parent = rcu_dereference(parent->group_leader);
2882d514487SKees Cook 	while (walker->pid > 0) {
2892d514487SKees Cook 		if (!thread_group_leader(walker))
2902d514487SKees Cook 			walker = rcu_dereference(walker->group_leader);
2912d514487SKees Cook 		if (walker == parent) {
2922d514487SKees Cook 			rc = 1;
2932d514487SKees Cook 			break;
2942d514487SKees Cook 		}
2952d514487SKees Cook 		walker = rcu_dereference(walker->real_parent);
2962d514487SKees Cook 	}
2972d514487SKees Cook 	rcu_read_unlock();
2982d514487SKees Cook 
2992d514487SKees Cook 	return rc;
3002d514487SKees Cook }
3012d514487SKees Cook 
3022d514487SKees Cook /**
3032d514487SKees Cook  * ptracer_exception_found - tracer registered as exception for this tracee
3042d514487SKees Cook  * @tracer: the task_struct of the process attempting ptrace
3052d514487SKees Cook  * @tracee: the task_struct of the process to be ptraced
3062d514487SKees Cook  *
30750523a29SJosh Stone  * Returns 1 if tracer has a ptracer exception ancestor for tracee.
3082d514487SKees Cook  */
3092d514487SKees Cook static int ptracer_exception_found(struct task_struct *tracer,
3102d514487SKees Cook 				   struct task_struct *tracee)
3112d514487SKees Cook {
3122d514487SKees Cook 	int rc = 0;
3132d514487SKees Cook 	struct ptrace_relation *relation;
3142d514487SKees Cook 	struct task_struct *parent = NULL;
315bf06189eSKees Cook 	bool found = false;
3162d514487SKees Cook 
3172d514487SKees Cook 	rcu_read_lock();
31850523a29SJosh Stone 
31950523a29SJosh Stone 	/*
32050523a29SJosh Stone 	 * If there's already an active tracing relationship, then make an
32150523a29SJosh Stone 	 * exception for the sake of other accesses, like process_vm_rw().
32250523a29SJosh Stone 	 */
32350523a29SJosh Stone 	parent = ptrace_parent(tracee);
32450523a29SJosh Stone 	if (parent != NULL && same_thread_group(parent, tracer)) {
32550523a29SJosh Stone 		rc = 1;
32650523a29SJosh Stone 		goto unlock;
32750523a29SJosh Stone 	}
32850523a29SJosh Stone 
32950523a29SJosh Stone 	/* Look for a PR_SET_PTRACER relationship. */
3302d514487SKees Cook 	if (!thread_group_leader(tracee))
3312d514487SKees Cook 		tracee = rcu_dereference(tracee->group_leader);
332235e7527SKees Cook 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
333235e7527SKees Cook 		if (relation->invalid)
334235e7527SKees Cook 			continue;
3352d514487SKees Cook 		if (relation->tracee == tracee) {
3362d514487SKees Cook 			parent = relation->tracer;
337bf06189eSKees Cook 			found = true;
3382d514487SKees Cook 			break;
3392d514487SKees Cook 		}
340235e7527SKees Cook 	}
3412d514487SKees Cook 
342bf06189eSKees Cook 	if (found && (parent == NULL || task_is_descendant(parent, tracer)))
3432d514487SKees Cook 		rc = 1;
34450523a29SJosh Stone 
34550523a29SJosh Stone unlock:
3462d514487SKees Cook 	rcu_read_unlock();
3472d514487SKees Cook 
3482d514487SKees Cook 	return rc;
3492d514487SKees Cook }
3502d514487SKees Cook 
3512d514487SKees Cook /**
3522d514487SKees Cook  * yama_ptrace_access_check - validate PTRACE_ATTACH calls
3532d514487SKees Cook  * @child: task that current task is attempting to ptrace
3542d514487SKees Cook  * @mode: ptrace attach mode
3552d514487SKees Cook  *
3562d514487SKees Cook  * Returns 0 if following the ptrace is allowed, -ve on error.
3572d514487SKees Cook  */
358b1d9e6b0SCasey Schaufler static int yama_ptrace_access_check(struct task_struct *child,
3592d514487SKees Cook 				    unsigned int mode)
3602d514487SKees Cook {
361b1d9e6b0SCasey Schaufler 	int rc = 0;
3622d514487SKees Cook 
3632d514487SKees Cook 	/* require ptrace target be a child of ptracer on attach */
3643dfb7d8cSJann Horn 	if (mode & PTRACE_MODE_ATTACH) {
365389da25fSKees Cook 		switch (ptrace_scope) {
366389da25fSKees Cook 		case YAMA_SCOPE_DISABLED:
367389da25fSKees Cook 			/* No additional restrictions. */
368389da25fSKees Cook 			break;
369389da25fSKees Cook 		case YAMA_SCOPE_RELATIONAL:
3704c44aaafSEric W. Biederman 			rcu_read_lock();
371389da25fSKees Cook 			if (!task_is_descendant(current, child) &&
3722d514487SKees Cook 			    !ptracer_exception_found(current, child) &&
3734c44aaafSEric W. Biederman 			    !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
3742d514487SKees Cook 				rc = -EPERM;
3754c44aaafSEric W. Biederman 			rcu_read_unlock();
376389da25fSKees Cook 			break;
377389da25fSKees Cook 		case YAMA_SCOPE_CAPABILITY:
3784c44aaafSEric W. Biederman 			rcu_read_lock();
3794c44aaafSEric W. Biederman 			if (!ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
380389da25fSKees Cook 				rc = -EPERM;
3814c44aaafSEric W. Biederman 			rcu_read_unlock();
382389da25fSKees Cook 			break;
383389da25fSKees Cook 		case YAMA_SCOPE_NO_ATTACH:
384389da25fSKees Cook 		default:
385389da25fSKees Cook 			rc = -EPERM;
386389da25fSKees Cook 			break;
387389da25fSKees Cook 		}
388389da25fSKees Cook 	}
3892d514487SKees Cook 
3908a56038cSKees Cook 	if (rc && (mode & PTRACE_MODE_NOAUDIT) == 0)
3918a56038cSKees Cook 		report_access("attach", child, current);
3922d514487SKees Cook 
3932d514487SKees Cook 	return rc;
3942d514487SKees Cook }
3952d514487SKees Cook 
3969d8dad74SKees Cook /**
3979d8dad74SKees Cook  * yama_ptrace_traceme - validate PTRACE_TRACEME calls
3989d8dad74SKees Cook  * @parent: task that will become the ptracer of the current task
3999d8dad74SKees Cook  *
4009d8dad74SKees Cook  * Returns 0 if following the ptrace is allowed, -ve on error.
4019d8dad74SKees Cook  */
402c6993e4aSKees Cook int yama_ptrace_traceme(struct task_struct *parent)
4039d8dad74SKees Cook {
404b1d9e6b0SCasey Schaufler 	int rc = 0;
4059d8dad74SKees Cook 
4069d8dad74SKees Cook 	/* Only disallow PTRACE_TRACEME on more aggressive settings. */
4079d8dad74SKees Cook 	switch (ptrace_scope) {
4089d8dad74SKees Cook 	case YAMA_SCOPE_CAPABILITY:
409eddc0a3aSEric W. Biederman 		if (!has_ns_capability(parent, current_user_ns(), CAP_SYS_PTRACE))
4109d8dad74SKees Cook 			rc = -EPERM;
4119d8dad74SKees Cook 		break;
4129d8dad74SKees Cook 	case YAMA_SCOPE_NO_ATTACH:
4139d8dad74SKees Cook 		rc = -EPERM;
4149d8dad74SKees Cook 		break;
4159d8dad74SKees Cook 	}
4169d8dad74SKees Cook 
417dca6b414SJann Horn 	if (rc) {
418dca6b414SJann Horn 		task_lock(current);
4198a56038cSKees Cook 		report_access("traceme", current, parent);
420dca6b414SJann Horn 		task_unlock(current);
421dca6b414SJann Horn 	}
4229d8dad74SKees Cook 
4239d8dad74SKees Cook 	return rc;
4249d8dad74SKees Cook }
4259d8dad74SKees Cook 
426ca97d939SJames Morris static struct security_hook_list yama_hooks[] __lsm_ro_after_init = {
427e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, yama_ptrace_access_check),
428e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, yama_ptrace_traceme),
429e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_prctl, yama_task_prctl),
430e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_free, yama_task_free),
4312d514487SKees Cook };
432b1d9e6b0SCasey Schaufler 
4332d514487SKees Cook #ifdef CONFIG_SYSCTL
434389da25fSKees Cook static int yama_dointvec_minmax(struct ctl_table *table, int write,
435389da25fSKees Cook 				void __user *buffer, size_t *lenp, loff_t *ppos)
436389da25fSKees Cook {
43741a4695cSKees Cook 	struct ctl_table table_copy;
438389da25fSKees Cook 
439389da25fSKees Cook 	if (write && !capable(CAP_SYS_PTRACE))
440389da25fSKees Cook 		return -EPERM;
441389da25fSKees Cook 
442389da25fSKees Cook 	/* Lock the max value if it ever gets set. */
44341a4695cSKees Cook 	table_copy = *table;
44441a4695cSKees Cook 	if (*(int *)table_copy.data == *(int *)table_copy.extra2)
44541a4695cSKees Cook 		table_copy.extra1 = table_copy.extra2;
446389da25fSKees Cook 
44741a4695cSKees Cook 	return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos);
448389da25fSKees Cook }
449389da25fSKees Cook 
4502d514487SKees Cook static int zero;
451389da25fSKees Cook static int max_scope = YAMA_SCOPE_NO_ATTACH;
4522d514487SKees Cook 
4532d514487SKees Cook struct ctl_path yama_sysctl_path[] = {
4542d514487SKees Cook 	{ .procname = "kernel", },
4552d514487SKees Cook 	{ .procname = "yama", },
4562d514487SKees Cook 	{ }
4572d514487SKees Cook };
4582d514487SKees Cook 
4592d514487SKees Cook static struct ctl_table yama_sysctl_table[] = {
4602d514487SKees Cook 	{
4612d514487SKees Cook 		.procname       = "ptrace_scope",
4622d514487SKees Cook 		.data           = &ptrace_scope,
4632d514487SKees Cook 		.maxlen         = sizeof(int),
4642d514487SKees Cook 		.mode           = 0644,
465389da25fSKees Cook 		.proc_handler   = yama_dointvec_minmax,
4662d514487SKees Cook 		.extra1         = &zero,
467389da25fSKees Cook 		.extra2         = &max_scope,
4682d514487SKees Cook 	},
4692d514487SKees Cook 	{ }
4702d514487SKees Cook };
471730daa16SKees Cook static void __init yama_init_sysctl(void)
4722d514487SKees Cook {
4732d514487SKees Cook 	if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
4742d514487SKees Cook 		panic("Yama: sysctl registration failed.\n");
4752d514487SKees Cook }
476730daa16SKees Cook #else
477730daa16SKees Cook static inline void yama_init_sysctl(void) { }
478730daa16SKees Cook #endif /* CONFIG_SYSCTL */
4792d514487SKees Cook 
480730daa16SKees Cook void __init yama_add_hooks(void)
481730daa16SKees Cook {
482730daa16SKees Cook 	pr_info("Yama: becoming mindful.\n");
483d69dece5SCasey Schaufler 	security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks), "yama");
484730daa16SKees Cook 	yama_init_sysctl();
485730daa16SKees Cook }
486