From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from canpmsgout03.his.huawei.com (canpmsgout03.his.huawei.com [113.46.200.218]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D47942F49FD; Thu, 9 Jul 2026 03:46:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.218 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783568778; cv=none; b=f5ApYc0DcuyPWkNC8JgG+7g/zIJtTAbs8lUs11iRsDaqEH0/eNpd3UCB18GPr6kpXbAOk/SDvyk0zqd+G+10bI7WUjZ0eOORDcnS8fsz88DX0SIyuqBNXLz3be6TE7/K2QJinU6uSenUT9/vrVWibvXFejaKlyD6ZYmFYhFO3vs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783568778; c=relaxed/simple; bh=y3xx833U4ww9Cq3u9w34mmvk34+zHYMoUoG7ghGtgRQ=; h=Message-ID:Date:MIME-Version:Subject:To:CC:References:From: In-Reply-To:Content-Type; b=SZQBEP+zraJVqO8k+hxSTApT3nAdnLnmCU70JMdrWaGZ5fLXFvMNyL/Jlij/bqdUPn0D3JXJ10PXucXtp/IHzprdaLKen9JytYt2IZfu8dJG6r9CYimjK7xo+o/kmqcAwELCR6uXloNXFP3sX9kIwf3ZNGoOekQyqzdCTEq33f8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=r6RtRSgk; arc=none smtp.client-ip=113.46.200.218 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="r6RtRSgk" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=S12kfcpDi3n+0LKTkd0ZeOMuD4be+hwO7l/dC6hKERM=; b=r6RtRSgklJXTYPLKflO27pyxFSmmCAshlJ9sm0FshpvKL/UlLPYd2FfTNtINKHoCzuTfShLyS LkOmzr7xxMEHmA7sCPUPICbnen8w5KxUkRZCWYkkaVaxSOG+7qe1F0UiIFaRZ9FEqLiKzBUVzBQ oR/081TML0omCrg301p3De4= Received: from mail.maildlp.com (unknown [172.19.163.0]) by canpmsgout03.his.huawei.com (SkyGuard) with ESMTPS id 4gwgdP686WzpSvs; Thu, 9 Jul 2026 11:37:29 +0800 (CST) Received: from dggpemf500011.china.huawei.com (unknown [7.185.36.131]) by mail.maildlp.com (Postfix) with ESMTPS id 6702240561; Thu, 9 Jul 2026 11:46:11 +0800 (CST) Received: from [10.67.109.254] (10.67.109.254) by dggpemf500011.china.huawei.com (7.185.36.131) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Thu, 9 Jul 2026 11:46:08 +0800 Message-ID: <5fb7a6bc-2a4b-4e91-b12d-2a1b0d3de88a@huawei.com> Date: Thu, 9 Jul 2026 11:46:07 +0800 Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [patch 03/18] entry: Provide [syscall_]enter_from_user_mode_randomize_stack() To: Thomas Gleixner , LKML CC: Peter Zijlstra , Michael Ellerman , Shrikanth Hegde , , Kees Cook , Huacai Chen , , Paul Walmsley , Palmer Dabbelt , , Sven Schnelle , , , Mark Rutland , Andy Lutomirski , Oleg Nesterov , Richard Henderson , Russell King , Catalin Marinas , Guo Ren , Geert Uytterhoeven , Thomas Bogendoerfer , Helge Deller , Yoshinori Sato , Richard Weinberger , Chris Zankel , , , , , , , , , Arnd Bergmann , Vineet Gupta , Will Deacon , Brian Cain , Michal Simek , Dinh Nguyen , "David S. Miller" , Andreas Larsson , , , , , , =?UTF-8?Q?Michal_Such=C3=A1nek?= , Jonathan Corbet , References: <20260707181957.433213175@kernel.org> <20260707190253.816918647@kernel.org> From: Jinjie Ruan In-Reply-To: <20260707190253.816918647@kernel.org> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-ClientProxiedBy: kwepems100002.china.huawei.com (7.221.188.206) To dggpemf500011.china.huawei.com (7.185.36.131) On 7/8/2026 3:06 AM, 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. > > 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 > --- > include/linux/entry-common.h | 56 +++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 56 insertions(+) > > --- a/include/linux/entry-common.h > +++ b/include/linux/entry-common.h > @@ -6,6 +6,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -150,6 +151,61 @@ static __always_inline long syscall_ente > } > > /** > + * 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) Perhaps this new function can also be reused when the ARM64 is switched to the generic entry as the irq also disabled now. --- a/arch/arm64/kernel/entry-common.c +++ b/arch/arm64/kernel/entry-common.c @@ -64,7 +64,7 @@ static void noinstr arm64_exit_to_kernel_mode(struct pt_regs *regs, static __always_inline void arm64_syscall_enter_from_user_mode(struct pt_regs *regs) { - enter_from_user_mode(regs); + enter_from_user_mode_randomize_stack(regs); mte_disable_tco_entry(current); sme_enter_from_user_mode(); } diff --git a/arch/arm64/kernel/syscall.c b/arch/arm64/kernel/syscall.c index e0a98fac3b85..42ac02573b66 100644 --- a/arch/arm64/kernel/syscall.c +++ b/arch/arm64/kernel/syscall.c @@ -6,7 +6,6 @@ #include #include #include -#include #include #include @@ -42,8 +41,6 @@ static void invoke_syscall(struct pt_regs *regs, unsigned int scno, { long ret; - add_random_kstack_offset(); - if (likely(scno < sc_nr)) { syscall_fn_t syscall_fn; syscall_fn = syscall_table[array_index_nospec(scno, sc_nr)]; > + > +/** > + * 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 >