core.c 31 KB

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