x86/entry_32: Move CLEAR_CPU_BUFFERS before restoring segments
Subsystems (2)
PATCH v3 1/1 x86/entry_32: Move CLEAR_CPU_BUFFERS before restoring segments
Robert Gill reported below #GP 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 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. Move CLEAR_CPU_BUFFERS to the macro RESTORE_REGS such that VERW is executed before user segments are restored. In the opportunistic SYSEXIT path use CLEAR_CPU_BUFFERS_SAFE that uses %ss instead.
Below are the locations where CLEAR_CPU_BUFFERS* is currently being used:
* entry_INT80_32(), entry_SYSENTER_32() and interrupts (via handle_exception_return) do:
restore_all_switch_stack:
[...]
RESTORE_REGS pop=4 clear_cpu_buf=1
pop %ebx pop %ecx pop %edx pop %esi pop %edi pop %ebp pop %eax verw 0xc0fc92c0 <------------- pop %ds pop %es pop %fs
* Opportunistic SYSEXIT explicitly does CLEAR_CPU_BUFFERS_SAFE:
[...]
verw %ss:0xc0fc92c0 <-------------
btrl $0x9,(%esp)
popf
pop %eax
sti
sysexit
* NMIs use RESTORE_ALL_NMI -> RESTORE_REGS:
nmi_return:
[...]
RESTORE_ALL_NMI cr3_reg=%edi
jmp 0xc0fb22e0 <asm_exc_nmi+612> test $0x1000,%edi je 0xc0fb22e0 <asm_exc_nmi+612> mov %edi,%cr3 pop %ebx pop %ecx pop %edx pop %esi pop %edi pop %ebp pop %eax verw 0xc0fc92c0 <------------- pop %ds pop %es pop %fs
---
v3:
- 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]
--- arch/x86/entry/entry_32.S | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
arch/x86/entry/entry_32.S +22 -6
@@ -253,6 +253,17 @@ .Lend_\@: .endm +/* + * Safer version of CLEAR_CPU_BUFFERS that uses %ss to reference VERW operand + * mds_verw_sel. This ensures VERW will not #GP for an arbitrary user %ds + * value. + */ +.macro CLEAR_CPU_BUFFERS_SAFE + ALTERNATIVE "jmp .Lskip_verw\@", "", X86_FEATURE_CLEAR_CPU_BUF + verw %ss:_ASM_RIP(mds_verw_sel) +.Lskip_verw\@: +.endm + .macro RESTORE_INT_REGS popl %ebx popl %ecx
@@ -263,8 +274,15 @@ popl %eax .endm -.macro RESTORE_REGS pop=0 +.macro RESTORE_REGS pop=0 clear_cpu_buf=0 RESTORE_INT_REGS + /* + * CLEAR_CPU_BUFFERS must be done before restoring segment + * registers to avoid #GP when executing VERW with user %ds. + */ + .if \clear_cpu_buf + CLEAR_CPU_BUFFERS + .endif 1: popl %ds 2: popl %es 3: popl %fs
@@ -299,7 +317,7 @@ BUG_IF_WRONG_CR3 - RESTORE_REGS pop=\pop + RESTORE_REGS pop=\pop clear_cpu_buf=1 .endm .macro CHECK_AND_APPLY_ESPFIX
@@ -871,6 +889,7 @@ SYM_FUNC_START(entry_SYSENTER_32) /* Now ready to switch the cr3 */ SWITCH_TO_USER_CR3 scratch_reg=%eax + CLEAR_CPU_BUFFERS_SAFE /* * Restore all flags except IF. (We restore IF separately because
@@ -881,7 +900,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.
@@ -950,8 +968,7 @@ restore_all_switch_stack: BUG_IF_WRONG_CR3 /* Restore user state */ - RESTORE_REGS pop=4 # skip orig_eax/error_code - CLEAR_CPU_BUFFERS + RESTORE_REGS pop=4 clear_cpu_buf=1 # skip orig_eax/error_code .Lirq_return: /* * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization
@@ -1144,7 +1161,6 @@ SYM_CODE_START(asm_exc_nmi) /* Not on SYSENTER stack. */ call exc_nmi - CLEAR_CPU_BUFFERS jmp .Lnmi_return .Lnmi_from_sysenter_stack:
---
base-commit: f2661062f16b2de5d7b6a5c42a9a5c96326b8454
change-id: 20240426-fix-dosemu-vm86-dd111a01737e
Best regards,
Signature · 2 lines
Thanks, Pawan
Quoted 195 lines · Pawan Gupta
Robert Gill reported below #GP 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 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. MoveCLEAR_CPU_BUFFERSto the macroRESTORE_REGSsuch that VERW is executed before user segments are restored. In the opportunistic SYSEXIT path useCLEAR_CPU_BUFFERS_SAFEthat uses %ss instead.Below are the locations where CLEAR_CPU_BUFFERS* is currently being used:
*
entry_INT80_32(),entry_SYSENTER_32()and interrupts (viahandle_exception_return) do:
restore_all_switch_stack:
[...]
RESTORE_REGSpop=4 clear_cpu_buf=1pop %ebx pop %ecx pop %edx pop %esi pop %edi pop %ebp pop %eax verw 0xc0fc92c0 <------------- pop %ds pop %es pop %fs* Opportunistic SYSEXIT explicitly does
CLEAR_CPU_BUFFERS_SAFE:[...] verw %ss:0xc0fc92c0 <------------- btrl $0x9,(%esp) popf pop %eax sti sysexit* NMIs use
RESTORE_ALL_NMI->RESTORE_REGS:nmi_return: [...] RESTORE_ALL_NMI cr3_reg=%edi jmp 0xc0fb22e0 <asm_exc_nmi+612> test $0x1000,%edi je 0xc0fb22e0 <asm_exc_nmi+612> mov %edi,%cr3 pop %ebx pop %ecx pop %edx pop %esi pop %edi pop %ebp pop %eax verw 0xc0fc92c0 <------------- pop %ds pop %es pop %fsFixes 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]>---
v3:
- SimplifyCLEAR_CPU_BUFFERS_SAFEby 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 likevm86()that might change %ds (Dave).v1: https://lore.kernel.org/r/[email protected]
--- arch/x86/entry/entry_32.S | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)arch/x86/entry/entry_32.S +22
-7@@ -253,6 +253,17 @@ .Lend_\@: .endm +/* + * Safer version of CLEAR_CPU_BUFFERS that uses %ss to reference VERW operand + * mds_verw_sel. This ensures VERW will not #GP for an arbitrary user %ds + * value. + */ +.macro CLEAR_CPU_BUFFERS_SAFE + ALTERNATIVE "jmp .Lskip_verw\@", "", X86_FEATURE_CLEAR_CPU_BUF + verw %ss:_ASM_RIP(mds_verw_sel) +.Lskip_verw\@: +.endm + .macro RESTORE_INT_REGS popl %ebx popl %ecx@@ -263,8 +274,15 @@ popl %eax .endm -.macro RESTORE_REGS pop=0 +.macro RESTORE_REGS pop=0 clear_cpu_buf=0 RESTORE_INT_REGS + /* + * CLEAR_CPU_BUFFERS must be done before restoring segment + * registers to avoid #GP when executing VERW with user %ds. + */ + .if \clear_cpu_buf + CLEAR_CPU_BUFFERS + .endif 1: popl %ds 2: popl %es 3: popl %fs@@ -299,7 +317,7 @@ BUG_IF_WRONG_CR3 - RESTORE_REGS pop=\pop + RESTORE_REGS pop=\pop clear_cpu_buf=1 .endm .macro CHECK_AND_APPLY_ESPFIX@@ -871,6 +889,7 @@ SYM_FUNC_START(entry_SYSENTER_32) /* Now ready to switch the cr3 */ SWITCH_TO_USER_CR3 scratch_reg=%eax + CLEAR_CPU_BUFFERS_SAFE /* * Restore all flags except IF. (We restore IF separately because@@ -881,7 +900,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.@@ -950,8 +968,7 @@ restore_all_switch_stack: BUG_IF_WRONG_CR3 /* Restore user state */ - RESTORE_REGS pop=4 # skip orig_eax/error_code - CLEAR_CPU_BUFFERS + RESTORE_REGS pop=4 clear_cpu_buf=1 # skip orig_eax/error_code .Lirq_return: /* * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization@@ -1144,7 +1161,6 @@ SYM_CODE_START(asm_exc_nmi) /* Not on SYSENTER stack. */ call exc_nmi - CLEAR_CPU_BUFFERS jmp .Lnmi_return .Lnmi_from_sysenter_stack: ---base-commit:
f2661062f16b2de5d7b6a5c42a9a5c96326b8454
change-id: 20240426-fix-dosemu-vm86-dd111a01737e
Quoted 6 lines
Best regards,
--
Thanks,
Pawan
Perhaps I should have been a bit clearer, but I meant adding the SS override to the VERW instructions in their present locations, instead of moving it into RESTORE_REGS. IIRC we don't want data reads (including stack pops) between the VERW instruction and IRET/SYSEXIT.
Brian Gerst
Quoted 3 lines · Brian Gerst
Perhaps I should have been a bit clearer, but I meant adding the SS override to the VERW instructions in their present locations, instead of moving it into
RESTORE_REGS.
I did consider that way, but moving it to RESTORE_REGS saves a callsite for CLEAR_CPU_BUFFERS. I believe Dave prefered it within RESTORE_REGS, but that was before the SS override. Unless anyone has a strong preference, I will change the current callsites to include the SS override, and not move CLEAR_CPU_BUFFERS to RESTORE_REGS.
Quoted 2 lines
IIRC we don't want data reads (including stack pops) between the VERW instruction and IRET/SYSEXIT.
Stack pops are okay if the memory *and* the registers doesn't contain sensitive data, which doesn't seem to be the case here. But, SS override is a safer option.
BTW, Dave pointed out that CLEAR_CPU_BUFFERS needs to be done after RESTORE_ALL_NMI in the NMI return path, that adds an extra callsite:
---
arch/x86/entry/entry_32.S +2 -1
@@ -1144,7 +1144,6 @@ SYM_CODE_START(asm_exc_nmi) /* Not on SYSENTER stack. */ call exc_nmi - CLEAR_CPU_BUFFERS jmp .Lnmi_return .Lnmi_from_sysenter_stack:
@@ -1165,6 +1164,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
@@ -1206,6 +1206,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)