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