core.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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. insn_byte_t prefix;
  160. int i;
  161. if (search_exception_tables((unsigned long)addr))
  162. return 0; /* Page fault may occur on this address. */
  163. /* 2nd-byte opcode */
  164. if (insn->opcode.nbytes == 2)
  165. return test_bit(insn->opcode.bytes[1],
  166. (unsigned long *)twobyte_is_boostable);
  167. if (insn->opcode.nbytes != 1)
  168. return 0;
  169. for_each_insn_prefix(insn, i, prefix) {
  170. insn_attr_t attr;
  171. attr = inat_get_opcode_attribute(prefix);
  172. /* Can't boost Address-size override prefix and CS override prefix */
  173. if (prefix == 0x2e || inat_is_address_size_prefix(attr))
  174. return 0;
  175. }
  176. opcode = insn->opcode.bytes[0];
  177. switch (opcode & 0xf0) {
  178. case 0x60:
  179. /* can't boost "bound" */
  180. return (opcode != 0x62);
  181. case 0x70:
  182. return 0; /* can't boost conditional jump */
  183. case 0x90:
  184. return opcode != 0x9a; /* can't boost call far */
  185. case 0xc0:
  186. /* can't boost software-interruptions */
  187. return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
  188. case 0xd0:
  189. /* can boost AA* and XLAT */
  190. return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
  191. case 0xe0:
  192. /* can boost in/out and absolute jmps */
  193. return ((opcode & 0x04) || opcode == 0xea);
  194. case 0xf0:
  195. /* clear and set flags are boostable */
  196. return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
  197. default:
  198. /* call is not boostable */
  199. return opcode != 0x9a;
  200. }
  201. }
  202. static unsigned long
  203. __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)
  204. {
  205. struct kprobe *kp;
  206. unsigned long faddr;
  207. kp = get_kprobe((void *)addr);
  208. faddr = ftrace_location(addr);
  209. /*
  210. * Addresses inside the ftrace location are refused by
  211. * arch_check_ftrace_location(). Something went terribly wrong
  212. * if such an address is checked here.
  213. */
  214. if (WARN_ON(faddr && faddr != addr))
  215. return 0UL;
  216. /*
  217. * Use the current code if it is not modified by Kprobe
  218. * and it cannot be modified by ftrace.
  219. */
  220. if (!kp && !faddr)
  221. return addr;
  222. /*
  223. * Basically, kp->ainsn.insn has an original instruction.
  224. * However, RIP-relative instruction can not do single-stepping
  225. * at different place, __copy_instruction() tweaks the displacement of
  226. * that instruction. In that case, we can't recover the instruction
  227. * from the kp->ainsn.insn.
  228. *
  229. * On the other hand, in case on normal Kprobe, kp->opcode has a copy
  230. * of the first byte of the probed instruction, which is overwritten
  231. * by int3. And the instruction at kp->addr is not modified by kprobes
  232. * except for the first byte, we can recover the original instruction
  233. * from it and kp->opcode.
  234. *
  235. * In case of Kprobes using ftrace, we do not have a copy of
  236. * the original instruction. In fact, the ftrace location might
  237. * be modified at anytime and even could be in an inconsistent state.
  238. * Fortunately, we know that the original code is the ideal 5-byte
  239. * long NOP.
  240. */
  241. if (probe_kernel_read(buf, (void *)addr,
  242. MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
  243. return 0UL;
  244. if (faddr)
  245. memcpy(buf, ideal_nops[NOP_ATOMIC5], 5);
  246. else
  247. buf[0] = kp->opcode;
  248. return (unsigned long)buf;
  249. }
  250. /*
  251. * Recover the probed instruction at addr for further analysis.
  252. * Caller must lock kprobes by kprobe_mutex, or disable preemption
  253. * for preventing to release referencing kprobes.
  254. * Returns zero if the instruction can not get recovered (or access failed).
  255. */
  256. unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
  257. {
  258. unsigned long __addr;
  259. __addr = __recover_optprobed_insn(buf, addr);
  260. if (__addr != addr)
  261. return __addr;
  262. return __recover_probed_insn(buf, addr);
  263. }
  264. /* Check if paddr is at an instruction boundary */
  265. static int can_probe(unsigned long paddr)
  266. {
  267. unsigned long addr, __addr, offset = 0;
  268. struct insn insn;
  269. kprobe_opcode_t buf[MAX_INSN_SIZE];
  270. if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
  271. return 0;
  272. /* Decode instructions */
  273. addr = paddr - offset;
  274. while (addr < paddr) {
  275. /*
  276. * Check if the instruction has been modified by another
  277. * kprobe, in which case we replace the breakpoint by the
  278. * original instruction in our buffer.
  279. * Also, jump optimization will change the breakpoint to
  280. * relative-jump. Since the relative-jump itself is
  281. * normally used, we just go through if there is no kprobe.
  282. */
  283. __addr = recover_probed_instruction(buf, addr);
  284. if (!__addr)
  285. return 0;
  286. kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE);
  287. insn_get_length(&insn);
  288. /*
  289. * Another debugging subsystem might insert this breakpoint.
  290. * In that case, we can't recover it.
  291. */
  292. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  293. return 0;
  294. addr += insn.length;
  295. }
  296. return (addr == paddr);
  297. }
  298. /*
  299. * Returns non-zero if opcode modifies the interrupt flag.
  300. */
  301. static int is_IF_modifier(kprobe_opcode_t *insn)
  302. {
  303. /* Skip prefixes */
  304. insn = skip_prefixes(insn);
  305. switch (*insn) {
  306. case 0xfa: /* cli */
  307. case 0xfb: /* sti */
  308. case 0xcf: /* iret/iretd */
  309. case 0x9d: /* popf/popfd */
  310. return 1;
  311. }
  312. return 0;
  313. }
  314. /*
  315. * Copy an instruction with recovering modified instruction by kprobes
  316. * and adjust the displacement if the instruction uses the %rip-relative
  317. * addressing mode. Note that since @real will be the final place of copied
  318. * instruction, displacement must be adjust by @real, not @dest.
  319. * This returns the length of copied instruction, or 0 if it has an error.
  320. */
  321. int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn)
  322. {
  323. kprobe_opcode_t buf[MAX_INSN_SIZE];
  324. unsigned long recovered_insn =
  325. recover_probed_instruction(buf, (unsigned long)src);
  326. if (!recovered_insn || !insn)
  327. return 0;
  328. /* This can access kernel text if given address is not recovered */
  329. if (probe_kernel_read(dest, (void *)recovered_insn, MAX_INSN_SIZE))
  330. return 0;
  331. kernel_insn_init(insn, dest, MAX_INSN_SIZE);
  332. insn_get_length(insn);
  333. /* Another subsystem puts a breakpoint, failed to recover */
  334. if (insn->opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  335. return 0;
  336. /* We should not singlestep on the exception masking instructions */
  337. if (insn_masking_exception(insn))
  338. return 0;
  339. #ifdef CONFIG_X86_64
  340. /* Only x86_64 has RIP relative instructions */
  341. if (insn_rip_relative(insn)) {
  342. s64 newdisp;
  343. u8 *disp;
  344. /*
  345. * The copied instruction uses the %rip-relative addressing
  346. * mode. Adjust the displacement for the difference between
  347. * the original location of this instruction and the location
  348. * of the copy that will actually be run. The tricky bit here
  349. * is making sure that the sign extension happens correctly in
  350. * this calculation, since we need a signed 32-bit result to
  351. * be sign-extended to 64 bits when it's added to the %rip
  352. * value and yield the same 64-bit result that the sign-
  353. * extension of the original signed 32-bit displacement would
  354. * have given.
  355. */
  356. newdisp = (u8 *) src + (s64) insn->displacement.value
  357. - (u8 *) real;
  358. if ((s64) (s32) newdisp != newdisp) {
  359. pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp);
  360. return 0;
  361. }
  362. disp = (u8 *) dest + insn_offset_displacement(insn);
  363. *(s32 *) disp = (s32) newdisp;
  364. }
  365. #endif
  366. return insn->length;
  367. }
  368. /* Prepare reljump right after instruction to boost */
  369. static int prepare_boost(kprobe_opcode_t *buf, struct kprobe *p,
  370. struct insn *insn)
  371. {
  372. int len = insn->length;
  373. if (can_boost(insn, p->addr) &&
  374. MAX_INSN_SIZE - len >= RELATIVEJUMP_SIZE) {
  375. /*
  376. * These instructions can be executed directly if it
  377. * jumps back to correct address.
  378. */
  379. synthesize_reljump(buf + len, p->ainsn.insn + len,
  380. p->addr + insn->length);
  381. len += RELATIVEJUMP_SIZE;
  382. p->ainsn.boostable = true;
  383. } else {
  384. p->ainsn.boostable = false;
  385. }
  386. return len;
  387. }
  388. /* Make page to RO mode when allocate it */
  389. void *alloc_insn_page(void)
  390. {
  391. void *page;
  392. page = module_alloc(PAGE_SIZE);
  393. if (!page)
  394. return NULL;
  395. /*
  396. * First make the page read-only, and only then make it executable to
  397. * prevent it from being W+X in between.
  398. */
  399. set_memory_ro((unsigned long)page, 1);
  400. /*
  401. * TODO: Once additional kernel code protection mechanisms are set, ensure
  402. * that the page was not maliciously altered and it is still zeroed.
  403. */
  404. set_memory_x((unsigned long)page, 1);
  405. return page;
  406. }
  407. /* Recover page to RW mode before releasing it */
  408. void free_insn_page(void *page)
  409. {
  410. /*
  411. * First make the page non-executable, and only then make it writable to
  412. * prevent it from being W+X in between.
  413. */
  414. set_memory_nx((unsigned long)page, 1);
  415. set_memory_rw((unsigned long)page, 1);
  416. module_memfree(page);
  417. }
  418. static int arch_copy_kprobe(struct kprobe *p)
  419. {
  420. struct insn insn;
  421. kprobe_opcode_t buf[MAX_INSN_SIZE];
  422. int len;
  423. /* Copy an instruction with recovering if other optprobe modifies it.*/
  424. len = __copy_instruction(buf, p->addr, p->ainsn.insn, &insn);
  425. if (!len)
  426. return -EINVAL;
  427. /*
  428. * __copy_instruction can modify the displacement of the instruction,
  429. * but it doesn't affect boostable check.
  430. */
  431. len = prepare_boost(buf, p, &insn);
  432. /* Check whether the instruction modifies Interrupt Flag or not */
  433. p->ainsn.if_modifier = is_IF_modifier(buf);
  434. /* Also, displacement change doesn't affect the first byte */
  435. p->opcode = buf[0];
  436. /* OK, write back the instruction(s) into ROX insn buffer */
  437. text_poke(p->ainsn.insn, buf, len);
  438. return 0;
  439. }
  440. int arch_prepare_kprobe(struct kprobe *p)
  441. {
  442. int ret;
  443. if (alternatives_text_reserved(p->addr, p->addr))
  444. return -EINVAL;
  445. if (!can_probe((unsigned long)p->addr))
  446. return -EILSEQ;
  447. /* insn: must be on special executable page on x86. */
  448. p->ainsn.insn = get_insn_slot();
  449. if (!p->ainsn.insn)
  450. return -ENOMEM;
  451. ret = arch_copy_kprobe(p);
  452. if (ret) {
  453. free_insn_slot(p->ainsn.insn, 0);
  454. p->ainsn.insn = NULL;
  455. }
  456. return ret;
  457. }
  458. void arch_arm_kprobe(struct kprobe *p)
  459. {
  460. text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
  461. }
  462. void arch_disarm_kprobe(struct kprobe *p)
  463. {
  464. text_poke(p->addr, &p->opcode, 1);
  465. }
  466. void arch_remove_kprobe(struct kprobe *p)
  467. {
  468. if (p->ainsn.insn) {
  469. free_insn_slot(p->ainsn.insn, p->ainsn.boostable);
  470. p->ainsn.insn = NULL;
  471. }
  472. }
  473. static nokprobe_inline void
  474. save_previous_kprobe(struct kprobe_ctlblk *kcb)
  475. {
  476. kcb->prev_kprobe.kp = kprobe_running();
  477. kcb->prev_kprobe.status = kcb->kprobe_status;
  478. kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
  479. kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
  480. }
  481. static nokprobe_inline void
  482. restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  483. {
  484. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  485. kcb->kprobe_status = kcb->prev_kprobe.status;
  486. kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
  487. kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
  488. }
  489. static nokprobe_inline void
  490. set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  491. struct kprobe_ctlblk *kcb)
  492. {
  493. __this_cpu_write(current_kprobe, p);
  494. kcb->kprobe_saved_flags = kcb->kprobe_old_flags
  495. = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
  496. if (p->ainsn.if_modifier)
  497. kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
  498. }
  499. static nokprobe_inline void clear_btf(void)
  500. {
  501. if (test_thread_flag(TIF_BLOCKSTEP)) {
  502. unsigned long debugctl = get_debugctlmsr();
  503. debugctl &= ~DEBUGCTLMSR_BTF;
  504. update_debugctlmsr(debugctl);
  505. }
  506. }
  507. static nokprobe_inline void restore_btf(void)
  508. {
  509. if (test_thread_flag(TIF_BLOCKSTEP)) {
  510. unsigned long debugctl = get_debugctlmsr();
  511. debugctl |= DEBUGCTLMSR_BTF;
  512. update_debugctlmsr(debugctl);
  513. }
  514. }
  515. void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
  516. {
  517. unsigned long *sara = stack_addr(regs);
  518. ri->ret_addr = (kprobe_opcode_t *) *sara;
  519. ri->fp = sara;
  520. /* Replace the return addr with trampoline addr */
  521. *sara = (unsigned long) &kretprobe_trampoline;
  522. }
  523. NOKPROBE_SYMBOL(arch_prepare_kretprobe);
  524. static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
  525. struct kprobe_ctlblk *kcb, int reenter)
  526. {
  527. if (setup_detour_execution(p, regs, reenter))
  528. return;
  529. #if !defined(CONFIG_PREEMPT)
  530. if (p->ainsn.boostable && !p->post_handler) {
  531. /* Boost up -- we can execute copied instructions directly */
  532. if (!reenter)
  533. reset_current_kprobe();
  534. /*
  535. * Reentering boosted probe doesn't reset current_kprobe,
  536. * nor set current_kprobe, because it doesn't use single
  537. * stepping.
  538. */
  539. regs->ip = (unsigned long)p->ainsn.insn;
  540. return;
  541. }
  542. #endif
  543. if (reenter) {
  544. save_previous_kprobe(kcb);
  545. set_current_kprobe(p, regs, kcb);
  546. kcb->kprobe_status = KPROBE_REENTER;
  547. } else
  548. kcb->kprobe_status = KPROBE_HIT_SS;
  549. /* Prepare real single stepping */
  550. clear_btf();
  551. regs->flags |= X86_EFLAGS_TF;
  552. regs->flags &= ~X86_EFLAGS_IF;
  553. /* single step inline if the instruction is an int3 */
  554. if (p->opcode == BREAKPOINT_INSTRUCTION)
  555. regs->ip = (unsigned long)p->addr;
  556. else
  557. regs->ip = (unsigned long)p->ainsn.insn;
  558. }
  559. NOKPROBE_SYMBOL(setup_singlestep);
  560. /*
  561. * We have reentered the kprobe_handler(), since another probe was hit while
  562. * within the handler. We save the original kprobes variables and just single
  563. * step on the instruction of the new probe without calling any user handlers.
  564. */
  565. static int reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
  566. struct kprobe_ctlblk *kcb)
  567. {
  568. switch (kcb->kprobe_status) {
  569. case KPROBE_HIT_SSDONE:
  570. case KPROBE_HIT_ACTIVE:
  571. case KPROBE_HIT_SS:
  572. kprobes_inc_nmissed_count(p);
  573. setup_singlestep(p, regs, kcb, 1);
  574. break;
  575. case KPROBE_REENTER:
  576. /* A probe has been hit in the codepath leading up to, or just
  577. * after, single-stepping of a probed instruction. This entire
  578. * codepath should strictly reside in .kprobes.text section.
  579. * Raise a BUG or we'll continue in an endless reentering loop
  580. * and eventually a stack overflow.
  581. */
  582. pr_err("Unrecoverable kprobe detected.\n");
  583. dump_kprobe(p);
  584. BUG();
  585. default:
  586. /* impossible cases */
  587. WARN_ON(1);
  588. return 0;
  589. }
  590. return 1;
  591. }
  592. NOKPROBE_SYMBOL(reenter_kprobe);
  593. /*
  594. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  595. * remain disabled throughout this function.
  596. */
  597. int kprobe_int3_handler(struct pt_regs *regs)
  598. {
  599. kprobe_opcode_t *addr;
  600. struct kprobe *p;
  601. struct kprobe_ctlblk *kcb;
  602. if (user_mode(regs))
  603. return 0;
  604. addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
  605. /*
  606. * We don't want to be preempted for the entire duration of kprobe
  607. * processing. Since int3 and debug trap disables irqs and we clear
  608. * IF while singlestepping, it must be no preemptible.
  609. */
  610. kcb = get_kprobe_ctlblk();
  611. p = get_kprobe(addr);
  612. if (p) {
  613. if (kprobe_running()) {
  614. if (reenter_kprobe(p, regs, kcb))
  615. return 1;
  616. } else {
  617. set_current_kprobe(p, regs, kcb);
  618. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  619. /*
  620. * If we have no pre-handler or it returned 0, we
  621. * continue with normal processing. If we have a
  622. * pre-handler and it returned non-zero, that means
  623. * user handler setup registers to exit to another
  624. * instruction, we must skip the single stepping.
  625. */
  626. if (!p->pre_handler || !p->pre_handler(p, regs))
  627. setup_singlestep(p, regs, kcb, 0);
  628. else
  629. reset_current_kprobe();
  630. return 1;
  631. }
  632. } else if (*addr != BREAKPOINT_INSTRUCTION) {
  633. /*
  634. * The breakpoint instruction was removed right
  635. * after we hit it. Another cpu has removed
  636. * either a probepoint or a debugger breakpoint
  637. * at this address. In either case, no further
  638. * handling of this interrupt is appropriate.
  639. * Back up over the (now missing) int3 and run
  640. * the original instruction.
  641. */
  642. regs->ip = (unsigned long)addr;
  643. return 1;
  644. } /* else: not a kprobe fault; let the kernel handle it */
  645. return 0;
  646. }
  647. NOKPROBE_SYMBOL(kprobe_int3_handler);
  648. /*
  649. * When a retprobed function returns, this code saves registers and
  650. * calls trampoline_handler() runs, which calls the kretprobe's handler.
  651. */
  652. asm(
  653. ".global kretprobe_trampoline\n"
  654. ".type kretprobe_trampoline, @function\n"
  655. "kretprobe_trampoline:\n"
  656. #ifdef CONFIG_X86_64
  657. /* We don't bother saving the ss register */
  658. " pushq %rsp\n"
  659. " pushfq\n"
  660. SAVE_REGS_STRING
  661. " movq %rsp, %rdi\n"
  662. " call trampoline_handler\n"
  663. /* Replace saved sp with true return address. */
  664. " movq %rax, 152(%rsp)\n"
  665. RESTORE_REGS_STRING
  666. " popfq\n"
  667. #else
  668. " pushf\n"
  669. SAVE_REGS_STRING
  670. " movl %esp, %eax\n"
  671. " call trampoline_handler\n"
  672. /* Move flags to cs */
  673. " movl 56(%esp), %edx\n"
  674. " movl %edx, 52(%esp)\n"
  675. /* Replace saved flags with true return address. */
  676. " movl %eax, 56(%esp)\n"
  677. RESTORE_REGS_STRING
  678. " popf\n"
  679. #endif
  680. " ret\n"
  681. ".size kretprobe_trampoline, .-kretprobe_trampoline\n"
  682. );
  683. NOKPROBE_SYMBOL(kretprobe_trampoline);
  684. STACK_FRAME_NON_STANDARD(kretprobe_trampoline);
  685. /*
  686. * Called from kretprobe_trampoline
  687. */
  688. __visible __used void *trampoline_handler(struct pt_regs *regs)
  689. {
  690. struct kretprobe_instance *ri = NULL;
  691. struct hlist_head *head, empty_rp;
  692. struct hlist_node *tmp;
  693. unsigned long flags, orig_ret_address = 0;
  694. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  695. kprobe_opcode_t *correct_ret_addr = NULL;
  696. void *frame_pointer;
  697. bool skipped = false;
  698. /*
  699. * Set a dummy kprobe for avoiding kretprobe recursion.
  700. * Since kretprobe never run in kprobe handler, kprobe must not
  701. * be running at this point.
  702. */
  703. kprobe_busy_begin();
  704. INIT_HLIST_HEAD(&empty_rp);
  705. kretprobe_hash_lock(current, &head, &flags);
  706. /* fixup registers */
  707. #ifdef CONFIG_X86_64
  708. regs->cs = __KERNEL_CS;
  709. /* On x86-64, we use pt_regs->sp for return address holder. */
  710. frame_pointer = &regs->sp;
  711. #else
  712. regs->cs = __KERNEL_CS | get_kernel_rpl();
  713. regs->gs = 0;
  714. /* On x86-32, we use pt_regs->flags for return address holder. */
  715. frame_pointer = &regs->flags;
  716. #endif
  717. regs->ip = trampoline_address;
  718. regs->orig_ax = ~0UL;
  719. /*
  720. * It is possible to have multiple instances associated with a given
  721. * task either because multiple functions in the call path have
  722. * return probes installed on them, and/or more than one
  723. * return probe was registered for a target function.
  724. *
  725. * We can handle this because:
  726. * - instances are always pushed into the head of the list
  727. * - when multiple return probes are registered for the same
  728. * function, the (chronologically) first instance's ret_addr
  729. * will be the real return address, and all the rest will
  730. * point to kretprobe_trampoline.
  731. */
  732. hlist_for_each_entry(ri, head, hlist) {
  733. if (ri->task != current)
  734. /* another task is sharing our hash bucket */
  735. continue;
  736. /*
  737. * Return probes must be pushed on this hash list correct
  738. * order (same as return order) so that it can be poped
  739. * correctly. However, if we find it is pushed it incorrect
  740. * order, this means we find a function which should not be
  741. * probed, because the wrong order entry is pushed on the
  742. * path of processing other kretprobe itself.
  743. */
  744. if (ri->fp != frame_pointer) {
  745. if (!skipped)
  746. pr_warn("kretprobe is stacked incorrectly. Trying to fixup.\n");
  747. skipped = true;
  748. continue;
  749. }
  750. orig_ret_address = (unsigned long)ri->ret_addr;
  751. if (skipped)
  752. pr_warn("%ps must be blacklisted because of incorrect kretprobe order\n",
  753. ri->rp->kp.addr);
  754. if (orig_ret_address != trampoline_address)
  755. /*
  756. * This is the real return address. Any other
  757. * instances associated with this task are for
  758. * other calls deeper on the call stack
  759. */
  760. break;
  761. }
  762. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  763. correct_ret_addr = ri->ret_addr;
  764. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  765. if (ri->task != current)
  766. /* another task is sharing our hash bucket */
  767. continue;
  768. if (ri->fp != frame_pointer)
  769. continue;
  770. orig_ret_address = (unsigned long)ri->ret_addr;
  771. if (ri->rp && ri->rp->handler) {
  772. __this_cpu_write(current_kprobe, &ri->rp->kp);
  773. ri->ret_addr = correct_ret_addr;
  774. ri->rp->handler(ri, regs);
  775. __this_cpu_write(current_kprobe, &kprobe_busy);
  776. }
  777. recycle_rp_inst(ri, &empty_rp);
  778. if (orig_ret_address != trampoline_address)
  779. /*
  780. * This is the real return address. Any other
  781. * instances associated with this task are for
  782. * other calls deeper on the call stack
  783. */
  784. break;
  785. }
  786. kretprobe_hash_unlock(current, &flags);
  787. kprobe_busy_end();
  788. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  789. hlist_del(&ri->hlist);
  790. kfree(ri);
  791. }
  792. return (void *)orig_ret_address;
  793. }
  794. NOKPROBE_SYMBOL(trampoline_handler);
  795. /*
  796. * Called after single-stepping. p->addr is the address of the
  797. * instruction whose first byte has been replaced by the "int 3"
  798. * instruction. To avoid the SMP problems that can occur when we
  799. * temporarily put back the original opcode to single-step, we
  800. * single-stepped a copy of the instruction. The address of this
  801. * copy is p->ainsn.insn.
  802. *
  803. * This function prepares to return from the post-single-step
  804. * interrupt. We have to fix up the stack as follows:
  805. *
  806. * 0) Except in the case of absolute or indirect jump or call instructions,
  807. * the new ip is relative to the copied instruction. We need to make
  808. * it relative to the original instruction.
  809. *
  810. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  811. * flags are set in the just-pushed flags, and may need to be cleared.
  812. *
  813. * 2) If the single-stepped instruction was a call, the return address
  814. * that is atop the stack is the address following the copied instruction.
  815. * We need to make it the address following the original instruction.
  816. *
  817. * If this is the first time we've single-stepped the instruction at
  818. * this probepoint, and the instruction is boostable, boost it: add a
  819. * jump instruction after the copied instruction, that jumps to the next
  820. * instruction after the probepoint.
  821. */
  822. static void resume_execution(struct kprobe *p, struct pt_regs *regs,
  823. struct kprobe_ctlblk *kcb)
  824. {
  825. unsigned long *tos = stack_addr(regs);
  826. unsigned long copy_ip = (unsigned long)p->ainsn.insn;
  827. unsigned long orig_ip = (unsigned long)p->addr;
  828. kprobe_opcode_t *insn = p->ainsn.insn;
  829. /* Skip prefixes */
  830. insn = skip_prefixes(insn);
  831. regs->flags &= ~X86_EFLAGS_TF;
  832. switch (*insn) {
  833. case 0x9c: /* pushfl */
  834. *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
  835. *tos |= kcb->kprobe_old_flags;
  836. break;
  837. case 0xc2: /* iret/ret/lret */
  838. case 0xc3:
  839. case 0xca:
  840. case 0xcb:
  841. case 0xcf:
  842. case 0xea: /* jmp absolute -- ip is correct */
  843. /* ip is already adjusted, no more changes required */
  844. p->ainsn.boostable = true;
  845. goto no_change;
  846. case 0xe8: /* call relative - Fix return addr */
  847. *tos = orig_ip + (*tos - copy_ip);
  848. break;
  849. #ifdef CONFIG_X86_32
  850. case 0x9a: /* call absolute -- same as call absolute, indirect */
  851. *tos = orig_ip + (*tos - copy_ip);
  852. goto no_change;
  853. #endif
  854. case 0xff:
  855. if ((insn[1] & 0x30) == 0x10) {
  856. /*
  857. * call absolute, indirect
  858. * Fix return addr; ip is correct.
  859. * But this is not boostable
  860. */
  861. *tos = orig_ip + (*tos - copy_ip);
  862. goto no_change;
  863. } else if (((insn[1] & 0x31) == 0x20) ||
  864. ((insn[1] & 0x31) == 0x21)) {
  865. /*
  866. * jmp near and far, absolute indirect
  867. * ip is correct. And this is boostable
  868. */
  869. p->ainsn.boostable = true;
  870. goto no_change;
  871. }
  872. default:
  873. break;
  874. }
  875. regs->ip += orig_ip - copy_ip;
  876. no_change:
  877. restore_btf();
  878. }
  879. NOKPROBE_SYMBOL(resume_execution);
  880. /*
  881. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  882. * remain disabled throughout this function.
  883. */
  884. int kprobe_debug_handler(struct pt_regs *regs)
  885. {
  886. struct kprobe *cur = kprobe_running();
  887. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  888. if (!cur)
  889. return 0;
  890. resume_execution(cur, regs, kcb);
  891. regs->flags |= kcb->kprobe_saved_flags;
  892. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  893. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  894. cur->post_handler(cur, regs, 0);
  895. }
  896. /* Restore back the original saved kprobes variables and continue. */
  897. if (kcb->kprobe_status == KPROBE_REENTER) {
  898. restore_previous_kprobe(kcb);
  899. goto out;
  900. }
  901. reset_current_kprobe();
  902. out:
  903. /*
  904. * if somebody else is singlestepping across a probe point, flags
  905. * will have TF set, in which case, continue the remaining processing
  906. * of do_debug, as if this is not a probe hit.
  907. */
  908. if (regs->flags & X86_EFLAGS_TF)
  909. return 0;
  910. return 1;
  911. }
  912. NOKPROBE_SYMBOL(kprobe_debug_handler);
  913. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  914. {
  915. struct kprobe *cur = kprobe_running();
  916. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  917. if (unlikely(regs->ip == (unsigned long)cur->ainsn.insn)) {
  918. /* This must happen on single-stepping */
  919. WARN_ON(kcb->kprobe_status != KPROBE_HIT_SS &&
  920. kcb->kprobe_status != KPROBE_REENTER);
  921. /*
  922. * We are here because the instruction being single
  923. * stepped caused a page fault. We reset the current
  924. * kprobe and the ip points back to the probe address
  925. * and allow the page fault handler to continue as a
  926. * normal page fault.
  927. */
  928. regs->ip = (unsigned long)cur->addr;
  929. /*
  930. * Trap flag (TF) has been set here because this fault
  931. * happened where the single stepping will be done.
  932. * So clear it by resetting the current kprobe:
  933. */
  934. regs->flags &= ~X86_EFLAGS_TF;
  935. /*
  936. * Since the single step (trap) has been cancelled,
  937. * we need to restore BTF here.
  938. */
  939. restore_btf();
  940. /*
  941. * If the TF flag was set before the kprobe hit,
  942. * don't touch it:
  943. */
  944. regs->flags |= kcb->kprobe_old_flags;
  945. if (kcb->kprobe_status == KPROBE_REENTER)
  946. restore_previous_kprobe(kcb);
  947. else
  948. reset_current_kprobe();
  949. } else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
  950. kcb->kprobe_status == KPROBE_HIT_SSDONE) {
  951. /*
  952. * We increment the nmissed count for accounting,
  953. * we can also use npre/npostfault count for accounting
  954. * these specific fault cases.
  955. */
  956. kprobes_inc_nmissed_count(cur);
  957. /*
  958. * We come here because instructions in the pre/post
  959. * handler caused the page_fault, this could happen
  960. * if handler tries to access user space by
  961. * copy_from_user(), get_user() etc. Let the
  962. * user-specified handler try to fix it first.
  963. */
  964. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  965. return 1;
  966. /*
  967. * In case the user-specified fault handler returned
  968. * zero, try to fix up.
  969. */
  970. if (fixup_exception(regs, trapnr))
  971. return 1;
  972. /*
  973. * fixup routine could not handle it,
  974. * Let do_page_fault() fix it.
  975. */
  976. }
  977. return 0;
  978. }
  979. NOKPROBE_SYMBOL(kprobe_fault_handler);
  980. /*
  981. * Wrapper routine for handling exceptions.
  982. */
  983. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  984. void *data)
  985. {
  986. struct die_args *args = data;
  987. int ret = NOTIFY_DONE;
  988. if (args->regs && user_mode(args->regs))
  989. return ret;
  990. if (val == DIE_GPF) {
  991. /*
  992. * To be potentially processing a kprobe fault and to
  993. * trust the result from kprobe_running(), we have
  994. * be non-preemptible.
  995. */
  996. if (!preemptible() && kprobe_running() &&
  997. kprobe_fault_handler(args->regs, args->trapnr))
  998. ret = NOTIFY_STOP;
  999. }
  1000. return ret;
  1001. }
  1002. NOKPROBE_SYMBOL(kprobe_exceptions_notify);
  1003. bool arch_within_kprobe_blacklist(unsigned long addr)
  1004. {
  1005. bool is_in_entry_trampoline_section = false;
  1006. #ifdef CONFIG_X86_64
  1007. is_in_entry_trampoline_section =
  1008. (addr >= (unsigned long)__entry_trampoline_start &&
  1009. addr < (unsigned long)__entry_trampoline_end);
  1010. #endif
  1011. return (addr >= (unsigned long)__kprobes_text_start &&
  1012. addr < (unsigned long)__kprobes_text_end) ||
  1013. (addr >= (unsigned long)__entry_text_start &&
  1014. addr < (unsigned long)__entry_text_end) ||
  1015. is_in_entry_trampoline_section;
  1016. }
  1017. int __init arch_populate_kprobe_blacklist(void)
  1018. {
  1019. return kprobe_add_area_blacklist((unsigned long)__entry_text_start,
  1020. (unsigned long)__entry_text_end);
  1021. }
  1022. int __init arch_init_kprobes(void)
  1023. {
  1024. return 0;
  1025. }
  1026. int arch_trampoline_kprobe(struct kprobe *p)
  1027. {
  1028. return 0;
  1029. }