Re: [patch 08/18] x86/syscall: Use [syscall_]enter_from_user_mode_randomize_stack()

Radu Rendec <[email protected]> Wed, 08 Jul 2026 16:59:18 -0400
Newsgroups org.kernel.vger.linux-hexagon,dev.linux.lists.loongarch,org.infradead.lists.linux-riscv,org.infradead.lists.linux-snps-arc,org.infradead.lists.linux-um,org.kernel.vger.linux-alpha,org.kernel.vger.linux-arch,org.kernel.vger.linux-csky,org.kernel.vger.linux-doc,org.kernel.vger.linux-kernel,org.kernel.vger.linux-m68k,org.kernel.vger.linux-mips,org.kernel.vger.linux-openrisc,org.kernel.vger.linux-parisc,org.kernel.vger.linux-s390,org.kernel.vger.linux-sh,org.kernel.vger.sparclinux,org.ozlabs.lists.linuxppc-dev
Message-ID <[email protected]>
On Tue, 2026-07-07 at 21:06 +0200, Thomas Gleixner wrote:
> These functions integrate the stack randomization.
>=20
> syscall_enter_from_user_mode_randomize_stack() has the advantage that the
> randomization happens early right after enter_from_user_mode().
>=20
> In both cases also the overhead of get/put_cpu_var() in
> add_random_kstack_offset() is avoided.
>=20
> No functional change.
>=20
> Signed-off-by: Thomas Gleixner <[email protected]>
> Cc: [email protected]
> ---
> =C2=A0arch/x86/entry/syscall_32.c=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0 |=C2=A0=C2=A0 19 +++++--------------
> =C2=A0arch/x86/entry/syscall_64.c=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0 |=C2=A0=C2=A0=C2=A0 3 +--
> =C2=A0arch/x86/include/asm/entry-common.h |=C2=A0=C2=A0=C2=A0 1 -
> =C2=A03 files changed, 6 insertions(+), 17 deletions(-)
>=20
> --- a/arch/x86/entry/syscall_32.c
> +++ b/arch/x86/entry/syscall_32.c
> @@ -142,10 +142,9 @@ static __always_inline bool int80_is_ext
> =C2=A0	 * int80_is_external() below which calls into the APIC driver.
> =C2=A0	 * Identical for soft and external interrupts.
> =C2=A0	 */
> -	enter_from_user_mode(regs);
> +	enter_from_user_mode_randomize_stack(regs);
> =C2=A0
> =C2=A0	instrumentation_begin();
> -	add_random_kstack_offset();
> =C2=A0
> =C2=A0	/* Validate that this is a soft interrupt to the extent possible *=
/
> =C2=A0	if (unlikely(int80_is_external()))
> @@ -210,11 +209,9 @@ DEFINE_FREDENTRY_RAW(int80_emulation)
> =C2=A0{
> =C2=A0	int nr;
> =C2=A0
> -	enter_from_user_mode(regs);
> +	enter_from_user_mode_randomize_stack(regs);
> =C2=A0
> =C2=A0	instrumentation_begin();
> -	add_random_kstack_offset();
> -
> =C2=A0	/*
> =C2=A0	 * FRED pushed 0 into regs::orig_ax and regs::ax contains the
> =C2=A0	 * syscall number.
> @@ -252,10 +249,10 @@ DEFINE_FREDENTRY_RAW(int80_emulation)
> =C2=A0	 * orig_ax, the int return value truncates it. This matches
> =C2=A0	 * the semantics of syscall_get_nr().
> =C2=A0	 */
> -	nr =3D syscall_enter_from_user_mode(regs, nr);
> +	nr =3D syscall_enter_from_user_mode_randomize_stack(regs, nr);
> +
> =C2=A0	instrumentation_begin();
> =C2=A0
> -	add_random_kstack_offset();
> =C2=A0	do_syscall_32_irqs_on(regs, nr);
> =C2=A0
> =C2=A0	instrumentation_end();
> @@ -268,15 +265,9 @@ static noinstr bool __do_fast_syscall_32
> =C2=A0	int nr =3D syscall_32_enter(regs);
> =C2=A0	int res;
> =C2=A0
> -	/*
> -	 * This cannot use syscall_enter_from_user_mode() as it has to
> -	 * fetch EBP before invoking any of the syscall entry work
> -	 * functions.
> -	 */
> -	enter_from_user_mode(regs);
> +	enter_from_user_mode_randomize_stack(regs);
> =C2=A0
> =C2=A0	instrumentation_begin();
> -	add_random_kstack_offset();
> =C2=A0	local_irq_enable();
> =C2=A0	/* Fetch EBP from where the vDSO stashed it. */
> =C2=A0	if (IS_ENABLED(CONFIG_X86_64)) {
> --- a/arch/x86/entry/syscall_64.c
> +++ b/arch/x86/entry/syscall_64.c
> @@ -86,10 +86,9 @@ static __always_inline bool do_syscall_x
> =C2=A0/* Returns true to return using SYSRET, or false to use IRET */
> =C2=A0__visible noinstr bool do_syscall_64(struct pt_regs *regs, int nr)
> =C2=A0{
> -	nr =3D syscall_enter_from_user_mode(regs, nr);
> +	nr =3D syscall_enter_from_user_mode_randomize_stack(regs, nr);
> =C2=A0
> =C2=A0	instrumentation_begin();
> -	add_random_kstack_offset();
> =C2=A0
> =C2=A0	if (!do_syscall_x64(regs, nr) && !do_syscall_x32(regs, nr) && nr !=
=3D -1) {
> =C2=A0		/* Invalid system call, but still a system call. */
> --- a/arch/x86/include/asm/entry-common.h
> +++ b/arch/x86/include/asm/entry-common.h
> @@ -2,7 +2,6 @@
> =C2=A0#ifndef _ASM_X86_ENTRY_COMMON_H
> =C2=A0#define _ASM_X86_ENTRY_COMMON_H
> =C2=A0
> -#include <linux/randomize_kstack.h>
> =C2=A0#include <linux/user-return-notifier.h>
> =C2=A0
> =C2=A0#include <asm/nospec-branch.h>

Reviewed-by: Radu Rendec <[email protected]>