core.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. * Kernel Probes (KProbes)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2002, 2004
  19. *
  20. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  21. * Probes initial implementation ( includes contributions from
  22. * Rusty Russell).
  23. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  24. * interface to access function arguments.
  25. * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  26. * <prasanna@in.ibm.com> adapted for x86_64 from i386.
  27. * 2005-Mar Roland McGrath <roland@redhat.com>
  28. * Fixed to handle %rip-relative addressing mode correctly.
  29. * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
  30. * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  31. * <prasanna@in.ibm.com> added function-return probes.
  32. * 2005-May Rusty Lynch <rusty.lynch@intel.com>
  33. * Added function return probes functionality
  34. * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
  35. * kprobe-booster and kretprobe-booster for i386.
  36. * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
  37. * and kretprobe-booster for x86-64
  38. * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
  39. * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
  40. * unified x86 kprobes code.
  41. */
  42. #include <linux/kprobes.h>
  43. #include <linux/ptrace.h>
  44. #include <linux/string.h>
  45. #include <linux/slab.h>
  46. #include <linux/hardirq.h>
  47. #include <linux/preempt.h>
  48. #include <linux/sched/debug.h>
  49. #include <linux/extable.h>
  50. #include <linux/kdebug.h>
  51. #include <linux/kallsyms.h>
  52. #include <linux/ftrace.h>
  53. #include <linux/frame.h>
  54. #include <linux/kasan.h>
  55. #include <linux/moduleloader.h>
  56. #include <asm/text-patching.h>
  57. #include <asm/cacheflush.h>
  58. #include <asm/desc.h>
  59. #include <asm/pgtable.h>
  60. #include <linux/uaccess.h>
  61. #include <asm/alternative.h>
  62. #include <asm/insn.h>
  63. #include <asm/debugreg.h>
  64. #include <asm/set_memory.h>
  65. #include "common.h"
  66. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  67. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  68. #define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
  69. #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
  70. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  71. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  72. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  73. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  74. << (row % 32))
  75. /*
  76. * Undefined/reserved opcodes, conditional jump, Opcode Extension
  77. * Groups, and some special opcodes can not boost.
  78. * This is non-const and volatile to keep gcc from statically
  79. * optimizing it out, as variable_test_bit makes gcc think only
  80. * *(unsigned long*) is used.
  81. */
  82. static volatile u32 twobyte_is_boostable[256 / 32] = {
  83. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  84. /* ---------------------------------------------- */
  85. W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
  86. W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) , /* 10 */
  87. W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
  88. W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
  89. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  90. W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
  91. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
  92. W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
  93. W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
  94. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  95. W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
  96. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
  97. W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
  98. W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
  99. W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
  100. W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
  101. /* ----------------------------------------------- */
  102. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  103. };
  104. #undef W
  105. struct kretprobe_blackpoint kretprobe_blacklist[] = {
  106. {"__switch_to", }, /* This function switches only current task, but
  107. doesn't switch kernel stack.*/
  108. {NULL, NULL} /* Terminator */
  109. };
  110. const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
  111. static nokprobe_inline void
  112. __synthesize_relative_insn(void *dest, void *from, void *to, u8 op)
  113. {
  114. struct __arch_relative_insn {
  115. u8 op;
  116. s32 raddr;
  117. } __packed *insn;
  118. insn = (struct __arch_relative_insn *)dest;
  119. insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
  120. insn->op = op;
  121. }
  122. /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
  123. void synthesize_reljump(void *dest, void *from, void *to)
  124. {
  125. __synthesize_relative_insn(dest, from, to, RELATIVEJUMP_OPCODE);
  126. }
  127. NOKPROBE_SYMBOL(synthesize_reljump);
  128. /* Insert a call instruction at address 'from', which calls address 'to'.*/
  129. void synthesize_relcall(void *dest, void *from, void *to)
  130. {
  131. __synthesize_relative_insn(dest, from, to, RELATIVECALL_OPCODE);
  132. }
  133. NOKPROBE_SYMBOL(synthesize_relcall);
  134. /*
  135. * Skip the prefixes of the instruction.
  136. */
  137. static kprobe_opcode_t *skip_prefixes(kprobe_opcode_t *insn)
  138. {
  139. insn_attr_t attr;
  140. attr = inat_get_opcode_attribute((insn_byte_t)*insn);
  141. while (inat_is_legacy_prefix(attr)) {
  142. insn++;
  143. attr = inat_get_opcode_attribute((insn_byte_t)*insn);
  144. }
  145. #ifdef CONFIG_X86_64
  146. if (inat_is_rex_prefix(attr))
  147. insn++;
  148. #endif
  149. return insn;
  150. }
  151. NOKPROBE_SYMBOL(skip_prefixes);
  152. /*
  153. * Returns non-zero if INSN is boostable.
  154. * RIP relative instructions are adjusted at copying time in 64 bits mode
  155. */
  156. int can_boost(struct insn *insn, void *addr)
  157. {
  158. kprobe_opcode_t opcode;
  159. if (search_exception_tables((unsigned long)addr))
  160. return 0; /* Page fault may occur on this address. */
  161. /* 2nd-byte opcode */
  162. if (insn->opcode.nbytes == 2)
  163. return test_bit(insn->opcode.bytes[1],
  164. (unsigned long *)twobyte_is_boostable);
  165. if (insn->opcode.nbytes != 1)
  166. return 0;
  167. /* Can't boost Address-size override prefix */
  168. if (unlikely(inat_is_address_size_prefix(insn->attr)))
  169. return 0;
  170. opcode = insn->opcode.bytes[0];
  171. switch (opcode & 0xf0) {
  172. case 0x60:
  173. /* can't boost "bound" */
  174. return (opcode != 0x62);
  175. case 0x70:
  176. return 0; /* can't boost conditional jump */
  177. case 0x90:
  178. return opcode != 0x9a; /* can't boost call far */
  179. case 0xc0:
  180. /* can't boost software-interruptions */
  181. return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
  182. case 0xd0:
  183. /* can boost AA* and XLAT */
  184. return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
  185. case 0xe0:
  186. /* can boost in/out and absolute jmps */
  187. return ((opcode & 0x04) || opcode == 0xea);
  188. case 0xf0:
  189. /* clear and set flags are boostable */
  190. return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
  191. default:
  192. /* CS override prefix and call are not boostable */
  193. return (opcode != 0x2e && opcode != 0x9a);
  194. }
  195. }
  196. static unsigned long
  197. __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)
  198. {
  199. struct kprobe *kp;
  200. unsigned long faddr;
  201. kp = get_kprobe((void *)addr);
  202. faddr = ftrace_location(addr);
  203. /*
  204. * Addresses inside the ftrace location are refused by
  205. * arch_check_ftrace_location(). Something went terribly wrong
  206. * if such an address is checked here.
  207. */
  208. if (WARN_ON(faddr && faddr != addr))
  209. return 0UL;
  210. /*
  211. * Use the current code if it is not modified by Kprobe
  212. * and it cannot be modified by ftrace.
  213. */
  214. if (!kp && !faddr)
  215. return addr;
  216. /*
  217. * Basically, kp->ainsn.insn has an original instruction.
  218. * However, RIP-relative instruction can not do single-stepping
  219. * at different place, __copy_instruction() tweaks the displacement of
  220. * that instruction. In that case, we can't recover the instruction
  221. * from the kp->ainsn.insn.
  222. *
  223. * On the other hand, in case on normal Kprobe, kp->opcode has a copy
  224. * of the first byte of the probed instruction, which is overwritten
  225. * by int3. And the instruction at kp->addr is not modified by kprobes
  226. * except for the first byte, we can recover the original instruction
  227. * from it and kp->opcode.
  228. *
  229. * In case of Kprobes using ftrace, we do not have a copy of
  230. * the original instruction. In fact, the ftrace location might
  231. * be modified at anytime and even could be in an inconsistent state.
  232. * Fortunately, we know that the original code is the ideal 5-byte
  233. * long NOP.
  234. */
  235. if (probe_kernel_read(buf, (void *)addr,
  236. MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
  237. return 0UL;
  238. if (faddr)
  239. memcpy(buf, ideal_nops[NOP_ATOMIC5], 5);
  240. else
  241. buf[0] = kp->opcode;
  242. return (unsigned long)buf;
  243. }
  244. /*
  245. * Recover the probed instruction at addr for further analysis.
  246. * Caller must lock kprobes by kprobe_mutex, or disable preemption
  247. * for preventing to release referencing kprobes.
  248. * Returns zero if the instruction can not get recovered (or access failed).
  249. */
  250. unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
  251. {
  252. unsigned long __addr;
  253. __addr = __recover_optprobed_insn(buf, addr);
  254. if (__addr != addr)
  255. return __addr;
  256. return __recover_probed_insn(buf, addr);
  257. }
  258. /* Check if paddr is at an instruction boundary */
  259. static int can_probe(unsigned long paddr)
  260. {
  261. unsigned long addr, __addr, offset = 0;
  262. struct insn insn;
  263. kprobe_opcode_t buf[MAX_INSN_SIZE];
  264. if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
  265. return 0;
  266. /* Decode instructions */
  267. addr = paddr - offset;
  268. while (addr < paddr) {
  269. /*
  270. * Check if the instruction has been modified by another
  271. * kprobe, in which case we replace the breakpoint by the
  272. * original instruction in our buffer.
  273. * Also, jump optimization will change the breakpoint to
  274. * relative-jump. Since the relative-jump itself is
  275. * normally used, we just go through if there is no kprobe.
  276. */
  277. __addr = recover_probed_instruction(buf, addr);
  278. if (!__addr)
  279. return 0;
  280. kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE);
  281. insn_get_length(&insn);
  282. /*
  283. * Another debugging subsystem might insert this breakpoint.
  284. * In that case, we can't recover it.
  285. */
  286. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  287. return 0;
  288. addr += insn.length;
  289. }
  290. return (addr == paddr);
  291. }
  292. /*
  293. * Returns non-zero if opcode modifies the interrupt flag.
  294. */
  295. static int is_IF_modifier(kprobe_opcode_t *insn)
  296. {
  297. /* Skip prefixes */
  298. insn = skip_prefixes(insn);
  299. switch (*insn) {
  300. case 0xfa: /* cli */
  301. case 0xfb: /* sti */
  302. case 0xcf: /* iret/iretd */
  303. case 0x9d: /* popf/popfd */
  304. return 1;
  305. }
  306. return 0;
  307. }
  308. /*
  309. * Copy an instruction with recovering modified instruction by kprobes
  310. * and adjust the displacement if the instruction uses the %rip-relative
  311. * addressing mode. Note that since @real will be the final place of copied
  312. * instruction, displacement must be adjust by @real, not @dest.
  313. * This returns the length of copied instruction, or 0 if it has an error.
  314. */
  315. int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn)
  316. {
  317. kprobe_opcode_t buf[MAX_INSN_SIZE];
  318. unsigned long recovered_insn =
  319. recover_probed_instruction(buf, (unsigned long)src);
  320. if (!recovered_insn || !insn)
  321. return 0;
  322. /* This can access kernel text if given address is not recovered */
  323. if (probe_kernel_read(dest, (void *)recovered_insn, MAX_INSN_SIZE))
  324. return 0;
  325. kernel_insn_init(insn, dest, MAX_INSN_SIZE);
  326. insn_get_length(insn);
  327. /* Another subsystem puts a breakpoint, failed to recover */
  328. if (insn->opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  329. return 0;
  330. /* We should not singlestep on the exception masking instructions */
  331. if (insn_masking_exception(insn))
  332. return 0;
  333. #ifdef CONFIG_X86_64
  334. /* Only x86_64 has RIP relative instructions */
  335. if (insn_rip_relative(insn)) {
  336. s64 newdisp;
  337. u8 *disp;
  338. /*
  339. * The copied instruction uses the %rip-relative addressing
  340. * mode. Adjust the displacement for the difference between
  341. * the original location of this instruction and the location
  342. * of the copy that will actually be run. The tricky bit here
  343. * is making sure that the sign extension happens correctly in
  344. * this calculation, since we need a signed 32-bit result to
  345. * be sign-extended to 64 bits when it's added to the %rip
  346. * value and yield the same 64-bit result that the sign-
  347. * extension of the original signed 32-bit displacement would
  348. * have given.
  349. */
  350. newdisp = (u8 *) src + (s64) insn->displacement.value
  351. - (u8 *) real;
  352. if ((s64) (s32) newdisp != newdisp) {
  353. pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp);
  354. return 0;
  355. }
  356. disp = (u8 *) dest + insn_offset_displacement(insn);
  357. *(s32 *) disp = (s32) newdisp;
  358. }
  359. #endif
  360. return insn->length;
  361. }
  362. /* Prepare reljump right after instruction to boost */
  363. static int prepare_boost(kprobe_opcode_t *buf, struct kprobe *p,
  364. struct insn *insn)
  365. {
  366. int len = insn->length;
  367. if (can_boost(insn, p->addr) &&
  368. MAX_INSN_SIZE - len >= RELATIVEJUMP_SIZE) {
  369. /*
  370. * These instructions can be executed directly if it
  371. * jumps back to correct address.
  372. */
  373. synthesize_reljump(buf + len, p->ainsn.insn + len,
  374. p->addr + insn->length);
  375. len += RELATIVEJUMP_SIZE;
  376. p->ainsn.boostable = true;
  377. } else {
  378. p->ainsn.boostable = false;
  379. }
  380. return len;
  381. }
  382. /* Make page to RO mode when allocate it */
  383. void *alloc_insn_page(void)
  384. {
  385. void *page;
  386. page = module_alloc(PAGE_SIZE);
  387. if (page)
  388. set_memory_ro((unsigned long)page & PAGE_MASK, 1);
  389. return page;
  390. }
  391. /* Recover page to RW mode before releasing it */
  392. void free_insn_page(void *page)
  393. {
  394. set_memory_nx((unsigned long)page & PAGE_MASK, 1);
  395. set_memory_rw((unsigned long)page & PAGE_MASK, 1);
  396. module_memfree(page);
  397. }
  398. static int arch_copy_kprobe(struct kprobe *p)
  399. {
  400. struct insn insn;
  401. kprobe_opcode_t buf[MAX_INSN_SIZE];
  402. int len;
  403. /* Copy an instruction with recovering if other optprobe modifies it.*/
  404. len = __copy_instruction(buf, p->addr, p->ainsn.insn, &insn);
  405. if (!len)
  406. return -EINVAL;
  407. /*
  408. * __copy_instruction can modify the displacement of the instruction,
  409. * but it doesn't affect boostable check.
  410. */
  411. len = prepare_boost(buf, p, &insn);
  412. /* Check whether the instruction modifies Interrupt Flag or not */
  413. p->ainsn.if_modifier = is_IF_modifier(buf);
  414. /* Also, displacement change doesn't affect the first byte */
  415. p->opcode = buf[0];
  416. /* OK, write back the instruction(s) into ROX insn buffer */
  417. text_poke(p->ainsn.insn, buf, len);
  418. return 0;
  419. }
  420. int arch_prepare_kprobe(struct kprobe *p)
  421. {
  422. int ret;
  423. if (alternatives_text_reserved(p->addr, p->addr))
  424. return -EINVAL;
  425. if (!can_probe((unsigned long)p->addr))
  426. return -EILSEQ;
  427. /* insn: must be on special executable page on x86. */
  428. p->ainsn.insn = get_insn_slot();
  429. if (!p->ainsn.insn)
  430. return -ENOMEM;
  431. ret = arch_copy_kprobe(p);
  432. if (ret) {
  433. free_insn_slot(p->ainsn.insn, 0);
  434. p->ainsn.insn = NULL;
  435. }
  436. return ret;
  437. }
  438. void arch_arm_kprobe(struct kprobe *p)
  439. {
  440. text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
  441. }
  442. void arch_disarm_kprobe(struct kprobe *p)
  443. {
  444. text_poke(p->addr, &p->opcode, 1);
  445. }
  446. void arch_remove_kprobe(struct kprobe *p)
  447. {
  448. if (p->ainsn.insn) {
  449. free_insn_slot(p->ainsn.insn, p->ainsn.boostable);
  450. p->ainsn.insn = NULL;
  451. }
  452. }
  453. static nokprobe_inline void
  454. save_previous_kprobe(struct kprobe_ctlblk *kcb)
  455. {
  456. kcb->prev_kprobe.kp = kprobe_running();
  457. kcb->prev_kprobe.status = kcb->kprobe_status;
  458. kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
  459. kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
  460. }
  461. static nokprobe_inline void
  462. restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  463. {
  464. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  465. kcb->kprobe_status = kcb->prev_kprobe.status;
  466. kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
  467. kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
  468. }
  469. static nokprobe_inline void
  470. set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  471. struct kprobe_ctlblk *kcb)
  472. {
  473. __this_cpu_write(current_kprobe, p);
  474. kcb->kprobe_saved_flags = kcb->kprobe_old_flags
  475. = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
  476. if (p->ainsn.if_modifier)
  477. kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
  478. }
  479. static nokprobe_inline void clear_btf(void)
  480. {
  481. if (test_thread_flag(TIF_BLOCKSTEP)) {
  482. unsigned long debugctl = get_debugctlmsr();
  483. debugctl &= ~DEBUGCTLMSR_BTF;
  484. update_debugctlmsr(debugctl);
  485. }
  486. }
  487. static nokprobe_inline void restore_btf(void)
  488. {
  489. if (test_thread_flag(TIF_BLOCKSTEP)) {
  490. unsigned long debugctl = get_debugctlmsr();
  491. debugctl |= DEBUGCTLMSR_BTF;
  492. update_debugctlmsr(debugctl);
  493. }
  494. }
  495. void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
  496. {
  497. unsigned long *sara = stack_addr(regs);
  498. ri->ret_addr = (kprobe_opcode_t *) *sara;
  499. /* Replace the return addr with trampoline addr */
  500. *sara = (unsigned long) &kretprobe_trampoline;
  501. }
  502. NOKPROBE_SYMBOL(arch_prepare_kretprobe);
  503. static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
  504. struct kprobe_ctlblk *kcb, int reenter)
  505. {
  506. if (setup_detour_execution(p, regs, reenter))
  507. return;
  508. #if !defined(CONFIG_PREEMPT)
  509. if (p->ainsn.boostable && !p->post_handler) {
  510. /* Boost up -- we can execute copied instructions directly */
  511. if (!reenter)
  512. reset_current_kprobe();
  513. /*
  514. * Reentering boosted probe doesn't reset current_kprobe,
  515. * nor set current_kprobe, because it doesn't use single
  516. * stepping.
  517. */
  518. regs->ip = (unsigned long)p->ainsn.insn;
  519. return;
  520. }
  521. #endif
  522. if (reenter) {
  523. save_previous_kprobe(kcb);
  524. set_current_kprobe(p, regs, kcb);
  525. kcb->kprobe_status = KPROBE_REENTER;
  526. } else
  527. kcb->kprobe_status = KPROBE_HIT_SS;
  528. /* Prepare real single stepping */
  529. clear_btf();
  530. regs->flags |= X86_EFLAGS_TF;
  531. regs->flags &= ~X86_EFLAGS_IF;
  532. /* single step inline if the instruction is an int3 */
  533. if (p->opcode == BREAKPOINT_INSTRUCTION)
  534. regs->ip = (unsigned long)p->addr;
  535. else
  536. regs->ip = (unsigned long)p->ainsn.insn;
  537. }
  538. NOKPROBE_SYMBOL(setup_singlestep);
  539. /*
  540. * We have reentered the kprobe_handler(), since another probe was hit while
  541. * within the handler. We save the original kprobes variables and just single
  542. * step on the instruction of the new probe without calling any user handlers.
  543. */
  544. static int reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
  545. struct kprobe_ctlblk *kcb)
  546. {
  547. switch (kcb->kprobe_status) {
  548. case KPROBE_HIT_SSDONE:
  549. case KPROBE_HIT_ACTIVE:
  550. case KPROBE_HIT_SS:
  551. kprobes_inc_nmissed_count(p);
  552. setup_singlestep(p, regs, kcb, 1);
  553. break;
  554. case KPROBE_REENTER:
  555. /* A probe has been hit in the codepath leading up to, or just
  556. * after, single-stepping of a probed instruction. This entire
  557. * codepath should strictly reside in .kprobes.text section.
  558. * Raise a BUG or we'll continue in an endless reentering loop
  559. * and eventually a stack overflow.
  560. */
  561. pr_err("Unrecoverable kprobe detected.\n");
  562. dump_kprobe(p);
  563. BUG();
  564. default:
  565. /* impossible cases */
  566. WARN_ON(1);
  567. return 0;
  568. }
  569. return 1;
  570. }
  571. NOKPROBE_SYMBOL(reenter_kprobe);
  572. /*
  573. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  574. * remain disabled throughout this function.
  575. */
  576. int kprobe_int3_handler(struct pt_regs *regs)
  577. {
  578. kprobe_opcode_t *addr;
  579. struct kprobe *p;
  580. struct kprobe_ctlblk *kcb;
  581. if (user_mode(regs))
  582. return 0;
  583. addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
  584. /*
  585. * We don't want to be preempted for the entire duration of kprobe
  586. * processing. Since int3 and debug trap disables irqs and we clear
  587. * IF while singlestepping, it must be no preemptible.
  588. */
  589. kcb = get_kprobe_ctlblk();
  590. p = get_kprobe(addr);
  591. if (p) {
  592. if (kprobe_running()) {
  593. if (reenter_kprobe(p, regs, kcb))
  594. return 1;
  595. } else {
  596. set_current_kprobe(p, regs, kcb);
  597. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  598. /*
  599. * If we have no pre-handler or it returned 0, we
  600. * continue with normal processing. If we have a
  601. * pre-handler and it returned non-zero, that means
  602. * user handler setup registers to exit to another
  603. * instruction, we must skip the single stepping.
  604. */
  605. if (!p->pre_handler || !p->pre_handler(p, regs))
  606. setup_singlestep(p, regs, kcb, 0);
  607. else
  608. reset_current_kprobe();
  609. return 1;
  610. }
  611. } else if (*addr != BREAKPOINT_INSTRUCTION) {
  612. /*
  613. * The breakpoint instruction was removed right
  614. * after we hit it. Another cpu has removed
  615. * either a probepoint or a debugger breakpoint
  616. * at this address. In either case, no further
  617. * handling of this interrupt is appropriate.
  618. * Back up over the (now missing) int3 and run
  619. * the original instruction.
  620. */
  621. regs->ip = (unsigned long)addr;
  622. return 1;
  623. } /* else: not a kprobe fault; let the kernel handle it */
  624. return 0;
  625. }
  626. NOKPROBE_SYMBOL(kprobe_int3_handler);
  627. /*
  628. * When a retprobed function returns, this code saves registers and
  629. * calls trampoline_handler() runs, which calls the kretprobe's handler.
  630. */
  631. asm(
  632. ".global kretprobe_trampoline\n"
  633. ".type kretprobe_trampoline, @function\n"
  634. "kretprobe_trampoline:\n"
  635. #ifdef CONFIG_X86_64
  636. /* We don't bother saving the ss register */
  637. " pushq %rsp\n"
  638. " pushfq\n"
  639. SAVE_REGS_STRING
  640. " movq %rsp, %rdi\n"
  641. " call trampoline_handler\n"
  642. /* Replace saved sp with true return address. */
  643. " movq %rax, 152(%rsp)\n"
  644. RESTORE_REGS_STRING
  645. " popfq\n"
  646. #else
  647. " pushf\n"
  648. SAVE_REGS_STRING
  649. " movl %esp, %eax\n"
  650. " call trampoline_handler\n"
  651. /* Move flags to cs */
  652. " movl 56(%esp), %edx\n"
  653. " movl %edx, 52(%esp)\n"
  654. /* Replace saved flags with true return address. */
  655. " movl %eax, 56(%esp)\n"
  656. RESTORE_REGS_STRING
  657. " popf\n"
  658. #endif
  659. " ret\n"
  660. ".size kretprobe_trampoline, .-kretprobe_trampoline\n"
  661. );
  662. NOKPROBE_SYMBOL(kretprobe_trampoline);
  663. STACK_FRAME_NON_STANDARD(kretprobe_trampoline);
  664. /*
  665. * Called from kretprobe_trampoline
  666. */
  667. __visible __used void *trampoline_handler(struct pt_regs *regs)
  668. {
  669. struct kretprobe_instance *ri = NULL;
  670. struct hlist_head *head, empty_rp;
  671. struct hlist_node *tmp;
  672. unsigned long flags, orig_ret_address = 0;
  673. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  674. kprobe_opcode_t *correct_ret_addr = NULL;
  675. INIT_HLIST_HEAD(&empty_rp);
  676. kretprobe_hash_lock(current, &head, &flags);
  677. /* fixup registers */
  678. #ifdef CONFIG_X86_64
  679. regs->cs = __KERNEL_CS;
  680. #else
  681. regs->cs = __KERNEL_CS | get_kernel_rpl();
  682. regs->gs = 0;
  683. #endif
  684. regs->ip = trampoline_address;
  685. regs->orig_ax = ~0UL;
  686. /*
  687. * It is possible to have multiple instances associated with a given
  688. * task either because multiple functions in the call path have
  689. * return probes installed on them, and/or more than one
  690. * return probe was registered for a target function.
  691. *
  692. * We can handle this because:
  693. * - instances are always pushed into the head of the list
  694. * - when multiple return probes are registered for the same
  695. * function, the (chronologically) first instance's ret_addr
  696. * will be the real return address, and all the rest will
  697. * point to kretprobe_trampoline.
  698. */
  699. hlist_for_each_entry(ri, head, hlist) {
  700. if (ri->task != current)
  701. /* another task is sharing our hash bucket */
  702. continue;
  703. orig_ret_address = (unsigned long)ri->ret_addr;
  704. if (orig_ret_address != trampoline_address)
  705. /*
  706. * This is the real return address. Any other
  707. * instances associated with this task are for
  708. * other calls deeper on the call stack
  709. */
  710. break;
  711. }
  712. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  713. correct_ret_addr = ri->ret_addr;
  714. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  715. if (ri->task != current)
  716. /* another task is sharing our hash bucket */
  717. continue;
  718. orig_ret_address = (unsigned long)ri->ret_addr;
  719. if (ri->rp && ri->rp->handler) {
  720. __this_cpu_write(current_kprobe, &ri->rp->kp);
  721. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  722. ri->ret_addr = correct_ret_addr;
  723. ri->rp->handler(ri, regs);
  724. __this_cpu_write(current_kprobe, NULL);
  725. }
  726. recycle_rp_inst(ri, &empty_rp);
  727. if (orig_ret_address != trampoline_address)
  728. /*
  729. * This is the real return address. Any other
  730. * instances associated with this task are for
  731. * other calls deeper on the call stack
  732. */
  733. break;
  734. }
  735. kretprobe_hash_unlock(current, &flags);
  736. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  737. hlist_del(&ri->hlist);
  738. kfree(ri);
  739. }
  740. return (void *)orig_ret_address;
  741. }
  742. NOKPROBE_SYMBOL(trampoline_handler);
  743. /*
  744. * Called after single-stepping. p->addr is the address of the
  745. * instruction whose first byte has been replaced by the "int 3"
  746. * instruction. To avoid the SMP problems that can occur when we
  747. * temporarily put back the original opcode to single-step, we
  748. * single-stepped a copy of the instruction. The address of this
  749. * copy is p->ainsn.insn.
  750. *
  751. * This function prepares to return from the post-single-step
  752. * interrupt. We have to fix up the stack as follows:
  753. *
  754. * 0) Except in the case of absolute or indirect jump or call instructions,
  755. * the new ip is relative to the copied instruction. We need to make
  756. * it relative to the original instruction.
  757. *
  758. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  759. * flags are set in the just-pushed flags, and may need to be cleared.
  760. *
  761. * 2) If the single-stepped instruction was a call, the return address
  762. * that is atop the stack is the address following the copied instruction.
  763. * We need to make it the address following the original instruction.
  764. *
  765. * If this is the first time we've single-stepped the instruction at
  766. * this probepoint, and the instruction is boostable, boost it: add a
  767. * jump instruction after the copied instruction, that jumps to the next
  768. * instruction after the probepoint.
  769. */
  770. static void resume_execution(struct kprobe *p, struct pt_regs *regs,
  771. struct kprobe_ctlblk *kcb)
  772. {
  773. unsigned long *tos = stack_addr(regs);
  774. unsigned long copy_ip = (unsigned long)p->ainsn.insn;
  775. unsigned long orig_ip = (unsigned long)p->addr;
  776. kprobe_opcode_t *insn = p->ainsn.insn;
  777. /* Skip prefixes */
  778. insn = skip_prefixes(insn);
  779. regs->flags &= ~X86_EFLAGS_TF;
  780. switch (*insn) {
  781. case 0x9c: /* pushfl */
  782. *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
  783. *tos |= kcb->kprobe_old_flags;
  784. break;
  785. case 0xc2: /* iret/ret/lret */
  786. case 0xc3:
  787. case 0xca:
  788. case 0xcb:
  789. case 0xcf:
  790. case 0xea: /* jmp absolute -- ip is correct */
  791. /* ip is already adjusted, no more changes required */
  792. p->ainsn.boostable = true;
  793. goto no_change;
  794. case 0xe8: /* call relative - Fix return addr */
  795. *tos = orig_ip + (*tos - copy_ip);
  796. break;
  797. #ifdef CONFIG_X86_32
  798. case 0x9a: /* call absolute -- same as call absolute, indirect */
  799. *tos = orig_ip + (*tos - copy_ip);
  800. goto no_change;
  801. #endif
  802. case 0xff:
  803. if ((insn[1] & 0x30) == 0x10) {
  804. /*
  805. * call absolute, indirect
  806. * Fix return addr; ip is correct.
  807. * But this is not boostable
  808. */
  809. *tos = orig_ip + (*tos - copy_ip);
  810. goto no_change;
  811. } else if (((insn[1] & 0x31) == 0x20) ||
  812. ((insn[1] & 0x31) == 0x21)) {
  813. /*
  814. * jmp near and far, absolute indirect
  815. * ip is correct. And this is boostable
  816. */
  817. p->ainsn.boostable = true;
  818. goto no_change;
  819. }
  820. default:
  821. break;
  822. }
  823. regs->ip += orig_ip - copy_ip;
  824. no_change:
  825. restore_btf();
  826. }
  827. NOKPROBE_SYMBOL(resume_execution);
  828. /*
  829. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  830. * remain disabled throughout this function.
  831. */
  832. int kprobe_debug_handler(struct pt_regs *regs)
  833. {
  834. struct kprobe *cur = kprobe_running();
  835. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  836. if (!cur)
  837. return 0;
  838. resume_execution(cur, regs, kcb);
  839. regs->flags |= kcb->kprobe_saved_flags;
  840. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  841. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  842. cur->post_handler(cur, regs, 0);
  843. }
  844. /* Restore back the original saved kprobes variables and continue. */
  845. if (kcb->kprobe_status == KPROBE_REENTER) {
  846. restore_previous_kprobe(kcb);
  847. goto out;
  848. }
  849. reset_current_kprobe();
  850. out:
  851. /*
  852. * if somebody else is singlestepping across a probe point, flags
  853. * will have TF set, in which case, continue the remaining processing
  854. * of do_debug, as if this is not a probe hit.
  855. */
  856. if (regs->flags & X86_EFLAGS_TF)
  857. return 0;
  858. return 1;
  859. }
  860. NOKPROBE_SYMBOL(kprobe_debug_handler);
  861. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  862. {
  863. struct kprobe *cur = kprobe_running();
  864. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  865. if (unlikely(regs->ip == (unsigned long)cur->ainsn.insn)) {
  866. /* This must happen on single-stepping */
  867. WARN_ON(kcb->kprobe_status != KPROBE_HIT_SS &&
  868. kcb->kprobe_status != KPROBE_REENTER);
  869. /*
  870. * We are here because the instruction being single
  871. * stepped caused a page fault. We reset the current
  872. * kprobe and the ip points back to the probe address
  873. * and allow the page fault handler to continue as a
  874. * normal page fault.
  875. */
  876. regs->ip = (unsigned long)cur->addr;
  877. /*
  878. * Trap flag (TF) has been set here because this fault
  879. * happened where the single stepping will be done.
  880. * So clear it by resetting the current kprobe:
  881. */
  882. regs->flags &= ~X86_EFLAGS_TF;
  883. /*
  884. * If the TF flag was set before the kprobe hit,
  885. * don't touch it:
  886. */
  887. regs->flags |= kcb->kprobe_old_flags;
  888. if (kcb->kprobe_status == KPROBE_REENTER)
  889. restore_previous_kprobe(kcb);
  890. else
  891. reset_current_kprobe();
  892. } else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
  893. kcb->kprobe_status == KPROBE_HIT_SSDONE) {
  894. /*
  895. * We increment the nmissed count for accounting,
  896. * we can also use npre/npostfault count for accounting
  897. * these specific fault cases.
  898. */
  899. kprobes_inc_nmissed_count(cur);
  900. /*
  901. * We come here because instructions in the pre/post
  902. * handler caused the page_fault, this could happen
  903. * if handler tries to access user space by
  904. * copy_from_user(), get_user() etc. Let the
  905. * user-specified handler try to fix it first.
  906. */
  907. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  908. return 1;
  909. }
  910. return 0;
  911. }
  912. NOKPROBE_SYMBOL(kprobe_fault_handler);
  913. bool arch_within_kprobe_blacklist(unsigned long addr)
  914. {
  915. return (addr >= (unsigned long)__kprobes_text_start &&
  916. addr < (unsigned long)__kprobes_text_end) ||
  917. (addr >= (unsigned long)__entry_text_start &&
  918. addr < (unsigned long)__entry_text_end);
  919. }
  920. int __init arch_init_kprobes(void)
  921. {
  922. return 0;
  923. }
  924. int arch_trampoline_kprobe(struct kprobe *p)
  925. {
  926. return 0;
  927. }