Skip to contentSkip to conversation

x86: clear EXTRA_REGS for all executable formats

PATCH In review Denys Vlasenko 3 participants 3 messages Apr 4, 2015 – Apr 7, 2015

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

PATCH 1/1 x86: clear EXTRA_REGS for all executable formats

On failure, sys_execve does not clobber EXTRA_REGS, so we can just return to userpsace without saving/restoring them.

On success, ELF_PLAT_INIT in sys_execve clears all these registers.

On other executable formats:
binfmt_flat.c has similar FLAT_PLAT_INIT, but x86 (and everyone else except sh) doesn't define it.
binfmt_elf_fdpic.c has ELF_FDPIC_PLAT_INIT, but x86 (and most others) doesn't define it.
There are no such hooks in binfmt_aout.c et al. We inherit EXTRA_REGS from the prior executable.

This was not intended.

This change removes SAVE/RESTORE_EXTRA_REGS in stub_execve, removes register clearing in ELF_PLAT_INIT,
and instead simply clears them on success in stub_execve.

Run-tested.

CC Linus Torvalds <[email protected]>
CC Steven Rostedt <[email protected]>
CC Ingo Molnar <[email protected]>
CC Borislav Petkov <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Alexei Starovoitov <[email protected]>
CC Will Drewry <[email protected]>
CC Kees Cook <[email protected]>
---
 arch/x86/include/asm/calling.h |  9 ++++++++
 arch/x86/include/asm/elf.h     |  7 +++---
 arch/x86/kernel/entry_64.S     | 50 +++++++++++++++++++-----------------------
 3 files changed, 35 insertions(+), 31 deletions(-)
arch/x86/include/asm/calling.h +9 -0
index 4b5f7bf..1c8b50e 100644
@@ -151,6 +151,15 @@ For 32-bit we have the following conventions - kernel is built with
 	movq_cfi_restore 5*8+\offset, rbx
 	.endm
 
+	.macro ZERO_EXTRA_REGS
+	xorl	%r15d, %r15d
+	xorl	%r14d, %r14d
+	xorl	%r13d, %r13d
+	xorl	%r12d, %r12d
+	xorl	%ebp, %ebp
+	xorl	%ebx, %ebx
+	.endm
+
 	.macro RESTORE_C_REGS_HELPER rstor_rax=1, rstor_rcx=1, rstor_r11=1, rstor_r8910=1, rstor_rdx=1
 	.if \rstor_r11
 	movq_cfi_restore 6*8, r11
