core.c 31 KB

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