Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Yama Linux Security Module
0004  *
0005  * Author: Kees Cook <keescook@chromium.org>
0006  *
0007  * Copyright (C) 2010 Canonical, Ltd.
0008  * Copyright (C) 2011 The Chromium OS Authors.
0009  */
0010 
0011 #include <linux/lsm_hooks.h>
0012 #include <linux/sysctl.h>
0013 #include <linux/ptrace.h>
0014 #include <linux/prctl.h>
0015 #include <linux/ratelimit.h>
0016 #include <linux/workqueue.h>
0017 #include <linux/string_helpers.h>
0018 #include <linux/task_work.h>
0019 #include <linux/sched.h>
0020 #include <linux/spinlock.h>
0021 
0022 #define YAMA_SCOPE_DISABLED 0
0023 #define YAMA_SCOPE_RELATIONAL   1
0024 #define YAMA_SCOPE_CAPABILITY   2
0025 #define YAMA_SCOPE_NO_ATTACH    3
0026 
0027 static int ptrace_scope = YAMA_SCOPE_RELATIONAL;
0028 
0029 /* describe a ptrace relationship for potential exception */
0030 struct ptrace_relation {
0031     struct task_struct *tracer;
0032     struct task_struct *tracee;
0033     bool invalid;
0034     struct list_head node;
0035     struct rcu_head rcu;
0036 };
0037 
0038 static LIST_HEAD(ptracer_relations);
0039 static DEFINE_SPINLOCK(ptracer_relations_lock);
0040 
0041 static void yama_relation_cleanup(struct work_struct *work);
0042 static DECLARE_WORK(yama_relation_work, yama_relation_cleanup);
0043 
0044 struct access_report_info {
0045     struct callback_head work;
0046     const char *access;
0047     struct task_struct *target;
0048     struct task_struct *agent;
0049 };
0050 
0051 static void __report_access(struct callback_head *work)
0052 {
0053     struct access_report_info *info =
0054         container_of(work, struct access_report_info, work);
0055     char *target_cmd, *agent_cmd;
0056 
0057     target_cmd = kstrdup_quotable_cmdline(info->target, GFP_KERNEL);
0058     agent_cmd = kstrdup_quotable_cmdline(info->agent, GFP_KERNEL);
0059 
0060     pr_notice_ratelimited(
0061         "ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n",
0062         info->access, target_cmd, info->target->pid, agent_cmd,
0063         info->agent->pid);
0064 
0065     kfree(agent_cmd);
0066     kfree(target_cmd);
0067 
0068     put_task_struct(info->agent);
0069     put_task_struct(info->target);
0070     kfree(info);
0071 }
0072 
0073 /* defers execution because cmdline access can sleep */
0074 static void report_access(const char *access, struct task_struct *target,
0075                 struct task_struct *agent)
0076 {
0077     struct access_report_info *info;
0078     char agent_comm[sizeof(agent->comm)];
0079 
0080     assert_spin_locked(&target->alloc_lock); /* for target->comm */
0081 
0082     if (current->flags & PF_KTHREAD) {
0083         /* I don't think kthreads call task_work_run() before exiting.
0084          * Imagine angry ranting about procfs here.
0085          */
0086         pr_notice_ratelimited(
0087             "ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n",
0088             access, target->comm, target->pid,
0089             get_task_comm(agent_comm, agent), agent->pid);
0090         return;
0091     }
0092 
0093     info = kmalloc(sizeof(*info), GFP_ATOMIC);
0094     if (!info)
0095         return;
0096     init_task_work(&info->work, __report_access);
0097     get_task_struct(target);
0098     get_task_struct(agent);
0099     info->access = access;
0100     info->target = target;
0101     info->agent = agent;
0102     if (task_work_add(current, &info->work, TWA_RESUME) == 0)
0103         return; /* success */
0104 
0105     WARN(1, "report_access called from exiting task");
0106     put_task_struct(target);
0107     put_task_struct(agent);
0108     kfree(info);
0109 }
0110 
0111 /**
0112  * yama_relation_cleanup - remove invalid entries from the relation list
0113  *
0114  */
0115 static void yama_relation_cleanup(struct work_struct *work)
0116 {
0117     struct ptrace_relation *relation;
0118 
0119     spin_lock(&ptracer_relations_lock);
0120     rcu_read_lock();
0121     list_for_each_entry_rcu(relation, &ptracer_relations, node) {
0122         if (relation->invalid) {
0123             list_del_rcu(&relation->node);
0124             kfree_rcu(relation, rcu);
0125         }
0126     }
0127     rcu_read_unlock();
0128     spin_unlock(&ptracer_relations_lock);
0129 }
0130 
0131 /**
0132  * yama_ptracer_add - add/replace an exception for this tracer/tracee pair
0133  * @tracer: the task_struct of the process doing the ptrace
0134  * @tracee: the task_struct of the process to be ptraced
0135  *
0136  * Each tracee can have, at most, one tracer registered. Each time this
0137  * is called, the prior registered tracer will be replaced for the tracee.
0138  *
0139  * Returns 0 if relationship was added, -ve on error.
0140  */
0141 static int yama_ptracer_add(struct task_struct *tracer,
0142                 struct task_struct *tracee)
0143 {
0144     struct ptrace_relation *relation, *added;
0145 
0146     added = kmalloc(sizeof(*added), GFP_KERNEL);
0147     if (!added)
0148         return -ENOMEM;
0149 
0150     added->tracee = tracee;
0151     added->tracer = tracer;
0152     added->invalid = false;
0153 
0154     spin_lock(&ptracer_relations_lock);
0155     rcu_read_lock();
0156     list_for_each_entry_rcu(relation, &ptracer_relations, node) {
0157         if (relation->invalid)
0158             continue;
0159         if (relation->tracee == tracee) {
0160             list_replace_rcu(&relation->node, &added->node);
0161             kfree_rcu(relation, rcu);
0162             goto out;
0163         }
0164     }
0165 
0166     list_add_rcu(&added->node, &ptracer_relations);
0167 
0168 out:
0169     rcu_read_unlock();
0170     spin_unlock(&ptracer_relations_lock);
0171     return 0;
0172 }
0173 
0174 /**
0175  * yama_ptracer_del - remove exceptions related to the given tasks
0176  * @tracer: remove any relation where tracer task matches
0177  * @tracee: remove any relation where tracee task matches
0178  */
0179 static void yama_ptracer_del(struct task_struct *tracer,
0180                  struct task_struct *tracee)
0181 {
0182     struct ptrace_relation *relation;
0183     bool marked = false;
0184 
0185     rcu_read_lock();
0186     list_for_each_entry_rcu(relation, &ptracer_relations, node) {
0187         if (relation->invalid)
0188             continue;
0189         if (relation->tracee == tracee ||
0190             (tracer && relation->tracer == tracer)) {
0191             relation->invalid = true;
0192             marked = true;
0193         }
0194     }
0195     rcu_read_unlock();
0196 
0197     if (marked)
0198         schedule_work(&yama_relation_work);
0199 }
0200 
0201 /**
0202  * yama_task_free - check for task_pid to remove from exception list
0203  * @task: task being removed
0204  */
0205 static void yama_task_free(struct task_struct *task)
0206 {
0207     yama_ptracer_del(task, task);
0208 }
0209 
0210 /**
0211  * yama_task_prctl - check for Yama-specific prctl operations
0212  * @option: operation
0213  * @arg2: argument
0214  * @arg3: argument
0215  * @arg4: argument
0216  * @arg5: argument
0217  *
0218  * Return 0 on success, -ve on error.  -ENOSYS is returned when Yama
0219  * does not handle the given option.
0220  */
0221 static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
0222                unsigned long arg4, unsigned long arg5)
0223 {
0224     int rc = -ENOSYS;
0225     struct task_struct *myself = current;
0226 
0227     switch (option) {
0228     case PR_SET_PTRACER:
0229         /* Since a thread can call prctl(), find the group leader
0230          * before calling _add() or _del() on it, since we want
0231          * process-level granularity of control. The tracer group
0232          * leader checking is handled later when walking the ancestry
0233          * at the time of PTRACE_ATTACH check.
0234          */
0235         rcu_read_lock();
0236         if (!thread_group_leader(myself))
0237             myself = rcu_dereference(myself->group_leader);
0238         get_task_struct(myself);
0239         rcu_read_unlock();
0240 
0241         if (arg2 == 0) {
0242             yama_ptracer_del(NULL, myself);
0243             rc = 0;
0244         } else if (arg2 == PR_SET_PTRACER_ANY || (int)arg2 == -1) {
0245             rc = yama_ptracer_add(NULL, myself);
0246         } else {
0247             struct task_struct *tracer;
0248 
0249             tracer = find_get_task_by_vpid(arg2);
0250             if (!tracer) {
0251                 rc = -EINVAL;
0252             } else {
0253                 rc = yama_ptracer_add(tracer, myself);
0254                 put_task_struct(tracer);
0255             }
0256         }
0257 
0258         put_task_struct(myself);
0259         break;
0260     }
0261 
0262     return rc;
0263 }
0264 
0265 /**
0266  * task_is_descendant - walk up a process family tree looking for a match
0267  * @parent: the process to compare against while walking up from child
0268  * @child: the process to start from while looking upwards for parent
0269  *
0270  * Returns 1 if child is a descendant of parent, 0 if not.
0271  */
0272 static int task_is_descendant(struct task_struct *parent,
0273                   struct task_struct *child)
0274 {
0275     int rc = 0;
0276     struct task_struct *walker = child;
0277 
0278     if (!parent || !child)
0279         return 0;
0280 
0281     rcu_read_lock();
0282     if (!thread_group_leader(parent))
0283         parent = rcu_dereference(parent->group_leader);
0284     while (walker->pid > 0) {
0285         if (!thread_group_leader(walker))
0286             walker = rcu_dereference(walker->group_leader);
0287         if (walker == parent) {
0288             rc = 1;
0289             break;
0290         }
0291         walker = rcu_dereference(walker->real_parent);
0292     }
0293     rcu_read_unlock();
0294 
0295     return rc;
0296 }
0297 
0298 /**
0299  * ptracer_exception_found - tracer registered as exception for this tracee
0300  * @tracer: the task_struct of the process attempting ptrace
0301  * @tracee: the task_struct of the process to be ptraced
0302  *
0303  * Returns 1 if tracer has a ptracer exception ancestor for tracee.
0304  */
0305 static int ptracer_exception_found(struct task_struct *tracer,
0306                    struct task_struct *tracee)
0307 {
0308     int rc = 0;
0309     struct ptrace_relation *relation;
0310     struct task_struct *parent = NULL;
0311     bool found = false;
0312 
0313     rcu_read_lock();
0314 
0315     /*
0316      * If there's already an active tracing relationship, then make an
0317      * exception for the sake of other accesses, like process_vm_rw().
0318      */
0319     parent = ptrace_parent(tracee);
0320     if (parent != NULL && same_thread_group(parent, tracer)) {
0321         rc = 1;
0322         goto unlock;
0323     }
0324 
0325     /* Look for a PR_SET_PTRACER relationship. */
0326     if (!thread_group_leader(tracee))
0327         tracee = rcu_dereference(tracee->group_leader);
0328     list_for_each_entry_rcu(relation, &ptracer_relations, node) {
0329         if (relation->invalid)
0330             continue;
0331         if (relation->tracee == tracee) {
0332             parent = relation->tracer;
0333             found = true;
0334             break;
0335         }
0336     }
0337 
0338     if (found && (parent == NULL || task_is_descendant(parent, tracer)))
0339         rc = 1;
0340 
0341 unlock:
0342     rcu_read_unlock();
0343 
0344     return rc;
0345 }
0346 
0347 /**
0348  * yama_ptrace_access_check - validate PTRACE_ATTACH calls
0349  * @child: task that current task is attempting to ptrace
0350  * @mode: ptrace attach mode
0351  *
0352  * Returns 0 if following the ptrace is allowed, -ve on error.
0353  */
0354 static int yama_ptrace_access_check(struct task_struct *child,
0355                     unsigned int mode)
0356 {
0357     int rc = 0;
0358 
0359     /* require ptrace target be a child of ptracer on attach */
0360     if (mode & PTRACE_MODE_ATTACH) {
0361         switch (ptrace_scope) {
0362         case YAMA_SCOPE_DISABLED:
0363             /* No additional restrictions. */
0364             break;
0365         case YAMA_SCOPE_RELATIONAL:
0366             rcu_read_lock();
0367             if (!pid_alive(child))
0368                 rc = -EPERM;
0369             if (!rc && !task_is_descendant(current, child) &&
0370                 !ptracer_exception_found(current, child) &&
0371                 !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
0372                 rc = -EPERM;
0373             rcu_read_unlock();
0374             break;
0375         case YAMA_SCOPE_CAPABILITY:
0376             rcu_read_lock();
0377             if (!ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
0378                 rc = -EPERM;
0379             rcu_read_unlock();
0380             break;
0381         case YAMA_SCOPE_NO_ATTACH:
0382         default:
0383             rc = -EPERM;
0384             break;
0385         }
0386     }
0387 
0388     if (rc && (mode & PTRACE_MODE_NOAUDIT) == 0)
0389         report_access("attach", child, current);
0390 
0391     return rc;
0392 }
0393 
0394 /**
0395  * yama_ptrace_traceme - validate PTRACE_TRACEME calls
0396  * @parent: task that will become the ptracer of the current task
0397  *
0398  * Returns 0 if following the ptrace is allowed, -ve on error.
0399  */
0400 static int yama_ptrace_traceme(struct task_struct *parent)
0401 {
0402     int rc = 0;
0403 
0404     /* Only disallow PTRACE_TRACEME on more aggressive settings. */
0405     switch (ptrace_scope) {
0406     case YAMA_SCOPE_CAPABILITY:
0407         if (!has_ns_capability(parent, current_user_ns(), CAP_SYS_PTRACE))
0408             rc = -EPERM;
0409         break;
0410     case YAMA_SCOPE_NO_ATTACH:
0411         rc = -EPERM;
0412         break;
0413     }
0414 
0415     if (rc) {
0416         task_lock(current);
0417         report_access("traceme", current, parent);
0418         task_unlock(current);
0419     }
0420 
0421     return rc;
0422 }
0423 
0424 static struct security_hook_list yama_hooks[] __lsm_ro_after_init = {
0425     LSM_HOOK_INIT(ptrace_access_check, yama_ptrace_access_check),
0426     LSM_HOOK_INIT(ptrace_traceme, yama_ptrace_traceme),
0427     LSM_HOOK_INIT(task_prctl, yama_task_prctl),
0428     LSM_HOOK_INIT(task_free, yama_task_free),
0429 };
0430 
0431 #ifdef CONFIG_SYSCTL
0432 static int yama_dointvec_minmax(struct ctl_table *table, int write,
0433                 void *buffer, size_t *lenp, loff_t *ppos)
0434 {
0435     struct ctl_table table_copy;
0436 
0437     if (write && !capable(CAP_SYS_PTRACE))
0438         return -EPERM;
0439 
0440     /* Lock the max value if it ever gets set. */
0441     table_copy = *table;
0442     if (*(int *)table_copy.data == *(int *)table_copy.extra2)
0443         table_copy.extra1 = table_copy.extra2;
0444 
0445     return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos);
0446 }
0447 
0448 static int max_scope = YAMA_SCOPE_NO_ATTACH;
0449 
0450 static struct ctl_path yama_sysctl_path[] = {
0451     { .procname = "kernel", },
0452     { .procname = "yama", },
0453     { }
0454 };
0455 
0456 static struct ctl_table yama_sysctl_table[] = {
0457     {
0458         .procname       = "ptrace_scope",
0459         .data           = &ptrace_scope,
0460         .maxlen         = sizeof(int),
0461         .mode           = 0644,
0462         .proc_handler   = yama_dointvec_minmax,
0463         .extra1         = SYSCTL_ZERO,
0464         .extra2         = &max_scope,
0465     },
0466     { }
0467 };
0468 static void __init yama_init_sysctl(void)
0469 {
0470     if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table))
0471         panic("Yama: sysctl registration failed.\n");
0472 }
0473 #else
0474 static inline void yama_init_sysctl(void) { }
0475 #endif /* CONFIG_SYSCTL */
0476 
0477 static int __init yama_init(void)
0478 {
0479     pr_info("Yama: becoming mindful.\n");
0480     security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks), "yama");
0481     yama_init_sysctl();
0482     return 0;
0483 }
0484 
0485 DEFINE_LSM(yama) = {
0486     .name = "yama",
0487     .init = yama_init,
0488 };