x86: Make x32 syscall support conditional on a kernel parameter
authorBen Hutchings <ben@decadent.org.uk>
Mon, 12 Feb 2018 23:59:26 +0000 (23:59 +0000)
committerBen Hutchings <benh@debian.org>
Tue, 9 Jun 2020 17:50:00 +0000 (18:50 +0100)
Bug-Debian: https://bugs.debian.org/708070
Forwarded: https://lore.kernel.org/lkml/1415245982.3398.53.camel@decadent.org.uk/T/#u

Enabling x32 in the standard amd64 kernel would increase its attack
surface while provide 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, adding another flavour just to turn on x32 seems wasteful.  And
the only differences on syscall entry are a few instructions that mask
out the x32 flag and compare the syscall number.

Use a static key to control whether x32 syscalls are really enabled, a
Kconfig parameter to set its default value and a kernel parameter
"syscall.x32" to change it at boot time.

Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
Gbp-Pq: Topic features/x86
Gbp-Pq: Name x86-make-x32-syscall-support-conditional.patch

Documentation/admin-guide/kernel-parameters.txt
arch/x86/Kconfig
arch/x86/entry/common.c
arch/x86/entry/syscall_64.c
arch/x86/include/asm/elf.h
arch/x86/include/asm/syscall.h

index a000098e8e0eb773dae6eb2dc9ec5fc75dc50491..942df1dc095aef82b15515d3ff03473c0c730610 100644 (file)
 
        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
index beea77046f9bab3eef998e8dd4c3248932dbc004..bed21b1853e11f0f00e1f599fdb7a397970fabd1 100644 (file)
@@ -2912,6 +2912,14 @@ config COMPAT_32
        select HAVE_UID16
        select OLD_SIGSUSPEND3
 
+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
index 9747876980b5d9e3eac01dea4738b304b1cabbf9..99d1983066590179681c1867b5669a48bb877742 100644 (file)
@@ -293,7 +293,8 @@ __visible void do_syscall_64(unsigned long nr, struct pt_regs *regs)
                nr = array_index_nospec(nr, NR_syscalls);
                regs->ax = sys_call_table[nr](regs);
 #ifdef CONFIG_X86_X32_ABI
-       } else if (likely((nr & __X32_SYSCALL_BIT) &&
+       } else if (x32_enabled &&
+                  likely((nr & __X32_SYSCALL_BIT) &&
                          (nr & ~__X32_SYSCALL_BIT) < X32_NR_syscalls)) {
                nr = array_index_nospec(nr & ~__X32_SYSCALL_BIT,
                                        X32_NR_syscalls);
index adf619a856e8d2dde3db5193adb5c7513c49214a..cf450e0a9d5a37f2989a64520faf5eaf7577688a 100644 (file)
@@ -4,6 +4,9 @@
 #include <linux/linkage.h>
 #include <linux/sys.h>
 #include <linux/cache.h>
+#include <linux/moduleparam.h>
+#undef MODULE_PARAM_PREFIX
+#define MODULE_PARAM_PREFIX "syscall."
 #include <linux/syscalls.h>
 #include <asm/asm-offsets.h>
 #include <asm/syscall.h>
@@ -53,4 +56,47 @@ asmlinkage const sys_call_ptr_t x32_sys_call_table[__NR_syscall_x32_max+1] = {
 #undef __SYSCALL_64
 #undef __SYSCALL_X32
 
+/* Maybe enable x32 syscalls */
+
+#if defined(CONFIG_X86_X32_DISABLED)
+DEFINE_STATIC_KEY_FALSE(x32_enabled_skey);
+#else
+DEFINE_STATIC_KEY_TRUE(x32_enabled_skey);
+#endif
+
+static int __init x32_param_set(const char *val, const struct kernel_param *p)
+{
+       bool enabled;
+       int ret;
+
+       ret = kstrtobool(val, &enabled);
+       if (ret)
+               return ret;
+       if (IS_ENABLED(CONFIG_X86_X32_DISABLED)) {
+               if (enabled) {
+                       static_key_enable(&x32_enabled_skey.key);
+                       pr_info("Enabled x32 syscalls\n");
+               }
+       } else {
+               if (!enabled) {
+                       static_key_disable(&x32_enabled_skey.key);
+                       pr_info("Disabled x32 syscalls\n");
+               }
+       }
+       return 0;
+}
+
+static int x32_param_get(char *buffer, const struct kernel_param *p)
+{
+       return sprintf(buffer, "%c\n",
+                      static_key_enabled(&x32_enabled_skey) ? 'Y' : 'N');
+}
+
+static const struct kernel_param_ops x32_param_ops = {
+       .set = x32_param_set,
+       .get = x32_param_get,
+};
+
+arch_param_cb(x32, &x32_param_ops, NULL, 0444);
+
 #endif
index 69c0f892e310a37a9f8bd543227a73104616b5df..37cf2da3dd1c42e11f220779f440759020c554df 100644 (file)
@@ -11,6 +11,9 @@
 #include <asm/user.h>
 #include <asm/auxvec.h>
 #include <asm/fsgsbase.h>
+#ifndef COMPILE_OFFSETS /* avoid a circular dependency on asm-offsets.h */
+#include <asm/syscall.h>
+#endif
 
 typedef unsigned long elf_greg_t;
 
@@ -163,7 +166,8 @@ do {                                                \
 
 #define compat_elf_check_arch(x)                                       \
        (elf_check_arch_ia32(x) ||                                      \
-        (IS_ENABLED(CONFIG_X86_X32_ABI) && (x)->e_machine == EM_X86_64))
+        (IS_ENABLED(CONFIG_X86_X32_ABI) && x32_enabled &&              \
+         (x)->e_machine == EM_X86_64))
 
 #if __USER32_DS != __USER_DS
 # error "The following code assumes __USER32_DS == __USER_DS"
index 8db3fdb6102ecb373f085de3a6033c645fbe644c..704f7fc1dd086858c3be46813cca57c2ac107d48 100644 (file)
@@ -13,6 +13,7 @@
 #include <uapi/linux/audit.h>
 #include <linux/sched.h>
 #include <linux/err.h>
+#include <linux/jump_label.h>
 #include <asm/asm-offsets.h>   /* For NR_syscalls */
 #include <asm/thread_info.h>   /* for TS_COMPAT */
 #include <asm/unistd.h>
@@ -40,6 +41,18 @@ extern const sys_call_ptr_t ia32_sys_call_table[];
 extern const sys_call_ptr_t x32_sys_call_table[];
 #endif
 
+#if defined(CONFIG_X86_X32_ABI)
+#if defined(CONFIG_X86_X32_DISABLED)
+DECLARE_STATIC_KEY_FALSE(x32_enabled_skey);
+#define x32_enabled static_branch_unlikely(&x32_enabled_skey)
+#else
+DECLARE_STATIC_KEY_TRUE(x32_enabled_skey);
+#define x32_enabled static_branch_likely(&x32_enabled_skey)
+#endif
+#else
+#define x32_enabled 0
+#endif
+
 /*
  * Only the low 32 bits of orig_ax are meaningful, so we return int.
  * This importantly ignores the high bits on 64-bit, so comparisons