David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Kernel Probes (KProbes) |
| 4 | * kernel/kprobes.c |
| 5 | * |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6 | * Copyright (C) IBM Corporation, 2002, 2004 |
| 7 | * |
| 8 | * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel |
| 9 | * Probes initial implementation (includes suggestions from |
| 10 | * Rusty Russell). |
| 11 | * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with |
| 12 | * hlists and exceptions notifier as suggested by Andi Kleen. |
| 13 | * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes |
| 14 | * interface to access function arguments. |
| 15 | * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes |
| 16 | * exceptions notifier to be first on the priority list. |
| 17 | * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston |
| 18 | * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi |
| 19 | * <prasanna@in.ibm.com> added function-return probes. |
| 20 | */ |
| 21 | #include <linux/kprobes.h> |
| 22 | #include <linux/hash.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/stddef.h> |
| 26 | #include <linux/export.h> |
| 27 | #include <linux/moduleloader.h> |
| 28 | #include <linux/kallsyms.h> |
| 29 | #include <linux/freezer.h> |
| 30 | #include <linux/seq_file.h> |
| 31 | #include <linux/debugfs.h> |
| 32 | #include <linux/sysctl.h> |
| 33 | #include <linux/kdebug.h> |
| 34 | #include <linux/memory.h> |
| 35 | #include <linux/ftrace.h> |
| 36 | #include <linux/cpu.h> |
| 37 | #include <linux/jump_label.h> |
| 38 | |
| 39 | #include <asm/sections.h> |
| 40 | #include <asm/cacheflush.h> |
| 41 | #include <asm/errno.h> |
| 42 | #include <linux/uaccess.h> |
| 43 | |
| 44 | #define KPROBE_HASH_BITS 6 |
| 45 | #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS) |
| 46 | |
| 47 | |
| 48 | static int kprobes_initialized; |
| 49 | static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE]; |
| 50 | static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE]; |
| 51 | |
| 52 | /* NOTE: change this value only with kprobe_mutex held */ |
| 53 | static bool kprobes_all_disarmed; |
| 54 | |
| 55 | /* This protects kprobe_table and optimizing_list */ |
| 56 | static DEFINE_MUTEX(kprobe_mutex); |
| 57 | static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL; |
| 58 | static struct { |
| 59 | raw_spinlock_t lock ____cacheline_aligned_in_smp; |
| 60 | } kretprobe_table_locks[KPROBE_TABLE_SIZE]; |
| 61 | |
| 62 | kprobe_opcode_t * __weak kprobe_lookup_name(const char *name, |
| 63 | unsigned int __unused) |
| 64 | { |
| 65 | return ((kprobe_opcode_t *)(kallsyms_lookup_name(name))); |
| 66 | } |
| 67 | |
| 68 | static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash) |
| 69 | { |
| 70 | return &(kretprobe_table_locks[hash].lock); |
| 71 | } |
| 72 | |
| 73 | /* Blacklist -- list of struct kprobe_blacklist_entry */ |
| 74 | static LIST_HEAD(kprobe_blacklist); |
| 75 | |
| 76 | #ifdef __ARCH_WANT_KPROBES_INSN_SLOT |
| 77 | /* |
| 78 | * kprobe->ainsn.insn points to the copy of the instruction to be |
| 79 | * single-stepped. x86_64, POWER4 and above have no-exec support and |
| 80 | * stepping on the instruction on a vmalloced/kmalloced/data page |
| 81 | * is a recipe for disaster |
| 82 | */ |
| 83 | struct kprobe_insn_page { |
| 84 | struct list_head list; |
| 85 | kprobe_opcode_t *insns; /* Page of instruction slots */ |
| 86 | struct kprobe_insn_cache *cache; |
| 87 | int nused; |
| 88 | int ngarbage; |
| 89 | char slot_used[]; |
| 90 | }; |
| 91 | |
| 92 | #define KPROBE_INSN_PAGE_SIZE(slots) \ |
| 93 | (offsetof(struct kprobe_insn_page, slot_used) + \ |
| 94 | (sizeof(char) * (slots))) |
| 95 | |
| 96 | static int slots_per_page(struct kprobe_insn_cache *c) |
| 97 | { |
| 98 | return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t)); |
| 99 | } |
| 100 | |
| 101 | enum kprobe_slot_state { |
| 102 | SLOT_CLEAN = 0, |
| 103 | SLOT_DIRTY = 1, |
| 104 | SLOT_USED = 2, |
| 105 | }; |
| 106 | |
| 107 | void __weak *alloc_insn_page(void) |
| 108 | { |
| 109 | return module_alloc(PAGE_SIZE); |
| 110 | } |
| 111 | |
| 112 | void __weak free_insn_page(void *page) |
| 113 | { |
| 114 | module_memfree(page); |
| 115 | } |
| 116 | |
| 117 | struct kprobe_insn_cache kprobe_insn_slots = { |
| 118 | .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex), |
| 119 | .alloc = alloc_insn_page, |
| 120 | .free = free_insn_page, |
| 121 | .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages), |
| 122 | .insn_size = MAX_INSN_SIZE, |
| 123 | .nr_garbage = 0, |
| 124 | }; |
| 125 | static int collect_garbage_slots(struct kprobe_insn_cache *c); |
| 126 | |
| 127 | /** |
| 128 | * __get_insn_slot() - Find a slot on an executable page for an instruction. |
| 129 | * We allocate an executable page if there's no room on existing ones. |
| 130 | */ |
| 131 | kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c) |
| 132 | { |
| 133 | struct kprobe_insn_page *kip; |
| 134 | kprobe_opcode_t *slot = NULL; |
| 135 | |
| 136 | /* Since the slot array is not protected by rcu, we need a mutex */ |
| 137 | mutex_lock(&c->mutex); |
| 138 | retry: |
| 139 | rcu_read_lock(); |
| 140 | list_for_each_entry_rcu(kip, &c->pages, list) { |
| 141 | if (kip->nused < slots_per_page(c)) { |
| 142 | int i; |
| 143 | for (i = 0; i < slots_per_page(c); i++) { |
| 144 | if (kip->slot_used[i] == SLOT_CLEAN) { |
| 145 | kip->slot_used[i] = SLOT_USED; |
| 146 | kip->nused++; |
| 147 | slot = kip->insns + (i * c->insn_size); |
| 148 | rcu_read_unlock(); |
| 149 | goto out; |
| 150 | } |
| 151 | } |
| 152 | /* kip->nused is broken. Fix it. */ |
| 153 | kip->nused = slots_per_page(c); |
| 154 | WARN_ON(1); |
| 155 | } |
| 156 | } |
| 157 | rcu_read_unlock(); |
| 158 | |
| 159 | /* If there are any garbage slots, collect it and try again. */ |
| 160 | if (c->nr_garbage && collect_garbage_slots(c) == 0) |
| 161 | goto retry; |
| 162 | |
| 163 | /* All out of space. Need to allocate a new page. */ |
| 164 | kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL); |
| 165 | if (!kip) |
| 166 | goto out; |
| 167 | |
| 168 | /* |
| 169 | * Use module_alloc so this page is within +/- 2GB of where the |
| 170 | * kernel image and loaded module images reside. This is required |
| 171 | * so x86_64 can correctly handle the %rip-relative fixups. |
| 172 | */ |
| 173 | kip->insns = c->alloc(); |
| 174 | if (!kip->insns) { |
| 175 | kfree(kip); |
| 176 | goto out; |
| 177 | } |
| 178 | INIT_LIST_HEAD(&kip->list); |
| 179 | memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c)); |
| 180 | kip->slot_used[0] = SLOT_USED; |
| 181 | kip->nused = 1; |
| 182 | kip->ngarbage = 0; |
| 183 | kip->cache = c; |
| 184 | list_add_rcu(&kip->list, &c->pages); |
| 185 | slot = kip->insns; |
| 186 | out: |
| 187 | mutex_unlock(&c->mutex); |
| 188 | return slot; |
| 189 | } |
| 190 | |
| 191 | /* Return 1 if all garbages are collected, otherwise 0. */ |
| 192 | static int collect_one_slot(struct kprobe_insn_page *kip, int idx) |
| 193 | { |
| 194 | kip->slot_used[idx] = SLOT_CLEAN; |
| 195 | kip->nused--; |
| 196 | if (kip->nused == 0) { |
| 197 | /* |
| 198 | * Page is no longer in use. Free it unless |
| 199 | * it's the last one. We keep the last one |
| 200 | * so as not to have to set it up again the |
| 201 | * next time somebody inserts a probe. |
| 202 | */ |
| 203 | if (!list_is_singular(&kip->list)) { |
| 204 | list_del_rcu(&kip->list); |
| 205 | synchronize_rcu(); |
| 206 | kip->cache->free(kip->insns); |
| 207 | kfree(kip); |
| 208 | } |
| 209 | return 1; |
| 210 | } |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static int collect_garbage_slots(struct kprobe_insn_cache *c) |
| 215 | { |
| 216 | struct kprobe_insn_page *kip, *next; |
| 217 | |
| 218 | /* Ensure no-one is interrupted on the garbages */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 219 | synchronize_rcu(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 220 | |
| 221 | list_for_each_entry_safe(kip, next, &c->pages, list) { |
| 222 | int i; |
| 223 | if (kip->ngarbage == 0) |
| 224 | continue; |
| 225 | kip->ngarbage = 0; /* we will collect all garbages */ |
| 226 | for (i = 0; i < slots_per_page(c); i++) { |
| 227 | if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i)) |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | c->nr_garbage = 0; |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | void __free_insn_slot(struct kprobe_insn_cache *c, |
| 236 | kprobe_opcode_t *slot, int dirty) |
| 237 | { |
| 238 | struct kprobe_insn_page *kip; |
| 239 | long idx; |
| 240 | |
| 241 | mutex_lock(&c->mutex); |
| 242 | rcu_read_lock(); |
| 243 | list_for_each_entry_rcu(kip, &c->pages, list) { |
| 244 | idx = ((long)slot - (long)kip->insns) / |
| 245 | (c->insn_size * sizeof(kprobe_opcode_t)); |
| 246 | if (idx >= 0 && idx < slots_per_page(c)) |
| 247 | goto out; |
| 248 | } |
| 249 | /* Could not find this slot. */ |
| 250 | WARN_ON(1); |
| 251 | kip = NULL; |
| 252 | out: |
| 253 | rcu_read_unlock(); |
| 254 | /* Mark and sweep: this may sleep */ |
| 255 | if (kip) { |
| 256 | /* Check double free */ |
| 257 | WARN_ON(kip->slot_used[idx] != SLOT_USED); |
| 258 | if (dirty) { |
| 259 | kip->slot_used[idx] = SLOT_DIRTY; |
| 260 | kip->ngarbage++; |
| 261 | if (++c->nr_garbage > slots_per_page(c)) |
| 262 | collect_garbage_slots(c); |
| 263 | } else { |
| 264 | collect_one_slot(kip, idx); |
| 265 | } |
| 266 | } |
| 267 | mutex_unlock(&c->mutex); |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Check given address is on the page of kprobe instruction slots. |
| 272 | * This will be used for checking whether the address on a stack |
| 273 | * is on a text area or not. |
| 274 | */ |
| 275 | bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr) |
| 276 | { |
| 277 | struct kprobe_insn_page *kip; |
| 278 | bool ret = false; |
| 279 | |
| 280 | rcu_read_lock(); |
| 281 | list_for_each_entry_rcu(kip, &c->pages, list) { |
| 282 | if (addr >= (unsigned long)kip->insns && |
| 283 | addr < (unsigned long)kip->insns + PAGE_SIZE) { |
| 284 | ret = true; |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | rcu_read_unlock(); |
| 289 | |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | #ifdef CONFIG_OPTPROBES |
| 294 | /* For optimized_kprobe buffer */ |
| 295 | struct kprobe_insn_cache kprobe_optinsn_slots = { |
| 296 | .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex), |
| 297 | .alloc = alloc_insn_page, |
| 298 | .free = free_insn_page, |
| 299 | .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages), |
| 300 | /* .insn_size is initialized later */ |
| 301 | .nr_garbage = 0, |
| 302 | }; |
| 303 | #endif |
| 304 | #endif |
| 305 | |
| 306 | /* We have preemption disabled.. so it is safe to use __ versions */ |
| 307 | static inline void set_kprobe_instance(struct kprobe *kp) |
| 308 | { |
| 309 | __this_cpu_write(kprobe_instance, kp); |
| 310 | } |
| 311 | |
| 312 | static inline void reset_kprobe_instance(void) |
| 313 | { |
| 314 | __this_cpu_write(kprobe_instance, NULL); |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * This routine is called either: |
| 319 | * - under the kprobe_mutex - during kprobe_[un]register() |
| 320 | * OR |
| 321 | * - with preemption disabled - from arch/xxx/kernel/kprobes.c |
| 322 | */ |
| 323 | struct kprobe *get_kprobe(void *addr) |
| 324 | { |
| 325 | struct hlist_head *head; |
| 326 | struct kprobe *p; |
| 327 | |
| 328 | head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)]; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 329 | hlist_for_each_entry_rcu(p, head, hlist, |
| 330 | lockdep_is_held(&kprobe_mutex)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 331 | if (p->addr == addr) |
| 332 | return p; |
| 333 | } |
| 334 | |
| 335 | return NULL; |
| 336 | } |
| 337 | NOKPROBE_SYMBOL(get_kprobe); |
| 338 | |
| 339 | static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs); |
| 340 | |
| 341 | /* Return true if the kprobe is an aggregator */ |
| 342 | static inline int kprobe_aggrprobe(struct kprobe *p) |
| 343 | { |
| 344 | return p->pre_handler == aggr_pre_handler; |
| 345 | } |
| 346 | |
| 347 | /* Return true(!0) if the kprobe is unused */ |
| 348 | static inline int kprobe_unused(struct kprobe *p) |
| 349 | { |
| 350 | return kprobe_aggrprobe(p) && kprobe_disabled(p) && |
| 351 | list_empty(&p->list); |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Keep all fields in the kprobe consistent |
| 356 | */ |
| 357 | static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p) |
| 358 | { |
| 359 | memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t)); |
| 360 | memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn)); |
| 361 | } |
| 362 | |
| 363 | #ifdef CONFIG_OPTPROBES |
| 364 | /* NOTE: change this value only with kprobe_mutex held */ |
| 365 | static bool kprobes_allow_optimization; |
| 366 | |
| 367 | /* |
| 368 | * Call all pre_handler on the list, but ignores its return value. |
| 369 | * This must be called from arch-dep optimized caller. |
| 370 | */ |
| 371 | void opt_pre_handler(struct kprobe *p, struct pt_regs *regs) |
| 372 | { |
| 373 | struct kprobe *kp; |
| 374 | |
| 375 | list_for_each_entry_rcu(kp, &p->list, list) { |
| 376 | if (kp->pre_handler && likely(!kprobe_disabled(kp))) { |
| 377 | set_kprobe_instance(kp); |
| 378 | kp->pre_handler(kp, regs); |
| 379 | } |
| 380 | reset_kprobe_instance(); |
| 381 | } |
| 382 | } |
| 383 | NOKPROBE_SYMBOL(opt_pre_handler); |
| 384 | |
| 385 | /* Free optimized instructions and optimized_kprobe */ |
| 386 | static void free_aggr_kprobe(struct kprobe *p) |
| 387 | { |
| 388 | struct optimized_kprobe *op; |
| 389 | |
| 390 | op = container_of(p, struct optimized_kprobe, kp); |
| 391 | arch_remove_optimized_kprobe(op); |
| 392 | arch_remove_kprobe(p); |
| 393 | kfree(op); |
| 394 | } |
| 395 | |
| 396 | /* Return true(!0) if the kprobe is ready for optimization. */ |
| 397 | static inline int kprobe_optready(struct kprobe *p) |
| 398 | { |
| 399 | struct optimized_kprobe *op; |
| 400 | |
| 401 | if (kprobe_aggrprobe(p)) { |
| 402 | op = container_of(p, struct optimized_kprobe, kp); |
| 403 | return arch_prepared_optinsn(&op->optinsn); |
| 404 | } |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | /* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */ |
| 410 | static inline int kprobe_disarmed(struct kprobe *p) |
| 411 | { |
| 412 | struct optimized_kprobe *op; |
| 413 | |
| 414 | /* If kprobe is not aggr/opt probe, just return kprobe is disabled */ |
| 415 | if (!kprobe_aggrprobe(p)) |
| 416 | return kprobe_disabled(p); |
| 417 | |
| 418 | op = container_of(p, struct optimized_kprobe, kp); |
| 419 | |
| 420 | return kprobe_disabled(p) && list_empty(&op->list); |
| 421 | } |
| 422 | |
| 423 | /* Return true(!0) if the probe is queued on (un)optimizing lists */ |
| 424 | static int kprobe_queued(struct kprobe *p) |
| 425 | { |
| 426 | struct optimized_kprobe *op; |
| 427 | |
| 428 | if (kprobe_aggrprobe(p)) { |
| 429 | op = container_of(p, struct optimized_kprobe, kp); |
| 430 | if (!list_empty(&op->list)) |
| 431 | return 1; |
| 432 | } |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Return an optimized kprobe whose optimizing code replaces |
| 438 | * instructions including addr (exclude breakpoint). |
| 439 | */ |
| 440 | static struct kprobe *get_optimized_kprobe(unsigned long addr) |
| 441 | { |
| 442 | int i; |
| 443 | struct kprobe *p = NULL; |
| 444 | struct optimized_kprobe *op; |
| 445 | |
| 446 | /* Don't check i == 0, since that is a breakpoint case. */ |
| 447 | for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++) |
| 448 | p = get_kprobe((void *)(addr - i)); |
| 449 | |
| 450 | if (p && kprobe_optready(p)) { |
| 451 | op = container_of(p, struct optimized_kprobe, kp); |
| 452 | if (arch_within_optimized_kprobe(op, addr)) |
| 453 | return p; |
| 454 | } |
| 455 | |
| 456 | return NULL; |
| 457 | } |
| 458 | |
| 459 | /* Optimization staging list, protected by kprobe_mutex */ |
| 460 | static LIST_HEAD(optimizing_list); |
| 461 | static LIST_HEAD(unoptimizing_list); |
| 462 | static LIST_HEAD(freeing_list); |
| 463 | |
| 464 | static void kprobe_optimizer(struct work_struct *work); |
| 465 | static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer); |
| 466 | #define OPTIMIZE_DELAY 5 |
| 467 | |
| 468 | /* |
| 469 | * Optimize (replace a breakpoint with a jump) kprobes listed on |
| 470 | * optimizing_list. |
| 471 | */ |
| 472 | static void do_optimize_kprobes(void) |
| 473 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 474 | lockdep_assert_held(&text_mutex); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 475 | /* |
| 476 | * The optimization/unoptimization refers online_cpus via |
| 477 | * stop_machine() and cpu-hotplug modifies online_cpus. |
| 478 | * And same time, text_mutex will be held in cpu-hotplug and here. |
| 479 | * This combination can cause a deadlock (cpu-hotplug try to lock |
| 480 | * text_mutex but stop_machine can not be done because online_cpus |
| 481 | * has been changed) |
| 482 | * To avoid this deadlock, caller must have locked cpu hotplug |
| 483 | * for preventing cpu-hotplug outside of text_mutex locking. |
| 484 | */ |
| 485 | lockdep_assert_cpus_held(); |
| 486 | |
| 487 | /* Optimization never be done when disarmed */ |
| 488 | if (kprobes_all_disarmed || !kprobes_allow_optimization || |
| 489 | list_empty(&optimizing_list)) |
| 490 | return; |
| 491 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 492 | arch_optimize_kprobes(&optimizing_list); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Unoptimize (replace a jump with a breakpoint and remove the breakpoint |
| 497 | * if need) kprobes listed on unoptimizing_list. |
| 498 | */ |
| 499 | static void do_unoptimize_kprobes(void) |
| 500 | { |
| 501 | struct optimized_kprobe *op, *tmp; |
| 502 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 503 | lockdep_assert_held(&text_mutex); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 504 | /* See comment in do_optimize_kprobes() */ |
| 505 | lockdep_assert_cpus_held(); |
| 506 | |
| 507 | /* Unoptimization must be done anytime */ |
| 508 | if (list_empty(&unoptimizing_list)) |
| 509 | return; |
| 510 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 511 | arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list); |
| 512 | /* Loop free_list for disarming */ |
| 513 | list_for_each_entry_safe(op, tmp, &freeing_list, list) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 514 | /* Switching from detour code to origin */ |
| 515 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 516 | /* Disarm probes if marked disabled */ |
| 517 | if (kprobe_disabled(&op->kp)) |
| 518 | arch_disarm_kprobe(&op->kp); |
| 519 | if (kprobe_unused(&op->kp)) { |
| 520 | /* |
| 521 | * Remove unused probes from hash list. After waiting |
| 522 | * for synchronization, these probes are reclaimed. |
| 523 | * (reclaiming is done by do_free_cleaned_kprobes.) |
| 524 | */ |
| 525 | hlist_del_rcu(&op->kp.hlist); |
| 526 | } else |
| 527 | list_del_init(&op->list); |
| 528 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* Reclaim all kprobes on the free_list */ |
| 532 | static void do_free_cleaned_kprobes(void) |
| 533 | { |
| 534 | struct optimized_kprobe *op, *tmp; |
| 535 | |
| 536 | list_for_each_entry_safe(op, tmp, &freeing_list, list) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 537 | list_del_init(&op->list); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 538 | if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) { |
| 539 | /* |
| 540 | * This must not happen, but if there is a kprobe |
| 541 | * still in use, keep it on kprobes hash list. |
| 542 | */ |
| 543 | continue; |
| 544 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 545 | free_aggr_kprobe(&op->kp); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /* Start optimizer after OPTIMIZE_DELAY passed */ |
| 550 | static void kick_kprobe_optimizer(void) |
| 551 | { |
| 552 | schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY); |
| 553 | } |
| 554 | |
| 555 | /* Kprobe jump optimizer */ |
| 556 | static void kprobe_optimizer(struct work_struct *work) |
| 557 | { |
| 558 | mutex_lock(&kprobe_mutex); |
| 559 | cpus_read_lock(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 560 | mutex_lock(&text_mutex); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 561 | /* Lock modules while optimizing kprobes */ |
| 562 | mutex_lock(&module_mutex); |
| 563 | |
| 564 | /* |
| 565 | * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed) |
| 566 | * kprobes before waiting for quiesence period. |
| 567 | */ |
| 568 | do_unoptimize_kprobes(); |
| 569 | |
| 570 | /* |
| 571 | * Step 2: Wait for quiesence period to ensure all potentially |
| 572 | * preempted tasks to have normally scheduled. Because optprobe |
| 573 | * may modify multiple instructions, there is a chance that Nth |
| 574 | * instruction is preempted. In that case, such tasks can return |
| 575 | * to 2nd-Nth byte of jump instruction. This wait is for avoiding it. |
| 576 | * Note that on non-preemptive kernel, this is transparently converted |
| 577 | * to synchronoze_sched() to wait for all interrupts to have completed. |
| 578 | */ |
| 579 | synchronize_rcu_tasks(); |
| 580 | |
| 581 | /* Step 3: Optimize kprobes after quiesence period */ |
| 582 | do_optimize_kprobes(); |
| 583 | |
| 584 | /* Step 4: Free cleaned kprobes after quiesence period */ |
| 585 | do_free_cleaned_kprobes(); |
| 586 | |
| 587 | mutex_unlock(&module_mutex); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 588 | mutex_unlock(&text_mutex); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 589 | cpus_read_unlock(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 590 | |
| 591 | /* Step 5: Kick optimizer again if needed */ |
| 592 | if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) |
| 593 | kick_kprobe_optimizer(); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 594 | |
| 595 | mutex_unlock(&kprobe_mutex); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /* Wait for completing optimization and unoptimization */ |
| 599 | void wait_for_kprobe_optimizer(void) |
| 600 | { |
| 601 | mutex_lock(&kprobe_mutex); |
| 602 | |
| 603 | while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) { |
| 604 | mutex_unlock(&kprobe_mutex); |
| 605 | |
| 606 | /* this will also make optimizing_work execute immmediately */ |
| 607 | flush_delayed_work(&optimizing_work); |
| 608 | /* @optimizing_work might not have been queued yet, relax */ |
| 609 | cpu_relax(); |
| 610 | |
| 611 | mutex_lock(&kprobe_mutex); |
| 612 | } |
| 613 | |
| 614 | mutex_unlock(&kprobe_mutex); |
| 615 | } |
| 616 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 617 | static bool optprobe_queued_unopt(struct optimized_kprobe *op) |
| 618 | { |
| 619 | struct optimized_kprobe *_op; |
| 620 | |
| 621 | list_for_each_entry(_op, &unoptimizing_list, list) { |
| 622 | if (op == _op) |
| 623 | return true; |
| 624 | } |
| 625 | |
| 626 | return false; |
| 627 | } |
| 628 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 629 | /* Optimize kprobe if p is ready to be optimized */ |
| 630 | static void optimize_kprobe(struct kprobe *p) |
| 631 | { |
| 632 | struct optimized_kprobe *op; |
| 633 | |
| 634 | /* Check if the kprobe is disabled or not ready for optimization. */ |
| 635 | if (!kprobe_optready(p) || !kprobes_allow_optimization || |
| 636 | (kprobe_disabled(p) || kprobes_all_disarmed)) |
| 637 | return; |
| 638 | |
| 639 | /* kprobes with post_handler can not be optimized */ |
| 640 | if (p->post_handler) |
| 641 | return; |
| 642 | |
| 643 | op = container_of(p, struct optimized_kprobe, kp); |
| 644 | |
| 645 | /* Check there is no other kprobes at the optimized instructions */ |
| 646 | if (arch_check_optimized_kprobe(op) < 0) |
| 647 | return; |
| 648 | |
| 649 | /* Check if it is already optimized. */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 650 | if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) { |
| 651 | if (optprobe_queued_unopt(op)) { |
| 652 | /* This is under unoptimizing. Just dequeue the probe */ |
| 653 | list_del_init(&op->list); |
| 654 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 655 | return; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 656 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 657 | op->kp.flags |= KPROBE_FLAG_OPTIMIZED; |
| 658 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 659 | /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */ |
| 660 | if (WARN_ON_ONCE(!list_empty(&op->list))) |
| 661 | return; |
| 662 | |
| 663 | list_add(&op->list, &optimizing_list); |
| 664 | kick_kprobe_optimizer(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /* Short cut to direct unoptimizing */ |
| 668 | static void force_unoptimize_kprobe(struct optimized_kprobe *op) |
| 669 | { |
| 670 | lockdep_assert_cpus_held(); |
| 671 | arch_unoptimize_kprobe(op); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 672 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 673 | if (kprobe_disabled(&op->kp)) |
| 674 | arch_disarm_kprobe(&op->kp); |
| 675 | } |
| 676 | |
| 677 | /* Unoptimize a kprobe if p is optimized */ |
| 678 | static void unoptimize_kprobe(struct kprobe *p, bool force) |
| 679 | { |
| 680 | struct optimized_kprobe *op; |
| 681 | |
| 682 | if (!kprobe_aggrprobe(p) || kprobe_disarmed(p)) |
| 683 | return; /* This is not an optprobe nor optimized */ |
| 684 | |
| 685 | op = container_of(p, struct optimized_kprobe, kp); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 686 | if (!kprobe_optimized(p)) |
| 687 | return; |
| 688 | |
| 689 | if (!list_empty(&op->list)) { |
| 690 | if (optprobe_queued_unopt(op)) { |
| 691 | /* Queued in unoptimizing queue */ |
| 692 | if (force) { |
| 693 | /* |
| 694 | * Forcibly unoptimize the kprobe here, and queue it |
| 695 | * in the freeing list for release afterwards. |
| 696 | */ |
| 697 | force_unoptimize_kprobe(op); |
| 698 | list_move(&op->list, &freeing_list); |
| 699 | } |
| 700 | } else { |
| 701 | /* Dequeue from the optimizing queue */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 702 | list_del_init(&op->list); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 703 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 704 | } |
| 705 | return; |
| 706 | } |
| 707 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 708 | /* Optimized kprobe case */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 709 | if (force) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 710 | /* Forcibly update the code: this is a special case */ |
| 711 | force_unoptimize_kprobe(op); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 712 | } else { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 713 | list_add(&op->list, &unoptimizing_list); |
| 714 | kick_kprobe_optimizer(); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | /* Cancel unoptimizing for reusing */ |
| 719 | static int reuse_unused_kprobe(struct kprobe *ap) |
| 720 | { |
| 721 | struct optimized_kprobe *op; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 722 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 723 | /* |
| 724 | * Unused kprobe MUST be on the way of delayed unoptimizing (means |
| 725 | * there is still a relative jump) and disabled. |
| 726 | */ |
| 727 | op = container_of(ap, struct optimized_kprobe, kp); |
| 728 | WARN_ON_ONCE(list_empty(&op->list)); |
| 729 | /* Enable the probe again */ |
| 730 | ap->flags &= ~KPROBE_FLAG_DISABLED; |
| 731 | /* Optimize it again (remove from op->list) */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 732 | if (!kprobe_optready(ap)) |
| 733 | return -EINVAL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 734 | |
| 735 | optimize_kprobe(ap); |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | /* Remove optimized instructions */ |
| 740 | static void kill_optimized_kprobe(struct kprobe *p) |
| 741 | { |
| 742 | struct optimized_kprobe *op; |
| 743 | |
| 744 | op = container_of(p, struct optimized_kprobe, kp); |
| 745 | if (!list_empty(&op->list)) |
| 746 | /* Dequeue from the (un)optimization queue */ |
| 747 | list_del_init(&op->list); |
| 748 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; |
| 749 | |
| 750 | if (kprobe_unused(p)) { |
| 751 | /* Enqueue if it is unused */ |
| 752 | list_add(&op->list, &freeing_list); |
| 753 | /* |
| 754 | * Remove unused probes from the hash list. After waiting |
| 755 | * for synchronization, this probe is reclaimed. |
| 756 | * (reclaiming is done by do_free_cleaned_kprobes().) |
| 757 | */ |
| 758 | hlist_del_rcu(&op->kp.hlist); |
| 759 | } |
| 760 | |
| 761 | /* Don't touch the code, because it is already freed. */ |
| 762 | arch_remove_optimized_kprobe(op); |
| 763 | } |
| 764 | |
| 765 | static inline |
| 766 | void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p) |
| 767 | { |
| 768 | if (!kprobe_ftrace(p)) |
| 769 | arch_prepare_optimized_kprobe(op, p); |
| 770 | } |
| 771 | |
| 772 | /* Try to prepare optimized instructions */ |
| 773 | static void prepare_optimized_kprobe(struct kprobe *p) |
| 774 | { |
| 775 | struct optimized_kprobe *op; |
| 776 | |
| 777 | op = container_of(p, struct optimized_kprobe, kp); |
| 778 | __prepare_optimized_kprobe(op, p); |
| 779 | } |
| 780 | |
| 781 | /* Allocate new optimized_kprobe and try to prepare optimized instructions */ |
| 782 | static struct kprobe *alloc_aggr_kprobe(struct kprobe *p) |
| 783 | { |
| 784 | struct optimized_kprobe *op; |
| 785 | |
| 786 | op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL); |
| 787 | if (!op) |
| 788 | return NULL; |
| 789 | |
| 790 | INIT_LIST_HEAD(&op->list); |
| 791 | op->kp.addr = p->addr; |
| 792 | __prepare_optimized_kprobe(op, p); |
| 793 | |
| 794 | return &op->kp; |
| 795 | } |
| 796 | |
| 797 | static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p); |
| 798 | |
| 799 | /* |
| 800 | * Prepare an optimized_kprobe and optimize it |
| 801 | * NOTE: p must be a normal registered kprobe |
| 802 | */ |
| 803 | static void try_to_optimize_kprobe(struct kprobe *p) |
| 804 | { |
| 805 | struct kprobe *ap; |
| 806 | struct optimized_kprobe *op; |
| 807 | |
| 808 | /* Impossible to optimize ftrace-based kprobe */ |
| 809 | if (kprobe_ftrace(p)) |
| 810 | return; |
| 811 | |
| 812 | /* For preparing optimization, jump_label_text_reserved() is called */ |
| 813 | cpus_read_lock(); |
| 814 | jump_label_lock(); |
| 815 | mutex_lock(&text_mutex); |
| 816 | |
| 817 | ap = alloc_aggr_kprobe(p); |
| 818 | if (!ap) |
| 819 | goto out; |
| 820 | |
| 821 | op = container_of(ap, struct optimized_kprobe, kp); |
| 822 | if (!arch_prepared_optinsn(&op->optinsn)) { |
| 823 | /* If failed to setup optimizing, fallback to kprobe */ |
| 824 | arch_remove_optimized_kprobe(op); |
| 825 | kfree(op); |
| 826 | goto out; |
| 827 | } |
| 828 | |
| 829 | init_aggr_kprobe(ap, p); |
| 830 | optimize_kprobe(ap); /* This just kicks optimizer thread */ |
| 831 | |
| 832 | out: |
| 833 | mutex_unlock(&text_mutex); |
| 834 | jump_label_unlock(); |
| 835 | cpus_read_unlock(); |
| 836 | } |
| 837 | |
| 838 | #ifdef CONFIG_SYSCTL |
| 839 | static void optimize_all_kprobes(void) |
| 840 | { |
| 841 | struct hlist_head *head; |
| 842 | struct kprobe *p; |
| 843 | unsigned int i; |
| 844 | |
| 845 | mutex_lock(&kprobe_mutex); |
| 846 | /* If optimization is already allowed, just return */ |
| 847 | if (kprobes_allow_optimization) |
| 848 | goto out; |
| 849 | |
| 850 | cpus_read_lock(); |
| 851 | kprobes_allow_optimization = true; |
| 852 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 853 | head = &kprobe_table[i]; |
| 854 | hlist_for_each_entry_rcu(p, head, hlist) |
| 855 | if (!kprobe_disabled(p)) |
| 856 | optimize_kprobe(p); |
| 857 | } |
| 858 | cpus_read_unlock(); |
| 859 | printk(KERN_INFO "Kprobes globally optimized\n"); |
| 860 | out: |
| 861 | mutex_unlock(&kprobe_mutex); |
| 862 | } |
| 863 | |
| 864 | static void unoptimize_all_kprobes(void) |
| 865 | { |
| 866 | struct hlist_head *head; |
| 867 | struct kprobe *p; |
| 868 | unsigned int i; |
| 869 | |
| 870 | mutex_lock(&kprobe_mutex); |
| 871 | /* If optimization is already prohibited, just return */ |
| 872 | if (!kprobes_allow_optimization) { |
| 873 | mutex_unlock(&kprobe_mutex); |
| 874 | return; |
| 875 | } |
| 876 | |
| 877 | cpus_read_lock(); |
| 878 | kprobes_allow_optimization = false; |
| 879 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 880 | head = &kprobe_table[i]; |
| 881 | hlist_for_each_entry_rcu(p, head, hlist) { |
| 882 | if (!kprobe_disabled(p)) |
| 883 | unoptimize_kprobe(p, false); |
| 884 | } |
| 885 | } |
| 886 | cpus_read_unlock(); |
| 887 | mutex_unlock(&kprobe_mutex); |
| 888 | |
| 889 | /* Wait for unoptimizing completion */ |
| 890 | wait_for_kprobe_optimizer(); |
| 891 | printk(KERN_INFO "Kprobes globally unoptimized\n"); |
| 892 | } |
| 893 | |
| 894 | static DEFINE_MUTEX(kprobe_sysctl_mutex); |
| 895 | int sysctl_kprobes_optimization; |
| 896 | int proc_kprobes_optimization_handler(struct ctl_table *table, int write, |
| 897 | void __user *buffer, size_t *length, |
| 898 | loff_t *ppos) |
| 899 | { |
| 900 | int ret; |
| 901 | |
| 902 | mutex_lock(&kprobe_sysctl_mutex); |
| 903 | sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0; |
| 904 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 905 | |
| 906 | if (sysctl_kprobes_optimization) |
| 907 | optimize_all_kprobes(); |
| 908 | else |
| 909 | unoptimize_all_kprobes(); |
| 910 | mutex_unlock(&kprobe_sysctl_mutex); |
| 911 | |
| 912 | return ret; |
| 913 | } |
| 914 | #endif /* CONFIG_SYSCTL */ |
| 915 | |
| 916 | /* Put a breakpoint for a probe. Must be called with text_mutex locked */ |
| 917 | static void __arm_kprobe(struct kprobe *p) |
| 918 | { |
| 919 | struct kprobe *_p; |
| 920 | |
| 921 | /* Check collision with other optimized kprobes */ |
| 922 | _p = get_optimized_kprobe((unsigned long)p->addr); |
| 923 | if (unlikely(_p)) |
| 924 | /* Fallback to unoptimized kprobe */ |
| 925 | unoptimize_kprobe(_p, true); |
| 926 | |
| 927 | arch_arm_kprobe(p); |
| 928 | optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */ |
| 929 | } |
| 930 | |
| 931 | /* Remove the breakpoint of a probe. Must be called with text_mutex locked */ |
| 932 | static void __disarm_kprobe(struct kprobe *p, bool reopt) |
| 933 | { |
| 934 | struct kprobe *_p; |
| 935 | |
| 936 | /* Try to unoptimize */ |
| 937 | unoptimize_kprobe(p, kprobes_all_disarmed); |
| 938 | |
| 939 | if (!kprobe_queued(p)) { |
| 940 | arch_disarm_kprobe(p); |
| 941 | /* If another kprobe was blocked, optimize it. */ |
| 942 | _p = get_optimized_kprobe((unsigned long)p->addr); |
| 943 | if (unlikely(_p) && reopt) |
| 944 | optimize_kprobe(_p); |
| 945 | } |
| 946 | /* TODO: reoptimize others after unoptimized this probe */ |
| 947 | } |
| 948 | |
| 949 | #else /* !CONFIG_OPTPROBES */ |
| 950 | |
| 951 | #define optimize_kprobe(p) do {} while (0) |
| 952 | #define unoptimize_kprobe(p, f) do {} while (0) |
| 953 | #define kill_optimized_kprobe(p) do {} while (0) |
| 954 | #define prepare_optimized_kprobe(p) do {} while (0) |
| 955 | #define try_to_optimize_kprobe(p) do {} while (0) |
| 956 | #define __arm_kprobe(p) arch_arm_kprobe(p) |
| 957 | #define __disarm_kprobe(p, o) arch_disarm_kprobe(p) |
| 958 | #define kprobe_disarmed(p) kprobe_disabled(p) |
| 959 | #define wait_for_kprobe_optimizer() do {} while (0) |
| 960 | |
| 961 | static int reuse_unused_kprobe(struct kprobe *ap) |
| 962 | { |
| 963 | /* |
| 964 | * If the optimized kprobe is NOT supported, the aggr kprobe is |
| 965 | * released at the same time that the last aggregated kprobe is |
| 966 | * unregistered. |
| 967 | * Thus there should be no chance to reuse unused kprobe. |
| 968 | */ |
| 969 | printk(KERN_ERR "Error: There should be no unused kprobe here.\n"); |
| 970 | return -EINVAL; |
| 971 | } |
| 972 | |
| 973 | static void free_aggr_kprobe(struct kprobe *p) |
| 974 | { |
| 975 | arch_remove_kprobe(p); |
| 976 | kfree(p); |
| 977 | } |
| 978 | |
| 979 | static struct kprobe *alloc_aggr_kprobe(struct kprobe *p) |
| 980 | { |
| 981 | return kzalloc(sizeof(struct kprobe), GFP_KERNEL); |
| 982 | } |
| 983 | #endif /* CONFIG_OPTPROBES */ |
| 984 | |
| 985 | #ifdef CONFIG_KPROBES_ON_FTRACE |
| 986 | static struct ftrace_ops kprobe_ftrace_ops __read_mostly = { |
| 987 | .func = kprobe_ftrace_handler, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 988 | .flags = FTRACE_OPS_FL_SAVE_REGS, |
| 989 | }; |
| 990 | |
| 991 | static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = { |
| 992 | .func = kprobe_ftrace_handler, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 993 | .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY, |
| 994 | }; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 995 | |
| 996 | static int kprobe_ipmodify_enabled; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 997 | static int kprobe_ftrace_enabled; |
| 998 | |
| 999 | /* Must ensure p->addr is really on ftrace */ |
| 1000 | static int prepare_kprobe(struct kprobe *p) |
| 1001 | { |
| 1002 | if (!kprobe_ftrace(p)) |
| 1003 | return arch_prepare_kprobe(p); |
| 1004 | |
| 1005 | return arch_prepare_kprobe_ftrace(p); |
| 1006 | } |
| 1007 | |
| 1008 | /* Caller must lock kprobe_mutex */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1009 | static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, |
| 1010 | int *cnt) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1011 | { |
| 1012 | int ret = 0; |
| 1013 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1014 | ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1015 | if (ret) { |
| 1016 | pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n", |
| 1017 | p->addr, ret); |
| 1018 | return ret; |
| 1019 | } |
| 1020 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1021 | if (*cnt == 0) { |
| 1022 | ret = register_ftrace_function(ops); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1023 | if (ret) { |
| 1024 | pr_debug("Failed to init kprobe-ftrace (%d)\n", ret); |
| 1025 | goto err_ftrace; |
| 1026 | } |
| 1027 | } |
| 1028 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1029 | (*cnt)++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1030 | return ret; |
| 1031 | |
| 1032 | err_ftrace: |
| 1033 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1034 | * At this point, sinec ops is not registered, we should be sefe from |
| 1035 | * registering empty filter. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1036 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1037 | ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1038 | return ret; |
| 1039 | } |
| 1040 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1041 | static int arm_kprobe_ftrace(struct kprobe *p) |
| 1042 | { |
| 1043 | bool ipmodify = (p->post_handler != NULL); |
| 1044 | |
| 1045 | return __arm_kprobe_ftrace(p, |
| 1046 | ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops, |
| 1047 | ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled); |
| 1048 | } |
| 1049 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1050 | /* Caller must lock kprobe_mutex */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1051 | static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, |
| 1052 | int *cnt) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1053 | { |
| 1054 | int ret = 0; |
| 1055 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1056 | if (*cnt == 1) { |
| 1057 | ret = unregister_ftrace_function(ops); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1058 | if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret)) |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1062 | (*cnt)--; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1063 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1064 | ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1065 | WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n", |
| 1066 | p->addr, ret); |
| 1067 | return ret; |
| 1068 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1069 | |
| 1070 | static int disarm_kprobe_ftrace(struct kprobe *p) |
| 1071 | { |
| 1072 | bool ipmodify = (p->post_handler != NULL); |
| 1073 | |
| 1074 | return __disarm_kprobe_ftrace(p, |
| 1075 | ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops, |
| 1076 | ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled); |
| 1077 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1078 | #else /* !CONFIG_KPROBES_ON_FTRACE */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1079 | static inline int prepare_kprobe(struct kprobe *p) |
| 1080 | { |
| 1081 | return arch_prepare_kprobe(p); |
| 1082 | } |
| 1083 | |
| 1084 | static inline int arm_kprobe_ftrace(struct kprobe *p) |
| 1085 | { |
| 1086 | return -ENODEV; |
| 1087 | } |
| 1088 | |
| 1089 | static inline int disarm_kprobe_ftrace(struct kprobe *p) |
| 1090 | { |
| 1091 | return -ENODEV; |
| 1092 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1093 | #endif |
| 1094 | |
| 1095 | /* Arm a kprobe with text_mutex */ |
| 1096 | static int arm_kprobe(struct kprobe *kp) |
| 1097 | { |
| 1098 | if (unlikely(kprobe_ftrace(kp))) |
| 1099 | return arm_kprobe_ftrace(kp); |
| 1100 | |
| 1101 | cpus_read_lock(); |
| 1102 | mutex_lock(&text_mutex); |
| 1103 | __arm_kprobe(kp); |
| 1104 | mutex_unlock(&text_mutex); |
| 1105 | cpus_read_unlock(); |
| 1106 | |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | /* Disarm a kprobe with text_mutex */ |
| 1111 | static int disarm_kprobe(struct kprobe *kp, bool reopt) |
| 1112 | { |
| 1113 | if (unlikely(kprobe_ftrace(kp))) |
| 1114 | return disarm_kprobe_ftrace(kp); |
| 1115 | |
| 1116 | cpus_read_lock(); |
| 1117 | mutex_lock(&text_mutex); |
| 1118 | __disarm_kprobe(kp, reopt); |
| 1119 | mutex_unlock(&text_mutex); |
| 1120 | cpus_read_unlock(); |
| 1121 | |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | /* |
| 1126 | * Aggregate handlers for multiple kprobes support - these handlers |
| 1127 | * take care of invoking the individual kprobe handlers on p->list |
| 1128 | */ |
| 1129 | static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs) |
| 1130 | { |
| 1131 | struct kprobe *kp; |
| 1132 | |
| 1133 | list_for_each_entry_rcu(kp, &p->list, list) { |
| 1134 | if (kp->pre_handler && likely(!kprobe_disabled(kp))) { |
| 1135 | set_kprobe_instance(kp); |
| 1136 | if (kp->pre_handler(kp, regs)) |
| 1137 | return 1; |
| 1138 | } |
| 1139 | reset_kprobe_instance(); |
| 1140 | } |
| 1141 | return 0; |
| 1142 | } |
| 1143 | NOKPROBE_SYMBOL(aggr_pre_handler); |
| 1144 | |
| 1145 | static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs, |
| 1146 | unsigned long flags) |
| 1147 | { |
| 1148 | struct kprobe *kp; |
| 1149 | |
| 1150 | list_for_each_entry_rcu(kp, &p->list, list) { |
| 1151 | if (kp->post_handler && likely(!kprobe_disabled(kp))) { |
| 1152 | set_kprobe_instance(kp); |
| 1153 | kp->post_handler(kp, regs, flags); |
| 1154 | reset_kprobe_instance(); |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | NOKPROBE_SYMBOL(aggr_post_handler); |
| 1159 | |
| 1160 | static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs, |
| 1161 | int trapnr) |
| 1162 | { |
| 1163 | struct kprobe *cur = __this_cpu_read(kprobe_instance); |
| 1164 | |
| 1165 | /* |
| 1166 | * if we faulted "during" the execution of a user specified |
| 1167 | * probe handler, invoke just that probe's fault handler |
| 1168 | */ |
| 1169 | if (cur && cur->fault_handler) { |
| 1170 | if (cur->fault_handler(cur, regs, trapnr)) |
| 1171 | return 1; |
| 1172 | } |
| 1173 | return 0; |
| 1174 | } |
| 1175 | NOKPROBE_SYMBOL(aggr_fault_handler); |
| 1176 | |
| 1177 | /* Walks the list and increments nmissed count for multiprobe case */ |
| 1178 | void kprobes_inc_nmissed_count(struct kprobe *p) |
| 1179 | { |
| 1180 | struct kprobe *kp; |
| 1181 | if (!kprobe_aggrprobe(p)) { |
| 1182 | p->nmissed++; |
| 1183 | } else { |
| 1184 | list_for_each_entry_rcu(kp, &p->list, list) |
| 1185 | kp->nmissed++; |
| 1186 | } |
| 1187 | return; |
| 1188 | } |
| 1189 | NOKPROBE_SYMBOL(kprobes_inc_nmissed_count); |
| 1190 | |
| 1191 | void recycle_rp_inst(struct kretprobe_instance *ri, |
| 1192 | struct hlist_head *head) |
| 1193 | { |
| 1194 | struct kretprobe *rp = ri->rp; |
| 1195 | |
| 1196 | /* remove rp inst off the rprobe_inst_table */ |
| 1197 | hlist_del(&ri->hlist); |
| 1198 | INIT_HLIST_NODE(&ri->hlist); |
| 1199 | if (likely(rp)) { |
| 1200 | raw_spin_lock(&rp->lock); |
| 1201 | hlist_add_head(&ri->hlist, &rp->free_instances); |
| 1202 | raw_spin_unlock(&rp->lock); |
| 1203 | } else |
| 1204 | /* Unregistering */ |
| 1205 | hlist_add_head(&ri->hlist, head); |
| 1206 | } |
| 1207 | NOKPROBE_SYMBOL(recycle_rp_inst); |
| 1208 | |
| 1209 | void kretprobe_hash_lock(struct task_struct *tsk, |
| 1210 | struct hlist_head **head, unsigned long *flags) |
| 1211 | __acquires(hlist_lock) |
| 1212 | { |
| 1213 | unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS); |
| 1214 | raw_spinlock_t *hlist_lock; |
| 1215 | |
| 1216 | *head = &kretprobe_inst_table[hash]; |
| 1217 | hlist_lock = kretprobe_table_lock_ptr(hash); |
| 1218 | raw_spin_lock_irqsave(hlist_lock, *flags); |
| 1219 | } |
| 1220 | NOKPROBE_SYMBOL(kretprobe_hash_lock); |
| 1221 | |
| 1222 | static void kretprobe_table_lock(unsigned long hash, |
| 1223 | unsigned long *flags) |
| 1224 | __acquires(hlist_lock) |
| 1225 | { |
| 1226 | raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash); |
| 1227 | raw_spin_lock_irqsave(hlist_lock, *flags); |
| 1228 | } |
| 1229 | NOKPROBE_SYMBOL(kretprobe_table_lock); |
| 1230 | |
| 1231 | void kretprobe_hash_unlock(struct task_struct *tsk, |
| 1232 | unsigned long *flags) |
| 1233 | __releases(hlist_lock) |
| 1234 | { |
| 1235 | unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS); |
| 1236 | raw_spinlock_t *hlist_lock; |
| 1237 | |
| 1238 | hlist_lock = kretprobe_table_lock_ptr(hash); |
| 1239 | raw_spin_unlock_irqrestore(hlist_lock, *flags); |
| 1240 | } |
| 1241 | NOKPROBE_SYMBOL(kretprobe_hash_unlock); |
| 1242 | |
| 1243 | static void kretprobe_table_unlock(unsigned long hash, |
| 1244 | unsigned long *flags) |
| 1245 | __releases(hlist_lock) |
| 1246 | { |
| 1247 | raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash); |
| 1248 | raw_spin_unlock_irqrestore(hlist_lock, *flags); |
| 1249 | } |
| 1250 | NOKPROBE_SYMBOL(kretprobe_table_unlock); |
| 1251 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1252 | struct kprobe kprobe_busy = { |
| 1253 | .addr = (void *) get_kprobe, |
| 1254 | }; |
| 1255 | |
| 1256 | void kprobe_busy_begin(void) |
| 1257 | { |
| 1258 | struct kprobe_ctlblk *kcb; |
| 1259 | |
| 1260 | preempt_disable(); |
| 1261 | __this_cpu_write(current_kprobe, &kprobe_busy); |
| 1262 | kcb = get_kprobe_ctlblk(); |
| 1263 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; |
| 1264 | } |
| 1265 | |
| 1266 | void kprobe_busy_end(void) |
| 1267 | { |
| 1268 | __this_cpu_write(current_kprobe, NULL); |
| 1269 | preempt_enable(); |
| 1270 | } |
| 1271 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1272 | /* |
| 1273 | * This function is called from finish_task_switch when task tk becomes dead, |
| 1274 | * so that we can recycle any function-return probe instances associated |
| 1275 | * with this task. These left over instances represent probed functions |
| 1276 | * that have been called but will never return. |
| 1277 | */ |
| 1278 | void kprobe_flush_task(struct task_struct *tk) |
| 1279 | { |
| 1280 | struct kretprobe_instance *ri; |
| 1281 | struct hlist_head *head, empty_rp; |
| 1282 | struct hlist_node *tmp; |
| 1283 | unsigned long hash, flags = 0; |
| 1284 | |
| 1285 | if (unlikely(!kprobes_initialized)) |
| 1286 | /* Early boot. kretprobe_table_locks not yet initialized. */ |
| 1287 | return; |
| 1288 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1289 | kprobe_busy_begin(); |
| 1290 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1291 | INIT_HLIST_HEAD(&empty_rp); |
| 1292 | hash = hash_ptr(tk, KPROBE_HASH_BITS); |
| 1293 | head = &kretprobe_inst_table[hash]; |
| 1294 | kretprobe_table_lock(hash, &flags); |
| 1295 | hlist_for_each_entry_safe(ri, tmp, head, hlist) { |
| 1296 | if (ri->task == tk) |
| 1297 | recycle_rp_inst(ri, &empty_rp); |
| 1298 | } |
| 1299 | kretprobe_table_unlock(hash, &flags); |
| 1300 | hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) { |
| 1301 | hlist_del(&ri->hlist); |
| 1302 | kfree(ri); |
| 1303 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1304 | |
| 1305 | kprobe_busy_end(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1306 | } |
| 1307 | NOKPROBE_SYMBOL(kprobe_flush_task); |
| 1308 | |
| 1309 | static inline void free_rp_inst(struct kretprobe *rp) |
| 1310 | { |
| 1311 | struct kretprobe_instance *ri; |
| 1312 | struct hlist_node *next; |
| 1313 | |
| 1314 | hlist_for_each_entry_safe(ri, next, &rp->free_instances, hlist) { |
| 1315 | hlist_del(&ri->hlist); |
| 1316 | kfree(ri); |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | static void cleanup_rp_inst(struct kretprobe *rp) |
| 1321 | { |
| 1322 | unsigned long flags, hash; |
| 1323 | struct kretprobe_instance *ri; |
| 1324 | struct hlist_node *next; |
| 1325 | struct hlist_head *head; |
| 1326 | |
| 1327 | /* No race here */ |
| 1328 | for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) { |
| 1329 | kretprobe_table_lock(hash, &flags); |
| 1330 | head = &kretprobe_inst_table[hash]; |
| 1331 | hlist_for_each_entry_safe(ri, next, head, hlist) { |
| 1332 | if (ri->rp == rp) |
| 1333 | ri->rp = NULL; |
| 1334 | } |
| 1335 | kretprobe_table_unlock(hash, &flags); |
| 1336 | } |
| 1337 | free_rp_inst(rp); |
| 1338 | } |
| 1339 | NOKPROBE_SYMBOL(cleanup_rp_inst); |
| 1340 | |
| 1341 | /* Add the new probe to ap->list */ |
| 1342 | static int add_new_kprobe(struct kprobe *ap, struct kprobe *p) |
| 1343 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1344 | if (p->post_handler) |
| 1345 | unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */ |
| 1346 | |
| 1347 | list_add_rcu(&p->list, &ap->list); |
| 1348 | if (p->post_handler && !ap->post_handler) |
| 1349 | ap->post_handler = aggr_post_handler; |
| 1350 | |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * Fill in the required fields of the "manager kprobe". Replace the |
| 1356 | * earlier kprobe in the hlist with the manager kprobe |
| 1357 | */ |
| 1358 | static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p) |
| 1359 | { |
| 1360 | /* Copy p's insn slot to ap */ |
| 1361 | copy_kprobe(p, ap); |
| 1362 | flush_insn_slot(ap); |
| 1363 | ap->addr = p->addr; |
| 1364 | ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED; |
| 1365 | ap->pre_handler = aggr_pre_handler; |
| 1366 | ap->fault_handler = aggr_fault_handler; |
| 1367 | /* We don't care the kprobe which has gone. */ |
| 1368 | if (p->post_handler && !kprobe_gone(p)) |
| 1369 | ap->post_handler = aggr_post_handler; |
| 1370 | |
| 1371 | INIT_LIST_HEAD(&ap->list); |
| 1372 | INIT_HLIST_NODE(&ap->hlist); |
| 1373 | |
| 1374 | list_add_rcu(&p->list, &ap->list); |
| 1375 | hlist_replace_rcu(&p->hlist, &ap->hlist); |
| 1376 | } |
| 1377 | |
| 1378 | /* |
| 1379 | * This is the second or subsequent kprobe at the address - handle |
| 1380 | * the intricacies |
| 1381 | */ |
| 1382 | static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p) |
| 1383 | { |
| 1384 | int ret = 0; |
| 1385 | struct kprobe *ap = orig_p; |
| 1386 | |
| 1387 | cpus_read_lock(); |
| 1388 | |
| 1389 | /* For preparing optimization, jump_label_text_reserved() is called */ |
| 1390 | jump_label_lock(); |
| 1391 | mutex_lock(&text_mutex); |
| 1392 | |
| 1393 | if (!kprobe_aggrprobe(orig_p)) { |
| 1394 | /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */ |
| 1395 | ap = alloc_aggr_kprobe(orig_p); |
| 1396 | if (!ap) { |
| 1397 | ret = -ENOMEM; |
| 1398 | goto out; |
| 1399 | } |
| 1400 | init_aggr_kprobe(ap, orig_p); |
| 1401 | } else if (kprobe_unused(ap)) { |
| 1402 | /* This probe is going to die. Rescue it */ |
| 1403 | ret = reuse_unused_kprobe(ap); |
| 1404 | if (ret) |
| 1405 | goto out; |
| 1406 | } |
| 1407 | |
| 1408 | if (kprobe_gone(ap)) { |
| 1409 | /* |
| 1410 | * Attempting to insert new probe at the same location that |
| 1411 | * had a probe in the module vaddr area which already |
| 1412 | * freed. So, the instruction slot has already been |
| 1413 | * released. We need a new slot for the new probe. |
| 1414 | */ |
| 1415 | ret = arch_prepare_kprobe(ap); |
| 1416 | if (ret) |
| 1417 | /* |
| 1418 | * Even if fail to allocate new slot, don't need to |
| 1419 | * free aggr_probe. It will be used next time, or |
| 1420 | * freed by unregister_kprobe. |
| 1421 | */ |
| 1422 | goto out; |
| 1423 | |
| 1424 | /* Prepare optimized instructions if possible. */ |
| 1425 | prepare_optimized_kprobe(ap); |
| 1426 | |
| 1427 | /* |
| 1428 | * Clear gone flag to prevent allocating new slot again, and |
| 1429 | * set disabled flag because it is not armed yet. |
| 1430 | */ |
| 1431 | ap->flags = (ap->flags & ~KPROBE_FLAG_GONE) |
| 1432 | | KPROBE_FLAG_DISABLED; |
| 1433 | } |
| 1434 | |
| 1435 | /* Copy ap's insn slot to p */ |
| 1436 | copy_kprobe(ap, p); |
| 1437 | ret = add_new_kprobe(ap, p); |
| 1438 | |
| 1439 | out: |
| 1440 | mutex_unlock(&text_mutex); |
| 1441 | jump_label_unlock(); |
| 1442 | cpus_read_unlock(); |
| 1443 | |
| 1444 | if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) { |
| 1445 | ap->flags &= ~KPROBE_FLAG_DISABLED; |
| 1446 | if (!kprobes_all_disarmed) { |
| 1447 | /* Arm the breakpoint again. */ |
| 1448 | ret = arm_kprobe(ap); |
| 1449 | if (ret) { |
| 1450 | ap->flags |= KPROBE_FLAG_DISABLED; |
| 1451 | list_del_rcu(&p->list); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1452 | synchronize_rcu(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | return ret; |
| 1457 | } |
| 1458 | |
| 1459 | bool __weak arch_within_kprobe_blacklist(unsigned long addr) |
| 1460 | { |
| 1461 | /* The __kprobes marked functions and entry code must not be probed */ |
| 1462 | return addr >= (unsigned long)__kprobes_text_start && |
| 1463 | addr < (unsigned long)__kprobes_text_end; |
| 1464 | } |
| 1465 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1466 | static bool __within_kprobe_blacklist(unsigned long addr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1467 | { |
| 1468 | struct kprobe_blacklist_entry *ent; |
| 1469 | |
| 1470 | if (arch_within_kprobe_blacklist(addr)) |
| 1471 | return true; |
| 1472 | /* |
| 1473 | * If there exists a kprobe_blacklist, verify and |
| 1474 | * fail any probe registration in the prohibited area |
| 1475 | */ |
| 1476 | list_for_each_entry(ent, &kprobe_blacklist, list) { |
| 1477 | if (addr >= ent->start_addr && addr < ent->end_addr) |
| 1478 | return true; |
| 1479 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1480 | return false; |
| 1481 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1482 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1483 | bool within_kprobe_blacklist(unsigned long addr) |
| 1484 | { |
| 1485 | char symname[KSYM_NAME_LEN], *p; |
| 1486 | |
| 1487 | if (__within_kprobe_blacklist(addr)) |
| 1488 | return true; |
| 1489 | |
| 1490 | /* Check if the address is on a suffixed-symbol */ |
| 1491 | if (!lookup_symbol_name(addr, symname)) { |
| 1492 | p = strchr(symname, '.'); |
| 1493 | if (!p) |
| 1494 | return false; |
| 1495 | *p = '\0'; |
| 1496 | addr = (unsigned long)kprobe_lookup_name(symname, 0); |
| 1497 | if (addr) |
| 1498 | return __within_kprobe_blacklist(addr); |
| 1499 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1500 | return false; |
| 1501 | } |
| 1502 | |
| 1503 | /* |
| 1504 | * If we have a symbol_name argument, look it up and add the offset field |
| 1505 | * to it. This way, we can specify a relative address to a symbol. |
| 1506 | * This returns encoded errors if it fails to look up symbol or invalid |
| 1507 | * combination of parameters. |
| 1508 | */ |
| 1509 | static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr, |
| 1510 | const char *symbol_name, unsigned int offset) |
| 1511 | { |
| 1512 | if ((symbol_name && addr) || (!symbol_name && !addr)) |
| 1513 | goto invalid; |
| 1514 | |
| 1515 | if (symbol_name) { |
| 1516 | addr = kprobe_lookup_name(symbol_name, offset); |
| 1517 | if (!addr) |
| 1518 | return ERR_PTR(-ENOENT); |
| 1519 | } |
| 1520 | |
| 1521 | addr = (kprobe_opcode_t *)(((char *)addr) + offset); |
| 1522 | if (addr) |
| 1523 | return addr; |
| 1524 | |
| 1525 | invalid: |
| 1526 | return ERR_PTR(-EINVAL); |
| 1527 | } |
| 1528 | |
| 1529 | static kprobe_opcode_t *kprobe_addr(struct kprobe *p) |
| 1530 | { |
| 1531 | return _kprobe_addr(p->addr, p->symbol_name, p->offset); |
| 1532 | } |
| 1533 | |
| 1534 | /* Check passed kprobe is valid and return kprobe in kprobe_table. */ |
| 1535 | static struct kprobe *__get_valid_kprobe(struct kprobe *p) |
| 1536 | { |
| 1537 | struct kprobe *ap, *list_p; |
| 1538 | |
| 1539 | ap = get_kprobe(p->addr); |
| 1540 | if (unlikely(!ap)) |
| 1541 | return NULL; |
| 1542 | |
| 1543 | if (p != ap) { |
| 1544 | list_for_each_entry_rcu(list_p, &ap->list, list) |
| 1545 | if (list_p == p) |
| 1546 | /* kprobe p is a valid probe */ |
| 1547 | goto valid; |
| 1548 | return NULL; |
| 1549 | } |
| 1550 | valid: |
| 1551 | return ap; |
| 1552 | } |
| 1553 | |
| 1554 | /* Return error if the kprobe is being re-registered */ |
| 1555 | static inline int check_kprobe_rereg(struct kprobe *p) |
| 1556 | { |
| 1557 | int ret = 0; |
| 1558 | |
| 1559 | mutex_lock(&kprobe_mutex); |
| 1560 | if (__get_valid_kprobe(p)) |
| 1561 | ret = -EINVAL; |
| 1562 | mutex_unlock(&kprobe_mutex); |
| 1563 | |
| 1564 | return ret; |
| 1565 | } |
| 1566 | |
| 1567 | int __weak arch_check_ftrace_location(struct kprobe *p) |
| 1568 | { |
| 1569 | unsigned long ftrace_addr; |
| 1570 | |
| 1571 | ftrace_addr = ftrace_location((unsigned long)p->addr); |
| 1572 | if (ftrace_addr) { |
| 1573 | #ifdef CONFIG_KPROBES_ON_FTRACE |
| 1574 | /* Given address is not on the instruction boundary */ |
| 1575 | if ((unsigned long)p->addr != ftrace_addr) |
| 1576 | return -EILSEQ; |
| 1577 | p->flags |= KPROBE_FLAG_FTRACE; |
| 1578 | #else /* !CONFIG_KPROBES_ON_FTRACE */ |
| 1579 | return -EINVAL; |
| 1580 | #endif |
| 1581 | } |
| 1582 | return 0; |
| 1583 | } |
| 1584 | |
| 1585 | static int check_kprobe_address_safe(struct kprobe *p, |
| 1586 | struct module **probed_mod) |
| 1587 | { |
| 1588 | int ret; |
| 1589 | |
| 1590 | ret = arch_check_ftrace_location(p); |
| 1591 | if (ret) |
| 1592 | return ret; |
| 1593 | jump_label_lock(); |
| 1594 | preempt_disable(); |
| 1595 | |
| 1596 | /* Ensure it is not in reserved area nor out of text */ |
| 1597 | if (!kernel_text_address((unsigned long) p->addr) || |
| 1598 | within_kprobe_blacklist((unsigned long) p->addr) || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1599 | jump_label_text_reserved(p->addr, p->addr) || |
| 1600 | find_bug((unsigned long)p->addr)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1601 | ret = -EINVAL; |
| 1602 | goto out; |
| 1603 | } |
| 1604 | |
| 1605 | /* Check if are we probing a module */ |
| 1606 | *probed_mod = __module_text_address((unsigned long) p->addr); |
| 1607 | if (*probed_mod) { |
| 1608 | /* |
| 1609 | * We must hold a refcount of the probed module while updating |
| 1610 | * its code to prohibit unexpected unloading. |
| 1611 | */ |
| 1612 | if (unlikely(!try_module_get(*probed_mod))) { |
| 1613 | ret = -ENOENT; |
| 1614 | goto out; |
| 1615 | } |
| 1616 | |
| 1617 | /* |
| 1618 | * If the module freed .init.text, we couldn't insert |
| 1619 | * kprobes in there. |
| 1620 | */ |
| 1621 | if (within_module_init((unsigned long)p->addr, *probed_mod) && |
| 1622 | (*probed_mod)->state != MODULE_STATE_COMING) { |
| 1623 | module_put(*probed_mod); |
| 1624 | *probed_mod = NULL; |
| 1625 | ret = -ENOENT; |
| 1626 | } |
| 1627 | } |
| 1628 | out: |
| 1629 | preempt_enable(); |
| 1630 | jump_label_unlock(); |
| 1631 | |
| 1632 | return ret; |
| 1633 | } |
| 1634 | |
| 1635 | int register_kprobe(struct kprobe *p) |
| 1636 | { |
| 1637 | int ret; |
| 1638 | struct kprobe *old_p; |
| 1639 | struct module *probed_mod; |
| 1640 | kprobe_opcode_t *addr; |
| 1641 | |
| 1642 | /* Adjust probe address from symbol */ |
| 1643 | addr = kprobe_addr(p); |
| 1644 | if (IS_ERR(addr)) |
| 1645 | return PTR_ERR(addr); |
| 1646 | p->addr = addr; |
| 1647 | |
| 1648 | ret = check_kprobe_rereg(p); |
| 1649 | if (ret) |
| 1650 | return ret; |
| 1651 | |
| 1652 | /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */ |
| 1653 | p->flags &= KPROBE_FLAG_DISABLED; |
| 1654 | p->nmissed = 0; |
| 1655 | INIT_LIST_HEAD(&p->list); |
| 1656 | |
| 1657 | ret = check_kprobe_address_safe(p, &probed_mod); |
| 1658 | if (ret) |
| 1659 | return ret; |
| 1660 | |
| 1661 | mutex_lock(&kprobe_mutex); |
| 1662 | |
| 1663 | old_p = get_kprobe(p->addr); |
| 1664 | if (old_p) { |
| 1665 | /* Since this may unoptimize old_p, locking text_mutex. */ |
| 1666 | ret = register_aggr_kprobe(old_p, p); |
| 1667 | goto out; |
| 1668 | } |
| 1669 | |
| 1670 | cpus_read_lock(); |
| 1671 | /* Prevent text modification */ |
| 1672 | mutex_lock(&text_mutex); |
| 1673 | ret = prepare_kprobe(p); |
| 1674 | mutex_unlock(&text_mutex); |
| 1675 | cpus_read_unlock(); |
| 1676 | if (ret) |
| 1677 | goto out; |
| 1678 | |
| 1679 | INIT_HLIST_NODE(&p->hlist); |
| 1680 | hlist_add_head_rcu(&p->hlist, |
| 1681 | &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]); |
| 1682 | |
| 1683 | if (!kprobes_all_disarmed && !kprobe_disabled(p)) { |
| 1684 | ret = arm_kprobe(p); |
| 1685 | if (ret) { |
| 1686 | hlist_del_rcu(&p->hlist); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1687 | synchronize_rcu(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1688 | goto out; |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | /* Try to optimize kprobe */ |
| 1693 | try_to_optimize_kprobe(p); |
| 1694 | out: |
| 1695 | mutex_unlock(&kprobe_mutex); |
| 1696 | |
| 1697 | if (probed_mod) |
| 1698 | module_put(probed_mod); |
| 1699 | |
| 1700 | return ret; |
| 1701 | } |
| 1702 | EXPORT_SYMBOL_GPL(register_kprobe); |
| 1703 | |
| 1704 | /* Check if all probes on the aggrprobe are disabled */ |
| 1705 | static int aggr_kprobe_disabled(struct kprobe *ap) |
| 1706 | { |
| 1707 | struct kprobe *kp; |
| 1708 | |
| 1709 | list_for_each_entry_rcu(kp, &ap->list, list) |
| 1710 | if (!kprobe_disabled(kp)) |
| 1711 | /* |
| 1712 | * There is an active probe on the list. |
| 1713 | * We can't disable this ap. |
| 1714 | */ |
| 1715 | return 0; |
| 1716 | |
| 1717 | return 1; |
| 1718 | } |
| 1719 | |
| 1720 | /* Disable one kprobe: Make sure called under kprobe_mutex is locked */ |
| 1721 | static struct kprobe *__disable_kprobe(struct kprobe *p) |
| 1722 | { |
| 1723 | struct kprobe *orig_p; |
| 1724 | int ret; |
| 1725 | |
| 1726 | /* Get an original kprobe for return */ |
| 1727 | orig_p = __get_valid_kprobe(p); |
| 1728 | if (unlikely(orig_p == NULL)) |
| 1729 | return ERR_PTR(-EINVAL); |
| 1730 | |
| 1731 | if (!kprobe_disabled(p)) { |
| 1732 | /* Disable probe if it is a child probe */ |
| 1733 | if (p != orig_p) |
| 1734 | p->flags |= KPROBE_FLAG_DISABLED; |
| 1735 | |
| 1736 | /* Try to disarm and disable this/parent probe */ |
| 1737 | if (p == orig_p || aggr_kprobe_disabled(orig_p)) { |
| 1738 | /* |
| 1739 | * If kprobes_all_disarmed is set, orig_p |
| 1740 | * should have already been disarmed, so |
| 1741 | * skip unneed disarming process. |
| 1742 | */ |
| 1743 | if (!kprobes_all_disarmed) { |
| 1744 | ret = disarm_kprobe(orig_p, true); |
| 1745 | if (ret) { |
| 1746 | p->flags &= ~KPROBE_FLAG_DISABLED; |
| 1747 | return ERR_PTR(ret); |
| 1748 | } |
| 1749 | } |
| 1750 | orig_p->flags |= KPROBE_FLAG_DISABLED; |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | return orig_p; |
| 1755 | } |
| 1756 | |
| 1757 | /* |
| 1758 | * Unregister a kprobe without a scheduler synchronization. |
| 1759 | */ |
| 1760 | static int __unregister_kprobe_top(struct kprobe *p) |
| 1761 | { |
| 1762 | struct kprobe *ap, *list_p; |
| 1763 | |
| 1764 | /* Disable kprobe. This will disarm it if needed. */ |
| 1765 | ap = __disable_kprobe(p); |
| 1766 | if (IS_ERR(ap)) |
| 1767 | return PTR_ERR(ap); |
| 1768 | |
| 1769 | if (ap == p) |
| 1770 | /* |
| 1771 | * This probe is an independent(and non-optimized) kprobe |
| 1772 | * (not an aggrprobe). Remove from the hash list. |
| 1773 | */ |
| 1774 | goto disarmed; |
| 1775 | |
| 1776 | /* Following process expects this probe is an aggrprobe */ |
| 1777 | WARN_ON(!kprobe_aggrprobe(ap)); |
| 1778 | |
| 1779 | if (list_is_singular(&ap->list) && kprobe_disarmed(ap)) |
| 1780 | /* |
| 1781 | * !disarmed could be happen if the probe is under delayed |
| 1782 | * unoptimizing. |
| 1783 | */ |
| 1784 | goto disarmed; |
| 1785 | else { |
| 1786 | /* If disabling probe has special handlers, update aggrprobe */ |
| 1787 | if (p->post_handler && !kprobe_gone(p)) { |
| 1788 | list_for_each_entry_rcu(list_p, &ap->list, list) { |
| 1789 | if ((list_p != p) && (list_p->post_handler)) |
| 1790 | goto noclean; |
| 1791 | } |
| 1792 | ap->post_handler = NULL; |
| 1793 | } |
| 1794 | noclean: |
| 1795 | /* |
| 1796 | * Remove from the aggrprobe: this path will do nothing in |
| 1797 | * __unregister_kprobe_bottom(). |
| 1798 | */ |
| 1799 | list_del_rcu(&p->list); |
| 1800 | if (!kprobe_disabled(ap) && !kprobes_all_disarmed) |
| 1801 | /* |
| 1802 | * Try to optimize this probe again, because post |
| 1803 | * handler may have been changed. |
| 1804 | */ |
| 1805 | optimize_kprobe(ap); |
| 1806 | } |
| 1807 | return 0; |
| 1808 | |
| 1809 | disarmed: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1810 | hlist_del_rcu(&ap->hlist); |
| 1811 | return 0; |
| 1812 | } |
| 1813 | |
| 1814 | static void __unregister_kprobe_bottom(struct kprobe *p) |
| 1815 | { |
| 1816 | struct kprobe *ap; |
| 1817 | |
| 1818 | if (list_empty(&p->list)) |
| 1819 | /* This is an independent kprobe */ |
| 1820 | arch_remove_kprobe(p); |
| 1821 | else if (list_is_singular(&p->list)) { |
| 1822 | /* This is the last child of an aggrprobe */ |
| 1823 | ap = list_entry(p->list.next, struct kprobe, list); |
| 1824 | list_del(&p->list); |
| 1825 | free_aggr_kprobe(ap); |
| 1826 | } |
| 1827 | /* Otherwise, do nothing. */ |
| 1828 | } |
| 1829 | |
| 1830 | int register_kprobes(struct kprobe **kps, int num) |
| 1831 | { |
| 1832 | int i, ret = 0; |
| 1833 | |
| 1834 | if (num <= 0) |
| 1835 | return -EINVAL; |
| 1836 | for (i = 0; i < num; i++) { |
| 1837 | ret = register_kprobe(kps[i]); |
| 1838 | if (ret < 0) { |
| 1839 | if (i > 0) |
| 1840 | unregister_kprobes(kps, i); |
| 1841 | break; |
| 1842 | } |
| 1843 | } |
| 1844 | return ret; |
| 1845 | } |
| 1846 | EXPORT_SYMBOL_GPL(register_kprobes); |
| 1847 | |
| 1848 | void unregister_kprobe(struct kprobe *p) |
| 1849 | { |
| 1850 | unregister_kprobes(&p, 1); |
| 1851 | } |
| 1852 | EXPORT_SYMBOL_GPL(unregister_kprobe); |
| 1853 | |
| 1854 | void unregister_kprobes(struct kprobe **kps, int num) |
| 1855 | { |
| 1856 | int i; |
| 1857 | |
| 1858 | if (num <= 0) |
| 1859 | return; |
| 1860 | mutex_lock(&kprobe_mutex); |
| 1861 | for (i = 0; i < num; i++) |
| 1862 | if (__unregister_kprobe_top(kps[i]) < 0) |
| 1863 | kps[i]->addr = NULL; |
| 1864 | mutex_unlock(&kprobe_mutex); |
| 1865 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1866 | synchronize_rcu(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1867 | for (i = 0; i < num; i++) |
| 1868 | if (kps[i]->addr) |
| 1869 | __unregister_kprobe_bottom(kps[i]); |
| 1870 | } |
| 1871 | EXPORT_SYMBOL_GPL(unregister_kprobes); |
| 1872 | |
| 1873 | int __weak kprobe_exceptions_notify(struct notifier_block *self, |
| 1874 | unsigned long val, void *data) |
| 1875 | { |
| 1876 | return NOTIFY_DONE; |
| 1877 | } |
| 1878 | NOKPROBE_SYMBOL(kprobe_exceptions_notify); |
| 1879 | |
| 1880 | static struct notifier_block kprobe_exceptions_nb = { |
| 1881 | .notifier_call = kprobe_exceptions_notify, |
| 1882 | .priority = 0x7fffffff /* we need to be notified first */ |
| 1883 | }; |
| 1884 | |
| 1885 | unsigned long __weak arch_deref_entry_point(void *entry) |
| 1886 | { |
| 1887 | return (unsigned long)entry; |
| 1888 | } |
| 1889 | |
| 1890 | #ifdef CONFIG_KRETPROBES |
| 1891 | /* |
| 1892 | * This kprobe pre_handler is registered with every kretprobe. When probe |
| 1893 | * hits it will set up the return probe. |
| 1894 | */ |
| 1895 | static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) |
| 1896 | { |
| 1897 | struct kretprobe *rp = container_of(p, struct kretprobe, kp); |
| 1898 | unsigned long hash, flags = 0; |
| 1899 | struct kretprobe_instance *ri; |
| 1900 | |
| 1901 | /* |
| 1902 | * To avoid deadlocks, prohibit return probing in NMI contexts, |
| 1903 | * just skip the probe and increase the (inexact) 'nmissed' |
| 1904 | * statistical counter, so that the user is informed that |
| 1905 | * something happened: |
| 1906 | */ |
| 1907 | if (unlikely(in_nmi())) { |
| 1908 | rp->nmissed++; |
| 1909 | return 0; |
| 1910 | } |
| 1911 | |
| 1912 | /* TODO: consider to only swap the RA after the last pre_handler fired */ |
| 1913 | hash = hash_ptr(current, KPROBE_HASH_BITS); |
| 1914 | raw_spin_lock_irqsave(&rp->lock, flags); |
| 1915 | if (!hlist_empty(&rp->free_instances)) { |
| 1916 | ri = hlist_entry(rp->free_instances.first, |
| 1917 | struct kretprobe_instance, hlist); |
| 1918 | hlist_del(&ri->hlist); |
| 1919 | raw_spin_unlock_irqrestore(&rp->lock, flags); |
| 1920 | |
| 1921 | ri->rp = rp; |
| 1922 | ri->task = current; |
| 1923 | |
| 1924 | if (rp->entry_handler && rp->entry_handler(ri, regs)) { |
| 1925 | raw_spin_lock_irqsave(&rp->lock, flags); |
| 1926 | hlist_add_head(&ri->hlist, &rp->free_instances); |
| 1927 | raw_spin_unlock_irqrestore(&rp->lock, flags); |
| 1928 | return 0; |
| 1929 | } |
| 1930 | |
| 1931 | arch_prepare_kretprobe(ri, regs); |
| 1932 | |
| 1933 | /* XXX(hch): why is there no hlist_move_head? */ |
| 1934 | INIT_HLIST_NODE(&ri->hlist); |
| 1935 | kretprobe_table_lock(hash, &flags); |
| 1936 | hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]); |
| 1937 | kretprobe_table_unlock(hash, &flags); |
| 1938 | } else { |
| 1939 | rp->nmissed++; |
| 1940 | raw_spin_unlock_irqrestore(&rp->lock, flags); |
| 1941 | } |
| 1942 | return 0; |
| 1943 | } |
| 1944 | NOKPROBE_SYMBOL(pre_handler_kretprobe); |
| 1945 | |
| 1946 | bool __weak arch_kprobe_on_func_entry(unsigned long offset) |
| 1947 | { |
| 1948 | return !offset; |
| 1949 | } |
| 1950 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1951 | /** |
| 1952 | * kprobe_on_func_entry() -- check whether given address is function entry |
| 1953 | * @addr: Target address |
| 1954 | * @sym: Target symbol name |
| 1955 | * @offset: The offset from the symbol or the address |
| 1956 | * |
| 1957 | * This checks whether the given @addr+@offset or @sym+@offset is on the |
| 1958 | * function entry address or not. |
| 1959 | * This returns 0 if it is the function entry, or -EINVAL if it is not. |
| 1960 | * And also it returns -ENOENT if it fails the symbol or address lookup. |
| 1961 | * Caller must pass @addr or @sym (either one must be NULL), or this |
| 1962 | * returns -EINVAL. |
| 1963 | */ |
| 1964 | int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1965 | { |
| 1966 | kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset); |
| 1967 | |
| 1968 | if (IS_ERR(kp_addr)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1969 | return PTR_ERR(kp_addr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1970 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1971 | if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset)) |
| 1972 | return -ENOENT; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1973 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1974 | if (!arch_kprobe_on_func_entry(offset)) |
| 1975 | return -EINVAL; |
| 1976 | |
| 1977 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | int register_kretprobe(struct kretprobe *rp) |
| 1981 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1982 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1983 | struct kretprobe_instance *inst; |
| 1984 | int i; |
| 1985 | void *addr; |
| 1986 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1987 | ret = kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset); |
| 1988 | if (ret) |
| 1989 | return ret; |
| 1990 | |
| 1991 | /* If only rp->kp.addr is specified, check reregistering kprobes */ |
| 1992 | if (rp->kp.addr && check_kprobe_rereg(&rp->kp)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1993 | return -EINVAL; |
| 1994 | |
| 1995 | if (kretprobe_blacklist_size) { |
| 1996 | addr = kprobe_addr(&rp->kp); |
| 1997 | if (IS_ERR(addr)) |
| 1998 | return PTR_ERR(addr); |
| 1999 | |
| 2000 | for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { |
| 2001 | if (kretprobe_blacklist[i].addr == addr) |
| 2002 | return -EINVAL; |
| 2003 | } |
| 2004 | } |
| 2005 | |
| 2006 | rp->kp.pre_handler = pre_handler_kretprobe; |
| 2007 | rp->kp.post_handler = NULL; |
| 2008 | rp->kp.fault_handler = NULL; |
| 2009 | |
| 2010 | /* Pre-allocate memory for max kretprobe instances */ |
| 2011 | if (rp->maxactive <= 0) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2012 | #ifdef CONFIG_PREEMPTION |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2013 | rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus()); |
| 2014 | #else |
| 2015 | rp->maxactive = num_possible_cpus(); |
| 2016 | #endif |
| 2017 | } |
| 2018 | raw_spin_lock_init(&rp->lock); |
| 2019 | INIT_HLIST_HEAD(&rp->free_instances); |
| 2020 | for (i = 0; i < rp->maxactive; i++) { |
| 2021 | inst = kmalloc(sizeof(struct kretprobe_instance) + |
| 2022 | rp->data_size, GFP_KERNEL); |
| 2023 | if (inst == NULL) { |
| 2024 | free_rp_inst(rp); |
| 2025 | return -ENOMEM; |
| 2026 | } |
| 2027 | INIT_HLIST_NODE(&inst->hlist); |
| 2028 | hlist_add_head(&inst->hlist, &rp->free_instances); |
| 2029 | } |
| 2030 | |
| 2031 | rp->nmissed = 0; |
| 2032 | /* Establish function entry probe point */ |
| 2033 | ret = register_kprobe(&rp->kp); |
| 2034 | if (ret != 0) |
| 2035 | free_rp_inst(rp); |
| 2036 | return ret; |
| 2037 | } |
| 2038 | EXPORT_SYMBOL_GPL(register_kretprobe); |
| 2039 | |
| 2040 | int register_kretprobes(struct kretprobe **rps, int num) |
| 2041 | { |
| 2042 | int ret = 0, i; |
| 2043 | |
| 2044 | if (num <= 0) |
| 2045 | return -EINVAL; |
| 2046 | for (i = 0; i < num; i++) { |
| 2047 | ret = register_kretprobe(rps[i]); |
| 2048 | if (ret < 0) { |
| 2049 | if (i > 0) |
| 2050 | unregister_kretprobes(rps, i); |
| 2051 | break; |
| 2052 | } |
| 2053 | } |
| 2054 | return ret; |
| 2055 | } |
| 2056 | EXPORT_SYMBOL_GPL(register_kretprobes); |
| 2057 | |
| 2058 | void unregister_kretprobe(struct kretprobe *rp) |
| 2059 | { |
| 2060 | unregister_kretprobes(&rp, 1); |
| 2061 | } |
| 2062 | EXPORT_SYMBOL_GPL(unregister_kretprobe); |
| 2063 | |
| 2064 | void unregister_kretprobes(struct kretprobe **rps, int num) |
| 2065 | { |
| 2066 | int i; |
| 2067 | |
| 2068 | if (num <= 0) |
| 2069 | return; |
| 2070 | mutex_lock(&kprobe_mutex); |
| 2071 | for (i = 0; i < num; i++) |
| 2072 | if (__unregister_kprobe_top(&rps[i]->kp) < 0) |
| 2073 | rps[i]->kp.addr = NULL; |
| 2074 | mutex_unlock(&kprobe_mutex); |
| 2075 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2076 | synchronize_rcu(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2077 | for (i = 0; i < num; i++) { |
| 2078 | if (rps[i]->kp.addr) { |
| 2079 | __unregister_kprobe_bottom(&rps[i]->kp); |
| 2080 | cleanup_rp_inst(rps[i]); |
| 2081 | } |
| 2082 | } |
| 2083 | } |
| 2084 | EXPORT_SYMBOL_GPL(unregister_kretprobes); |
| 2085 | |
| 2086 | #else /* CONFIG_KRETPROBES */ |
| 2087 | int register_kretprobe(struct kretprobe *rp) |
| 2088 | { |
| 2089 | return -ENOSYS; |
| 2090 | } |
| 2091 | EXPORT_SYMBOL_GPL(register_kretprobe); |
| 2092 | |
| 2093 | int register_kretprobes(struct kretprobe **rps, int num) |
| 2094 | { |
| 2095 | return -ENOSYS; |
| 2096 | } |
| 2097 | EXPORT_SYMBOL_GPL(register_kretprobes); |
| 2098 | |
| 2099 | void unregister_kretprobe(struct kretprobe *rp) |
| 2100 | { |
| 2101 | } |
| 2102 | EXPORT_SYMBOL_GPL(unregister_kretprobe); |
| 2103 | |
| 2104 | void unregister_kretprobes(struct kretprobe **rps, int num) |
| 2105 | { |
| 2106 | } |
| 2107 | EXPORT_SYMBOL_GPL(unregister_kretprobes); |
| 2108 | |
| 2109 | static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) |
| 2110 | { |
| 2111 | return 0; |
| 2112 | } |
| 2113 | NOKPROBE_SYMBOL(pre_handler_kretprobe); |
| 2114 | |
| 2115 | #endif /* CONFIG_KRETPROBES */ |
| 2116 | |
| 2117 | /* Set the kprobe gone and remove its instruction buffer. */ |
| 2118 | static void kill_kprobe(struct kprobe *p) |
| 2119 | { |
| 2120 | struct kprobe *kp; |
| 2121 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2122 | if (WARN_ON_ONCE(kprobe_gone(p))) |
| 2123 | return; |
| 2124 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2125 | p->flags |= KPROBE_FLAG_GONE; |
| 2126 | if (kprobe_aggrprobe(p)) { |
| 2127 | /* |
| 2128 | * If this is an aggr_kprobe, we have to list all the |
| 2129 | * chained probes and mark them GONE. |
| 2130 | */ |
| 2131 | list_for_each_entry_rcu(kp, &p->list, list) |
| 2132 | kp->flags |= KPROBE_FLAG_GONE; |
| 2133 | p->post_handler = NULL; |
| 2134 | kill_optimized_kprobe(p); |
| 2135 | } |
| 2136 | /* |
| 2137 | * Here, we can remove insn_slot safely, because no thread calls |
| 2138 | * the original probed function (which will be freed soon) any more. |
| 2139 | */ |
| 2140 | arch_remove_kprobe(p); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2141 | |
| 2142 | /* |
| 2143 | * The module is going away. We should disarm the kprobe which |
| 2144 | * is using ftrace, because ftrace framework is still available at |
| 2145 | * MODULE_STATE_GOING notification. |
| 2146 | */ |
| 2147 | if (kprobe_ftrace(p) && !kprobe_disabled(p) && !kprobes_all_disarmed) |
| 2148 | disarm_kprobe_ftrace(p); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | /* Disable one kprobe */ |
| 2152 | int disable_kprobe(struct kprobe *kp) |
| 2153 | { |
| 2154 | int ret = 0; |
| 2155 | struct kprobe *p; |
| 2156 | |
| 2157 | mutex_lock(&kprobe_mutex); |
| 2158 | |
| 2159 | /* Disable this kprobe */ |
| 2160 | p = __disable_kprobe(kp); |
| 2161 | if (IS_ERR(p)) |
| 2162 | ret = PTR_ERR(p); |
| 2163 | |
| 2164 | mutex_unlock(&kprobe_mutex); |
| 2165 | return ret; |
| 2166 | } |
| 2167 | EXPORT_SYMBOL_GPL(disable_kprobe); |
| 2168 | |
| 2169 | /* Enable one kprobe */ |
| 2170 | int enable_kprobe(struct kprobe *kp) |
| 2171 | { |
| 2172 | int ret = 0; |
| 2173 | struct kprobe *p; |
| 2174 | |
| 2175 | mutex_lock(&kprobe_mutex); |
| 2176 | |
| 2177 | /* Check whether specified probe is valid. */ |
| 2178 | p = __get_valid_kprobe(kp); |
| 2179 | if (unlikely(p == NULL)) { |
| 2180 | ret = -EINVAL; |
| 2181 | goto out; |
| 2182 | } |
| 2183 | |
| 2184 | if (kprobe_gone(kp)) { |
| 2185 | /* This kprobe has gone, we couldn't enable it. */ |
| 2186 | ret = -EINVAL; |
| 2187 | goto out; |
| 2188 | } |
| 2189 | |
| 2190 | if (p != kp) |
| 2191 | kp->flags &= ~KPROBE_FLAG_DISABLED; |
| 2192 | |
| 2193 | if (!kprobes_all_disarmed && kprobe_disabled(p)) { |
| 2194 | p->flags &= ~KPROBE_FLAG_DISABLED; |
| 2195 | ret = arm_kprobe(p); |
| 2196 | if (ret) |
| 2197 | p->flags |= KPROBE_FLAG_DISABLED; |
| 2198 | } |
| 2199 | out: |
| 2200 | mutex_unlock(&kprobe_mutex); |
| 2201 | return ret; |
| 2202 | } |
| 2203 | EXPORT_SYMBOL_GPL(enable_kprobe); |
| 2204 | |
| 2205 | /* Caller must NOT call this in usual path. This is only for critical case */ |
| 2206 | void dump_kprobe(struct kprobe *kp) |
| 2207 | { |
| 2208 | pr_err("Dumping kprobe:\n"); |
| 2209 | pr_err("Name: %s\nOffset: %x\nAddress: %pS\n", |
| 2210 | kp->symbol_name, kp->offset, kp->addr); |
| 2211 | } |
| 2212 | NOKPROBE_SYMBOL(dump_kprobe); |
| 2213 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2214 | int kprobe_add_ksym_blacklist(unsigned long entry) |
| 2215 | { |
| 2216 | struct kprobe_blacklist_entry *ent; |
| 2217 | unsigned long offset = 0, size = 0; |
| 2218 | |
| 2219 | if (!kernel_text_address(entry) || |
| 2220 | !kallsyms_lookup_size_offset(entry, &size, &offset)) |
| 2221 | return -EINVAL; |
| 2222 | |
| 2223 | ent = kmalloc(sizeof(*ent), GFP_KERNEL); |
| 2224 | if (!ent) |
| 2225 | return -ENOMEM; |
| 2226 | ent->start_addr = entry; |
| 2227 | ent->end_addr = entry + size; |
| 2228 | INIT_LIST_HEAD(&ent->list); |
| 2229 | list_add_tail(&ent->list, &kprobe_blacklist); |
| 2230 | |
| 2231 | return (int)size; |
| 2232 | } |
| 2233 | |
| 2234 | /* Add all symbols in given area into kprobe blacklist */ |
| 2235 | int kprobe_add_area_blacklist(unsigned long start, unsigned long end) |
| 2236 | { |
| 2237 | unsigned long entry; |
| 2238 | int ret = 0; |
| 2239 | |
| 2240 | for (entry = start; entry < end; entry += ret) { |
| 2241 | ret = kprobe_add_ksym_blacklist(entry); |
| 2242 | if (ret < 0) |
| 2243 | return ret; |
| 2244 | if (ret == 0) /* In case of alias symbol */ |
| 2245 | ret = 1; |
| 2246 | } |
| 2247 | return 0; |
| 2248 | } |
| 2249 | |
| 2250 | int __init __weak arch_populate_kprobe_blacklist(void) |
| 2251 | { |
| 2252 | return 0; |
| 2253 | } |
| 2254 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2255 | /* |
| 2256 | * Lookup and populate the kprobe_blacklist. |
| 2257 | * |
| 2258 | * Unlike the kretprobe blacklist, we'll need to determine |
| 2259 | * the range of addresses that belong to the said functions, |
| 2260 | * since a kprobe need not necessarily be at the beginning |
| 2261 | * of a function. |
| 2262 | */ |
| 2263 | static int __init populate_kprobe_blacklist(unsigned long *start, |
| 2264 | unsigned long *end) |
| 2265 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2266 | unsigned long entry; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2267 | unsigned long *iter; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2268 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2269 | |
| 2270 | for (iter = start; iter < end; iter++) { |
| 2271 | entry = arch_deref_entry_point((void *)*iter); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2272 | ret = kprobe_add_ksym_blacklist(entry); |
| 2273 | if (ret == -EINVAL) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2274 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2275 | if (ret < 0) |
| 2276 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2277 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2278 | |
| 2279 | /* Symbols in __kprobes_text are blacklisted */ |
| 2280 | ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start, |
| 2281 | (unsigned long)__kprobes_text_end); |
| 2282 | |
| 2283 | return ret ? : arch_populate_kprobe_blacklist(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | /* Module notifier call back, checking kprobes on the module */ |
| 2287 | static int kprobes_module_callback(struct notifier_block *nb, |
| 2288 | unsigned long val, void *data) |
| 2289 | { |
| 2290 | struct module *mod = data; |
| 2291 | struct hlist_head *head; |
| 2292 | struct kprobe *p; |
| 2293 | unsigned int i; |
| 2294 | int checkcore = (val == MODULE_STATE_GOING); |
| 2295 | |
| 2296 | if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE) |
| 2297 | return NOTIFY_DONE; |
| 2298 | |
| 2299 | /* |
| 2300 | * When MODULE_STATE_GOING was notified, both of module .text and |
| 2301 | * .init.text sections would be freed. When MODULE_STATE_LIVE was |
| 2302 | * notified, only .init.text section would be freed. We need to |
| 2303 | * disable kprobes which have been inserted in the sections. |
| 2304 | */ |
| 2305 | mutex_lock(&kprobe_mutex); |
| 2306 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 2307 | head = &kprobe_table[i]; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2308 | hlist_for_each_entry_rcu(p, head, hlist) { |
| 2309 | if (kprobe_gone(p)) |
| 2310 | continue; |
| 2311 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2312 | if (within_module_init((unsigned long)p->addr, mod) || |
| 2313 | (checkcore && |
| 2314 | within_module_core((unsigned long)p->addr, mod))) { |
| 2315 | /* |
| 2316 | * The vaddr this probe is installed will soon |
| 2317 | * be vfreed buy not synced to disk. Hence, |
| 2318 | * disarming the breakpoint isn't needed. |
| 2319 | * |
| 2320 | * Note, this will also move any optimized probes |
| 2321 | * that are pending to be removed from their |
| 2322 | * corresponding lists to the freeing_list and |
| 2323 | * will not be touched by the delayed |
| 2324 | * kprobe_optimizer work handler. |
| 2325 | */ |
| 2326 | kill_kprobe(p); |
| 2327 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2328 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2329 | } |
| 2330 | mutex_unlock(&kprobe_mutex); |
| 2331 | return NOTIFY_DONE; |
| 2332 | } |
| 2333 | |
| 2334 | static struct notifier_block kprobe_module_nb = { |
| 2335 | .notifier_call = kprobes_module_callback, |
| 2336 | .priority = 0 |
| 2337 | }; |
| 2338 | |
| 2339 | /* Markers of _kprobe_blacklist section */ |
| 2340 | extern unsigned long __start_kprobe_blacklist[]; |
| 2341 | extern unsigned long __stop_kprobe_blacklist[]; |
| 2342 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2343 | void kprobe_free_init_mem(void) |
| 2344 | { |
| 2345 | void *start = (void *)(&__init_begin); |
| 2346 | void *end = (void *)(&__init_end); |
| 2347 | struct hlist_head *head; |
| 2348 | struct kprobe *p; |
| 2349 | int i; |
| 2350 | |
| 2351 | mutex_lock(&kprobe_mutex); |
| 2352 | |
| 2353 | /* Kill all kprobes on initmem */ |
| 2354 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 2355 | head = &kprobe_table[i]; |
| 2356 | hlist_for_each_entry(p, head, hlist) { |
| 2357 | if (start <= (void *)p->addr && (void *)p->addr < end) |
| 2358 | kill_kprobe(p); |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | mutex_unlock(&kprobe_mutex); |
| 2363 | } |
| 2364 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2365 | static int __init init_kprobes(void) |
| 2366 | { |
| 2367 | int i, err = 0; |
| 2368 | |
| 2369 | /* FIXME allocate the probe table, currently defined statically */ |
| 2370 | /* initialize all list heads */ |
| 2371 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 2372 | INIT_HLIST_HEAD(&kprobe_table[i]); |
| 2373 | INIT_HLIST_HEAD(&kretprobe_inst_table[i]); |
| 2374 | raw_spin_lock_init(&(kretprobe_table_locks[i].lock)); |
| 2375 | } |
| 2376 | |
| 2377 | err = populate_kprobe_blacklist(__start_kprobe_blacklist, |
| 2378 | __stop_kprobe_blacklist); |
| 2379 | if (err) { |
| 2380 | pr_err("kprobes: failed to populate blacklist: %d\n", err); |
| 2381 | pr_err("Please take care of using kprobes.\n"); |
| 2382 | } |
| 2383 | |
| 2384 | if (kretprobe_blacklist_size) { |
| 2385 | /* lookup the function address from its name */ |
| 2386 | for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { |
| 2387 | kretprobe_blacklist[i].addr = |
| 2388 | kprobe_lookup_name(kretprobe_blacklist[i].name, 0); |
| 2389 | if (!kretprobe_blacklist[i].addr) |
| 2390 | printk("kretprobe: lookup failed: %s\n", |
| 2391 | kretprobe_blacklist[i].name); |
| 2392 | } |
| 2393 | } |
| 2394 | |
| 2395 | #if defined(CONFIG_OPTPROBES) |
| 2396 | #if defined(__ARCH_WANT_KPROBES_INSN_SLOT) |
| 2397 | /* Init kprobe_optinsn_slots */ |
| 2398 | kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE; |
| 2399 | #endif |
| 2400 | /* By default, kprobes can be optimized */ |
| 2401 | kprobes_allow_optimization = true; |
| 2402 | #endif |
| 2403 | |
| 2404 | /* By default, kprobes are armed */ |
| 2405 | kprobes_all_disarmed = false; |
| 2406 | |
| 2407 | err = arch_init_kprobes(); |
| 2408 | if (!err) |
| 2409 | err = register_die_notifier(&kprobe_exceptions_nb); |
| 2410 | if (!err) |
| 2411 | err = register_module_notifier(&kprobe_module_nb); |
| 2412 | |
| 2413 | kprobes_initialized = (err == 0); |
| 2414 | |
| 2415 | if (!err) |
| 2416 | init_test_probes(); |
| 2417 | return err; |
| 2418 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2419 | subsys_initcall(init_kprobes); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2420 | |
| 2421 | #ifdef CONFIG_DEBUG_FS |
| 2422 | static void report_probe(struct seq_file *pi, struct kprobe *p, |
| 2423 | const char *sym, int offset, char *modname, struct kprobe *pp) |
| 2424 | { |
| 2425 | char *kprobe_type; |
| 2426 | void *addr = p->addr; |
| 2427 | |
| 2428 | if (p->pre_handler == pre_handler_kretprobe) |
| 2429 | kprobe_type = "r"; |
| 2430 | else |
| 2431 | kprobe_type = "k"; |
| 2432 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2433 | if (!kallsyms_show_value(pi->file->f_cred)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2434 | addr = NULL; |
| 2435 | |
| 2436 | if (sym) |
| 2437 | seq_printf(pi, "%px %s %s+0x%x %s ", |
| 2438 | addr, kprobe_type, sym, offset, |
| 2439 | (modname ? modname : " ")); |
| 2440 | else /* try to use %pS */ |
| 2441 | seq_printf(pi, "%px %s %pS ", |
| 2442 | addr, kprobe_type, p->addr); |
| 2443 | |
| 2444 | if (!pp) |
| 2445 | pp = p; |
| 2446 | seq_printf(pi, "%s%s%s%s\n", |
| 2447 | (kprobe_gone(p) ? "[GONE]" : ""), |
| 2448 | ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""), |
| 2449 | (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""), |
| 2450 | (kprobe_ftrace(pp) ? "[FTRACE]" : "")); |
| 2451 | } |
| 2452 | |
| 2453 | static void *kprobe_seq_start(struct seq_file *f, loff_t *pos) |
| 2454 | { |
| 2455 | return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL; |
| 2456 | } |
| 2457 | |
| 2458 | static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos) |
| 2459 | { |
| 2460 | (*pos)++; |
| 2461 | if (*pos >= KPROBE_TABLE_SIZE) |
| 2462 | return NULL; |
| 2463 | return pos; |
| 2464 | } |
| 2465 | |
| 2466 | static void kprobe_seq_stop(struct seq_file *f, void *v) |
| 2467 | { |
| 2468 | /* Nothing to do */ |
| 2469 | } |
| 2470 | |
| 2471 | static int show_kprobe_addr(struct seq_file *pi, void *v) |
| 2472 | { |
| 2473 | struct hlist_head *head; |
| 2474 | struct kprobe *p, *kp; |
| 2475 | const char *sym = NULL; |
| 2476 | unsigned int i = *(loff_t *) v; |
| 2477 | unsigned long offset = 0; |
| 2478 | char *modname, namebuf[KSYM_NAME_LEN]; |
| 2479 | |
| 2480 | head = &kprobe_table[i]; |
| 2481 | preempt_disable(); |
| 2482 | hlist_for_each_entry_rcu(p, head, hlist) { |
| 2483 | sym = kallsyms_lookup((unsigned long)p->addr, NULL, |
| 2484 | &offset, &modname, namebuf); |
| 2485 | if (kprobe_aggrprobe(p)) { |
| 2486 | list_for_each_entry_rcu(kp, &p->list, list) |
| 2487 | report_probe(pi, kp, sym, offset, modname, p); |
| 2488 | } else |
| 2489 | report_probe(pi, p, sym, offset, modname, NULL); |
| 2490 | } |
| 2491 | preempt_enable(); |
| 2492 | return 0; |
| 2493 | } |
| 2494 | |
| 2495 | static const struct seq_operations kprobes_seq_ops = { |
| 2496 | .start = kprobe_seq_start, |
| 2497 | .next = kprobe_seq_next, |
| 2498 | .stop = kprobe_seq_stop, |
| 2499 | .show = show_kprobe_addr |
| 2500 | }; |
| 2501 | |
| 2502 | static int kprobes_open(struct inode *inode, struct file *filp) |
| 2503 | { |
| 2504 | return seq_open(filp, &kprobes_seq_ops); |
| 2505 | } |
| 2506 | |
| 2507 | static const struct file_operations debugfs_kprobes_operations = { |
| 2508 | .open = kprobes_open, |
| 2509 | .read = seq_read, |
| 2510 | .llseek = seq_lseek, |
| 2511 | .release = seq_release, |
| 2512 | }; |
| 2513 | |
| 2514 | /* kprobes/blacklist -- shows which functions can not be probed */ |
| 2515 | static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos) |
| 2516 | { |
| 2517 | return seq_list_start(&kprobe_blacklist, *pos); |
| 2518 | } |
| 2519 | |
| 2520 | static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos) |
| 2521 | { |
| 2522 | return seq_list_next(v, &kprobe_blacklist, pos); |
| 2523 | } |
| 2524 | |
| 2525 | static int kprobe_blacklist_seq_show(struct seq_file *m, void *v) |
| 2526 | { |
| 2527 | struct kprobe_blacklist_entry *ent = |
| 2528 | list_entry(v, struct kprobe_blacklist_entry, list); |
| 2529 | |
| 2530 | /* |
| 2531 | * If /proc/kallsyms is not showing kernel address, we won't |
| 2532 | * show them here either. |
| 2533 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2534 | if (!kallsyms_show_value(m->file->f_cred)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2535 | seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL, |
| 2536 | (void *)ent->start_addr); |
| 2537 | else |
| 2538 | seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr, |
| 2539 | (void *)ent->end_addr, (void *)ent->start_addr); |
| 2540 | return 0; |
| 2541 | } |
| 2542 | |
| 2543 | static const struct seq_operations kprobe_blacklist_seq_ops = { |
| 2544 | .start = kprobe_blacklist_seq_start, |
| 2545 | .next = kprobe_blacklist_seq_next, |
| 2546 | .stop = kprobe_seq_stop, /* Reuse void function */ |
| 2547 | .show = kprobe_blacklist_seq_show, |
| 2548 | }; |
| 2549 | |
| 2550 | static int kprobe_blacklist_open(struct inode *inode, struct file *filp) |
| 2551 | { |
| 2552 | return seq_open(filp, &kprobe_blacklist_seq_ops); |
| 2553 | } |
| 2554 | |
| 2555 | static const struct file_operations debugfs_kprobe_blacklist_ops = { |
| 2556 | .open = kprobe_blacklist_open, |
| 2557 | .read = seq_read, |
| 2558 | .llseek = seq_lseek, |
| 2559 | .release = seq_release, |
| 2560 | }; |
| 2561 | |
| 2562 | static int arm_all_kprobes(void) |
| 2563 | { |
| 2564 | struct hlist_head *head; |
| 2565 | struct kprobe *p; |
| 2566 | unsigned int i, total = 0, errors = 0; |
| 2567 | int err, ret = 0; |
| 2568 | |
| 2569 | mutex_lock(&kprobe_mutex); |
| 2570 | |
| 2571 | /* If kprobes are armed, just return */ |
| 2572 | if (!kprobes_all_disarmed) |
| 2573 | goto already_enabled; |
| 2574 | |
| 2575 | /* |
| 2576 | * optimize_kprobe() called by arm_kprobe() checks |
| 2577 | * kprobes_all_disarmed, so set kprobes_all_disarmed before |
| 2578 | * arm_kprobe. |
| 2579 | */ |
| 2580 | kprobes_all_disarmed = false; |
| 2581 | /* Arming kprobes doesn't optimize kprobe itself */ |
| 2582 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 2583 | head = &kprobe_table[i]; |
| 2584 | /* Arm all kprobes on a best-effort basis */ |
| 2585 | hlist_for_each_entry_rcu(p, head, hlist) { |
| 2586 | if (!kprobe_disabled(p)) { |
| 2587 | err = arm_kprobe(p); |
| 2588 | if (err) { |
| 2589 | errors++; |
| 2590 | ret = err; |
| 2591 | } |
| 2592 | total++; |
| 2593 | } |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | if (errors) |
| 2598 | pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n", |
| 2599 | errors, total); |
| 2600 | else |
| 2601 | pr_info("Kprobes globally enabled\n"); |
| 2602 | |
| 2603 | already_enabled: |
| 2604 | mutex_unlock(&kprobe_mutex); |
| 2605 | return ret; |
| 2606 | } |
| 2607 | |
| 2608 | static int disarm_all_kprobes(void) |
| 2609 | { |
| 2610 | struct hlist_head *head; |
| 2611 | struct kprobe *p; |
| 2612 | unsigned int i, total = 0, errors = 0; |
| 2613 | int err, ret = 0; |
| 2614 | |
| 2615 | mutex_lock(&kprobe_mutex); |
| 2616 | |
| 2617 | /* If kprobes are already disarmed, just return */ |
| 2618 | if (kprobes_all_disarmed) { |
| 2619 | mutex_unlock(&kprobe_mutex); |
| 2620 | return 0; |
| 2621 | } |
| 2622 | |
| 2623 | kprobes_all_disarmed = true; |
| 2624 | |
| 2625 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 2626 | head = &kprobe_table[i]; |
| 2627 | /* Disarm all kprobes on a best-effort basis */ |
| 2628 | hlist_for_each_entry_rcu(p, head, hlist) { |
| 2629 | if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) { |
| 2630 | err = disarm_kprobe(p, false); |
| 2631 | if (err) { |
| 2632 | errors++; |
| 2633 | ret = err; |
| 2634 | } |
| 2635 | total++; |
| 2636 | } |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | if (errors) |
| 2641 | pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n", |
| 2642 | errors, total); |
| 2643 | else |
| 2644 | pr_info("Kprobes globally disabled\n"); |
| 2645 | |
| 2646 | mutex_unlock(&kprobe_mutex); |
| 2647 | |
| 2648 | /* Wait for disarming all kprobes by optimizer */ |
| 2649 | wait_for_kprobe_optimizer(); |
| 2650 | |
| 2651 | return ret; |
| 2652 | } |
| 2653 | |
| 2654 | /* |
| 2655 | * XXX: The debugfs bool file interface doesn't allow for callbacks |
| 2656 | * when the bool state is switched. We can reuse that facility when |
| 2657 | * available |
| 2658 | */ |
| 2659 | static ssize_t read_enabled_file_bool(struct file *file, |
| 2660 | char __user *user_buf, size_t count, loff_t *ppos) |
| 2661 | { |
| 2662 | char buf[3]; |
| 2663 | |
| 2664 | if (!kprobes_all_disarmed) |
| 2665 | buf[0] = '1'; |
| 2666 | else |
| 2667 | buf[0] = '0'; |
| 2668 | buf[1] = '\n'; |
| 2669 | buf[2] = 0x00; |
| 2670 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 2671 | } |
| 2672 | |
| 2673 | static ssize_t write_enabled_file_bool(struct file *file, |
| 2674 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 2675 | { |
| 2676 | char buf[32]; |
| 2677 | size_t buf_size; |
| 2678 | int ret = 0; |
| 2679 | |
| 2680 | buf_size = min(count, (sizeof(buf)-1)); |
| 2681 | if (copy_from_user(buf, user_buf, buf_size)) |
| 2682 | return -EFAULT; |
| 2683 | |
| 2684 | buf[buf_size] = '\0'; |
| 2685 | switch (buf[0]) { |
| 2686 | case 'y': |
| 2687 | case 'Y': |
| 2688 | case '1': |
| 2689 | ret = arm_all_kprobes(); |
| 2690 | break; |
| 2691 | case 'n': |
| 2692 | case 'N': |
| 2693 | case '0': |
| 2694 | ret = disarm_all_kprobes(); |
| 2695 | break; |
| 2696 | default: |
| 2697 | return -EINVAL; |
| 2698 | } |
| 2699 | |
| 2700 | if (ret) |
| 2701 | return ret; |
| 2702 | |
| 2703 | return count; |
| 2704 | } |
| 2705 | |
| 2706 | static const struct file_operations fops_kp = { |
| 2707 | .read = read_enabled_file_bool, |
| 2708 | .write = write_enabled_file_bool, |
| 2709 | .llseek = default_llseek, |
| 2710 | }; |
| 2711 | |
| 2712 | static int __init debugfs_kprobe_init(void) |
| 2713 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2714 | struct dentry *dir; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2715 | unsigned int value = 1; |
| 2716 | |
| 2717 | dir = debugfs_create_dir("kprobes", NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2718 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2719 | debugfs_create_file("list", 0400, dir, NULL, |
| 2720 | &debugfs_kprobes_operations); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2721 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2722 | debugfs_create_file("enabled", 0600, dir, &value, &fops_kp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2723 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2724 | debugfs_create_file("blacklist", 0400, dir, NULL, |
| 2725 | &debugfs_kprobe_blacklist_ops); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2726 | |
| 2727 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | late_initcall(debugfs_kprobe_init); |
| 2731 | #endif /* CONFIG_DEBUG_FS */ |