Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Yama Linux Security Module |
| 4 | * |
| 5 | * Author: Kees Cook <keescook@chromium.org> |
| 6 | * |
| 7 | * Copyright (C) 2010 Canonical, Ltd. |
| 8 | * Copyright (C) 2011 The Chromium OS Authors. |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 9 | */ |
| 10 | |
Casey Schaufler | 3c4ed7b | 2015-05-02 15:10:46 -0700 | [diff] [blame] | 11 | #include <linux/lsm_hooks.h> |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 12 | #include <linux/sysctl.h> |
| 13 | #include <linux/ptrace.h> |
| 14 | #include <linux/prctl.h> |
| 15 | #include <linux/ratelimit.h> |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 16 | #include <linux/workqueue.h> |
Kees Cook | 8a56038 | 2016-04-20 15:46:26 -0700 | [diff] [blame] | 17 | #include <linux/string_helpers.h> |
Jann Horn | dca6b41 | 2016-05-22 06:01:34 +0200 | [diff] [blame] | 18 | #include <linux/task_work.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/spinlock.h> |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 21 | |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 22 | #define YAMA_SCOPE_DISABLED 0 |
| 23 | #define YAMA_SCOPE_RELATIONAL 1 |
| 24 | #define YAMA_SCOPE_CAPABILITY 2 |
| 25 | #define YAMA_SCOPE_NO_ATTACH 3 |
| 26 | |
| 27 | static int ptrace_scope = YAMA_SCOPE_RELATIONAL; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 28 | |
| 29 | /* describe a ptrace relationship for potential exception */ |
| 30 | struct ptrace_relation { |
| 31 | struct task_struct *tracer; |
| 32 | struct task_struct *tracee; |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 33 | bool invalid; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 34 | struct list_head node; |
Kees Cook | 93b69d4 | 2012-10-18 14:53:58 -0700 | [diff] [blame] | 35 | struct rcu_head rcu; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static LIST_HEAD(ptracer_relations); |
| 39 | static DEFINE_SPINLOCK(ptracer_relations_lock); |
| 40 | |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 41 | static void yama_relation_cleanup(struct work_struct *work); |
| 42 | static DECLARE_WORK(yama_relation_work, yama_relation_cleanup); |
| 43 | |
Jann Horn | dca6b41 | 2016-05-22 06:01:34 +0200 | [diff] [blame] | 44 | struct access_report_info { |
| 45 | struct callback_head work; |
| 46 | const char *access; |
| 47 | struct task_struct *target; |
| 48 | struct task_struct *agent; |
| 49 | }; |
| 50 | |
| 51 | static void __report_access(struct callback_head *work) |
Kees Cook | 8a56038 | 2016-04-20 15:46:26 -0700 | [diff] [blame] | 52 | { |
Jann Horn | dca6b41 | 2016-05-22 06:01:34 +0200 | [diff] [blame] | 53 | struct access_report_info *info = |
| 54 | container_of(work, struct access_report_info, work); |
Kees Cook | 8a56038 | 2016-04-20 15:46:26 -0700 | [diff] [blame] | 55 | char *target_cmd, *agent_cmd; |
| 56 | |
Jann Horn | dca6b41 | 2016-05-22 06:01:34 +0200 | [diff] [blame] | 57 | target_cmd = kstrdup_quotable_cmdline(info->target, GFP_KERNEL); |
| 58 | agent_cmd = kstrdup_quotable_cmdline(info->agent, GFP_KERNEL); |
Kees Cook | 8a56038 | 2016-04-20 15:46:26 -0700 | [diff] [blame] | 59 | |
| 60 | pr_notice_ratelimited( |
| 61 | "ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n", |
Jann Horn | dca6b41 | 2016-05-22 06:01:34 +0200 | [diff] [blame] | 62 | info->access, target_cmd, info->target->pid, agent_cmd, |
| 63 | info->agent->pid); |
Kees Cook | 8a56038 | 2016-04-20 15:46:26 -0700 | [diff] [blame] | 64 | |
| 65 | kfree(agent_cmd); |
| 66 | kfree(target_cmd); |
Jann Horn | dca6b41 | 2016-05-22 06:01:34 +0200 | [diff] [blame] | 67 | |
| 68 | put_task_struct(info->agent); |
| 69 | put_task_struct(info->target); |
| 70 | kfree(info); |
| 71 | } |
| 72 | |
| 73 | /* defers execution because cmdline access can sleep */ |
| 74 | static void report_access(const char *access, struct task_struct *target, |
| 75 | struct task_struct *agent) |
| 76 | { |
| 77 | struct access_report_info *info; |
| 78 | char agent_comm[sizeof(agent->comm)]; |
| 79 | |
| 80 | assert_spin_locked(&target->alloc_lock); /* for target->comm */ |
| 81 | |
| 82 | if (current->flags & PF_KTHREAD) { |
| 83 | /* I don't think kthreads call task_work_run() before exiting. |
| 84 | * Imagine angry ranting about procfs here. |
| 85 | */ |
| 86 | pr_notice_ratelimited( |
| 87 | "ptrace %s of \"%s\"[%d] was attempted by \"%s\"[%d]\n", |
| 88 | access, target->comm, target->pid, |
| 89 | get_task_comm(agent_comm, agent), agent->pid); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | info = kmalloc(sizeof(*info), GFP_ATOMIC); |
| 94 | if (!info) |
| 95 | return; |
| 96 | init_task_work(&info->work, __report_access); |
| 97 | get_task_struct(target); |
| 98 | get_task_struct(agent); |
| 99 | info->access = access; |
| 100 | info->target = target; |
| 101 | info->agent = agent; |
Jens Axboe | 91989c7 | 2020-10-16 09:02:26 -0600 | [diff] [blame] | 102 | if (task_work_add(current, &info->work, TWA_RESUME) == 0) |
Jann Horn | dca6b41 | 2016-05-22 06:01:34 +0200 | [diff] [blame] | 103 | return; /* success */ |
| 104 | |
| 105 | WARN(1, "report_access called from exiting task"); |
| 106 | put_task_struct(target); |
| 107 | put_task_struct(agent); |
| 108 | kfree(info); |
Kees Cook | 8a56038 | 2016-04-20 15:46:26 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 111 | /** |
| 112 | * yama_relation_cleanup - remove invalid entries from the relation list |
| 113 | * |
| 114 | */ |
| 115 | static void yama_relation_cleanup(struct work_struct *work) |
| 116 | { |
| 117 | struct ptrace_relation *relation; |
| 118 | |
| 119 | spin_lock(&ptracer_relations_lock); |
| 120 | rcu_read_lock(); |
| 121 | list_for_each_entry_rcu(relation, &ptracer_relations, node) { |
| 122 | if (relation->invalid) { |
| 123 | list_del_rcu(&relation->node); |
| 124 | kfree_rcu(relation, rcu); |
| 125 | } |
| 126 | } |
| 127 | rcu_read_unlock(); |
| 128 | spin_unlock(&ptracer_relations_lock); |
| 129 | } |
| 130 | |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 131 | /** |
| 132 | * yama_ptracer_add - add/replace an exception for this tracer/tracee pair |
| 133 | * @tracer: the task_struct of the process doing the ptrace |
| 134 | * @tracee: the task_struct of the process to be ptraced |
| 135 | * |
| 136 | * Each tracee can have, at most, one tracer registered. Each time this |
| 137 | * is called, the prior registered tracer will be replaced for the tracee. |
| 138 | * |
| 139 | * Returns 0 if relationship was added, -ve on error. |
| 140 | */ |
| 141 | static int yama_ptracer_add(struct task_struct *tracer, |
| 142 | struct task_struct *tracee) |
| 143 | { |
Kees Cook | 93b69d4 | 2012-10-18 14:53:58 -0700 | [diff] [blame] | 144 | struct ptrace_relation *relation, *added; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 145 | |
| 146 | added = kmalloc(sizeof(*added), GFP_KERNEL); |
| 147 | if (!added) |
| 148 | return -ENOMEM; |
| 149 | |
Kees Cook | 93b69d4 | 2012-10-18 14:53:58 -0700 | [diff] [blame] | 150 | added->tracee = tracee; |
| 151 | added->tracer = tracer; |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 152 | added->invalid = false; |
Kees Cook | 93b69d4 | 2012-10-18 14:53:58 -0700 | [diff] [blame] | 153 | |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 154 | spin_lock(&ptracer_relations_lock); |
Kees Cook | 93b69d4 | 2012-10-18 14:53:58 -0700 | [diff] [blame] | 155 | rcu_read_lock(); |
| 156 | list_for_each_entry_rcu(relation, &ptracer_relations, node) { |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 157 | if (relation->invalid) |
| 158 | continue; |
Kees Cook | 93b69d4 | 2012-10-18 14:53:58 -0700 | [diff] [blame] | 159 | if (relation->tracee == tracee) { |
| 160 | list_replace_rcu(&relation->node, &added->node); |
| 161 | kfree_rcu(relation, rcu); |
| 162 | goto out; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 163 | } |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 164 | } |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 165 | |
Kees Cook | 93b69d4 | 2012-10-18 14:53:58 -0700 | [diff] [blame] | 166 | list_add_rcu(&added->node, &ptracer_relations); |
| 167 | |
| 168 | out: |
| 169 | rcu_read_unlock(); |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 170 | spin_unlock(&ptracer_relations_lock); |
Kees Cook | 93b69d4 | 2012-10-18 14:53:58 -0700 | [diff] [blame] | 171 | return 0; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * yama_ptracer_del - remove exceptions related to the given tasks |
| 176 | * @tracer: remove any relation where tracer task matches |
| 177 | * @tracee: remove any relation where tracee task matches |
| 178 | */ |
| 179 | static void yama_ptracer_del(struct task_struct *tracer, |
| 180 | struct task_struct *tracee) |
| 181 | { |
Kees Cook | 93b69d4 | 2012-10-18 14:53:58 -0700 | [diff] [blame] | 182 | struct ptrace_relation *relation; |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 183 | bool marked = false; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 184 | |
Kees Cook | 93b69d4 | 2012-10-18 14:53:58 -0700 | [diff] [blame] | 185 | rcu_read_lock(); |
| 186 | list_for_each_entry_rcu(relation, &ptracer_relations, node) { |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 187 | if (relation->invalid) |
| 188 | continue; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 189 | if (relation->tracee == tracee || |
Kees Cook | bf06189 | 2012-02-14 16:48:09 -0800 | [diff] [blame] | 190 | (tracer && relation->tracer == tracer)) { |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 191 | relation->invalid = true; |
| 192 | marked = true; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 193 | } |
Kees Cook | 93b69d4 | 2012-10-18 14:53:58 -0700 | [diff] [blame] | 194 | } |
| 195 | rcu_read_unlock(); |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 196 | |
| 197 | if (marked) |
| 198 | schedule_work(&yama_relation_work); |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
| 202 | * yama_task_free - check for task_pid to remove from exception list |
| 203 | * @task: task being removed |
| 204 | */ |
Jann Horn | 1aa176e | 2019-03-27 17:21:42 -0700 | [diff] [blame] | 205 | static void yama_task_free(struct task_struct *task) |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 206 | { |
| 207 | yama_ptracer_del(task, task); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * yama_task_prctl - check for Yama-specific prctl operations |
| 212 | * @option: operation |
| 213 | * @arg2: argument |
| 214 | * @arg3: argument |
| 215 | * @arg4: argument |
| 216 | * @arg5: argument |
| 217 | * |
| 218 | * Return 0 on success, -ve on error. -ENOSYS is returned when Yama |
| 219 | * does not handle the given option. |
| 220 | */ |
Jann Horn | 1aa176e | 2019-03-27 17:21:42 -0700 | [diff] [blame] | 221 | static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3, |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 222 | unsigned long arg4, unsigned long arg5) |
| 223 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 224 | int rc = -ENOSYS; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 225 | struct task_struct *myself = current; |
| 226 | |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 227 | switch (option) { |
| 228 | case PR_SET_PTRACER: |
| 229 | /* Since a thread can call prctl(), find the group leader |
| 230 | * before calling _add() or _del() on it, since we want |
| 231 | * process-level granularity of control. The tracer group |
| 232 | * leader checking is handled later when walking the ancestry |
| 233 | * at the time of PTRACE_ATTACH check. |
| 234 | */ |
| 235 | rcu_read_lock(); |
| 236 | if (!thread_group_leader(myself)) |
| 237 | myself = rcu_dereference(myself->group_leader); |
| 238 | get_task_struct(myself); |
| 239 | rcu_read_unlock(); |
| 240 | |
| 241 | if (arg2 == 0) { |
| 242 | yama_ptracer_del(NULL, myself); |
| 243 | rc = 0; |
Kees Cook | 2e4930e | 2012-08-27 11:38:13 -0700 | [diff] [blame] | 244 | } else if (arg2 == PR_SET_PTRACER_ANY || (int)arg2 == -1) { |
Kees Cook | bf06189 | 2012-02-14 16:48:09 -0800 | [diff] [blame] | 245 | rc = yama_ptracer_add(NULL, myself); |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 246 | } else { |
| 247 | struct task_struct *tracer; |
| 248 | |
Mike Rapoport | 2ee0826 | 2018-02-06 15:40:17 -0800 | [diff] [blame] | 249 | tracer = find_get_task_by_vpid(arg2); |
| 250 | if (!tracer) { |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 251 | rc = -EINVAL; |
Mike Rapoport | 2ee0826 | 2018-02-06 15:40:17 -0800 | [diff] [blame] | 252 | } else { |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 253 | rc = yama_ptracer_add(tracer, myself); |
| 254 | put_task_struct(tracer); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | put_task_struct(myself); |
| 259 | break; |
| 260 | } |
| 261 | |
| 262 | return rc; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * task_is_descendant - walk up a process family tree looking for a match |
| 267 | * @parent: the process to compare against while walking up from child |
| 268 | * @child: the process to start from while looking upwards for parent |
| 269 | * |
| 270 | * Returns 1 if child is a descendant of parent, 0 if not. |
| 271 | */ |
| 272 | static int task_is_descendant(struct task_struct *parent, |
| 273 | struct task_struct *child) |
| 274 | { |
| 275 | int rc = 0; |
| 276 | struct task_struct *walker = child; |
| 277 | |
| 278 | if (!parent || !child) |
| 279 | return 0; |
| 280 | |
| 281 | rcu_read_lock(); |
| 282 | if (!thread_group_leader(parent)) |
| 283 | parent = rcu_dereference(parent->group_leader); |
| 284 | while (walker->pid > 0) { |
| 285 | if (!thread_group_leader(walker)) |
| 286 | walker = rcu_dereference(walker->group_leader); |
| 287 | if (walker == parent) { |
| 288 | rc = 1; |
| 289 | break; |
| 290 | } |
| 291 | walker = rcu_dereference(walker->real_parent); |
| 292 | } |
| 293 | rcu_read_unlock(); |
| 294 | |
| 295 | return rc; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * ptracer_exception_found - tracer registered as exception for this tracee |
| 300 | * @tracer: the task_struct of the process attempting ptrace |
| 301 | * @tracee: the task_struct of the process to be ptraced |
| 302 | * |
Josh Stone | 50523a2 | 2016-12-02 15:49:43 -0800 | [diff] [blame] | 303 | * Returns 1 if tracer has a ptracer exception ancestor for tracee. |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 304 | */ |
| 305 | static int ptracer_exception_found(struct task_struct *tracer, |
| 306 | struct task_struct *tracee) |
| 307 | { |
| 308 | int rc = 0; |
| 309 | struct ptrace_relation *relation; |
| 310 | struct task_struct *parent = NULL; |
Kees Cook | bf06189 | 2012-02-14 16:48:09 -0800 | [diff] [blame] | 311 | bool found = false; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 312 | |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 313 | rcu_read_lock(); |
Josh Stone | 50523a2 | 2016-12-02 15:49:43 -0800 | [diff] [blame] | 314 | |
| 315 | /* |
| 316 | * If there's already an active tracing relationship, then make an |
| 317 | * exception for the sake of other accesses, like process_vm_rw(). |
| 318 | */ |
| 319 | parent = ptrace_parent(tracee); |
| 320 | if (parent != NULL && same_thread_group(parent, tracer)) { |
| 321 | rc = 1; |
| 322 | goto unlock; |
| 323 | } |
| 324 | |
| 325 | /* Look for a PR_SET_PTRACER relationship. */ |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 326 | if (!thread_group_leader(tracee)) |
| 327 | tracee = rcu_dereference(tracee->group_leader); |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 328 | list_for_each_entry_rcu(relation, &ptracer_relations, node) { |
| 329 | if (relation->invalid) |
| 330 | continue; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 331 | if (relation->tracee == tracee) { |
| 332 | parent = relation->tracer; |
Kees Cook | bf06189 | 2012-02-14 16:48:09 -0800 | [diff] [blame] | 333 | found = true; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 334 | break; |
| 335 | } |
Kees Cook | 235e752 | 2012-11-19 15:21:26 -0800 | [diff] [blame] | 336 | } |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 337 | |
Kees Cook | bf06189 | 2012-02-14 16:48:09 -0800 | [diff] [blame] | 338 | if (found && (parent == NULL || task_is_descendant(parent, tracer))) |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 339 | rc = 1; |
Josh Stone | 50523a2 | 2016-12-02 15:49:43 -0800 | [diff] [blame] | 340 | |
| 341 | unlock: |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 342 | rcu_read_unlock(); |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 343 | |
| 344 | return rc; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * yama_ptrace_access_check - validate PTRACE_ATTACH calls |
| 349 | * @child: task that current task is attempting to ptrace |
| 350 | * @mode: ptrace attach mode |
| 351 | * |
| 352 | * Returns 0 if following the ptrace is allowed, -ve on error. |
| 353 | */ |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 354 | static int yama_ptrace_access_check(struct task_struct *child, |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 355 | unsigned int mode) |
| 356 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 357 | int rc = 0; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 358 | |
| 359 | /* require ptrace target be a child of ptracer on attach */ |
Jann Horn | 3dfb7d8 | 2016-01-20 15:00:01 -0800 | [diff] [blame] | 360 | if (mode & PTRACE_MODE_ATTACH) { |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 361 | switch (ptrace_scope) { |
| 362 | case YAMA_SCOPE_DISABLED: |
| 363 | /* No additional restrictions. */ |
| 364 | break; |
| 365 | case YAMA_SCOPE_RELATIONAL: |
Eric W. Biederman | 4c44aaa | 2012-07-26 05:05:21 -0700 | [diff] [blame] | 366 | rcu_read_lock(); |
Kees Cook | 9474f4e | 2019-01-16 10:31:09 -0800 | [diff] [blame] | 367 | if (!pid_alive(child)) |
| 368 | rc = -EPERM; |
| 369 | if (!rc && !task_is_descendant(current, child) && |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 370 | !ptracer_exception_found(current, child) && |
Eric W. Biederman | 4c44aaa | 2012-07-26 05:05:21 -0700 | [diff] [blame] | 371 | !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE)) |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 372 | rc = -EPERM; |
Eric W. Biederman | 4c44aaa | 2012-07-26 05:05:21 -0700 | [diff] [blame] | 373 | rcu_read_unlock(); |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 374 | break; |
| 375 | case YAMA_SCOPE_CAPABILITY: |
Eric W. Biederman | 4c44aaa | 2012-07-26 05:05:21 -0700 | [diff] [blame] | 376 | rcu_read_lock(); |
| 377 | if (!ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE)) |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 378 | rc = -EPERM; |
Eric W. Biederman | 4c44aaa | 2012-07-26 05:05:21 -0700 | [diff] [blame] | 379 | rcu_read_unlock(); |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 380 | break; |
| 381 | case YAMA_SCOPE_NO_ATTACH: |
| 382 | default: |
| 383 | rc = -EPERM; |
| 384 | break; |
| 385 | } |
| 386 | } |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 387 | |
Kees Cook | 8a56038 | 2016-04-20 15:46:26 -0700 | [diff] [blame] | 388 | if (rc && (mode & PTRACE_MODE_NOAUDIT) == 0) |
| 389 | report_access("attach", child, current); |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 390 | |
| 391 | return rc; |
| 392 | } |
| 393 | |
Kees Cook | 9d8dad7 | 2012-08-09 19:01:26 -0700 | [diff] [blame] | 394 | /** |
| 395 | * yama_ptrace_traceme - validate PTRACE_TRACEME calls |
| 396 | * @parent: task that will become the ptracer of the current task |
| 397 | * |
| 398 | * Returns 0 if following the ptrace is allowed, -ve on error. |
| 399 | */ |
Jann Horn | 1aa176e | 2019-03-27 17:21:42 -0700 | [diff] [blame] | 400 | static int yama_ptrace_traceme(struct task_struct *parent) |
Kees Cook | 9d8dad7 | 2012-08-09 19:01:26 -0700 | [diff] [blame] | 401 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 402 | int rc = 0; |
Kees Cook | 9d8dad7 | 2012-08-09 19:01:26 -0700 | [diff] [blame] | 403 | |
| 404 | /* Only disallow PTRACE_TRACEME on more aggressive settings. */ |
| 405 | switch (ptrace_scope) { |
| 406 | case YAMA_SCOPE_CAPABILITY: |
Eric W. Biederman | eddc0a3 | 2013-03-21 02:30:41 -0700 | [diff] [blame] | 407 | if (!has_ns_capability(parent, current_user_ns(), CAP_SYS_PTRACE)) |
Kees Cook | 9d8dad7 | 2012-08-09 19:01:26 -0700 | [diff] [blame] | 408 | rc = -EPERM; |
| 409 | break; |
| 410 | case YAMA_SCOPE_NO_ATTACH: |
| 411 | rc = -EPERM; |
| 412 | break; |
| 413 | } |
| 414 | |
Jann Horn | dca6b41 | 2016-05-22 06:01:34 +0200 | [diff] [blame] | 415 | if (rc) { |
| 416 | task_lock(current); |
Kees Cook | 8a56038 | 2016-04-20 15:46:26 -0700 | [diff] [blame] | 417 | report_access("traceme", current, parent); |
Jann Horn | dca6b41 | 2016-05-22 06:01:34 +0200 | [diff] [blame] | 418 | task_unlock(current); |
| 419 | } |
Kees Cook | 9d8dad7 | 2012-08-09 19:01:26 -0700 | [diff] [blame] | 420 | |
| 421 | return rc; |
| 422 | } |
| 423 | |
James Morris | ca97d93 | 2017-02-15 00:18:51 +1100 | [diff] [blame] | 424 | static struct security_hook_list yama_hooks[] __lsm_ro_after_init = { |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 425 | LSM_HOOK_INIT(ptrace_access_check, yama_ptrace_access_check), |
| 426 | LSM_HOOK_INIT(ptrace_traceme, yama_ptrace_traceme), |
| 427 | LSM_HOOK_INIT(task_prctl, yama_task_prctl), |
| 428 | LSM_HOOK_INIT(task_free, yama_task_free), |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 429 | }; |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 430 | |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 431 | #ifdef CONFIG_SYSCTL |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 432 | static int yama_dointvec_minmax(struct ctl_table *table, int write, |
Christoph Hellwig | 3292739 | 2020-04-24 08:43:38 +0200 | [diff] [blame] | 433 | void *buffer, size_t *lenp, loff_t *ppos) |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 434 | { |
Kees Cook | 41a4695c | 2013-02-27 08:37:56 -0800 | [diff] [blame] | 435 | struct ctl_table table_copy; |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 436 | |
| 437 | if (write && !capable(CAP_SYS_PTRACE)) |
| 438 | return -EPERM; |
| 439 | |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 440 | /* Lock the max value if it ever gets set. */ |
Kees Cook | 41a4695c | 2013-02-27 08:37:56 -0800 | [diff] [blame] | 441 | table_copy = *table; |
| 442 | if (*(int *)table_copy.data == *(int *)table_copy.extra2) |
| 443 | table_copy.extra1 = table_copy.extra2; |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 444 | |
Kees Cook | 41a4695c | 2013-02-27 08:37:56 -0800 | [diff] [blame] | 445 | return proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos); |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 448 | static int max_scope = YAMA_SCOPE_NO_ATTACH; |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 449 | |
Jann Horn | 1aa176e | 2019-03-27 17:21:42 -0700 | [diff] [blame] | 450 | static struct ctl_path yama_sysctl_path[] = { |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 451 | { .procname = "kernel", }, |
| 452 | { .procname = "yama", }, |
| 453 | { } |
| 454 | }; |
| 455 | |
| 456 | static struct ctl_table yama_sysctl_table[] = { |
| 457 | { |
| 458 | .procname = "ptrace_scope", |
| 459 | .data = &ptrace_scope, |
| 460 | .maxlen = sizeof(int), |
| 461 | .mode = 0644, |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 462 | .proc_handler = yama_dointvec_minmax, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 463 | .extra1 = SYSCTL_ZERO, |
Kees Cook | 389da25 | 2012-04-16 11:56:45 -0700 | [diff] [blame] | 464 | .extra2 = &max_scope, |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 465 | }, |
| 466 | { } |
| 467 | }; |
Kees Cook | 730daa1 | 2015-07-23 18:02:48 -0700 | [diff] [blame] | 468 | static void __init yama_init_sysctl(void) |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 469 | { |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 470 | if (!register_sysctl_paths(yama_sysctl_path, yama_sysctl_table)) |
| 471 | panic("Yama: sysctl registration failed.\n"); |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 472 | } |
Kees Cook | 730daa1 | 2015-07-23 18:02:48 -0700 | [diff] [blame] | 473 | #else |
| 474 | static inline void yama_init_sysctl(void) { } |
| 475 | #endif /* CONFIG_SYSCTL */ |
Kees Cook | 2d51448 | 2011-12-21 12:17:04 -0800 | [diff] [blame] | 476 | |
Kees Cook | d6aed64 | 2018-09-14 15:37:20 -0700 | [diff] [blame] | 477 | static int __init yama_init(void) |
Kees Cook | 730daa1 | 2015-07-23 18:02:48 -0700 | [diff] [blame] | 478 | { |
| 479 | pr_info("Yama: becoming mindful.\n"); |
Casey Schaufler | d69dece5 | 2017-01-18 17:09:05 -0800 | [diff] [blame] | 480 | security_add_hooks(yama_hooks, ARRAY_SIZE(yama_hooks), "yama"); |
Kees Cook | 730daa1 | 2015-07-23 18:02:48 -0700 | [diff] [blame] | 481 | yama_init_sysctl(); |
Kees Cook | d6aed64 | 2018-09-14 15:37:20 -0700 | [diff] [blame] | 482 | return 0; |
Kees Cook | 730daa1 | 2015-07-23 18:02:48 -0700 | [diff] [blame] | 483 | } |
Kees Cook | d6aed64 | 2018-09-14 15:37:20 -0700 | [diff] [blame] | 484 | |
| 485 | DEFINE_LSM(yama) = { |
| 486 | .name = "yama", |
| 487 | .init = yama_init, |
| 488 | }; |