0087-x86-entry-64-Split-the-IRET-to-user-and-IRET-to-kern.patch 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Andy Lutomirski <[email protected]>
  3. Date: Thu, 2 Nov 2017 00:58:59 -0700
  4. Subject: [PATCH] x86/entry/64: Split the IRET-to-user and IRET-to-kernel paths
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. These code paths will diverge soon.
  10. Signed-off-by: Andy Lutomirski <[email protected]>
  11. Cc: Borislav Petkov <[email protected]>
  12. Cc: Brian Gerst <[email protected]>
  13. Cc: Dave Hansen <[email protected]>
  14. Cc: Linus Torvalds <[email protected]>
  15. Cc: Peter Zijlstra <[email protected]>
  16. Cc: Thomas Gleixner <[email protected]>
  17. Link: http://lkml.kernel.org/r/dccf8c7b3750199b4b30383c812d4e2931811509.1509609304.git.luto@kernel.org
  18. Signed-off-by: Ingo Molnar <[email protected]>
  19. (cherry picked from commit 26c4ef9c49d8a0341f6d97ce2cfdd55d1236ed29)
  20. Signed-off-by: Andy Whitcroft <[email protected]>
  21. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  22. (cherry picked from commit 64adfba0aeb668304d171c383ac80b22158ec128)
  23. Signed-off-by: Fabian Grünbichler <[email protected]>
  24. ---
  25. arch/x86/entry/entry_64.S | 34 +++++++++++++++++++++++++---------
  26. arch/x86/entry/entry_64_compat.S | 2 +-
  27. arch/x86/kernel/head_64.S | 2 +-
  28. 3 files changed, 27 insertions(+), 11 deletions(-)
  29. diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
  30. index fac354ddf056..e546441fbec3 100644
  31. --- a/arch/x86/entry/entry_64.S
  32. +++ b/arch/x86/entry/entry_64.S
  33. @@ -321,7 +321,7 @@ syscall_return_via_sysret:
  34. opportunistic_sysret_failed:
  35. SWAPGS
  36. - jmp restore_regs_and_iret
  37. + jmp restore_regs_and_return_to_usermode
  38. END(entry_SYSCALL_64)
  39. ENTRY(stub_ptregs_64)
  40. @@ -423,7 +423,7 @@ ENTRY(ret_from_fork)
  41. call syscall_return_slowpath /* returns with IRQs disabled */
  42. TRACE_IRQS_ON /* user mode is traced as IRQS on */
  43. SWAPGS
  44. - jmp restore_regs_and_iret
  45. + jmp restore_regs_and_return_to_usermode
  46. 1:
  47. /* kernel thread */
  48. @@ -612,7 +612,20 @@ GLOBAL(retint_user)
  49. call prepare_exit_to_usermode
  50. TRACE_IRQS_IRETQ
  51. SWAPGS
  52. - jmp restore_regs_and_iret
  53. +
  54. +GLOBAL(restore_regs_and_return_to_usermode)
  55. +#ifdef CONFIG_DEBUG_ENTRY
  56. + /* Assert that pt_regs indicates user mode. */
  57. + testl $3, CS(%rsp)
  58. + jnz 1f
  59. + ud2
  60. +1:
  61. +#endif
  62. + RESTORE_EXTRA_REGS
  63. + RESTORE_C_REGS
  64. + REMOVE_PT_GPREGS_FROM_STACK 8
  65. + INTERRUPT_RETURN
  66. +
  67. /* Returning to kernel space */
  68. retint_kernel:
  69. @@ -632,11 +645,14 @@ retint_kernel:
  70. */
  71. TRACE_IRQS_IRETQ
  72. -/*
  73. - * At this label, code paths which return to kernel and to user,
  74. - * which come from interrupts/exception and from syscalls, merge.
  75. - */
  76. -GLOBAL(restore_regs_and_iret)
  77. +GLOBAL(restore_regs_and_return_to_kernel)
  78. +#ifdef CONFIG_DEBUG_ENTRY
  79. + /* Assert that pt_regs indicates kernel mode. */
  80. + testl $3, CS(%rsp)
  81. + jz 1f
  82. + ud2
  83. +1:
  84. +#endif
  85. RESTORE_EXTRA_REGS
  86. RESTORE_C_REGS
  87. REMOVE_PT_GPREGS_FROM_STACK 8
  88. @@ -1340,7 +1356,7 @@ ENTRY(nmi)
  89. * work, because we don't want to enable interrupts.
  90. */
  91. SWAPGS
  92. - jmp restore_regs_and_iret
  93. + jmp restore_regs_and_return_to_usermode
  94. .Lnmi_from_kernel:
  95. /*
  96. diff --git a/arch/x86/entry/entry_64_compat.S b/arch/x86/entry/entry_64_compat.S
  97. index d8468ba24be0..2b3a88feaa2b 100644
  98. --- a/arch/x86/entry/entry_64_compat.S
  99. +++ b/arch/x86/entry/entry_64_compat.S
  100. @@ -337,7 +337,7 @@ ENTRY(entry_INT80_compat)
  101. /* Go back to user mode. */
  102. TRACE_IRQS_ON
  103. SWAPGS
  104. - jmp restore_regs_and_iret
  105. + jmp restore_regs_and_return_to_usermode
  106. END(entry_INT80_compat)
  107. ALIGN
  108. diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S
  109. index 4117c1e0b3d2..e785734980ad 100644
  110. --- a/arch/x86/kernel/head_64.S
  111. +++ b/arch/x86/kernel/head_64.S
  112. @@ -311,7 +311,7 @@ early_idt_handler_common:
  113. 20:
  114. decl early_recursion_flag(%rip)
  115. - jmp restore_regs_and_iret
  116. + jmp restore_regs_and_return_to_kernel
  117. END(early_idt_handler_common)
  118. __INITDATA
  119. --
  120. 2.14.2