aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Lutomirski <luto@kernel.org>2019-07-01 23:43:21 -0400
committerThomas Gleixner <tglx@linutronix.de>2019-07-02 02:45:20 -0400
commit539bca535decb11a0861b6205c6684b8e908589b (patch)
tree9059dc59b231d9a4152b67638f67751bc1c92a60
parentdffb3f9db6b593f3ed6ab4c8d8f10e0aa6aa7a88 (diff)
x86/entry/64: Fix and clean up paranoid_exit
paranoid_exit needs to restore CR3 before GSBASE. Doing it in the opposite order crashes if the exception came from a context with user GSBASE and user CR3 -- RESTORE_CR3 cannot resture user CR3 if run with user GSBASE. This results in infinitely recursing exceptions if user code does SYSENTER with TF set if both FSGSBASE and PTI are enabled. The old code worked if user code just set TF without SYSENTER because #DB from user mode is special cased in idtentry and paranoid_exit doesn't run. Fix it by cleaning up the spaghetti code. All that paranoid_exit needs to do is to disable IRQs, handle IRQ tracing, then restore CR3, and restore GSBASE. Simply do those actions in that order. Fixes: 708078f65721 ("x86/entry/64: Handle FSGSBASE enabled paranoid entry/exit") Reported-by: Vegard Nossum <vegard.nossum@oracle.com> Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com> Signed-off-by: Andy Lutomirski <luto@kernel.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: Borislav Petkov <bp@alien8.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: "H . Peter Anvin" <hpa@zytor.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Ravi Shankar <ravi.v.shankar@intel.com> Cc: H. Peter Anvin <hpa@zytor.com> Link: https://lkml.kernel.org/r/59725ceb08977359489fbed979716949ad45f616.1562035429.git.luto@kernel.org
-rw-r--r--arch/x86/entry/entry_64.S33
1 files changed, 17 insertions, 16 deletions
diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index 54b1b0468b2b..670306f588bf 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -1256,31 +1256,32 @@ END(paranoid_entry)
1256ENTRY(paranoid_exit) 1256ENTRY(paranoid_exit)
1257 UNWIND_HINT_REGS 1257 UNWIND_HINT_REGS
1258 DISABLE_INTERRUPTS(CLBR_ANY) 1258 DISABLE_INTERRUPTS(CLBR_ANY)
1259 TRACE_IRQS_OFF_DEBUG
1260 1259
1261 /* Handle GS depending on FSGSBASE availability */ 1260 /*
1262 ALTERNATIVE "jmp .Lparanoid_exit_checkgs", "nop",X86_FEATURE_FSGSBASE 1261 * The order of operations is important. IRQ tracing requires
1262 * kernel GSBASE and CR3. RESTORE_CR3 requires kernel GS base.
1263 *
1264 * NB to anyone to tries to optimize this code: this code does
1265 * not execute at all for exceptions coming from user mode. Those
1266 * exceptions go through error_exit instead.
1267 */
1268 TRACE_IRQS_IRETQ_DEBUG
1269 RESTORE_CR3 scratch_reg=%rax save_reg=%r14
1270
1271 /* Handle the three GSBASE cases. */
1272 ALTERNATIVE "jmp .Lparanoid_exit_checkgs", "", X86_FEATURE_FSGSBASE
1263 1273
1264 /* With FSGSBASE enabled, unconditionally restore GSBASE */ 1274 /* With FSGSBASE enabled, unconditionally restore GSBASE */
1265 wrgsbase %rbx 1275 wrgsbase %rbx
1266 jmp .Lparanoid_exit_no_swapgs; 1276 jmp restore_regs_and_return_to_kernel
1267 1277
1268.Lparanoid_exit_checkgs: 1278.Lparanoid_exit_checkgs:
1269 /* On non-FSGSBASE systems, conditionally do SWAPGS */ 1279 /* On non-FSGSBASE systems, conditionally do SWAPGS */
1270 testl %ebx, %ebx 1280 testl %ebx, %ebx
1271 jnz .Lparanoid_exit_no_swapgs 1281 jnz restore_regs_and_return_to_kernel
1272 TRACE_IRQS_IRETQ
1273 /* Always restore stashed CR3 value (see paranoid_entry) */
1274 RESTORE_CR3 scratch_reg=%rbx save_reg=%r14
1275 SWAPGS_UNSAFE_STACK
1276 jmp .Lparanoid_exit_restore
1277
1278.Lparanoid_exit_no_swapgs:
1279 TRACE_IRQS_IRETQ_DEBUG
1280 /* Always restore stashed CR3 value (see paranoid_entry) */
1281 RESTORE_CR3 scratch_reg=%rbx save_reg=%r14
1282 1282
1283.Lparanoid_exit_restore: 1283 /* We are returning to a context with user GSBASE. */
1284 SWAPGS_UNSAFE_STACK
1284 jmp restore_regs_and_return_to_kernel 1285 jmp restore_regs_and_return_to_kernel
1285END(paranoid_exit) 1286END(paranoid_exit)
1286 1287