1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/kernel.h>
23#include <linux/kprobes.h>
24#include <linux/module.h>
25#include <linux/slab.h>
26#include <linux/stop_machine.h>
27#include <linux/sched/debug.h>
28#include <linux/stringify.h>
29#include <asm/traps.h>
30#include <asm/opcodes.h>
31#include <asm/cacheflush.h>
32#include <linux/percpu.h>
33#include <linux/bug.h>
34#include <asm/patch.h>
35
36#include "../decode-arm.h"
37#include "../decode-thumb.h"
38#include "core.h"
39
40#define MIN_STACK_SIZE(addr) \
41 min((unsigned long)MAX_STACK_SIZE, \
42 (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
43
44#define flush_insns(addr, size) \
45 flush_icache_range((unsigned long)(addr), \
46 (unsigned long)(addr) + \
47 (size))
48
49
50#define JPROBE_MAGIC_ADDR 0xffffffff
51
52DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
53DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
54
55
56int __kprobes arch_prepare_kprobe(struct kprobe *p)
57{
58 kprobe_opcode_t insn;
59 kprobe_opcode_t tmp_insn[MAX_INSN_SIZE];
60 unsigned long addr = (unsigned long)p->addr;
61 bool thumb;
62 kprobe_decode_insn_t *decode_insn;
63 const union decode_action *actions;
64 int is;
65 const struct decode_checker **checkers;
66
67 if (in_exception_text(addr))
68 return -EINVAL;
69
70#ifdef CONFIG_THUMB2_KERNEL
71 thumb = true;
72 addr &= ~1;
73 insn = __mem_to_opcode_thumb16(((u16 *)addr)[0]);
74 if (is_wide_instruction(insn)) {
75 u16 inst2 = __mem_to_opcode_thumb16(((u16 *)addr)[1]);
76 insn = __opcode_thumb32_compose(insn, inst2);
77 decode_insn = thumb32_probes_decode_insn;
78 actions = kprobes_t32_actions;
79 checkers = kprobes_t32_checkers;
80 } else {
81 decode_insn = thumb16_probes_decode_insn;
82 actions = kprobes_t16_actions;
83 checkers = kprobes_t16_checkers;
84 }
85#else
86 thumb = false;
87 if (addr & 0x3)
88 return -EINVAL;
89 insn = __mem_to_opcode_arm(*p->addr);
90 decode_insn = arm_probes_decode_insn;
91 actions = kprobes_arm_actions;
92 checkers = kprobes_arm_checkers;
93#endif
94
95 p->opcode = insn;
96 p->ainsn.insn = tmp_insn;
97
98 switch ((*decode_insn)(insn, &p->ainsn, true, actions, checkers)) {
99 case INSN_REJECTED:
100 return -EINVAL;
101
102 case INSN_GOOD:
103 p->ainsn.insn = get_insn_slot();
104 if (!p->ainsn.insn)
105 return -ENOMEM;
106 for (is = 0; is < MAX_INSN_SIZE; ++is)
107 p->ainsn.insn[is] = tmp_insn[is];
108 flush_insns(p->ainsn.insn,
109 sizeof(p->ainsn.insn[0]) * MAX_INSN_SIZE);
110 p->ainsn.insn_fn = (probes_insn_fn_t *)
111 ((uintptr_t)p->ainsn.insn | thumb);
112 break;
113
114 case INSN_GOOD_NO_SLOT:
115 p->ainsn.insn = NULL;
116 break;
117 }
118
119
120
121
122
123
124 if ((p->ainsn.stack_space < 0) ||
125 (p->ainsn.stack_space > MAX_STACK_SIZE))
126 return -EINVAL;
127
128 return 0;
129}
130
131void __kprobes arch_arm_kprobe(struct kprobe *p)
132{
133 unsigned int brkp;
134 void *addr;
135
136 if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
137
138 addr = (void *)((uintptr_t)p->addr & ~1);
139
140 if (is_wide_instruction(p->opcode))
141 brkp = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION;
142 else
143 brkp = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION;
144 } else {
145 kprobe_opcode_t insn = p->opcode;
146
147 addr = p->addr;
148 brkp = KPROBE_ARM_BREAKPOINT_INSTRUCTION;
149
150 if (insn >= 0xe0000000)
151 brkp |= 0xe0000000;
152 else
153 brkp |= insn & 0xf0000000;
154 }
155
156 patch_text(addr, brkp);
157}
158
159
160
161
162
163
164
165
166
167struct patch {
168 void *addr;
169 unsigned int insn;
170};
171
172static int __kprobes_remove_breakpoint(void *data)
173{
174 struct patch *p = data;
175 __patch_text(p->addr, p->insn);
176 return 0;
177}
178
179void __kprobes kprobes_remove_breakpoint(void *addr, unsigned int insn)
180{
181 struct patch p = {
182 .addr = addr,
183 .insn = insn,
184 };
185 stop_machine(__kprobes_remove_breakpoint, &p, cpu_online_mask);
186}
187
188void __kprobes arch_disarm_kprobe(struct kprobe *p)
189{
190 kprobes_remove_breakpoint((void *)((uintptr_t)p->addr & ~1),
191 p->opcode);
192}
193
194void __kprobes arch_remove_kprobe(struct kprobe *p)
195{
196 if (p->ainsn.insn) {
197 free_insn_slot(p->ainsn.insn, 0);
198 p->ainsn.insn = NULL;
199 }
200}
201
202static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
203{
204 kcb->prev_kprobe.kp = kprobe_running();
205 kcb->prev_kprobe.status = kcb->kprobe_status;
206}
207
208static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
209{
210 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
211 kcb->kprobe_status = kcb->prev_kprobe.status;
212}
213
214static void __kprobes set_current_kprobe(struct kprobe *p)
215{
216 __this_cpu_write(current_kprobe, p);
217}
218
219static void __kprobes
220singlestep_skip(struct kprobe *p, struct pt_regs *regs)
221{
222#ifdef CONFIG_THUMB2_KERNEL
223 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
224 if (is_wide_instruction(p->opcode))
225 regs->ARM_pc += 4;
226 else
227 regs->ARM_pc += 2;
228#else
229 regs->ARM_pc += 4;
230#endif
231}
232
233static inline void __kprobes
234singlestep(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
235{
236 p->ainsn.insn_singlestep(p->opcode, &p->ainsn, regs);
237}
238
239
240
241
242
243
244
245
246void __kprobes kprobe_handler(struct pt_regs *regs)
247{
248 struct kprobe *p, *cur;
249 struct kprobe_ctlblk *kcb;
250
251 kcb = get_kprobe_ctlblk();
252 cur = kprobe_running();
253
254#ifdef CONFIG_THUMB2_KERNEL
255
256
257
258
259
260 p = get_kprobe((kprobe_opcode_t *)(regs->ARM_pc | 1));
261 if (!p)
262 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
263
264#else
265 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
266#endif
267
268 if (p) {
269 if (!p->ainsn.insn_check_cc(regs->ARM_cpsr)) {
270
271
272
273
274
275
276 singlestep_skip(p, regs);
277 } else if (cur) {
278
279 switch (kcb->kprobe_status) {
280 case KPROBE_HIT_ACTIVE:
281 case KPROBE_HIT_SSDONE:
282 case KPROBE_HIT_SS:
283
284 kprobes_inc_nmissed_count(p);
285 save_previous_kprobe(kcb);
286 set_current_kprobe(p);
287 kcb->kprobe_status = KPROBE_REENTER;
288 singlestep(p, regs, kcb);
289 restore_previous_kprobe(kcb);
290 break;
291 case KPROBE_REENTER:
292
293 pr_warn("Unrecoverable kprobe detected at %p.\n",
294 p->addr);
295
296 default:
297
298 BUG();
299 }
300 } else {
301
302 set_current_kprobe(p);
303 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
304
305
306
307
308
309
310
311
312 if (!p->pre_handler || !p->pre_handler(p, regs)) {
313 kcb->kprobe_status = KPROBE_HIT_SS;
314 singlestep(p, regs, kcb);
315 if (p->post_handler) {
316 kcb->kprobe_status = KPROBE_HIT_SSDONE;
317 p->post_handler(p, regs, 0);
318 }
319 reset_current_kprobe();
320 }
321 }
322 } else if (cur) {
323
324 if (cur->break_handler && cur->break_handler(cur, regs)) {
325 kcb->kprobe_status = KPROBE_HIT_SS;
326 singlestep(cur, regs, kcb);
327 if (cur->post_handler) {
328 kcb->kprobe_status = KPROBE_HIT_SSDONE;
329 cur->post_handler(cur, regs, 0);
330 }
331 }
332 reset_current_kprobe();
333 } else {
334
335
336
337
338
339
340 }
341}
342
343static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
344{
345 unsigned long flags;
346 local_irq_save(flags);
347 kprobe_handler(regs);
348 local_irq_restore(flags);
349 return 0;
350}
351
352int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
353{
354 struct kprobe *cur = kprobe_running();
355 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
356
357 switch (kcb->kprobe_status) {
358 case KPROBE_HIT_SS:
359 case KPROBE_REENTER:
360
361
362
363
364
365
366
367 regs->ARM_pc = (long)cur->addr;
368 if (kcb->kprobe_status == KPROBE_REENTER) {
369 restore_previous_kprobe(kcb);
370 } else {
371 reset_current_kprobe();
372 }
373 break;
374
375 case KPROBE_HIT_ACTIVE:
376 case KPROBE_HIT_SSDONE:
377
378
379
380
381
382 kprobes_inc_nmissed_count(cur);
383
384
385
386
387
388
389
390
391 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
392 return 1;
393 break;
394
395 default:
396 break;
397 }
398
399 return 0;
400}
401
402int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
403 unsigned long val, void *data)
404{
405
406
407
408
409 return NOTIFY_DONE;
410}
411
412
413
414
415
416
417
418
419
420void __naked __kprobes kretprobe_trampoline(void)
421{
422 __asm__ __volatile__ (
423 "stmdb sp!, {r0 - r11} \n\t"
424 "mov r0, sp \n\t"
425 "bl trampoline_handler \n\t"
426 "mov lr, r0 \n\t"
427 "ldmia sp!, {r0 - r11} \n\t"
428#ifdef CONFIG_THUMB2_KERNEL
429 "bx lr \n\t"
430#else
431 "mov pc, lr \n\t"
432#endif
433 : : : "memory");
434}
435
436
437static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
438{
439 struct kretprobe_instance *ri = NULL;
440 struct hlist_head *head, empty_rp;
441 struct hlist_node *tmp;
442 unsigned long flags, orig_ret_address = 0;
443 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
444 kprobe_opcode_t *correct_ret_addr = NULL;
445
446 INIT_HLIST_HEAD(&empty_rp);
447 kretprobe_hash_lock(current, &head, &flags);
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
463 if (ri->task != current)
464
465 continue;
466
467 orig_ret_address = (unsigned long)ri->ret_addr;
468
469 if (orig_ret_address != trampoline_address)
470
471
472
473
474
475 break;
476 }
477
478 kretprobe_assert(ri, orig_ret_address, trampoline_address);
479
480 correct_ret_addr = ri->ret_addr;
481 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
482 if (ri->task != current)
483
484 continue;
485
486 orig_ret_address = (unsigned long)ri->ret_addr;
487 if (ri->rp && ri->rp->handler) {
488 __this_cpu_write(current_kprobe, &ri->rp->kp);
489 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
490 ri->ret_addr = correct_ret_addr;
491 ri->rp->handler(ri, regs);
492 __this_cpu_write(current_kprobe, NULL);
493 }
494
495 recycle_rp_inst(ri, &empty_rp);
496
497 if (orig_ret_address != trampoline_address)
498
499
500
501
502
503 break;
504 }
505
506 kretprobe_hash_unlock(current, &flags);
507
508 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
509 hlist_del(&ri->hlist);
510 kfree(ri);
511 }
512
513 return (void *)orig_ret_address;
514}
515
516void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
517 struct pt_regs *regs)
518{
519 ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
520
521
522 regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
523}
524
525int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
526{
527 struct jprobe *jp = container_of(p, struct jprobe, kp);
528 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
529 long sp_addr = regs->ARM_sp;
530 long cpsr;
531
532 kcb->jprobe_saved_regs = *regs;
533 memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
534 regs->ARM_pc = (long)jp->entry;
535
536 cpsr = regs->ARM_cpsr | PSR_I_BIT;
537#ifdef CONFIG_THUMB2_KERNEL
538
539 if (regs->ARM_pc & 1)
540 cpsr |= PSR_T_BIT;
541 else
542 cpsr &= ~PSR_T_BIT;
543#endif
544 regs->ARM_cpsr = cpsr;
545
546 preempt_disable();
547 return 1;
548}
549
550void __kprobes jprobe_return(void)
551{
552 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
553
554 __asm__ __volatile__ (
555
556
557
558
559
560
561
562
563
564
565
566
567#ifdef CONFIG_THUMB2_KERNEL
568 "sub r0, %0, %1 \n\t"
569 "mov sp, r0 \n\t"
570#else
571 "sub sp, %0, %1 \n\t"
572#endif
573 "ldr r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
574 "str %0, [sp, %2] \n\t"
575 "str r0, [sp, %3] \n\t"
576 "mov r0, sp \n\t"
577 "bl kprobe_handler \n\t"
578
579
580
581
582
583#ifdef CONFIG_THUMB2_KERNEL
584 "ldr lr, [sp, %2] \n\t"
585 "ldrd r0, r1, [sp, %5] \n\t"
586 "ldr r2, [sp, %4] \n\t"
587 "stmdb lr!, {r0, r1, r2} \n\t"
588
589 "ldmia sp, {r0 - r12} \n\t"
590 "mov sp, lr \n\t"
591 "ldr lr, [sp], #4 \n\t"
592 "rfeia sp! \n\t"
593#else
594 "ldr r0, [sp, %4] \n\t"
595 "msr cpsr_cxsf, r0 \n\t"
596 "ldmia sp, {r0 - pc} \n\t"
597#endif
598 :
599 : "r" (kcb->jprobe_saved_regs.ARM_sp),
600 "I" (sizeof(struct pt_regs) * 2),
601 "J" (offsetof(struct pt_regs, ARM_sp)),
602 "J" (offsetof(struct pt_regs, ARM_pc)),
603 "J" (offsetof(struct pt_regs, ARM_cpsr)),
604 "J" (offsetof(struct pt_regs, ARM_lr))
605 : "memory", "cc");
606}
607
608int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
609{
610 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
611 long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
612 long orig_sp = regs->ARM_sp;
613 struct jprobe *jp = container_of(p, struct jprobe, kp);
614
615 if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
616 if (orig_sp != stack_addr) {
617 struct pt_regs *saved_regs =
618 (struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
619 printk("current sp %lx does not match saved sp %lx\n",
620 orig_sp, stack_addr);
621 printk("Saved registers for jprobe %p\n", jp);
622 show_regs(saved_regs);
623 printk("Current registers\n");
624 show_regs(regs);
625 BUG();
626 }
627 *regs = kcb->jprobe_saved_regs;
628 memcpy((void *)stack_addr, kcb->jprobes_stack,
629 MIN_STACK_SIZE(stack_addr));
630 preempt_enable_no_resched();
631 return 1;
632 }
633 return 0;
634}
635
636int __kprobes arch_trampoline_kprobe(struct kprobe *p)
637{
638 return 0;
639}
640
641#ifdef CONFIG_THUMB2_KERNEL
642
643static struct undef_hook kprobes_thumb16_break_hook = {
644 .instr_mask = 0xffff,
645 .instr_val = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION,
646 .cpsr_mask = MODE_MASK,
647 .cpsr_val = SVC_MODE,
648 .fn = kprobe_trap_handler,
649};
650
651static struct undef_hook kprobes_thumb32_break_hook = {
652 .instr_mask = 0xffffffff,
653 .instr_val = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION,
654 .cpsr_mask = MODE_MASK,
655 .cpsr_val = SVC_MODE,
656 .fn = kprobe_trap_handler,
657};
658
659#else
660
661static struct undef_hook kprobes_arm_break_hook = {
662 .instr_mask = 0x0fffffff,
663 .instr_val = KPROBE_ARM_BREAKPOINT_INSTRUCTION,
664 .cpsr_mask = MODE_MASK,
665 .cpsr_val = SVC_MODE,
666 .fn = kprobe_trap_handler,
667};
668
669#endif
670
671int __init arch_init_kprobes()
672{
673 arm_probes_decode_init();
674#ifdef CONFIG_THUMB2_KERNEL
675 register_undef_hook(&kprobes_thumb16_break_hook);
676 register_undef_hook(&kprobes_thumb32_break_hook);
677#else
678 register_undef_hook(&kprobes_arm_break_hook);
679#endif
680 return 0;
681}
682