uprobes.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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(unsigned long tramp)
  328. {
  329. return tramp + (uretprobe_syscall_check - uretprobe_trampoline_entry);
  330. }
  331. SYSCALL_DEFINE0(uretprobe)
  332. {
  333. struct pt_regs *regs = task_pt_regs(current);
  334. unsigned long err, ip, sp, r11_cx_ax[3], tramp;
  335. /* If there's no trampoline, we are called from wrong place. */
  336. tramp = uprobe_get_trampoline_vaddr();
  337. if (unlikely(tramp == UPROBE_NO_TRAMPOLINE_VADDR))
  338. goto sigill;
  339. /* Make sure the ip matches the only allowed sys_uretprobe caller. */
  340. if (unlikely(regs->ip != trampoline_check_ip(tramp)))
  341. goto sigill;
  342. err = copy_from_user(r11_cx_ax, (void __user *)regs->sp, sizeof(r11_cx_ax));
  343. if (err)
  344. goto sigill;
  345. /* expose the "right" values of r11/cx/ax/sp to uprobe_consumer/s */
  346. regs->r11 = r11_cx_ax[0];
  347. regs->cx = r11_cx_ax[1];
  348. regs->ax = r11_cx_ax[2];
  349. regs->sp += sizeof(r11_cx_ax);
  350. regs->orig_ax = -1;
  351. ip = regs->ip;
  352. sp = regs->sp;
  353. uprobe_handle_trampoline(regs);
  354. /*
  355. * Some of the uprobe consumers has changed sp, we can do nothing,
  356. * just return via iret.
  357. * .. or shadow stack is enabled, in which case we need to skip
  358. * return through the user space stack address.
  359. */
  360. if (regs->sp != sp || shstk_is_enabled())
  361. return regs->ax;
  362. regs->sp -= sizeof(r11_cx_ax);
  363. /* for the case uprobe_consumer has changed r11/cx */
  364. r11_cx_ax[0] = regs->r11;
  365. r11_cx_ax[1] = regs->cx;
  366. /*
  367. * ax register is passed through as return value, so we can use
  368. * its space on stack for ip value and jump to it through the
  369. * trampoline's ret instruction
  370. */
  371. r11_cx_ax[2] = regs->ip;
  372. regs->ip = ip;
  373. err = copy_to_user((void __user *)regs->sp, r11_cx_ax, sizeof(r11_cx_ax));
  374. if (err)
  375. goto sigill;
  376. /* ensure sysret, see do_syscall_64() */
  377. regs->r11 = regs->flags;
  378. regs->cx = regs->ip;
  379. return regs->ax;
  380. sigill:
  381. force_sig(SIGILL);
  382. return -1;
  383. }
  384. /*
  385. * If arch_uprobe->insn doesn't use rip-relative addressing, return
  386. * immediately. Otherwise, rewrite the instruction so that it accesses
  387. * its memory operand indirectly through a scratch register. Set
  388. * defparam->fixups accordingly. (The contents of the scratch register
  389. * will be saved before we single-step the modified instruction,
  390. * and restored afterward).
  391. *
  392. * We do this because a rip-relative instruction can access only a
  393. * relatively small area (+/- 2 GB from the instruction), and the XOL
  394. * area typically lies beyond that area. At least for instructions
  395. * that store to memory, we can't execute the original instruction
  396. * and "fix things up" later, because the misdirected store could be
  397. * disastrous.
  398. *
  399. * Some useful facts about rip-relative instructions:
  400. *
  401. * - There's always a modrm byte with bit layout "00 reg 101".
  402. * - There's never a SIB byte.
  403. * - The displacement is always 4 bytes.
  404. * - REX.B=1 bit in REX prefix, which normally extends r/m field,
  405. * has no effect on rip-relative mode. It doesn't make modrm byte
  406. * with r/m=101 refer to register 1101 = R13.
  407. */
  408. static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn)
  409. {
  410. u8 *cursor;
  411. u8 reg;
  412. u8 reg2;
  413. if (!insn_rip_relative(insn))
  414. return;
  415. /*
  416. * insn_rip_relative() would have decoded rex_prefix, vex_prefix, modrm.
  417. * Clear REX.b bit (extension of MODRM.rm field):
  418. * we want to encode low numbered reg, not r8+.
  419. */
  420. if (insn->rex_prefix.nbytes) {
  421. cursor = auprobe->insn + insn_offset_rex_prefix(insn);
  422. /* REX byte has 0100wrxb layout, clearing REX.b bit */
  423. *cursor &= 0xfe;
  424. }
  425. /*
  426. * Similar treatment for VEX3/EVEX prefix.
  427. * TODO: add XOP treatment when insn decoder supports them
  428. */
  429. if (insn->vex_prefix.nbytes >= 3) {
  430. /*
  431. * vex2: c5 rvvvvLpp (has no b bit)
  432. * vex3/xop: c4/8f rxbmmmmm wvvvvLpp
  433. * evex: 62 rxbR00mm wvvvv1pp zllBVaaa
  434. * Setting VEX3.b (setting because it has inverted meaning).
  435. * Setting EVEX.x since (in non-SIB encoding) EVEX.x
  436. * is the 4th bit of MODRM.rm, and needs the same treatment.
  437. * For VEX3-encoded insns, VEX3.x value has no effect in
  438. * non-SIB encoding, the change is superfluous but harmless.
  439. */
  440. cursor = auprobe->insn + insn_offset_vex_prefix(insn) + 1;
  441. *cursor |= 0x60;
  442. }
  443. /*
  444. * Convert from rip-relative addressing to register-relative addressing
  445. * via a scratch register.
  446. *
  447. * This is tricky since there are insns with modrm byte
  448. * which also use registers not encoded in modrm byte:
  449. * [i]div/[i]mul: implicitly use dx:ax
  450. * shift ops: implicitly use cx
  451. * cmpxchg: implicitly uses ax
  452. * cmpxchg8/16b: implicitly uses dx:ax and bx:cx
  453. * Encoding: 0f c7/1 modrm
  454. * The code below thinks that reg=1 (cx), chooses si as scratch.
  455. * mulx: implicitly uses dx: mulx r/m,r1,r2 does r1:r2 = dx * r/m.
  456. * First appeared in Haswell (BMI2 insn). It is vex-encoded.
  457. * Example where none of bx,cx,dx can be used as scratch reg:
  458. * c4 e2 63 f6 0d disp32 mulx disp32(%rip),%ebx,%ecx
  459. * [v]pcmpistri: implicitly uses cx, xmm0
  460. * [v]pcmpistrm: implicitly uses xmm0
  461. * [v]pcmpestri: implicitly uses ax, dx, cx, xmm0
  462. * [v]pcmpestrm: implicitly uses ax, dx, xmm0
  463. * Evil SSE4.2 string comparison ops from hell.
  464. * maskmovq/[v]maskmovdqu: implicitly uses (ds:rdi) as destination.
  465. * Encoding: 0f f7 modrm, 66 0f f7 modrm, vex-encoded: c5 f9 f7 modrm.
  466. * Store op1, byte-masked by op2 msb's in each byte, to (ds:rdi).
  467. * AMD says it has no 3-operand form (vex.vvvv must be 1111)
  468. * and that it can have only register operands, not mem
  469. * (its modrm byte must have mode=11).
  470. * If these restrictions will ever be lifted,
  471. * we'll need code to prevent selection of di as scratch reg!
  472. *
  473. * Summary: I don't know any insns with modrm byte which
  474. * use SI register implicitly. DI register is used only
  475. * by one insn (maskmovq) and BX register is used
  476. * only by one too (cmpxchg8b).
  477. * BP is stack-segment based (may be a problem?).
  478. * AX, DX, CX are off-limits (many implicit users).
  479. * SP is unusable (it's stack pointer - think about "pop mem";
  480. * also, rsp+disp32 needs sib encoding -> insn length change).
  481. */
  482. reg = MODRM_REG(insn); /* Fetch modrm.reg */
  483. reg2 = 0xff; /* Fetch vex.vvvv */
  484. if (insn->vex_prefix.nbytes)
  485. reg2 = insn->vex_prefix.bytes[2];
  486. /*
  487. * TODO: add XOP vvvv reading.
  488. *
  489. * vex.vvvv field is in bits 6-3, bits are inverted.
  490. * But in 32-bit mode, high-order bit may be ignored.
  491. * Therefore, let's consider only 3 low-order bits.
  492. */
  493. reg2 = ((reg2 >> 3) & 0x7) ^ 0x7;
  494. /*
  495. * Register numbering is ax,cx,dx,bx, sp,bp,si,di, r8..r15.
  496. *
  497. * Choose scratch reg. Order is important: must not select bx
  498. * if we can use si (cmpxchg8b case!)
  499. */
  500. if (reg != 6 && reg2 != 6) {
  501. reg2 = 6;
  502. auprobe->defparam.fixups |= UPROBE_FIX_RIP_SI;
  503. } else if (reg != 7 && reg2 != 7) {
  504. reg2 = 7;
  505. auprobe->defparam.fixups |= UPROBE_FIX_RIP_DI;
  506. /* TODO (paranoia): force maskmovq to not use di */
  507. } else {
  508. reg2 = 3;
  509. auprobe->defparam.fixups |= UPROBE_FIX_RIP_BX;
  510. }
  511. /*
  512. * Point cursor at the modrm byte. The next 4 bytes are the
  513. * displacement. Beyond the displacement, for some instructions,
  514. * is the immediate operand.
  515. */
  516. cursor = auprobe->insn + insn_offset_modrm(insn);
  517. /*
  518. * Change modrm from "00 reg 101" to "10 reg reg2". Example:
  519. * 89 05 disp32 mov %eax,disp32(%rip) becomes
  520. * 89 86 disp32 mov %eax,disp32(%rsi)
  521. */
  522. *cursor = 0x80 | (reg << 3) | reg2;
  523. }
  524. static inline unsigned long *
  525. scratch_reg(struct arch_uprobe *auprobe, struct pt_regs *regs)
  526. {
  527. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_SI)
  528. return &regs->si;
  529. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_DI)
  530. return &regs->di;
  531. return &regs->bx;
  532. }
  533. /*
  534. * If we're emulating a rip-relative instruction, save the contents
  535. * of the scratch register and store the target address in that register.
  536. */
  537. static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  538. {
  539. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) {
  540. struct uprobe_task *utask = current->utask;
  541. unsigned long *sr = scratch_reg(auprobe, regs);
  542. utask->autask.saved_scratch_register = *sr;
  543. *sr = utask->vaddr + auprobe->defparam.ilen;
  544. }
  545. }
  546. static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  547. {
  548. if (auprobe->defparam.fixups & UPROBE_FIX_RIP_MASK) {
  549. struct uprobe_task *utask = current->utask;
  550. unsigned long *sr = scratch_reg(auprobe, regs);
  551. *sr = utask->autask.saved_scratch_register;
  552. }
  553. }
  554. #else /* 32-bit: */
  555. /*
  556. * No RIP-relative addressing on 32-bit
  557. */
  558. static void riprel_analyze(struct arch_uprobe *auprobe, struct insn *insn)
  559. {
  560. }
  561. static void riprel_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  562. {
  563. }
  564. static void riprel_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  565. {
  566. }
  567. #endif /* CONFIG_X86_64 */
  568. struct uprobe_xol_ops {
  569. bool (*emulate)(struct arch_uprobe *, struct pt_regs *);
  570. int (*pre_xol)(struct arch_uprobe *, struct pt_regs *);
  571. int (*post_xol)(struct arch_uprobe *, struct pt_regs *);
  572. void (*abort)(struct arch_uprobe *, struct pt_regs *);
  573. };
  574. static inline int sizeof_long(struct pt_regs *regs)
  575. {
  576. /*
  577. * Check registers for mode as in_xxx_syscall() does not apply here.
  578. */
  579. return user_64bit_mode(regs) ? 8 : 4;
  580. }
  581. static int default_pre_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  582. {
  583. riprel_pre_xol(auprobe, regs);
  584. return 0;
  585. }
  586. static int emulate_push_stack(struct pt_regs *regs, unsigned long val)
  587. {
  588. unsigned long new_sp = regs->sp - sizeof_long(regs);
  589. if (copy_to_user((void __user *)new_sp, &val, sizeof_long(regs)))
  590. return -EFAULT;
  591. regs->sp = new_sp;
  592. return 0;
  593. }
  594. /*
  595. * We have to fix things up as follows:
  596. *
  597. * Typically, the new ip is relative to the copied instruction. We need
  598. * to make it relative to the original instruction (FIX_IP). Exceptions
  599. * are return instructions and absolute or indirect jump or call instructions.
  600. *
  601. * If the single-stepped instruction was a call, the return address that
  602. * is atop the stack is the address following the copied instruction. We
  603. * need to make it the address following the original instruction (FIX_CALL).
  604. *
  605. * If the original instruction was a rip-relative instruction such as
  606. * "movl %edx,0xnnnn(%rip)", we have instead executed an equivalent
  607. * instruction using a scratch register -- e.g., "movl %edx,0xnnnn(%rsi)".
  608. * We need to restore the contents of the scratch register
  609. * (FIX_RIP_reg).
  610. */
  611. static int default_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  612. {
  613. struct uprobe_task *utask = current->utask;
  614. riprel_post_xol(auprobe, regs);
  615. if (auprobe->defparam.fixups & UPROBE_FIX_IP) {
  616. long correction = utask->vaddr - utask->xol_vaddr;
  617. regs->ip += correction;
  618. } else if (auprobe->defparam.fixups & UPROBE_FIX_CALL) {
  619. regs->sp += sizeof_long(regs); /* Pop incorrect return address */
  620. if (emulate_push_stack(regs, utask->vaddr + auprobe->defparam.ilen))
  621. return -ERESTART;
  622. }
  623. /* popf; tell the caller to not touch TF */
  624. if (auprobe->defparam.fixups & UPROBE_FIX_SETF)
  625. utask->autask.saved_tf = true;
  626. return 0;
  627. }
  628. static void default_abort_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  629. {
  630. riprel_post_xol(auprobe, regs);
  631. }
  632. static const struct uprobe_xol_ops default_xol_ops = {
  633. .pre_xol = default_pre_xol_op,
  634. .post_xol = default_post_xol_op,
  635. .abort = default_abort_op,
  636. };
  637. static bool branch_is_call(struct arch_uprobe *auprobe)
  638. {
  639. return auprobe->branch.opc1 == 0xe8;
  640. }
  641. #define CASE_COND \
  642. COND(70, 71, XF(OF)) \
  643. COND(72, 73, XF(CF)) \
  644. COND(74, 75, XF(ZF)) \
  645. COND(78, 79, XF(SF)) \
  646. COND(7a, 7b, XF(PF)) \
  647. COND(76, 77, XF(CF) || XF(ZF)) \
  648. COND(7c, 7d, XF(SF) != XF(OF)) \
  649. COND(7e, 7f, XF(ZF) || XF(SF) != XF(OF))
  650. #define COND(op_y, op_n, expr) \
  651. case 0x ## op_y: DO((expr) != 0) \
  652. case 0x ## op_n: DO((expr) == 0)
  653. #define XF(xf) (!!(flags & X86_EFLAGS_ ## xf))
  654. static bool is_cond_jmp_opcode(u8 opcode)
  655. {
  656. switch (opcode) {
  657. #define DO(expr) \
  658. return true;
  659. CASE_COND
  660. #undef DO
  661. default:
  662. return false;
  663. }
  664. }
  665. static bool check_jmp_cond(struct arch_uprobe *auprobe, struct pt_regs *regs)
  666. {
  667. unsigned long flags = regs->flags;
  668. switch (auprobe->branch.opc1) {
  669. #define DO(expr) \
  670. return expr;
  671. CASE_COND
  672. #undef DO
  673. default: /* not a conditional jmp */
  674. return true;
  675. }
  676. }
  677. #undef XF
  678. #undef COND
  679. #undef CASE_COND
  680. static bool branch_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  681. {
  682. unsigned long new_ip = regs->ip += auprobe->branch.ilen;
  683. unsigned long offs = (long)auprobe->branch.offs;
  684. if (branch_is_call(auprobe)) {
  685. /*
  686. * If it fails we execute this (mangled, see the comment in
  687. * branch_clear_offset) insn out-of-line. In the likely case
  688. * this should trigger the trap, and the probed application
  689. * should die or restart the same insn after it handles the
  690. * signal, arch_uprobe_post_xol() won't be even called.
  691. *
  692. * But there is corner case, see the comment in ->post_xol().
  693. */
  694. if (emulate_push_stack(regs, new_ip))
  695. return false;
  696. } else if (!check_jmp_cond(auprobe, regs)) {
  697. offs = 0;
  698. }
  699. regs->ip = new_ip + offs;
  700. return true;
  701. }
  702. static bool push_emulate_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  703. {
  704. unsigned long *src_ptr = (void *)regs + auprobe->push.reg_offset;
  705. if (emulate_push_stack(regs, *src_ptr))
  706. return false;
  707. regs->ip += auprobe->push.ilen;
  708. return true;
  709. }
  710. static int branch_post_xol_op(struct arch_uprobe *auprobe, struct pt_regs *regs)
  711. {
  712. BUG_ON(!branch_is_call(auprobe));
  713. /*
  714. * We can only get here if branch_emulate_op() failed to push the ret
  715. * address _and_ another thread expanded our stack before the (mangled)
  716. * "call" insn was executed out-of-line. Just restore ->sp and restart.
  717. * We could also restore ->ip and try to call branch_emulate_op() again.
  718. */
  719. regs->sp += sizeof_long(regs);
  720. return -ERESTART;
  721. }
  722. static void branch_clear_offset(struct arch_uprobe *auprobe, struct insn *insn)
  723. {
  724. /*
  725. * Turn this insn into "call 1f; 1:", this is what we will execute
  726. * out-of-line if ->emulate() fails. We only need this to generate
  727. * a trap, so that the probed task receives the correct signal with
  728. * the properly filled siginfo.
  729. *
  730. * But see the comment in ->post_xol(), in the unlikely case it can
  731. * succeed. So we need to ensure that the new ->ip can not fall into
  732. * the non-canonical area and trigger #GP.
  733. *
  734. * We could turn it into (say) "pushf", but then we would need to
  735. * divorce ->insn[] and ->ixol[]. We need to preserve the 1st byte
  736. * of ->insn[] for set_orig_insn().
  737. */
  738. memset(auprobe->insn + insn_offset_immediate(insn),
  739. 0, insn->immediate.nbytes);
  740. }
  741. static const struct uprobe_xol_ops branch_xol_ops = {
  742. .emulate = branch_emulate_op,
  743. .post_xol = branch_post_xol_op,
  744. };
  745. static const struct uprobe_xol_ops push_xol_ops = {
  746. .emulate = push_emulate_op,
  747. };
  748. /* Returns -ENOSYS if branch_xol_ops doesn't handle this insn */
  749. static int branch_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
  750. {
  751. u8 opc1 = OPCODE1(insn);
  752. insn_byte_t p;
  753. int i;
  754. switch (opc1) {
  755. case 0xeb: /* jmp 8 */
  756. case 0xe9: /* jmp 32 */
  757. break;
  758. case 0x90: /* prefix* + nop; same as jmp with .offs = 0 */
  759. goto setup;
  760. case 0xe8: /* call relative */
  761. branch_clear_offset(auprobe, insn);
  762. break;
  763. case 0x0f:
  764. if (insn->opcode.nbytes != 2)
  765. return -ENOSYS;
  766. /*
  767. * If it is a "near" conditional jmp, OPCODE2() - 0x10 matches
  768. * OPCODE1() of the "short" jmp which checks the same condition.
  769. */
  770. opc1 = OPCODE2(insn) - 0x10;
  771. fallthrough;
  772. default:
  773. if (!is_cond_jmp_opcode(opc1))
  774. return -ENOSYS;
  775. }
  776. /*
  777. * 16-bit overrides such as CALLW (66 e8 nn nn) are not supported.
  778. * Intel and AMD behavior differ in 64-bit mode: Intel ignores 66 prefix.
  779. * No one uses these insns, reject any branch insns with such prefix.
  780. */
  781. for_each_insn_prefix(insn, i, p) {
  782. if (p == 0x66)
  783. return -ENOTSUPP;
  784. }
  785. setup:
  786. auprobe->branch.opc1 = opc1;
  787. auprobe->branch.ilen = insn->length;
  788. auprobe->branch.offs = insn->immediate.value;
  789. auprobe->ops = &branch_xol_ops;
  790. return 0;
  791. }
  792. /* Returns -ENOSYS if push_xol_ops doesn't handle this insn */
  793. static int push_setup_xol_ops(struct arch_uprobe *auprobe, struct insn *insn)
  794. {
  795. u8 opc1 = OPCODE1(insn), reg_offset = 0;
  796. if (opc1 < 0x50 || opc1 > 0x57)
  797. return -ENOSYS;
  798. if (insn->length > 2)
  799. return -ENOSYS;
  800. if (insn->length == 2) {
  801. /* only support rex_prefix 0x41 (x64 only) */
  802. #ifdef CONFIG_X86_64
  803. if (insn->rex_prefix.nbytes != 1 ||
  804. insn->rex_prefix.bytes[0] != 0x41)
  805. return -ENOSYS;
  806. switch (opc1) {
  807. case 0x50:
  808. reg_offset = offsetof(struct pt_regs, r8);
  809. break;
  810. case 0x51:
  811. reg_offset = offsetof(struct pt_regs, r9);
  812. break;
  813. case 0x52:
  814. reg_offset = offsetof(struct pt_regs, r10);
  815. break;
  816. case 0x53:
  817. reg_offset = offsetof(struct pt_regs, r11);
  818. break;
  819. case 0x54:
  820. reg_offset = offsetof(struct pt_regs, r12);
  821. break;
  822. case 0x55:
  823. reg_offset = offsetof(struct pt_regs, r13);
  824. break;
  825. case 0x56:
  826. reg_offset = offsetof(struct pt_regs, r14);
  827. break;
  828. case 0x57:
  829. reg_offset = offsetof(struct pt_regs, r15);
  830. break;
  831. }
  832. #else
  833. return -ENOSYS;
  834. #endif
  835. } else {
  836. switch (opc1) {
  837. case 0x50:
  838. reg_offset = offsetof(struct pt_regs, ax);
  839. break;
  840. case 0x51:
  841. reg_offset = offsetof(struct pt_regs, cx);
  842. break;
  843. case 0x52:
  844. reg_offset = offsetof(struct pt_regs, dx);
  845. break;
  846. case 0x53:
  847. reg_offset = offsetof(struct pt_regs, bx);
  848. break;
  849. case 0x54:
  850. reg_offset = offsetof(struct pt_regs, sp);
  851. break;
  852. case 0x55:
  853. reg_offset = offsetof(struct pt_regs, bp);
  854. break;
  855. case 0x56:
  856. reg_offset = offsetof(struct pt_regs, si);
  857. break;
  858. case 0x57:
  859. reg_offset = offsetof(struct pt_regs, di);
  860. break;
  861. }
  862. }
  863. auprobe->push.reg_offset = reg_offset;
  864. auprobe->push.ilen = insn->length;
  865. auprobe->ops = &push_xol_ops;
  866. return 0;
  867. }
  868. /**
  869. * arch_uprobe_analyze_insn - instruction analysis including validity and fixups.
  870. * @auprobe: the probepoint information.
  871. * @mm: the probed address space.
  872. * @addr: virtual address at which to install the probepoint
  873. * Return 0 on success or a -ve number on error.
  874. */
  875. int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long addr)
  876. {
  877. struct insn insn;
  878. u8 fix_ip_or_call = UPROBE_FIX_IP;
  879. int ret;
  880. ret = uprobe_init_insn(auprobe, &insn, is_64bit_mm(mm));
  881. if (ret)
  882. return ret;
  883. ret = branch_setup_xol_ops(auprobe, &insn);
  884. if (ret != -ENOSYS)
  885. return ret;
  886. ret = push_setup_xol_ops(auprobe, &insn);
  887. if (ret != -ENOSYS)
  888. return ret;
  889. /*
  890. * Figure out which fixups default_post_xol_op() will need to perform,
  891. * and annotate defparam->fixups accordingly.
  892. */
  893. switch (OPCODE1(&insn)) {
  894. case 0x9d: /* popf */
  895. auprobe->defparam.fixups |= UPROBE_FIX_SETF;
  896. break;
  897. case 0xc3: /* ret or lret -- ip is correct */
  898. case 0xcb:
  899. case 0xc2:
  900. case 0xca:
  901. case 0xea: /* jmp absolute -- ip is correct */
  902. fix_ip_or_call = 0;
  903. break;
  904. case 0x9a: /* call absolute - Fix return addr, not ip */
  905. fix_ip_or_call = UPROBE_FIX_CALL;
  906. break;
  907. case 0xff:
  908. switch (MODRM_REG(&insn)) {
  909. case 2: case 3: /* call or lcall, indirect */
  910. fix_ip_or_call = UPROBE_FIX_CALL;
  911. break;
  912. case 4: case 5: /* jmp or ljmp, indirect */
  913. fix_ip_or_call = 0;
  914. break;
  915. }
  916. fallthrough;
  917. default:
  918. riprel_analyze(auprobe, &insn);
  919. }
  920. auprobe->defparam.ilen = insn.length;
  921. auprobe->defparam.fixups |= fix_ip_or_call;
  922. auprobe->ops = &default_xol_ops;
  923. return 0;
  924. }
  925. /*
  926. * arch_uprobe_pre_xol - prepare to execute out of line.
  927. * @auprobe: the probepoint information.
  928. * @regs: reflects the saved user state of current task.
  929. */
  930. int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  931. {
  932. struct uprobe_task *utask = current->utask;
  933. if (auprobe->ops->pre_xol) {
  934. int err = auprobe->ops->pre_xol(auprobe, regs);
  935. if (err)
  936. return err;
  937. }
  938. regs->ip = utask->xol_vaddr;
  939. utask->autask.saved_trap_nr = current->thread.trap_nr;
  940. current->thread.trap_nr = UPROBE_TRAP_NR;
  941. utask->autask.saved_tf = !!(regs->flags & X86_EFLAGS_TF);
  942. regs->flags |= X86_EFLAGS_TF;
  943. if (test_tsk_thread_flag(current, TIF_BLOCKSTEP))
  944. set_task_blockstep(current, false);
  945. return 0;
  946. }
  947. /*
  948. * If xol insn itself traps and generates a signal(Say,
  949. * SIGILL/SIGSEGV/etc), then detect the case where a singlestepped
  950. * instruction jumps back to its own address. It is assumed that anything
  951. * like do_page_fault/do_trap/etc sets thread.trap_nr != -1.
  952. *
  953. * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
  954. * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
  955. * UPROBE_TRAP_NR == -1 set by arch_uprobe_pre_xol().
  956. */
  957. bool arch_uprobe_xol_was_trapped(struct task_struct *t)
  958. {
  959. if (t->thread.trap_nr != UPROBE_TRAP_NR)
  960. return true;
  961. return false;
  962. }
  963. /*
  964. * Called after single-stepping. To avoid the SMP problems that can
  965. * occur when we temporarily put back the original opcode to
  966. * single-step, we single-stepped a copy of the instruction.
  967. *
  968. * This function prepares to resume execution after the single-step.
  969. */
  970. int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  971. {
  972. struct uprobe_task *utask = current->utask;
  973. bool send_sigtrap = utask->autask.saved_tf;
  974. int err = 0;
  975. WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR);
  976. current->thread.trap_nr = utask->autask.saved_trap_nr;
  977. if (auprobe->ops->post_xol) {
  978. err = auprobe->ops->post_xol(auprobe, regs);
  979. if (err) {
  980. /*
  981. * Restore ->ip for restart or post mortem analysis.
  982. * ->post_xol() must not return -ERESTART unless this
  983. * is really possible.
  984. */
  985. regs->ip = utask->vaddr;
  986. if (err == -ERESTART)
  987. err = 0;
  988. send_sigtrap = false;
  989. }
  990. }
  991. /*
  992. * arch_uprobe_pre_xol() doesn't save the state of TIF_BLOCKSTEP
  993. * so we can get an extra SIGTRAP if we do not clear TF. We need
  994. * to examine the opcode to make it right.
  995. */
  996. if (send_sigtrap)
  997. send_sig(SIGTRAP, current, 0);
  998. if (!utask->autask.saved_tf)
  999. regs->flags &= ~X86_EFLAGS_TF;
  1000. return err;
  1001. }
  1002. /* callback routine for handling exceptions. */
  1003. int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data)
  1004. {
  1005. struct die_args *args = data;
  1006. struct pt_regs *regs = args->regs;
  1007. int ret = NOTIFY_DONE;
  1008. /* We are only interested in userspace traps */
  1009. if (regs && !user_mode(regs))
  1010. return NOTIFY_DONE;
  1011. switch (val) {
  1012. case DIE_INT3:
  1013. if (uprobe_pre_sstep_notifier(regs))
  1014. ret = NOTIFY_STOP;
  1015. break;
  1016. case DIE_DEBUG:
  1017. if (uprobe_post_sstep_notifier(regs))
  1018. ret = NOTIFY_STOP;
  1019. break;
  1020. default:
  1021. break;
  1022. }
  1023. return ret;
  1024. }
  1025. /*
  1026. * This function gets called when XOL instruction either gets trapped or
  1027. * the thread has a fatal signal. Reset the instruction pointer to its
  1028. * probed address for the potential restart or for post mortem analysis.
  1029. */
  1030. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  1031. {
  1032. struct uprobe_task *utask = current->utask;
  1033. if (auprobe->ops->abort)
  1034. auprobe->ops->abort(auprobe, regs);
  1035. current->thread.trap_nr = utask->autask.saved_trap_nr;
  1036. regs->ip = utask->vaddr;
  1037. /* clear TF if it was set by us in arch_uprobe_pre_xol() */
  1038. if (!utask->autask.saved_tf)
  1039. regs->flags &= ~X86_EFLAGS_TF;
  1040. }
  1041. static bool __skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  1042. {
  1043. if (auprobe->ops->emulate)
  1044. return auprobe->ops->emulate(auprobe, regs);
  1045. return false;
  1046. }
  1047. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  1048. {
  1049. bool ret = __skip_sstep(auprobe, regs);
  1050. if (ret && (regs->flags & X86_EFLAGS_TF))
  1051. send_sig(SIGTRAP, current, 0);
  1052. return ret;
  1053. }
  1054. unsigned long
  1055. arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs)
  1056. {
  1057. int rasize = sizeof_long(regs), nleft;
  1058. unsigned long orig_ret_vaddr = 0; /* clear high bits for 32-bit apps */
  1059. if (copy_from_user(&orig_ret_vaddr, (void __user *)regs->sp, rasize))
  1060. return -1;
  1061. /* check whether address has been already hijacked */
  1062. if (orig_ret_vaddr == trampoline_vaddr)
  1063. return orig_ret_vaddr;
  1064. nleft = copy_to_user((void __user *)regs->sp, &trampoline_vaddr, rasize);
  1065. if (likely(!nleft)) {
  1066. if (shstk_update_last_frame(trampoline_vaddr)) {
  1067. force_sig(SIGSEGV);
  1068. return -1;
  1069. }
  1070. return orig_ret_vaddr;
  1071. }
  1072. if (nleft != rasize) {
  1073. pr_err("return address clobbered: pid=%d, %%sp=%#lx, %%ip=%#lx\n",
  1074. current->pid, regs->sp, regs->ip);
  1075. force_sig(SIGSEGV);
  1076. }
  1077. return -1;
  1078. }
  1079. bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
  1080. struct pt_regs *regs)
  1081. {
  1082. if (ctx == RP_CHECK_CALL) /* sp was just decremented by "call" insn */
  1083. return regs->sp < ret->stack;
  1084. else
  1085. return regs->sp <= ret->stack;
  1086. }