core.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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/module.h>
  49. #include <linux/kdebug.h>
  50. #include <linux/kallsyms.h>
  51. #include <linux/ftrace.h>
  52. #include <asm/cacheflush.h>
  53. #include <asm/desc.h>
  54. #include <asm/pgtable.h>
  55. #include <asm/uaccess.h>
  56. #include <asm/alternative.h>
  57. #include <asm/insn.h>
  58. #include <asm/debugreg.h>
  59. #include "common.h"
  60. void jprobe_return_end(void);
  61. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  62. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  63. #define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
  64. #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
  65. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  66. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  67. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  68. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  69. << (row % 32))
  70. /*
  71. * Undefined/reserved opcodes, conditional jump, Opcode Extension
  72. * Groups, and some special opcodes can not boost.
  73. * This is non-const and volatile to keep gcc from statically
  74. * optimizing it out, as variable_test_bit makes gcc think only
  75. * *(unsigned long*) is used.
  76. */
  77. static volatile u32 twobyte_is_boostable[256 / 32] = {
  78. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  79. /* ---------------------------------------------- */
  80. W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
  81. W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
  82. W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
  83. W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
  84. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  85. W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
  86. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
  87. W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
  88. W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
  89. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  90. W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
  91. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
  92. W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
  93. W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
  94. W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
  95. W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
  96. /* ----------------------------------------------- */
  97. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  98. };
  99. #undef W
  100. struct kretprobe_blackpoint kretprobe_blacklist[] = {
  101. {"__switch_to", }, /* This function switches only current task, but
  102. doesn't switch kernel stack.*/
  103. {NULL, NULL} /* Terminator */
  104. };
  105. const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
  106. static void __kprobes __synthesize_relative_insn(void *from, void *to, u8 op)
  107. {
  108. struct __arch_relative_insn {
  109. u8 op;
  110. s32 raddr;
  111. } __packed *insn;
  112. insn = (struct __arch_relative_insn *)from;
  113. insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
  114. insn->op = op;
  115. }
  116. /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
  117. void __kprobes synthesize_reljump(void *from, void *to)
  118. {
  119. __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
  120. }
  121. /* Insert a call instruction at address 'from', which calls address 'to'.*/
  122. void __kprobes synthesize_relcall(void *from, void *to)
  123. {
  124. __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
  125. }
  126. /*
  127. * Skip the prefixes of the instruction.
  128. */
  129. static kprobe_opcode_t *__kprobes skip_prefixes(kprobe_opcode_t *insn)
  130. {
  131. insn_attr_t attr;
  132. attr = inat_get_opcode_attribute((insn_byte_t)*insn);
  133. while (inat_is_legacy_prefix(attr)) {
  134. insn++;
  135. attr = inat_get_opcode_attribute((insn_byte_t)*insn);
  136. }
  137. #ifdef CONFIG_X86_64
  138. if (inat_is_rex_prefix(attr))
  139. insn++;
  140. #endif
  141. return insn;
  142. }
  143. /*
  144. * Returns non-zero if opcode is boostable.
  145. * RIP relative instructions are adjusted at copying time in 64 bits mode
  146. */
  147. int __kprobes can_boost(kprobe_opcode_t *opcodes)
  148. {
  149. kprobe_opcode_t opcode;
  150. kprobe_opcode_t *orig_opcodes = opcodes;
  151. if (search_exception_tables((unsigned long)opcodes))
  152. return 0; /* Page fault may occur on this address. */
  153. retry:
  154. if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
  155. return 0;
  156. opcode = *(opcodes++);
  157. /* 2nd-byte opcode */
  158. if (opcode == 0x0f) {
  159. if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
  160. return 0;
  161. return test_bit(*opcodes,
  162. (unsigned long *)twobyte_is_boostable);
  163. }
  164. switch (opcode & 0xf0) {
  165. #ifdef CONFIG_X86_64
  166. case 0x40:
  167. goto retry; /* REX prefix is boostable */
  168. #endif
  169. case 0x60:
  170. if (0x63 < opcode && opcode < 0x67)
  171. goto retry; /* prefixes */
  172. /* can't boost Address-size override and bound */
  173. return (opcode != 0x62 && opcode != 0x67);
  174. case 0x70:
  175. return 0; /* can't boost conditional jump */
  176. case 0xc0:
  177. /* can't boost software-interruptions */
  178. return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
  179. case 0xd0:
  180. /* can boost AA* and XLAT */
  181. return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
  182. case 0xe0:
  183. /* can boost in/out and absolute jmps */
  184. return ((opcode & 0x04) || opcode == 0xea);
  185. case 0xf0:
  186. if ((opcode & 0x0c) == 0 && opcode != 0xf1)
  187. goto retry; /* lock/rep(ne) prefix */
  188. /* clear and set flags are boostable */
  189. return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
  190. default:
  191. /* segment override prefixes are boostable */
  192. if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
  193. goto retry; /* prefixes */
  194. /* CS override prefix and call are not boostable */
  195. return (opcode != 0x2e && opcode != 0x9a);
  196. }
  197. }
  198. static unsigned long
  199. __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)
  200. {
  201. struct kprobe *kp;
  202. kp = get_kprobe((void *)addr);
  203. /* There is no probe, return original address */
  204. if (!kp)
  205. return addr;
  206. /*
  207. * Basically, kp->ainsn.insn has an original instruction.
  208. * However, RIP-relative instruction can not do single-stepping
  209. * at different place, __copy_instruction() tweaks the displacement of
  210. * that instruction. In that case, we can't recover the instruction
  211. * from the kp->ainsn.insn.
  212. *
  213. * On the other hand, kp->opcode has a copy of the first byte of
  214. * the probed instruction, which is overwritten by int3. And
  215. * the instruction at kp->addr is not modified by kprobes except
  216. * for the first byte, we can recover the original instruction
  217. * from it and kp->opcode.
  218. */
  219. memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  220. buf[0] = kp->opcode;
  221. return (unsigned long)buf;
  222. }
  223. /*
  224. * Recover the probed instruction at addr for further analysis.
  225. * Caller must lock kprobes by kprobe_mutex, or disable preemption
  226. * for preventing to release referencing kprobes.
  227. */
  228. unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
  229. {
  230. unsigned long __addr;
  231. __addr = __recover_optprobed_insn(buf, addr);
  232. if (__addr != addr)
  233. return __addr;
  234. return __recover_probed_insn(buf, addr);
  235. }
  236. /* Check if paddr is at an instruction boundary */
  237. static int __kprobes can_probe(unsigned long paddr)
  238. {
  239. unsigned long addr, __addr, offset = 0;
  240. struct insn insn;
  241. kprobe_opcode_t buf[MAX_INSN_SIZE];
  242. if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
  243. return 0;
  244. /* Decode instructions */
  245. addr = paddr - offset;
  246. while (addr < paddr) {
  247. /*
  248. * Check if the instruction has been modified by another
  249. * kprobe, in which case we replace the breakpoint by the
  250. * original instruction in our buffer.
  251. * Also, jump optimization will change the breakpoint to
  252. * relative-jump. Since the relative-jump itself is
  253. * normally used, we just go through if there is no kprobe.
  254. */
  255. __addr = recover_probed_instruction(buf, addr);
  256. kernel_insn_init(&insn, (void *)__addr);
  257. insn_get_length(&insn);
  258. /*
  259. * Another debugging subsystem might insert this breakpoint.
  260. * In that case, we can't recover it.
  261. */
  262. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  263. return 0;
  264. addr += insn.length;
  265. }
  266. return (addr == paddr);
  267. }
  268. /*
  269. * Returns non-zero if opcode modifies the interrupt flag.
  270. */
  271. static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
  272. {
  273. /* Skip prefixes */
  274. insn = skip_prefixes(insn);
  275. switch (*insn) {
  276. case 0xfa: /* cli */
  277. case 0xfb: /* sti */
  278. case 0xcf: /* iret/iretd */
  279. case 0x9d: /* popf/popfd */
  280. return 1;
  281. }
  282. return 0;
  283. }
  284. /*
  285. * Copy an instruction and adjust the displacement if the instruction
  286. * uses the %rip-relative addressing mode.
  287. * If it does, Return the address of the 32-bit displacement word.
  288. * If not, return null.
  289. * Only applicable to 64-bit x86.
  290. */
  291. int __kprobes __copy_instruction(u8 *dest, u8 *src)
  292. {
  293. struct insn insn;
  294. kprobe_opcode_t buf[MAX_INSN_SIZE];
  295. kernel_insn_init(&insn, (void *)recover_probed_instruction(buf, (unsigned long)src));
  296. insn_get_length(&insn);
  297. /* Another subsystem puts a breakpoint, failed to recover */
  298. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  299. return 0;
  300. memcpy(dest, insn.kaddr, insn.length);
  301. #ifdef CONFIG_X86_64
  302. if (insn_rip_relative(&insn)) {
  303. s64 newdisp;
  304. u8 *disp;
  305. kernel_insn_init(&insn, dest);
  306. insn_get_displacement(&insn);
  307. /*
  308. * The copied instruction uses the %rip-relative addressing
  309. * mode. Adjust the displacement for the difference between
  310. * the original location of this instruction and the location
  311. * of the copy that will actually be run. The tricky bit here
  312. * is making sure that the sign extension happens correctly in
  313. * this calculation, since we need a signed 32-bit result to
  314. * be sign-extended to 64 bits when it's added to the %rip
  315. * value and yield the same 64-bit result that the sign-
  316. * extension of the original signed 32-bit displacement would
  317. * have given.
  318. */
  319. newdisp = (u8 *) src + (s64) insn.displacement.value - (u8 *) dest;
  320. BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
  321. disp = (u8 *) dest + insn_offset_displacement(&insn);
  322. *(s32 *) disp = (s32) newdisp;
  323. }
  324. #endif
  325. return insn.length;
  326. }
  327. static void __kprobes arch_copy_kprobe(struct kprobe *p)
  328. {
  329. /* Copy an instruction with recovering if other optprobe modifies it.*/
  330. __copy_instruction(p->ainsn.insn, p->addr);
  331. /*
  332. * __copy_instruction can modify the displacement of the instruction,
  333. * but it doesn't affect boostable check.
  334. */
  335. if (can_boost(p->ainsn.insn))
  336. p->ainsn.boostable = 0;
  337. else
  338. p->ainsn.boostable = -1;
  339. /* Also, displacement change doesn't affect the first byte */
  340. p->opcode = p->ainsn.insn[0];
  341. }
  342. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  343. {
  344. if (alternatives_text_reserved(p->addr, p->addr))
  345. return -EINVAL;
  346. if (!can_probe((unsigned long)p->addr))
  347. return -EILSEQ;
  348. /* insn: must be on special executable page on x86. */
  349. p->ainsn.insn = get_insn_slot();
  350. if (!p->ainsn.insn)
  351. return -ENOMEM;
  352. arch_copy_kprobe(p);
  353. return 0;
  354. }
  355. void __kprobes arch_arm_kprobe(struct kprobe *p)
  356. {
  357. text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
  358. }
  359. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  360. {
  361. text_poke(p->addr, &p->opcode, 1);
  362. }
  363. void __kprobes arch_remove_kprobe(struct kprobe *p)
  364. {
  365. if (p->ainsn.insn) {
  366. free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
  367. p->ainsn.insn = NULL;
  368. }
  369. }
  370. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  371. {
  372. kcb->prev_kprobe.kp = kprobe_running();
  373. kcb->prev_kprobe.status = kcb->kprobe_status;
  374. kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
  375. kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
  376. }
  377. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  378. {
  379. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  380. kcb->kprobe_status = kcb->prev_kprobe.status;
  381. kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
  382. kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
  383. }
  384. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  385. struct kprobe_ctlblk *kcb)
  386. {
  387. __this_cpu_write(current_kprobe, p);
  388. kcb->kprobe_saved_flags = kcb->kprobe_old_flags
  389. = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
  390. if (is_IF_modifier(p->ainsn.insn))
  391. kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
  392. }
  393. static void __kprobes clear_btf(void)
  394. {
  395. if (test_thread_flag(TIF_BLOCKSTEP)) {
  396. unsigned long debugctl = get_debugctlmsr();
  397. debugctl &= ~DEBUGCTLMSR_BTF;
  398. update_debugctlmsr(debugctl);
  399. }
  400. }
  401. static void __kprobes restore_btf(void)
  402. {
  403. if (test_thread_flag(TIF_BLOCKSTEP)) {
  404. unsigned long debugctl = get_debugctlmsr();
  405. debugctl |= DEBUGCTLMSR_BTF;
  406. update_debugctlmsr(debugctl);
  407. }
  408. }
  409. void __kprobes
  410. arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
  411. {
  412. unsigned long *sara = stack_addr(regs);
  413. ri->ret_addr = (kprobe_opcode_t *) *sara;
  414. /* Replace the return addr with trampoline addr */
  415. *sara = (unsigned long) &kretprobe_trampoline;
  416. }
  417. static void __kprobes
  418. setup_singlestep(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb, int reenter)
  419. {
  420. if (setup_detour_execution(p, regs, reenter))
  421. return;
  422. #if !defined(CONFIG_PREEMPT)
  423. if (p->ainsn.boostable == 1 && !p->post_handler) {
  424. /* Boost up -- we can execute copied instructions directly */
  425. if (!reenter)
  426. reset_current_kprobe();
  427. /*
  428. * Reentering boosted probe doesn't reset current_kprobe,
  429. * nor set current_kprobe, because it doesn't use single
  430. * stepping.
  431. */
  432. regs->ip = (unsigned long)p->ainsn.insn;
  433. preempt_enable_no_resched();
  434. return;
  435. }
  436. #endif
  437. if (reenter) {
  438. save_previous_kprobe(kcb);
  439. set_current_kprobe(p, regs, kcb);
  440. kcb->kprobe_status = KPROBE_REENTER;
  441. } else
  442. kcb->kprobe_status = KPROBE_HIT_SS;
  443. /* Prepare real single stepping */
  444. clear_btf();
  445. regs->flags |= X86_EFLAGS_TF;
  446. regs->flags &= ~X86_EFLAGS_IF;
  447. /* single step inline if the instruction is an int3 */
  448. if (p->opcode == BREAKPOINT_INSTRUCTION)
  449. regs->ip = (unsigned long)p->addr;
  450. else
  451. regs->ip = (unsigned long)p->ainsn.insn;
  452. }
  453. /*
  454. * We have reentered the kprobe_handler(), since another probe was hit while
  455. * within the handler. We save the original kprobes variables and just single
  456. * step on the instruction of the new probe without calling any user handlers.
  457. */
  458. static int __kprobes
  459. reenter_kprobe(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  460. {
  461. switch (kcb->kprobe_status) {
  462. case KPROBE_HIT_SSDONE:
  463. case KPROBE_HIT_ACTIVE:
  464. kprobes_inc_nmissed_count(p);
  465. setup_singlestep(p, regs, kcb, 1);
  466. break;
  467. case KPROBE_HIT_SS:
  468. /* A probe has been hit in the codepath leading up to, or just
  469. * after, single-stepping of a probed instruction. This entire
  470. * codepath should strictly reside in .kprobes.text section.
  471. * Raise a BUG or we'll continue in an endless reentering loop
  472. * and eventually a stack overflow.
  473. */
  474. printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
  475. p->addr);
  476. dump_kprobe(p);
  477. BUG();
  478. default:
  479. /* impossible cases */
  480. WARN_ON(1);
  481. return 0;
  482. }
  483. return 1;
  484. }
  485. /*
  486. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  487. * remain disabled throughout this function.
  488. */
  489. static int __kprobes kprobe_handler(struct pt_regs *regs)
  490. {
  491. kprobe_opcode_t *addr;
  492. struct kprobe *p;
  493. struct kprobe_ctlblk *kcb;
  494. addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
  495. /*
  496. * We don't want to be preempted for the entire
  497. * duration of kprobe processing. We conditionally
  498. * re-enable preemption at the end of this function,
  499. * and also in reenter_kprobe() and setup_singlestep().
  500. */
  501. preempt_disable();
  502. kcb = get_kprobe_ctlblk();
  503. p = get_kprobe(addr);
  504. if (p) {
  505. if (kprobe_running()) {
  506. if (reenter_kprobe(p, regs, kcb))
  507. return 1;
  508. } else {
  509. set_current_kprobe(p, regs, kcb);
  510. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  511. /*
  512. * If we have no pre-handler or it returned 0, we
  513. * continue with normal processing. If we have a
  514. * pre-handler and it returned non-zero, it prepped
  515. * for calling the break_handler below on re-entry
  516. * for jprobe processing, so get out doing nothing
  517. * more here.
  518. */
  519. if (!p->pre_handler || !p->pre_handler(p, regs))
  520. setup_singlestep(p, regs, kcb, 0);
  521. return 1;
  522. }
  523. } else if (*addr != BREAKPOINT_INSTRUCTION) {
  524. /*
  525. * The breakpoint instruction was removed right
  526. * after we hit it. Another cpu has removed
  527. * either a probepoint or a debugger breakpoint
  528. * at this address. In either case, no further
  529. * handling of this interrupt is appropriate.
  530. * Back up over the (now missing) int3 and run
  531. * the original instruction.
  532. */
  533. regs->ip = (unsigned long)addr;
  534. preempt_enable_no_resched();
  535. return 1;
  536. } else if (kprobe_running()) {
  537. p = __this_cpu_read(current_kprobe);
  538. if (p->break_handler && p->break_handler(p, regs)) {
  539. if (!skip_singlestep(p, regs, kcb))
  540. setup_singlestep(p, regs, kcb, 0);
  541. return 1;
  542. }
  543. } /* else: not a kprobe fault; let the kernel handle it */
  544. preempt_enable_no_resched();
  545. return 0;
  546. }
  547. /*
  548. * When a retprobed function returns, this code saves registers and
  549. * calls trampoline_handler() runs, which calls the kretprobe's handler.
  550. */
  551. static void __used __kprobes kretprobe_trampoline_holder(void)
  552. {
  553. asm volatile (
  554. ".global kretprobe_trampoline\n"
  555. "kretprobe_trampoline: \n"
  556. #ifdef CONFIG_X86_64
  557. /* We don't bother saving the ss register */
  558. " pushq %rsp\n"
  559. " pushfq\n"
  560. SAVE_REGS_STRING
  561. " movq %rsp, %rdi\n"
  562. " call trampoline_handler\n"
  563. /* Replace saved sp with true return address. */
  564. " movq %rax, 152(%rsp)\n"
  565. RESTORE_REGS_STRING
  566. " popfq\n"
  567. #else
  568. " pushf\n"
  569. SAVE_REGS_STRING
  570. " movl %esp, %eax\n"
  571. " call trampoline_handler\n"
  572. /* Move flags to cs */
  573. " movl 56(%esp), %edx\n"
  574. " movl %edx, 52(%esp)\n"
  575. /* Replace saved flags with true return address. */
  576. " movl %eax, 56(%esp)\n"
  577. RESTORE_REGS_STRING
  578. " popf\n"
  579. #endif
  580. " ret\n");
  581. }
  582. /*
  583. * Called from kretprobe_trampoline
  584. */
  585. static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
  586. {
  587. struct kretprobe_instance *ri = NULL;
  588. struct hlist_head *head, empty_rp;
  589. struct hlist_node *tmp;
  590. unsigned long flags, orig_ret_address = 0;
  591. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  592. kprobe_opcode_t *correct_ret_addr = NULL;
  593. INIT_HLIST_HEAD(&empty_rp);
  594. kretprobe_hash_lock(current, &head, &flags);
  595. /* fixup registers */
  596. #ifdef CONFIG_X86_64
  597. regs->cs = __KERNEL_CS;
  598. #else
  599. regs->cs = __KERNEL_CS | get_kernel_rpl();
  600. regs->gs = 0;
  601. #endif
  602. regs->ip = trampoline_address;
  603. regs->orig_ax = ~0UL;
  604. /*
  605. * It is possible to have multiple instances associated with a given
  606. * task either because multiple functions in the call path have
  607. * return probes installed on them, and/or more than one
  608. * return probe was registered for a target function.
  609. *
  610. * We can handle this because:
  611. * - instances are always pushed into the head of the list
  612. * - when multiple return probes are registered for the same
  613. * function, the (chronologically) first instance's ret_addr
  614. * will be the real return address, and all the rest will
  615. * point to kretprobe_trampoline.
  616. */
  617. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  618. if (ri->task != current)
  619. /* another task is sharing our hash bucket */
  620. continue;
  621. orig_ret_address = (unsigned long)ri->ret_addr;
  622. if (orig_ret_address != trampoline_address)
  623. /*
  624. * This is the real return address. Any other
  625. * instances associated with this task are for
  626. * other calls deeper on the call stack
  627. */
  628. break;
  629. }
  630. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  631. correct_ret_addr = ri->ret_addr;
  632. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  633. if (ri->task != current)
  634. /* another task is sharing our hash bucket */
  635. continue;
  636. orig_ret_address = (unsigned long)ri->ret_addr;
  637. if (ri->rp && ri->rp->handler) {
  638. __this_cpu_write(current_kprobe, &ri->rp->kp);
  639. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  640. ri->ret_addr = correct_ret_addr;
  641. ri->rp->handler(ri, regs);
  642. __this_cpu_write(current_kprobe, NULL);
  643. }
  644. recycle_rp_inst(ri, &empty_rp);
  645. if (orig_ret_address != trampoline_address)
  646. /*
  647. * This is the real return address. Any other
  648. * instances associated with this task are for
  649. * other calls deeper on the call stack
  650. */
  651. break;
  652. }
  653. kretprobe_hash_unlock(current, &flags);
  654. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  655. hlist_del(&ri->hlist);
  656. kfree(ri);
  657. }
  658. return (void *)orig_ret_address;
  659. }
  660. /*
  661. * Called after single-stepping. p->addr is the address of the
  662. * instruction whose first byte has been replaced by the "int 3"
  663. * instruction. To avoid the SMP problems that can occur when we
  664. * temporarily put back the original opcode to single-step, we
  665. * single-stepped a copy of the instruction. The address of this
  666. * copy is p->ainsn.insn.
  667. *
  668. * This function prepares to return from the post-single-step
  669. * interrupt. We have to fix up the stack as follows:
  670. *
  671. * 0) Except in the case of absolute or indirect jump or call instructions,
  672. * the new ip is relative to the copied instruction. We need to make
  673. * it relative to the original instruction.
  674. *
  675. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  676. * flags are set in the just-pushed flags, and may need to be cleared.
  677. *
  678. * 2) If the single-stepped instruction was a call, the return address
  679. * that is atop the stack is the address following the copied instruction.
  680. * We need to make it the address following the original instruction.
  681. *
  682. * If this is the first time we've single-stepped the instruction at
  683. * this probepoint, and the instruction is boostable, boost it: add a
  684. * jump instruction after the copied instruction, that jumps to the next
  685. * instruction after the probepoint.
  686. */
  687. static void __kprobes
  688. resume_execution(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  689. {
  690. unsigned long *tos = stack_addr(regs);
  691. unsigned long copy_ip = (unsigned long)p->ainsn.insn;
  692. unsigned long orig_ip = (unsigned long)p->addr;
  693. kprobe_opcode_t *insn = p->ainsn.insn;
  694. /* Skip prefixes */
  695. insn = skip_prefixes(insn);
  696. regs->flags &= ~X86_EFLAGS_TF;
  697. switch (*insn) {
  698. case 0x9c: /* pushfl */
  699. *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
  700. *tos |= kcb->kprobe_old_flags;
  701. break;
  702. case 0xc2: /* iret/ret/lret */
  703. case 0xc3:
  704. case 0xca:
  705. case 0xcb:
  706. case 0xcf:
  707. case 0xea: /* jmp absolute -- ip is correct */
  708. /* ip is already adjusted, no more changes required */
  709. p->ainsn.boostable = 1;
  710. goto no_change;
  711. case 0xe8: /* call relative - Fix return addr */
  712. *tos = orig_ip + (*tos - copy_ip);
  713. break;
  714. #ifdef CONFIG_X86_32
  715. case 0x9a: /* call absolute -- same as call absolute, indirect */
  716. *tos = orig_ip + (*tos - copy_ip);
  717. goto no_change;
  718. #endif
  719. case 0xff:
  720. if ((insn[1] & 0x30) == 0x10) {
  721. /*
  722. * call absolute, indirect
  723. * Fix return addr; ip is correct.
  724. * But this is not boostable
  725. */
  726. *tos = orig_ip + (*tos - copy_ip);
  727. goto no_change;
  728. } else if (((insn[1] & 0x31) == 0x20) ||
  729. ((insn[1] & 0x31) == 0x21)) {
  730. /*
  731. * jmp near and far, absolute indirect
  732. * ip is correct. And this is boostable
  733. */
  734. p->ainsn.boostable = 1;
  735. goto no_change;
  736. }
  737. default:
  738. break;
  739. }
  740. if (p->ainsn.boostable == 0) {
  741. if ((regs->ip > copy_ip) &&
  742. (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
  743. /*
  744. * These instructions can be executed directly if it
  745. * jumps back to correct address.
  746. */
  747. synthesize_reljump((void *)regs->ip,
  748. (void *)orig_ip + (regs->ip - copy_ip));
  749. p->ainsn.boostable = 1;
  750. } else {
  751. p->ainsn.boostable = -1;
  752. }
  753. }
  754. regs->ip += orig_ip - copy_ip;
  755. no_change:
  756. restore_btf();
  757. }
  758. /*
  759. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  760. * remain disabled throughout this function.
  761. */
  762. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  763. {
  764. struct kprobe *cur = kprobe_running();
  765. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  766. if (!cur)
  767. return 0;
  768. resume_execution(cur, regs, kcb);
  769. regs->flags |= kcb->kprobe_saved_flags;
  770. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  771. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  772. cur->post_handler(cur, regs, 0);
  773. }
  774. /* Restore back the original saved kprobes variables and continue. */
  775. if (kcb->kprobe_status == KPROBE_REENTER) {
  776. restore_previous_kprobe(kcb);
  777. goto out;
  778. }
  779. reset_current_kprobe();
  780. out:
  781. preempt_enable_no_resched();
  782. /*
  783. * if somebody else is singlestepping across a probe point, flags
  784. * will have TF set, in which case, continue the remaining processing
  785. * of do_debug, as if this is not a probe hit.
  786. */
  787. if (regs->flags & X86_EFLAGS_TF)
  788. return 0;
  789. return 1;
  790. }
  791. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  792. {
  793. struct kprobe *cur = kprobe_running();
  794. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  795. switch (kcb->kprobe_status) {
  796. case KPROBE_HIT_SS:
  797. case KPROBE_REENTER:
  798. /*
  799. * We are here because the instruction being single
  800. * stepped caused a page fault. We reset the current
  801. * kprobe and the ip points back to the probe address
  802. * and allow the page fault handler to continue as a
  803. * normal page fault.
  804. */
  805. regs->ip = (unsigned long)cur->addr;
  806. regs->flags |= kcb->kprobe_old_flags;
  807. if (kcb->kprobe_status == KPROBE_REENTER)
  808. restore_previous_kprobe(kcb);
  809. else
  810. reset_current_kprobe();
  811. preempt_enable_no_resched();
  812. break;
  813. case KPROBE_HIT_ACTIVE:
  814. case KPROBE_HIT_SSDONE:
  815. /*
  816. * We increment the nmissed count for accounting,
  817. * we can also use npre/npostfault count for accounting
  818. * these specific fault cases.
  819. */
  820. kprobes_inc_nmissed_count(cur);
  821. /*
  822. * We come here because instructions in the pre/post
  823. * handler caused the page_fault, this could happen
  824. * if handler tries to access user space by
  825. * copy_from_user(), get_user() etc. Let the
  826. * user-specified handler try to fix it first.
  827. */
  828. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  829. return 1;
  830. /*
  831. * In case the user-specified fault handler returned
  832. * zero, try to fix up.
  833. */
  834. if (fixup_exception(regs))
  835. return 1;
  836. /*
  837. * fixup routine could not handle it,
  838. * Let do_page_fault() fix it.
  839. */
  840. break;
  841. default:
  842. break;
  843. }
  844. return 0;
  845. }
  846. /*
  847. * Wrapper routine for handling exceptions.
  848. */
  849. int __kprobes
  850. kprobe_exceptions_notify(struct notifier_block *self, unsigned long val, void *data)
  851. {
  852. struct die_args *args = data;
  853. int ret = NOTIFY_DONE;
  854. if (args->regs && user_mode_vm(args->regs))
  855. return ret;
  856. switch (val) {
  857. case DIE_INT3:
  858. if (kprobe_handler(args->regs))
  859. ret = NOTIFY_STOP;
  860. break;
  861. case DIE_DEBUG:
  862. if (post_kprobe_handler(args->regs)) {
  863. /*
  864. * Reset the BS bit in dr6 (pointed by args->err) to
  865. * denote completion of processing
  866. */
  867. (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
  868. ret = NOTIFY_STOP;
  869. }
  870. break;
  871. case DIE_GPF:
  872. /*
  873. * To be potentially processing a kprobe fault and to
  874. * trust the result from kprobe_running(), we have
  875. * be non-preemptible.
  876. */
  877. if (!preemptible() && kprobe_running() &&
  878. kprobe_fault_handler(args->regs, args->trapnr))
  879. ret = NOTIFY_STOP;
  880. break;
  881. default:
  882. break;
  883. }
  884. return ret;
  885. }
  886. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  887. {
  888. struct jprobe *jp = container_of(p, struct jprobe, kp);
  889. unsigned long addr;
  890. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  891. kcb->jprobe_saved_regs = *regs;
  892. kcb->jprobe_saved_sp = stack_addr(regs);
  893. addr = (unsigned long)(kcb->jprobe_saved_sp);
  894. /*
  895. * As Linus pointed out, gcc assumes that the callee
  896. * owns the argument space and could overwrite it, e.g.
  897. * tailcall optimization. So, to be absolutely safe
  898. * we also save and restore enough stack bytes to cover
  899. * the argument area.
  900. */
  901. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
  902. MIN_STACK_SIZE(addr));
  903. regs->flags &= ~X86_EFLAGS_IF;
  904. trace_hardirqs_off();
  905. regs->ip = (unsigned long)(jp->entry);
  906. return 1;
  907. }
  908. void __kprobes jprobe_return(void)
  909. {
  910. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  911. asm volatile (
  912. #ifdef CONFIG_X86_64
  913. " xchg %%rbx,%%rsp \n"
  914. #else
  915. " xchgl %%ebx,%%esp \n"
  916. #endif
  917. " int3 \n"
  918. " .globl jprobe_return_end\n"
  919. " jprobe_return_end: \n"
  920. " nop \n"::"b"
  921. (kcb->jprobe_saved_sp):"memory");
  922. }
  923. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  924. {
  925. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  926. u8 *addr = (u8 *) (regs->ip - 1);
  927. struct jprobe *jp = container_of(p, struct jprobe, kp);
  928. if ((addr > (u8 *) jprobe_return) &&
  929. (addr < (u8 *) jprobe_return_end)) {
  930. if (stack_addr(regs) != kcb->jprobe_saved_sp) {
  931. struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
  932. printk(KERN_ERR
  933. "current sp %p does not match saved sp %p\n",
  934. stack_addr(regs), kcb->jprobe_saved_sp);
  935. printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
  936. show_regs(saved_regs);
  937. printk(KERN_ERR "Current registers\n");
  938. show_regs(regs);
  939. BUG();
  940. }
  941. *regs = kcb->jprobe_saved_regs;
  942. memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
  943. kcb->jprobes_stack,
  944. MIN_STACK_SIZE(kcb->jprobe_saved_sp));
  945. preempt_enable_no_resched();
  946. return 1;
  947. }
  948. return 0;
  949. }
  950. int __init arch_init_kprobes(void)
  951. {
  952. return arch_init_optprobes();
  953. }
  954. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  955. {
  956. return 0;
  957. }