0108-x86-traps-Use-a-new-on_thread_stack-helper-to-clean-.patch 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Andy Lutomirski <[email protected]>
  3. Date: Thu, 2 Nov 2017 00:59:17 -0700
  4. Subject: [PATCH] x86/traps: Use a new on_thread_stack() helper to clean up an
  5. assertion
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. CVE-2017-5754
  10. Let's keep the stack-related logic together rather than open-coding
  11. a comparison in an assertion in the traps code.
  12. Signed-off-by: Andy Lutomirski <[email protected]>
  13. Reviewed-by: Borislav Petkov <[email protected]>
  14. Cc: Borislav Petkov <[email protected]>
  15. Cc: Brian Gerst <[email protected]>
  16. Cc: Dave Hansen <[email protected]>
  17. Cc: Linus Torvalds <[email protected]>
  18. Cc: Peter Zijlstra <[email protected]>
  19. Cc: Thomas Gleixner <[email protected]>
  20. Link: http://lkml.kernel.org/r/856b15bee1f55017b8f79d3758b0d51c48a08cf8.1509609304.git.luto@kernel.org
  21. Signed-off-by: Ingo Molnar <[email protected]>
  22. (backported from commit 3383642c2f9d4f5b4fa37436db4a109a1a10018c)
  23. Signed-off-by: Andy Whitcroft <[email protected]>
  24. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  25. (cherry picked from commit 243de7bd3434c50fb07dd0fc84c462236cfcba3e)
  26. Signed-off-by: Fabian Grünbichler <[email protected]>
  27. ---
  28. arch/x86/include/asm/processor.h | 8 ++++++++
  29. arch/x86/include/asm/thread_info.h | 22 +++++++++++-----------
  30. arch/x86/kernel/traps.c | 3 +--
  31. 3 files changed, 20 insertions(+), 13 deletions(-)
  32. diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h
  33. index cec9a329c0f1..79739e5f939a 100644
  34. --- a/arch/x86/include/asm/processor.h
  35. +++ b/arch/x86/include/asm/processor.h
  36. @@ -159,6 +159,8 @@ enum cpuid_regs_idx {
  37. extern struct cpuinfo_x86 boot_cpu_data;
  38. extern struct cpuinfo_x86 new_cpu_data;
  39. +#include <linux/thread_info.h>
  40. +
  41. extern struct tss_struct doublefault_tss;
  42. extern __u32 cpu_caps_cleared[NCAPINTS];
  43. extern __u32 cpu_caps_set[NCAPINTS];
  44. @@ -534,6 +536,12 @@ static inline unsigned long current_top_of_stack(void)
  45. #endif
  46. }
  47. +static inline bool on_thread_stack(void)
  48. +{
  49. + return (unsigned long)(current_top_of_stack() -
  50. + current_stack_pointer()) < THREAD_SIZE;
  51. +}
  52. +
  53. #ifdef CONFIG_PARAVIRT
  54. #include <asm/paravirt.h>
  55. #else
  56. diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h
  57. index e00e1bd6e7b3..ec8ef3bbb7dc 100644
  58. --- a/arch/x86/include/asm/thread_info.h
  59. +++ b/arch/x86/include/asm/thread_info.h
  60. @@ -48,6 +48,17 @@
  61. * - this struct shares the supervisor stack pages
  62. */
  63. #ifndef __ASSEMBLY__
  64. +static inline unsigned long current_stack_pointer(void)
  65. +{
  66. + unsigned long sp;
  67. +#ifdef CONFIG_X86_64
  68. + asm("mov %%rsp,%0" : "=g" (sp));
  69. +#else
  70. + asm("mov %%esp,%0" : "=g" (sp));
  71. +#endif
  72. + return sp;
  73. +}
  74. +
  75. struct task_struct;
  76. #include <asm/cpufeature.h>
  77. #include <linux/atomic.h>
  78. @@ -155,17 +166,6 @@ struct thread_info {
  79. */
  80. #ifndef __ASSEMBLY__
  81. -static inline unsigned long current_stack_pointer(void)
  82. -{
  83. - unsigned long sp;
  84. -#ifdef CONFIG_X86_64
  85. - asm("mov %%rsp,%0" : "=g" (sp));
  86. -#else
  87. - asm("mov %%esp,%0" : "=g" (sp));
  88. -#endif
  89. - return sp;
  90. -}
  91. -
  92. /*
  93. * Walks up the stack frames to make sure that the specified object is
  94. * entirely contained by a single stack frame.
  95. diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
  96. index b2157d4a5338..3a46cab2696e 100644
  97. --- a/arch/x86/kernel/traps.c
  98. +++ b/arch/x86/kernel/traps.c
  99. @@ -153,8 +153,7 @@ void ist_begin_non_atomic(struct pt_regs *regs)
  100. * will catch asm bugs and any attempt to use ist_preempt_enable
  101. * from double_fault.
  102. */
  103. - BUG_ON((unsigned long)(current_top_of_stack() -
  104. - current_stack_pointer()) >= THREAD_SIZE);
  105. + BUG_ON(!on_thread_stack());
  106. preempt_enable_no_resched();
  107. }
  108. --
  109. 2.14.2