kprobes.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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 "kprobes-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. } __attribute__((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. #ifdef KPROBES_CAN_USE_FTRACE
  486. static void __kprobes skip_singlestep(struct kprobe *p, struct pt_regs *regs,
  487. struct kprobe_ctlblk *kcb)
  488. {
  489. /*
  490. * Emulate singlestep (and also recover regs->ip)
  491. * as if there is a 5byte nop
  492. */
  493. regs->ip = (unsigned long)p->addr + MCOUNT_INSN_SIZE;
  494. if (unlikely(p->post_handler)) {
  495. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  496. p->post_handler(p, regs, 0);
  497. }
  498. __this_cpu_write(current_kprobe, NULL);
  499. }
  500. #endif
  501. /*
  502. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  503. * remain disabled throughout this function.
  504. */
  505. static int __kprobes kprobe_handler(struct pt_regs *regs)
  506. {
  507. kprobe_opcode_t *addr;
  508. struct kprobe *p;
  509. struct kprobe_ctlblk *kcb;
  510. addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
  511. /*
  512. * We don't want to be preempted for the entire
  513. * duration of kprobe processing. We conditionally
  514. * re-enable preemption at the end of this function,
  515. * and also in reenter_kprobe() and setup_singlestep().
  516. */
  517. preempt_disable();
  518. kcb = get_kprobe_ctlblk();
  519. p = get_kprobe(addr);
  520. if (p) {
  521. if (kprobe_running()) {
  522. if (reenter_kprobe(p, regs, kcb))
  523. return 1;
  524. } else {
  525. set_current_kprobe(p, regs, kcb);
  526. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  527. /*
  528. * If we have no pre-handler or it returned 0, we
  529. * continue with normal processing. If we have a
  530. * pre-handler and it returned non-zero, it prepped
  531. * for calling the break_handler below on re-entry
  532. * for jprobe processing, so get out doing nothing
  533. * more here.
  534. */
  535. if (!p->pre_handler || !p->pre_handler(p, regs))
  536. setup_singlestep(p, regs, kcb, 0);
  537. return 1;
  538. }
  539. } else if (*addr != BREAKPOINT_INSTRUCTION) {
  540. /*
  541. * The breakpoint instruction was removed right
  542. * after we hit it. Another cpu has removed
  543. * either a probepoint or a debugger breakpoint
  544. * at this address. In either case, no further
  545. * handling of this interrupt is appropriate.
  546. * Back up over the (now missing) int3 and run
  547. * the original instruction.
  548. */
  549. regs->ip = (unsigned long)addr;
  550. preempt_enable_no_resched();
  551. return 1;
  552. } else if (kprobe_running()) {
  553. p = __this_cpu_read(current_kprobe);
  554. if (p->break_handler && p->break_handler(p, regs)) {
  555. #ifdef KPROBES_CAN_USE_FTRACE
  556. if (kprobe_ftrace(p)) {
  557. skip_singlestep(p, regs, kcb);
  558. return 1;
  559. }
  560. #endif
  561. setup_singlestep(p, regs, kcb, 0);
  562. return 1;
  563. }
  564. } /* else: not a kprobe fault; let the kernel handle it */
  565. preempt_enable_no_resched();
  566. return 0;
  567. }
  568. /*
  569. * When a retprobed function returns, this code saves registers and
  570. * calls trampoline_handler() runs, which calls the kretprobe's handler.
  571. */
  572. static void __used __kprobes kretprobe_trampoline_holder(void)
  573. {
  574. asm volatile (
  575. ".global kretprobe_trampoline\n"
  576. "kretprobe_trampoline: \n"
  577. #ifdef CONFIG_X86_64
  578. /* We don't bother saving the ss register */
  579. " pushq %rsp\n"
  580. " pushfq\n"
  581. SAVE_REGS_STRING
  582. " movq %rsp, %rdi\n"
  583. " call trampoline_handler\n"
  584. /* Replace saved sp with true return address. */
  585. " movq %rax, 152(%rsp)\n"
  586. RESTORE_REGS_STRING
  587. " popfq\n"
  588. #else
  589. " pushf\n"
  590. SAVE_REGS_STRING
  591. " movl %esp, %eax\n"
  592. " call trampoline_handler\n"
  593. /* Move flags to cs */
  594. " movl 56(%esp), %edx\n"
  595. " movl %edx, 52(%esp)\n"
  596. /* Replace saved flags with true return address. */
  597. " movl %eax, 56(%esp)\n"
  598. RESTORE_REGS_STRING
  599. " popf\n"
  600. #endif
  601. " ret\n");
  602. }
  603. /*
  604. * Called from kretprobe_trampoline
  605. */
  606. static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
  607. {
  608. struct kretprobe_instance *ri = NULL;
  609. struct hlist_head *head, empty_rp;
  610. struct hlist_node *node, *tmp;
  611. unsigned long flags, orig_ret_address = 0;
  612. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  613. kprobe_opcode_t *correct_ret_addr = NULL;
  614. INIT_HLIST_HEAD(&empty_rp);
  615. kretprobe_hash_lock(current, &head, &flags);
  616. /* fixup registers */
  617. #ifdef CONFIG_X86_64
  618. regs->cs = __KERNEL_CS;
  619. #else
  620. regs->cs = __KERNEL_CS | get_kernel_rpl();
  621. regs->gs = 0;
  622. #endif
  623. regs->ip = trampoline_address;
  624. regs->orig_ax = ~0UL;
  625. /*
  626. * It is possible to have multiple instances associated with a given
  627. * task either because multiple functions in the call path have
  628. * return probes installed on them, and/or more than one
  629. * return probe was registered for a target function.
  630. *
  631. * We can handle this because:
  632. * - instances are always pushed into the head of the list
  633. * - when multiple return probes are registered for the same
  634. * function, the (chronologically) first instance's ret_addr
  635. * will be the real return address, and all the rest will
  636. * point to kretprobe_trampoline.
  637. */
  638. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  639. if (ri->task != current)
  640. /* another task is sharing our hash bucket */
  641. continue;
  642. orig_ret_address = (unsigned long)ri->ret_addr;
  643. if (orig_ret_address != trampoline_address)
  644. /*
  645. * This is the real return address. Any other
  646. * instances associated with this task are for
  647. * other calls deeper on the call stack
  648. */
  649. break;
  650. }
  651. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  652. correct_ret_addr = ri->ret_addr;
  653. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  654. if (ri->task != current)
  655. /* another task is sharing our hash bucket */
  656. continue;
  657. orig_ret_address = (unsigned long)ri->ret_addr;
  658. if (ri->rp && ri->rp->handler) {
  659. __this_cpu_write(current_kprobe, &ri->rp->kp);
  660. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  661. ri->ret_addr = correct_ret_addr;
  662. ri->rp->handler(ri, regs);
  663. __this_cpu_write(current_kprobe, NULL);
  664. }
  665. recycle_rp_inst(ri, &empty_rp);
  666. if (orig_ret_address != trampoline_address)
  667. /*
  668. * This is the real return address. Any other
  669. * instances associated with this task are for
  670. * other calls deeper on the call stack
  671. */
  672. break;
  673. }
  674. kretprobe_hash_unlock(current, &flags);
  675. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  676. hlist_del(&ri->hlist);
  677. kfree(ri);
  678. }
  679. return (void *)orig_ret_address;
  680. }
  681. /*
  682. * Called after single-stepping. p->addr is the address of the
  683. * instruction whose first byte has been replaced by the "int 3"
  684. * instruction. To avoid the SMP problems that can occur when we
  685. * temporarily put back the original opcode to single-step, we
  686. * single-stepped a copy of the instruction. The address of this
  687. * copy is p->ainsn.insn.
  688. *
  689. * This function prepares to return from the post-single-step
  690. * interrupt. We have to fix up the stack as follows:
  691. *
  692. * 0) Except in the case of absolute or indirect jump or call instructions,
  693. * the new ip is relative to the copied instruction. We need to make
  694. * it relative to the original instruction.
  695. *
  696. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  697. * flags are set in the just-pushed flags, and may need to be cleared.
  698. *
  699. * 2) If the single-stepped instruction was a call, the return address
  700. * that is atop the stack is the address following the copied instruction.
  701. * We need to make it the address following the original instruction.
  702. *
  703. * If this is the first time we've single-stepped the instruction at
  704. * this probepoint, and the instruction is boostable, boost it: add a
  705. * jump instruction after the copied instruction, that jumps to the next
  706. * instruction after the probepoint.
  707. */
  708. static void __kprobes
  709. resume_execution(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  710. {
  711. unsigned long *tos = stack_addr(regs);
  712. unsigned long copy_ip = (unsigned long)p->ainsn.insn;
  713. unsigned long orig_ip = (unsigned long)p->addr;
  714. kprobe_opcode_t *insn = p->ainsn.insn;
  715. /* Skip prefixes */
  716. insn = skip_prefixes(insn);
  717. regs->flags &= ~X86_EFLAGS_TF;
  718. switch (*insn) {
  719. case 0x9c: /* pushfl */
  720. *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
  721. *tos |= kcb->kprobe_old_flags;
  722. break;
  723. case 0xc2: /* iret/ret/lret */
  724. case 0xc3:
  725. case 0xca:
  726. case 0xcb:
  727. case 0xcf:
  728. case 0xea: /* jmp absolute -- ip is correct */
  729. /* ip is already adjusted, no more changes required */
  730. p->ainsn.boostable = 1;
  731. goto no_change;
  732. case 0xe8: /* call relative - Fix return addr */
  733. *tos = orig_ip + (*tos - copy_ip);
  734. break;
  735. #ifdef CONFIG_X86_32
  736. case 0x9a: /* call absolute -- same as call absolute, indirect */
  737. *tos = orig_ip + (*tos - copy_ip);
  738. goto no_change;
  739. #endif
  740. case 0xff:
  741. if ((insn[1] & 0x30) == 0x10) {
  742. /*
  743. * call absolute, indirect
  744. * Fix return addr; ip is correct.
  745. * But this is not boostable
  746. */
  747. *tos = orig_ip + (*tos - copy_ip);
  748. goto no_change;
  749. } else if (((insn[1] & 0x31) == 0x20) ||
  750. ((insn[1] & 0x31) == 0x21)) {
  751. /*
  752. * jmp near and far, absolute indirect
  753. * ip is correct. And this is boostable
  754. */
  755. p->ainsn.boostable = 1;
  756. goto no_change;
  757. }
  758. default:
  759. break;
  760. }
  761. if (p->ainsn.boostable == 0) {
  762. if ((regs->ip > copy_ip) &&
  763. (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
  764. /*
  765. * These instructions can be executed directly if it
  766. * jumps back to correct address.
  767. */
  768. synthesize_reljump((void *)regs->ip,
  769. (void *)orig_ip + (regs->ip - copy_ip));
  770. p->ainsn.boostable = 1;
  771. } else {
  772. p->ainsn.boostable = -1;
  773. }
  774. }
  775. regs->ip += orig_ip - copy_ip;
  776. no_change:
  777. restore_btf();
  778. }
  779. /*
  780. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  781. * remain disabled throughout this function.
  782. */
  783. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  784. {
  785. struct kprobe *cur = kprobe_running();
  786. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  787. if (!cur)
  788. return 0;
  789. resume_execution(cur, regs, kcb);
  790. regs->flags |= kcb->kprobe_saved_flags;
  791. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  792. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  793. cur->post_handler(cur, regs, 0);
  794. }
  795. /* Restore back the original saved kprobes variables and continue. */
  796. if (kcb->kprobe_status == KPROBE_REENTER) {
  797. restore_previous_kprobe(kcb);
  798. goto out;
  799. }
  800. reset_current_kprobe();
  801. out:
  802. preempt_enable_no_resched();
  803. /*
  804. * if somebody else is singlestepping across a probe point, flags
  805. * will have TF set, in which case, continue the remaining processing
  806. * of do_debug, as if this is not a probe hit.
  807. */
  808. if (regs->flags & X86_EFLAGS_TF)
  809. return 0;
  810. return 1;
  811. }
  812. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  813. {
  814. struct kprobe *cur = kprobe_running();
  815. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  816. switch (kcb->kprobe_status) {
  817. case KPROBE_HIT_SS:
  818. case KPROBE_REENTER:
  819. /*
  820. * We are here because the instruction being single
  821. * stepped caused a page fault. We reset the current
  822. * kprobe and the ip points back to the probe address
  823. * and allow the page fault handler to continue as a
  824. * normal page fault.
  825. */
  826. regs->ip = (unsigned long)cur->addr;
  827. regs->flags |= kcb->kprobe_old_flags;
  828. if (kcb->kprobe_status == KPROBE_REENTER)
  829. restore_previous_kprobe(kcb);
  830. else
  831. reset_current_kprobe();
  832. preempt_enable_no_resched();
  833. break;
  834. case KPROBE_HIT_ACTIVE:
  835. case KPROBE_HIT_SSDONE:
  836. /*
  837. * We increment the nmissed count for accounting,
  838. * we can also use npre/npostfault count for accounting
  839. * these specific fault cases.
  840. */
  841. kprobes_inc_nmissed_count(cur);
  842. /*
  843. * We come here because instructions in the pre/post
  844. * handler caused the page_fault, this could happen
  845. * if handler tries to access user space by
  846. * copy_from_user(), get_user() etc. Let the
  847. * user-specified handler try to fix it first.
  848. */
  849. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  850. return 1;
  851. /*
  852. * In case the user-specified fault handler returned
  853. * zero, try to fix up.
  854. */
  855. if (fixup_exception(regs))
  856. return 1;
  857. /*
  858. * fixup routine could not handle it,
  859. * Let do_page_fault() fix it.
  860. */
  861. break;
  862. default:
  863. break;
  864. }
  865. return 0;
  866. }
  867. /*
  868. * Wrapper routine for handling exceptions.
  869. */
  870. int __kprobes
  871. kprobe_exceptions_notify(struct notifier_block *self, unsigned long val, void *data)
  872. {
  873. struct die_args *args = data;
  874. int ret = NOTIFY_DONE;
  875. if (args->regs && user_mode_vm(args->regs))
  876. return ret;
  877. switch (val) {
  878. case DIE_INT3:
  879. if (kprobe_handler(args->regs))
  880. ret = NOTIFY_STOP;
  881. break;
  882. case DIE_DEBUG:
  883. if (post_kprobe_handler(args->regs)) {
  884. /*
  885. * Reset the BS bit in dr6 (pointed by args->err) to
  886. * denote completion of processing
  887. */
  888. (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
  889. ret = NOTIFY_STOP;
  890. }
  891. break;
  892. case DIE_GPF:
  893. /*
  894. * To be potentially processing a kprobe fault and to
  895. * trust the result from kprobe_running(), we have
  896. * be non-preemptible.
  897. */
  898. if (!preemptible() && kprobe_running() &&
  899. kprobe_fault_handler(args->regs, args->trapnr))
  900. ret = NOTIFY_STOP;
  901. break;
  902. default:
  903. break;
  904. }
  905. return ret;
  906. }
  907. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  908. {
  909. struct jprobe *jp = container_of(p, struct jprobe, kp);
  910. unsigned long addr;
  911. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  912. kcb->jprobe_saved_regs = *regs;
  913. kcb->jprobe_saved_sp = stack_addr(regs);
  914. addr = (unsigned long)(kcb->jprobe_saved_sp);
  915. /*
  916. * As Linus pointed out, gcc assumes that the callee
  917. * owns the argument space and could overwrite it, e.g.
  918. * tailcall optimization. So, to be absolutely safe
  919. * we also save and restore enough stack bytes to cover
  920. * the argument area.
  921. */
  922. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
  923. MIN_STACK_SIZE(addr));
  924. regs->flags &= ~X86_EFLAGS_IF;
  925. trace_hardirqs_off();
  926. regs->ip = (unsigned long)(jp->entry);
  927. return 1;
  928. }
  929. void __kprobes jprobe_return(void)
  930. {
  931. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  932. asm volatile (
  933. #ifdef CONFIG_X86_64
  934. " xchg %%rbx,%%rsp \n"
  935. #else
  936. " xchgl %%ebx,%%esp \n"
  937. #endif
  938. " int3 \n"
  939. " .globl jprobe_return_end\n"
  940. " jprobe_return_end: \n"
  941. " nop \n"::"b"
  942. (kcb->jprobe_saved_sp):"memory");
  943. }
  944. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  945. {
  946. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  947. u8 *addr = (u8 *) (regs->ip - 1);
  948. struct jprobe *jp = container_of(p, struct jprobe, kp);
  949. if ((addr > (u8 *) jprobe_return) &&
  950. (addr < (u8 *) jprobe_return_end)) {
  951. if (stack_addr(regs) != kcb->jprobe_saved_sp) {
  952. struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
  953. printk(KERN_ERR
  954. "current sp %p does not match saved sp %p\n",
  955. stack_addr(regs), kcb->jprobe_saved_sp);
  956. printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
  957. show_regs(saved_regs);
  958. printk(KERN_ERR "Current registers\n");
  959. show_regs(regs);
  960. BUG();
  961. }
  962. *regs = kcb->jprobe_saved_regs;
  963. memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
  964. kcb->jprobes_stack,
  965. MIN_STACK_SIZE(kcb->jprobe_saved_sp));
  966. preempt_enable_no_resched();
  967. return 1;
  968. }
  969. return 0;
  970. }
  971. #ifdef KPROBES_CAN_USE_FTRACE
  972. /* Ftrace callback handler for kprobes */
  973. void __kprobes kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
  974. struct ftrace_ops *ops, struct pt_regs *regs)
  975. {
  976. struct kprobe *p;
  977. struct kprobe_ctlblk *kcb;
  978. unsigned long flags;
  979. /* Disable irq for emulating a breakpoint and avoiding preempt */
  980. local_irq_save(flags);
  981. p = get_kprobe((kprobe_opcode_t *)ip);
  982. if (unlikely(!p) || kprobe_disabled(p))
  983. goto end;
  984. kcb = get_kprobe_ctlblk();
  985. if (kprobe_running()) {
  986. kprobes_inc_nmissed_count(p);
  987. } else {
  988. /* Kprobe handler expects regs->ip = ip + 1 as breakpoint hit */
  989. regs->ip = ip + sizeof(kprobe_opcode_t);
  990. __this_cpu_write(current_kprobe, p);
  991. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  992. if (!p->pre_handler || !p->pre_handler(p, regs))
  993. skip_singlestep(p, regs, kcb);
  994. /*
  995. * If pre_handler returns !0, it sets regs->ip and
  996. * resets current kprobe.
  997. */
  998. }
  999. end:
  1000. local_irq_restore(flags);
  1001. }
  1002. int __kprobes arch_prepare_kprobe_ftrace(struct kprobe *p)
  1003. {
  1004. p->ainsn.insn = NULL;
  1005. p->ainsn.boostable = -1;
  1006. return 0;
  1007. }
  1008. #endif
  1009. int __init arch_init_kprobes(void)
  1010. {
  1011. return arch_init_optprobes();
  1012. }
  1013. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  1014. {
  1015. return 0;
  1016. }