Skip to contentSkip to conversation

x86: PUSH_AND_CLEAR_REGS_COMPAT

RFC PATCH In review Peter Zijlstra 3 participants 4 messages Apr 8, 2022 – Apr 10, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 x86: PUSH_AND_CLEAR_REGS_COMPAT

How insane?

---
 arch/x86/entry/calling.h         | 25 ++++++++----
 arch/x86/entry/entry_64_compat.S | 87 ++--------------------------------------
 2 files changed, 21 insertions(+), 91 deletions(-)
arch/x86/entry/calling.h +18 -7
index a4c061fb7c6e..7f938704da59 100644
@@ -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,7 +94,7 @@ For 32-bit we have the following conventions - kernel is built with
 	.endif
 .endm
 
-.macro CLEAR_REGS
+.macro CLEAR_REGS_lo
 	/*
 	 * Sanitize registers of values that a speculation attack might
 	 * otherwise want to exploit. The lower registers are likely clobbered
@@ -101,22 +103,31 @@ For 32-bit we have the following conventions - kernel is built with
 	 */
 	xorl	%edx,  %edx	/* nospec dx  */
 	xorl	%ecx,  %ecx	/* nospec cx  */
+	xorl	%ebx,  %ebx	/* nospec rbx */
+	xorl	%ebp,  %ebp	/* nospec rbp */
+.endm
+
+.macro CLEAR_REGS_hi
 	xorl	%r8d,  %r8d	/* nospec r8  */
 	xorl	%r9d,  %r9d	/* nospec r9  */
 	xorl	%r10d, %r10d	/* nospec r10 */
 	xorl	%r11d, %r11d	/* nospec r11 */
-	xorl	%ebx,  %ebx	/* nospec rbx */
-	xorl	%ebp,  %ebp	/* nospec rbp */
 	xorl	%r12d, %r12d	/* nospec r12 */
 	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 save_rdi=1
+	PUSH_REGS rdx=\rdx, rax=\rax, save_ret=\save_ret save_rdi=\save_rdi
+	CLEAR_REGS_lo
+	CLEAR_REGS_hi
 .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_COMPAT rdx=%rdx rax=%rax save_ret=0 save_rdi=1
+	CLEAR_REGS_hi
+	PUSH_REGS rdx=\rdx, rax=\rax, save_ret=\save_ret save_rdi=\save_rdi
+	CLEAR_REGS_lo
 .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

#1 lore raw
Quoted 2 lines · Peter Zijlstra

How insane?

Anyway, the questino is; since int80 doesn't wipe the high regs, can we get away with the SYS*_compat things not doing that either and then all using the normal PUSH_AND_CLEAR_REGS without having to invent _COMPAT for that?

#2 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 2 lines · Peter Zijlstra

How insane?

Anyway, the questino is; since int80 doesn't wipe the high regs, can we get away with the SYS*_compat things not doing that either and then all using the normal PUSH_AND_CLEAR_REGS without having to invent _COMPAT for that?

I'd rather not, clearing the register values on the stack is a good thing as it gives attackers less control. In fact I wish we could do that for the 64-bit syscalls, but alas, callee-saved registers and all.

Signature · 1 line
Josh
#3 lore raw
In reply to Peter Zijlstra
Quoted 9 lines · Peter Zijlstra
Quoted 2 lines · Peter Zijlstra

How insane?

Anyway, the questino is; since int80 doesn't wipe the high regs, can we get away with the SYS*_compat things not doing that either and then all using the normal PUSH_AND_CLEAR_REGS without having to invent _COMPAT for that?

For a pure 32-bit process it doesn't matter whether the upper registers are preserved or not, since they are inaccessible. Mixed-mode code may assume the upper registers are preserved across a call to 32-bit code, although it would be very unlikely to encounter SYSENTER or SYSCALL instructions anywhere but the Linux VDSO (and use anywhere else would cause undesirable results).

There is no compelling reason to not preserve them, and code simplification is a good benefit.

--
Brian Gerst