public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86/entry/64: Fix CR3 restore order in paranoid_exit()
@ 2018-02-14  2:27 Josh Poimboeuf
  2018-02-14  4:19 ` Dave Hansen
  2018-02-14  7:35 ` [PATCH] x86/entry/64: Fix CR3 restore order " Ingo Molnar
  0 siblings, 2 replies; 10+ messages in thread
From: Josh Poimboeuf @ 2018-02-14  2:27 UTC (permalink / raw)
  To: x86
  Cc: linux-kernel, Andy Lutomirski, Peter Zijlstra, Dave Hansen,
	David Woodhouse, Thomas Gleixner, Ingo Molnar

The paranoid exit code only restores the saved CR3 when it switches back
to the user GS.  However, even in the kernel GS case, it's possible that
it needs to restore a user CR3, if for example, the paranoid exception
occurred in the syscall exit path between SWITCH_TO_USER_CR3_STACK and
SWAPGS.

Fix that issue by making the CR3 restore unconditional.  This is
symmetrical with the unconditional CR3 save in paranoid_entry().

Also, since RESTORE_CR3 is now done before the EBX compare, it needs to
use a different scratch register (R15 instead of RBX).

I haven't actually seen any real-world bugs caused by this, so I'm not
sure how theoretical it is.  I just stumbled upon it in code review when
looking for another bug.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
---
 arch/x86/entry/entry_64.S | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index cd216c9431e1..68c95a09b48d 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -1167,10 +1167,10 @@ ENTRY(paranoid_exit)
 	UNWIND_HINT_REGS
 	DISABLE_INTERRUPTS(CLBR_ANY)
 	TRACE_IRQS_OFF_DEBUG
+	RESTORE_CR3	scratch_reg=%r15 save_reg=%r14
 	testl	%ebx, %ebx			/* swapgs needed? */
 	jnz	.Lparanoid_exit_no_swapgs
 	TRACE_IRQS_IRETQ
-	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
 	SWAPGS_UNSAFE_STACK
 	jmp	.Lparanoid_exit_restore
 .Lparanoid_exit_no_swapgs:
-- 
2.14.3

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2018-02-15  0:31 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-02-14  2:27 [PATCH] x86/entry/64: Fix CR3 restore order in paranoid_exit() Josh Poimboeuf
2018-02-14  4:19 ` Dave Hansen
2018-02-14  7:39   ` Ingo Molnar
2018-02-14 16:11     ` Josh Poimboeuf
2018-02-14 22:27       ` Ingo Molnar
2018-02-14 16:13     ` Thomas Gleixner
2018-02-14 17:16     ` Andy Lutomirski
2018-02-14 23:31     ` [tip:x86/pti] x86/entry/64: Fix CR3 restore " tip-bot for Ingo Molnar
2018-02-15  0:31     ` tip-bot for Ingo Molnar
2018-02-14  7:35 ` [PATCH] x86/entry/64: Fix CR3 restore order " Ingo Molnar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox