core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * arch/arm/kernel/kprobes.c
  3. *
  4. * Kprobes on ARM
  5. *
  6. * Abhishek Sagar <sagar.abhishek@gmail.com>
  7. * Copyright (C) 2006, 2007 Motorola Inc.
  8. *
  9. * Nicolas Pitre <nico@marvell.com>
  10. * Copyright (C) 2007 Marvell Ltd.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/kprobes.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/stop_machine.h>
  26. #include <linux/sched/debug.h>
  27. #include <linux/stringify.h>
  28. #include <asm/traps.h>
  29. #include <asm/opcodes.h>
  30. #include <asm/cacheflush.h>
  31. #include <linux/percpu.h>
  32. #include <linux/bug.h>
  33. #include <asm/patch.h>
  34. #include <asm/sections.h>
  35. #include "../decode-arm.h"
  36. #include "../decode-thumb.h"
  37. #include "core.h"
  38. #define MIN_STACK_SIZE(addr) \
  39. min((unsigned long)MAX_STACK_SIZE, \
  40. (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
  41. #define flush_insns(addr, size) \
  42. flush_icache_range((unsigned long)(addr), \
  43. (unsigned long)(addr) + \
  44. (size))
  45. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  46. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  47. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  48. {
  49. kprobe_opcode_t insn;
  50. kprobe_opcode_t tmp_insn[MAX_INSN_SIZE];
  51. unsigned long addr = (unsigned long)p->addr;
  52. bool thumb;
  53. kprobe_decode_insn_t *decode_insn;
  54. const union decode_action *actions;
  55. int is;
  56. const struct decode_checker **checkers;
  57. #ifdef CONFIG_THUMB2_KERNEL
  58. thumb = true;
  59. addr &= ~1; /* Bit 0 would normally be set to indicate Thumb code */
  60. insn = __mem_to_opcode_thumb16(((u16 *)addr)[0]);
  61. if (is_wide_instruction(insn)) {
  62. u16 inst2 = __mem_to_opcode_thumb16(((u16 *)addr)[1]);
  63. insn = __opcode_thumb32_compose(insn, inst2);
  64. decode_insn = thumb32_probes_decode_insn;
  65. actions = kprobes_t32_actions;
  66. checkers = kprobes_t32_checkers;
  67. } else {
  68. decode_insn = thumb16_probes_decode_insn;
  69. actions = kprobes_t16_actions;
  70. checkers = kprobes_t16_checkers;
  71. }
  72. #else /* !CONFIG_THUMB2_KERNEL */
  73. thumb = false;
  74. if (addr & 0x3)
  75. return -EINVAL;
  76. insn = __mem_to_opcode_arm(*p->addr);
  77. decode_insn = arm_probes_decode_insn;
  78. actions = kprobes_arm_actions;
  79. checkers = kprobes_arm_checkers;
  80. #endif
  81. p->opcode = insn;
  82. p->ainsn.insn = tmp_insn;
  83. switch ((*decode_insn)(insn, &p->ainsn, true, actions, checkers)) {
  84. case INSN_REJECTED: /* not supported */
  85. return -EINVAL;
  86. case INSN_GOOD: /* instruction uses slot */
  87. p->ainsn.insn = get_insn_slot();
  88. if (!p->ainsn.insn)
  89. return -ENOMEM;
  90. for (is = 0; is < MAX_INSN_SIZE; ++is)
  91. p->ainsn.insn[is] = tmp_insn[is];
  92. flush_insns(p->ainsn.insn,
  93. sizeof(p->ainsn.insn[0]) * MAX_INSN_SIZE);
  94. p->ainsn.insn_fn = (probes_insn_fn_t *)
  95. ((uintptr_t)p->ainsn.insn | thumb);
  96. break;
  97. case INSN_GOOD_NO_SLOT: /* instruction doesn't need insn slot */
  98. p->ainsn.insn = NULL;
  99. break;
  100. }
  101. /*
  102. * Never instrument insn like 'str r0, [sp, +/-r1]'. Also, insn likes
  103. * 'str r0, [sp, #-68]' should also be prohibited.
  104. * See __und_svc.
  105. */
  106. if ((p->ainsn.stack_space < 0) ||
  107. (p->ainsn.stack_space > MAX_STACK_SIZE))
  108. return -EINVAL;
  109. return 0;
  110. }
  111. void __kprobes arch_arm_kprobe(struct kprobe *p)
  112. {
  113. unsigned int brkp;
  114. void *addr;
  115. if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
  116. /* Remove any Thumb flag */
  117. addr = (void *)((uintptr_t)p->addr & ~1);
  118. if (is_wide_instruction(p->opcode))
  119. brkp = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION;
  120. else
  121. brkp = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION;
  122. } else {
  123. kprobe_opcode_t insn = p->opcode;
  124. addr = p->addr;
  125. brkp = KPROBE_ARM_BREAKPOINT_INSTRUCTION;
  126. if (insn >= 0xe0000000)
  127. brkp |= 0xe0000000; /* Unconditional instruction */
  128. else
  129. brkp |= insn & 0xf0000000; /* Copy condition from insn */
  130. }
  131. patch_text(addr, brkp);
  132. }
  133. /*
  134. * The actual disarming is done here on each CPU and synchronized using
  135. * stop_machine. This synchronization is necessary on SMP to avoid removing
  136. * a probe between the moment the 'Undefined Instruction' exception is raised
  137. * and the moment the exception handler reads the faulting instruction from
  138. * memory. It is also needed to atomically set the two half-words of a 32-bit
  139. * Thumb breakpoint.
  140. */
  141. struct patch {
  142. void *addr;
  143. unsigned int insn;
  144. };
  145. static int __kprobes_remove_breakpoint(void *data)
  146. {
  147. struct patch *p = data;
  148. __patch_text(p->addr, p->insn);
  149. return 0;
  150. }
  151. void __kprobes kprobes_remove_breakpoint(void *addr, unsigned int insn)
  152. {
  153. struct patch p = {
  154. .addr = addr,
  155. .insn = insn,
  156. };
  157. stop_machine_cpuslocked(__kprobes_remove_breakpoint, &p,
  158. cpu_online_mask);
  159. }
  160. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  161. {
  162. kprobes_remove_breakpoint((void *)((uintptr_t)p->addr & ~1),
  163. p->opcode);
  164. }
  165. void __kprobes arch_remove_kprobe(struct kprobe *p)
  166. {
  167. if (p->ainsn.insn) {
  168. free_insn_slot(p->ainsn.insn, 0);
  169. p->ainsn.insn = NULL;
  170. }
  171. }
  172. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  173. {
  174. kcb->prev_kprobe.kp = kprobe_running();
  175. kcb->prev_kprobe.status = kcb->kprobe_status;
  176. }
  177. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  178. {
  179. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  180. kcb->kprobe_status = kcb->prev_kprobe.status;
  181. }
  182. static void __kprobes set_current_kprobe(struct kprobe *p)
  183. {
  184. __this_cpu_write(current_kprobe, p);
  185. }
  186. static void __kprobes
  187. singlestep_skip(struct kprobe *p, struct pt_regs *regs)
  188. {
  189. #ifdef CONFIG_THUMB2_KERNEL
  190. regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
  191. if (is_wide_instruction(p->opcode))
  192. regs->ARM_pc += 4;
  193. else
  194. regs->ARM_pc += 2;
  195. #else
  196. regs->ARM_pc += 4;
  197. #endif
  198. }
  199. static inline void __kprobes
  200. singlestep(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  201. {
  202. p->ainsn.insn_singlestep(p->opcode, &p->ainsn, regs);
  203. }
  204. /*
  205. * Called with IRQs disabled. IRQs must remain disabled from that point
  206. * all the way until processing this kprobe is complete. The current
  207. * kprobes implementation cannot process more than one nested level of
  208. * kprobe, and that level is reserved for user kprobe handlers, so we can't
  209. * risk encountering a new kprobe in an interrupt handler.
  210. */
  211. void __kprobes kprobe_handler(struct pt_regs *regs)
  212. {
  213. struct kprobe *p, *cur;
  214. struct kprobe_ctlblk *kcb;
  215. kcb = get_kprobe_ctlblk();
  216. cur = kprobe_running();
  217. #ifdef CONFIG_THUMB2_KERNEL
  218. /*
  219. * First look for a probe which was registered using an address with
  220. * bit 0 set, this is the usual situation for pointers to Thumb code.
  221. * If not found, fallback to looking for one with bit 0 clear.
  222. */
  223. p = get_kprobe((kprobe_opcode_t *)(regs->ARM_pc | 1));
  224. if (!p)
  225. p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
  226. #else /* ! CONFIG_THUMB2_KERNEL */
  227. p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
  228. #endif
  229. if (p) {
  230. if (!p->ainsn.insn_check_cc(regs->ARM_cpsr)) {
  231. /*
  232. * Probe hit but conditional execution check failed,
  233. * so just skip the instruction and continue as if
  234. * nothing had happened.
  235. * In this case, we can skip recursing check too.
  236. */
  237. singlestep_skip(p, regs);
  238. } else if (cur) {
  239. /* Kprobe is pending, so we're recursing. */
  240. switch (kcb->kprobe_status) {
  241. case KPROBE_HIT_ACTIVE:
  242. case KPROBE_HIT_SSDONE:
  243. case KPROBE_HIT_SS:
  244. /* A pre- or post-handler probe got us here. */
  245. kprobes_inc_nmissed_count(p);
  246. save_previous_kprobe(kcb);
  247. set_current_kprobe(p);
  248. kcb->kprobe_status = KPROBE_REENTER;
  249. singlestep(p, regs, kcb);
  250. restore_previous_kprobe(kcb);
  251. break;
  252. case KPROBE_REENTER:
  253. /* A nested probe was hit in FIQ, it is a BUG */
  254. pr_warn("Unrecoverable kprobe detected.\n");
  255. dump_kprobe(p);
  256. /* fall through */
  257. default:
  258. /* impossible cases */
  259. BUG();
  260. }
  261. } else {
  262. /* Probe hit and conditional execution check ok. */
  263. set_current_kprobe(p);
  264. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  265. /*
  266. * If we have no pre-handler or it returned 0, we
  267. * continue with normal processing. If we have a
  268. * pre-handler and it returned non-zero, it will
  269. * modify the execution path and no need to single
  270. * stepping. Let's just reset current kprobe and exit.
  271. */
  272. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  273. kcb->kprobe_status = KPROBE_HIT_SS;
  274. singlestep(p, regs, kcb);
  275. if (p->post_handler) {
  276. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  277. p->post_handler(p, regs, 0);
  278. }
  279. }
  280. reset_current_kprobe();
  281. }
  282. } else {
  283. /*
  284. * The probe was removed and a race is in progress.
  285. * There is nothing we can do about it. Let's restart
  286. * the instruction. By the time we can restart, the
  287. * real instruction will be there.
  288. */
  289. }
  290. }
  291. static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
  292. {
  293. unsigned long flags;
  294. local_irq_save(flags);
  295. kprobe_handler(regs);
  296. local_irq_restore(flags);
  297. return 0;
  298. }
  299. int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
  300. {
  301. struct kprobe *cur = kprobe_running();
  302. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  303. switch (kcb->kprobe_status) {
  304. case KPROBE_HIT_SS:
  305. case KPROBE_REENTER:
  306. /*
  307. * We are here because the instruction being single
  308. * stepped caused a page fault. We reset the current
  309. * kprobe and the PC to point back to the probe address
  310. * and allow the page fault handler to continue as a
  311. * normal page fault.
  312. */
  313. regs->ARM_pc = (long)cur->addr;
  314. if (kcb->kprobe_status == KPROBE_REENTER) {
  315. restore_previous_kprobe(kcb);
  316. } else {
  317. reset_current_kprobe();
  318. }
  319. break;
  320. case KPROBE_HIT_ACTIVE:
  321. case KPROBE_HIT_SSDONE:
  322. /*
  323. * We increment the nmissed count for accounting,
  324. * we can also use npre/npostfault count for accounting
  325. * these specific fault cases.
  326. */
  327. kprobes_inc_nmissed_count(cur);
  328. /*
  329. * We come here because instructions in the pre/post
  330. * handler caused the page_fault, this could happen
  331. * if handler tries to access user space by
  332. * copy_from_user(), get_user() etc. Let the
  333. * user-specified handler try to fix it.
  334. */
  335. if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
  336. return 1;
  337. break;
  338. default:
  339. break;
  340. }
  341. return 0;
  342. }
  343. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  344. unsigned long val, void *data)
  345. {
  346. /*
  347. * notify_die() is currently never called on ARM,
  348. * so this callback is currently empty.
  349. */
  350. return NOTIFY_DONE;
  351. }
  352. /*
  353. * When a retprobed function returns, trampoline_handler() is called,
  354. * calling the kretprobe's handler. We construct a struct pt_regs to
  355. * give a view of registers r0-r11 to the user return-handler. This is
  356. * not a complete pt_regs structure, but that should be plenty sufficient
  357. * for kretprobe handlers which should normally be interested in r0 only
  358. * anyway.
  359. */
  360. void __naked __kprobes kretprobe_trampoline(void)
  361. {
  362. __asm__ __volatile__ (
  363. "stmdb sp!, {r0 - r11} \n\t"
  364. "mov r0, sp \n\t"
  365. "bl trampoline_handler \n\t"
  366. "mov lr, r0 \n\t"
  367. "ldmia sp!, {r0 - r11} \n\t"
  368. #ifdef CONFIG_THUMB2_KERNEL
  369. "bx lr \n\t"
  370. #else
  371. "mov pc, lr \n\t"
  372. #endif
  373. : : : "memory");
  374. }
  375. /* Called from kretprobe_trampoline */
  376. static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
  377. {
  378. struct kretprobe_instance *ri = NULL;
  379. struct hlist_head *head, empty_rp;
  380. struct hlist_node *tmp;
  381. unsigned long flags, orig_ret_address = 0;
  382. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  383. kprobe_opcode_t *correct_ret_addr = NULL;
  384. INIT_HLIST_HEAD(&empty_rp);
  385. kretprobe_hash_lock(current, &head, &flags);
  386. /*
  387. * It is possible to have multiple instances associated with a given
  388. * task either because multiple functions in the call path have
  389. * a return probe installed on them, and/or more than one return
  390. * probe was registered for a target function.
  391. *
  392. * We can handle this because:
  393. * - instances are always inserted at the head of the list
  394. * - when multiple return probes are registered for the same
  395. * function, the first instance's ret_addr will point to the
  396. * real return address, and all the rest will point to
  397. * kretprobe_trampoline
  398. */
  399. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  400. if (ri->task != current)
  401. /* another task is sharing our hash bucket */
  402. continue;
  403. orig_ret_address = (unsigned long)ri->ret_addr;
  404. if (orig_ret_address != trampoline_address)
  405. /*
  406. * This is the real return address. Any other
  407. * instances associated with this task are for
  408. * other calls deeper on the call stack
  409. */
  410. break;
  411. }
  412. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  413. correct_ret_addr = ri->ret_addr;
  414. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  415. if (ri->task != current)
  416. /* another task is sharing our hash bucket */
  417. continue;
  418. orig_ret_address = (unsigned long)ri->ret_addr;
  419. if (ri->rp && ri->rp->handler) {
  420. __this_cpu_write(current_kprobe, &ri->rp->kp);
  421. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  422. ri->ret_addr = correct_ret_addr;
  423. ri->rp->handler(ri, regs);
  424. __this_cpu_write(current_kprobe, NULL);
  425. }
  426. recycle_rp_inst(ri, &empty_rp);
  427. if (orig_ret_address != trampoline_address)
  428. /*
  429. * This is the real return address. Any other
  430. * instances associated with this task are for
  431. * other calls deeper on the call stack
  432. */
  433. break;
  434. }
  435. kretprobe_hash_unlock(current, &flags);
  436. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  437. hlist_del(&ri->hlist);
  438. kfree(ri);
  439. }
  440. return (void *)orig_ret_address;
  441. }
  442. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  443. struct pt_regs *regs)
  444. {
  445. ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
  446. /* Replace the return addr with trampoline addr. */
  447. regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
  448. }
  449. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  450. {
  451. return 0;
  452. }
  453. #ifdef CONFIG_THUMB2_KERNEL
  454. static struct undef_hook kprobes_thumb16_break_hook = {
  455. .instr_mask = 0xffff,
  456. .instr_val = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION,
  457. .cpsr_mask = MODE_MASK,
  458. .cpsr_val = SVC_MODE,
  459. .fn = kprobe_trap_handler,
  460. };
  461. static struct undef_hook kprobes_thumb32_break_hook = {
  462. .instr_mask = 0xffffffff,
  463. .instr_val = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION,
  464. .cpsr_mask = MODE_MASK,
  465. .cpsr_val = SVC_MODE,
  466. .fn = kprobe_trap_handler,
  467. };
  468. #else /* !CONFIG_THUMB2_KERNEL */
  469. static struct undef_hook kprobes_arm_break_hook = {
  470. .instr_mask = 0x0fffffff,
  471. .instr_val = KPROBE_ARM_BREAKPOINT_INSTRUCTION,
  472. .cpsr_mask = MODE_MASK,
  473. .cpsr_val = SVC_MODE,
  474. .fn = kprobe_trap_handler,
  475. };
  476. #endif /* !CONFIG_THUMB2_KERNEL */
  477. int __init arch_init_kprobes()
  478. {
  479. arm_probes_decode_init();
  480. #ifdef CONFIG_THUMB2_KERNEL
  481. register_undef_hook(&kprobes_thumb16_break_hook);
  482. register_undef_hook(&kprobes_thumb32_break_hook);
  483. #else
  484. register_undef_hook(&kprobes_arm_break_hook);
  485. #endif
  486. return 0;
  487. }
  488. bool arch_within_kprobe_blacklist(unsigned long addr)
  489. {
  490. void *a = (void *)addr;
  491. return __in_irqentry_text(addr) ||
  492. in_entry_text(addr) ||
  493. in_idmap_text(addr) ||
  494. memory_contains(__kprobes_text_start, __kprobes_text_end, a, 1);
  495. }