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 21389da25fSKees Cook #define YAMA_SCOPE_DISABLED 0 22389da25fSKees Cook #define YAMA_SCOPE_RELATIONAL 1 23389da25fSKees Cook #define YAMA_SCOPE_CAPABILITY 2 24389da25fSKees Cook #define YAMA_SCOPE_NO_ATTACH 3 25389da25fSKees Cook 26389da25fSKees Cook static int ptrace_scope = YAMA_SCOPE_RELATIONAL; 272d514487SKees Cook 282d514487SKees Cook /* describe a ptrace relationship for potential exception */ 292d514487SKees Cook struct ptrace_relation { 302d514487SKees Cook struct task_struct *tracer; 312d514487SKees Cook struct task_struct *tracee; 322d514487SKees Cook struct list_head node; 332d514487SKees Cook }; 342d514487SKees Cook 352d514487SKees Cook static LIST_HEAD(ptracer_relations); 362d514487SKees Cook static DEFINE_SPINLOCK(ptracer_relations_lock); 372d514487SKees Cook 382d514487SKees Cook /** 392d514487SKees Cook * yama_ptracer_add - add/replace an exception for this tracer/tracee pair 402d514487SKees Cook * @tracer: the task_struct of the process doing the ptrace 412d514487SKees Cook * @tracee: the task_struct of the process to be ptraced 422d514487SKees Cook * 432d514487SKees Cook * Each tracee can have, at most, one tracer registered. Each time this 442d514487SKees Cook * is called, the prior registered tracer will be replaced for the tracee. 452d514487SKees Cook * 462d514487SKees Cook * Returns 0 if relationship was added, -ve on error. 472d514487SKees Cook */ 482d514487SKees Cook static int yama_ptracer_add(struct task_struct *tracer, 492d514487SKees Cook struct task_struct *tracee) 502d514487SKees Cook { 512d514487SKees Cook int rc = 0; 522d514487SKees Cook struct ptrace_relation *added; 532d514487SKees Cook struct ptrace_relation *entry, *relation = NULL; 542d514487SKees Cook 552d514487SKees Cook added = kmalloc(sizeof(*added), GFP_KERNEL); 562d514487SKees Cook if (!added) 572d514487SKees Cook return -ENOMEM; 582d514487SKees Cook 592d514487SKees Cook spin_lock_bh(&ptracer_relations_lock); 602d514487SKees Cook list_for_each_entry(entry, &ptracer_relations, node) 612d514487SKees Cook if (entry->tracee == tracee) { 622d514487SKees Cook relation = entry; 632d514487SKees Cook break; 642d514487SKees Cook } 652d514487SKees Cook if (!relation) { 662d514487SKees Cook relation = added; 672d514487SKees Cook relation->tracee = tracee; 682d514487SKees Cook list_add(&relation->node, &ptracer_relations); 692d514487SKees Cook } 702d514487SKees Cook relation->tracer = tracer; 712d514487SKees Cook 722d514487SKees Cook spin_unlock_bh(&ptracer_relations_lock); 732d514487SKees Cook if (added != relation) 742d514487SKees Cook kfree(added); 752d514487SKees Cook 762d514487SKees Cook return rc; 772d514487SKees Cook } 782d514487SKees Cook 792d514487SKees Cook /** 802d514487SKees Cook * yama_ptracer_del - remove exceptions related to the given tasks 812d514487SKees Cook * @tracer: remove any relation where tracer task matches 822d514487SKees Cook * @tracee: remove any relation where tracee task matches 832d514487SKees Cook */ 842d514487SKees Cook static void yama_ptracer_del(struct task_struct *tracer, 852d514487SKees Cook struct task_struct *tracee) 862d514487SKees Cook { 872d514487SKees Cook struct ptrace_relation *relation, *safe; 882d514487SKees Cook 892d514487SKees Cook spin_lock_bh(&ptracer_relations_lock); 902d514487SKees Cook list_for_each_entry_safe(relation, safe, &ptracer_relations, node) 912d514487SKees Cook if (relation->tracee == tracee || 92bf06189eSKees Cook (tracer && relation->tracer == tracer)) { 932d514487SKees Cook list_del(&relation->node); 942d514487SKees Cook kfree(relation); 952d514487SKees Cook } 962d514487SKees Cook spin_unlock_bh(&ptracer_relations_lock); 972d514487SKees Cook } 982d514487SKees Cook 992d514487SKees Cook /** 1002d514487SKees Cook * yama_task_free - check for task_pid to remove from exception list 1012d514487SKees Cook * @task: task being removed 1022d514487SKees Cook */ 1032d514487SKees Cook static void yama_task_free(struct task_struct *task) 1042d514487SKees Cook { 1052d514487SKees Cook yama_ptracer_del(task, task); 1062d514487SKees Cook } 1072d514487SKees Cook 1082d514487SKees Cook /** 1092d514487SKees Cook * yama_task_prctl - check for Yama-specific prctl operations 1102d514487SKees Cook * @option: operation 1112d514487SKees Cook * @arg2: argument 1122d514487SKees Cook * @arg3: argument 1132d514487SKees Cook * @arg4: argument 1142d514487SKees Cook * @arg5: argument 1152d514487SKees Cook * 1162d514487SKees Cook * Return 0 on success, -ve on error. -ENOSYS is returned when Yama 1172d514487SKees Cook * does not handle the given option. 1182d514487SKees Cook */ 1192d514487SKees Cook static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3, 1202d514487SKees Cook unsigned long arg4, unsigned long arg5) 1212d514487SKees Cook { 1222d514487SKees Cook int rc; 1232d514487SKees Cook struct task_struct *myself = current; 1242d514487SKees Cook 1252d514487SKees Cook rc = cap_task_prctl(option, arg2, arg3, arg4, arg5); 1262d514487SKees Cook if (rc != -ENOSYS) 1272d514487SKees Cook return rc; 1282d514487SKees Cook 1292d514487SKees Cook switch (option) { 1302d514487SKees Cook case PR_SET_PTRACER: 1312d514487SKees Cook /* Since a thread can call prctl(), find the group leader 1322d514487SKees Cook * before calling _add() or _del() on it, since we want 1332d514487SKees Cook * process-level granularity of control. The tracer group 1342d514487SKees Cook * leader checking is handled later when walking the ancestry 1352d514487SKees Cook * at the time of PTRACE_ATTACH check. 1362d514487SKees Cook */ 1372d514487SKees Cook rcu_read_lock(); 1382d514487SKees Cook if (!thread_group_leader(myself)) 1392d514487SKees Cook myself = rcu_dereference(myself->group_leader); 1402d514487SKees Cook get_task_struct(myself); 1412d514487SKees Cook rcu_read_unlock(); 1422d514487SKees Cook 1432d514487SKees Cook if (arg2 == 0) { 1442d514487SKees Cook yama_ptracer_del(NULL, myself); 1452d514487SKees Cook rc = 0; 146bf06189eSKees Cook } else if (arg2 == PR_SET_PTRACER_ANY) { 147bf06189eSKees Cook rc = yama_ptracer_add(NULL, myself); 1482d514487SKees Cook } else { 1492d514487SKees Cook struct task_struct *tracer; 1502d514487SKees Cook 1512d514487SKees Cook rcu_read_lock(); 1522d514487SKees Cook tracer = find_task_by_vpid(arg2); 1532d514487SKees Cook if (tracer) 1542d514487SKees Cook get_task_struct(tracer); 1552d514487SKees Cook else 1562d514487SKees Cook rc = -EINVAL; 1572d514487SKees Cook rcu_read_unlock(); 1582d514487SKees Cook 1592d514487SKees Cook if (tracer) { 1602d514487SKees Cook rc = yama_ptracer_add(tracer, myself); 1612d514487SKees Cook put_task_struct(tracer); 1622d514487SKees Cook } 1632d514487SKees Cook } 1642d514487SKees Cook 1652d514487SKees Cook put_task_struct(myself); 1662d514487SKees Cook break; 1672d514487SKees Cook } 1682d514487SKees Cook 1692d514487SKees Cook return rc; 1702d514487SKees Cook } 1712d514487SKees Cook 1722d514487SKees Cook /** 1732d514487SKees Cook * task_is_descendant - walk up a process family tree looking for a match 1742d514487SKees Cook * @parent: the process to compare against while walking up from child 1752d514487SKees Cook * @child: the process to start from while looking upwards for parent 1762d514487SKees Cook * 1772d514487SKees Cook * Returns 1 if child is a descendant of parent, 0 if not. 1782d514487SKees Cook */ 1792d514487SKees Cook static int task_is_descendant(struct task_struct *parent, 1802d514487SKees Cook struct task_struct *child) 1812d514487SKees Cook { 1822d514487SKees Cook int rc = 0; 1832d514487SKees Cook struct task_struct *walker = child; 1842d514487SKees Cook 1852d514487SKees Cook if (!parent || !child) 1862d514487SKees Cook return 0; 1872d514487SKees Cook 1882d514487SKees Cook rcu_read_lock(); 1892d514487SKees Cook if (!thread_group_leader(parent)) 1902d514487SKees Cook parent = rcu_dereference(parent->group_leader); 1912d514487SKees Cook while (walker->pid > 0) { 1922d514487SKees Cook if (!thread_group_leader(walker)) 1932d514487SKees Cook walker = rcu_dereference(walker->group_leader); 1942d514487SKees Cook if (walker == parent) { 1952d514487SKees Cook rc = 1; 1962d514487SKees Cook break; 1972d514487SKees Cook } 1982d514487SKees Cook walker = rcu_dereference(walker->real_parent); 1992d514487SKees Cook } 2002d514487SKees Cook rcu_read_unlock(); 2012d514487SKees Cook 2022d514487SKees Cook return rc; 2032d514487SKees Cook } 2042d514487SKees Cook 2052d514487SKees Cook /** 2062d514487SKees Cook * ptracer_exception_found - tracer registered as exception for this tracee 2072d514487SKees Cook * @tracer: the task_struct of the process attempting ptrace 2082d514487SKees Cook * @tracee: the task_struct of the process to be ptraced 2092d514487SKees Cook * 2102d514487SKees Cook * Returns 1 if tracer has is ptracer exception ancestor for tracee. 2112d514487SKees Cook */ 2122d514487SKees Cook static int ptracer_exception_found(struct task_struct *tracer, 2132d514487SKees Cook struct task_struct *tracee) 2142d514487SKees Cook { 2152d514487SKees Cook int rc = 0; 2162d514487SKees Cook struct ptrace_relation *relation; 2172d514487SKees Cook struct task_struct *parent = NULL; 218bf06189eSKees Cook bool found = false; 2192d514487SKees Cook 2202d514487SKees Cook spin_lock_bh(&ptracer_relations_lock); 2212d514487SKees Cook rcu_read_lock(); 2222d514487SKees Cook if (!thread_group_leader(tracee)) 2232d514487SKees Cook tracee = rcu_dereference(tracee->group_leader); 2242d514487SKees Cook list_for_each_entry(relation, &ptracer_relations, node) 2252d514487SKees Cook if (relation->tracee == tracee) { 2262d514487SKees Cook parent = relation->tracer; 227bf06189eSKees Cook found = true; 2282d514487SKees Cook break; 2292d514487SKees Cook } 2302d514487SKees Cook 231bf06189eSKees Cook if (found && (parent == NULL || task_is_descendant(parent, tracer))) 2322d514487SKees Cook rc = 1; 2332d514487SKees Cook rcu_read_unlock(); 2342d514487SKees Cook spin_unlock_bh(&ptracer_relations_lock); 2352d514487SKees Cook 2362d514487SKees Cook return rc; 2372d514487SKees Cook } 2382d514487SKees Cook 2392d514487SKees Cook /** 2402d514487SKees Cook * yama_ptrace_access_check - validate PTRACE_ATTACH calls 2412d514487SKees Cook * @child: task that current task is attempting to ptrace 2422d514487SKees Cook * @mode: ptrace attach mode 2432d514487SKees Cook * 2442d514487SKees Cook * Returns 0 if following the ptrace is allowed, -ve on error. 2452d514487SKees Cook */ 2462d514487SKees Cook static int yama_ptrace_access_check(struct task_struct *child, 2472d514487SKees Cook unsigned int mode) 2482d514487SKees Cook { 2492d514487SKees Cook int rc; 2502d514487SKees Cook 2512d514487SKees Cook /* If standard caps disallows it, so does Yama. We should 2522d514487SKees Cook * only tighten restrictions further. 2532d514487SKees Cook */ 2542d514487SKees Cook rc = cap_ptrace_access_check(child, mode); 2552d514487SKees Cook if (rc) 2562d514487SKees Cook return rc; 2572d514487SKees Cook 2582d514487SKees Cook /* require ptrace target be a child of ptracer on attach */ 259389da25fSKees Cook if (mode == PTRACE_MODE_ATTACH) { 260389da25fSKees Cook switch (ptrace_scope) { 261389da25fSKees Cook case YAMA_SCOPE_DISABLED: 262389da25fSKees Cook /* No additional restrictions. */ 263389da25fSKees Cook break; 264389da25fSKees Cook case YAMA_SCOPE_RELATIONAL: 265389da25fSKees Cook if (!task_is_descendant(current, child) && 2662d514487SKees Cook !ptracer_exception_found(current, child) && 2672cc8a716SKees Cook !ns_capable(task_user_ns(child), CAP_SYS_PTRACE)) 2682d514487SKees Cook rc = -EPERM; 269389da25fSKees Cook break; 270389da25fSKees Cook case YAMA_SCOPE_CAPABILITY: 2712cc8a716SKees Cook if (!ns_capable(task_user_ns(child), CAP_SYS_PTRACE)) 272389da25fSKees Cook rc = -EPERM; 273389da25fSKees Cook break; 274389da25fSKees Cook case YAMA_SCOPE_NO_ATTACH: 275389da25fSKees Cook default: 276389da25fSKees Cook rc = -EPERM; 277389da25fSKees Cook break; 278389da25fSKees Cook } 279389da25fSKees Cook } 2802d514487SKees Cook 2812d514487SKees Cook if (rc) { 2822d514487SKees Cook char name[sizeof(current->comm)]; 283389da25fSKees Cook printk_ratelimited(KERN_NOTICE 284389da25fSKees Cook "ptrace of pid %d was attempted by: %s (pid %d)\n", 2852d514487SKees Cook child->pid, 2862d514487SKees Cook get_task_comm(name, current), 2872d514487SKees Cook current->pid); 2882d514487SKees Cook } 2892d514487SKees Cook 2902d514487SKees Cook return rc; 2912d514487SKees Cook } 2922d514487SKees Cook 293*9d8dad74SKees Cook /** 294*9d8dad74SKees Cook * yama_ptrace_traceme - validate PTRACE_TRACEME calls 295*9d8dad74SKees Cook * @parent: task that will become the ptracer of the current task 296*9d8dad74SKees Cook * 297*9d8dad74SKees Cook * Returns 0 if following the ptrace is allowed, -ve on error. 298*9d8dad74SKees Cook */ 299*9d8dad74SKees Cook static int yama_ptrace_traceme(struct task_struct *parent) 300*9d8dad74SKees Cook { 301*9d8dad74SKees Cook int rc; 302*9d8dad74SKees Cook 303*9d8dad74SKees Cook /* If standard caps disallows it, so does Yama. We should 304*9d8dad74SKees Cook * only tighten restrictions further. 305*9d8dad74SKees Cook */ 306*9d8dad74SKees Cook rc = cap_ptrace_traceme(parent); 307*9d8dad74SKees Cook if (rc) 308*9d8dad74SKees Cook return rc; 309*9d8dad74SKees Cook 310*9d8dad74SKees Cook /* Only disallow PTRACE_TRACEME on more aggressive settings. */ 311*9d8dad74SKees Cook switch (ptrace_scope) { 312*9d8dad74SKees Cook case YAMA_SCOPE_CAPABILITY: 313*9d8dad74SKees Cook if (!ns_capable(task_user_ns(parent), CAP_SYS_PTRACE)) 314*9d8dad74SKees Cook rc = -EPERM; 315*9d8dad74SKees Cook break; 316*9d8dad74SKees Cook case YAMA_SCOPE_NO_ATTACH: 317*9d8dad74SKees Cook rc = -EPERM; 318*9d8dad74SKees Cook break; 319*9d8dad74SKees Cook } 320*9d8dad74SKees Cook 321*9d8dad74SKees Cook if (rc) { 322*9d8dad74SKees Cook char name[sizeof(current->comm)]; 323*9d8dad74SKees Cook printk_ratelimited(KERN_NOTICE 324*9d8dad74SKees Cook "ptraceme of pid %d was attempted by: %s (pid %d)\n", 325*9d8dad74SKees Cook current->pid, 326*9d8dad74SKees Cook get_task_comm(name, parent), 327*9d8dad74SKees Cook parent->pid); 328*9d8dad74SKees Cook } 329*9d8dad74SKees Cook 330*9d8dad74SKees Cook return rc; 331*9d8dad74SKees Cook } 332*9d8dad74SKees Cook 3332d514487SKees Cook static struct security_operations yama_ops = { 3342d514487SKees Cook .name = "yama", 3352d514487SKees Cook 3362d514487SKees Cook .ptrace_access_check = yama_ptrace_access_check, 337*9d8dad74SKees Cook .ptrace_traceme = yama_ptrace_traceme, 3382d514487SKees Cook .task_prctl = yama_task_prctl, 3392d514487SKees Cook .task_free = yama_task_free, 3402d514487SKees Cook }; 3412d514487SKees Cook 3422d514487SKees Cook #ifdef CONFIG_SYSCTL 343389da25fSKees Cook static int yama_dointvec_minmax(struct ctl_table *table, int write, 344389da25fSKees Cook void __user *buffer, size_t *lenp, loff_t *ppos) 345389da25fSKees Cook { 346389da25fSKees Cook int rc; 347389da25fSKees Cook 348389da25fSKees Cook if (write && !capable(CAP_SYS_PTRACE)) 349389da25fSKees Cook return -EPERM; 350389da25fSKees Cook 351389da25fSKees Cook rc = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 352389da25fSKees Cook if (rc) 353389da25fSKees Cook return rc; 354389da25fSKees Cook 355389da25fSKees Cook /* Lock the max value if it ever gets set. */ 356389da25fSKees Cook if (write && *(int *)table->data == *(int *)table->extra2) 357389da25fSKees Cook table->extra1 = table->extra2; 358389da25fSKees Cook 359389da25fSKees Cook return rc; 360389da25fSKees Cook } 361389da25fSKees Cook 3622d514487SKees Cook static int zero; 363389da25fSKees Cook static int max_scope = YAMA_SCOPE_NO_ATTACH; 3642d514487SKees Cook 3652d514487SKees Cook struct ctl_path yama_sysctl_path[] = { 3662d514487SKees Cook { .procname = "kernel", }, 3672d514487SKees Cook { .procname = "yama", }, 3682d514487SKees Cook { } 3692d514487SKees Cook }; 3702d514487SKees Cook 3712d514487SKees Cook static struct ctl_table yama_sysctl_table[] = { 3722d514487SKees Cook { 3732d514487SKees Cook .procname = "ptrace_scope", 3742d514487SKees Cook .data = &ptrace_scope, 3752d514487SKees Cook .maxlen = sizeof(int), 3762d514487SKees Cook .mode = 0644, 377389da25fSKees Cook .proc_handler = yama_dointvec_minmax, 3782d514487SKees Cook .extra1 = &zero, 379389da25fSKees Cook .extra2 = &max_scope, 3802d514487SKees Cook }, 3812d514487SKees Cook { } 3822d514487SKees Cook }; 3832d514487SKees Cook #endif /* CONFIG_SYSCTL */ 3842d514487SKees Cook 3852d514487SKees Cook static __init int yama_init(void) 3862d514487SKees Cook { 3872d514487SKees Cook if (!security_module_enable(&yama_ops)) 3882d514487SKees Cook return 0; 3892d514487SKees Cook 3902d514487SKees Cook printk(KERN_INFO "Yama: becoming mindful.\n"); 3912d514487SKees Cook 3922d514487SKees Cook if (register_security(&yama_ops)) 3932d514487SKees Cook panic("Yama: kernel registration failed.\n"); 3942d514487SKees Cook 3952d514487SKees Cook #ifdef CONFIG_SYSCTL 3962d514487SKees Cook if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table)) 3972d514487SKees Cook panic("Yama: sysctl registration failed.\n"); 3982d514487SKees Cook #endif 3992d514487SKees Cook 4002d514487SKees Cook return 0; 4012d514487SKees Cook } 4022d514487SKees Cook 4032d514487SKees Cook security_initcall(yama_init); 404