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