arch/x86/include/asm/elf.h +4 -3
index bd292ce..935588d9 100644
@@ -171,10 +171,11 @@ do {						\
 static inline void elf_common_init(struct thread_struct *t,
 				   struct pt_regs *regs, const u16 ds)
 {
-	regs->ax = regs->bx = regs->cx = regs->dx = 0;
-	regs->si = regs->di = regs->bp = 0;
+	/* Commented-out registers are cleared in stub_execve */
+	/*regs->ax = regs->bx =*/ regs->cx = regs->dx = 0;
+	regs->si = regs->di /*= regs->bp*/ = 0;
 	regs->r8 = regs->r9 = regs->r10 = regs->r11 = 0;
-	regs->r12 = regs->r13 = regs->r14 = regs->r15 = 0;
+	/*regs->r12 = regs->r13 = regs->r14 = regs->r15 = 0;*/
 	t->fs = t->gs = 0;
 	t->fsindex = t->gsindex = 0;
 	t->ds = t->es = ds;
arch/x86/kernel/entry_64.S +22 -28

72 lines not on this page · raw message

Signature · 1 line
1.8.1.4
#1 lore raw
Quoted 5 lines · Denys Vlasenko

On failure, sys_execve does not clobber EXTRA_REGS, so we can just return to userpsace without saving/restoring them.

On success, ELF_PLAT_INIT in sys_execve clears all these registers.

Quoted 7 lines

On other executable formats:
binfmt_flat.c has similar FLAT_PLAT_INIT, but x86 (and everyone else except sh) doesn't define it.
binfmt_elf_fdpic.c has ELF_FDPIC_PLAT_INIT, but x86 (and most others) doesn't define it.
There are no such hooks in binfmt_aout.c et al. We inherit EXTRA_REGS from the prior executable.

None of these are supported on x86-64. The first two are for embedded MMU-less systems. a.out is only supported for a few old arches (x86-32, alpha, m68k). ia32_aout.c does explicitly clear the extra registers.

--
Brian Gerst

#2 lore raw

[tip:x86/asm] x86/asm/entry: Clear EXTRA_REGS for all executable formats

In reply to Denys Vlasenko
x86/asm/entry: Clear EXTRA_REGS for all executable formats
Commit-ID:  fc3e958a2b552fe6210e6de3bebb4348156a0b5f
Gitweb:     http://git.kernel.org/tip/fc3e958a2b552fe6210e6de3bebb4348156a0b5f
Author:     Denys Vlasenko <[email protected]>
AuthorDate: Sat, 4 Apr 2015 20:55:19 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 6 Apr 2015 09:24:08 +0200

x86/asm/entry: Clear EXTRA_REGS for all executable formats

On failure, sys_execve() does not clobber EXTRA_REGS, so we can just return to userpsace without saving/restoring them.

On success, ELF_PLAT_INIT() in sys_execve() clears all these registers.

On other executable formats:

- binfmt_flat.c has similar FLAT_PLAT_INIT, but x86 (and everyone else except sh) doesn't define it.

- binfmt_elf_fdpic.c has ELF_FDPIC_PLAT_INIT, but x86 (and most others) doesn't define it.

- There are no such hooks in binfmt_aout.c et al. We inherit EXTRA_REGS from the prior executable.

This inconsistency was not intended.

This change removes SAVE/RESTORE_EXTRA_REGS in stub_execve, removes register clearing in ELF_PLAT_INIT(),
and instead simply clears them on success in stub_execve.

Run-tested.

Cc Alexei Starovoitov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Kees Cook <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Will Drewry <[email protected]>
---
 arch/x86/include/asm/calling.h |  9 ++++++++
 arch/x86/include/asm/elf.h     |  7 +++---
 arch/x86/kernel/entry_64.S     | 50 +++++++++++++++++++-----------------------
 3 files changed, 35 insertions(+), 31 deletions(-)
arch/x86/include/asm/calling.h +9 -0
index 4b5f7bf..1c8b50e 100644
@@ -151,6 +151,15 @@ For 32-bit we have the following conventions - kernel is built with
 	movq_cfi_restore 5*8+\offset, rbx
 	.endm
 
+	.macro ZERO_EXTRA_REGS
+	xorl	%r15d, %r15d
+	xorl	%r14d, %r14d
+	xorl	%r13d, %r13d
+	xorl	%r12d, %r12d
+	xorl	%ebp, %ebp
+	xorl	%ebx, %ebx
+	.endm
+
 	.macro RESTORE_C_REGS_HELPER rstor_rax=1, rstor_rcx=1, rstor_r11=1, rstor_r8910=1, rstor_rdx=1
 	.if \rstor_r11
 	movq_cfi_restore 6*8, r11
arch/x86/include/asm/elf.h +4 -3
index ca3347a..3563107 100644
@@ -171,10 +171,11 @@ do {						\
 static inline void elf_common_init(struct thread_struct *t,
 				   struct pt_regs *regs, const u16 ds)
 {
-	regs->ax = regs->bx = regs->cx = regs->dx = 0;
-	regs->si = regs->di = regs->bp = 0;
+	/* Commented-out registers are cleared in stub_execve */
+	/*regs->ax = regs->bx =*/ regs->cx = regs->dx = 0;
+	regs->si = regs->di /*= regs->bp*/ = 0;
 	regs->r8 = regs->r9 = regs->r10 = regs->r11 = 0;
-	regs->r12 = regs->r13 = regs->r14 = regs->r15 = 0;
+	/*regs->r12 = regs->r13 = regs->r14 = regs->r15 = 0;*/
 	t->fs = t->gs = 0;
 	t->fsindex = t->gsindex = 0;
 	t->ds = t->es = ds;
arch/x86/kernel/entry_64.S +22 -28

72 lines not on this page · raw message