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

From: Brian Gerst
Date: Thu Jun 08 2023 - 00:37:58 EST


On Wed, Jun 7, 2023 at 3:41 AM Nikolay Borisov <nik.borisov@xxxxxxxx> wrote:
>
> 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);
> +
> }

Disabling USER32_CS isn't really necessary, as simply running 32-bit
user code (in a normally 64-bit process) doesn't pose much of a threat
to the kernel with 32-bit syscalls disabled.

Wine, for example, is moving to a model where the main code runs in
64-bit mode, uses only 64-bit unix libraries, and the 32->64 bit
transitions are done entirely in userspace. It will still need the
ability to use 32-bit code segments, but won't need 32-bit unix
libraries or syscalls to run 32-bit Windows code.

Brian Gerst