Skip to contentSkip to conversation

x86: Make x32 syscall support conditional on a kernel parameter

RFC PATCH In review Ben Hutchings 3 participants 5 messages Nov 6, 2014 – Dec 7, 2014

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

RFC PATCH 1/1 x86: Make x32 syscall support conditional on a kernel parameter

Enabling x32 in a distribution default kernel increases its attack surface while providing no benefit to the vast majority of its users. No-one seems interested in regularly checking for vulnerabilities specific to x32 (at least no-one with a white hat).

Still, requiring a separate or custom configuration just to turn on x32 seems wasteful. And the only differences on syscall entry are two instructions (mask out the x32 flag and compare the syscall number).

So pad the standard comparison with a nop and add a kernel parameter "syscall.x32" which controls whether this is replaced with the x32 version at boot time. Add a Kconfig parameter to set the default.

---
This is currently used in Debian, where x32 is an unofficial port that can be installed in parallel with amd64 (multiarch). I acknowledge the patch is a bit hacky, e.g. it has several magic numbers. I'm sending this to find out whether anything like this would be acceptable upstream.

Ben.

 Documentation/kernel-parameters.txt |  4 ++++
 arch/x86/Kconfig                    |  8 +++++++
 arch/x86/include/asm/elf.h          |  8 ++++++-
 arch/x86/kernel/entry_64.S          | 26 ++++++++++++++++++----
 arch/x86/kernel/syscall_64.c        | 43 +++++++++++++++++++++++++++++++++++++
 5 files changed, 84 insertions(+), 5 deletions(-)
Documentation/kernel-parameters.txt +4 -0
index 4c81a86..1e161b0 100644
@@ -3397,6 +3397,10 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 
 	switches=	[HW,M68k]
 
+	syscall.x32=	[KNL,x86_64] Enable/disable use of x32 syscalls on
+			an x86_64 kernel where CONFIG_X86_X32 is enabled.
+			Default depends on CONFIG_X86_X32_DISABLED.
+
 	sysfs.deprecated=0|1 [KNL]
 			Enable/disable old style sysfs layout for old udev
 			on older distributions. When this option is enabled
arch/x86/Kconfig +8 -0
index ded8a67..9627922 100644
@@ -2455,6 +2455,14 @@ config X86_X32
 	  elf32_x86_64 support enabled to compile a kernel with this
 	  option set.
 
+config X86_X32_DISABLED
+	bool "x32 ABI disabled by default"
+	depends on X86_X32
+	default n
+	help
+	  Disable the x32 ABI unless explicitly enabled using the
+	  kernel paramter "syscall.x32=y".
+
 config COMPAT
 	def_bool y
 	depends on IA32_EMULATION || X86_X32
