Re: [patch 03/18] entry: Provide [syscall_]enter_from_user_mode_randomize_stack()

Radu Rendec <[email protected]> Wed, 08 Jul 2026 13:26:19 -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:
> Randomizing the syscall stack can only happen after state is established
> via enter_from_user_mode() or syscall_enter_from_user_mode(). The earlier
> it happens the better.
>=20
> Provide two new macros to consolidate that:
>=20
> =C2=A0 - enter_from_user_mode_randomize_stack()
> 	enter_from_user_mode();
> 	add_random_kstack_offset_irqsoff();
>=20
> =C2=A0 - syscall_enter_from_user_mode_randomize_stack()
> 	enter_from_user_mode_randomize_stack();
> 	syscall_enter_from_user_mode_work();
> =C2=A0=C2=A0=C2=A0=20
> to reduce boiler plate code.
>=20
> Those are macros and not inline functions as the latter would limit the
> stack randomization scope to the inline function itself.
>=20
> Signed-off-by: Thomas Gleixner <[email protected]>
> ---
> =C2=A0include/linux/entry-common.h |=C2=A0=C2=A0 56 +++++++++++++++++++++=
++++++++++++++++++++++
> =C2=A01 file changed, 56 insertions(+)
>=20
> --- a/include/linux/entry-common.h
> +++ b/include/linux/entry-common.h
> @@ -6,6 +6,7 @@
> =C2=A0#include <linux/irq-entry-common.h>
> =C2=A0#include <linux/livepatch.h>
> =C2=A0#include <linux/ptrace.h>
> +#include <linux/randomize_kstack.h>
> =C2=A0#include <linux/resume_user_mode.h>
> =C2=A0#include <linux/seccomp.h>
> =C2=A0#include <linux/sched.h>
> @@ -150,6 +151,61 @@ static __always_inline long syscall_ente
> =C2=A0}
> =C2=A0
> =C2=A0/**
> + * enter_from_user_mode_randomize_stack - Establish state and add stack =
randomization
> + *					=C2=A0 before invoking syscall_enter_from_user_mode_work()
> + * @regs:	Pointer to currents pt_regs
                           ^^^^^^^^

Nit: current

> + *
> + * Invoked from architecture specific syscall entry code with interrupts
> + * disabled. The calling code has to be non-instrumentable. When the fun=
ction
> + * returns all state is correct, interrupts are still disabled and the
> + * subsequent functions can be instrumented.
> + *
> + * Implemented as a macro so that the stack randomization is effective
> + * throughout the function in which it is invoked. An inline would only =
make it
> + * effective in the scope of the inline function.
> + */
> +#define enter_from_user_mode_randomize_stack(regs)			\
> +do {									\
> +	enter_from_user_mode(regs);					\
> +	instrumentation_begin();					\
> +	add_random_kstack_offset_irqsoff();				\
> +	instrumentation_end();						\
> +} while (0)
> +
> +/**
> + * syscall_enter_from_user_mode_randomize_stack - Establish state and ch=
eck and handle work
> + *						=C2=A0 before invoking a syscall
> + * @regs:	Pointer to currents pt_regs
                           ^^^^^^^^

Same here.

> + * @syscall:	The syscall number
> + *
> + * Invoked from architecture specific syscall entry code with interrupts
> + * disabled. The calling code has to be non-instrumentable. When the
> + * function returns all state is correct, interrupts are enabled and the
> + * subsequent functions can be instrumented.
> + *
> + * This is the combination of enter_from_user_mode_randomize_stack() and
> + * syscall_enter_from_user_mode_work() to be used when there is no
> + * architecture specific work to be done between the two.
> + *
> + * Returns: The original or a modified syscall number. See
> + * syscall_enter_from_user_mode_work() for further explanation.
> + *
> + * Implemented as a macro to make stack randomization effective in the c=
alling
> + * scope.
> + */
> +#define syscall_enter_from_user_mode_randomize_stack(regs, syscall)	\
> +({									\
> +	enter_from_user_mode_randomize_stack(regs);			\
> +									\
> +	instrumentation_begin();					\
> +	local_irq_enable();						\
> +	long _ret =3D syscall_enter_from_user_mode_work(regs, syscall);	\
> +	instrumentation_end();						\
> +									\
> +	_ret;								\
> +})
> +
> +/**
> =C2=A0 * syscall_enter_from_user_mode - Establish state and check and han=
dle work
> =C2=A0 *				=C2=A0 before invoking a syscall
> =C2=A0 * @regs:	Pointer to currents pt_regs

With the above nits,

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