x86,entry: Use PUSH_AND_CLEAR_REGS for compat
Subsystems (2)
PATCH v3 1/1 x86,entry: Use PUSH_AND_CLEAR_REGS for compat
Since the upper regs don't exist for ia32 code, preserving them doesn't hurt and it simplifies the code.
If there was any attack surface on this, that attack surface already exists for INT80 and needs to be otherwise dealt with.
Notably:
- SYSENTER: didn't clear si, dx, cx.
- SYSCALL, INT80: *do* clear si since the C functions don't take a second argument.
Add a clear_rsi argument to CLEAR_REGS for these 3 sites, and have SYSENTER clear everything (no code relies on those registers not being cleared and selftests pass).
---
arch/x86/entry/calling.h +10 -6
@@ -63,13 +63,15 @@ For 32-bit we have the following conventions - kernel is built with* for assembly code: */ -.macro PUSH_REGS rdx=%rdx rax=%rax save_ret=0 +.macro PUSH_REGS rdx=%rdx rax=%rax save_ret=0 save_rdi=1 .if \save_ret pushq %rsi /* pt_regs->si */ movq 8(%rsp), %rsi /* temporarily store the return address in %rsi */ movq %rdi, 8(%rsp) /* pt_regs->di (overwriting original return address) */ .else + .if \save_rdi pushq %rdi /* pt_regs->di */ + .endif pushq %rsi /* pt_regs->si */ .endif pushq \rdx /* pt_regs->dx */
@@ -92,13 +94,16 @@ For 32-bit we have the following conventions - kernel is built with .endif .endm -.macro CLEAR_REGS +.macro CLEAR_REGS clear_rsi=0/* * Sanitize registers of values that a speculation attack might * otherwise want to exploit. The lower registers are likely clobbered * well before they could be put to use in a speculative execution * gadget. */ + .if \clear_rsi + xorl %esi, %esi /* nospec si */ + .endif xorl %edx, %edx /* nospec dx */ xorl %ecx, %ecx /* nospec cx */ xorl %r8d, %r8d /* nospec r8 */
@@ -111,12 +116,11 @@ For 32-bit we have the following conventions - kernel is built with xorl %r13d, %r13d /* nospec r13 */ xorl %r14d, %r14d /* nospec r14 */ xorl %r15d, %r15d /* nospec r15 */ - .endm -.macro PUSH_AND_CLEAR_REGS rdx=%rdx rax=%rax save_ret=0 - PUSH_REGS rdx=\rdx, rax=\rax, save_ret=\save_ret - CLEAR_REGS +.macro PUSH_AND_CLEAR_REGS rdx=%rdx rax=%rax save_ret=0 save_rdi=1 clear_rsi=0 + PUSH_REGS rdx=\rdx, rax=\rax, save_ret=\save_ret save_rdi=\save_rdi + CLEAR_REGS clear_rsi=\clear_rsi .endm .macro POP_REGS pop_rdi=1 skip_r11rcx=0
arch/x86/entry/entry_64_compat.S +3 -84
105 lines not on this page · raw message
Quoted 5 lines · Peter Zijlstra
Since the upper regs don't exist for ia32 code, preserving them doesn't hurt and it simplifies the code.
Quoted 9 lines
If there was any attack surface on this, that attack surface already exists for INT80 and needs to be otherwise dealt with.
Notably:
- SYSENTER: didn't clear si, dx, cx.
- SYSCALL, INT80: *do* clear si since the C functions don't take a second argument.
If CLEAR_REGS for SYSCALL, INT80 clears si, it is better, IMO, to make CLEAR_REGS clear si unconditionally.
SYSCALL, INT80 will explicitly clear si via calling syscall_enter_from_user_mode().
But some handlers called from the macro idtentry don't clear si explicitly, although it is likely to be cleared.
So if %rsi is a concern for SYSCALL, INT80, please make CLEAR_REGS clear %rsi unconditionally.
Thanks
Lai
Quoted 11 lines · Lai Jiangshan
Quoted 6 lines · Peter Zijlstra
Notably:
- SYSENTER: didn't clear si, dx, cx.
- SYSCALL, INT80: *do* clear si since the C functions don't take a second argument.If
CLEAR_REGSfor SYSCALL, INT80 clears si, it is better, IMO, to makeCLEAR_REGSclear si unconditionally.
Well, I didn't want to add the overhead to 64bit native syscalls, but Linus just suggested the same thing elsewhere. So yeah.
He also suggested cleaning up INT80 like below to get rid of the save_rdi wart.
(Linus, can I add your SoB to the thing?)
---
Subject: x86/entry: Simplify entry_INT80_compat()
From: Linus Torvalds <[email protected]>
Date: Fri Apr 29 22:52:21 CEST 2022
Instead of playing silly games with rdi, use rax for simpler and more consistent code.
--- arch/x86/entry/entry_64_compat.S | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
arch/x86/entry/entry_64_compat.S +9 -10
@@ -362,26 +362,25 @@ SYM_CODE_START(entry_INT80_compat) /* switch to thread stack expects orig_ax and rdi to be pushed */ pushq %rax /* pt_regs->orig_ax */ - pushq %rdi /* pt_regs->di */ /* Need to switch before accessing the thread stack. */ - SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi + SWITCH_TO_KERNEL_CR3 scratch_reg=%rax /* In the Xen PV case we already run on the thread stack. */ ALTERNATIVE "", "jmp .Lint80_keep_stack", X86_FEATURE_XENPV - movq %rsp, %rdi + movq %rsp, %rax movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp - pushq 6*8(%rdi) /* regs->ss */ - pushq 5*8(%rdi) /* regs->rsp */ - pushq 4*8(%rdi) /* regs->eflags */ - pushq 3*8(%rdi) /* regs->cs */ - pushq 2*8(%rdi) /* regs->ip */ - pushq 1*8(%rdi) /* regs->orig_ax */ - pushq (%rdi) /* pt_regs->di */ + pushq 5*8(%rax) /* regs->ss */ + pushq 4*8(%rax) /* regs->rsp */ + pushq 3*8(%rax) /* regs->eflags */ + pushq 2*8(%rax) /* regs->cs */ + pushq 1*8(%rax) /* regs->ip */ + pushq 0*8(%rax) /* regs->orig_ax */ .Lint80_keep_stack: + pushq %rdi /* pt_regs->di */ pushq %rsi /* pt_regs->si */ xorl %esi, %esi /* nospec si */ pushq %rdx /* pt_regs->dx */
PATCH v4 1/1 x86,entry: Use PUSH_AND_CLEAR_REGS for compat
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Lai Jiangshan
Quoted 6 lines · Peter Zijlstra
Notably:
- SYSENTER: didn't clear si, dx, cx.
- SYSCALL, INT80: *do* clear si since the C functions don't take a second argument.
Quoted 8 lines
Quoted 2 lines
If
CLEAR_REGSfor SYSCALL, INT80 clears si, it is better, IMO, to makeCLEAR_REGSclear si unconditionally.Well, I didn't want to add the overhead to 64bit native syscalls, but Linus just suggested the same thing elsewhere. So yeah.
He also suggested cleaning up INT80 like below to get rid of the
save_rdiwart.
Which then results in...
---
Subject: x86,entry: Use PUSH_AND_CLEAR_REGS for compat
From: Peter Zijlstra <[email protected]>
Date: Sat, 9 Apr 2022 00:38:27 +0200
Since the upper regs don't exist for ia32 code, preserving them doesn't hurt and it simplifies the code.
If there was any attack surface on this, that attack surface already exists for INT80 and needs to be otherwise dealt with.
Notably:
- 32bit SYSENTER: didn't clear si, dx, cx.
- 32bit SYSCALL, INT80: *do* clear si since the C functions don't take a second argument.
- 64bit: don't clear si since the C functions take a second argument.
SYSENTER should be clearing all those 3 registers, nothing uses them and selftests pass. Unconditionally clear rsi since it simplifies code.
--- arch/x86/entry/calling.h | 1 arch/x86/entry/entry_64_compat.S | 87 +-------------------------------------- 2 files changed, 4 insertions(+), 84 deletions(-)
arch/x86/entry/calling.h +1 -0
@@ -99,6 +99,7 @@ For 32-bit we have the following convent* well before they could be put to use in a speculative execution * gadget. */ + xorl %esi, %esi /* nospec si */ xorl %edx, %edx /* nospec dx */ xorl %ecx, %ecx /* nospec cx */ xorl %r8d, %r8d /* nospec r8 */
arch/x86/entry/entry_64_compat.S +3 -84
105 lines not on this page · raw message
Quoted 2 lines · Peter Zijlstra
(Linus, can I add your SoB to the thing?)
If you teste this with some actual old int80 compat syscalls, then absolutely:
Quoted 3 lines
+ pushq %rdi /* pt_regs->di */
pushq %rsi /* pt_regs->si */
xorl %esi, %esi /* nospec si */It would probably make sense to add a comment about why %rdi isn't cleared when pushed, like all the other registers are.
Even if that comment is just "%rdi will be overwritten as arg0 of the call to C, so no need to clear it".
Maybe as part of the PUSH_AND_CLEAR_REGS changes?
Linus
Quoted 5 lines · Linus Torvalds
Quoted 2 lines · Peter Zijlstra
(Linus, can I add your SoB to the thing?)
If you teste this with some actual old int80 compat syscalls, then absolutely:
I ran tools/testing/selftests/x86/*_32 on it. That definitely tickles the int80 path. Also, without the off-by-one fixed that gives some generous helpings of segfault.
Quoted 1 line
Signed-off-by Linus Torvalds <[email protected]>
Thanks!
Quoted 11 lines
Quoted 3 lines
+ pushq %rdi /* pt_regs->di */ pushq %rsi /* pt_regs->si */ xorl %esi, %esi /* nospec si */It would probably make sense to add a comment about why %rdi isn't cleared when pushed, like all the other registers are.
Even if that comment is just "%rdi will be overwritten as arg0 of the call to C, so no need to clear it".
Maybe as part of the
PUSH_AND_CLEAR_REGSchanges?
I'll stick the comment on.