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> 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 { 1572d514487SKees Cook int rc; 1582d514487SKees Cook struct task_struct *myself = current; 1592d514487SKees Cook 1602d514487SKees Cook rc = cap_task_prctl(option, arg2, arg3, arg4, arg5); 1612d514487SKees Cook if (rc != -ENOSYS) 1622d514487SKees Cook return rc; 1632d514487SKees Cook 1642d514487SKees Cook switch (option) { 1652d514487SKees Cook case PR_SET_PTRACER: 1662d514487SKees Cook /* Since a thread can call prctl(), find the group leader 1672d514487SKees Cook * before calling _add() or _del() on it, since we want 1682d514487SKees Cook * process-level granularity of control. The tracer group 1692d514487SKees Cook * leader checking is handled later when walking the ancestry 1702d514487SKees Cook * at the time of PTRACE_ATTACH check. 1712d514487SKees Cook */ 1722d514487SKees Cook rcu_read_lock(); 1732d514487SKees Cook if (!thread_group_leader(myself)) 1742d514487SKees Cook myself = rcu_dereference(myself->group_leader); 1752d514487SKees Cook get_task_struct(myself); 1762d514487SKees Cook rcu_read_unlock(); 1772d514487SKees Cook 1782d514487SKees Cook if (arg2 == 0) { 1792d514487SKees Cook yama_ptracer_del(NULL, myself); 1802d514487SKees Cook rc = 0; 1812e4930ebSKees Cook } else if (arg2 == PR_SET_PTRACER_ANY || (int)arg2 == -1) { 182bf06189eSKees Cook rc = yama_ptracer_add(NULL, myself); 1832d514487SKees Cook } else { 1842d514487SKees Cook struct task_struct *tracer; 1852d514487SKees Cook 1862d514487SKees Cook rcu_read_lock(); 1872d514487SKees Cook tracer = find_task_by_vpid(arg2); 1882d514487SKees Cook if (tracer) 1892d514487SKees Cook get_task_struct(tracer); 1902d514487SKees Cook else 1912d514487SKees Cook rc = -EINVAL; 1922d514487SKees Cook rcu_read_unlock(); 1932d514487SKees Cook 1942d514487SKees Cook if (tracer) { 1952d514487SKees Cook rc = yama_ptracer_add(tracer, myself); 1962d514487SKees Cook put_task_struct(tracer); 1972d514487SKees Cook } 1982d514487SKees Cook } 1992d514487SKees Cook 2002d514487SKees Cook put_task_struct(myself); 2012d514487SKees Cook break; 2022d514487SKees Cook } 2032d514487SKees Cook 2042d514487SKees Cook return rc; 2052d514487SKees Cook } 2062d514487SKees Cook 2072d514487SKees Cook /** 2082d514487SKees Cook * task_is_descendant - walk up a process family tree looking for a match 2092d514487SKees Cook * @parent: the process to compare against while walking up from child 2102d514487SKees Cook * @child: the process to start from while looking upwards for parent 2112d514487SKees Cook * 2122d514487SKees Cook * Returns 1 if child is a descendant of parent, 0 if not. 2132d514487SKees Cook */ 2142d514487SKees Cook static int task_is_descendant(struct task_struct *parent, 2152d514487SKees Cook struct task_struct *child) 2162d514487SKees Cook { 2172d514487SKees Cook int rc = 0; 2182d514487SKees Cook struct task_struct *walker = child; 2192d514487SKees Cook 2202d514487SKees Cook if (!parent || !child) 2212d514487SKees Cook return 0; 2222d514487SKees Cook 2232d514487SKees Cook rcu_read_lock(); 2242d514487SKees Cook if (!thread_group_leader(parent)) 2252d514487SKees Cook parent = rcu_dereference(parent->group_leader); 2262d514487SKees Cook while (walker->pid > 0) { 2272d514487SKees Cook if (!thread_group_leader(walker)) 2282d514487SKees Cook walker = rcu_dereference(walker->group_leader); 2292d514487SKees Cook if (walker == parent) { 2302d514487SKees Cook rc = 1; 2312d514487SKees Cook break; 2322d514487SKees Cook } 2332d514487SKees Cook walker = rcu_dereference(walker->real_parent); 2342d514487SKees Cook } 2352d514487SKees Cook rcu_read_unlock(); 2362d514487SKees Cook 2372d514487SKees Cook return rc; 2382d514487SKees Cook } 2392d514487SKees Cook 2402d514487SKees Cook /** 2412d514487SKees Cook * ptracer_exception_found - tracer registered as exception for this tracee 2422d514487SKees Cook * @tracer: the task_struct of the process attempting ptrace 2432d514487SKees Cook * @tracee: the task_struct of the process to be ptraced 2442d514487SKees Cook * 2452d514487SKees Cook * Returns 1 if tracer has is ptracer exception ancestor for tracee. 2462d514487SKees Cook */ 2472d514487SKees Cook static int ptracer_exception_found(struct task_struct *tracer, 2482d514487SKees Cook struct task_struct *tracee) 2492d514487SKees Cook { 2502d514487SKees Cook int rc = 0; 2512d514487SKees Cook struct ptrace_relation *relation; 2522d514487SKees Cook struct task_struct *parent = NULL; 253bf06189eSKees Cook bool found = false; 2542d514487SKees Cook 2552d514487SKees Cook rcu_read_lock(); 2562d514487SKees Cook if (!thread_group_leader(tracee)) 2572d514487SKees Cook tracee = rcu_dereference(tracee->group_leader); 258235e7527SKees Cook list_for_each_entry_rcu(relation, &ptracer_relations, node) { 259235e7527SKees Cook if (relation->invalid) 260235e7527SKees Cook continue; 2612d514487SKees Cook if (relation->tracee == tracee) { 2622d514487SKees Cook parent = relation->tracer; 263bf06189eSKees Cook found = true; 2642d514487SKees Cook break; 2652d514487SKees Cook } 266235e7527SKees Cook } 2672d514487SKees Cook 268bf06189eSKees Cook if (found && (parent == NULL || task_is_descendant(parent, tracer))) 2692d514487SKees Cook rc = 1; 2702d514487SKees Cook rcu_read_unlock(); 2712d514487SKees Cook 2722d514487SKees Cook return rc; 2732d514487SKees Cook } 2742d514487SKees Cook 2752d514487SKees Cook /** 2762d514487SKees Cook * yama_ptrace_access_check - validate PTRACE_ATTACH calls 2772d514487SKees Cook * @child: task that current task is attempting to ptrace 2782d514487SKees Cook * @mode: ptrace attach mode 2792d514487SKees Cook * 2802d514487SKees Cook * Returns 0 if following the ptrace is allowed, -ve on error. 2812d514487SKees Cook */ 282c6993e4aSKees Cook int yama_ptrace_access_check(struct task_struct *child, 2832d514487SKees Cook unsigned int mode) 2842d514487SKees Cook { 2852d514487SKees Cook int rc; 2862d514487SKees Cook 2872d514487SKees Cook /* If standard caps disallows it, so does Yama. We should 2882d514487SKees Cook * only tighten restrictions further. 2892d514487SKees Cook */ 2902d514487SKees Cook rc = cap_ptrace_access_check(child, mode); 2912d514487SKees Cook if (rc) 2922d514487SKees Cook return rc; 2932d514487SKees Cook 2942d514487SKees Cook /* require ptrace target be a child of ptracer on attach */ 295389da25fSKees Cook if (mode == PTRACE_MODE_ATTACH) { 296389da25fSKees Cook switch (ptrace_scope) { 297389da25fSKees Cook case YAMA_SCOPE_DISABLED: 298389da25fSKees Cook /* No additional restrictions. */ 299389da25fSKees Cook break; 300389da25fSKees Cook case YAMA_SCOPE_RELATIONAL: 3014c44aaafSEric W. Biederman rcu_read_lock(); 302389da25fSKees Cook if (!task_is_descendant(current, child) && 3032d514487SKees Cook !ptracer_exception_found(current, child) && 3044c44aaafSEric W. Biederman !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE)) 3052d514487SKees Cook rc = -EPERM; 3064c44aaafSEric W. Biederman rcu_read_unlock(); 307389da25fSKees Cook break; 308389da25fSKees Cook case YAMA_SCOPE_CAPABILITY: 3094c44aaafSEric W. Biederman rcu_read_lock(); 3104c44aaafSEric W. Biederman if (!ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE)) 311389da25fSKees Cook rc = -EPERM; 3124c44aaafSEric W. Biederman rcu_read_unlock(); 313389da25fSKees Cook break; 314389da25fSKees Cook case YAMA_SCOPE_NO_ATTACH: 315389da25fSKees Cook default: 316389da25fSKees Cook rc = -EPERM; 317389da25fSKees Cook break; 318389da25fSKees Cook } 319389da25fSKees Cook } 3202d514487SKees Cook 3212d514487SKees Cook if (rc) { 322389da25fSKees Cook printk_ratelimited(KERN_NOTICE 323389da25fSKees Cook "ptrace of pid %d was attempted by: %s (pid %d)\n", 3247612bfeeSKees Cook child->pid, current->comm, current->pid); 3252d514487SKees Cook } 3262d514487SKees Cook 3272d514487SKees Cook return rc; 3282d514487SKees Cook } 3292d514487SKees Cook 3309d8dad74SKees Cook /** 3319d8dad74SKees Cook * yama_ptrace_traceme - validate PTRACE_TRACEME calls 3329d8dad74SKees Cook * @parent: task that will become the ptracer of the current task 3339d8dad74SKees Cook * 3349d8dad74SKees Cook * Returns 0 if following the ptrace is allowed, -ve on error. 3359d8dad74SKees Cook */ 336c6993e4aSKees Cook int yama_ptrace_traceme(struct task_struct *parent) 3379d8dad74SKees Cook { 3389d8dad74SKees Cook int rc; 3399d8dad74SKees Cook 3409d8dad74SKees Cook /* If standard caps disallows it, so does Yama. We should 3419d8dad74SKees Cook * only tighten restrictions further. 3429d8dad74SKees Cook */ 3439d8dad74SKees Cook rc = cap_ptrace_traceme(parent); 3449d8dad74SKees Cook if (rc) 3459d8dad74SKees Cook return rc; 3469d8dad74SKees Cook 3479d8dad74SKees Cook /* Only disallow PTRACE_TRACEME on more aggressive settings. */ 3489d8dad74SKees Cook switch (ptrace_scope) { 3499d8dad74SKees Cook case YAMA_SCOPE_CAPABILITY: 350eddc0a3aSEric W. Biederman if (!has_ns_capability(parent, current_user_ns(), CAP_SYS_PTRACE)) 3519d8dad74SKees Cook rc = -EPERM; 3529d8dad74SKees Cook break; 3539d8dad74SKees Cook case YAMA_SCOPE_NO_ATTACH: 3549d8dad74SKees Cook rc = -EPERM; 3559d8dad74SKees Cook break; 3569d8dad74SKees Cook } 3579d8dad74SKees Cook 3589d8dad74SKees Cook if (rc) { 3599d8dad74SKees Cook printk_ratelimited(KERN_NOTICE 3609d8dad74SKees Cook "ptraceme of pid %d was attempted by: %s (pid %d)\n", 3617612bfeeSKees Cook current->pid, parent->comm, parent->pid); 3629d8dad74SKees Cook } 3639d8dad74SKees Cook 3649d8dad74SKees Cook return rc; 3659d8dad74SKees Cook } 3669d8dad74SKees Cook 367c6993e4aSKees Cook #ifndef CONFIG_SECURITY_YAMA_STACKED 3682d514487SKees Cook static struct security_operations yama_ops = { 3692d514487SKees Cook .name = "yama", 3702d514487SKees Cook 3712d514487SKees Cook .ptrace_access_check = yama_ptrace_access_check, 3729d8dad74SKees Cook .ptrace_traceme = yama_ptrace_traceme, 3732d514487SKees Cook .task_prctl = yama_task_prctl, 3742d514487SKees Cook .task_free = yama_task_free, 3752d514487SKees Cook }; 376c6993e4aSKees Cook #endif 3772d514487SKees Cook 3782d514487SKees Cook #ifdef CONFIG_SYSCTL 379389da25fSKees Cook static int yama_dointvec_minmax(struct ctl_table *table, int write, 380389da25fSKees Cook void __user *buffer, size_t *lenp, loff_t *ppos) 381389da25fSKees Cook { 382*41a4695cSKees Cook struct ctl_table table_copy; 383389da25fSKees Cook 384389da25fSKees Cook if (write && !capable(CAP_SYS_PTRACE)) 385389da25fSKees Cook return -EPERM; 386389da25fSKees Cook 387389da25fSKees Cook /* Lock the max value if it ever gets set. */ 388*41a4695cSKees Cook table_copy = *table; 389*41a4695cSKees Cook if (*(int *)table_copy.data == *(int *)table_copy.extra2) 390*41a4695cSKees Cook table_copy.extra1 = table_copy.extra2; 391389da25fSKees Cook 392*41a4695cSKees Cook return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos); 393389da25fSKees Cook } 394389da25fSKees Cook 3952d514487SKees Cook static int zero; 396389da25fSKees Cook static int max_scope = YAMA_SCOPE_NO_ATTACH; 3972d514487SKees Cook 3982d514487SKees Cook struct ctl_path yama_sysctl_path[] = { 3992d514487SKees Cook { .procname = "kernel", }, 4002d514487SKees Cook { .procname = "yama", }, 4012d514487SKees Cook { } 4022d514487SKees Cook }; 4032d514487SKees Cook 4042d514487SKees Cook static struct ctl_table yama_sysctl_table[] = { 4052d514487SKees Cook { 4062d514487SKees Cook .procname = "ptrace_scope", 4072d514487SKees Cook .data = &ptrace_scope, 4082d514487SKees Cook .maxlen = sizeof(int), 4092d514487SKees Cook .mode = 0644, 410389da25fSKees Cook .proc_handler = yama_dointvec_minmax, 4112d514487SKees Cook .extra1 = &zero, 412389da25fSKees Cook .extra2 = &max_scope, 4132d514487SKees Cook }, 4142d514487SKees Cook { } 4152d514487SKees Cook }; 4162d514487SKees Cook #endif /* CONFIG_SYSCTL */ 4172d514487SKees Cook 4182d514487SKees Cook static __init int yama_init(void) 4192d514487SKees Cook { 420c6993e4aSKees Cook #ifndef CONFIG_SECURITY_YAMA_STACKED 4212d514487SKees Cook if (!security_module_enable(&yama_ops)) 4222d514487SKees Cook return 0; 423c6993e4aSKees Cook #endif 4242d514487SKees Cook 4252d514487SKees Cook printk(KERN_INFO "Yama: becoming mindful.\n"); 4262d514487SKees Cook 427c6993e4aSKees Cook #ifndef CONFIG_SECURITY_YAMA_STACKED 4282d514487SKees Cook if (register_security(&yama_ops)) 4292d514487SKees Cook panic("Yama: kernel registration failed.\n"); 430c6993e4aSKees Cook #endif 4312d514487SKees Cook 4322d514487SKees Cook #ifdef CONFIG_SYSCTL 4332d514487SKees Cook if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table)) 4342d514487SKees Cook panic("Yama: sysctl registration failed.\n"); 4352d514487SKees Cook #endif 4362d514487SKees Cook 4372d514487SKees Cook return 0; 4382d514487SKees Cook } 4392d514487SKees Cook 4402d514487SKees Cook security_initcall(yama_init); 441