0139-x86-unwinder-Handle-stack-overflows-more-gracefully.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. From 77f0942d5c0f9adc2a6cdd1f0f17c1c6e631b5a6 Mon Sep 17 00:00:00 2001
  2. From: Josh Poimboeuf <[email protected]>
  3. Date: Mon, 4 Dec 2017 15:07:09 +0100
  4. Subject: [PATCH 139/242] x86/unwinder: Handle stack overflows more gracefully
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. There are at least two unwinder bugs hindering the debugging of
  10. stack-overflow crashes:
  11. - It doesn't deal gracefully with the case where the stack overflows and
  12. the stack pointer itself isn't on a valid stack but the
  13. to-be-dereferenced data *is*.
  14. - The ORC oops dump code doesn't know how to print partial pt_regs, for the
  15. case where if we get an interrupt/exception in *early* entry code
  16. before the full pt_regs have been saved.
  17. Fix both issues.
  18. http://lkml.kernel.org/r/20171126024031.uxi4numpbjm5rlbr@treble
  19. Signed-off-by: Josh Poimboeuf <[email protected]>
  20. Signed-off-by: Thomas Gleixner <[email protected]>
  21. Reviewed-by: Borislav Petkov <[email protected]>
  22. Cc: Andy Lutomirski <[email protected]>
  23. Cc: Boris Ostrovsky <[email protected]>
  24. Cc: Borislav Petkov <[email protected]>
  25. Cc: Brian Gerst <[email protected]>
  26. Cc: Dave Hansen <[email protected]>
  27. Cc: Dave Hansen <[email protected]>
  28. Cc: David Laight <[email protected]>
  29. Cc: Denys Vlasenko <[email protected]>
  30. Cc: Eduardo Valentin <[email protected]>
  31. Cc: Greg KH <[email protected]>
  32. Cc: H. Peter Anvin <[email protected]>
  33. Cc: Juergen Gross <[email protected]>
  34. Cc: Linus Torvalds <[email protected]>
  35. Cc: Peter Zijlstra <[email protected]>
  36. Cc: Rik van Riel <[email protected]>
  37. Cc: Will Deacon <[email protected]>
  38. Cc: [email protected]
  39. Cc: [email protected]
  40. Cc: [email protected]
  41. Cc: [email protected]
  42. Link: https://lkml.kernel.org/r/[email protected]
  43. Signed-off-by: Ingo Molnar <[email protected]>
  44. (backported from commit b02fcf9ba1211097754b286043cd87a8b4907e75)
  45. Signed-off-by: Andy Whitcroft <[email protected]>
  46. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  47. (cherry picked from commit 9e51f396b068c3e8495cd130113e2f73b2b1f6b0)
  48. Signed-off-by: Fabian Grünbichler <[email protected]>
  49. ---
  50. arch/x86/include/asm/kdebug.h | 1 +
  51. arch/x86/include/asm/unwind.h | 7 ++++
  52. arch/x86/kernel/dumpstack.c | 29 ++++++++++++++--
  53. arch/x86/kernel/process_64.c | 12 +++----
  54. arch/x86/kernel/unwind_orc.c | 78 +++++++++++++++----------------------------
  55. 5 files changed, 67 insertions(+), 60 deletions(-)
  56. diff --git a/arch/x86/include/asm/kdebug.h b/arch/x86/include/asm/kdebug.h
  57. index 29a594a3b82a..2a7769dd8fa2 100644
  58. --- a/arch/x86/include/asm/kdebug.h
  59. +++ b/arch/x86/include/asm/kdebug.h
  60. @@ -25,6 +25,7 @@ extern void die(const char *, struct pt_regs *,long);
  61. extern int __must_check __die(const char *, struct pt_regs *, long);
  62. extern void show_stack_regs(struct pt_regs *regs);
  63. extern void __show_regs(struct pt_regs *regs, int all);
  64. +extern void show_iret_regs(struct pt_regs *regs);
  65. extern unsigned long oops_begin(void);
  66. extern void oops_end(unsigned long, struct pt_regs *, int signr);
  67. diff --git a/arch/x86/include/asm/unwind.h b/arch/x86/include/asm/unwind.h
  68. index 35d67dc7b69f..38fa6154e382 100644
  69. --- a/arch/x86/include/asm/unwind.h
  70. +++ b/arch/x86/include/asm/unwind.h
  71. @@ -6,6 +6,9 @@
  72. #include <asm/ptrace.h>
  73. #include <asm/stacktrace.h>
  74. +#define IRET_FRAME_OFFSET (offsetof(struct pt_regs, ip))
  75. +#define IRET_FRAME_SIZE (sizeof(struct pt_regs) - IRET_FRAME_OFFSET)
  76. +
  77. struct unwind_state {
  78. struct stack_info stack_info;
  79. unsigned long stack_mask;
  80. @@ -51,6 +54,10 @@ void unwind_start(struct unwind_state *state, struct task_struct *task,
  81. }
  82. #if defined(CONFIG_UNWINDER_ORC) || defined(CONFIG_UNWINDER_FRAME_POINTER)
  83. +/*
  84. + * WARNING: The entire pt_regs may not be safe to dereference. In some cases,
  85. + * only the iret frame registers are accessible. Use with caution!
  86. + */
  87. static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state)
  88. {
  89. if (unwind_done(state))
  90. diff --git a/arch/x86/kernel/dumpstack.c b/arch/x86/kernel/dumpstack.c
  91. index dbce3cca94cb..695cdce5dfc8 100644
  92. --- a/arch/x86/kernel/dumpstack.c
  93. +++ b/arch/x86/kernel/dumpstack.c
  94. @@ -50,6 +50,28 @@ static void printk_stack_address(unsigned long address, int reliable,
  95. printk("%s %s%pB\n", log_lvl, reliable ? "" : "? ", (void *)address);
  96. }
  97. +void show_iret_regs(struct pt_regs *regs)
  98. +{
  99. + printk(KERN_DEFAULT "RIP: %04x:%pS\n", (int)regs->cs, (void *)regs->ip);
  100. + printk(KERN_DEFAULT "RSP: %04x:%016lx EFLAGS: %08lx", (int)regs->ss,
  101. + regs->sp, regs->flags);
  102. +}
  103. +
  104. +static void show_regs_safe(struct stack_info *info, struct pt_regs *regs)
  105. +{
  106. + if (on_stack(info, regs, sizeof(*regs)))
  107. + __show_regs(regs, 0);
  108. + else if (on_stack(info, (void *)regs + IRET_FRAME_OFFSET,
  109. + IRET_FRAME_SIZE)) {
  110. + /*
  111. + * When an interrupt or exception occurs in entry code, the
  112. + * full pt_regs might not have been saved yet. In that case
  113. + * just print the iret frame.
  114. + */
  115. + show_iret_regs(regs);
  116. + }
  117. +}
  118. +
  119. void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  120. unsigned long *stack, char *log_lvl)
  121. {
  122. @@ -94,6 +116,9 @@ void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  123. if (stack_name)
  124. printk("%s <%s>\n", log_lvl, stack_name);
  125. + if (regs)
  126. + show_regs_safe(&stack_info, regs);
  127. +
  128. /*
  129. * Scan the stack, printing any text addresses we find. At the
  130. * same time, follow proper stack frames with the unwinder.
  131. @@ -116,7 +141,7 @@ void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  132. /*
  133. * Don't print regs->ip again if it was already printed
  134. - * by __show_regs() below.
  135. + * by show_regs_safe() below.
  136. */
  137. if (regs && stack == &regs->ip) {
  138. unwind_next_frame(&state);
  139. @@ -154,7 +179,7 @@ void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  140. /* if the frame has entry regs, print them */
  141. regs = unwind_get_entry_regs(&state);
  142. if (regs)
  143. - __show_regs(regs, 0);
  144. + show_regs_safe(&stack_info, regs);
  145. }
  146. if (stack_name)
  147. diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
  148. index b08b9b6c40eb..01b119bebb68 100644
  149. --- a/arch/x86/kernel/process_64.c
  150. +++ b/arch/x86/kernel/process_64.c
  151. @@ -69,10 +69,8 @@ void __show_regs(struct pt_regs *regs, int all)
  152. unsigned int fsindex, gsindex;
  153. unsigned int ds, cs, es;
  154. - printk(KERN_DEFAULT "RIP: %04lx:%pS\n", regs->cs & 0xffff,
  155. - (void *)regs->ip);
  156. - printk(KERN_DEFAULT "RSP: %04lx:%016lx EFLAGS: %08lx", regs->ss,
  157. - regs->sp, regs->flags);
  158. + show_iret_regs(regs);
  159. +
  160. if (regs->orig_ax != -1)
  161. pr_cont(" ORIG_RAX: %016lx\n", regs->orig_ax);
  162. else
  163. @@ -89,6 +87,9 @@ void __show_regs(struct pt_regs *regs, int all)
  164. printk(KERN_DEFAULT "R13: %016lx R14: %016lx R15: %016lx\n",
  165. regs->r13, regs->r14, regs->r15);
  166. + if (!all)
  167. + return;
  168. +
  169. asm("movl %%ds,%0" : "=r" (ds));
  170. asm("movl %%cs,%0" : "=r" (cs));
  171. asm("movl %%es,%0" : "=r" (es));
  172. @@ -99,9 +100,6 @@ void __show_regs(struct pt_regs *regs, int all)
  173. rdmsrl(MSR_GS_BASE, gs);
  174. rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
  175. - if (!all)
  176. - return;
  177. -
  178. cr0 = read_cr0();
  179. cr2 = read_cr2();
  180. cr3 = __read_cr3();
  181. diff --git a/arch/x86/kernel/unwind_orc.c b/arch/x86/kernel/unwind_orc.c
  182. index cea85bfe93f7..702f15f6b5be 100644
  183. --- a/arch/x86/kernel/unwind_orc.c
  184. +++ b/arch/x86/kernel/unwind_orc.c
  185. @@ -253,22 +253,15 @@ unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
  186. return NULL;
  187. }
  188. -static bool stack_access_ok(struct unwind_state *state, unsigned long addr,
  189. +static bool stack_access_ok(struct unwind_state *state, unsigned long _addr,
  190. size_t len)
  191. {
  192. struct stack_info *info = &state->stack_info;
  193. + void *addr = (void *)_addr;
  194. - /*
  195. - * If the address isn't on the current stack, switch to the next one.
  196. - *
  197. - * We may have to traverse multiple stacks to deal with the possibility
  198. - * that info->next_sp could point to an empty stack and the address
  199. - * could be on a subsequent stack.
  200. - */
  201. - while (!on_stack(info, (void *)addr, len))
  202. - if (get_stack_info(info->next_sp, state->task, info,
  203. - &state->stack_mask))
  204. - return false;
  205. + if (!on_stack(info, addr, len) &&
  206. + (get_stack_info(addr, state->task, info, &state->stack_mask)))
  207. + return false;
  208. return true;
  209. }
  210. @@ -283,42 +276,32 @@ static bool deref_stack_reg(struct unwind_state *state, unsigned long addr,
  211. return true;
  212. }
  213. -#define REGS_SIZE (sizeof(struct pt_regs))
  214. -#define SP_OFFSET (offsetof(struct pt_regs, sp))
  215. -#define IRET_REGS_SIZE (REGS_SIZE - offsetof(struct pt_regs, ip))
  216. -#define IRET_SP_OFFSET (SP_OFFSET - offsetof(struct pt_regs, ip))
  217. -
  218. static bool deref_stack_regs(struct unwind_state *state, unsigned long addr,
  219. - unsigned long *ip, unsigned long *sp, bool full)
  220. + unsigned long *ip, unsigned long *sp)
  221. {
  222. - size_t regs_size = full ? REGS_SIZE : IRET_REGS_SIZE;
  223. - size_t sp_offset = full ? SP_OFFSET : IRET_SP_OFFSET;
  224. - struct pt_regs *regs = (struct pt_regs *)(addr + regs_size - REGS_SIZE);
  225. -
  226. - if (IS_ENABLED(CONFIG_X86_64)) {
  227. - if (!stack_access_ok(state, addr, regs_size))
  228. - return false;
  229. -
  230. - *ip = regs->ip;
  231. - *sp = regs->sp;
  232. + struct pt_regs *regs = (struct pt_regs *)addr;
  233. - return true;
  234. - }
  235. + /* x86-32 support will be more complicated due to the &regs->sp hack */
  236. + BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32));
  237. - if (!stack_access_ok(state, addr, sp_offset))
  238. + if (!stack_access_ok(state, addr, sizeof(struct pt_regs)))
  239. return false;
  240. *ip = regs->ip;
  241. + *sp = regs->sp;
  242. + return true;
  243. +}
  244. - if (user_mode(regs)) {
  245. - if (!stack_access_ok(state, addr + sp_offset,
  246. - REGS_SIZE - SP_OFFSET))
  247. - return false;
  248. +static bool deref_stack_iret_regs(struct unwind_state *state, unsigned long addr,
  249. + unsigned long *ip, unsigned long *sp)
  250. +{
  251. + struct pt_regs *regs = (void *)addr - IRET_FRAME_OFFSET;
  252. - *sp = regs->sp;
  253. - } else
  254. - *sp = (unsigned long)&regs->sp;
  255. + if (!stack_access_ok(state, addr, IRET_FRAME_SIZE))
  256. + return false;
  257. + *ip = regs->ip;
  258. + *sp = regs->sp;
  259. return true;
  260. }
  261. @@ -327,7 +310,6 @@ bool unwind_next_frame(struct unwind_state *state)
  262. unsigned long ip_p, sp, orig_ip, prev_sp = state->sp;
  263. enum stack_type prev_type = state->stack_info.type;
  264. struct orc_entry *orc;
  265. - struct pt_regs *ptregs;
  266. bool indirect = false;
  267. if (unwind_done(state))
  268. @@ -435,8 +417,8 @@ bool unwind_next_frame(struct unwind_state *state)
  269. break;
  270. case ORC_TYPE_REGS:
  271. - if (!deref_stack_regs(state, sp, &state->ip, &state->sp, true)) {
  272. - orc_warn("can't dereference registers at %p for ip %p\n",
  273. + if (!deref_stack_regs(state, sp, &state->ip, &state->sp)) {
  274. + orc_warn("can't dereference registers at %p for ip %pB\n",
  275. (void *)sp, (void *)orig_ip);
  276. goto done;
  277. }
  278. @@ -447,20 +429,14 @@ bool unwind_next_frame(struct unwind_state *state)
  279. break;
  280. case ORC_TYPE_REGS_IRET:
  281. - if (!deref_stack_regs(state, sp, &state->ip, &state->sp, false)) {
  282. - orc_warn("can't dereference iret registers at %p for ip %p\n",
  283. + if (!deref_stack_iret_regs(state, sp, &state->ip, &state->sp)) {
  284. + orc_warn("can't dereference iret registers at %p for ip %pB\n",
  285. (void *)sp, (void *)orig_ip);
  286. goto done;
  287. }
  288. - ptregs = container_of((void *)sp, struct pt_regs, ip);
  289. - if ((unsigned long)ptregs >= prev_sp &&
  290. - on_stack(&state->stack_info, ptregs, REGS_SIZE)) {
  291. - state->regs = ptregs;
  292. - state->full_regs = false;
  293. - } else
  294. - state->regs = NULL;
  295. -
  296. + state->regs = (void *)sp - IRET_FRAME_OFFSET;
  297. + state->full_regs = false;
  298. state->signal = true;
  299. break;
  300. --
  301. 2.14.2