xref: /openbmc/linux/security/yama/yama_lsm.c (revision 3dfb7d8cdbc7ea0c2970450e60818bb3eefbad69)
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>
212d514487SKees Cook 
22389da25fSKees Cook #define YAMA_SCOPE_DISABLED	0
23389da25fSKees Cook #define YAMA_SCOPE_RELATIONAL	1
24389da25fSKees Cook #define YAMA_SCOPE_CAPABILITY	2
25389da25fSKees Cook #define YAMA_SCOPE_NO_ATTACH	3
26389da25fSKees Cook 
27389da25fSKees Cook static int ptrace_scope = YAMA_SCOPE_RELATIONAL;
282d514487SKees Cook 
292d514487SKees Cook /* describe a ptrace relationship for potential exception */
302d514487SKees Cook struct ptrace_relation {
312d514487SKees Cook 	struct task_struct *tracer;
322d514487SKees Cook 	struct task_struct *tracee;
33235e7527SKees Cook 	bool invalid;
342d514487SKees Cook 	struct list_head node;
3593b69d43SKees Cook 	struct rcu_head rcu;
362d514487SKees Cook };
372d514487SKees Cook 
382d514487SKees Cook static LIST_HEAD(ptracer_relations);
392d514487SKees Cook static DEFINE_SPINLOCK(ptracer_relations_lock);
402d514487SKees Cook 
41235e7527SKees Cook static void yama_relation_cleanup(struct work_struct *work);
42235e7527SKees Cook static DECLARE_WORK(yama_relation_work, yama_relation_cleanup);
43235e7527SKees Cook 
44235e7527SKees Cook /**
45235e7527SKees Cook  * yama_relation_cleanup - remove invalid entries from the relation list
46235e7527SKees Cook  *
47235e7527SKees Cook  */
48235e7527SKees Cook static void yama_relation_cleanup(struct work_struct *work)
49235e7527SKees Cook {
50235e7527SKees Cook 	struct ptrace_relation *relation;
51235e7527SKees Cook 
52235e7527SKees Cook 	spin_lock(&ptracer_relations_lock);
53235e7527SKees Cook 	rcu_read_lock();
54235e7527SKees Cook 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
55235e7527SKees Cook 		if (relation->invalid) {
56235e7527SKees Cook 			list_del_rcu(&relation->node);
57235e7527SKees Cook 			kfree_rcu(relation, rcu);
58235e7527SKees Cook 		}
59235e7527SKees Cook 	}
60235e7527SKees Cook 	rcu_read_unlock();
61235e7527SKees Cook 	spin_unlock(&ptracer_relations_lock);
62235e7527SKees Cook }
63235e7527SKees Cook 
642d514487SKees Cook /**
652d514487SKees Cook  * yama_ptracer_add - add/replace an exception for this tracer/tracee pair
662d514487SKees Cook  * @tracer: the task_struct of the process doing the ptrace
672d514487SKees Cook  * @tracee: the task_struct of the process to be ptraced
682d514487SKees Cook  *
692d514487SKees Cook  * Each tracee can have, at most, one tracer registered. Each time this
702d514487SKees Cook  * is called, the prior registered tracer will be replaced for the tracee.
712d514487SKees Cook  *
722d514487SKees Cook  * Returns 0 if relationship was added, -ve on error.
732d514487SKees Cook  */
742d514487SKees Cook static int yama_ptracer_add(struct task_struct *tracer,
752d514487SKees Cook 			    struct task_struct *tracee)
762d514487SKees Cook {
7793b69d43SKees Cook 	struct ptrace_relation *relation, *added;
782d514487SKees Cook 
792d514487SKees Cook 	added = kmalloc(sizeof(*added), GFP_KERNEL);
802d514487SKees Cook 	if (!added)
812d514487SKees Cook 		return -ENOMEM;
822d514487SKees Cook 
8393b69d43SKees Cook 	added->tracee = tracee;
8493b69d43SKees Cook 	added->tracer = tracer;
85235e7527SKees Cook 	added->invalid = false;
8693b69d43SKees Cook 
87235e7527SKees Cook 	spin_lock(&ptracer_relations_lock);
8893b69d43SKees Cook 	rcu_read_lock();
8993b69d43SKees Cook 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
90235e7527SKees Cook 		if (relation->invalid)
91235e7527SKees Cook 			continue;
9293b69d43SKees Cook 		if (relation->tracee == tracee) {
9393b69d43SKees Cook 			list_replace_rcu(&relation->node, &added->node);
9493b69d43SKees Cook 			kfree_rcu(relation, rcu);
9593b69d43SKees Cook 			goto out;
962d514487SKees Cook 		}
972d514487SKees Cook 	}
982d514487SKees Cook 
9993b69d43SKees Cook 	list_add_rcu(&added->node, &ptracer_relations);
10093b69d43SKees Cook 
10193b69d43SKees Cook out:
10293b69d43SKees Cook 	rcu_read_unlock();
103235e7527SKees Cook 	spin_unlock(&ptracer_relations_lock);
10493b69d43SKees Cook 	return 0;
1052d514487SKees Cook }
1062d514487SKees Cook 
1072d514487SKees Cook /**
1082d514487SKees Cook  * yama_ptracer_del - remove exceptions related to the given tasks
1092d514487SKees Cook  * @tracer: remove any relation where tracer task matches
1102d514487SKees Cook  * @tracee: remove any relation where tracee task matches
1112d514487SKees Cook  */
1122d514487SKees Cook static void yama_ptracer_del(struct task_struct *tracer,
1132d514487SKees Cook 			     struct task_struct *tracee)
1142d514487SKees Cook {
11593b69d43SKees Cook 	struct ptrace_relation *relation;
116235e7527SKees Cook 	bool marked = false;
1172d514487SKees Cook 
11893b69d43SKees Cook 	rcu_read_lock();
11993b69d43SKees Cook 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
120235e7527SKees Cook 		if (relation->invalid)
121235e7527SKees Cook 			continue;
1222d514487SKees Cook 		if (relation->tracee == tracee ||
123bf06189eSKees Cook 		    (tracer && relation->tracer == tracer)) {
124235e7527SKees Cook 			relation->invalid = true;
125235e7527SKees Cook 			marked = true;
1262d514487SKees Cook 		}
12793b69d43SKees Cook 	}
12893b69d43SKees Cook 	rcu_read_unlock();
129235e7527SKees Cook 
130235e7527SKees Cook 	if (marked)
131235e7527SKees Cook 		schedule_work(&yama_relation_work);
1322d514487SKees Cook }
1332d514487SKees Cook 
1342d514487SKees Cook /**
1352d514487SKees Cook  * yama_task_free - check for task_pid to remove from exception list
1362d514487SKees Cook  * @task: task being removed
1372d514487SKees Cook  */
138c6993e4aSKees Cook void yama_task_free(struct task_struct *task)
1392d514487SKees Cook {
1402d514487SKees Cook 	yama_ptracer_del(task, task);
1412d514487SKees Cook }
1422d514487SKees Cook 
1432d514487SKees Cook /**
1442d514487SKees Cook  * yama_task_prctl - check for Yama-specific prctl operations
1452d514487SKees Cook  * @option: operation
1462d514487SKees Cook  * @arg2: argument
1472d514487SKees Cook  * @arg3: argument
1482d514487SKees Cook  * @arg4: argument
1492d514487SKees Cook  * @arg5: argument
1502d514487SKees Cook  *
1512d514487SKees Cook  * Return 0 on success, -ve on error.  -ENOSYS is returned when Yama
1522d514487SKees Cook  * does not handle the given option.
1532d514487SKees Cook  */
154c6993e4aSKees Cook int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
1552d514487SKees Cook 			   unsigned long arg4, unsigned long arg5)
1562d514487SKees Cook {
157b1d9e6b0SCasey Schaufler 	int rc = -ENOSYS;
1582d514487SKees Cook 	struct task_struct *myself = current;
1592d514487SKees Cook 
1602d514487SKees Cook 	switch (option) {
1612d514487SKees Cook 	case PR_SET_PTRACER:
1622d514487SKees Cook 		/* Since a thread can call prctl(), find the group leader
1632d514487SKees Cook 		 * before calling _add() or _del() on it, since we want
1642d514487SKees Cook 		 * process-level granularity of control. The tracer group
1652d514487SKees Cook 		 * leader checking is handled later when walking the ancestry
1662d514487SKees Cook 		 * at the time of PTRACE_ATTACH check.
1672d514487SKees Cook 		 */
1682d514487SKees Cook 		rcu_read_lock();
1692d514487SKees Cook 		if (!thread_group_leader(myself))
1702d514487SKees Cook 			myself = rcu_dereference(myself->group_leader);
1712d514487SKees Cook 		get_task_struct(myself);
1722d514487SKees Cook 		rcu_read_unlock();
1732d514487SKees Cook 
1742d514487SKees Cook 		if (arg2 == 0) {
1752d514487SKees Cook 			yama_ptracer_del(NULL, myself);
1762d514487SKees Cook 			rc = 0;
1772e4930ebSKees Cook 		} else if (arg2 == PR_SET_PTRACER_ANY || (int)arg2 == -1) {
178bf06189eSKees Cook 			rc = yama_ptracer_add(NULL, myself);
1792d514487SKees Cook 		} else {
1802d514487SKees Cook 			struct task_struct *tracer;
1812d514487SKees Cook 
1822d514487SKees Cook 			rcu_read_lock();
1832d514487SKees Cook 			tracer = find_task_by_vpid(arg2);
1842d514487SKees Cook 			if (tracer)
1852d514487SKees Cook 				get_task_struct(tracer);
1862d514487SKees Cook 			else
1872d514487SKees Cook 				rc = -EINVAL;
1882d514487SKees Cook 			rcu_read_unlock();
1892d514487SKees Cook 
1902d514487SKees Cook 			if (tracer) {
1912d514487SKees Cook 				rc = yama_ptracer_add(tracer, myself);
1922d514487SKees Cook 				put_task_struct(tracer);
1932d514487SKees Cook 			}
1942d514487SKees Cook 		}
1952d514487SKees Cook 
1962d514487SKees Cook 		put_task_struct(myself);
1972d514487SKees Cook 		break;
1982d514487SKees Cook 	}
1992d514487SKees Cook 
2002d514487SKees Cook 	return rc;
2012d514487SKees Cook }
2022d514487SKees Cook 
2032d514487SKees Cook /**
2042d514487SKees Cook  * task_is_descendant - walk up a process family tree looking for a match
2052d514487SKees Cook  * @parent: the process to compare against while walking up from child
2062d514487SKees Cook  * @child: the process to start from while looking upwards for parent
2072d514487SKees Cook  *
2082d514487SKees Cook  * Returns 1 if child is a descendant of parent, 0 if not.
2092d514487SKees Cook  */
2102d514487SKees Cook static int task_is_descendant(struct task_struct *parent,
2112d514487SKees Cook 			      struct task_struct *child)
2122d514487SKees Cook {
2132d514487SKees Cook 	int rc = 0;
2142d514487SKees Cook 	struct task_struct *walker = child;
2152d514487SKees Cook 
2162d514487SKees Cook 	if (!parent || !child)
2172d514487SKees Cook 		return 0;
2182d514487SKees Cook 
2192d514487SKees Cook 	rcu_read_lock();
2202d514487SKees Cook 	if (!thread_group_leader(parent))
2212d514487SKees Cook 		parent = rcu_dereference(parent->group_leader);
2222d514487SKees Cook 	while (walker->pid > 0) {
2232d514487SKees Cook 		if (!thread_group_leader(walker))
2242d514487SKees Cook 			walker = rcu_dereference(walker->group_leader);
2252d514487SKees Cook 		if (walker == parent) {
2262d514487SKees Cook 			rc = 1;
2272d514487SKees Cook 			break;
2282d514487SKees Cook 		}
2292d514487SKees Cook 		walker = rcu_dereference(walker->real_parent);
2302d514487SKees Cook 	}
2312d514487SKees Cook 	rcu_read_unlock();
2322d514487SKees Cook 
2332d514487SKees Cook 	return rc;
2342d514487SKees Cook }
2352d514487SKees Cook 
2362d514487SKees Cook /**
2372d514487SKees Cook  * ptracer_exception_found - tracer registered as exception for this tracee
2382d514487SKees Cook  * @tracer: the task_struct of the process attempting ptrace
2392d514487SKees Cook  * @tracee: the task_struct of the process to be ptraced
2402d514487SKees Cook  *
2412d514487SKees Cook  * Returns 1 if tracer has is ptracer exception ancestor for tracee.
2422d514487SKees Cook  */
2432d514487SKees Cook static int ptracer_exception_found(struct task_struct *tracer,
2442d514487SKees Cook 				   struct task_struct *tracee)
2452d514487SKees Cook {
2462d514487SKees Cook 	int rc = 0;
2472d514487SKees Cook 	struct ptrace_relation *relation;
2482d514487SKees Cook 	struct task_struct *parent = NULL;
249bf06189eSKees Cook 	bool found = false;
2502d514487SKees Cook 
2512d514487SKees Cook 	rcu_read_lock();
2522d514487SKees Cook 	if (!thread_group_leader(tracee))
2532d514487SKees Cook 		tracee = rcu_dereference(tracee->group_leader);
254235e7527SKees Cook 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
255235e7527SKees Cook 		if (relation->invalid)
256235e7527SKees Cook 			continue;
2572d514487SKees Cook 		if (relation->tracee == tracee) {
2582d514487SKees Cook 			parent = relation->tracer;
259bf06189eSKees Cook 			found = true;
2602d514487SKees Cook 			break;
2612d514487SKees Cook 		}
262235e7527SKees Cook 	}
2632d514487SKees Cook 
264bf06189eSKees Cook 	if (found && (parent == NULL || task_is_descendant(parent, tracer)))
2652d514487SKees Cook 		rc = 1;
2662d514487SKees Cook 	rcu_read_unlock();
2672d514487SKees Cook 
2682d514487SKees Cook 	return rc;
2692d514487SKees Cook }
2702d514487SKees Cook 
2712d514487SKees Cook /**
2722d514487SKees Cook  * yama_ptrace_access_check - validate PTRACE_ATTACH calls
2732d514487SKees Cook  * @child: task that current task is attempting to ptrace
2742d514487SKees Cook  * @mode: ptrace attach mode
2752d514487SKees Cook  *
2762d514487SKees Cook  * Returns 0 if following the ptrace is allowed, -ve on error.
2772d514487SKees Cook  */
278b1d9e6b0SCasey Schaufler static int yama_ptrace_access_check(struct task_struct *child,
2792d514487SKees Cook 				    unsigned int mode)
2802d514487SKees Cook {
281b1d9e6b0SCasey Schaufler 	int rc = 0;
2822d514487SKees Cook 
2832d514487SKees Cook 	/* require ptrace target be a child of ptracer on attach */
284*3dfb7d8cSJann Horn 	if (mode & PTRACE_MODE_ATTACH) {
285389da25fSKees Cook 		switch (ptrace_scope) {
286389da25fSKees Cook 		case YAMA_SCOPE_DISABLED:
287389da25fSKees Cook 			/* No additional restrictions. */
288389da25fSKees Cook 			break;
289389da25fSKees Cook 		case YAMA_SCOPE_RELATIONAL:
2904c44aaafSEric W. Biederman 			rcu_read_lock();
291389da25fSKees Cook 			if (!task_is_descendant(current, child) &&
2922d514487SKees Cook 			    !ptracer_exception_found(current, child) &&
2934c44aaafSEric W. Biederman 			    !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
2942d514487SKees Cook 				rc = -EPERM;
2954c44aaafSEric W. Biederman 			rcu_read_unlock();
296389da25fSKees Cook 			break;
297389da25fSKees Cook 		case YAMA_SCOPE_CAPABILITY:
2984c44aaafSEric W. Biederman 			rcu_read_lock();
2994c44aaafSEric W. Biederman 			if (!ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
300389da25fSKees Cook 				rc = -EPERM;
3014c44aaafSEric W. Biederman 			rcu_read_unlock();
302389da25fSKees Cook 			break;
303389da25fSKees Cook 		case YAMA_SCOPE_NO_ATTACH:
304389da25fSKees Cook 		default:
305389da25fSKees Cook 			rc = -EPERM;
306389da25fSKees Cook 			break;
307389da25fSKees Cook 		}
308389da25fSKees Cook 	}
3092d514487SKees Cook 
310*3dfb7d8cSJann Horn 	if (rc && (mode & PTRACE_MODE_NOAUDIT) == 0) {
311389da25fSKees Cook 		printk_ratelimited(KERN_NOTICE
312389da25fSKees Cook 			"ptrace of pid %d was attempted by: %s (pid %d)\n",
3137612bfeeSKees Cook 			child->pid, current->comm, current->pid);
3142d514487SKees Cook 	}
3152d514487SKees Cook 
3162d514487SKees Cook 	return rc;
3172d514487SKees Cook }
3182d514487SKees Cook 
3199d8dad74SKees Cook /**
3209d8dad74SKees Cook  * yama_ptrace_traceme - validate PTRACE_TRACEME calls
3219d8dad74SKees Cook  * @parent: task that will become the ptracer of the current task
3229d8dad74SKees Cook  *
3239d8dad74SKees Cook  * Returns 0 if following the ptrace is allowed, -ve on error.
3249d8dad74SKees Cook  */
325c6993e4aSKees Cook int yama_ptrace_traceme(struct task_struct *parent)
3269d8dad74SKees Cook {
327b1d9e6b0SCasey Schaufler 	int rc = 0;
3289d8dad74SKees Cook 
3299d8dad74SKees Cook 	/* Only disallow PTRACE_TRACEME on more aggressive settings. */
3309d8dad74SKees Cook 	switch (ptrace_scope) {
3319d8dad74SKees Cook 	case YAMA_SCOPE_CAPABILITY:
332eddc0a3aSEric W. Biederman 		if (!has_ns_capability(parent, current_user_ns(), CAP_SYS_PTRACE))
3339d8dad74SKees Cook 			rc = -EPERM;
3349d8dad74SKees Cook 		break;
3359d8dad74SKees Cook 	case YAMA_SCOPE_NO_ATTACH:
3369d8dad74SKees Cook 		rc = -EPERM;
3379d8dad74SKees Cook 		break;
3389d8dad74SKees Cook 	}
3399d8dad74SKees Cook 
3409d8dad74SKees Cook 	if (rc) {
3419d8dad74SKees Cook 		printk_ratelimited(KERN_NOTICE
3429d8dad74SKees Cook 			"ptraceme of pid %d was attempted by: %s (pid %d)\n",
3437612bfeeSKees Cook 			current->pid, parent->comm, parent->pid);
3449d8dad74SKees Cook 	}
3459d8dad74SKees Cook 
3469d8dad74SKees Cook 	return rc;
3479d8dad74SKees Cook }
3489d8dad74SKees Cook 
349b1d9e6b0SCasey Schaufler static struct security_hook_list yama_hooks[] = {
350e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, yama_ptrace_access_check),
351e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, yama_ptrace_traceme),
352e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_prctl, yama_task_prctl),
353e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_free, yama_task_free),
3542d514487SKees Cook };
355b1d9e6b0SCasey Schaufler 
3562d514487SKees Cook #ifdef CONFIG_SYSCTL
357389da25fSKees Cook static int yama_dointvec_minmax(struct ctl_table *table, int write,
358389da25fSKees Cook 				void __user *buffer, size_t *lenp, loff_t *ppos)
359389da25fSKees Cook {
36041a4695cSKees Cook 	struct ctl_table table_copy;
361389da25fSKees Cook 
362389da25fSKees Cook 	if (write && !capable(CAP_SYS_PTRACE))
363389da25fSKees Cook 		return -EPERM;
364389da25fSKees Cook 
365389da25fSKees Cook 	/* Lock the max value if it ever gets set. */
36641a4695cSKees Cook 	table_copy = *table;
36741a4695cSKees Cook 	if (*(int *)table_copy.data == *(int *)table_copy.extra2)
36841a4695cSKees Cook 		table_copy.extra1 = table_copy.extra2;
369389da25fSKees Cook 
37041a4695cSKees Cook 	return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos);
371389da25fSKees Cook }
372389da25fSKees Cook 
3732d514487SKees Cook static int zero;
374389da25fSKees Cook static int max_scope = YAMA_SCOPE_NO_ATTACH;
3752d514487SKees Cook 
3762d514487SKees Cook struct ctl_path yama_sysctl_path[] = {
3772d514487SKees Cook 	{ .procname = "kernel", },
3782d514487SKees Cook 	{ .procname = "yama", },
3792d514487SKees Cook 	{ }
3802d514487SKees Cook };
3812d514487SKees Cook 
3822d514487SKees Cook static struct ctl_table yama_sysctl_table[] = {
3832d514487SKees Cook 	{
3842d514487SKees Cook 		.procname       = "ptrace_scope",
3852d514487SKees Cook 		.data           = &ptrace_scope,
3862d514487SKees Cook 		.maxlen         = sizeof(int),
3872d514487SKees Cook 		.mode           = 0644,
388389da25fSKees Cook 		.proc_handler   = yama_dointvec_minmax,
3892d514487SKees Cook 		.extra1         = &zero,
390389da25fSKees Cook 		.extra2         = &max_scope,
3912d514487SKees Cook 	},
3922d514487SKees Cook 	{ }
3932d514487SKees Cook };
394730daa16SKees Cook static void __init yama_init_sysctl(void)
3952d514487SKees Cook {
3962d514487SKees Cook 	if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
3972d514487SKees Cook 		panic("Yama: sysctl registration failed.\n");
3982d514487SKees Cook }
399730daa16SKees Cook #else
400730daa16SKees Cook static inline void yama_init_sysctl(void) { }
401730daa16SKees Cook #endif /* CONFIG_SYSCTL */
4022d514487SKees Cook 
403730daa16SKees Cook void __init yama_add_hooks(void)
404730daa16SKees Cook {
405730daa16SKees Cook 	pr_info("Yama: becoming mindful.\n");
406730daa16SKees Cook 	security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks));
407730daa16SKees Cook 	yama_init_sysctl();
408730daa16SKees Cook }
409