kprobes.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arch/arm64/kernel/probes/kprobes.c
  4. *
  5. * Kprobes support for ARM64
  6. *
  7. * Copyright (C) 2013 Linaro Limited.
  8. * Author: Sandeepa Prabhu <sandeepa.prabhu@linaro.org>
  9. */
  10. #define pr_fmt(fmt) "kprobes: " fmt
  11. #include <linux/execmem.h>
  12. #include <linux/extable.h>
  13. #include <linux/kasan.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kprobes.h>
  16. #include <linux/sched/debug.h>
  17. #include <linux/set_memory.h>
  18. #include <linux/slab.h>
  19. #include <linux/stop_machine.h>
  20. #include <linux/stringify.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/vmalloc.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/daifflags.h>
  25. #include <asm/debug-monitors.h>
  26. #include <asm/insn.h>
  27. #include <asm/irq.h>
  28. #include <asm/patching.h>
  29. #include <asm/ptrace.h>
  30. #include <asm/sections.h>
  31. #include <asm/system_misc.h>
  32. #include <asm/traps.h>
  33. #include "decode-insn.h"
  34. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  35. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  36. static void __kprobes
  37. post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
  38. void *alloc_insn_page(void)
  39. {
  40. void *addr;
  41. addr = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
  42. if (!addr)
  43. return NULL;
  44. set_memory_rox((unsigned long)addr, 1);
  45. return addr;
  46. }
  47. static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
  48. {
  49. kprobe_opcode_t *addr = p->ainsn.api.insn;
  50. /*
  51. * Prepare insn slot, Mark Rutland points out it depends on a coupe of
  52. * subtleties:
  53. *
  54. * - That the I-cache maintenance for these instructions is complete
  55. * *before* the kprobe BRK is written (and aarch64_insn_patch_text_nosync()
  56. * ensures this, but just omits causing a Context-Synchronization-Event
  57. * on all CPUS).
  58. *
  59. * - That the kprobe BRK results in an exception (and consequently a
  60. * Context-Synchronoization-Event), which ensures that the CPU will
  61. * fetch thesingle-step slot instructions *after* this, ensuring that
  62. * the new instructions are used
  63. *
  64. * It supposes to place ISB after patching to guarantee I-cache maintenance
  65. * is observed on all CPUS, however, single-step slot is installed in
  66. * the BRK exception handler, so it is unnecessary to generate
  67. * Contex-Synchronization-Event via ISB again.
  68. */
  69. aarch64_insn_patch_text_nosync(addr, p->opcode);
  70. aarch64_insn_patch_text_nosync(addr + 1, BRK64_OPCODE_KPROBES_SS);
  71. /*
  72. * Needs restoring of return address after stepping xol.
  73. */
  74. p->ainsn.api.restore = (unsigned long) p->addr +
  75. sizeof(kprobe_opcode_t);
  76. }
  77. static void __kprobes arch_prepare_simulate(struct kprobe *p)
  78. {
  79. /* This instructions is not executed xol. No need to adjust the PC */
  80. p->ainsn.api.restore = 0;
  81. }
  82. static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
  83. {
  84. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  85. if (p->ainsn.api.handler)
  86. p->ainsn.api.handler((u32)p->opcode, (long)p->addr, regs);
  87. /* single step simulated, now go for post processing */
  88. post_kprobe_handler(p, kcb, regs);
  89. }
  90. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  91. {
  92. unsigned long probe_addr = (unsigned long)p->addr;
  93. if (probe_addr & 0x3)
  94. return -EINVAL;
  95. /* copy instruction */
  96. p->opcode = le32_to_cpu(*p->addr);
  97. if (search_exception_tables(probe_addr))
  98. return -EINVAL;
  99. /* decode instruction */
  100. switch (arm_kprobe_decode_insn(p->addr, &p->ainsn)) {
  101. case INSN_REJECTED: /* insn not supported */
  102. return -EINVAL;
  103. case INSN_GOOD_NO_SLOT: /* insn need simulation */
  104. p->ainsn.api.insn = NULL;
  105. break;
  106. case INSN_GOOD: /* instruction uses slot */
  107. p->ainsn.api.insn = get_insn_slot();
  108. if (!p->ainsn.api.insn)
  109. return -ENOMEM;
  110. break;
  111. }
  112. /* prepare the instruction */
  113. if (p->ainsn.api.insn)
  114. arch_prepare_ss_slot(p);
  115. else
  116. arch_prepare_simulate(p);
  117. return 0;
  118. }
  119. /* arm kprobe: install breakpoint in text */
  120. void __kprobes arch_arm_kprobe(struct kprobe *p)
  121. {
  122. void *addr = p->addr;
  123. u32 insn = BRK64_OPCODE_KPROBES;
  124. aarch64_insn_patch_text(&addr, &insn, 1);
  125. }
  126. /* disarm kprobe: remove breakpoint from text */
  127. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  128. {
  129. void *addr = p->addr;
  130. aarch64_insn_patch_text(&addr, &p->opcode, 1);
  131. }
  132. void __kprobes arch_remove_kprobe(struct kprobe *p)
  133. {
  134. if (p->ainsn.api.insn) {
  135. free_insn_slot(p->ainsn.api.insn, 0);
  136. p->ainsn.api.insn = NULL;
  137. }
  138. }
  139. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  140. {
  141. kcb->prev_kprobe.kp = kprobe_running();
  142. kcb->prev_kprobe.status = kcb->kprobe_status;
  143. }
  144. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  145. {
  146. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  147. kcb->kprobe_status = kcb->prev_kprobe.status;
  148. }
  149. static void __kprobes set_current_kprobe(struct kprobe *p)
  150. {
  151. __this_cpu_write(current_kprobe, p);
  152. }
  153. /*
  154. * Mask all of DAIF while executing the instruction out-of-line, to keep things
  155. * simple and avoid nesting exceptions. Interrupts do have to be disabled since
  156. * the kprobe state is per-CPU and doesn't get migrated.
  157. */
  158. static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
  159. struct pt_regs *regs)
  160. {
  161. kcb->saved_irqflag = regs->pstate & DAIF_MASK;
  162. regs->pstate |= DAIF_MASK;
  163. }
  164. static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
  165. struct pt_regs *regs)
  166. {
  167. regs->pstate &= ~DAIF_MASK;
  168. regs->pstate |= kcb->saved_irqflag;
  169. }
  170. static void __kprobes setup_singlestep(struct kprobe *p,
  171. struct pt_regs *regs,
  172. struct kprobe_ctlblk *kcb, int reenter)
  173. {
  174. unsigned long slot;
  175. if (reenter) {
  176. save_previous_kprobe(kcb);
  177. set_current_kprobe(p);
  178. kcb->kprobe_status = KPROBE_REENTER;
  179. } else {
  180. kcb->kprobe_status = KPROBE_HIT_SS;
  181. }
  182. if (p->ainsn.api.insn) {
  183. /* prepare for single stepping */
  184. slot = (unsigned long)p->ainsn.api.insn;
  185. kprobes_save_local_irqflag(kcb, regs);
  186. instruction_pointer_set(regs, slot);
  187. } else {
  188. /* insn simulation */
  189. arch_simulate_insn(p, regs);
  190. }
  191. }
  192. static int __kprobes reenter_kprobe(struct kprobe *p,
  193. struct pt_regs *regs,
  194. struct kprobe_ctlblk *kcb)
  195. {
  196. switch (kcb->kprobe_status) {
  197. case KPROBE_HIT_SSDONE:
  198. case KPROBE_HIT_ACTIVE:
  199. kprobes_inc_nmissed_count(p);
  200. setup_singlestep(p, regs, kcb, 1);
  201. break;
  202. case KPROBE_HIT_SS:
  203. case KPROBE_REENTER:
  204. pr_warn("Failed to recover from reentered kprobes.\n");
  205. dump_kprobe(p);
  206. BUG();
  207. break;
  208. default:
  209. WARN_ON(1);
  210. return 0;
  211. }
  212. return 1;
  213. }
  214. static void __kprobes
  215. post_kprobe_handler(struct kprobe *cur, struct kprobe_ctlblk *kcb, struct pt_regs *regs)
  216. {
  217. /* return addr restore if non-branching insn */
  218. if (cur->ainsn.api.restore != 0)
  219. instruction_pointer_set(regs, cur->ainsn.api.restore);
  220. /* restore back original saved kprobe variables and continue */
  221. if (kcb->kprobe_status == KPROBE_REENTER) {
  222. restore_previous_kprobe(kcb);
  223. return;
  224. }
  225. /* call post handler */
  226. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  227. if (cur->post_handler)
  228. cur->post_handler(cur, regs, 0);
  229. reset_current_kprobe();
  230. }
  231. int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
  232. {
  233. struct kprobe *cur = kprobe_running();
  234. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  235. switch (kcb->kprobe_status) {
  236. case KPROBE_HIT_SS:
  237. case KPROBE_REENTER:
  238. /*
  239. * We are here because the instruction being single
  240. * stepped caused a page fault. We reset the current
  241. * kprobe and the ip points back to the probe address
  242. * and allow the page fault handler to continue as a
  243. * normal page fault.
  244. */
  245. instruction_pointer_set(regs, (unsigned long) cur->addr);
  246. BUG_ON(!instruction_pointer(regs));
  247. if (kcb->kprobe_status == KPROBE_REENTER) {
  248. restore_previous_kprobe(kcb);
  249. } else {
  250. kprobes_restore_local_irqflag(kcb, regs);
  251. reset_current_kprobe();
  252. }
  253. break;
  254. }
  255. return 0;
  256. }
  257. static int __kprobes
  258. kprobe_breakpoint_handler(struct pt_regs *regs, unsigned long esr)
  259. {
  260. struct kprobe *p, *cur_kprobe;
  261. struct kprobe_ctlblk *kcb;
  262. unsigned long addr = instruction_pointer(regs);
  263. kcb = get_kprobe_ctlblk();
  264. cur_kprobe = kprobe_running();
  265. p = get_kprobe((kprobe_opcode_t *) addr);
  266. if (WARN_ON_ONCE(!p)) {
  267. /*
  268. * Something went wrong. This BRK used an immediate reserved
  269. * for kprobes, but we couldn't find any corresponding probe.
  270. */
  271. return DBG_HOOK_ERROR;
  272. }
  273. if (cur_kprobe) {
  274. /* Hit a kprobe inside another kprobe */
  275. if (!reenter_kprobe(p, regs, kcb))
  276. return DBG_HOOK_ERROR;
  277. } else {
  278. /* Probe hit */
  279. set_current_kprobe(p);
  280. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  281. /*
  282. * If we have no pre-handler or it returned 0, we
  283. * continue with normal processing. If we have a
  284. * pre-handler and it returned non-zero, it will
  285. * modify the execution path and not need to single-step
  286. * Let's just reset current kprobe and exit.
  287. */
  288. if (!p->pre_handler || !p->pre_handler(p, regs))
  289. setup_singlestep(p, regs, kcb, 0);
  290. else
  291. reset_current_kprobe();
  292. }
  293. return DBG_HOOK_HANDLED;
  294. }
  295. static struct break_hook kprobes_break_hook = {
  296. .imm = KPROBES_BRK_IMM,
  297. .fn = kprobe_breakpoint_handler,
  298. };
  299. static int __kprobes
  300. kprobe_breakpoint_ss_handler(struct pt_regs *regs, unsigned long esr)
  301. {
  302. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  303. unsigned long addr = instruction_pointer(regs);
  304. struct kprobe *cur = kprobe_running();
  305. if (cur && (kcb->kprobe_status & (KPROBE_HIT_SS | KPROBE_REENTER)) &&
  306. ((unsigned long)&cur->ainsn.api.insn[1] == addr)) {
  307. kprobes_restore_local_irqflag(kcb, regs);
  308. post_kprobe_handler(cur, kcb, regs);
  309. return DBG_HOOK_HANDLED;
  310. }
  311. /* not ours, kprobes should ignore it */
  312. return DBG_HOOK_ERROR;
  313. }
  314. static struct break_hook kprobes_break_ss_hook = {
  315. .imm = KPROBES_BRK_SS_IMM,
  316. .fn = kprobe_breakpoint_ss_handler,
  317. };
  318. static int __kprobes
  319. kretprobe_breakpoint_handler(struct pt_regs *regs, unsigned long esr)
  320. {
  321. if (regs->pc != (unsigned long)__kretprobe_trampoline)
  322. return DBG_HOOK_ERROR;
  323. regs->pc = kretprobe_trampoline_handler(regs, (void *)regs->regs[29]);
  324. return DBG_HOOK_HANDLED;
  325. }
  326. static struct break_hook kretprobes_break_hook = {
  327. .imm = KRETPROBES_BRK_IMM,
  328. .fn = kretprobe_breakpoint_handler,
  329. };
  330. /*
  331. * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
  332. * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
  333. */
  334. int __init arch_populate_kprobe_blacklist(void)
  335. {
  336. int ret;
  337. ret = kprobe_add_area_blacklist((unsigned long)__entry_text_start,
  338. (unsigned long)__entry_text_end);
  339. if (ret)
  340. return ret;
  341. ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
  342. (unsigned long)__irqentry_text_end);
  343. if (ret)
  344. return ret;
  345. ret = kprobe_add_area_blacklist((unsigned long)__hyp_text_start,
  346. (unsigned long)__hyp_text_end);
  347. if (ret || is_kernel_in_hyp_mode())
  348. return ret;
  349. ret = kprobe_add_area_blacklist((unsigned long)__hyp_idmap_text_start,
  350. (unsigned long)__hyp_idmap_text_end);
  351. return ret;
  352. }
  353. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  354. struct pt_regs *regs)
  355. {
  356. ri->ret_addr = (kprobe_opcode_t *)regs->regs[30];
  357. ri->fp = (void *)regs->regs[29];
  358. /* replace return addr (x30) with trampoline */
  359. regs->regs[30] = (long)&__kretprobe_trampoline;
  360. }
  361. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  362. {
  363. return 0;
  364. }
  365. int __init arch_init_kprobes(void)
  366. {
  367. register_kernel_break_hook(&kprobes_break_hook);
  368. register_kernel_break_hook(&kprobes_break_ss_hook);
  369. register_kernel_break_hook(&kretprobes_break_hook);
  370. return 0;
  371. }