arch/x86/include/asm/elf.h +7 -1
index ca3347a..2a8b89a 100644
@@ -154,6 +154,12 @@ do {						\
 
 #else /* CONFIG_X86_32 */
 
+#ifdef CONFIG_X86_X32_ABI
+extern bool x32_enabled;
+#else
+#define x32_enabled 0
+#endif
+
 /*
  * This is used to ensure we don't load something for the wrong architecture.
  */
@@ -162,7 +168,7 @@ do {						\
 
 #define compat_elf_check_arch(x)					\
 	(elf_check_arch_ia32(x) ||					\
-	 (IS_ENABLED(CONFIG_X86_X32_ABI) && (x)->e_machine == EM_X86_64))
+	 (x32_enabled && (x)->e_machine == EM_X86_64))
 
 #if __USER32_DS != __USER_DS
 # error "The following code assumes __USER32_DS == __USER_DS"
arch/x86/kernel/entry_64.S +22 -4
index df088bb..50b265a 100644
@@ -414,8 +414,12 @@ system_call_fastpath:
 #if __SYSCALL_MASK == ~0
 	cmpq $__NR_syscall_max,%rax
 #else
-	andl $__SYSCALL_MASK,%eax
-	cmpl $__NR_syscall_max,%eax
+	.globl system_call_fast_compare
+	.globl system_call_fast_compare_end
+system_call_fast_compare:
+	cmpq $511,%rax			/* x32 syscalls start at 512 */
+	.byte P6_NOP4
+system_call_fast_compare_end:
 #endif
 	ja ret_from_sys_call  /* and return regs->ax */
 	movq %r10,%rcx
@@ -520,8 +524,12 @@ tracesys_phase2:
 #if __SYSCALL_MASK == ~0
 	cmpq $__NR_syscall_max,%rax
 #else
-	andl $__SYSCALL_MASK,%eax
-	cmpl $__NR_syscall_max,%eax
+	.globl system_call_trace_compare
+	.globl system_call_trace_compare_end
+system_call_trace_compare:
+	cmpq $511,%rax			/* x32 syscalls start at 512 */
+	.byte P6_NOP4
+system_call_trace_compare_end:
 #endif
 	ja   int_ret_from_sys_call	/* RAX(%rsp) is already set */
 	movq %r10,%rcx	/* fixup for C */
@@ -593,6 +601,16 @@ int_restore_rest:
 	CFI_ENDPROC
 END(system_call)
 
+#if __SYSCALL_MASK != ~0
+	/* This replaces the usual comparisons if syscall.x32 is set */
+	.globl system_call_mask_compare
+	.globl system_call_mask_compare_end
+system_call_mask_compare:
+	andl $__SYSCALL_MASK,%eax
+	cmpl $__NR_syscall_max,%eax
+system_call_mask_compare_end:
+#endif
+
 	.macro FORK_LIKE func
 ENTRY(stub_\func)
 	CFI_STARTPROC
arch/x86/kernel/syscall_64.c +43 -0

54 lines not on this page · raw message

Signature · 2 lines
Ben Hutchings
Beware of programmers who carry screwdrivers. - Leonard Brandwein
#1 lore raw

arch/x86/Kconfig config X86_X32 · line 2464

Quoted 13 lines · Ben Hutchings
arch/x86/Kconfig +7 -0
@@ -2455,6 +2455,14 @@ config X86_X32
          elf32_x86_64 support enabled to compile a kernel with this
          option set.
 
+config X86_X32_DISABLED
+       bool "x32 ABI disabled by default"
+       depends on X86_X32
+       default n
+       help
+         Disable the x32 ABI unless explicitly enabled using the
+         kernel paramter "syscall.x32=y".

parameter

arch/x86/kernel/syscall_64.c asmlinkage const sys_call_ptr_t sys_call_table[__NR_syscall_max+1] = { · line 55

Quoted 10 lines
arch/x86/kernel/syscall_64.c +5 -0
index 4ac730b..7a6e66f 100644
 
+static int __init x32_enable(void)
+{
+       BUG_ON(system_call_fast_compare_end - system_call_fast_compare != 10);
+       BUG_ON(system_call_trace_compare_end - system_call_trace_compare != 10);
+       BUG_ON(system_call_mask_compare_end - system_call_mask_compare != 10);

BUILD_BUG_ON

arch/x86/kernel/syscall_64.c asmlinkage const sys_call_ptr_t sys_call_table[__NR_syscall_max+1] = { · line 64

Quoted 9 lines
arch/x86/kernel/syscall_64.c +9 -0
+
+       if (x32_enabled) {
+               text_poke_early(system_call_fast_compare,
+                               system_call_mask_compare, 10);
+               text_poke_early(system_call_trace_compare,
+                               system_call_mask_compare, 10);
+#ifdef CONFIG_X86_X32_DISABLED
+               pr_info("Enabled x32 syscalls\n");
+#endif

I don't think it hurts to print this unconditionally.

arch/x86/kernel/syscall_64.c asmlinkage const sys_call_ptr_t sys_call_table[__NR_syscall_max+1] = { · line 75

Quoted 11 lines
arch/x86/kernel/syscall_64.c +11 -0
+       }
+#ifndef CONFIG_X86_X32_DISABLED
+       else
+               pr_info("Disabled x32 syscalls\n");
+#endif
+
+       return 0;
+}
+late_initcall(x32_enable);
+
+#endif

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#2 lore raw
Quoted 1 line · Geert Uytterhoeven

On Thu, Nov 6, 2014 at 4:53 AM, Ben Hutchings <[email protected]> wrote:

[...]

Quoted 5 lines
Quoted 4 lines
arch/x86/kernel/syscall_64.c +0 -0
index 4ac730b..7a6e66f 100644
Quoted 7 lines

arch/x86/kernel/syscall_64.c asmlinkage const sys_call_ptr_t sys_call_table[__NR_syscall_max+1] = { · line 55

Quoted 5 lines
arch/x86/kernel/syscall_64.c +5 -0
+static int __init x32_enable(void)
+{
+       BUG_ON(system_call_fast_compare_end - system_call_fast_compare != 10);
+       BUG_ON(system_call_trace_compare_end - system_call_trace_compare != 10);
+       BUG_ON(system_call_mask_compare_end - system_call_mask_compare != 10);

BUILD_BUG_ON

No, these can't be evaluated at compile time because the symbols are external.

Quoted 11 lines

arch/x86/kernel/syscall_64.c asmlinkage const sys_call_ptr_t sys_call_table[__NR_syscall_max+1] = { · line 64

Quoted 9 lines
arch/x86/kernel/syscall_64.c +9 -0
+
+       if (x32_enabled) {
+               text_poke_early(system_call_fast_compare,
+                               system_call_mask_compare, 10);
+               text_poke_early(system_call_trace_compare,
+                               system_call_mask_compare, 10);
+#ifdef CONFIG_X86_X32_DISABLED
+               pr_info("Enabled x32 syscalls\n");
+#endif

I don't think it hurts to print this unconditionally.

[...]

It seemed like it would be unnecessary noise in an x32-only system.

Ben.

Signature · 2 lines
Ben Hutchings
Beware of programmers who carry screwdrivers. - Leonard Brandwein
#3 lore raw
In reply to Ben Hutchings
Quoted 112 lines · Ben Hutchings

Enabling x32 in a distribution default kernel increases its attack surface while providing no benefit to the vast majority of its users. No-one seems interested in regularly checking for vulnerabilities specific to x32 (at least no-one with a white hat).

Still, requiring a separate or custom configuration just to turn on x32 seems wasteful. And the only differences on syscall entry are two instructions (mask out the x32 flag and compare the syscall number).

So pad the standard comparison with a nop and add a kernel parameter "syscall.x32" which controls whether this is replaced with the x32 version at boot time. Add a Kconfig parameter to set the default.

---
This is currently used in Debian, where x32 is an unofficial port that can be installed in parallel with amd64 (multiarch). I acknowledge the patch is a bit hacky, e.g. it has several magic numbers. I'm sending this to find out whether anything like this would be acceptable upstream.

Ben.

 Documentation/kernel-parameters.txt |  4 ++++
 arch/x86/Kconfig                    |  8 +++++++
 arch/x86/include/asm/elf.h          |  8 ++++++-
 arch/x86/kernel/entry_64.S          | 26 ++++++++++++++++++----
 arch/x86/kernel/syscall_64.c        | 43 +++++++++++++++++++++++++++++++++++++
 5 files changed, 84 insertions(+), 5 deletions(-)
Documentation/kernel-parameters.txt +4 -0
index 4c81a86..1e161b0 100644
@@ -3397,6 +3397,10 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 
 	switches=	[HW,M68k]
 
+	syscall.x32=	[KNL,x86_64] Enable/disable use of x32 syscalls on
+			an x86_64 kernel where CONFIG_X86_X32 is enabled.
+			Default depends on CONFIG_X86_X32_DISABLED.
+
 	sysfs.deprecated=0|1 [KNL]
 			Enable/disable old style sysfs layout for old udev
 			on older distributions. When this option is enabled
arch/x86/Kconfig +8 -0
index ded8a67..9627922 100644
@@ -2455,6 +2455,14 @@ config X86_X32
 	  elf32_x86_64 support enabled to compile a kernel with this
 	  option set.
 
+config X86_X32_DISABLED
+	bool "x32 ABI disabled by default"
+	depends on X86_X32
+	default n
+	help
+	  Disable the x32 ABI unless explicitly enabled using the
+	  kernel paramter "syscall.x32=y".
+
 config COMPAT
 	def_bool y
 	depends on IA32_EMULATION || X86_X32
arch/x86/include/asm/elf.h +7 -1
index ca3347a..2a8b89a 100644
@@ -154,6 +154,12 @@ do {						\
 
 #else /* CONFIG_X86_32 */
 
+#ifdef CONFIG_X86_X32_ABI
+extern bool x32_enabled;
+#else
+#define x32_enabled 0
+#endif
+
 /*
  * This is used to ensure we don't load something for the wrong architecture.
  */
@@ -162,7 +168,7 @@ do {						\
 
 #define compat_elf_check_arch(x)					\
 	(elf_check_arch_ia32(x) ||					\
-	 (IS_ENABLED(CONFIG_X86_X32_ABI) && (x)->e_machine == EM_X86_64))
+	 (x32_enabled && (x)->e_machine == EM_X86_64))
 
 #if __USER32_DS != __USER_DS
 # error "The following code assumes __USER32_DS == __USER_DS"
arch/x86/kernel/entry_64.S +6 -2
index df088bb..50b265a 100644
@@ -414,8 +414,12 @@ system_call_fastpath:
 #if __SYSCALL_MASK == ~0
 	cmpq $__NR_syscall_max,%rax
 #else
-	andl $__SYSCALL_MASK,%eax
-	cmpl $__NR_syscall_max,%eax
+	.globl system_call_fast_compare
+	.globl system_call_fast_compare_end
+system_call_fast_compare:
+	cmpq $511,%rax			/* x32 syscalls start at 512 */
+	.byte P6_NOP4
+system_call_fast_compare_end:
 #endif
 	ja ret_from_sys_call  /* and return regs->ax */
 	movq %r10,%rcx
@@ -520,8 +524,12 @@ tracesys_phase2:
 #if __SYSCALL_MASK == ~0

arch/x86/kernel/entry_64.S tracesys_phase2: · line 532

Quoted 10 lines
arch/x86/kernel/entry_64.S +6 -2
 	cmpq $__NR_syscall_max,%rax
 #else
-	andl $__SYSCALL_MASK,%eax
-	cmpl $__NR_syscall_max,%eax
+	.globl system_call_trace_compare
+	.globl system_call_trace_compare_end
+system_call_trace_compare:
+	cmpq $511,%rax			/* x32 syscalls start at 512 */
+	.byte P6_NOP4
+system_call_trace_compare_end:

I think that, if x32 is disabled, that andl $__SYSCALL_MASK,%rax should be nopped out. Can this, and the comparison, use the standard alternatives mechanism?

And why are there extra syscall numbers in x32 land? There's an x32 bit *and* extra numbers. I'm confused.

That magic number is bad. I would make it a real #define and put an appropriate assertion into whatever generates syscalls_64.h.

Also, if you do this, can you change all four instances of %eax in there to %rax? %eax is just wrong AFAICT.

Also, as a heads up to anyone who actually uses this: I was confused about the audit situation on x32-enabled kernels. I thought that this patch had been applied:

https://www.redhat.com/archives/linux-audit/2014-May/msg00099.html

but it wasn't. As a result, the commit message for this fix is wrong:

81f49a8fd708 x86, x32, audit: Fix x32's AUDIT_ARCH wrt audit

It *is* a user-visible fix and should possibly go to -stable, especially if anyone actually uses this *!$@ thing. Of course, audit is royally screwed up on x32-enabled kernels anyway. I admit that I find myself barely caring.

--Andy

#4 lore raw
Quoted 14 lines · Andy Lutomirski
Quoted 13 lines · Ben Hutchings

Enabling x32 in a distribution default kernel increases its attack surface while providing no benefit to the vast majority of its users. No-one seems interested in regularly checking for vulnerabilities specific to x32 (at least no-one with a white hat).

Still, requiring a separate or custom configuration just to turn on x32 seems wasteful. And the only differences on syscall entry are two instructions (mask out the x32 flag and compare the syscall number).

So pad the standard comparison with a nop and add a kernel parameter "syscall.x32" which controls whether this is replaced with the x32 version at boot time. Add a Kconfig parameter to set the default.

[...]

Quoted 24 lines
Quoted 24 lines
arch/x86/kernel/entry_64.S +7 -4
@@ -414,8 +414,12 @@ system_call_fastpath:
 #if __SYSCALL_MASK == ~0
 	cmpq $__NR_syscall_max,%rax
 #else
-	andl $__SYSCALL_MASK,%eax
-	cmpl $__NR_syscall_max,%eax
+	.globl system_call_fast_compare
+	.globl system_call_fast_compare_end
+system_call_fast_compare:
+	cmpq $511,%rax			/* x32 syscalls start at 512 */
+	.byte P6_NOP4
+system_call_fast_compare_end:
 #endif
 	ja ret_from_sys_call  /* and return regs->ax */
 	movq %r10,%rcx
@@ -520,8 +524,12 @@ tracesys_phase2:
 #if __SYSCALL_MASK == ~0
 	cmpq $__NR_syscall_max,%rax
 #else
-	andl $__SYSCALL_MASK,%eax
-	cmpl $__NR_syscall_max,%eax
+	.globl system_call_trace_compare
Quoted 8 lines

arch/x86/kernel/entry_64.S tracesys_phase2: · line 532

Quoted 5 lines
arch/x86/kernel/entry_64.S +5 -0
+	.globl system_call_trace_compare_end
+system_call_trace_compare:
+	cmpq $511,%rax			/* x32 syscalls start at 512 */
+	.byte P6_NOP4
+system_call_trace_compare_end:

I think that, if x32 is disabled, that andl $__SYSCALL_MASK,%rax should be nopped out.

Yes, that's what happens.

Quoted 1 line

Can this, and the comparison, use the standard alternatives mechanism?

There isn't really a standard alternatives mechanism! There is a CPU- features alternatives mechanism, an SMP alternatives mechanism and a PV alternatives mechanism, but they don't share anything above the text_poke*() functions.

Quoted 2 lines

And why are there extra syscall numbers in x32 land? There's an x32 bit *and* extra numbers. I'm confused.

I think the extra numbers are assigned for system calls where adjustment is always needed on entry and exit, while the x32 bit is used to trigger compat handling if it's needed at a deeper level. is_compat_task() tests the latter.

Quoted 5 lines

That magic number is bad. I would make it a real #define and put an appropriate assertion into whatever generates syscalls_64.h.

Also, if you do this, can you change all four instances of %eax in there to %rax? %eax is just wrong AFAICT.

I can only guess that %eax is currently used because it makes the code slightly smaller (no REX prefix needed).

Quoted 6 lines

Also, as a heads up to anyone who actually uses this: I was confused about the audit situation on x32-enabled kernels. I thought that this patch had been applied:

https://www.redhat.com/archives/linux-audit/2014-May/msg00099.html

Quoted 8 lines

but it wasn't. As a result, the commit message for this fix is wrong:

81f49a8fd708 x86, x32, audit: Fix x32's AUDIT_ARCH wrt audit

It *is* a user-visible fix and should possibly go to -stable, especially if anyone actually uses this *!$@ thing. Of course, audit is royally screwed up on x32-enabled kernels anyway. I admit that I find myself barely caring.

Thanks; this went into 3.16.7-ckt2 and should go into a Debian kernel update soon.

Ben.

Signature · 3 lines
Ben Hutchings
Experience is directly proportional to the value of equipment destroyed.
                                                         - Carolyn Scheppner