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_cpuslocked(__kprobes_remove_breakpoint, &p,
186 cpu_online_mask);
187}
188
189void __kprobes arch_disarm_kprobe(struct kprobe *p)
190{
191 kprobes_remove_breakpoint((void *)((uintptr_t)p->addr & ~1),
192 p->opcode);
193}
194
195void __kprobes arch_remove_kprobe(struct kprobe *p)
196{
197 if (p->ainsn.insn) {
198 free_insn_slot(p->ainsn.insn, 0);
199 p->ainsn.insn = NULL;
200 }
201}
202
203static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
204{
205 kcb->prev_kprobe.kp = kprobe_running();
206 kcb->prev_kprobe.status = kcb->kprobe_status;
207}
208
209static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
210{
211 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
212 kcb->kprobe_status = kcb->prev_kprobe.status;
213}
214
215static void __kprobes set_current_kprobe(struct kprobe *p)
216{
217 __this_cpu_write(current_kprobe, p);
218}
219
220static void __kprobes
221singlestep_skip(struct kprobe *p, struct pt_regs *regs)
222{
223#ifdef CONFIG_THUMB2_KERNEL
224 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
225 if (is_wide_instruction(p->opcode))
226 regs->ARM_pc += 4;
227 else
228 regs->ARM_pc += 2;
229#else
230 regs->ARM_pc += 4;
231#endif
232}
233
234static inline void __kprobes
235singlestep(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
236{
237 p->ainsn.insn_singlestep(p->opcode, &p->ainsn, regs);
238}
239
240
241
242
243
244
245
246
247void __kprobes kprobe_handler(struct pt_regs *regs)
248{
249 struct kprobe *p, *cur;
250 struct kprobe_ctlblk *kcb;
251
252 kcb = get_kprobe_ctlblk();
253 cur = kprobe_running();
254
255#ifdef CONFIG_THUMB2_KERNEL
256
257
258
259
260
261 p = get_kprobe((kprobe_opcode_t *)(regs->ARM_pc | 1));
262 if (!p)
263 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
264
265#else
266 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
267#endif
268
269 if (p) {
270 if (!p->ainsn.insn_check_cc(regs->ARM_cpsr)) {
271
272
273
274
275
276
277 singlestep_skip(p, regs);
278 } else if (cur) {
279
280 switch (kcb->kprobe_status) {
281 case KPROBE_HIT_ACTIVE:
282 case KPROBE_HIT_SSDONE:
283 case KPROBE_HIT_SS:
284
285 kprobes_inc_nmissed_count(p);
286 save_previous_kprobe(kcb);
287 set_current_kprobe(p);
288 kcb->kprobe_status = KPROBE_REENTER;
289 singlestep(p, regs, kcb);
290 restore_previous_kprobe(kcb);
291 break;
292 case KPROBE_REENTER:
293
294 pr_warn("Unrecoverable kprobe detected at %p.\n",
295 p->addr);
296
297 default:
298
299 BUG();
300 }
301 } else {
302
303 set_current_kprobe(p);
304 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
305
306
307
308
309
310
311
312
313 if (!p->pre_handler || !p->pre_handler(p, regs)) {
314 kcb->kprobe_status = KPROBE_HIT_SS;
315 singlestep(p, regs, kcb);
316 if (p->post_handler) {
317 kcb->kprobe_status = KPROBE_HIT_SSDONE;
318 p->post_handler(p, regs, 0);
319 }
320 reset_current_kprobe();
321 }
322 }
323 } else if (cur) {
324
325 if (cur->break_handler && cur->break_handler(cur, regs)) {
326 kcb->kprobe_status = KPROBE_HIT_SS;
327 singlestep(cur, regs, kcb);
328 if (cur->post_handler) {
329 kcb->kprobe_status = KPROBE_HIT_SSDONE;
330 cur->post_handler(cur, regs, 0);
331 }
332 }
333 reset_current_kprobe();
334 } else {
335
336
337
338
339
340
341 }
342}
343
344static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
345{
346 unsigned long flags;
347 local_irq_save(flags);
348 kprobe_handler(regs);
349 local_irq_restore(flags);
350 return 0;
351}
352
353int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
354{
355 struct kprobe *cur = kprobe_running();
356 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
357
358 switch (kcb->kprobe_status) {
359 case KPROBE_HIT_SS:
360 case KPROBE_REENTER:
361
362
363
364
365
366
367
368 regs->ARM_pc = (long)cur->addr;
369 if (kcb->kprobe_status == KPROBE_REENTER) {
370 restore_previous_kprobe(kcb);
371 } else {
372 reset_current_kprobe();
373 }
374 break;
375
376 case KPROBE_HIT_ACTIVE:
377 case KPROBE_HIT_SSDONE:
378
379
380
381
382
383 kprobes_inc_nmissed_count(cur);
384
385
386
387
388
389
390
391
392 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
393 return 1;
394 break;
395
396 default:
397 break;
398 }
399
400 return 0;
401}
402
403int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
404 unsigned long val, void *data)
405{
406
407
408
409
410 return NOTIFY_DONE;
411}
412
413
414
415
416
417
418
419
420
421void __naked __kprobes kretprobe_trampoline(void)
422{
423 __asm__ __volatile__ (
424 "stmdb sp!, {r0 - r11} \n\t"
425 "mov r0, sp \n\t"
426 "bl trampoline_handler \n\t"
427 "mov lr, r0 \n\t"
428 "ldmia sp!, {r0 - r11} \n\t"
429#ifdef CONFIG_THUMB2_KERNEL
430 "bx lr \n\t"
431#else
432 "mov pc, lr \n\t"
433#endif
434 : : : "memory");
435}
436
437
438static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
439{
440 struct kretprobe_instance *ri = NULL;
441 struct hlist_head *head, empty_rp;
442 struct hlist_node *tmp;
443 unsigned long flags, orig_ret_address = 0;
444 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
445 kprobe_opcode_t *correct_ret_addr = NULL;
446
447 INIT_HLIST_HEAD(&empty_rp);
448 kretprobe_hash_lock(current, &head, &flags);
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
464 if (ri->task != current)
465
466 continue;
467
468 orig_ret_address = (unsigned long)ri->ret_addr;
469
470 if (orig_ret_address != trampoline_address)
471
472
473
474
475
476 break;
477 }
478
479 kretprobe_assert(ri, orig_ret_address, trampoline_address);
480
481 correct_ret_addr = ri->ret_addr;
482 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
483 if (ri->task != current)
484
485 continue;
486
487 orig_ret_address = (unsigned long)ri->ret_addr;
488 if (ri->rp && ri->rp->handler) {
489 __this_cpu_write(current_kprobe, &ri->rp->kp);
490 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
491 ri->ret_addr = correct_ret_addr;
492 ri->rp->handler(ri, regs);
493 __this_cpu_write(current_kprobe, NULL);
494 }
495
496 recycle_rp_inst(ri, &empty_rp);
497
498 if (orig_ret_address != trampoline_address)
499
500
501
502
503
504 break;
505 }
506
507 kretprobe_hash_unlock(current, &flags);
508
509 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
510 hlist_del(&ri->hlist);
511 kfree(ri);
512 }
513
514 return (void *)orig_ret_address;
515}
516
517void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
518 struct pt_regs *regs)
519{
520 ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
521
522
523 regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
524}
525
526int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
527{
528 struct jprobe *jp = container_of(p, struct jprobe, kp);
529 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
530 long sp_addr = regs->ARM_sp;
531 long cpsr;
532
533 kcb->jprobe_saved_regs = *regs;
534 memcpy(kcb->jprobes_stack, (void *)sp_addr, MIN_STACK_SIZE(sp_addr));
535 regs->ARM_pc = (long)jp->entry;
536
537 cpsr = regs->ARM_cpsr | PSR_I_BIT;
538#ifdef CONFIG_THUMB2_KERNEL
539
540 if (regs->ARM_pc & 1)
541 cpsr |= PSR_T_BIT;
542 else
543 cpsr &= ~PSR_T_BIT;
544#endif
545 regs->ARM_cpsr = cpsr;
546
547 preempt_disable();
548 return 1;
549}
550
551void __kprobes jprobe_return(void)
552{
553 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
554
555 __asm__ __volatile__ (
556
557
558
559
560
561
562
563
564
565
566
567
568#ifdef CONFIG_THUMB2_KERNEL
569 "sub r0, %0, %1 \n\t"
570 "mov sp, r0 \n\t"
571#else
572 "sub sp, %0, %1 \n\t"
573#endif
574 "ldr r0, ="__stringify(JPROBE_MAGIC_ADDR)"\n\t"
575 "str %0, [sp, %2] \n\t"
576 "str r0, [sp, %3] \n\t"
577 "mov r0, sp \n\t"
578 "bl kprobe_handler \n\t"
579
580
581
582
583
584#ifdef CONFIG_THUMB2_KERNEL
585 "ldr lr, [sp, %2] \n\t"
586 "ldrd r0, r1, [sp, %5] \n\t"
587 "ldr r2, [sp, %4] \n\t"
588 "stmdb lr!, {r0, r1, r2} \n\t"
589
590 "ldmia sp, {r0 - r12} \n\t"
591 "mov sp, lr \n\t"
592 "ldr lr, [sp], #4 \n\t"
593 "rfeia sp! \n\t"
594#else
595 "ldr r0, [sp, %4] \n\t"
596 "msr cpsr_cxsf, r0 \n\t"
597 "ldmia sp, {r0 - pc} \n\t"
598#endif
599 :
600 : "r" (kcb->jprobe_saved_regs.ARM_sp),
601 "I" (sizeof(struct pt_regs) * 2),
602 "J" (offsetof(struct pt_regs, ARM_sp)),
603 "J" (offsetof(struct pt_regs, ARM_pc)),
604 "J" (offsetof(struct pt_regs, ARM_cpsr)),
605 "J" (offsetof(struct pt_regs, ARM_lr))
606 : "memory", "cc");
607}
608
609int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
610{
611 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
612 long stack_addr = kcb->jprobe_saved_regs.ARM_sp;
613 long orig_sp = regs->ARM_sp;
614 struct jprobe *jp = container_of(p, struct jprobe, kp);
615
616 if (regs->ARM_pc == JPROBE_MAGIC_ADDR) {
617 if (orig_sp != stack_addr) {
618 struct pt_regs *saved_regs =
619 (struct pt_regs *)kcb->jprobe_saved_regs.ARM_sp;
620 printk("current sp %lx does not match saved sp %lx\n",
621 orig_sp, stack_addr);
622 printk("Saved registers for jprobe %p\n", jp);
623 show_regs(saved_regs);
624 printk("Current registers\n");
625 show_regs(regs);
626 BUG();
627 }
628 *regs = kcb->jprobe_saved_regs;
629 memcpy((void *)stack_addr, kcb->jprobes_stack,
630 MIN_STACK_SIZE(stack_addr));
631 preempt_enable_no_resched();
632 return 1;
633 }
634 return 0;
635}
636
637int __kprobes arch_trampoline_kprobe(struct kprobe *p)
638{
639 return 0;
640}
641
642#ifdef CONFIG_THUMB2_KERNEL
643
644static struct undef_hook kprobes_thumb16_break_hook = {
645 .instr_mask = 0xffff,
646 .instr_val = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION,
647 .cpsr_mask = MODE_MASK,
648 .cpsr_val = SVC_MODE,
649 .fn = kprobe_trap_handler,
650};
651
652static struct undef_hook kprobes_thumb32_break_hook = {
653 .instr_mask = 0xffffffff,
654 .instr_val = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION,
655 .cpsr_mask = MODE_MASK,
656 .cpsr_val = SVC_MODE,
657 .fn = kprobe_trap_handler,
658};
659
660#else
661
662static struct undef_hook kprobes_arm_break_hook = {
663 .instr_mask = 0x0fffffff,
664 .instr_val = KPROBE_ARM_BREAKPOINT_INSTRUCTION,
665 .cpsr_mask = MODE_MASK,
666 .cpsr_val = SVC_MODE,
667 .fn = kprobe_trap_handler,
668};
669
670#endif
671
672int __init arch_init_kprobes()
673{
674 arm_probes_decode_init();
675#ifdef CONFIG_THUMB2_KERNEL
676 register_undef_hook(&kprobes_thumb16_break_hook);
677 register_undef_hook(&kprobes_thumb32_break_hook);
678#else
679 register_undef_hook(&kprobes_arm_break_hook);
680#endif
681 return 0;
682}
683