[tip: core/entry] entry: Provide [syscall_]enter_from_user_mode_randomize_stack()

From: tip-bot2 for Thomas Gleixner

Date: Sun Jul 12 2026 - 08:45:22 EST


The following commit has been merged into the core/entry branch of tip:

Commit-ID: 855c103f86275a55eba115cabb86eb84f8236933
Gitweb: https://git.kernel.org/tip/855c103f86275a55eba115cabb86eb84f8236933
Author: Thomas Gleixner <tglx@xxxxxxxxxx>
AuthorDate: Tue, 07 Jul 2026 21:06:07 +02:00
Committer: Thomas Gleixner <tglx@xxxxxxxxxx>
CommitterDate: Sun, 12 Jul 2026 12:38:01 +02:00

entry: Provide [syscall_]enter_from_user_mode_randomize_stack()

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.

Provide two new macros to consolidate that:

- enter_from_user_mode_randomize_stack()
enter_from_user_mode();
add_random_kstack_offset_irqsoff();

- syscall_enter_from_user_mode_randomize_stack()
enter_from_user_mode_randomize_stack();
syscall_enter_from_user_mode_work();

to reduce boiler plate code.

Those are macros and not inline functions as the latter would limit the
stack randomization scope to the inline function itself.

Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxx>
Tested-by: Mukesh Kumar Chaurasiya (IBM) <mkchauras@xxxxxxxxx>
Reviewed-by: Radu Rendec <radu@xxxxxxxxxx>
Reviewed-by: Jinjie Ruan <ruanjinjie@xxxxxxxxxx>
Reviewed-by: Philippe Mathieu-Daudé <philmd@xxxxxxxxxxxxxxxx>
Reviewed-by: Mukesh Kumar Chaurasiya (IBM) <mkchauras@xxxxxxxxx>
Link: https://patch.msgid.link/20260707190253.816918647@xxxxxxxxxx
---
include/linux/entry-common.h | 56 +++++++++++++++++++++++++++++++++++-
1 file changed, 56 insertions(+)

diff --git a/include/linux/entry-common.h b/include/linux/entry-common.h
index 9336516..166b1ed 100644
--- a/include/linux/entry-common.h
+++ b/include/linux/entry-common.h
@@ -6,6 +6,7 @@
#include <linux/irq-entry-common.h>
#include <linux/livepatch.h>
#include <linux/ptrace.h>
+#include <linux/randomize_kstack.h>
#include <linux/resume_user_mode.h>
#include <linux/seccomp.h>
#include <linux/sched.h>
@@ -150,6 +151,61 @@ static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *re
}

/**
+ * enter_from_user_mode_randomize_stack - Establish state and add stack randomization
+ * before invoking syscall_enter_from_user_mode_work()
+ * @regs: Pointer to currents pt_regs
+ *
+ * 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 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 check and handle work
+ * before invoking a syscall
+ * @regs: Pointer to currents pt_regs
+ * @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 calling
+ * 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 = syscall_enter_from_user_mode_work(regs, syscall); \
+ instrumentation_end(); \
+ \
+ _ret; \
+})
+
+/**
* syscall_enter_from_user_mode - Establish state and check and handle work
* before invoking a syscall
* @regs: Pointer to currents pt_regs