[PATCH 3/3] x86: Disable running 32bit processes if ia32_disabled is passed

From: Nikolay Borisov
Date: Wed Jun 07 2023 - 03:35:57 EST


In addition to disabling 32bit syscall interface let's also disable the
ability to run 32bit processes altogether. This is achieved by setting
the GDT_ENTRY_DEFAULT_USER32_CS descriptor to not present which would
cause 32 bit processes to trap with a #NP exception. Furthermore,
forbid loading compat processes as well.

Signed-off-by: Nikolay Borisov <nik.borisov@xxxxxxxx>
---
arch/x86/include/asm/elf.h | 5 +++--
arch/x86/kernel/cpu/common.c | 8 ++++++++
2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h
index 18fd06f7936a..406245bc0fb0 100644
--- a/arch/x86/include/asm/elf.h
+++ b/arch/x86/include/asm/elf.h
@@ -148,9 +148,10 @@ do { \
#define elf_check_arch(x) \
((x)->e_machine == EM_X86_64)

+extern bool ia32_disabled;
#define compat_elf_check_arch(x) \
- (elf_check_arch_ia32(x) || \
- (IS_ENABLED(CONFIG_X86_X32_ABI) && (x)->e_machine == EM_X86_64))
+ (!ia32_disabled && (elf_check_arch_ia32(x) || \
+ (IS_ENABLED(CONFIG_X86_X32_ABI) && (x)->e_machine == EM_X86_64)))

static inline void elf_common_init(struct thread_struct *t,
struct pt_regs *regs, const u16 ds)
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 71f8b55f70c9..ddc301c09419 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -2359,6 +2359,11 @@ void microcode_check(struct cpuinfo_x86 *prev_info)
}
#endif

+static void remove_user32cs_from_gdt(void * __unused)
+{
+ get_current_gdt_rw()[GDT_ENTRY_DEFAULT_USER32_CS].p = 0;
+}
+
/*
* Invoked from core CPU hotplug code after hotplug operations
*/
@@ -2368,4 +2373,7 @@ void arch_smt_update(void)
cpu_bugs_smt_update();
/* Check whether IPI broadcasting can be enabled */
apic_smt_update();
+ if (ia32_disabled)
+ on_each_cpu(remove_user32cs_from_gdt, NULL, 1);
+
}
--
2.34.1