Fix dosemu vm86() fault
Subsystems (3)
Pawan Gupta
reviewerv6 cover Fix dosemu vm86() fault
v6:
- Use %ss in 64-bit mode as well for all VERW calls. This avoids any having a separate macro for 32-bit (Dave).
- Split 32-bit mode fixes into separate patches.
v5: https://lore.kernel.org/r/[email protected]
- Simplify the use of ALTERNATIVE construct (Uros/Jiri/Peter).
v4: https://lore.kernel.org/r/[email protected]
- Further simplify the patch by using %ss for all VERW calls in 32-bit mode (Brian).
- In NMI exit path move VERW after RESTORE_ALL_NMI that touches GPRs (Dave).
v3: https://lore.kernel.org/r/[email protected]
- Simplify CLEAR_CPU_BUFFERS_SAFE by using %ss instead of %ds (Brian).
- Do verw before popf in SYSEXIT path (Jari).
v2: https://lore.kernel.org/r/[email protected]
- Safe guard against any other system calls like vm86() that might change %ds (Dave).
v1: https://lore.kernel.org/r/[email protected]
Hi,
This series fixes a #GP in 32-bit kernels when executing vm86() system call in dosemu software. In 32-bit mode, their are cases when user can set an arbitrary %ds that can cause a #GP when executing VERW instruction. The fix is to use %ss for referencing the VERW operand.
Patch 1-2: Fixes the VERW callsites in 32-bit entry path.
Patch 3: Uses %ss for VERW in 32-bit and 64-bit mode.
The fix is tested with below kselftest on 32-bit kernel:
./tools/testing/selftests/x86/entry_from_vm86.c
64-bit kernel was boot tested. On a Rocket Lake, measuring the CPU cycles for VERW with and without the %ss shows no significant difference. This indicates that the scrubbing behavior of VERW is intact.
Thanks,
Pawan
---
Pawan Gupta (3):
x86/entry_32: Do not clobber user EFLAGS.ZF x86/entry_32: Clear CPU buffers after register restore in NMI return x86/bugs: Use stack segment selector for VERW operand
arch/x86/entry/entry_32.S | 6 ++++-- arch/x86/include/asm/nospec-branch.h | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-)
---
base-commit: 431c1646e1f86b949fa3685efc50b660a364c2b6
change-id: 20240426-fix-dosemu-vm86-dd111a01737e
Pawan Gupta
reviewerPATCH v6 1/3 x86/entry_32: Do not clobber user EFLAGS.ZF
Opportunistic SYSEXIT executes VERW to clear CPU buffers after user EFLAGS are restored. This can clobber user EFLAGS.ZF.
Move CLEAR_CPU_BUFFERS before the user EFLAGS are restored. This ensures that the user EFLAGS.ZF is not clobbered.
--- arch/x86/entry/entry_32.S | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/entry/entry_32.S +2 -1
@@ -871,6 +871,8 @@ SYM_FUNC_START(entry_SYSENTER_32) /* Now ready to switch the cr3 */ SWITCH_TO_USER_CR3 scratch_reg=%eax + /* Clobbers ZF */ + CLEAR_CPU_BUFFERS /* * Restore all flags except IF. (We restore IF separately because
@@ -881,7 +883,6 @@ SYM_FUNC_START(entry_SYSENTER_32) BUG_IF_WRONG_CR3 no_user_check=1 popfl popl %eax - CLEAR_CPU_BUFFERS /* * Return back to the vDSO, which will pop ecx and edx.
Signature · 1 line
2.34.1
Re: [PATCH v6 1/3] x86/entry_32: Do not clobber user EFLAGS.ZF
Quoted 5 lines · Pawan Gupta
Opportunistic SYSEXIT executes VERW to clear CPU buffers after user EFLAGS are restored. This can clobber user EFLAGS.ZF.
Move
CLEAR_CPU_BUFFERSbefore the user EFLAGS are restored. This ensures that the user EFLAGS.ZF is not clobbered.
Just to be clear, the new (later) location is also safe for RFDS because it only exposes the contents of EFLAGS (not sensitive) and RAX.
RAX might leak the old or new values of CR3, which do not seem super valuable to me.
Pawan Gupta
reviewerRe: [PATCH v6 1/3] x86/entry_32: Do not clobber user EFLAGS.ZF
Quoted 9 lines · Dave Hansen
Quoted 5 lines · Pawan Gupta
Opportunistic SYSEXIT executes VERW to clear CPU buffers after user EFLAGS are restored. This can clobber user EFLAGS.ZF.
Move
CLEAR_CPU_BUFFERSbefore the user EFLAGS are restored. This ensures that the user EFLAGS.ZF is not clobbered.Just to be clear, the new (later) location is also safe for RFDS because it only exposes the contents of EFLAGS (not sensitive) and RAX.
Right.
Quoted 4 lines
RAX might leak the old or new values of CR3, which do not seem super valuable to me.
Acked-by Dave Hansen <[email protected]>
Thanks.
Pawan Gupta
reviewerPATCH v6 2/3 x86/entry_32: Clear CPU buffers after register restore in NMI return
In reply to Pawan GuptaCPU buffers are currently cleared after call to exc_nmi, but before register state is restored. This may be okay for MDS mitigation but not for RDFS. Because RDFS mitigation requires CPU buffers to be cleared when registers don't have any sensitive data.
Move CLEAR_CPU_BUFFERS after RESTORE_ALL_NMI.
--- arch/x86/entry/entry_32.S | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/entry/entry_32.S +2 -1
@@ -1145,7 +1145,6 @@ SYM_CODE_START(asm_exc_nmi) /* Not on SYSENTER stack. */ call exc_nmi - CLEAR_CPU_BUFFERS jmp .Lnmi_return .Lnmi_from_sysenter_stack:
@@ -1166,6 +1165,7 @@ SYM_CODE_START(asm_exc_nmi) CHECK_AND_APPLY_ESPFIX RESTORE_ALL_NMI cr3_reg=%edi pop=4 + CLEAR_CPU_BUFFERS jmp .Lirq_return #ifdef CONFIG_X86_ESPFIX32
@@ -1207,6 +1207,7 @@ SYM_CODE_START(asm_exc_nmi) * 1 - orig_ax */ lss (1+5+6)*4(%esp), %esp # back to espfix stack + CLEAR_CPU_BUFFERS jmp .Lirq_return #endif SYM_CODE_END(asm_exc_nmi)
Signature · 1 line
2.34.1
Re: [PATCH v6 2/3] x86/entry_32: Clear CPU buffers after register restore in NMI return
Quoted 6 lines · Pawan Gupta
CPU buffers are currently cleared after call to
exc_nmi, but before register state is restored. This may be okay for MDS mitigation but not for RDFS. Because RDFS mitigation requires CPU buffers to be cleared when registers don't have any sensitive data.Move
CLEAR_CPU_BUFFERSafterRESTORE_ALL_NMI.
This needs some grammar tweaking, but we can do that when it get applied.
Pawan Gupta
reviewerPATCH v6 3/3 x86/bugs: Use stack segment selector for VERW operand
In reply to Pawan GuptaRobert Gill reported below #GP in 32-bit mode when dosemu software was executing vm86() system call:
general protection fault: 0000 [#1] PREEMPT SMP CPU: 4 PID: 4610 Comm: dosemu.bin Not tainted 6.6.21-gentoo-x86 #1 Hardware name: Dell Inc. PowerEdge 1950/0H723K, BIOS 2.7.0 10/30/2010 EIP: restore_all_switch_stack+0xbe/0xcf EAX: 00000000 EBX: 00000000 ECX: 00000000 EDX: 00000000 ESI: 00000000 EDI: 00000000 EBP: 00000000 ESP: ff8affdc DS: 0000 ES: 0000 FS: 0000 GS: 0033 SS: 0068 EFLAGS: 00010046 CR0: 80050033 CR2: 00c2101c CR3: 04b6d000 CR4: 000406d0 Call Trace: show_regs+0x70/0x78 die_addr+0x29/0x70 exc_general_protection+0x13c/0x348 exc_bounds+0x98/0x98 handle_exception+0x14d/0x14d exc_bounds+0x98/0x98 restore_all_switch_stack+0xbe/0xcf exc_bounds+0x98/0x98 restore_all_switch_stack+0xbe/0xcf
This only happens in 32-bit mode when VERW based mitigations like MDS/RFDS are enabled. This is because segment registers with an arbitrary user value can result in #GP when executing VERW. Intel SDM vol. 2C documents the following behavior for VERW instruction:
#GP(0) - If a memory operand effective address is outside the CS, DS, ES,
FS, or GS segment limit.
CLEAR_CPU_BUFFERS macro executes VERW instruction before returning to user space. Use %ss selector to reference VERW operand. This ensures VERW will not #GP for an arbitrary user %ds.
--- arch/x86/include/asm/nospec-branch.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
arch/x86/include/asm/nospec-branch.h +4 -2
@@ -318,12 +318,14 @@/* * Macro to execute VERW instruction that mitigate transient data sampling * attacks such as MDS. On affected systems a microcode update overloaded VERW -* instruction to also clear the CPU buffers. VERW clobbers CFLAGS.ZF. +* instruction to also clear the CPU buffers. VERW clobbers CFLAGS.ZF. Using %ss +* to reference VERW operand avoids a #GP fault for an arbitrary user %ds in +* 32-bit mode. * * Note: Only the memory operand variant of VERW clears the CPU buffers. */ .macro CLEAR_CPU_BUFFERS - ALTERNATIVE "", __stringify(verw _ASM_RIP(mds_verw_sel)), X86_FEATURE_CLEAR_CPU_BUF + ALTERNATIVE "", __stringify(verw %ss:_ASM_RIP(mds_verw_sel)), X86_FEATURE_CLEAR_CPU_BUF .endm#ifdef CONFIG_X86_64
Signature · 1 line
2.34.1
Pawan Gupta
reviewerRe: [PATCH v6 3/3] x86/bugs: Use stack segment selector for VERW operand
Quoted 54 lines · Pawan Gupta
Robert Gill reported below #GP in 32-bit mode when dosemu software was executing
vm86()system call:general protection fault: 0000 [#1] PREEMPT SMP CPU: 4 PID: 4610 Comm: dosemu.bin Not tainted 6.6.21-gentoo-x86 #1 Hardware name: Dell Inc. PowerEdge 1950/0H723K, BIOS 2.7.0 10/30/2010 EIP: restore_all_switch_stack+0xbe/0xcf EAX: 00000000 EBX: 00000000 ECX: 00000000 EDX: 00000000 ESI: 00000000 EDI: 00000000 EBP: 00000000 ESP: ff8affdc DS: 0000 ES: 0000 FS: 0000 GS: 0033 SS: 0068 EFLAGS: 00010046 CR0: 80050033 CR2: 00c2101c CR3: 04b6d000 CR4: 000406d0 Call Trace: show_regs+0x70/0x78 die_addr+0x29/0x70 exc_general_protection+0x13c/0x348 exc_bounds+0x98/0x98 handle_exception+0x14d/0x14d exc_bounds+0x98/0x98 restore_all_switch_stack+0xbe/0xcf exc_bounds+0x98/0x98 restore_all_switch_stack+0xbe/0xcfThis only happens in 32-bit mode when VERW based mitigations like MDS/RFDS are enabled. This is because segment registers with an arbitrary user value can result in #GP when executing VERW. Intel SDM vol. 2C documents the following behavior for VERW instruction:
#GP(0) - If a memory operand effective address is outside the CS, DS, ES,
FS, or GS segment limit.
CLEAR_CPU_BUFFERSmacro executes VERW instruction before returning to user space. Use %ss selector to reference VERW operand. This ensures VERW will not #GP for an arbitrary user %ds.Fixes a0e2dab44d22 ("x86/entry_32: Add VERW just before userspace transition")Cc [email protected] # 5.10+Reported-by Robert Gill <[email protected]>Suggested-by Dave Hansen <[email protected]>Suggested-by Brian Gerst <[email protected]> # Use %ssSigned-off-by Pawan Gupta <[email protected]>--- arch/x86/include/asm/nospec-branch.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)arch/x86/include/asm/nospec-branch.h +0
-0@@ -318,12 +318,14 @@/* * Macro to execute VERW instruction that mitigate transient data sampling * attacks such as MDS. On affected systems a microcode update overloaded VERW
arch/x86/include/asm/nospec-branch.h line 328
Quoted 10 lines
arch/x86/include/asm/nospec-branch.h +4
-2-* instruction to also clear the CPU buffers. VERW clobbers CFLAGS.ZF. +* instruction to also clear the CPU buffers. VERW clobbers CFLAGS.ZF. Using %ss +* to reference VERW operand avoids a #GP fault for an arbitrary user %ds in +* 32-bit mode. * * Note: Only the memory operand variant of VERW clears the CPU buffers. */ .macro CLEAR_CPU_BUFFERS - ALTERNATIVE "", __stringify(verw _ASM_RIP(mds_verw_sel)), X86_FEATURE_CLEAR_CPU_BUF + ALTERNATIVE "", __stringify(verw %ss:_ASM_RIP(mds_verw_sel)), X86_FEATURE_CLEAR_CPU_BUF
Kselftest ldt_gdt.c in 32-bit mode results in oops when using SS. Dave suggested to use CS instead, as it can't be user controlled. Using CS is also more intuitive because the operand for verw i.e. mds_verw_sel is in code section. With this change no oops were observed running kselftests, I will be sending the updated version soon.