core.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. pr_err("\tSrc: %p, Dest: %p, old disp: %x\n",
  355. src, real, insn->displacement.value);
  356. return 0;
  357. }
  358. disp = (u8 *) dest + insn_offset_displacement(insn);
  359. *(s32 *) disp = (s32) newdisp;
  360. }
  361. #endif
  362. return insn->length;
  363. }
  364. /* Prepare reljump right after instruction to boost */
  365. static int prepare_boost(kprobe_opcode_t *buf, struct kprobe *p,
  366. struct insn *insn)
  367. {
  368. int len = insn->length;
  369. if (can_boost(insn, p->addr) &&
  370. MAX_INSN_SIZE - len >= RELATIVEJUMP_SIZE) {
  371. /*
  372. * These instructions can be executed directly if it
  373. * jumps back to correct address.
  374. */
  375. synthesize_reljump(buf + len, p->ainsn.insn + len,
  376. p->addr + insn->length);
  377. len += RELATIVEJUMP_SIZE;
  378. p->ainsn.boostable = true;
  379. } else {
  380. p->ainsn.boostable = false;
  381. }
  382. return len;
  383. }
  384. /* Make page to RO mode when allocate it */
  385. void *alloc_insn_page(void)
  386. {
  387. void *page;
  388. page = module_alloc(PAGE_SIZE);
  389. if (page)
  390. set_memory_ro((unsigned long)page & PAGE_MASK, 1);
  391. return page;
  392. }
  393. /* Recover page to RW mode before releasing it */
  394. void free_insn_page(void *page)
  395. {
  396. set_memory_nx((unsigned long)page & PAGE_MASK, 1);
  397. set_memory_rw((unsigned long)page & PAGE_MASK, 1);
  398. module_memfree(page);
  399. }
  400. static int arch_copy_kprobe(struct kprobe *p)
  401. {
  402. struct insn insn;
  403. kprobe_opcode_t buf[MAX_INSN_SIZE];
  404. int len;
  405. /* Copy an instruction with recovering if other optprobe modifies it.*/
  406. len = __copy_instruction(buf, p->addr, p->ainsn.insn, &insn);
  407. if (!len)
  408. return -EINVAL;
  409. /*
  410. * __copy_instruction can modify the displacement of the instruction,
  411. * but it doesn't affect boostable check.
  412. */
  413. len = prepare_boost(buf, p, &insn);
  414. /* Check whether the instruction modifies Interrupt Flag or not */
  415. p->ainsn.if_modifier = is_IF_modifier(buf);
  416. /* Also, displacement change doesn't affect the first byte */
  417. p->opcode = buf[0];
  418. /* OK, write back the instruction(s) into ROX insn buffer */
  419. text_poke(p->ainsn.insn, buf, len);
  420. return 0;
  421. }
  422. int arch_prepare_kprobe(struct kprobe *p)
  423. {
  424. int ret;
  425. if (alternatives_text_reserved(p->addr, p->addr))
  426. return -EINVAL;
  427. if (!can_probe((unsigned long)p->addr))
  428. return -EILSEQ;
  429. /* insn: must be on special executable page on x86. */
  430. p->ainsn.insn = get_insn_slot();
  431. if (!p->ainsn.insn)
  432. return -ENOMEM;
  433. ret = arch_copy_kprobe(p);
  434. if (ret) {
  435. free_insn_slot(p->ainsn.insn, 0);
  436. p->ainsn.insn = NULL;
  437. }
  438. return ret;
  439. }
  440. void arch_arm_kprobe(struct kprobe *p)
  441. {
  442. text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
  443. }
  444. void arch_disarm_kprobe(struct kprobe *p)
  445. {
  446. text_poke(p->addr, &p->opcode, 1);
  447. }
  448. void arch_remove_kprobe(struct kprobe *p)
  449. {
  450. if (p->ainsn.insn) {
  451. free_insn_slot(p->ainsn.insn, p->ainsn.boostable);
  452. p->ainsn.insn = NULL;
  453. }
  454. }
  455. static nokprobe_inline void
  456. save_previous_kprobe(struct kprobe_ctlblk *kcb)
  457. {
  458. kcb->prev_kprobe.kp = kprobe_running();
  459. kcb->prev_kprobe.status = kcb->kprobe_status;
  460. kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
  461. kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
  462. }
  463. static nokprobe_inline void
  464. restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  465. {
  466. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  467. kcb->kprobe_status = kcb->prev_kprobe.status;
  468. kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
  469. kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
  470. }
  471. static nokprobe_inline void
  472. set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  473. struct kprobe_ctlblk *kcb)
  474. {
  475. __this_cpu_write(current_kprobe, p);
  476. kcb->kprobe_saved_flags = kcb->kprobe_old_flags
  477. = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
  478. if (p->ainsn.if_modifier)
  479. kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
  480. }
  481. static nokprobe_inline void clear_btf(void)
  482. {
  483. if (test_thread_flag(TIF_BLOCKSTEP)) {
  484. unsigned long debugctl = get_debugctlmsr();
  485. debugctl &= ~DEBUGCTLMSR_BTF;
  486. update_debugctlmsr(debugctl);
  487. }
  488. }
  489. static nokprobe_inline void restore_btf(void)
  490. {
  491. if (test_thread_flag(TIF_BLOCKSTEP)) {
  492. unsigned long debugctl = get_debugctlmsr();
  493. debugctl |= DEBUGCTLMSR_BTF;
  494. update_debugctlmsr(debugctl);
  495. }
  496. }
  497. void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
  498. {
  499. unsigned long *sara = stack_addr(regs);
  500. ri->ret_addr = (kprobe_opcode_t *) *sara;
  501. /* Replace the return addr with trampoline addr */
  502. *sara = (unsigned long) &kretprobe_trampoline;
  503. }
  504. NOKPROBE_SYMBOL(arch_prepare_kretprobe);
  505. static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
  506. struct kprobe_ctlblk *kcb, int reenter)
  507. {
  508. if (setup_detour_execution(p, regs, reenter))
  509. return;
  510. #if !defined(CONFIG_PREEMPT)
  511. if (p->ainsn.boostable && !p->post_handler) {
  512. /* Boost up -- we can execute copied instructions directly */
  513. if (!reenter)
  514. reset_current_kprobe();
  515. /*
  516. * Reentering boosted probe doesn't reset current_kprobe,
  517. * nor set current_kprobe, because it doesn't use single
  518. * stepping.
  519. */
  520. regs->ip = (unsigned long)p->ainsn.insn;
  521. preempt_enable_no_resched();
  522. return;
  523. }
  524. #endif
  525. if (reenter) {
  526. save_previous_kprobe(kcb);
  527. set_current_kprobe(p, regs, kcb);
  528. kcb->kprobe_status = KPROBE_REENTER;
  529. } else
  530. kcb->kprobe_status = KPROBE_HIT_SS;
  531. /* Prepare real single stepping */
  532. clear_btf();
  533. regs->flags |= X86_EFLAGS_TF;
  534. regs->flags &= ~X86_EFLAGS_IF;
  535. /* single step inline if the instruction is an int3 */
  536. if (p->opcode == BREAKPOINT_INSTRUCTION)
  537. regs->ip = (unsigned long)p->addr;
  538. else
  539. regs->ip = (unsigned long)p->ainsn.insn;
  540. }
  541. NOKPROBE_SYMBOL(setup_singlestep);
  542. /*
  543. * We have reentered the kprobe_handler(), since another probe was hit while
  544. * within the handler. We save the original kprobes variables and just single
  545. * step on the instruction of the new probe without calling any user handlers.
  546. */
  547. static int reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
  548. struct kprobe_ctlblk *kcb)
  549. {
  550. switch (kcb->kprobe_status) {
  551. case KPROBE_HIT_SSDONE:
  552. case KPROBE_HIT_ACTIVE:
  553. case KPROBE_HIT_SS:
  554. kprobes_inc_nmissed_count(p);
  555. setup_singlestep(p, regs, kcb, 1);
  556. break;
  557. case KPROBE_REENTER:
  558. /* A probe has been hit in the codepath leading up to, or just
  559. * after, single-stepping of a probed instruction. This entire
  560. * codepath should strictly reside in .kprobes.text section.
  561. * Raise a BUG or we'll continue in an endless reentering loop
  562. * and eventually a stack overflow.
  563. */
  564. printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
  565. p->addr);
  566. dump_kprobe(p);
  567. BUG();
  568. default:
  569. /* impossible cases */
  570. WARN_ON(1);
  571. return 0;
  572. }
  573. return 1;
  574. }
  575. NOKPROBE_SYMBOL(reenter_kprobe);
  576. /*
  577. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  578. * remain disabled throughout this function.
  579. */
  580. int kprobe_int3_handler(struct pt_regs *regs)
  581. {
  582. kprobe_opcode_t *addr;
  583. struct kprobe *p;
  584. struct kprobe_ctlblk *kcb;
  585. if (user_mode(regs))
  586. return 0;
  587. addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
  588. /*
  589. * We don't want to be preempted for the entire
  590. * duration of kprobe processing. We conditionally
  591. * re-enable preemption at the end of this function,
  592. * and also in reenter_kprobe() and setup_singlestep().
  593. */
  594. preempt_disable();
  595. kcb = get_kprobe_ctlblk();
  596. p = get_kprobe(addr);
  597. if (p) {
  598. if (kprobe_running()) {
  599. if (reenter_kprobe(p, regs, kcb))
  600. return 1;
  601. } else {
  602. set_current_kprobe(p, regs, kcb);
  603. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  604. /*
  605. * If we have no pre-handler or it returned 0, we
  606. * continue with normal processing. If we have a
  607. * pre-handler and it returned non-zero, that means
  608. * user handler setup registers to exit to another
  609. * instruction, we must skip the single stepping.
  610. */
  611. if (!p->pre_handler || !p->pre_handler(p, regs))
  612. setup_singlestep(p, regs, kcb, 0);
  613. return 1;
  614. }
  615. } else if (*addr != BREAKPOINT_INSTRUCTION) {
  616. /*
  617. * The breakpoint instruction was removed right
  618. * after we hit it. Another cpu has removed
  619. * either a probepoint or a debugger breakpoint
  620. * at this address. In either case, no further
  621. * handling of this interrupt is appropriate.
  622. * Back up over the (now missing) int3 and run
  623. * the original instruction.
  624. */
  625. regs->ip = (unsigned long)addr;
  626. preempt_enable_no_resched();
  627. return 1;
  628. } /* else: not a kprobe fault; let the kernel handle it */
  629. preempt_enable_no_resched();
  630. return 0;
  631. }
  632. NOKPROBE_SYMBOL(kprobe_int3_handler);
  633. /*
  634. * When a retprobed function returns, this code saves registers and
  635. * calls trampoline_handler() runs, which calls the kretprobe's handler.
  636. */
  637. asm(
  638. ".global kretprobe_trampoline\n"
  639. ".type kretprobe_trampoline, @function\n"
  640. "kretprobe_trampoline:\n"
  641. #ifdef CONFIG_X86_64
  642. /* We don't bother saving the ss register */
  643. " pushq %rsp\n"
  644. " pushfq\n"
  645. SAVE_REGS_STRING
  646. " movq %rsp, %rdi\n"
  647. " call trampoline_handler\n"
  648. /* Replace saved sp with true return address. */
  649. " movq %rax, 152(%rsp)\n"
  650. RESTORE_REGS_STRING
  651. " popfq\n"
  652. #else
  653. " pushf\n"
  654. SAVE_REGS_STRING
  655. " movl %esp, %eax\n"
  656. " call trampoline_handler\n"
  657. /* Move flags to cs */
  658. " movl 56(%esp), %edx\n"
  659. " movl %edx, 52(%esp)\n"
  660. /* Replace saved flags with true return address. */
  661. " movl %eax, 56(%esp)\n"
  662. RESTORE_REGS_STRING
  663. " popf\n"
  664. #endif
  665. " ret\n"
  666. ".size kretprobe_trampoline, .-kretprobe_trampoline\n"
  667. );
  668. NOKPROBE_SYMBOL(kretprobe_trampoline);
  669. STACK_FRAME_NON_STANDARD(kretprobe_trampoline);
  670. /*
  671. * Called from kretprobe_trampoline
  672. */
  673. __visible __used void *trampoline_handler(struct pt_regs *regs)
  674. {
  675. struct kretprobe_instance *ri = NULL;
  676. struct hlist_head *head, empty_rp;
  677. struct hlist_node *tmp;
  678. unsigned long flags, orig_ret_address = 0;
  679. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  680. kprobe_opcode_t *correct_ret_addr = NULL;
  681. INIT_HLIST_HEAD(&empty_rp);
  682. kretprobe_hash_lock(current, &head, &flags);
  683. /* fixup registers */
  684. #ifdef CONFIG_X86_64
  685. regs->cs = __KERNEL_CS;
  686. #else
  687. regs->cs = __KERNEL_CS | get_kernel_rpl();
  688. regs->gs = 0;
  689. #endif
  690. regs->ip = trampoline_address;
  691. regs->orig_ax = ~0UL;
  692. /*
  693. * It is possible to have multiple instances associated with a given
  694. * task either because multiple functions in the call path have
  695. * return probes installed on them, and/or more than one
  696. * return probe was registered for a target function.
  697. *
  698. * We can handle this because:
  699. * - instances are always pushed into the head of the list
  700. * - when multiple return probes are registered for the same
  701. * function, the (chronologically) first instance's ret_addr
  702. * will be the real return address, and all the rest will
  703. * point to kretprobe_trampoline.
  704. */
  705. hlist_for_each_entry(ri, head, hlist) {
  706. if (ri->task != current)
  707. /* another task is sharing our hash bucket */
  708. continue;
  709. orig_ret_address = (unsigned long)ri->ret_addr;
  710. if (orig_ret_address != trampoline_address)
  711. /*
  712. * This is the real return address. Any other
  713. * instances associated with this task are for
  714. * other calls deeper on the call stack
  715. */
  716. break;
  717. }
  718. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  719. correct_ret_addr = ri->ret_addr;
  720. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  721. if (ri->task != current)
  722. /* another task is sharing our hash bucket */
  723. continue;
  724. orig_ret_address = (unsigned long)ri->ret_addr;
  725. if (ri->rp && ri->rp->handler) {
  726. __this_cpu_write(current_kprobe, &ri->rp->kp);
  727. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  728. ri->ret_addr = correct_ret_addr;
  729. ri->rp->handler(ri, regs);
  730. __this_cpu_write(current_kprobe, NULL);
  731. }
  732. recycle_rp_inst(ri, &empty_rp);
  733. if (orig_ret_address != trampoline_address)
  734. /*
  735. * This is the real return address. Any other
  736. * instances associated with this task are for
  737. * other calls deeper on the call stack
  738. */
  739. break;
  740. }
  741. kretprobe_hash_unlock(current, &flags);
  742. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  743. hlist_del(&ri->hlist);
  744. kfree(ri);
  745. }
  746. return (void *)orig_ret_address;
  747. }
  748. NOKPROBE_SYMBOL(trampoline_handler);
  749. /*
  750. * Called after single-stepping. p->addr is the address of the
  751. * instruction whose first byte has been replaced by the "int 3"
  752. * instruction. To avoid the SMP problems that can occur when we
  753. * temporarily put back the original opcode to single-step, we
  754. * single-stepped a copy of the instruction. The address of this
  755. * copy is p->ainsn.insn.
  756. *
  757. * This function prepares to return from the post-single-step
  758. * interrupt. We have to fix up the stack as follows:
  759. *
  760. * 0) Except in the case of absolute or indirect jump or call instructions,
  761. * the new ip is relative to the copied instruction. We need to make
  762. * it relative to the original instruction.
  763. *
  764. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  765. * flags are set in the just-pushed flags, and may need to be cleared.
  766. *
  767. * 2) If the single-stepped instruction was a call, the return address
  768. * that is atop the stack is the address following the copied instruction.
  769. * We need to make it the address following the original instruction.
  770. *
  771. * If this is the first time we've single-stepped the instruction at
  772. * this probepoint, and the instruction is boostable, boost it: add a
  773. * jump instruction after the copied instruction, that jumps to the next
  774. * instruction after the probepoint.
  775. */
  776. static void resume_execution(struct kprobe *p, struct pt_regs *regs,
  777. struct kprobe_ctlblk *kcb)
  778. {
  779. unsigned long *tos = stack_addr(regs);
  780. unsigned long copy_ip = (unsigned long)p->ainsn.insn;
  781. unsigned long orig_ip = (unsigned long)p->addr;
  782. kprobe_opcode_t *insn = p->ainsn.insn;
  783. /* Skip prefixes */
  784. insn = skip_prefixes(insn);
  785. regs->flags &= ~X86_EFLAGS_TF;
  786. switch (*insn) {
  787. case 0x9c: /* pushfl */
  788. *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
  789. *tos |= kcb->kprobe_old_flags;
  790. break;
  791. case 0xc2: /* iret/ret/lret */
  792. case 0xc3:
  793. case 0xca:
  794. case 0xcb:
  795. case 0xcf:
  796. case 0xea: /* jmp absolute -- ip is correct */
  797. /* ip is already adjusted, no more changes required */
  798. p->ainsn.boostable = true;
  799. goto no_change;
  800. case 0xe8: /* call relative - Fix return addr */
  801. *tos = orig_ip + (*tos - copy_ip);
  802. break;
  803. #ifdef CONFIG_X86_32
  804. case 0x9a: /* call absolute -- same as call absolute, indirect */
  805. *tos = orig_ip + (*tos - copy_ip);
  806. goto no_change;
  807. #endif
  808. case 0xff:
  809. if ((insn[1] & 0x30) == 0x10) {
  810. /*
  811. * call absolute, indirect
  812. * Fix return addr; ip is correct.
  813. * But this is not boostable
  814. */
  815. *tos = orig_ip + (*tos - copy_ip);
  816. goto no_change;
  817. } else if (((insn[1] & 0x31) == 0x20) ||
  818. ((insn[1] & 0x31) == 0x21)) {
  819. /*
  820. * jmp near and far, absolute indirect
  821. * ip is correct. And this is boostable
  822. */
  823. p->ainsn.boostable = true;
  824. goto no_change;
  825. }
  826. default:
  827. break;
  828. }
  829. regs->ip += orig_ip - copy_ip;
  830. no_change:
  831. restore_btf();
  832. }
  833. NOKPROBE_SYMBOL(resume_execution);
  834. /*
  835. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  836. * remain disabled throughout this function.
  837. */
  838. int kprobe_debug_handler(struct pt_regs *regs)
  839. {
  840. struct kprobe *cur = kprobe_running();
  841. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  842. if (!cur)
  843. return 0;
  844. resume_execution(cur, regs, kcb);
  845. regs->flags |= kcb->kprobe_saved_flags;
  846. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  847. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  848. cur->post_handler(cur, regs, 0);
  849. }
  850. /* Restore back the original saved kprobes variables and continue. */
  851. if (kcb->kprobe_status == KPROBE_REENTER) {
  852. restore_previous_kprobe(kcb);
  853. goto out;
  854. }
  855. reset_current_kprobe();
  856. out:
  857. preempt_enable_no_resched();
  858. /*
  859. * if somebody else is singlestepping across a probe point, flags
  860. * will have TF set, in which case, continue the remaining processing
  861. * of do_debug, as if this is not a probe hit.
  862. */
  863. if (regs->flags & X86_EFLAGS_TF)
  864. return 0;
  865. return 1;
  866. }
  867. NOKPROBE_SYMBOL(kprobe_debug_handler);
  868. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  869. {
  870. struct kprobe *cur = kprobe_running();
  871. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  872. if (unlikely(regs->ip == (unsigned long)cur->ainsn.insn)) {
  873. /* This must happen on single-stepping */
  874. WARN_ON(kcb->kprobe_status != KPROBE_HIT_SS &&
  875. kcb->kprobe_status != KPROBE_REENTER);
  876. /*
  877. * We are here because the instruction being single
  878. * stepped caused a page fault. We reset the current
  879. * kprobe and the ip points back to the probe address
  880. * and allow the page fault handler to continue as a
  881. * normal page fault.
  882. */
  883. regs->ip = (unsigned long)cur->addr;
  884. /*
  885. * Trap flag (TF) has been set here because this fault
  886. * happened where the single stepping will be done.
  887. * So clear it by resetting the current kprobe:
  888. */
  889. regs->flags &= ~X86_EFLAGS_TF;
  890. /*
  891. * If the TF flag was set before the kprobe hit,
  892. * don't touch it:
  893. */
  894. regs->flags |= kcb->kprobe_old_flags;
  895. if (kcb->kprobe_status == KPROBE_REENTER)
  896. restore_previous_kprobe(kcb);
  897. else
  898. reset_current_kprobe();
  899. preempt_enable_no_resched();
  900. } else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
  901. kcb->kprobe_status == KPROBE_HIT_SSDONE) {
  902. /*
  903. * We increment the nmissed count for accounting,
  904. * we can also use npre/npostfault count for accounting
  905. * these specific fault cases.
  906. */
  907. kprobes_inc_nmissed_count(cur);
  908. /*
  909. * We come here because instructions in the pre/post
  910. * handler caused the page_fault, this could happen
  911. * if handler tries to access user space by
  912. * copy_from_user(), get_user() etc. Let the
  913. * user-specified handler try to fix it first.
  914. */
  915. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  916. return 1;
  917. /*
  918. * In case the user-specified fault handler returned
  919. * zero, try to fix up.
  920. */
  921. if (fixup_exception(regs, trapnr))
  922. return 1;
  923. /*
  924. * fixup routine could not handle it,
  925. * Let do_page_fault() fix it.
  926. */
  927. }
  928. return 0;
  929. }
  930. NOKPROBE_SYMBOL(kprobe_fault_handler);
  931. /*
  932. * Wrapper routine for handling exceptions.
  933. */
  934. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  935. void *data)
  936. {
  937. struct die_args *args = data;
  938. int ret = NOTIFY_DONE;
  939. if (args->regs && user_mode(args->regs))
  940. return ret;
  941. if (val == DIE_GPF) {
  942. /*
  943. * To be potentially processing a kprobe fault and to
  944. * trust the result from kprobe_running(), we have
  945. * be non-preemptible.
  946. */
  947. if (!preemptible() && kprobe_running() &&
  948. kprobe_fault_handler(args->regs, args->trapnr))
  949. ret = NOTIFY_STOP;
  950. }
  951. return ret;
  952. }
  953. NOKPROBE_SYMBOL(kprobe_exceptions_notify);
  954. bool arch_within_kprobe_blacklist(unsigned long addr)
  955. {
  956. bool is_in_entry_trampoline_section = false;
  957. #ifdef CONFIG_X86_64
  958. is_in_entry_trampoline_section =
  959. (addr >= (unsigned long)__entry_trampoline_start &&
  960. addr < (unsigned long)__entry_trampoline_end);
  961. #endif
  962. return (addr >= (unsigned long)__kprobes_text_start &&
  963. addr < (unsigned long)__kprobes_text_end) ||
  964. (addr >= (unsigned long)__entry_text_start &&
  965. addr < (unsigned long)__entry_text_end) ||
  966. is_in_entry_trampoline_section;
  967. }
  968. int __init arch_init_kprobes(void)
  969. {
  970. return 0;
  971. }
  972. int arch_trampoline_kprobe(struct kprobe *p)
  973. {
  974. return 0;
  975. }