uprobes.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * User-space Probes (UProbes) for x86
  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, 2008-2011
  19. * Authors:
  20. * Srikar Dronamraju
  21. * Jim Keniston
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/sched.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/uprobes.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/kdebug.h>
  29. #include <asm/processor.h>
  30. #include <asm/insn.h>
  31. /* Post-execution fixups. */
  32. /* Adjust IP back to vicinity of actual insn */
  33. #define UPROBE_FIX_IP 0x1
  34. /* Adjust the return address of a call insn */
  35. #define UPROBE_FIX_CALL 0x2
  36. /* Instruction will modify TF, don't change it */
  37. #define UPROBE_FIX_SETF 0x4
  38. #define UPROBE_FIX_RIP_AX 0x8000
  39. #define UPROBE_FIX_RIP_CX 0x4000
  40. #define UPROBE_TRAP_NR UINT_MAX
  41. /* Adaptations for mhiramat x86 decoder v14. */
  42. #define OPCODE1(insn) ((insn)->opcode.bytes[0])
  43. #define OPCODE2(insn) ((insn)->opcode.bytes[1])
  44. #define OPCODE3(insn) ((insn)->opcode.bytes[2])
  45. #define MODRM_REG(insn) X86_MODRM_REG((insn)->modrm.value)
  46. #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
  47. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  48. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  49. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  50. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  51. << (row % 32))
  52. /*
  53. * Good-instruction tables for 32-bit apps. This is non-const and volatile
  54. * to keep gcc from statically optimizing it out, as variable_test_bit makes
  55. * some versions of gcc to think only *(unsigned long*) is used.
  56. */
  57. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  58. static volatile u32 good_insns_32[256 / 32] = {
  59. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  60. /* ---------------------------------------------- */
  61. W(0x00, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) | /* 00 */
  62. W(0x10, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) , /* 10 */
  63. W(0x20, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1) | /* 20 */
  64. W(0x30, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1) , /* 30 */
  65. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  66. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  67. W(0x60, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
  68. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
  69. W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  70. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  71. W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
  72. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  73. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
  74. W(0xd0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  75. W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* e0 */
  76. W(0xf0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
  77. /* ---------------------------------------------- */
  78. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  79. };
  80. #else
  81. #define good_insns_32 NULL
  82. #endif
  83. /* Good-instruction tables for 64-bit apps */
  84. #if defined(CONFIG_X86_64)
  85. static volatile u32 good_insns_64[256 / 32] = {
  86. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  87. /* ---------------------------------------------- */
  88. W(0x00, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) | /* 00 */
  89. W(0x10, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 10 */
  90. W(0x20, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) | /* 20 */
  91. W(0x30, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 30 */
  92. W(0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 40 */
  93. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  94. W(0x60, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
  95. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
  96. W(0x80, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  97. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  98. W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
  99. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  100. W(0xc0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
  101. W(0xd0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  102. W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* e0 */
  103. W(0xf0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
  104. /* ---------------------------------------------- */
  105. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  106. };
  107. #else
  108. #define good_insns_64 NULL
  109. #endif
  110. /* Using this for both 64-bit and 32-bit apps */
  111. static volatile u32 good_2byte_insns[256 / 32] = {
  112. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  113. /* ---------------------------------------------- */
  114. W(0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1) | /* 00 */
  115. W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1) , /* 10 */
  116. W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 20 */
  117. W(0x30, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
  118. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  119. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  120. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 60 */
  121. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
  122. W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  123. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  124. W(0xa0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1) | /* a0 */
  125. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  126. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
  127. W(0xd0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  128. W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* e0 */
  129. W(0xf0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) /* f0 */
  130. /* ---------------------------------------------- */
  131. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  132. };
  133. #undef W
  134. /*
  135. * opcodes we'll probably never support:
  136. *
  137. * 6c-6d, e4-e5, ec-ed - in
  138. * 6e-6f, e6-e7, ee-ef - out
  139. * cc, cd - int3, int
  140. * cf - iret
  141. * d6 - illegal instruction
  142. * f1 - int1/icebp
  143. * f4 - hlt
  144. * fa, fb - cli, sti
  145. * 0f - lar, lsl, syscall, clts, sysret, sysenter, sysexit, invd, wbinvd, ud2
  146. *
  147. * invalid opcodes in 64-bit mode:
  148. *
  149. * 06, 0e, 16, 1e, 27, 2f, 37, 3f, 60-62, 82, c4-c5, d4-d5
  150. * 63 - we support this opcode in x86_64 but not in i386.
  151. *
  152. * opcodes we may need to refine support for:
  153. *
  154. * 0f - 2-byte instructions: For many of these instructions, the validity
  155. * depends on the prefix and/or the reg field. On such instructions, we
  156. * just consider the opcode combination valid if it corresponds to any
  157. * valid instruction.
  158. *
  159. * 8f - Group 1 - only reg = 0 is OK
  160. * c6-c7 - Group 11 - only reg = 0 is OK
  161. * d9-df - fpu insns with some illegal encodings
  162. * f2, f3 - repnz, repz prefixes. These are also the first byte for
  163. * certain floating-point instructions, such as addsd.
  164. *
  165. * fe - Group 4 - only reg = 0 or 1 is OK
  166. * ff - Group 5 - only reg = 0-6 is OK
  167. *
  168. * others -- Do we need to support these?
  169. *
  170. * 0f - (floating-point?) prefetch instructions
  171. * 07, 17, 1f - pop es, pop ss, pop ds
  172. * 26, 2e, 36, 3e - es:, cs:, ss:, ds: segment prefixes --
  173. * but 64 and 65 (fs: and gs:) seem to be used, so we support them
  174. * 67 - addr16 prefix
  175. * ce - into
  176. * f0 - lock prefix
  177. */
  178. /*
  179. * TODO:
  180. * - Where necessary, examine the modrm byte and allow only valid instructions
  181. * in the different Groups and fpu instructions.
  182. */
  183. static bool is_prefix_bad(struct insn *insn)
  184. {
  185. int i;
  186. for (i = 0; i < insn->prefixes.nbytes; i++) {
  187. switch (insn->prefixes.bytes[i]) {
  188. case 0x26: /* INAT_PFX_ES */
  189. case 0x2E: /* INAT_PFX_CS */
  190. case 0x36: /* INAT_PFX_DS */
  191. case 0x3E: /* INAT_PFX_SS */
  192. case 0xF0: /* INAT_PFX_LOCK */
  193. return true;
  194. }
  195. }
  196. return false;
  197. }
  198. static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool x86_64)
  199. {
  200. u32 volatile *good_insns;
  201. insn_init(insn, auprobe->insn, x86_64);
  202. /* has the side-effect of processing the entire instruction */
  203. insn_get_length(insn);
  204. if (WARN_ON_ONCE(!insn_complete(insn)))
  205. return -ENOEXEC;
  206. if (is_prefix_bad(insn))
  207. return -ENOTSUPP;
  208. if (x86_64)
  209. good_insns = good_insns_64;
  210. else
  211. good_insns = good_insns_32;
  212. if (test_bit(OPCODE1(insn), (unsigned long *)good_insns))
  213. return 0;
  214. if (insn->opcode.nbytes == 2) {
  215. if (test_bit(OPCODE2(insn), (unsigned long *)good_2byte_insns))
  216. return 0;
  217. }
  218. return -ENOTSUPP;
  219. }
  220. #ifdef CONFIG_X86_64
  221. static inline bool is_64bit_mm(struct mm_struct *mm)
  222. {
  223. return !config_enabled(CONFIG_IA32_EMULATION) ||
  224. !(mm->context.ia32_compat == TIF_IA32);
  225. }
  226. /*
  227. * If arch_uprobe->insn doesn't use rip-relative addressing, return
  228. * immediately. Otherwise, rewrite the instruction so that it accesses
  229. * its memory operand indirectly through a scratch register. Set
  230. * arch_uprobe->fixups and arch_uprobe->rip_rela_target_address
  231. * accordingly. (The contents of the scratch register will be saved
  232. * before we single-step the modified instruction, and restored
  233. * afterward.)
  234. *
  235. * We do this because a rip-relative instruction can access only a
  236. * relatively small area (+/- 2 GB from the instruction), and the XOL
  237. * area typically lies beyond that area. At least for instructions
  238. * that store to memory, we can't execute the original instruction
  239. * and "fix things up" later, because the misdirected store could be
  240. * disastrous.
  241. *
  242. * Some useful facts about rip-relative instructions:
  243. *
  244. * - There's always a modrm byte.
  245. * - There's never a SIB byte.
  246. * - The displacement is always 4 bytes.
  247. */
  248. static void
  249. handle_riprel_insn(struct arch_uprobe *auprobe, struct insn *insn)
  250. {
  251. u8 *cursor;
  252. u8 reg;
  253. if (!insn_rip_relative(insn))
  254. return;
  255. /*
  256. * insn_rip_relative() would have decoded rex_prefix, modrm.
  257. * Clear REX.b bit (extension of MODRM.rm field):
  258. * we want to encode rax/rcx, not r8/r9.
  259. */
  260. if (insn->rex_prefix.nbytes) {
  261. cursor = auprobe->insn + insn_offset_rex_prefix(insn);
  262. *cursor &= 0xfe; /* Clearing REX.B bit */
  263. }
  264. /*
  265. * Point cursor at the modrm byte. The next 4 bytes are the
  266. * displacement. Beyond the displacement, for some instructions,
  267. * is the immediate operand.
  268. */
  269. cursor = auprobe->insn + insn_offset_modrm(insn);
  270. /*
  271. * Convert from rip-relative addressing to indirect addressing
  272. * via a scratch register. Change the r/m field from 0x5 (%rip)
  273. * to 0x0 (%rax) or 0x1 (%rcx), and squeeze out the offset field.
  274. */
  275. reg = MODRM_REG(insn);
  276. if (reg == 0) {
  277. /*
  278. * The register operand (if any) is either the A register
  279. * (%rax, %eax, etc.) or (if the 0x4 bit is set in the
  280. * REX prefix) %r8. In any case, we know the C register
  281. * is NOT the register operand, so we use %rcx (register
  282. * #1) for the scratch register.
  283. */
  284. auprobe->fixups = UPROBE_FIX_RIP_CX;
  285. /* Change modrm from 00 000 101 to 00 000 001. */
  286. *cursor = 0x1;
  287. } else {
  288. /* Use %rax (register #0) for the scratch register. */
  289. auprobe->fixups = UPROBE_FIX_RIP_AX;
  290. /* Change modrm from 00 xxx 101 to 00 xxx 000 */
  291. *cursor = (reg << 3);
  292. }
  293. /* Target address = address of next instruction + (signed) offset */
  294. auprobe->rip_rela_target_address = (long)insn->length + insn->displacement.value;
  295. /* Displacement field is gone; slide immediate field (if any) over. */
  296. if (insn->immediate.nbytes) {
  297. cursor++;
  298. memmove(cursor, cursor + insn->displacement.nbytes, insn->immediate.nbytes);
  299. }
  300. }
  301. /*
  302. * If we're emulating a rip-relative instruction, save the contents
  303. * of the scratch register and store the target address in that register.
  304. */
  305. static void
  306. pre_xol_rip_insn(struct arch_uprobe *auprobe, struct pt_regs *regs,
  307. struct arch_uprobe_task *autask)
  308. {
  309. if (auprobe->fixups & UPROBE_FIX_RIP_AX) {
  310. autask->saved_scratch_register = regs->ax;
  311. regs->ax = current->utask->vaddr;
  312. regs->ax += auprobe->rip_rela_target_address;
  313. } else if (auprobe->fixups & UPROBE_FIX_RIP_CX) {
  314. autask->saved_scratch_register = regs->cx;
  315. regs->cx = current->utask->vaddr;
  316. regs->cx += auprobe->rip_rela_target_address;
  317. }
  318. }
  319. static void
  320. handle_riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs, long *correction)
  321. {
  322. if (auprobe->fixups & (UPROBE_FIX_RIP_AX | UPROBE_FIX_RIP_CX)) {
  323. struct arch_uprobe_task *autask;
  324. autask = &current->utask->autask;
  325. if (auprobe->fixups & UPROBE_FIX_RIP_AX)
  326. regs->ax = autask->saved_scratch_register;
  327. else
  328. regs->cx = autask->saved_scratch_register;
  329. /*
  330. * The original instruction includes a displacement, and so
  331. * is 4 bytes longer than what we've just single-stepped.
  332. * Caller may need to apply other fixups to handle stuff
  333. * like "jmpq *...(%rip)" and "callq *...(%rip)".
  334. */
  335. if (correction)
  336. *correction += 4;
  337. }
  338. }
  339. #else /* 32-bit: */
  340. static inline bool is_64bit_mm(struct mm_struct *mm)
  341. {
  342. return false;
  343. }
  344. /*
  345. * No RIP-relative addressing on 32-bit
  346. */
  347. static void handle_riprel_insn(struct arch_uprobe *auprobe, struct insn *insn)
  348. {
  349. }
  350. static void pre_xol_rip_insn(struct arch_uprobe *auprobe, struct pt_regs *regs,
  351. struct arch_uprobe_task *autask)
  352. {
  353. }
  354. static void handle_riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs,
  355. long *correction)
  356. {
  357. }
  358. #endif /* CONFIG_X86_64 */
  359. struct uprobe_xol_ops {
  360. bool (*emulate)(struct arch_uprobe *, struct pt_regs *);
  361. int (*pre_xol)(struct arch_uprobe *, struct pt_regs *);
  362. int (*post_xol)(struct arch_uprobe *, struct pt_regs *);
  363. void (*abort)(struct arch_uprobe *, struct pt_regs *);
  364. };
  365. static inline int sizeof_long(void)
  366. {
  367. return is_ia32_task() ? 4 : 8;
  368. }
  369. static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  370. {
  371. pre_xol_rip_insn(auprobe, regs, &current->utask->autask);
  372. return 0;
  373. }
  374. /*
  375. * Adjust the return address pushed by a call insn executed out of line.
  376. */
  377. static int adjust_ret_addr(unsigned long sp, long correction)
  378. {
  379. int rasize = sizeof_long();
  380. long ra;
  381. if (copy_from_user(&ra, (void __user *)sp, rasize))
  382. return -EFAULT;
  383. ra += correction;
  384. if (copy_to_user((void __user *)sp, &ra, rasize))
  385. return -EFAULT;
  386. return 0;
  387. }
  388. static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  389. {
  390. struct uprobe_task *utask = current->utask;
  391. long correction = (long)(utask->vaddr - utask->xol_vaddr);
  392. handle_riprel_post_xol(auprobe, regs, &correction);
  393. if (auprobe->fixups & UPROBE_FIX_IP)
  394. regs->ip += correction;
  395. if (auprobe->fixups & UPROBE_FIX_CALL) {
  396. if (adjust_ret_addr(regs->sp, correction)) {
  397. regs->sp += sizeof_long();
  398. return -ERESTART;
  399. }
  400. }
  401. return 0;
  402. }
  403. static void default_abort_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  404. {
  405. handle_riprel_post_xol(auprobe, regs, NULL);
  406. }
  407. static struct uprobe_xol_ops default_xol_ops = {
  408. .pre_xol = default_pre_xol_op,
  409. .post_xol = default_post_xol_op,
  410. .abort = default_abort_op,
  411. };
  412. static bool branch_is_call(struct arch_uprobe *auprobe)
  413. {
  414. return auprobe->branch.opc1 == 0xe8;
  415. }
  416. #define CASE_COND \
  417. COND(70, 71, XF(OF)) \
  418. COND(72, 73, XF(CF)) \
  419. COND(74, 75, XF(ZF)) \
  420. COND(78, 79, XF(SF)) \
  421. COND(7a, 7b, XF(PF)) \
  422. COND(76, 77, XF(CF) || XF(ZF)) \
  423. COND(7c, 7d, XF(SF) != XF(OF)) \
  424. COND(7e, 7f, XF(ZF) || XF(SF) != XF(OF))
  425. #define COND(op_y, op_n, expr) \
  426. case 0x ## op_y: DO((expr) != 0) \
  427. case 0x ## op_n: DO((expr) == 0)
  428. #define XF(xf) (!!(flags & X86_EFLAGS_ ## xf))
  429. static bool is_cond_jmp_opcode(u8 opcode)
  430. {
  431. switch (opcode) {
  432. #define DO(expr) \
  433. return true;
  434. CASE_COND
  435. #undef DO
  436. default:
  437. return false;
  438. }
  439. }
  440. static bool check_jmp_cond(struct arch_uprobe *auprobe, struct pt_regs *regs)
  441. {
  442. unsigned long flags = regs->flags;
  443. switch (auprobe->branch.opc1) {
  444. #define DO(expr) \
  445. return expr;
  446. CASE_COND
  447. #undef DO
  448. default: /* not a conditional jmp */
  449. return true;
  450. }
  451. }
  452. #undef XF
  453. #undef COND
  454. #undef CASE_COND
  455. static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  456. {
  457. unsigned long new_ip = regs->ip += auprobe->branch.ilen;
  458. unsigned long offs = (long)auprobe->branch.offs;
  459. if (branch_is_call(auprobe)) {
  460. unsigned long new_sp = regs->sp - sizeof_long();
  461. /*
  462. * If it fails we execute this (mangled, see the comment in
  463. * branch_clear_offset) insn out-of-line. In the likely case
  464. * this should trigger the trap, and the probed application
  465. * should die or restart the same insn after it handles the
  466. * signal, arch_uprobe_post_xol() won't be even called.
  467. *
  468. * But there is corner case, see the comment in ->post_xol().
  469. */
  470. if (copy_to_user((void __user *)new_sp, &new_ip, sizeof_long()))
  471. return false;
  472. regs->sp = new_sp;
  473. } else if (!check_jmp_cond(auprobe, regs)) {
  474. offs = 0;
  475. }
  476. regs->ip = new_ip + offs;
  477. return true;
  478. }
  479. static int branch_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  480. {
  481. BUG_ON(!branch_is_call(auprobe));
  482. /*
  483. * We can only get here if branch_emulate_op() failed to push the ret
  484. * address _and_ another thread expanded our stack before the (mangled)
  485. * "call" insn was executed out-of-line. Just restore ->sp and restart.
  486. * We could also restore ->ip and try to call branch_emulate_op() again.
  487. */
  488. regs->sp += sizeof_long();
  489. return -ERESTART;
  490. }
  491. static void branch_clear_offset(struct arch_uprobe *auprobe, struct insn *insn)
  492. {
  493. /*
  494. * Turn this insn into "call 1f; 1:", this is what we will execute
  495. * out-of-line if ->emulate() fails. We only need this to generate
  496. * a trap, so that the probed task receives the correct signal with
  497. * the properly filled siginfo.
  498. *
  499. * But see the comment in ->post_xol(), in the unlikely case it can
  500. * succeed. So we need to ensure that the new ->ip can not fall into
  501. * the non-canonical area and trigger #GP.
  502. *
  503. * We could turn it into (say) "pushf", but then we would need to
  504. * divorce ->insn[] and ->ixol[]. We need to preserve the 1st byte
  505. * of ->insn[] for set_orig_insn().
  506. */
  507. memset(auprobe->insn + insn_offset_immediate(insn),
  508. 0, insn->immediate.nbytes);
  509. }
  510. static struct uprobe_xol_ops branch_xol_ops = {
  511. .emulate = branch_emulate_op,
  512. .post_xol = branch_post_xol_op,
  513. };
  514. /* Returns -ENOSYS if branch_xol_ops doesn't handle this insn */
  515. static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
  516. {
  517. u8 opc1 = OPCODE1(insn);
  518. int i;
  519. switch (opc1) {
  520. case 0xeb: /* jmp 8 */
  521. case 0xe9: /* jmp 32 */
  522. case 0x90: /* prefix* + nop; same as jmp with .offs = 0 */
  523. break;
  524. case 0xe8: /* call relative */
  525. branch_clear_offset(auprobe, insn);
  526. break;
  527. case 0x0f:
  528. if (insn->opcode.nbytes != 2)
  529. return -ENOSYS;
  530. /*
  531. * If it is a "near" conditional jmp, OPCODE2() - 0x10 matches
  532. * OPCODE1() of the "short" jmp which checks the same condition.
  533. */
  534. opc1 = OPCODE2(insn) - 0x10;
  535. default:
  536. if (!is_cond_jmp_opcode(opc1))
  537. return -ENOSYS;
  538. }
  539. /*
  540. * 16-bit overrides such as CALLW (66 e8 nn nn) are not supported.
  541. * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
  542. * No one uses these insns, reject any branch insns with such prefix.
  543. */
  544. for (i = 0; i < insn->prefixes.nbytes; i++) {
  545. if (insn->prefixes.bytes[i] == 0x66)
  546. return -ENOTSUPP;
  547. }
  548. auprobe->branch.opc1 = opc1;
  549. auprobe->branch.ilen = insn->length;
  550. auprobe->branch.offs = insn->immediate.value;
  551. auprobe->ops = &branch_xol_ops;
  552. return 0;
  553. }
  554. /**
  555. * arch_uprobe_analyze_insn - instruction analysis including validity and fixups.
  556. * @mm: the probed address space.
  557. * @arch_uprobe: the probepoint information.
  558. * @addr: virtual address at which to install the probepoint
  559. * Return 0 on success or a -ve number on error.
  560. */
  561. int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long addr)
  562. {
  563. struct insn insn;
  564. bool fix_ip = true, fix_call = false;
  565. int ret;
  566. ret = uprobe_init_insn(auprobe, &insn, is_64bit_mm(mm));
  567. if (ret)
  568. return ret;
  569. ret = branch_setup_xol_ops(auprobe, &insn);
  570. if (ret != -ENOSYS)
  571. return ret;
  572. /*
  573. * Figure out which fixups arch_uprobe_post_xol() will need to perform,
  574. * and annotate arch_uprobe->fixups accordingly. To start with, ->fixups
  575. * is either zero or it reflects rip-related fixups.
  576. */
  577. switch (OPCODE1(&insn)) {
  578. case 0x9d: /* popf */
  579. auprobe->fixups |= UPROBE_FIX_SETF;
  580. break;
  581. case 0xc3: /* ret or lret -- ip is correct */
  582. case 0xcb:
  583. case 0xc2:
  584. case 0xca:
  585. fix_ip = false;
  586. break;
  587. case 0x9a: /* call absolute - Fix return addr, not ip */
  588. fix_call = true;
  589. fix_ip = false;
  590. break;
  591. case 0xea: /* jmp absolute -- ip is correct */
  592. fix_ip = false;
  593. break;
  594. case 0xff:
  595. switch (MODRM_REG(&insn)) {
  596. case 2: case 3: /* call or lcall, indirect */
  597. fix_call = true;
  598. case 4: case 5: /* jmp or ljmp, indirect */
  599. fix_ip = false;
  600. }
  601. /* fall through */
  602. default:
  603. handle_riprel_insn(auprobe, &insn);
  604. }
  605. if (fix_ip)
  606. auprobe->fixups |= UPROBE_FIX_IP;
  607. if (fix_call)
  608. auprobe->fixups |= UPROBE_FIX_CALL;
  609. auprobe->ops = &default_xol_ops;
  610. return 0;
  611. }
  612. /*
  613. * arch_uprobe_pre_xol - prepare to execute out of line.
  614. * @auprobe: the probepoint information.
  615. * @regs: reflects the saved user state of current task.
  616. */
  617. int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  618. {
  619. struct uprobe_task *utask = current->utask;
  620. if (auprobe->ops->pre_xol) {
  621. int err = auprobe->ops->pre_xol(auprobe, regs);
  622. if (err)
  623. return err;
  624. }
  625. regs->ip = utask->xol_vaddr;
  626. utask->autask.saved_trap_nr = current->thread.trap_nr;
  627. current->thread.trap_nr = UPROBE_TRAP_NR;
  628. utask->autask.saved_tf = !!(regs->flags & X86_EFLAGS_TF);
  629. regs->flags |= X86_EFLAGS_TF;
  630. if (test_tsk_thread_flag(current, TIF_BLOCKSTEP))
  631. set_task_blockstep(current, false);
  632. return 0;
  633. }
  634. /*
  635. * If xol insn itself traps and generates a signal(Say,
  636. * SIGILL/SIGSEGV/etc), then detect the case where a singlestepped
  637. * instruction jumps back to its own address. It is assumed that anything
  638. * like do_page_fault/do_trap/etc sets thread.trap_nr != -1.
  639. *
  640. * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
  641. * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
  642. * UPROBE_TRAP_NR == -1 set by arch_uprobe_pre_xol().
  643. */
  644. bool arch_uprobe_xol_was_trapped(struct task_struct *t)
  645. {
  646. if (t->thread.trap_nr != UPROBE_TRAP_NR)
  647. return true;
  648. return false;
  649. }
  650. /*
  651. * Called after single-stepping. To avoid the SMP problems that can
  652. * occur when we temporarily put back the original opcode to
  653. * single-step, we single-stepped a copy of the instruction.
  654. *
  655. * This function prepares to resume execution after the single-step.
  656. * We have to fix things up as follows:
  657. *
  658. * Typically, the new ip is relative to the copied instruction. We need
  659. * to make it relative to the original instruction (FIX_IP). Exceptions
  660. * are return instructions and absolute or indirect jump or call instructions.
  661. *
  662. * If the single-stepped instruction was a call, the return address that
  663. * is atop the stack is the address following the copied instruction. We
  664. * need to make it the address following the original instruction (FIX_CALL).
  665. *
  666. * If the original instruction was a rip-relative instruction such as
  667. * "movl %edx,0xnnnn(%rip)", we have instead executed an equivalent
  668. * instruction using a scratch register -- e.g., "movl %edx,(%rax)".
  669. * We need to restore the contents of the scratch register and adjust
  670. * the ip, keeping in mind that the instruction we executed is 4 bytes
  671. * shorter than the original instruction (since we squeezed out the offset
  672. * field). (FIX_RIP_AX or FIX_RIP_CX)
  673. */
  674. int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  675. {
  676. struct uprobe_task *utask = current->utask;
  677. WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR);
  678. if (auprobe->ops->post_xol) {
  679. int err = auprobe->ops->post_xol(auprobe, regs);
  680. if (err) {
  681. arch_uprobe_abort_xol(auprobe, regs);
  682. /*
  683. * Restart the probed insn. ->post_xol() must ensure
  684. * this is really possible if it returns -ERESTART.
  685. */
  686. if (err == -ERESTART)
  687. return 0;
  688. return err;
  689. }
  690. }
  691. current->thread.trap_nr = utask->autask.saved_trap_nr;
  692. /*
  693. * arch_uprobe_pre_xol() doesn't save the state of TIF_BLOCKSTEP
  694. * so we can get an extra SIGTRAP if we do not clear TF. We need
  695. * to examine the opcode to make it right.
  696. */
  697. if (utask->autask.saved_tf)
  698. send_sig(SIGTRAP, current, 0);
  699. else if (!(auprobe->fixups & UPROBE_FIX_SETF))
  700. regs->flags &= ~X86_EFLAGS_TF;
  701. return 0;
  702. }
  703. /* callback routine for handling exceptions. */
  704. int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data)
  705. {
  706. struct die_args *args = data;
  707. struct pt_regs *regs = args->regs;
  708. int ret = NOTIFY_DONE;
  709. /* We are only interested in userspace traps */
  710. if (regs && !user_mode_vm(regs))
  711. return NOTIFY_DONE;
  712. switch (val) {
  713. case DIE_INT3:
  714. if (uprobe_pre_sstep_notifier(regs))
  715. ret = NOTIFY_STOP;
  716. break;
  717. case DIE_DEBUG:
  718. if (uprobe_post_sstep_notifier(regs))
  719. ret = NOTIFY_STOP;
  720. default:
  721. break;
  722. }
  723. return ret;
  724. }
  725. /*
  726. * This function gets called when XOL instruction either gets trapped or
  727. * the thread has a fatal signal, or if arch_uprobe_post_xol() failed.
  728. * Reset the instruction pointer to its probed address for the potential
  729. * restart or for post mortem analysis.
  730. */
  731. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  732. {
  733. struct uprobe_task *utask = current->utask;
  734. if (auprobe->ops->abort)
  735. auprobe->ops->abort(auprobe, regs);
  736. current->thread.trap_nr = utask->autask.saved_trap_nr;
  737. regs->ip = utask->vaddr;
  738. /* clear TF if it was set by us in arch_uprobe_pre_xol() */
  739. if (!utask->autask.saved_tf)
  740. regs->flags &= ~X86_EFLAGS_TF;
  741. }
  742. static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  743. {
  744. if (auprobe->ops->emulate)
  745. return auprobe->ops->emulate(auprobe, regs);
  746. return false;
  747. }
  748. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  749. {
  750. bool ret = __skip_sstep(auprobe, regs);
  751. if (ret && (regs->flags & X86_EFLAGS_TF))
  752. send_sig(SIGTRAP, current, 0);
  753. return ret;
  754. }
  755. unsigned long
  756. arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs)
  757. {
  758. int rasize = sizeof_long(), nleft;
  759. unsigned long orig_ret_vaddr = 0; /* clear high bits for 32-bit apps */
  760. if (copy_from_user(&orig_ret_vaddr, (void __user *)regs->sp, rasize))
  761. return -1;
  762. /* check whether address has been already hijacked */
  763. if (orig_ret_vaddr == trampoline_vaddr)
  764. return orig_ret_vaddr;
  765. nleft = copy_to_user((void __user *)regs->sp, &trampoline_vaddr, rasize);
  766. if (likely(!nleft))
  767. return orig_ret_vaddr;
  768. if (nleft != rasize) {
  769. pr_err("uprobe: return address clobbered: pid=%d, %%sp=%#lx, "
  770. "%%ip=%#lx\n", current->pid, regs->sp, regs->ip);
  771. force_sig_info(SIGSEGV, SEND_SIG_FORCED, current);
  772. }
  773. return -1;
  774. }