xref: /linux/security/yama/yama_lsm.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22d514487SKees Cook /*
32d514487SKees Cook  * Yama Linux Security Module
42d514487SKees Cook  *
52d514487SKees Cook  * Author: Kees Cook <keescook@chromium.org>
62d514487SKees Cook  *
72d514487SKees Cook  * Copyright (C) 2010 Canonical, Ltd.
82d514487SKees Cook  * Copyright (C) 2011 The Chromium OS Authors.
92d514487SKees Cook  */
102d514487SKees Cook 
113c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h>
122d514487SKees Cook #include <linux/sysctl.h>
132d514487SKees Cook #include <linux/ptrace.h>
142d514487SKees Cook #include <linux/prctl.h>
152d514487SKees Cook #include <linux/ratelimit.h>
16235e7527SKees Cook #include <linux/workqueue.h>
178a56038cSKees Cook #include <linux/string_helpers.h>
18dca6b414SJann Horn #include <linux/task_work.h>
19dca6b414SJann Horn #include <linux/sched.h>
20dca6b414SJann Horn #include <linux/spinlock.h>
21f3b8788cSCasey Schaufler #include <uapi/linux/lsm.h>
222d514487SKees Cook 
23389da25fSKees Cook #define YAMA_SCOPE_DISABLED	0
24389da25fSKees Cook #define YAMA_SCOPE_RELATIONAL	1
25389da25fSKees Cook #define YAMA_SCOPE_CAPABILITY	2
26389da25fSKees Cook #define YAMA_SCOPE_NO_ATTACH	3
27389da25fSKees Cook 
28389da25fSKees Cook static int ptrace_scope = YAMA_SCOPE_RELATIONAL;
292d514487SKees Cook 
302d514487SKees Cook /* describe a ptrace relationship for potential exception */
312d514487SKees Cook struct ptrace_relation {
322d514487SKees Cook 	struct task_struct *tracer;
332d514487SKees Cook 	struct task_struct *tracee;
34235e7527SKees Cook 	bool invalid;
352d514487SKees Cook 	struct list_head node;
3693b69d43SKees Cook 	struct rcu_head rcu;
372d514487SKees Cook };
382d514487SKees Cook 
392d514487SKees Cook static LIST_HEAD(ptracer_relations);
402d514487SKees Cook static DEFINE_SPINLOCK(ptracer_relations_lock);
412d514487SKees Cook 
42235e7527SKees Cook static void yama_relation_cleanup(struct work_struct *work);
43235e7527SKees Cook static DECLARE_WORK(yama_relation_work, yama_relation_cleanup);
44235e7527SKees Cook 
45dca6b414SJann Horn struct access_report_info {
46dca6b414SJann Horn 	struct callback_head work;
47dca6b414SJann Horn 	const char *access;
48dca6b414SJann Horn 	struct task_struct *target;
49dca6b414SJann Horn 	struct task_struct *agent;
50dca6b414SJann Horn };
51dca6b414SJann Horn 
__report_access(struct callback_head * work)52dca6b414SJann Horn static void __report_access(struct callback_head *work)
538a56038cSKees Cook {
54dca6b414SJann Horn 	struct access_report_info *info =
55dca6b414SJann Horn 		container_of(work, struct access_report_info, work);
568a56038cSKees Cook 	char *target_cmd, *agent_cmd;
578a56038cSKees Cook 
58dca6b414SJann Horn 	target_cmd = kstrdup_quotable_cmdline(info->target, GFP_KERNEL);
59dca6b414SJann Horn 	agent_cmd = kstrdup_quotable_cmdline(info->agent, GFP_KERNEL);
608a56038cSKees Cook 
618a56038cSKees Cook 	pr_notice_ratelimited(
628a56038cSKees Cook 		"ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n",
63dca6b414SJann Horn 		info->access, target_cmd, info->target->pid, agent_cmd,
64dca6b414SJann Horn 		info->agent->pid);
658a56038cSKees Cook 
668a56038cSKees Cook 	kfree(agent_cmd);
678a56038cSKees Cook 	kfree(target_cmd);
68dca6b414SJann Horn 
69dca6b414SJann Horn 	put_task_struct(info->agent);
70dca6b414SJann Horn 	put_task_struct(info->target);
71dca6b414SJann Horn 	kfree(info);
72dca6b414SJann Horn }
73dca6b414SJann Horn 
74dca6b414SJann Horn /* defers execution because cmdline access can sleep */
report_access(const char * access,struct task_struct * target,struct task_struct * agent)75dca6b414SJann Horn static void report_access(const char *access, struct task_struct *target,
76dca6b414SJann Horn 				struct task_struct *agent)
77dca6b414SJann Horn {
78dca6b414SJann Horn 	struct access_report_info *info;
79dca6b414SJann Horn 	char agent_comm[sizeof(agent->comm)];
80dca6b414SJann Horn 
81dca6b414SJann Horn 	assert_spin_locked(&target->alloc_lock); /* for target->comm */
82dca6b414SJann Horn 
83dca6b414SJann Horn 	if (current->flags & PF_KTHREAD) {
84dca6b414SJann Horn 		/* I don't think kthreads call task_work_run() before exiting.
85dca6b414SJann Horn 		 * Imagine angry ranting about procfs here.
86dca6b414SJann Horn 		 */
87dca6b414SJann Horn 		pr_notice_ratelimited(
88dca6b414SJann Horn 		    "ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n",
89dca6b414SJann Horn 		    access, target->comm, target->pid,
90dca6b414SJann Horn 		    get_task_comm(agent_comm, agent), agent->pid);
91dca6b414SJann Horn 		return;
92dca6b414SJann Horn 	}
93dca6b414SJann Horn 
94dca6b414SJann Horn 	info = kmalloc(sizeof(*info), GFP_ATOMIC);
95dca6b414SJann Horn 	if (!info)
96dca6b414SJann Horn 		return;
97dca6b414SJann Horn 	init_task_work(&info->work, __report_access);
98dca6b414SJann Horn 	get_task_struct(target);
99dca6b414SJann Horn 	get_task_struct(agent);
100dca6b414SJann Horn 	info->access = access;
101dca6b414SJann Horn 	info->target = target;
102dca6b414SJann Horn 	info->agent = agent;
10391989c70SJens Axboe 	if (task_work_add(current, &info->work, TWA_RESUME) == 0)
104dca6b414SJann Horn 		return; /* success */
105dca6b414SJann Horn 
106dca6b414SJann Horn 	WARN(1, "report_access called from exiting task");
107dca6b414SJann Horn 	put_task_struct(target);
108dca6b414SJann Horn 	put_task_struct(agent);
109dca6b414SJann Horn 	kfree(info);
1108a56038cSKees Cook }
1118a56038cSKees Cook 
112235e7527SKees Cook /**
113235e7527SKees Cook  * yama_relation_cleanup - remove invalid entries from the relation list
114f7d3b1ffSChristian Göttsche  * @work: unused
115235e7527SKees Cook  *
116235e7527SKees Cook  */
yama_relation_cleanup(struct work_struct * work)117235e7527SKees Cook static void yama_relation_cleanup(struct work_struct *work)
118235e7527SKees Cook {
119235e7527SKees Cook 	struct ptrace_relation *relation;
120235e7527SKees Cook 
121235e7527SKees Cook 	spin_lock(&ptracer_relations_lock);
122235e7527SKees Cook 	rcu_read_lock();
123235e7527SKees Cook 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
124235e7527SKees Cook 		if (relation->invalid) {
125235e7527SKees Cook 			list_del_rcu(&relation->node);
126235e7527SKees Cook 			kfree_rcu(relation, rcu);
127235e7527SKees Cook 		}
128235e7527SKees Cook 	}
129235e7527SKees Cook 	rcu_read_unlock();
130235e7527SKees Cook 	spin_unlock(&ptracer_relations_lock);
131235e7527SKees Cook }
132235e7527SKees Cook 
1332d514487SKees Cook /**
1342d514487SKees Cook  * yama_ptracer_add - add/replace an exception for this tracer/tracee pair
1352d514487SKees Cook  * @tracer: the task_struct of the process doing the ptrace
1362d514487SKees Cook  * @tracee: the task_struct of the process to be ptraced
1372d514487SKees Cook  *
1382d514487SKees Cook  * Each tracee can have, at most, one tracer registered. Each time this
1392d514487SKees Cook  * is called, the prior registered tracer will be replaced for the tracee.
1402d514487SKees Cook  *
1412d514487SKees Cook  * Returns 0 if relationship was added, -ve on error.
1422d514487SKees Cook  */
yama_ptracer_add(struct task_struct * tracer,struct task_struct * tracee)1432d514487SKees Cook static int yama_ptracer_add(struct task_struct *tracer,
1442d514487SKees Cook 			    struct task_struct *tracee)
1452d514487SKees Cook {
14693b69d43SKees Cook 	struct ptrace_relation *relation, *added;
1472d514487SKees Cook 
1482d514487SKees Cook 	added = kmalloc(sizeof(*added), GFP_KERNEL);
1492d514487SKees Cook 	if (!added)
1502d514487SKees Cook 		return -ENOMEM;
1512d514487SKees Cook 
15293b69d43SKees Cook 	added->tracee = tracee;
15393b69d43SKees Cook 	added->tracer = tracer;
154235e7527SKees Cook 	added->invalid = false;
15593b69d43SKees Cook 
156235e7527SKees Cook 	spin_lock(&ptracer_relations_lock);
15793b69d43SKees Cook 	rcu_read_lock();
15893b69d43SKees Cook 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
159235e7527SKees Cook 		if (relation->invalid)
160235e7527SKees Cook 			continue;
16193b69d43SKees Cook 		if (relation->tracee == tracee) {
16293b69d43SKees Cook 			list_replace_rcu(&relation->node, &added->node);
16393b69d43SKees Cook 			kfree_rcu(relation, rcu);
16493b69d43SKees Cook 			goto out;
1652d514487SKees Cook 		}
1662d514487SKees Cook 	}
1672d514487SKees Cook 
16893b69d43SKees Cook 	list_add_rcu(&added->node, &ptracer_relations);
16993b69d43SKees Cook 
17093b69d43SKees Cook out:
17193b69d43SKees Cook 	rcu_read_unlock();
172235e7527SKees Cook 	spin_unlock(&ptracer_relations_lock);
17393b69d43SKees Cook 	return 0;
1742d514487SKees Cook }
1752d514487SKees Cook 
1762d514487SKees Cook /**
1772d514487SKees Cook  * yama_ptracer_del - remove exceptions related to the given tasks
1782d514487SKees Cook  * @tracer: remove any relation where tracer task matches
1792d514487SKees Cook  * @tracee: remove any relation where tracee task matches
1802d514487SKees Cook  */
yama_ptracer_del(struct task_struct * tracer,struct task_struct * tracee)1812d514487SKees Cook static void yama_ptracer_del(struct task_struct *tracer,
1822d514487SKees Cook 			     struct task_struct *tracee)
1832d514487SKees Cook {
18493b69d43SKees Cook 	struct ptrace_relation *relation;
185235e7527SKees Cook 	bool marked = false;
1862d514487SKees Cook 
18793b69d43SKees Cook 	rcu_read_lock();
18893b69d43SKees Cook 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
189235e7527SKees Cook 		if (relation->invalid)
190235e7527SKees Cook 			continue;
1912d514487SKees Cook 		if (relation->tracee == tracee ||
192bf06189eSKees Cook 		    (tracer && relation->tracer == tracer)) {
193235e7527SKees Cook 			relation->invalid = true;
194235e7527SKees Cook 			marked = true;
1952d514487SKees Cook 		}
19693b69d43SKees Cook 	}
19793b69d43SKees Cook 	rcu_read_unlock();
198235e7527SKees Cook 
199235e7527SKees Cook 	if (marked)
200235e7527SKees Cook 		schedule_work(&yama_relation_work);
2012d514487SKees Cook }
2022d514487SKees Cook 
2032d514487SKees Cook /**
2042d514487SKees Cook  * yama_task_free - check for task_pid to remove from exception list
2052d514487SKees Cook  * @task: task being removed
2062d514487SKees Cook  */
yama_task_free(struct task_struct * task)2071aa176efSJann Horn static void yama_task_free(struct task_struct *task)
2082d514487SKees Cook {
2092d514487SKees Cook 	yama_ptracer_del(task, task);
2102d514487SKees Cook }
2112d514487SKees Cook 
2122d514487SKees Cook /**
2132d514487SKees Cook  * yama_task_prctl - check for Yama-specific prctl operations
2142d514487SKees Cook  * @option: operation
2152d514487SKees Cook  * @arg2: argument
2162d514487SKees Cook  * @arg3: argument
2172d514487SKees Cook  * @arg4: argument
2182d514487SKees Cook  * @arg5: argument
2192d514487SKees Cook  *
2202d514487SKees Cook  * Return 0 on success, -ve on error.  -ENOSYS is returned when Yama
2212d514487SKees Cook  * does not handle the given option.
2222d514487SKees Cook  */
yama_task_prctl(int option,unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5)2231aa176efSJann Horn static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
2242d514487SKees Cook 			   unsigned long arg4, unsigned long arg5)
2252d514487SKees Cook {
226b1d9e6b0SCasey Schaufler 	int rc = -ENOSYS;
2272d514487SKees Cook 	struct task_struct *myself = current;
2282d514487SKees Cook 
2292d514487SKees Cook 	switch (option) {
2302d514487SKees Cook 	case PR_SET_PTRACER:
2312d514487SKees Cook 		/* Since a thread can call prctl(), find the group leader
2322d514487SKees Cook 		 * before calling _add() or _del() on it, since we want
2332d514487SKees Cook 		 * process-level granularity of control. The tracer group
2342d514487SKees Cook 		 * leader checking is handled later when walking the ancestry
2352d514487SKees Cook 		 * at the time of PTRACE_ATTACH check.
2362d514487SKees Cook 		 */
2372d514487SKees Cook 		rcu_read_lock();
2382d514487SKees Cook 		if (!thread_group_leader(myself))
2392d514487SKees Cook 			myself = rcu_dereference(myself->group_leader);
2402d514487SKees Cook 		get_task_struct(myself);
2412d514487SKees Cook 		rcu_read_unlock();
2422d514487SKees Cook 
2432d514487SKees Cook 		if (arg2 == 0) {
2442d514487SKees Cook 			yama_ptracer_del(NULL, myself);
2452d514487SKees Cook 			rc = 0;
2462e4930ebSKees Cook 		} else if (arg2 == PR_SET_PTRACER_ANY || (int)arg2 == -1) {
247bf06189eSKees Cook 			rc = yama_ptracer_add(NULL, myself);
2482d514487SKees Cook 		} else {
2492d514487SKees Cook 			struct task_struct *tracer;
2502d514487SKees Cook 
2512ee08260SMike Rapoport 			tracer = find_get_task_by_vpid(arg2);
2522ee08260SMike Rapoport 			if (!tracer) {
2532d514487SKees Cook 				rc = -EINVAL;
2542ee08260SMike Rapoport 			} else {
2552d514487SKees Cook 				rc = yama_ptracer_add(tracer, myself);
2562d514487SKees Cook 				put_task_struct(tracer);
2572d514487SKees Cook 			}
2582d514487SKees Cook 		}
2592d514487SKees Cook 
2602d514487SKees Cook 		put_task_struct(myself);
2612d514487SKees Cook 		break;
2622d514487SKees Cook 	}
2632d514487SKees Cook 
2642d514487SKees Cook 	return rc;
2652d514487SKees Cook }
2662d514487SKees Cook 
2672d514487SKees Cook /**
2682d514487SKees Cook  * task_is_descendant - walk up a process family tree looking for a match
2692d514487SKees Cook  * @parent: the process to compare against while walking up from child
2702d514487SKees Cook  * @child: the process to start from while looking upwards for parent
2712d514487SKees Cook  *
2722d514487SKees Cook  * Returns 1 if child is a descendant of parent, 0 if not.
2732d514487SKees Cook  */
task_is_descendant(struct task_struct * parent,struct task_struct * child)2742d514487SKees Cook static int task_is_descendant(struct task_struct *parent,
2752d514487SKees Cook 			      struct task_struct *child)
2762d514487SKees Cook {
2772d514487SKees Cook 	int rc = 0;
2782d514487SKees Cook 	struct task_struct *walker = child;
2792d514487SKees Cook 
2802d514487SKees Cook 	if (!parent || !child)
2812d514487SKees Cook 		return 0;
2822d514487SKees Cook 
2832d514487SKees Cook 	rcu_read_lock();
2842d514487SKees Cook 	if (!thread_group_leader(parent))
2852d514487SKees Cook 		parent = rcu_dereference(parent->group_leader);
2862d514487SKees Cook 	while (walker->pid > 0) {
2872d514487SKees Cook 		if (!thread_group_leader(walker))
2882d514487SKees Cook 			walker = rcu_dereference(walker->group_leader);
2892d514487SKees Cook 		if (walker == parent) {
2902d514487SKees Cook 			rc = 1;
2912d514487SKees Cook 			break;
2922d514487SKees Cook 		}
2932d514487SKees Cook 		walker = rcu_dereference(walker->real_parent);
2942d514487SKees Cook 	}
2952d514487SKees Cook 	rcu_read_unlock();
2962d514487SKees Cook 
2972d514487SKees Cook 	return rc;
2982d514487SKees Cook }
2992d514487SKees Cook 
3002d514487SKees Cook /**
3012d514487SKees Cook  * ptracer_exception_found - tracer registered as exception for this tracee
3022d514487SKees Cook  * @tracer: the task_struct of the process attempting ptrace
3032d514487SKees Cook  * @tracee: the task_struct of the process to be ptraced
3042d514487SKees Cook  *
30550523a29SJosh Stone  * Returns 1 if tracer has a ptracer exception ancestor for tracee.
3062d514487SKees Cook  */
ptracer_exception_found(struct task_struct * tracer,struct task_struct * tracee)3072d514487SKees Cook static int ptracer_exception_found(struct task_struct *tracer,
3082d514487SKees Cook 				   struct task_struct *tracee)
3092d514487SKees Cook {
3102d514487SKees Cook 	int rc = 0;
3112d514487SKees Cook 	struct ptrace_relation *relation;
3122d514487SKees Cook 	struct task_struct *parent = NULL;
313bf06189eSKees Cook 	bool found = false;
3142d514487SKees Cook 
3152d514487SKees Cook 	rcu_read_lock();
31650523a29SJosh Stone 
31750523a29SJosh Stone 	/*
31850523a29SJosh Stone 	 * If there's already an active tracing relationship, then make an
31950523a29SJosh Stone 	 * exception for the sake of other accesses, like process_vm_rw().
32050523a29SJosh Stone 	 */
32150523a29SJosh Stone 	parent = ptrace_parent(tracee);
32250523a29SJosh Stone 	if (parent != NULL && same_thread_group(parent, tracer)) {
32350523a29SJosh Stone 		rc = 1;
32450523a29SJosh Stone 		goto unlock;
32550523a29SJosh Stone 	}
32650523a29SJosh Stone 
32750523a29SJosh Stone 	/* Look for a PR_SET_PTRACER relationship. */
3282d514487SKees Cook 	if (!thread_group_leader(tracee))
3292d514487SKees Cook 		tracee = rcu_dereference(tracee->group_leader);
330235e7527SKees Cook 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
331235e7527SKees Cook 		if (relation->invalid)
332235e7527SKees Cook 			continue;
3332d514487SKees Cook 		if (relation->tracee == tracee) {
3342d514487SKees Cook 			parent = relation->tracer;
335bf06189eSKees Cook 			found = true;
3362d514487SKees Cook 			break;
3372d514487SKees Cook 		}
338235e7527SKees Cook 	}
3392d514487SKees Cook 
340bf06189eSKees Cook 	if (found && (parent == NULL || task_is_descendant(parent, tracer)))
3412d514487SKees Cook 		rc = 1;
34250523a29SJosh Stone 
34350523a29SJosh Stone unlock:
3442d514487SKees Cook 	rcu_read_unlock();
3452d514487SKees Cook 
3462d514487SKees Cook 	return rc;
3472d514487SKees Cook }
3482d514487SKees Cook 
3492d514487SKees Cook /**
3502d514487SKees Cook  * yama_ptrace_access_check - validate PTRACE_ATTACH calls
3512d514487SKees Cook  * @child: task that current task is attempting to ptrace
3522d514487SKees Cook  * @mode: ptrace attach mode
3532d514487SKees Cook  *
3542d514487SKees Cook  * Returns 0 if following the ptrace is allowed, -ve on error.
3552d514487SKees Cook  */
yama_ptrace_access_check(struct task_struct * child,unsigned int mode)356b1d9e6b0SCasey Schaufler static int yama_ptrace_access_check(struct task_struct *child,
3572d514487SKees Cook 				    unsigned int mode)
3582d514487SKees Cook {
359b1d9e6b0SCasey Schaufler 	int rc = 0;
3602d514487SKees Cook 
3612d514487SKees Cook 	/* require ptrace target be a child of ptracer on attach */
3623dfb7d8cSJann Horn 	if (mode & PTRACE_MODE_ATTACH) {
363389da25fSKees Cook 		switch (ptrace_scope) {
364389da25fSKees Cook 		case YAMA_SCOPE_DISABLED:
365389da25fSKees Cook 			/* No additional restrictions. */
366389da25fSKees Cook 			break;
367389da25fSKees Cook 		case YAMA_SCOPE_RELATIONAL:
3684c44aaafSEric W. Biederman 			rcu_read_lock();
3699474f4e7SKees Cook 			if (!pid_alive(child))
3709474f4e7SKees Cook 				rc = -EPERM;
3719474f4e7SKees Cook 			if (!rc && !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  */
yama_ptrace_traceme(struct task_struct * parent)4021aa176efSJann Horn static 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 
426b1a867eeSPaul Moore static const struct lsm_id yama_lsmid = {
427f3b8788cSCasey Schaufler 	.name = "yama",
428f3b8788cSCasey Schaufler 	.id = LSM_ID_YAMA,
429f3b8788cSCasey Schaufler };
430f3b8788cSCasey Schaufler 
431f22f9aafSPaul Moore static struct security_hook_list yama_hooks[] __ro_after_init = {
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
yama_dointvec_minmax(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)439*78eb4ea2SJoel Granados static int yama_dointvec_minmax(const struct ctl_table *table, int write,
44032927393SChristoph Hellwig 				void *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 
455389da25fSKees Cook static int max_scope = YAMA_SCOPE_NO_ATTACH;
4562d514487SKees Cook 
4572d514487SKees Cook static struct ctl_table yama_sysctl_table[] = {
4582d514487SKees Cook 	{
4592d514487SKees Cook 		.procname       = "ptrace_scope",
4602d514487SKees Cook 		.data           = &ptrace_scope,
4612d514487SKees Cook 		.maxlen         = sizeof(int),
4622d514487SKees Cook 		.mode           = 0644,
463389da25fSKees Cook 		.proc_handler   = yama_dointvec_minmax,
464eec4844fSMatteo Croce 		.extra1         = SYSCTL_ZERO,
465389da25fSKees Cook 		.extra2         = &max_scope,
4662d514487SKees Cook 	},
4672d514487SKees Cook };
yama_init_sysctl(void)468730daa16SKees Cook static void __init yama_init_sysctl(void)
4692d514487SKees Cook {
47098cfeb8dSLuis Chamberlain 	if (!register_sysctl("kernel/yama", yama_sysctl_table))
4712d514487SKees Cook 		panic("Yama: sysctl registration failed.\n");
4722d514487SKees Cook }
473730daa16SKees Cook #else
yama_init_sysctl(void)474730daa16SKees Cook static inline void yama_init_sysctl(void) { }
475730daa16SKees Cook #endif /* CONFIG_SYSCTL */
4762d514487SKees Cook 
yama_init(void)477d6aed64bSKees Cook static int __init yama_init(void)
478730daa16SKees Cook {
479730daa16SKees Cook 	pr_info("Yama: becoming mindful.\n");
480f3b8788cSCasey Schaufler 	security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks), &yama_lsmid);
481730daa16SKees Cook 	yama_init_sysctl();
482d6aed64bSKees Cook 	return 0;
483730daa16SKees Cook }
484d6aed64bSKees Cook 
485d6aed64bSKees Cook DEFINE_LSM(yama) = {
486d6aed64bSKees Cook 	.name = "yama",
487d6aed64bSKees Cook 	.init = yama_init,
488d6aed64bSKees Cook };
489