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>
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
44dca6b414SJann Horn struct access_report_info {
45dca6b414SJann Horn struct callback_head work;
46dca6b414SJann Horn const char *access;
47dca6b414SJann Horn struct task_struct *target;
48dca6b414SJann Horn struct task_struct *agent;
49dca6b414SJann Horn };
50dca6b414SJann Horn
__report_access(struct callback_head * work)51dca6b414SJann Horn static void __report_access(struct callback_head *work)
528a56038cSKees Cook {
53dca6b414SJann Horn struct access_report_info *info =
54dca6b414SJann Horn container_of(work, struct access_report_info, work);
558a56038cSKees Cook char *target_cmd, *agent_cmd;
568a56038cSKees Cook
57dca6b414SJann Horn target_cmd = kstrdup_quotable_cmdline(info->target, GFP_KERNEL);
58dca6b414SJann Horn agent_cmd = kstrdup_quotable_cmdline(info->agent, GFP_KERNEL);
598a56038cSKees Cook
608a56038cSKees Cook pr_notice_ratelimited(
618a56038cSKees Cook "ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n",
62dca6b414SJann Horn info->access, target_cmd, info->target->pid, agent_cmd,
63dca6b414SJann Horn info->agent->pid);
648a56038cSKees Cook
658a56038cSKees Cook kfree(agent_cmd);
668a56038cSKees Cook kfree(target_cmd);
67dca6b414SJann Horn
68dca6b414SJann Horn put_task_struct(info->agent);
69dca6b414SJann Horn put_task_struct(info->target);
70dca6b414SJann Horn kfree(info);
71dca6b414SJann Horn }
72dca6b414SJann Horn
73dca6b414SJann Horn /* defers execution because cmdline access can sleep */
report_access(const char * access,struct task_struct * target,struct task_struct * agent)74dca6b414SJann Horn static void report_access(const char *access, struct task_struct *target,
75dca6b414SJann Horn struct task_struct *agent)
76dca6b414SJann Horn {
77dca6b414SJann Horn struct access_report_info *info;
78dca6b414SJann Horn char agent_comm[sizeof(agent->comm)];
79dca6b414SJann Horn
80dca6b414SJann Horn assert_spin_locked(&target->alloc_lock); /* for target->comm */
81dca6b414SJann Horn
82dca6b414SJann Horn if (current->flags & PF_KTHREAD) {
83dca6b414SJann Horn /* I don't think kthreads call task_work_run() before exiting.
84dca6b414SJann Horn * Imagine angry ranting about procfs here.
85dca6b414SJann Horn */
86dca6b414SJann Horn pr_notice_ratelimited(
87dca6b414SJann Horn "ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n",
88dca6b414SJann Horn access, target->comm, target->pid,
89dca6b414SJann Horn get_task_comm(agent_comm, agent), agent->pid);
90dca6b414SJann Horn return;
91dca6b414SJann Horn }
92dca6b414SJann Horn
93dca6b414SJann Horn info = kmalloc(sizeof(*info), GFP_ATOMIC);
94dca6b414SJann Horn if (!info)
95dca6b414SJann Horn return;
96dca6b414SJann Horn init_task_work(&info->work, __report_access);
97dca6b414SJann Horn get_task_struct(target);
98dca6b414SJann Horn get_task_struct(agent);
99dca6b414SJann Horn info->access = access;
100dca6b414SJann Horn info->target = target;
101dca6b414SJann Horn info->agent = agent;
10291989c70SJens Axboe if (task_work_add(current, &info->work, TWA_RESUME) == 0)
103dca6b414SJann Horn return; /* success */
104dca6b414SJann Horn
105dca6b414SJann Horn WARN(1, "report_access called from exiting task");
106dca6b414SJann Horn put_task_struct(target);
107dca6b414SJann Horn put_task_struct(agent);
108dca6b414SJann Horn kfree(info);
1098a56038cSKees Cook }
1108a56038cSKees Cook
111235e7527SKees Cook /**
112235e7527SKees Cook * yama_relation_cleanup - remove invalid entries from the relation list
113235e7527SKees Cook *
114235e7527SKees Cook */
yama_relation_cleanup(struct work_struct * work)115235e7527SKees Cook static void yama_relation_cleanup(struct work_struct *work)
116235e7527SKees Cook {
117235e7527SKees Cook struct ptrace_relation *relation;
118235e7527SKees Cook
119235e7527SKees Cook spin_lock(&ptracer_relations_lock);
120235e7527SKees Cook rcu_read_lock();
121235e7527SKees Cook list_for_each_entry_rcu(relation, &ptracer_relations, node) {
122235e7527SKees Cook if (relation->invalid) {
123235e7527SKees Cook list_del_rcu(&relation->node);
124235e7527SKees Cook kfree_rcu(relation, rcu);
125235e7527SKees Cook }
126235e7527SKees Cook }
127235e7527SKees Cook rcu_read_unlock();
128235e7527SKees Cook spin_unlock(&ptracer_relations_lock);
129235e7527SKees Cook }
130235e7527SKees Cook
1312d514487SKees Cook /**
1322d514487SKees Cook * yama_ptracer_add - add/replace an exception for this tracer/tracee pair
1332d514487SKees Cook * @tracer: the task_struct of the process doing the ptrace
1342d514487SKees Cook * @tracee: the task_struct of the process to be ptraced
1352d514487SKees Cook *
1362d514487SKees Cook * Each tracee can have, at most, one tracer registered. Each time this
1372d514487SKees Cook * is called, the prior registered tracer will be replaced for the tracee.
1382d514487SKees Cook *
1392d514487SKees Cook * Returns 0 if relationship was added, -ve on error.
1402d514487SKees Cook */
yama_ptracer_add(struct task_struct * tracer,struct task_struct * tracee)1412d514487SKees Cook static int yama_ptracer_add(struct task_struct *tracer,
1422d514487SKees Cook struct task_struct *tracee)
1432d514487SKees Cook {
14493b69d43SKees Cook struct ptrace_relation *relation, *added;
1452d514487SKees Cook
1462d514487SKees Cook added = kmalloc(sizeof(*added), GFP_KERNEL);
1472d514487SKees Cook if (!added)
1482d514487SKees Cook return -ENOMEM;
1492d514487SKees Cook
15093b69d43SKees Cook added->tracee = tracee;
15193b69d43SKees Cook added->tracer = tracer;
152235e7527SKees Cook added->invalid = false;
15393b69d43SKees Cook
154235e7527SKees Cook spin_lock(&ptracer_relations_lock);
15593b69d43SKees Cook rcu_read_lock();
15693b69d43SKees Cook list_for_each_entry_rcu(relation, &ptracer_relations, node) {
157235e7527SKees Cook if (relation->invalid)
158235e7527SKees Cook continue;
15993b69d43SKees Cook if (relation->tracee == tracee) {
16093b69d43SKees Cook list_replace_rcu(&relation->node, &added->node);
16193b69d43SKees Cook kfree_rcu(relation, rcu);
16293b69d43SKees Cook goto out;
1632d514487SKees Cook }
1642d514487SKees Cook }
1652d514487SKees Cook
16693b69d43SKees Cook list_add_rcu(&added->node, &ptracer_relations);
16793b69d43SKees Cook
16893b69d43SKees Cook out:
16993b69d43SKees Cook rcu_read_unlock();
170235e7527SKees Cook spin_unlock(&ptracer_relations_lock);
17193b69d43SKees Cook return 0;
1722d514487SKees Cook }
1732d514487SKees Cook
1742d514487SKees Cook /**
1752d514487SKees Cook * yama_ptracer_del - remove exceptions related to the given tasks
1762d514487SKees Cook * @tracer: remove any relation where tracer task matches
1772d514487SKees Cook * @tracee: remove any relation where tracee task matches
1782d514487SKees Cook */
yama_ptracer_del(struct task_struct * tracer,struct task_struct * tracee)1792d514487SKees Cook static void yama_ptracer_del(struct task_struct *tracer,
1802d514487SKees Cook struct task_struct *tracee)
1812d514487SKees Cook {
18293b69d43SKees Cook struct ptrace_relation *relation;
183235e7527SKees Cook bool marked = false;
1842d514487SKees Cook
18593b69d43SKees Cook rcu_read_lock();
18693b69d43SKees Cook list_for_each_entry_rcu(relation, &ptracer_relations, node) {
187235e7527SKees Cook if (relation->invalid)
188235e7527SKees Cook continue;
1892d514487SKees Cook if (relation->tracee == tracee ||
190bf06189eSKees Cook (tracer && relation->tracer == tracer)) {
191235e7527SKees Cook relation->invalid = true;
192235e7527SKees Cook marked = true;
1932d514487SKees Cook }
19493b69d43SKees Cook }
19593b69d43SKees Cook rcu_read_unlock();
196235e7527SKees Cook
197235e7527SKees Cook if (marked)
198235e7527SKees Cook schedule_work(&yama_relation_work);
1992d514487SKees Cook }
2002d514487SKees Cook
2012d514487SKees Cook /**
2022d514487SKees Cook * yama_task_free - check for task_pid to remove from exception list
2032d514487SKees Cook * @task: task being removed
2042d514487SKees Cook */
yama_task_free(struct task_struct * task)2051aa176efSJann Horn static void yama_task_free(struct task_struct *task)
2062d514487SKees Cook {
2072d514487SKees Cook yama_ptracer_del(task, task);
2082d514487SKees Cook }
2092d514487SKees Cook
2102d514487SKees Cook /**
2112d514487SKees Cook * yama_task_prctl - check for Yama-specific prctl operations
2122d514487SKees Cook * @option: operation
2132d514487SKees Cook * @arg2: argument
2142d514487SKees Cook * @arg3: argument
2152d514487SKees Cook * @arg4: argument
2162d514487SKees Cook * @arg5: argument
2172d514487SKees Cook *
2182d514487SKees Cook * Return 0 on success, -ve on error. -ENOSYS is returned when Yama
2192d514487SKees Cook * does not handle the given option.
2202d514487SKees Cook */
yama_task_prctl(int option,unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5)2211aa176efSJann Horn static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
2222d514487SKees Cook unsigned long arg4, unsigned long arg5)
2232d514487SKees Cook {
224b1d9e6b0SCasey Schaufler int rc = -ENOSYS;
2252d514487SKees Cook struct task_struct *myself = current;
2262d514487SKees Cook
2272d514487SKees Cook switch (option) {
2282d514487SKees Cook case PR_SET_PTRACER:
2292d514487SKees Cook /* Since a thread can call prctl(), find the group leader
2302d514487SKees Cook * before calling _add() or _del() on it, since we want
2312d514487SKees Cook * process-level granularity of control. The tracer group
2322d514487SKees Cook * leader checking is handled later when walking the ancestry
2332d514487SKees Cook * at the time of PTRACE_ATTACH check.
2342d514487SKees Cook */
2352d514487SKees Cook rcu_read_lock();
2362d514487SKees Cook if (!thread_group_leader(myself))
2372d514487SKees Cook myself = rcu_dereference(myself->group_leader);
2382d514487SKees Cook get_task_struct(myself);
2392d514487SKees Cook rcu_read_unlock();
2402d514487SKees Cook
2412d514487SKees Cook if (arg2 == 0) {
2422d514487SKees Cook yama_ptracer_del(NULL, myself);
2432d514487SKees Cook rc = 0;
2442e4930ebSKees Cook } else if (arg2 == PR_SET_PTRACER_ANY || (int)arg2 == -1) {
245bf06189eSKees Cook rc = yama_ptracer_add(NULL, myself);
2462d514487SKees Cook } else {
2472d514487SKees Cook struct task_struct *tracer;
2482d514487SKees Cook
2492ee08260SMike Rapoport tracer = find_get_task_by_vpid(arg2);
2502ee08260SMike Rapoport if (!tracer) {
2512d514487SKees Cook rc = -EINVAL;
2522ee08260SMike Rapoport } else {
2532d514487SKees Cook rc = yama_ptracer_add(tracer, myself);
2542d514487SKees Cook put_task_struct(tracer);
2552d514487SKees Cook }
2562d514487SKees Cook }
2572d514487SKees Cook
2582d514487SKees Cook put_task_struct(myself);
2592d514487SKees Cook break;
2602d514487SKees Cook }
2612d514487SKees Cook
2622d514487SKees Cook return rc;
2632d514487SKees Cook }
2642d514487SKees Cook
2652d514487SKees Cook /**
2662d514487SKees Cook * task_is_descendant - walk up a process family tree looking for a match
2672d514487SKees Cook * @parent: the process to compare against while walking up from child
2682d514487SKees Cook * @child: the process to start from while looking upwards for parent
2692d514487SKees Cook *
2702d514487SKees Cook * Returns 1 if child is a descendant of parent, 0 if not.
2712d514487SKees Cook */
task_is_descendant(struct task_struct * parent,struct task_struct * child)2722d514487SKees Cook static int task_is_descendant(struct task_struct *parent,
2732d514487SKees Cook struct task_struct *child)
2742d514487SKees Cook {
2752d514487SKees Cook int rc = 0;
2762d514487SKees Cook struct task_struct *walker = child;
2772d514487SKees Cook
2782d514487SKees Cook if (!parent || !child)
2792d514487SKees Cook return 0;
2802d514487SKees Cook
2812d514487SKees Cook rcu_read_lock();
2822d514487SKees Cook if (!thread_group_leader(parent))
2832d514487SKees Cook parent = rcu_dereference(parent->group_leader);
2842d514487SKees Cook while (walker->pid > 0) {
2852d514487SKees Cook if (!thread_group_leader(walker))
2862d514487SKees Cook walker = rcu_dereference(walker->group_leader);
2872d514487SKees Cook if (walker == parent) {
2882d514487SKees Cook rc = 1;
2892d514487SKees Cook break;
2902d514487SKees Cook }
2912d514487SKees Cook walker = rcu_dereference(walker->real_parent);
2922d514487SKees Cook }
2932d514487SKees Cook rcu_read_unlock();
2942d514487SKees Cook
2952d514487SKees Cook return rc;
2962d514487SKees Cook }
2972d514487SKees Cook
2982d514487SKees Cook /**
2992d514487SKees Cook * ptracer_exception_found - tracer registered as exception for this tracee
3002d514487SKees Cook * @tracer: the task_struct of the process attempting ptrace
3012d514487SKees Cook * @tracee: the task_struct of the process to be ptraced
3022d514487SKees Cook *
30350523a29SJosh Stone * Returns 1 if tracer has a ptracer exception ancestor for tracee.
3042d514487SKees Cook */
ptracer_exception_found(struct task_struct * tracer,struct task_struct * tracee)3052d514487SKees Cook static int ptracer_exception_found(struct task_struct *tracer,
3062d514487SKees Cook struct task_struct *tracee)
3072d514487SKees Cook {
3082d514487SKees Cook int rc = 0;
3092d514487SKees Cook struct ptrace_relation *relation;
3102d514487SKees Cook struct task_struct *parent = NULL;
311bf06189eSKees Cook bool found = false;
3122d514487SKees Cook
3132d514487SKees Cook rcu_read_lock();
31450523a29SJosh Stone
31550523a29SJosh Stone /*
31650523a29SJosh Stone * If there's already an active tracing relationship, then make an
31750523a29SJosh Stone * exception for the sake of other accesses, like process_vm_rw().
31850523a29SJosh Stone */
31950523a29SJosh Stone parent = ptrace_parent(tracee);
32050523a29SJosh Stone if (parent != NULL && same_thread_group(parent, tracer)) {
32150523a29SJosh Stone rc = 1;
32250523a29SJosh Stone goto unlock;
32350523a29SJosh Stone }
32450523a29SJosh Stone
32550523a29SJosh Stone /* Look for a PR_SET_PTRACER relationship. */
3262d514487SKees Cook if (!thread_group_leader(tracee))
3272d514487SKees Cook tracee = rcu_dereference(tracee->group_leader);
328235e7527SKees Cook list_for_each_entry_rcu(relation, &ptracer_relations, node) {
329235e7527SKees Cook if (relation->invalid)
330235e7527SKees Cook continue;
3312d514487SKees Cook if (relation->tracee == tracee) {
3322d514487SKees Cook parent = relation->tracer;
333bf06189eSKees Cook found = true;
3342d514487SKees Cook break;
3352d514487SKees Cook }
336235e7527SKees Cook }
3372d514487SKees Cook
338bf06189eSKees Cook if (found && (parent == NULL || task_is_descendant(parent, tracer)))
3392d514487SKees Cook rc = 1;
34050523a29SJosh Stone
34150523a29SJosh Stone unlock:
3422d514487SKees Cook rcu_read_unlock();
3432d514487SKees Cook
3442d514487SKees Cook return rc;
3452d514487SKees Cook }
3462d514487SKees Cook
3472d514487SKees Cook /**
3482d514487SKees Cook * yama_ptrace_access_check - validate PTRACE_ATTACH calls
3492d514487SKees Cook * @child: task that current task is attempting to ptrace
3502d514487SKees Cook * @mode: ptrace attach mode
3512d514487SKees Cook *
3522d514487SKees Cook * Returns 0 if following the ptrace is allowed, -ve on error.
3532d514487SKees Cook */
yama_ptrace_access_check(struct task_struct * child,unsigned int mode)354b1d9e6b0SCasey Schaufler static int yama_ptrace_access_check(struct task_struct *child,
3552d514487SKees Cook unsigned int mode)
3562d514487SKees Cook {
357b1d9e6b0SCasey Schaufler int rc = 0;
3582d514487SKees Cook
3592d514487SKees Cook /* require ptrace target be a child of ptracer on attach */
3603dfb7d8cSJann Horn if (mode & PTRACE_MODE_ATTACH) {
361389da25fSKees Cook switch (ptrace_scope) {
362389da25fSKees Cook case YAMA_SCOPE_DISABLED:
363389da25fSKees Cook /* No additional restrictions. */
364389da25fSKees Cook break;
365389da25fSKees Cook case YAMA_SCOPE_RELATIONAL:
3664c44aaafSEric W. Biederman rcu_read_lock();
3679474f4e7SKees Cook if (!pid_alive(child))
3689474f4e7SKees Cook rc = -EPERM;
3699474f4e7SKees Cook if (!rc && !task_is_descendant(current, child) &&
3702d514487SKees Cook !ptracer_exception_found(current, child) &&
3714c44aaafSEric W. Biederman !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
3722d514487SKees Cook rc = -EPERM;
3734c44aaafSEric W. Biederman rcu_read_unlock();
374389da25fSKees Cook break;
375389da25fSKees Cook case YAMA_SCOPE_CAPABILITY:
3764c44aaafSEric W. Biederman rcu_read_lock();
3774c44aaafSEric W. Biederman if (!ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
378389da25fSKees Cook rc = -EPERM;
3794c44aaafSEric W. Biederman rcu_read_unlock();
380389da25fSKees Cook break;
381389da25fSKees Cook case YAMA_SCOPE_NO_ATTACH:
382389da25fSKees Cook default:
383389da25fSKees Cook rc = -EPERM;
384389da25fSKees Cook break;
385389da25fSKees Cook }
386389da25fSKees Cook }
3872d514487SKees Cook
3888a56038cSKees Cook if (rc && (mode & PTRACE_MODE_NOAUDIT) == 0)
3898a56038cSKees Cook report_access("attach", child, current);
3902d514487SKees Cook
3912d514487SKees Cook return rc;
3922d514487SKees Cook }
3932d514487SKees Cook
3949d8dad74SKees Cook /**
3959d8dad74SKees Cook * yama_ptrace_traceme - validate PTRACE_TRACEME calls
3969d8dad74SKees Cook * @parent: task that will become the ptracer of the current task
3979d8dad74SKees Cook *
3989d8dad74SKees Cook * Returns 0 if following the ptrace is allowed, -ve on error.
3999d8dad74SKees Cook */
yama_ptrace_traceme(struct task_struct * parent)4001aa176efSJann Horn static int yama_ptrace_traceme(struct task_struct *parent)
4019d8dad74SKees Cook {
402b1d9e6b0SCasey Schaufler int rc = 0;
4039d8dad74SKees Cook
4049d8dad74SKees Cook /* Only disallow PTRACE_TRACEME on more aggressive settings. */
4059d8dad74SKees Cook switch (ptrace_scope) {
4069d8dad74SKees Cook case YAMA_SCOPE_CAPABILITY:
407eddc0a3aSEric W. Biederman if (!has_ns_capability(parent, current_user_ns(), CAP_SYS_PTRACE))
4089d8dad74SKees Cook rc = -EPERM;
4099d8dad74SKees Cook break;
4109d8dad74SKees Cook case YAMA_SCOPE_NO_ATTACH:
4119d8dad74SKees Cook rc = -EPERM;
4129d8dad74SKees Cook break;
4139d8dad74SKees Cook }
4149d8dad74SKees Cook
415dca6b414SJann Horn if (rc) {
416dca6b414SJann Horn task_lock(current);
4178a56038cSKees Cook report_access("traceme", current, parent);
418dca6b414SJann Horn task_unlock(current);
419dca6b414SJann Horn }
4209d8dad74SKees Cook
4219d8dad74SKees Cook return rc;
4229d8dad74SKees Cook }
4239d8dad74SKees Cook
424f22f9aafSPaul Moore static struct security_hook_list yama_hooks[] __ro_after_init = {
425e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_access_check, yama_ptrace_access_check),
426e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_traceme, yama_ptrace_traceme),
427e20b043aSCasey Schaufler LSM_HOOK_INIT(task_prctl, yama_task_prctl),
428e20b043aSCasey Schaufler LSM_HOOK_INIT(task_free, yama_task_free),
4292d514487SKees Cook };
430b1d9e6b0SCasey Schaufler
4312d514487SKees Cook #ifdef CONFIG_SYSCTL
yama_dointvec_minmax(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)432389da25fSKees Cook static int yama_dointvec_minmax(struct ctl_table *table, int write,
43332927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
434389da25fSKees Cook {
43541a4695cSKees Cook struct ctl_table table_copy;
436389da25fSKees Cook
437389da25fSKees Cook if (write && !capable(CAP_SYS_PTRACE))
438389da25fSKees Cook return -EPERM;
439389da25fSKees Cook
440389da25fSKees Cook /* Lock the max value if it ever gets set. */
44141a4695cSKees Cook table_copy = *table;
44241a4695cSKees Cook if (*(int *)table_copy.data == *(int *)table_copy.extra2)
44341a4695cSKees Cook table_copy.extra1 = table_copy.extra2;
444389da25fSKees Cook
44541a4695cSKees Cook return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos);
446389da25fSKees Cook }
447389da25fSKees Cook
448389da25fSKees Cook static int max_scope = YAMA_SCOPE_NO_ATTACH;
4492d514487SKees Cook
4502d514487SKees Cook static struct ctl_table yama_sysctl_table[] = {
4512d514487SKees Cook {
4522d514487SKees Cook .procname = "ptrace_scope",
4532d514487SKees Cook .data = &ptrace_scope,
4542d514487SKees Cook .maxlen = sizeof(int),
4552d514487SKees Cook .mode = 0644,
456389da25fSKees Cook .proc_handler = yama_dointvec_minmax,
457eec4844fSMatteo Croce .extra1 = SYSCTL_ZERO,
458389da25fSKees Cook .extra2 = &max_scope,
4592d514487SKees Cook },
4602d514487SKees Cook { }
4612d514487SKees Cook };
yama_init_sysctl(void)462730daa16SKees Cook static void __init yama_init_sysctl(void)
4632d514487SKees Cook {
464*98cfeb8dSLuis Chamberlain if (!register_sysctl("kernel/yama", yama_sysctl_table))
4652d514487SKees Cook panic("Yama: sysctl registration failed.\n");
4662d514487SKees Cook }
467730daa16SKees Cook #else
yama_init_sysctl(void)468730daa16SKees Cook static inline void yama_init_sysctl(void) { }
469730daa16SKees Cook #endif /* CONFIG_SYSCTL */
4702d514487SKees Cook
yama_init(void)471d6aed64bSKees Cook static int __init yama_init(void)
472730daa16SKees Cook {
473730daa16SKees Cook pr_info("Yama: becoming mindful.\n");
474d69dece5SCasey Schaufler security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks), "yama");
475730daa16SKees Cook yama_init_sysctl();
476d6aed64bSKees Cook return 0;
477730daa16SKees Cook }
478d6aed64bSKees Cook
479d6aed64bSKees Cook DEFINE_LSM(yama) = {
480d6aed64bSKees Cook .name = "yama",
481d6aed64bSKees Cook .init = yama_init,
482d6aed64bSKees Cook };
483