kprobes.c 31 KB

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