xref: /openbmc/linux/security/yama/yama_lsm.c (revision bf06189e4d14641c0148bea16e9dd24943862215)
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 
152d514487SKees Cook #include <linux/security.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>
202d514487SKees Cook 
212d514487SKees Cook static int ptrace_scope = 1;
222d514487SKees Cook 
232d514487SKees Cook /* describe a ptrace relationship for potential exception */
242d514487SKees Cook struct ptrace_relation {
252d514487SKees Cook 	struct task_struct *tracer;
262d514487SKees Cook 	struct task_struct *tracee;
272d514487SKees Cook 	struct list_head node;
282d514487SKees Cook };
292d514487SKees Cook 
302d514487SKees Cook static LIST_HEAD(ptracer_relations);
312d514487SKees Cook static DEFINE_SPINLOCK(ptracer_relations_lock);
322d514487SKees Cook 
332d514487SKees Cook /**
342d514487SKees Cook  * yama_ptracer_add - add/replace an exception for this tracer/tracee pair
352d514487SKees Cook  * @tracer: the task_struct of the process doing the ptrace
362d514487SKees Cook  * @tracee: the task_struct of the process to be ptraced
372d514487SKees Cook  *
382d514487SKees Cook  * Each tracee can have, at most, one tracer registered. Each time this
392d514487SKees Cook  * is called, the prior registered tracer will be replaced for the tracee.
402d514487SKees Cook  *
412d514487SKees Cook  * Returns 0 if relationship was added, -ve on error.
422d514487SKees Cook  */
432d514487SKees Cook static int yama_ptracer_add(struct task_struct *tracer,
442d514487SKees Cook 			    struct task_struct *tracee)
452d514487SKees Cook {
462d514487SKees Cook 	int rc = 0;
472d514487SKees Cook 	struct ptrace_relation *added;
482d514487SKees Cook 	struct ptrace_relation *entry, *relation = NULL;
492d514487SKees Cook 
502d514487SKees Cook 	added = kmalloc(sizeof(*added), GFP_KERNEL);
512d514487SKees Cook 	if (!added)
522d514487SKees Cook 		return -ENOMEM;
532d514487SKees Cook 
542d514487SKees Cook 	spin_lock_bh(&ptracer_relations_lock);
552d514487SKees Cook 	list_for_each_entry(entry, &ptracer_relations, node)
562d514487SKees Cook 		if (entry->tracee == tracee) {
572d514487SKees Cook 			relation = entry;
582d514487SKees Cook 			break;
592d514487SKees Cook 		}
602d514487SKees Cook 	if (!relation) {
612d514487SKees Cook 		relation = added;
622d514487SKees Cook 		relation->tracee = tracee;
632d514487SKees Cook 		list_add(&relation->node, &ptracer_relations);
642d514487SKees Cook 	}
652d514487SKees Cook 	relation->tracer = tracer;
662d514487SKees Cook 
672d514487SKees Cook 	spin_unlock_bh(&ptracer_relations_lock);
682d514487SKees Cook 	if (added != relation)
692d514487SKees Cook 		kfree(added);
702d514487SKees Cook 
712d514487SKees Cook 	return rc;
722d514487SKees Cook }
732d514487SKees Cook 
742d514487SKees Cook /**
752d514487SKees Cook  * yama_ptracer_del - remove exceptions related to the given tasks
762d514487SKees Cook  * @tracer: remove any relation where tracer task matches
772d514487SKees Cook  * @tracee: remove any relation where tracee task matches
782d514487SKees Cook  */
792d514487SKees Cook static void yama_ptracer_del(struct task_struct *tracer,
802d514487SKees Cook 			     struct task_struct *tracee)
812d514487SKees Cook {
822d514487SKees Cook 	struct ptrace_relation *relation, *safe;
832d514487SKees Cook 
842d514487SKees Cook 	spin_lock_bh(&ptracer_relations_lock);
852d514487SKees Cook 	list_for_each_entry_safe(relation, safe, &ptracer_relations, node)
862d514487SKees Cook 		if (relation->tracee == tracee ||
87*bf06189eSKees Cook 		    (tracer && relation->tracer == tracer)) {
882d514487SKees Cook 			list_del(&relation->node);
892d514487SKees Cook 			kfree(relation);
902d514487SKees Cook 		}
912d514487SKees Cook 	spin_unlock_bh(&ptracer_relations_lock);
922d514487SKees Cook }
932d514487SKees Cook 
942d514487SKees Cook /**
952d514487SKees Cook  * yama_task_free - check for task_pid to remove from exception list
962d514487SKees Cook  * @task: task being removed
972d514487SKees Cook  */
982d514487SKees Cook static void yama_task_free(struct task_struct *task)
992d514487SKees Cook {
1002d514487SKees Cook 	yama_ptracer_del(task, task);
1012d514487SKees Cook }
1022d514487SKees Cook 
1032d514487SKees Cook /**
1042d514487SKees Cook  * yama_task_prctl - check for Yama-specific prctl operations
1052d514487SKees Cook  * @option: operation
1062d514487SKees Cook  * @arg2: argument
1072d514487SKees Cook  * @arg3: argument
1082d514487SKees Cook  * @arg4: argument
1092d514487SKees Cook  * @arg5: argument
1102d514487SKees Cook  *
1112d514487SKees Cook  * Return 0 on success, -ve on error.  -ENOSYS is returned when Yama
1122d514487SKees Cook  * does not handle the given option.
1132d514487SKees Cook  */
1142d514487SKees Cook static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
1152d514487SKees Cook 			   unsigned long arg4, unsigned long arg5)
1162d514487SKees Cook {
1172d514487SKees Cook 	int rc;
1182d514487SKees Cook 	struct task_struct *myself = current;
1192d514487SKees Cook 
1202d514487SKees Cook 	rc = cap_task_prctl(option, arg2, arg3, arg4, arg5);
1212d514487SKees Cook 	if (rc != -ENOSYS)
1222d514487SKees Cook 		return rc;
1232d514487SKees Cook 
1242d514487SKees Cook 	switch (option) {
1252d514487SKees Cook 	case PR_SET_PTRACER:
1262d514487SKees Cook 		/* Since a thread can call prctl(), find the group leader
1272d514487SKees Cook 		 * before calling _add() or _del() on it, since we want
1282d514487SKees Cook 		 * process-level granularity of control. The tracer group
1292d514487SKees Cook 		 * leader checking is handled later when walking the ancestry
1302d514487SKees Cook 		 * at the time of PTRACE_ATTACH check.
1312d514487SKees Cook 		 */
1322d514487SKees Cook 		rcu_read_lock();
1332d514487SKees Cook 		if (!thread_group_leader(myself))
1342d514487SKees Cook 			myself = rcu_dereference(myself->group_leader);
1352d514487SKees Cook 		get_task_struct(myself);
1362d514487SKees Cook 		rcu_read_unlock();
1372d514487SKees Cook 
1382d514487SKees Cook 		if (arg2 == 0) {
1392d514487SKees Cook 			yama_ptracer_del(NULL, myself);
1402d514487SKees Cook 			rc = 0;
141*bf06189eSKees Cook 		} else if (arg2 == PR_SET_PTRACER_ANY) {
142*bf06189eSKees Cook 			rc = yama_ptracer_add(NULL, myself);
1432d514487SKees Cook 		} else {
1442d514487SKees Cook 			struct task_struct *tracer;
1452d514487SKees Cook 
1462d514487SKees Cook 			rcu_read_lock();
1472d514487SKees Cook 			tracer = find_task_by_vpid(arg2);
1482d514487SKees Cook 			if (tracer)
1492d514487SKees Cook 				get_task_struct(tracer);
1502d514487SKees Cook 			else
1512d514487SKees Cook 				rc = -EINVAL;
1522d514487SKees Cook 			rcu_read_unlock();
1532d514487SKees Cook 
1542d514487SKees Cook 			if (tracer) {
1552d514487SKees Cook 				rc = yama_ptracer_add(tracer, myself);
1562d514487SKees Cook 				put_task_struct(tracer);
1572d514487SKees Cook 			}
1582d514487SKees Cook 		}
1592d514487SKees Cook 
1602d514487SKees Cook 		put_task_struct(myself);
1612d514487SKees Cook 		break;
1622d514487SKees Cook 	}
1632d514487SKees Cook 
1642d514487SKees Cook 	return rc;
1652d514487SKees Cook }
1662d514487SKees Cook 
1672d514487SKees Cook /**
1682d514487SKees Cook  * task_is_descendant - walk up a process family tree looking for a match
1692d514487SKees Cook  * @parent: the process to compare against while walking up from child
1702d514487SKees Cook  * @child: the process to start from while looking upwards for parent
1712d514487SKees Cook  *
1722d514487SKees Cook  * Returns 1 if child is a descendant of parent, 0 if not.
1732d514487SKees Cook  */
1742d514487SKees Cook static int task_is_descendant(struct task_struct *parent,
1752d514487SKees Cook 			      struct task_struct *child)
1762d514487SKees Cook {
1772d514487SKees Cook 	int rc = 0;
1782d514487SKees Cook 	struct task_struct *walker = child;
1792d514487SKees Cook 
1802d514487SKees Cook 	if (!parent || !child)
1812d514487SKees Cook 		return 0;
1822d514487SKees Cook 
1832d514487SKees Cook 	rcu_read_lock();
1842d514487SKees Cook 	if (!thread_group_leader(parent))
1852d514487SKees Cook 		parent = rcu_dereference(parent->group_leader);
1862d514487SKees Cook 	while (walker->pid > 0) {
1872d514487SKees Cook 		if (!thread_group_leader(walker))
1882d514487SKees Cook 			walker = rcu_dereference(walker->group_leader);
1892d514487SKees Cook 		if (walker == parent) {
1902d514487SKees Cook 			rc = 1;
1912d514487SKees Cook 			break;
1922d514487SKees Cook 		}
1932d514487SKees Cook 		walker = rcu_dereference(walker->real_parent);
1942d514487SKees Cook 	}
1952d514487SKees Cook 	rcu_read_unlock();
1962d514487SKees Cook 
1972d514487SKees Cook 	return rc;
1982d514487SKees Cook }
1992d514487SKees Cook 
2002d514487SKees Cook /**
2012d514487SKees Cook  * ptracer_exception_found - tracer registered as exception for this tracee
2022d514487SKees Cook  * @tracer: the task_struct of the process attempting ptrace
2032d514487SKees Cook  * @tracee: the task_struct of the process to be ptraced
2042d514487SKees Cook  *
2052d514487SKees Cook  * Returns 1 if tracer has is ptracer exception ancestor for tracee.
2062d514487SKees Cook  */
2072d514487SKees Cook static int ptracer_exception_found(struct task_struct *tracer,
2082d514487SKees Cook 				   struct task_struct *tracee)
2092d514487SKees Cook {
2102d514487SKees Cook 	int rc = 0;
2112d514487SKees Cook 	struct ptrace_relation *relation;
2122d514487SKees Cook 	struct task_struct *parent = NULL;
213*bf06189eSKees Cook 	bool found = false;
2142d514487SKees Cook 
2152d514487SKees Cook 	spin_lock_bh(&ptracer_relations_lock);
2162d514487SKees Cook 	rcu_read_lock();
2172d514487SKees Cook 	if (!thread_group_leader(tracee))
2182d514487SKees Cook 		tracee = rcu_dereference(tracee->group_leader);
2192d514487SKees Cook 	list_for_each_entry(relation, &ptracer_relations, node)
2202d514487SKees Cook 		if (relation->tracee == tracee) {
2212d514487SKees Cook 			parent = relation->tracer;
222*bf06189eSKees Cook 			found = true;
2232d514487SKees Cook 			break;
2242d514487SKees Cook 		}
2252d514487SKees Cook 
226*bf06189eSKees Cook 	if (found && (parent == NULL || task_is_descendant(parent, tracer)))
2272d514487SKees Cook 		rc = 1;
2282d514487SKees Cook 	rcu_read_unlock();
2292d514487SKees Cook 	spin_unlock_bh(&ptracer_relations_lock);
2302d514487SKees Cook 
2312d514487SKees Cook 	return rc;
2322d514487SKees Cook }
2332d514487SKees Cook 
2342d514487SKees Cook /**
2352d514487SKees Cook  * yama_ptrace_access_check - validate PTRACE_ATTACH calls
2362d514487SKees Cook  * @child: task that current task is attempting to ptrace
2372d514487SKees Cook  * @mode: ptrace attach mode
2382d514487SKees Cook  *
2392d514487SKees Cook  * Returns 0 if following the ptrace is allowed, -ve on error.
2402d514487SKees Cook  */
2412d514487SKees Cook static int yama_ptrace_access_check(struct task_struct *child,
2422d514487SKees Cook 				    unsigned int mode)
2432d514487SKees Cook {
2442d514487SKees Cook 	int rc;
2452d514487SKees Cook 
2462d514487SKees Cook 	/* If standard caps disallows it, so does Yama.  We should
2472d514487SKees Cook 	 * only tighten restrictions further.
2482d514487SKees Cook 	 */
2492d514487SKees Cook 	rc = cap_ptrace_access_check(child, mode);
2502d514487SKees Cook 	if (rc)
2512d514487SKees Cook 		return rc;
2522d514487SKees Cook 
2532d514487SKees Cook 	/* require ptrace target be a child of ptracer on attach */
2542d514487SKees Cook 	if (mode == PTRACE_MODE_ATTACH &&
2552d514487SKees Cook 	    ptrace_scope &&
2562d514487SKees Cook 	    !task_is_descendant(current, child) &&
2572d514487SKees Cook 	    !ptracer_exception_found(current, child) &&
2582d514487SKees Cook 	    !capable(CAP_SYS_PTRACE))
2592d514487SKees Cook 		rc = -EPERM;
2602d514487SKees Cook 
2612d514487SKees Cook 	if (rc) {
2622d514487SKees Cook 		char name[sizeof(current->comm)];
2632d514487SKees Cook 		printk_ratelimited(KERN_NOTICE "ptrace of non-child"
2642d514487SKees Cook 			" pid %d was attempted by: %s (pid %d)\n",
2652d514487SKees Cook 			child->pid,
2662d514487SKees Cook 			get_task_comm(name, current),
2672d514487SKees Cook 			current->pid);
2682d514487SKees Cook 	}
2692d514487SKees Cook 
2702d514487SKees Cook 	return rc;
2712d514487SKees Cook }
2722d514487SKees Cook 
2732d514487SKees Cook static struct security_operations yama_ops = {
2742d514487SKees Cook 	.name =			"yama",
2752d514487SKees Cook 
2762d514487SKees Cook 	.ptrace_access_check =	yama_ptrace_access_check,
2772d514487SKees Cook 	.task_prctl =		yama_task_prctl,
2782d514487SKees Cook 	.task_free =		yama_task_free,
2792d514487SKees Cook };
2802d514487SKees Cook 
2812d514487SKees Cook #ifdef CONFIG_SYSCTL
2822d514487SKees Cook static int zero;
2832d514487SKees Cook static int one = 1;
2842d514487SKees Cook 
2852d514487SKees Cook struct ctl_path yama_sysctl_path[] = {
2862d514487SKees Cook 	{ .procname = "kernel", },
2872d514487SKees Cook 	{ .procname = "yama", },
2882d514487SKees Cook 	{ }
2892d514487SKees Cook };
2902d514487SKees Cook 
2912d514487SKees Cook static struct ctl_table yama_sysctl_table[] = {
2922d514487SKees Cook 	{
2932d514487SKees Cook 		.procname       = "ptrace_scope",
2942d514487SKees Cook 		.data           = &ptrace_scope,
2952d514487SKees Cook 		.maxlen         = sizeof(int),
2962d514487SKees Cook 		.mode           = 0644,
2972d514487SKees Cook 		.proc_handler   = proc_dointvec_minmax,
2982d514487SKees Cook 		.extra1         = &zero,
2992d514487SKees Cook 		.extra2         = &one,
3002d514487SKees Cook 	},
3012d514487SKees Cook 	{ }
3022d514487SKees Cook };
3032d514487SKees Cook #endif /* CONFIG_SYSCTL */
3042d514487SKees Cook 
3052d514487SKees Cook static __init int yama_init(void)
3062d514487SKees Cook {
3072d514487SKees Cook 	if (!security_module_enable(&yama_ops))
3082d514487SKees Cook 		return 0;
3092d514487SKees Cook 
3102d514487SKees Cook 	printk(KERN_INFO "Yama: becoming mindful.\n");
3112d514487SKees Cook 
3122d514487SKees Cook 	if (register_security(&yama_ops))
3132d514487SKees Cook 		panic("Yama: kernel registration failed.\n");
3142d514487SKees Cook 
3152d514487SKees Cook #ifdef CONFIG_SYSCTL
3162d514487SKees Cook 	if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
3172d514487SKees Cook 		panic("Yama: sysctl registration failed.\n");
3182d514487SKees Cook #endif
3192d514487SKees Cook 
3202d514487SKees Cook 	return 0;
3212d514487SKees Cook }
3222d514487SKees Cook 
3232d514487SKees Cook security_initcall(yama_init);
324