uprobes.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * User-space Probes (UProbes) for x86
  4. *
  5. * Copyright (C) IBM Corporation, 2008-2011
  6. * Authors:
  7. * Srikar Dronamraju
  8. * Jim Keniston
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/uprobes.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/kdebug.h>
  17. #include <asm/processor.h>
  18. #include <asm/insn.h>
  19. #include <asm/mmu_context.h>
  20. /* Post-execution fixups. */
  21. /* Adjust IP back to vicinity of actual insn */
  22. #define UPROBE_FIX_IP 0x01
  23. /* Adjust the return address of a call insn */
  24. #define UPROBE_FIX_CALL 0x02
  25. /* Instruction will modify TF, don't change it */
  26. #define UPROBE_FIX_SETF 0x04
  27. #define UPROBE_FIX_RIP_SI 0x08
  28. #define UPROBE_FIX_RIP_DI 0x10
  29. #define UPROBE_FIX_RIP_BX 0x20
  30. #define UPROBE_FIX_RIP_MASK \
  31. (UPROBE_FIX_RIP_SI | UPROBE_FIX_RIP_DI | UPROBE_FIX_RIP_BX)
  32. #define UPROBE_TRAP_NR UINT_MAX
  33. /* Adaptations for mhiramat x86 decoder v14. */
  34. #define OPCODE1(insn) ((insn)->opcode.bytes[0])
  35. #define OPCODE2(insn) ((insn)->opcode.bytes[1])
  36. #define OPCODE3(insn) ((insn)->opcode.bytes[2])
  37. #define MODRM_REG(insn) X86_MODRM_REG((insn)->modrm.value)
  38. #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
  39. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  40. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  41. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  42. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  43. << (row % 32))
  44. /*
  45. * Good-instruction tables for 32-bit apps. This is non-const and volatile
  46. * to keep gcc from statically optimizing it out, as variable_test_bit makes
  47. * some versions of gcc to think only *(unsigned long*) is used.
  48. *
  49. * Opcodes we'll probably never support:
  50. * 6c-6f - ins,outs. SEGVs if used in userspace
  51. * e4-e7 - in,out imm. SEGVs if used in userspace
  52. * ec-ef - in,out acc. SEGVs if used in userspace
  53. * cc - int3. SIGTRAP if used in userspace
  54. * ce - into. Not used in userspace - no kernel support to make it useful. SEGVs
  55. * (why we support bound (62) then? it's similar, and similarly unused...)
  56. * f1 - int1. SIGTRAP if used in userspace
  57. * f4 - hlt. SEGVs if used in userspace
  58. * fa - cli. SEGVs if used in userspace
  59. * fb - sti. SEGVs if used in userspace
  60. *
  61. * Opcodes which need some work to be supported:
  62. * 07,17,1f - pop es/ss/ds
  63. * Normally not used in userspace, but would execute if used.
  64. * Can cause GP or stack exception if tries to load wrong segment descriptor.
  65. * We hesitate to run them under single step since kernel's handling
  66. * of userspace single-stepping (TF flag) is fragile.
  67. * We can easily refuse to support push es/cs/ss/ds (06/0e/16/1e)
  68. * on the same grounds that they are never used.
  69. * cd - int N.
  70. * Used by userspace for "int 80" syscall entry. (Other "int N"
  71. * cause GP -> SEGV since their IDT gates don't allow calls from CPL 3).
  72. * Not supported since kernel's handling of userspace single-stepping
  73. * (TF flag) is fragile.
  74. * cf - iret. Normally not used in userspace. Doesn't SEGV unless arguments are bad
  75. */
  76. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  77. static volatile u32 good_insns_32[256 / 32] = {
  78. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  79. /* ---------------------------------------------- */
  80. W(0x00, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 00 */
  81. W(0x10, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) , /* 10 */
  82. W(0x20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 20 */
  83. W(0x30, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 30 */
  84. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  85. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  86. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
  87. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
  88. W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
  91. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  92. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
  93. W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  94. W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* e0 */
  95. W(0xf0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
  96. /* ---------------------------------------------- */
  97. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  98. };
  99. #else
  100. #define good_insns_32 NULL
  101. #endif
  102. /* Good-instruction tables for 64-bit apps.
  103. *
  104. * Genuinely invalid opcodes:
  105. * 06,07 - formerly push/pop es
  106. * 0e - formerly push cs
  107. * 16,17 - formerly push/pop ss
  108. * 1e,1f - formerly push/pop ds
  109. * 27,2f,37,3f - formerly daa/das/aaa/aas
  110. * 60,61 - formerly pusha/popa
  111. * 62 - formerly bound. EVEX prefix for AVX512 (not yet supported)
  112. * 82 - formerly redundant encoding of Group1
  113. * 9a - formerly call seg:ofs
  114. * ce - formerly into
  115. * d4,d5 - formerly aam/aad
  116. * d6 - formerly undocumented salc
  117. * ea - formerly jmp seg:ofs
  118. *
  119. * Opcodes we'll probably never support:
  120. * 6c-6f - ins,outs. SEGVs if used in userspace
  121. * e4-e7 - in,out imm. SEGVs if used in userspace
  122. * ec-ef - in,out acc. SEGVs if used in userspace
  123. * cc - int3. SIGTRAP if used in userspace
  124. * f1 - int1. SIGTRAP if used in userspace
  125. * f4 - hlt. SEGVs if used in userspace
  126. * fa - cli. SEGVs if used in userspace
  127. * fb - sti. SEGVs if used in userspace
  128. *
  129. * Opcodes which need some work to be supported:
  130. * cd - int N.
  131. * Used by userspace for "int 80" syscall entry. (Other "int N"
  132. * cause GP -> SEGV since their IDT gates don't allow calls from CPL 3).
  133. * Not supported since kernel's handling of userspace single-stepping
  134. * (TF flag) is fragile.
  135. * cf - iret. Normally not used in userspace. Doesn't SEGV unless arguments are bad
  136. */
  137. #if defined(CONFIG_X86_64)
  138. static volatile u32 good_insns_64[256 / 32] = {
  139. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  140. /* ---------------------------------------------- */
  141. W(0x00, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1) | /* 00 */
  142. W(0x10, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0) , /* 10 */
  143. W(0x20, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) | /* 20 */
  144. W(0x30, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0) , /* 30 */
  145. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  146. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  147. W(0x60, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* 60 */
  148. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 70 */
  149. W(0x80, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  150. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1) , /* 90 */
  151. W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* a0 */
  152. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  153. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | /* c0 */
  154. W(0xd0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  155. W(0xe0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0) | /* e0 */
  156. W(0xf0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1) /* f0 */
  157. /* ---------------------------------------------- */
  158. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  159. };
  160. #else
  161. #define good_insns_64 NULL
  162. #endif
  163. /* Using this for both 64-bit and 32-bit apps.
  164. * Opcodes we don't support:
  165. * 0f 00 - SLDT/STR/LLDT/LTR/VERR/VERW/-/- group. System insns
  166. * 0f 01 - SGDT/SIDT/LGDT/LIDT/SMSW/-/LMSW/INVLPG group.
  167. * Also encodes tons of other system insns if mod=11.
  168. * Some are in fact non-system: xend, xtest, rdtscp, maybe more
  169. * 0f 05 - syscall
  170. * 0f 06 - clts (CPL0 insn)
  171. * 0f 07 - sysret
  172. * 0f 08 - invd (CPL0 insn)
  173. * 0f 09 - wbinvd (CPL0 insn)
  174. * 0f 0b - ud2
  175. * 0f 30 - wrmsr (CPL0 insn) (then why rdmsr is allowed, it's also CPL0 insn?)
  176. * 0f 34 - sysenter
  177. * 0f 35 - sysexit
  178. * 0f 37 - getsec
  179. * 0f 78 - vmread (Intel VMX. CPL0 insn)
  180. * 0f 79 - vmwrite (Intel VMX. CPL0 insn)
  181. * Note: with prefixes, these two opcodes are
  182. * extrq/insertq/AVX512 convert vector ops.
  183. * 0f ae - group15: [f]xsave,[f]xrstor,[v]{ld,st}mxcsr,clflush[opt],
  184. * {rd,wr}{fs,gs}base,{s,l,m}fence.
  185. * Why? They are all user-executable.
  186. */
  187. static volatile u32 good_2byte_insns[256 / 32] = {
  188. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  189. /* ---------------------------------------------- */
  190. W(0x00, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1) | /* 00 */
  191. W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 10 */
  192. W(0x20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 20 */
  193. W(0x30, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) , /* 30 */
  194. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  195. W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 50 */
  196. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 60 */
  197. W(0x70, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1) , /* 70 */
  198. W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
  199. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  200. W(0xa0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1) | /* a0 */
  201. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* b0 */
  202. W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
  203. W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
  204. W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* e0 */
  205. W(0xf0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) /* f0 */
  206. /* ---------------------------------------------- */
  207. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  208. };
  209. #undef W
  210. /*
  211. * opcodes we may need to refine support for:
  212. *
  213. * 0f - 2-byte instructions: For many of these instructions, the validity
  214. * depends on the prefix and/or the reg field. On such instructions, we
  215. * just consider the opcode combination valid if it corresponds to any
  216. * valid instruction.
  217. *
  218. * 8f - Group 1 - only reg = 0 is OK
  219. * c6-c7 - Group 11 - only reg = 0 is OK
  220. * d9-df - fpu insns with some illegal encodings
  221. * f2, f3 - repnz, repz prefixes. These are also the first byte for
  222. * certain floating-point instructions, such as addsd.
  223. *
  224. * fe - Group 4 - only reg = 0 or 1 is OK
  225. * ff - Group 5 - only reg = 0-6 is OK
  226. *
  227. * others -- Do we need to support these?
  228. *
  229. * 0f - (floating-point?) prefetch instructions
  230. * 07, 17, 1f - pop es, pop ss, pop ds
  231. * 26, 2e, 36, 3e - es:, cs:, ss:, ds: segment prefixes --
  232. * but 64 and 65 (fs: and gs:) seem to be used, so we support them
  233. * 67 - addr16 prefix
  234. * ce - into
  235. * f0 - lock prefix
  236. */
  237. /*
  238. * TODO:
  239. * - Where necessary, examine the modrm byte and allow only valid instructions
  240. * in the different Groups and fpu instructions.
  241. */
  242. static bool is_prefix_bad(struct insn *insn)
  243. {
  244. insn_byte_t p;
  245. int i;
  246. for_each_insn_prefix(insn, i, p) {
  247. insn_attr_t attr;
  248. attr = inat_get_opcode_attribute(p);
  249. switch (attr) {
  250. case INAT_MAKE_PREFIX(INAT_PFX_ES):
  251. case INAT_MAKE_PREFIX(INAT_PFX_CS):
  252. case INAT_MAKE_PREFIX(INAT_PFX_DS):
  253. case INAT_MAKE_PREFIX(INAT_PFX_SS):
  254. case INAT_MAKE_PREFIX(INAT_PFX_LOCK):
  255. return true;
  256. }
  257. }
  258. return false;
  259. }
  260. static int uprobe_init_insn(struct arch_uprobe *auprobe, struct insn *insn, bool x86_64)
  261. {
  262. enum insn_mode m = x86_64 ? INSN_MODE_64 : INSN_MODE_32;
  263. u32 volatile *good_insns;
  264. int ret;
  265. ret = insn_decode(insn, auprobe->insn, sizeof(auprobe->insn), m);
  266. if (ret < 0)
  267. return -ENOEXEC;
  268. if (is_prefix_bad(insn))
  269. return -ENOTSUPP;
  270. /* We should not singlestep on the exception masking instructions */
  271. if (insn_masking_exception(insn))
  272. return -ENOTSUPP;
  273. if (x86_64)
  274. good_insns = good_insns_64;
  275. else
  276. good_insns = good_insns_32;
  277. if (test_bit(OPCODE1(insn), (unsigned long *)good_insns))
  278. return 0;
  279. if (insn->opcode.nbytes == 2) {
  280. if (test_bit(OPCODE2(insn), (unsigned long *)good_2byte_insns))
  281. return 0;
  282. }
  283. return -ENOTSUPP;
  284. }
  285. #ifdef CONFIG_X86_64
  286. asm (
  287. ".pushsection .rodata\n"
  288. ".global uretprobe_trampoline_entry\n"
  289. "uretprobe_trampoline_entry:\n"
  290. "pushq %rax\n"
  291. "pushq %rcx\n"
  292. "pushq %r11\n"
  293. "movq $" __stringify(__NR_uretprobe) ", %rax\n"
  294. "syscall\n"
  295. ".global uretprobe_syscall_check\n"
  296. "uretprobe_syscall_check:\n"
  297. "popq %r11\n"
  298. "popq %rcx\n"
  299. /* The uretprobe syscall replaces stored %rax value with final
  300. * return address, so we don't restore %rax in here and just
  301. * call ret.
  302. */
  303. "retq\n"
  304. ".global uretprobe_trampoline_end\n"
  305. "uretprobe_trampoline_end:\n"
  306. ".popsection\n"
  307. );
  308. extern u8 uretprobe_trampoline_entry[];
  309. extern u8 uretprobe_trampoline_end[];
  310. extern u8 uretprobe_syscall_check[];
  311. void *arch_uprobe_trampoline(unsigned long *psize)
  312. {
  313. static uprobe_opcode_t insn = UPROBE_SWBP_INSN;
  314. struct pt_regs *regs = task_pt_regs(current);
  315. /*
  316. * At the moment the uretprobe syscall trampoline is supported
  317. * only for native 64-bit process, the compat process still uses
  318. * standard breakpoint.
  319. */
  320. if (user_64bit_mode(regs)) {
  321. *psize = uretprobe_trampoline_end - uretprobe_trampoline_entry;
  322. return uretprobe_trampoline_entry;
  323. }
  324. *psize = UPROBE_SWBP_INSN_SIZE;
  325. return &insn;
  326. }
  327. static unsigned long trampoline_check_ip(void)
  328. {
  329. unsigned long tramp = uprobe_get_trampoline_vaddr();
  330. return tramp + (uretprobe_syscall_check - uretprobe_trampoline_entry);
  331. }
  332. SYSCALL_DEFINE0(uretprobe)
  333. {
  334. struct pt_regs *regs = task_pt_regs(current);
  335. unsigned long err, ip, sp, r11_cx_ax[3];
  336. if (regs->ip != trampoline_check_ip())
  337. goto sigill;
  338. err = copy_from_user(r11_cx_ax, (void __user *)regs->sp, sizeof(r11_cx_ax));
  339. if (err)
  340. goto sigill;
  341. /* expose the "right" values of r11/cx/ax/sp to uprobe_consumer/s */
  342. regs->r11 = r11_cx_ax[0];
  343. regs->cx = r11_cx_ax[1];
  344. regs->ax = r11_cx_ax[2];
  345. regs->sp += sizeof(r11_cx_ax);
  346. regs->orig_ax = -1;
  347. ip = regs->ip;
  348. sp = regs->sp;
  349. uprobe_handle_trampoline(regs);
  350. /*
  351. * Some of the uprobe consumers has changed sp, we can do nothing,
  352. * just return via iret.
  353. * .. or shadow stack is enabled, in which case we need to skip
  354. * return through the user space stack address.
  355. */
  356. if (regs->sp != sp || shstk_is_enabled())
  357. return regs->ax;
  358. regs->sp -= sizeof(r11_cx_ax);
  359. /* for the case uprobe_consumer has changed r11/cx */
  360. r11_cx_ax[0] = regs->r11;
  361. r11_cx_ax[1] = regs->cx;
  362. /*
  363. * ax register is passed through as return value, so we can use
  364. * its space on stack for ip value and jump to it through the
  365. * trampoline's ret instruction
  366. */
  367. r11_cx_ax[2] = regs->ip;
  368. regs->ip = ip;
  369. err = copy_to_user((void __user *)regs->sp, r11_cx_ax, sizeof(r11_cx_ax));
  370. if (err)
  371. goto sigill;
  372. /* ensure sysret, see do_syscall_64() */
  373. regs->r11 = regs->flags;
  374. regs->cx = regs->ip;
  375. return regs->ax;
  376. sigill:
  377. force_sig(SIGILL);
  378. return -1;
  379. }
  380. /*
  381. * If arch_uprobe->insn doesn't use rip-relative addressing, return
  382. * immediately. Otherwise, rewrite the instruction so that it accesses
  383. * its memory operand indirectly through a scratch register. Set
  384. * defparam->fixups accordingly. (The contents of the scratch register
  385. * will be saved before we single-step the modified instruction,
  386. * and restored afterward).
  387. *
  388. * We do this because a rip-relative instruction can access only a
  389. * relatively small area (+/- 2 GB from the instruction), and the XOL
  390. * area typically lies beyond that area. At least for instructions
  391. * that store to memory, we can't execute the original instruction
  392. * and "fix things up" later, because the misdirected store could be
  393. * disastrous.
  394. *
  395. * Some useful facts about rip-relative instructions:
  396. *
  397. * - There's always a modrm byte with bit layout "00 reg 101".
  398. * - There's never a SIB byte.
  399. * - The displacement is always 4 bytes.
  400. * - REX.B=1 bit in REX prefix, which normally extends r/m field,
  401. * has no effect on rip-relative mode. It doesn't make modrm byte
  402. * with r/m=101 refer to register 1101 = R13.
  403. */
  404. static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn)
  405. {
  406. u8 *cursor;
  407. u8 reg;
  408. u8 reg2;
  409. if (!insn_rip_relative(insn))
  410. return;
  411. /*
  412. * insn_rip_relative() would have decoded rex_prefix, vex_prefix, modrm.
  413. * Clear REX.b bit (extension of MODRM.rm field):
  414. * we want to encode low numbered reg, not r8+.
  415. */
  416. if (insn->rex_prefix.nbytes) {
  417. cursor = auprobe->insn + insn_offset_rex_prefix(insn);
  418. /* REX byte has 0100wrxb layout, clearing REX.b bit */
  419. *cursor &= 0xfe;
  420. }
  421. /*
  422. * Similar treatment for VEX3/EVEX prefix.
  423. * TODO: add XOP treatment when insn decoder supports them
  424. */
  425. if (insn->vex_prefix.nbytes >= 3) {
  426. /*
  427. * vex2: c5 rvvvvLpp (has no b bit)
  428. * vex3/xop: c4/8f rxbmmmmm wvvvvLpp
  429. * evex: 62 rxbR00mm wvvvv1pp zllBVaaa
  430. * Setting VEX3.b (setting because it has inverted meaning).
  431. * Setting EVEX.x since (in non-SIB encoding) EVEX.x
  432. * is the 4th bit of MODRM.rm, and needs the same treatment.
  433. * For VEX3-encoded insns, VEX3.x value has no effect in
  434. * non-SIB encoding, the change is superfluous but harmless.
  435. */
  436. cursor = auprobe->insn + insn_offset_vex_prefix(insn) + 1;
  437. *cursor |= 0x60;
  438. }
  439. /*
  440. * Convert from rip-relative addressing to register-relative addressing
  441. * via a scratch register.
  442. *
  443. * This is tricky since there are insns with modrm byte
  444. * which also use registers not encoded in modrm byte:
  445. * [i]div/[i]mul: implicitly use dx:ax
  446. * shift ops: implicitly use cx
  447. * cmpxchg: implicitly uses ax
  448. * cmpxchg8/16b: implicitly uses dx:ax and bx:cx
  449. * Encoding: 0f c7/1 modrm
  450. * The code below thinks that reg=1 (cx), chooses si as scratch.
  451. * mulx: implicitly uses dx: mulx r/m,r1,r2 does r1:r2 = dx * r/m.
  452. * First appeared in Haswell (BMI2 insn). It is vex-encoded.
  453. * Example where none of bx,cx,dx can be used as scratch reg:
  454. * c4 e2 63 f6 0d disp32 mulx disp32(%rip),%ebx,%ecx
  455. * [v]pcmpistri: implicitly uses cx, xmm0
  456. * [v]pcmpistrm: implicitly uses xmm0
  457. * [v]pcmpestri: implicitly uses ax, dx, cx, xmm0
  458. * [v]pcmpestrm: implicitly uses ax, dx, xmm0
  459. * Evil SSE4.2 string comparison ops from hell.
  460. * maskmovq/[v]maskmovdqu: implicitly uses (ds:rdi) as destination.
  461. * Encoding: 0f f7 modrm, 66 0f f7 modrm, vex-encoded: c5 f9 f7 modrm.
  462. * Store op1, byte-masked by op2 msb's in each byte, to (ds:rdi).
  463. * AMD says it has no 3-operand form (vex.vvvv must be 1111)
  464. * and that it can have only register operands, not mem
  465. * (its modrm byte must have mode=11).
  466. * If these restrictions will ever be lifted,
  467. * we'll need code to prevent selection of di as scratch reg!
  468. *
  469. * Summary: I don't know any insns with modrm byte which
  470. * use SI register implicitly. DI register is used only
  471. * by one insn (maskmovq) and BX register is used
  472. * only by one too (cmpxchg8b).
  473. * BP is stack-segment based (may be a problem?).
  474. * AX, DX, CX are off-limits (many implicit users).
  475. * SP is unusable (it's stack pointer - think about "pop mem";
  476. * also, rsp+disp32 needs sib encoding -> insn length change).
  477. */
  478. reg = MODRM_REG(insn); /* Fetch modrm.reg */
  479. reg2 = 0xff; /* Fetch vex.vvvv */
  480. if (insn->vex_prefix.nbytes)
  481. reg2 = insn->vex_prefix.bytes[2];
  482. /*
  483. * TODO: add XOP vvvv reading.
  484. *
  485. * vex.vvvv field is in bits 6-3, bits are inverted.
  486. * But in 32-bit mode, high-order bit may be ignored.
  487. * Therefore, let's consider only 3 low-order bits.
  488. */
  489. reg2 = ((reg2 >> 3) & 0x7) ^ 0x7;
  490. /*
  491. * Register numbering is ax,cx,dx,bx, sp,bp,si,di, r8..r15.
  492. *
  493. * Choose scratch reg. Order is important: must not select bx
  494. * if we can use si (cmpxchg8b case!)
  495. */
  496. if (reg != 6 && reg2 != 6) {
  497. reg2 = 6;
  498. auprobe->defparam.fixups |= UPROBE_FIX_RIP_SI;
  499. } else if (reg != 7 && reg2 != 7) {
  500. reg2 = 7;
  501. auprobe->defparam.fixups |= UPROBE_FIX_RIP_DI;
  502. /* TODO (paranoia): force maskmovq to not use di */
  503. } else {
  504. reg2 = 3;
  505. auprobe->defparam.fixups |= UPROBE_FIX_RIP_BX;
  506. }
  507. /*
  508. * Point cursor at the modrm byte. The next 4 bytes are the
  509. * displacement. Beyond the displacement, for some instructions,
  510. * is the immediate operand.
  511. */
  512. cursor = auprobe->insn + insn_offset_modrm(insn);
  513. /*
  514. * Change modrm from "00 reg 101" to "10 reg reg2". Example:
  515. * 89 05 disp32 mov %eax,disp32(%rip) becomes
  516. * 89 86 disp32 mov %eax,disp32(%rsi)
  517. */
  518. *cursor = 0x80 | (reg << 3) | reg2;
  519. }
  520. static inline unsigned long *
  521. scratch_reg(struct arch_uprobe *auprobe, struct pt_regs *regs)
  522. {
  523. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_SI)
  524. return &regs->si;
  525. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_DI)
  526. return &regs->di;
  527. return &regs->bx;
  528. }
  529. /*
  530. * If we're emulating a rip-relative instruction, save the contents
  531. * of the scratch register and store the target address in that register.
  532. */
  533. static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  534. {
  535. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) {
  536. struct uprobe_task *utask = current->utask;
  537. unsigned long *sr = scratch_reg(auprobe, regs);
  538. utask->autask.saved_scratch_register = *sr;
  539. *sr = utask->vaddr + auprobe->defparam.ilen;
  540. }
  541. }
  542. static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  543. {
  544. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) {
  545. struct uprobe_task *utask = current->utask;
  546. unsigned long *sr = scratch_reg(auprobe, regs);
  547. *sr = utask->autask.saved_scratch_register;
  548. }
  549. }
  550. #else /* 32-bit: */
  551. /*
  552. * No RIP-relative addressing on 32-bit
  553. */
  554. static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn)
  555. {
  556. }
  557. static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  558. {
  559. }
  560. static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  561. {
  562. }
  563. #endif /* CONFIG_X86_64 */
  564. struct uprobe_xol_ops {
  565. bool (*emulate)(struct arch_uprobe *, struct pt_regs *);
  566. int (*pre_xol)(struct arch_uprobe *, struct pt_regs *);
  567. int (*post_xol)(struct arch_uprobe *, struct pt_regs *);
  568. void (*abort)(struct arch_uprobe *, struct pt_regs *);
  569. };
  570. static inline int sizeof_long(struct pt_regs *regs)
  571. {
  572. /*
  573. * Check registers for mode as in_xxx_syscall() does not apply here.
  574. */
  575. return user_64bit_mode(regs) ? 8 : 4;
  576. }
  577. static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  578. {
  579. riprel_pre_xol(auprobe, regs);
  580. return 0;
  581. }
  582. static int emulate_push_stack(struct pt_regs *regs, unsigned long val)
  583. {
  584. unsigned long new_sp = regs->sp - sizeof_long(regs);
  585. if (copy_to_user((void __user *)new_sp, &val, sizeof_long(regs)))
  586. return -EFAULT;
  587. regs->sp = new_sp;
  588. return 0;
  589. }
  590. /*
  591. * We have to fix things up as follows:
  592. *
  593. * Typically, the new ip is relative to the copied instruction. We need
  594. * to make it relative to the original instruction (FIX_IP). Exceptions
  595. * are return instructions and absolute or indirect jump or call instructions.
  596. *
  597. * If the single-stepped instruction was a call, the return address that
  598. * is atop the stack is the address following the copied instruction. We
  599. * need to make it the address following the original instruction (FIX_CALL).
  600. *
  601. * If the original instruction was a rip-relative instruction such as
  602. * "movl %edx,0xnnnn(%rip)", we have instead executed an equivalent
  603. * instruction using a scratch register -- e.g., "movl %edx,0xnnnn(%rsi)".
  604. * We need to restore the contents of the scratch register
  605. * (FIX_RIP_reg).
  606. */
  607. static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  608. {
  609. struct uprobe_task *utask = current->utask;
  610. riprel_post_xol(auprobe, regs);
  611. if (auprobe->defparam.fixups & UPROBE_FIX_IP) {
  612. long correction = utask->vaddr - utask->xol_vaddr;
  613. regs->ip += correction;
  614. } else if (auprobe->defparam.fixups & UPROBE_FIX_CALL) {
  615. regs->sp += sizeof_long(regs); /* Pop incorrect return address */
  616. if (emulate_push_stack(regs, utask->vaddr + auprobe->defparam.ilen))
  617. return -ERESTART;
  618. }
  619. /* popf; tell the caller to not touch TF */
  620. if (auprobe->defparam.fixups & UPROBE_FIX_SETF)
  621. utask->autask.saved_tf = true;
  622. return 0;
  623. }
  624. static void default_abort_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  625. {
  626. riprel_post_xol(auprobe, regs);
  627. }
  628. static const struct uprobe_xol_ops default_xol_ops = {
  629. .pre_xol = default_pre_xol_op,
  630. .post_xol = default_post_xol_op,
  631. .abort = default_abort_op,
  632. };
  633. static bool branch_is_call(struct arch_uprobe *auprobe)
  634. {
  635. return auprobe->branch.opc1 == 0xe8;
  636. }
  637. #define CASE_COND \
  638. COND(70, 71, XF(OF)) \
  639. COND(72, 73, XF(CF)) \
  640. COND(74, 75, XF(ZF)) \
  641. COND(78, 79, XF(SF)) \
  642. COND(7a, 7b, XF(PF)) \
  643. COND(76, 77, XF(CF) || XF(ZF)) \
  644. COND(7c, 7d, XF(SF) != XF(OF)) \
  645. COND(7e, 7f, XF(ZF) || XF(SF) != XF(OF))
  646. #define COND(op_y, op_n, expr) \
  647. case 0x ## op_y: DO((expr) != 0) \
  648. case 0x ## op_n: DO((expr) == 0)
  649. #define XF(xf) (!!(flags & X86_EFLAGS_ ## xf))
  650. static bool is_cond_jmp_opcode(u8 opcode)
  651. {
  652. switch (opcode) {
  653. #define DO(expr) \
  654. return true;
  655. CASE_COND
  656. #undef DO
  657. default:
  658. return false;
  659. }
  660. }
  661. static bool check_jmp_cond(struct arch_uprobe *auprobe, struct pt_regs *regs)
  662. {
  663. unsigned long flags = regs->flags;
  664. switch (auprobe->branch.opc1) {
  665. #define DO(expr) \
  666. return expr;
  667. CASE_COND
  668. #undef DO
  669. default: /* not a conditional jmp */
  670. return true;
  671. }
  672. }
  673. #undef XF
  674. #undef COND
  675. #undef CASE_COND
  676. static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  677. {
  678. unsigned long new_ip = regs->ip += auprobe->branch.ilen;
  679. unsigned long offs = (long)auprobe->branch.offs;
  680. if (branch_is_call(auprobe)) {
  681. /*
  682. * If it fails we execute this (mangled, see the comment in
  683. * branch_clear_offset) insn out-of-line. In the likely case
  684. * this should trigger the trap, and the probed application
  685. * should die or restart the same insn after it handles the
  686. * signal, arch_uprobe_post_xol() won't be even called.
  687. *
  688. * But there is corner case, see the comment in ->post_xol().
  689. */
  690. if (emulate_push_stack(regs, new_ip))
  691. return false;
  692. } else if (!check_jmp_cond(auprobe, regs)) {
  693. offs = 0;
  694. }
  695. regs->ip = new_ip + offs;
  696. return true;
  697. }
  698. static bool push_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  699. {
  700. unsigned long *src_ptr = (void *)regs + auprobe->push.reg_offset;
  701. if (emulate_push_stack(regs, *src_ptr))
  702. return false;
  703. regs->ip += auprobe->push.ilen;
  704. return true;
  705. }
  706. static int branch_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  707. {
  708. BUG_ON(!branch_is_call(auprobe));
  709. /*
  710. * We can only get here if branch_emulate_op() failed to push the ret
  711. * address _and_ another thread expanded our stack before the (mangled)
  712. * "call" insn was executed out-of-line. Just restore ->sp and restart.
  713. * We could also restore ->ip and try to call branch_emulate_op() again.
  714. */
  715. regs->sp += sizeof_long(regs);
  716. return -ERESTART;
  717. }
  718. static void branch_clear_offset(struct arch_uprobe *auprobe, struct insn *insn)
  719. {
  720. /*
  721. * Turn this insn into "call 1f; 1:", this is what we will execute
  722. * out-of-line if ->emulate() fails. We only need this to generate
  723. * a trap, so that the probed task receives the correct signal with
  724. * the properly filled siginfo.
  725. *
  726. * But see the comment in ->post_xol(), in the unlikely case it can
  727. * succeed. So we need to ensure that the new ->ip can not fall into
  728. * the non-canonical area and trigger #GP.
  729. *
  730. * We could turn it into (say) "pushf", but then we would need to
  731. * divorce ->insn[] and ->ixol[]. We need to preserve the 1st byte
  732. * of ->insn[] for set_orig_insn().
  733. */
  734. memset(auprobe->insn + insn_offset_immediate(insn),
  735. 0, insn->immediate.nbytes);
  736. }
  737. static const struct uprobe_xol_ops branch_xol_ops = {
  738. .emulate = branch_emulate_op,
  739. .post_xol = branch_post_xol_op,
  740. };
  741. static const struct uprobe_xol_ops push_xol_ops = {
  742. .emulate = push_emulate_op,
  743. };
  744. /* Returns -ENOSYS if branch_xol_ops doesn't handle this insn */
  745. static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
  746. {
  747. u8 opc1 = OPCODE1(insn);
  748. insn_byte_t p;
  749. int i;
  750. switch (opc1) {
  751. case 0xeb: /* jmp 8 */
  752. case 0xe9: /* jmp 32 */
  753. break;
  754. case 0x90: /* prefix* + nop; same as jmp with .offs = 0 */
  755. goto setup;
  756. case 0xe8: /* call relative */
  757. branch_clear_offset(auprobe, insn);
  758. break;
  759. case 0x0f:
  760. if (insn->opcode.nbytes != 2)
  761. return -ENOSYS;
  762. /*
  763. * If it is a "near" conditional jmp, OPCODE2() - 0x10 matches
  764. * OPCODE1() of the "short" jmp which checks the same condition.
  765. */
  766. opc1 = OPCODE2(insn) - 0x10;
  767. fallthrough;
  768. default:
  769. if (!is_cond_jmp_opcode(opc1))
  770. return -ENOSYS;
  771. }
  772. /*
  773. * 16-bit overrides such as CALLW (66 e8 nn nn) are not supported.
  774. * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
  775. * No one uses these insns, reject any branch insns with such prefix.
  776. */
  777. for_each_insn_prefix(insn, i, p) {
  778. if (p == 0x66)
  779. return -ENOTSUPP;
  780. }
  781. setup:
  782. auprobe->branch.opc1 = opc1;
  783. auprobe->branch.ilen = insn->length;
  784. auprobe->branch.offs = insn->immediate.value;
  785. auprobe->ops = &branch_xol_ops;
  786. return 0;
  787. }
  788. /* Returns -ENOSYS if push_xol_ops doesn't handle this insn */
  789. static int push_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
  790. {
  791. u8 opc1 = OPCODE1(insn), reg_offset = 0;
  792. if (opc1 < 0x50 || opc1 > 0x57)
  793. return -ENOSYS;
  794. if (insn->length > 2)
  795. return -ENOSYS;
  796. if (insn->length == 2) {
  797. /* only support rex_prefix 0x41 (x64 only) */
  798. #ifdef CONFIG_X86_64
  799. if (insn->rex_prefix.nbytes != 1 ||
  800. insn->rex_prefix.bytes[0] != 0x41)
  801. return -ENOSYS;
  802. switch (opc1) {
  803. case 0x50:
  804. reg_offset = offsetof(struct pt_regs, r8);
  805. break;
  806. case 0x51:
  807. reg_offset = offsetof(struct pt_regs, r9);
  808. break;
  809. case 0x52:
  810. reg_offset = offsetof(struct pt_regs, r10);
  811. break;
  812. case 0x53:
  813. reg_offset = offsetof(struct pt_regs, r11);
  814. break;
  815. case 0x54:
  816. reg_offset = offsetof(struct pt_regs, r12);
  817. break;
  818. case 0x55:
  819. reg_offset = offsetof(struct pt_regs, r13);
  820. break;
  821. case 0x56:
  822. reg_offset = offsetof(struct pt_regs, r14);
  823. break;
  824. case 0x57:
  825. reg_offset = offsetof(struct pt_regs, r15);
  826. break;
  827. }
  828. #else
  829. return -ENOSYS;
  830. #endif
  831. } else {
  832. switch (opc1) {
  833. case 0x50:
  834. reg_offset = offsetof(struct pt_regs, ax);
  835. break;
  836. case 0x51:
  837. reg_offset = offsetof(struct pt_regs, cx);
  838. break;
  839. case 0x52:
  840. reg_offset = offsetof(struct pt_regs, dx);
  841. break;
  842. case 0x53:
  843. reg_offset = offsetof(struct pt_regs, bx);
  844. break;
  845. case 0x54:
  846. reg_offset = offsetof(struct pt_regs, sp);
  847. break;
  848. case 0x55:
  849. reg_offset = offsetof(struct pt_regs, bp);
  850. break;
  851. case 0x56:
  852. reg_offset = offsetof(struct pt_regs, si);
  853. break;
  854. case 0x57:
  855. reg_offset = offsetof(struct pt_regs, di);
  856. break;
  857. }
  858. }
  859. auprobe->push.reg_offset = reg_offset;
  860. auprobe->push.ilen = insn->length;
  861. auprobe->ops = &push_xol_ops;
  862. return 0;
  863. }
  864. /**
  865. * arch_uprobe_analyze_insn - instruction analysis including validity and fixups.
  866. * @auprobe: the probepoint information.
  867. * @mm: the probed address space.
  868. * @addr: virtual address at which to install the probepoint
  869. * Return 0 on success or a -ve number on error.
  870. */
  871. int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long addr)
  872. {
  873. struct insn insn;
  874. u8 fix_ip_or_call = UPROBE_FIX_IP;
  875. int ret;
  876. ret = uprobe_init_insn(auprobe, &insn, is_64bit_mm(mm));
  877. if (ret)
  878. return ret;
  879. ret = branch_setup_xol_ops(auprobe, &insn);
  880. if (ret != -ENOSYS)
  881. return ret;
  882. ret = push_setup_xol_ops(auprobe, &insn);
  883. if (ret != -ENOSYS)
  884. return ret;
  885. /*
  886. * Figure out which fixups default_post_xol_op() will need to perform,
  887. * and annotate defparam->fixups accordingly.
  888. */
  889. switch (OPCODE1(&insn)) {
  890. case 0x9d: /* popf */
  891. auprobe->defparam.fixups |= UPROBE_FIX_SETF;
  892. break;
  893. case 0xc3: /* ret or lret -- ip is correct */
  894. case 0xcb:
  895. case 0xc2:
  896. case 0xca:
  897. case 0xea: /* jmp absolute -- ip is correct */
  898. fix_ip_or_call = 0;
  899. break;
  900. case 0x9a: /* call absolute - Fix return addr, not ip */
  901. fix_ip_or_call = UPROBE_FIX_CALL;
  902. break;
  903. case 0xff:
  904. switch (MODRM_REG(&insn)) {
  905. case 2: case 3: /* call or lcall, indirect */
  906. fix_ip_or_call = UPROBE_FIX_CALL;
  907. break;
  908. case 4: case 5: /* jmp or ljmp, indirect */
  909. fix_ip_or_call = 0;
  910. break;
  911. }
  912. fallthrough;
  913. default:
  914. riprel_analyze(auprobe, &insn);
  915. }
  916. auprobe->defparam.ilen = insn.length;
  917. auprobe->defparam.fixups |= fix_ip_or_call;
  918. auprobe->ops = &default_xol_ops;
  919. return 0;
  920. }
  921. /*
  922. * arch_uprobe_pre_xol - prepare to execute out of line.
  923. * @auprobe: the probepoint information.
  924. * @regs: reflects the saved user state of current task.
  925. */
  926. int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  927. {
  928. struct uprobe_task *utask = current->utask;
  929. if (auprobe->ops->pre_xol) {
  930. int err = auprobe->ops->pre_xol(auprobe, regs);
  931. if (err)
  932. return err;
  933. }
  934. regs->ip = utask->xol_vaddr;
  935. utask->autask.saved_trap_nr = current->thread.trap_nr;
  936. current->thread.trap_nr = UPROBE_TRAP_NR;
  937. utask->autask.saved_tf = !!(regs->flags & X86_EFLAGS_TF);
  938. regs->flags |= X86_EFLAGS_TF;
  939. if (test_tsk_thread_flag(current, TIF_BLOCKSTEP))
  940. set_task_blockstep(current, false);
  941. return 0;
  942. }
  943. /*
  944. * If xol insn itself traps and generates a signal(Say,
  945. * SIGILL/SIGSEGV/etc), then detect the case where a singlestepped
  946. * instruction jumps back to its own address. It is assumed that anything
  947. * like do_page_fault/do_trap/etc sets thread.trap_nr != -1.
  948. *
  949. * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
  950. * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
  951. * UPROBE_TRAP_NR == -1 set by arch_uprobe_pre_xol().
  952. */
  953. bool arch_uprobe_xol_was_trapped(struct task_struct *t)
  954. {
  955. if (t->thread.trap_nr != UPROBE_TRAP_NR)
  956. return true;
  957. return false;
  958. }
  959. /*
  960. * Called after single-stepping. To avoid the SMP problems that can
  961. * occur when we temporarily put back the original opcode to
  962. * single-step, we single-stepped a copy of the instruction.
  963. *
  964. * This function prepares to resume execution after the single-step.
  965. */
  966. int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  967. {
  968. struct uprobe_task *utask = current->utask;
  969. bool send_sigtrap = utask->autask.saved_tf;
  970. int err = 0;
  971. WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR);
  972. current->thread.trap_nr = utask->autask.saved_trap_nr;
  973. if (auprobe->ops->post_xol) {
  974. err = auprobe->ops->post_xol(auprobe, regs);
  975. if (err) {
  976. /*
  977. * Restore ->ip for restart or post mortem analysis.
  978. * ->post_xol() must not return -ERESTART unless this
  979. * is really possible.
  980. */
  981. regs->ip = utask->vaddr;
  982. if (err == -ERESTART)
  983. err = 0;
  984. send_sigtrap = false;
  985. }
  986. }
  987. /*
  988. * arch_uprobe_pre_xol() doesn't save the state of TIF_BLOCKSTEP
  989. * so we can get an extra SIGTRAP if we do not clear TF. We need
  990. * to examine the opcode to make it right.
  991. */
  992. if (send_sigtrap)
  993. send_sig(SIGTRAP, current, 0);
  994. if (!utask->autask.saved_tf)
  995. regs->flags &= ~X86_EFLAGS_TF;
  996. return err;
  997. }
  998. /* callback routine for handling exceptions. */
  999. int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data)
  1000. {
  1001. struct die_args *args = data;
  1002. struct pt_regs *regs = args->regs;
  1003. int ret = NOTIFY_DONE;
  1004. /* We are only interested in userspace traps */
  1005. if (regs && !user_mode(regs))
  1006. return NOTIFY_DONE;
  1007. switch (val) {
  1008. case DIE_INT3:
  1009. if (uprobe_pre_sstep_notifier(regs))
  1010. ret = NOTIFY_STOP;
  1011. break;
  1012. case DIE_DEBUG:
  1013. if (uprobe_post_sstep_notifier(regs))
  1014. ret = NOTIFY_STOP;
  1015. break;
  1016. default:
  1017. break;
  1018. }
  1019. return ret;
  1020. }
  1021. /*
  1022. * This function gets called when XOL instruction either gets trapped or
  1023. * the thread has a fatal signal. Reset the instruction pointer to its
  1024. * probed address for the potential restart or for post mortem analysis.
  1025. */
  1026. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  1027. {
  1028. struct uprobe_task *utask = current->utask;
  1029. if (auprobe->ops->abort)
  1030. auprobe->ops->abort(auprobe, regs);
  1031. current->thread.trap_nr = utask->autask.saved_trap_nr;
  1032. regs->ip = utask->vaddr;
  1033. /* clear TF if it was set by us in arch_uprobe_pre_xol() */
  1034. if (!utask->autask.saved_tf)
  1035. regs->flags &= ~X86_EFLAGS_TF;
  1036. }
  1037. static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  1038. {
  1039. if (auprobe->ops->emulate)
  1040. return auprobe->ops->emulate(auprobe, regs);
  1041. return false;
  1042. }
  1043. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  1044. {
  1045. bool ret = __skip_sstep(auprobe, regs);
  1046. if (ret && (regs->flags & X86_EFLAGS_TF))
  1047. send_sig(SIGTRAP, current, 0);
  1048. return ret;
  1049. }
  1050. unsigned long
  1051. arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs)
  1052. {
  1053. int rasize = sizeof_long(regs), nleft;
  1054. unsigned long orig_ret_vaddr = 0; /* clear high bits for 32-bit apps */
  1055. if (copy_from_user(&orig_ret_vaddr, (void __user *)regs->sp, rasize))
  1056. return -1;
  1057. /* check whether address has been already hijacked */
  1058. if (orig_ret_vaddr == trampoline_vaddr)
  1059. return orig_ret_vaddr;
  1060. nleft = copy_to_user((void __user *)regs->sp, &trampoline_vaddr, rasize);
  1061. if (likely(!nleft)) {
  1062. if (shstk_update_last_frame(trampoline_vaddr)) {
  1063. force_sig(SIGSEGV);
  1064. return -1;
  1065. }
  1066. return orig_ret_vaddr;
  1067. }
  1068. if (nleft != rasize) {
  1069. pr_err("return address clobbered: pid=%d, %%sp=%#lx, %%ip=%#lx\n",
  1070. current->pid, regs->sp, regs->ip);
  1071. force_sig(SIGSEGV);
  1072. }
  1073. return -1;
  1074. }
  1075. bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
  1076. struct pt_regs *regs)
  1077. {
  1078. if (ctx == RP_CHECK_CALL) /* sp was just decremented by "call" insn */
  1079. return regs->sp < ret->stack;
  1080. else
  1081. return regs->sp <= ret->stack;
  1082. }