From: Ryan Roberts <ryan.roberts@arm.com> Date: 2026-01-19 13:01:34
[Kees; I'm hoping this is now good-to-go via your hardening tree? Please shout
if you think there is more work to be done here!]
Hi All,
As I reported at [1], kstack offset randomisation suffers from a couple of bugs
and, on arm64 at least, the performance is poor. This series attempts to fix
both; patch 1 provides back-portable fixes for the functional bugs. Patches 2-3
propose a performance improvement approach.
I've looked at a few different options but ultimately decided that Jeremy's
original prng approach is the fastest. I made the argument that this approach is
secure "enough" in the RFC [2] and the responses indicated agreement.
More details in the commit logs.
Performance
===========
Mean and tail performance of 3 "small" syscalls was measured. syscall was made
10 million times and each individually measured and binned. These results have
low noise so I'm confident that they are trustworthy.
The baseline is v6.18-rc5 with stack randomization turned *off*. So I'm showing
performance cost of turning it on without any changes to the implementation,
then the reduced performance cost of turning it on with my changes applied.
**NOTE**: The below results were generated using the RFC patches but there is no
meaningful change, so the numbers are still valid.
arm64 (AWS Graviton3):
+-----------------+--------------+-------------+---------------+
| Benchmark | Result Class | v6.18-rc5 | per-task-prng |
| | | rndstack-on | |
| | | | |
+=================+==============+=============+===============+
| syscall/getpid | mean (ns) | (R) 15.62% | (R) 3.43% |
| | p99 (ns) | (R) 155.01% | (R) 3.20% |
| | p99.9 (ns) | (R) 156.71% | (R) 2.93% |
+-----------------+--------------+-------------+---------------+
| syscall/getppid | mean (ns) | (R) 14.09% | (R) 2.12% |
| | p99 (ns) | (R) 152.81% | 1.55% |
| | p99.9 (ns) | (R) 153.67% | 1.77% |
+-----------------+--------------+-------------+---------------+
| syscall/invalid | mean (ns) | (R) 13.89% | (R) 3.32% |
| | p99 (ns) | (R) 165.82% | (R) 3.51% |
| | p99.9 (ns) | (R) 168.83% | (R) 3.77% |
+-----------------+--------------+-------------+---------------+
Because arm64 was previously using get_random_u16(), it was expensive when it
didn't have any buffered bits and had to call into the crng. That's what caused
the enormous tail latency.
x86 (AWS Sapphire Rapids):
+-----------------+--------------+-------------+---------------+
| Benchmark | Result Class | v6.18-rc5 | per-task-prng |
| | | rndstack-on | |
| | | | |
+=================+==============+=============+===============+
| syscall/getpid | mean (ns) | (R) 13.32% | (R) 4.60% |
| | p99 (ns) | (R) 13.38% | (R) 18.08% |
| | p99.9 (ns) | 16.26% | (R) 19.38% |
+-----------------+--------------+-------------+---------------+
| syscall/getppid | mean (ns) | (R) 11.96% | (R) 5.26% |
| | p99 (ns) | (R) 11.83% | (R) 8.35% |
| | p99.9 (ns) | (R) 11.42% | (R) 22.37% |
+-----------------+--------------+-------------+---------------+
| syscall/invalid | mean (ns) | (R) 10.58% | (R) 2.91% |
| | p99 (ns) | (R) 10.51% | (R) 4.36% |
| | p99.9 (ns) | (R) 10.35% | (R) 21.97% |
+-----------------+--------------+-------------+---------------+
I was surprised to see that the baseline cost on x86 is 10-12% since it is just
using rdtsc. But as I say, I believe the results are accurate.
Changes since v3 (RFC) [4]
==========================
- Patch 1: Fixed typo in commit log (per David L)
- Patch 2: Reinstated prandom_u32_state() as out-of-line function, which
forwards to inline version (per David L)
- Patch 3: Added supplementary info about benefits of removing
choose_random_kstack_offset() (per Mark R)
Changes since v2 (RFC) [3]
==========================
- Moved late_initcall() to initialize kstack_rnd_state out of
randomize_kstack.h and into main.c. (issue noticed by kernel test robot)
Changes since v1 (RFC) [2]
==========================
- Introduced patch 2 to make prandom_u32_state() __always_inline (needed since
its called from noinstr code)
- In patch 3, prng is now per-cpu instead of per-task (per Ard)
[1] https://lore.kernel.org/all/dd8c37bc-795f-4c7a-9086-69e584d8ab24@arm.com/
[2] https://lore.kernel.org/all/20251127105958.2427758-1-ryan.roberts@arm.com/
[3] https://lore.kernel.org/all/20251215163520.1144179-1-ryan.roberts@arm.com/
[4] https://lore.kernel.org/all/20260102131156.3265118-1-ryan.roberts@arm.com/
Thanks,
Ryan
Ryan Roberts (3):
randomize_kstack: Maintain kstack_offset per task
prandom: Add __always_inline version of prandom_u32_state()
randomize_kstack: Unify random source across arches
arch/Kconfig | 5 ++-
arch/arm64/kernel/syscall.c | 11 ------
arch/loongarch/kernel/syscall.c | 11 ------
arch/powerpc/kernel/syscall.c | 12 -------
arch/riscv/kernel/traps.c | 12 -------
arch/s390/include/asm/entry-common.h | 8 -----
arch/x86/include/asm/entry-common.h | 12 -------
include/linux/prandom.h | 20 +++++++++++
include/linux/randomize_kstack.h | 54 +++++++++++-----------------
init/main.c | 9 ++++-
kernel/fork.c | 1 +
lib/random32.c | 8 +----
12 files changed, 52 insertions(+), 111 deletions(-)
--
2.43.0
From: Ryan Roberts <ryan.roberts@arm.com> Date: 2026-01-19 13:01:38
kstack_offset was previously maintained per-cpu, but this caused a
couple of issues. So let's instead make it per-task.
Issue 1: add_random_kstack_offset() and choose_random_kstack_offset()
expected and required to be called with interrupts and preemption
disabled so that it could manipulate per-cpu state. But arm64, loongarch
and risc-v are calling them with interrupts and preemption enabled. I
don't _think_ this causes any functional issues, but it's certainly
unexpected and could lead to manipulating the wrong cpu's state, which
could cause a minor performance degradation due to bouncing the cache
lines. By maintaining the state per-task those functions can safely be
called in preemptible context.
Issue 2: add_random_kstack_offset() is called before executing the
syscall and expands the stack using a previously chosen random offset.
choose_random_kstack_offset() is called after executing the syscall and
chooses and stores a new random offset for the next syscall. With
per-cpu storage for this offset, an attacker could force cpu migration
during the execution of the syscall and prevent the offset from being
updated for the original cpu such that it is predictable for the next
syscall on that cpu. By maintaining the state per-task, this problem
goes away because the per-task random offset is updated after the
syscall regardless of which cpu it is executing on.
Fixes: 39218ff4c625 ("stack: Optionally randomize kernel stack offset each syscall")
Closes: https://lore.kernel.org/all/dd8c37bc-795f-4c7a-9086-69e584d8ab24@arm.com/
Cc: stable@vger.kernel.org
Acked-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
---
include/linux/randomize_kstack.h | 26 +++++++++++++++-----------
include/linux/sched.h | 4 ++++
init/main.c | 1 -
kernel/fork.c | 2 ++
4 files changed, 21 insertions(+), 12 deletions(-)
@@ -50,15 +49,14 @@ DECLARE_PER_CPU(u32, kstack_offset);*add_random_kstack_offset-Increasestackutilizationbypreviously*chosenrandomoffset*-*Thisshouldbeusedinthesyscallentrypathwheninterruptsand-*preemptaredisabled,andafteruserregistershavebeenstoredto-*thestack.Fortestingtheresultingentropy,pleasesee:-*tools/testing/selftests/lkdtm/stack-entropy.sh+*Thisshouldbeusedinthesyscallentrypathafteruserregistershavebeen+*storedtothestack.Preemptionmaybeenabled.Fortestingtheresulting+*entropy,pleasesee:tools/testing/selftests/lkdtm/stack-entropy.sh*/#define add_random_kstack_offset() do { \if(static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,\&randomize_kstack_offset)){\-u32offset=raw_cpu_read(kstack_offset);\+u32offset=current->kstack_offset;\u8*ptr=__kstack_alloca(KSTACK_OFFSET_MAX(offset));\/* Keep allocation even after "ptr" loses scope. */\asmvolatile(""::"r"(ptr):"memory");\
@@ -69,9 +67,9 @@ DECLARE_PER_CPU(u32, kstack_offset);*choose_random_kstack_offset-Choosetherandomoffsetforthenext*add_random_kstack_offset()*-*Thisshouldonlybeusedduringsyscallexitwheninterruptsand-*preemptaredisabled.Thispositioninthesyscallflowisdoneto-*frustrateattacksfromuserspaceattemptingtolearnthenextoffset:+*Thisshouldonlybeusedduringsyscallexit.Preemptionmaybeenabled.This+*positioninthesyscallflowisdonetofrustrateattacksfromuserspace+*attemptingtolearnthenextoffset:*-Maximizethetiminguncertaintyvisiblefromuserspace:ifthe*offsetischosenatsyscallentry,userspacehasmuchmorecontrol*overthetimingbetweenchoosingoffsets."How long will we be in
@@ -85,14 +83,20 @@ DECLARE_PER_CPU(u32, kstack_offset);#define choose_random_kstack_offset(rand) do { \if(static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,\&randomize_kstack_offset)){\-u32offset=raw_cpu_read(kstack_offset);\+u32offset=current->kstack_offset;\offset=ror32(offset,5)^(rand);\-raw_cpu_write(kstack_offset,offset);\+current->kstack_offset=offset;\}\}while(0)++staticinlinevoidrandom_kstack_task_init(structtask_struct*tsk)+{+tsk->kstack_offset=0;+}#else /* CONFIG_RANDOMIZE_KSTACK_OFFSET */#define add_random_kstack_offset() do { } while (0)#define choose_random_kstack_offset(rand) do { } while (0)+#define random_kstack_task_init(tsk) do { } while (0)#endif /* CONFIG_RANDOMIZE_KSTACK_OFFSET */#endif
Nit: This seems to be throwing a u32 potentially in between a couple of
void*/ulong sized objects.
It probably doesn't matter with struct randomization and it's really
hard to get right among the web of task_struct #ifdefs. But, it would be
nice to at _least_ nestle this next to another int-sized thing.
Does it really even need to be 32 bits? x86 has this comment:
/*
* This value will get limited by KSTACK_OFFSET_MAX(), which is 10
* bits. The actual entropy will be further reduced by the compiler
* when applying stack alignment constraints (see cc_stack_align4/8 in
* arch/x86/Makefile), which will remove the 3 (x86_64) or 2 (ia32)
* low bits from any entropy chosen here.
*
* Therefore, final stack offset entropy will be 7 (x86_64) or
* 8 (ia32) bits.
*/
From: Ryan Roberts <ryan.roberts@arm.com> Date: 2026-01-19 16:51:39
Thanks for the review!
On 19/01/2026 16:10, Dave Hansen wrote:
On 1/19/26 05:01, Ryan Roberts wrote:
...
quoted
Cc: stable@vger.kernel.org
Since this doesn't fix any known functional issues, if it were me, I'd
leave stable@ alone. It isn't clear that this is stable material.
I listed 2 issues in the commit log; I agree that issue 1 falls into the
category of "don't really care", but issue 2 means that kstack randomization is
currently trivial to defeat. That's the reason I thought it would valuable in
stable.
But if you're saying don't bother and others agree, then this whole patch can be
dropped; this is just intended to be the backportable fix. Patch 3 reimplements
this entirely for upstream.
I'll wait and see if others have opinions if that's ok?
Nit: This seems to be throwing a u32 potentially in between a couple of
void*/ulong sized objects.
Yeah, I spent a bit of time with pahole but eventually concluded that it was
difficult to find somewhere to nestle it that would work reliably cross arch.
Eventually I just decided to group it with other stack meta data.
It probably doesn't matter with struct randomization and it's really
hard to get right among the web of task_struct #ifdefs. But, it would be
nice to at _least_ nestle this next to another int-sized thing.
Does it really even need to be 32 bits? x86 has this comment:
quoted
/*
* This value will get limited by KSTACK_OFFSET_MAX(), which is 10
* bits. The actual entropy will be further reduced by the compiler
* when applying stack alignment constraints (see cc_stack_align4/8 in
* arch/x86/Makefile), which will remove the 3 (x86_64) or 2 (ia32)
* low bits from any entropy chosen here.
*
* Therefore, final stack offset entropy will be 7 (x86_64) or
* 8 (ia32) bits.
*/
For more recent kernels it's 6 bits shifted by 4 for 64-bit kernels or 8 bits
shifted by 2 for 32-bit kernels regardless of arch. So could probably make it
work with 8 bits of storage. Although I was deliberately trying to keep the
change simple, since it was intended for backporting. Patch 3 rips it out.
Overall I'd prefer to leave it all as is. But if people don't think we should
backport, then let's just drop the whole patch.
Thanks,
Ryan
From: Dave Hansen <hidden> Date: 2026-01-19 16:53:36
On 1/19/26 08:51, Ryan Roberts wrote:
quoted
Since this doesn't fix any known functional issues, if it were me, I'd
leave stable@ alone. It isn't clear that this is stable material.
I listed 2 issues in the commit log; I agree that issue 1 falls into the
category of "don't really care", but issue 2 means that kstack randomization is
currently trivial to defeat. That's the reason I thought it would valuable in
stable.
But if you're saying don't bother and others agree, then this whole patch can be
dropped; this is just intended to be the backportable fix. Patch 3 reimplements
this entirely for upstream.
I'll wait and see if others have opinions if that's ok?
From: Ryan Roberts <ryan.roberts@arm.com> Date: 2026-01-19 13:01:42
We will shortly use prandom_u32_state() to implement kstack offset
randomization and some arches need to call it from non-instrumentable
context. So let's implement prandom_u32_state() as an out-of-line
wrapper around a new __always_inline prandom_u32_state_inline(). kstack
offset randomization will use this new version.
Acked-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
---
include/linux/prandom.h | 20 ++++++++++++++++++++
lib/random32.c | 8 +-------
2 files changed, 21 insertions(+), 7 deletions(-)
From: "Jason A. Donenfeld" <Jason@zx2c4.com> Date: 2026-01-28 17:00:53
On Mon, Jan 19, 2026 at 01:01:09PM +0000, Ryan Roberts wrote:
quoted hunk
We will shortly use prandom_u32_state() to implement kstack offset
randomization and some arches need to call it from non-instrumentable
context. So let's implement prandom_u32_state() as an out-of-line
wrapper around a new __always_inline prandom_u32_state_inline(). kstack
offset randomization will use this new version.
Acked-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
---
include/linux/prandom.h | 20 ++++++++++++++++++++
lib/random32.c | 8 +-------
2 files changed, 21 insertions(+), 7 deletions(-)
This is pretty bikesheddy and I'm not really entirely convinced that my
intuition is correct here, but I thought I should at least ask. Do you
think this would be better called __prandom_u32_state(), where the "__"
is kind of a, "don't use this directly unless you know what you're doing
because it's sort of internal"? It seems like either we make this inline
for everybody, or if there's a good reason for having most users use the
non-inline version, then we should be careful that new users don't use
the inline version. I was thinking the __ would help with that.
Jason
From: Ryan Roberts <ryan.roberts@arm.com> Date: 2026-01-28 17:33:24
On 28/01/2026 17:00, Jason A. Donenfeld wrote:
On Mon, Jan 19, 2026 at 01:01:09PM +0000, Ryan Roberts wrote:
quoted
We will shortly use prandom_u32_state() to implement kstack offset
randomization and some arches need to call it from non-instrumentable
context. So let's implement prandom_u32_state() as an out-of-line
wrapper around a new __always_inline prandom_u32_state_inline(). kstack
offset randomization will use this new version.
Acked-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
---
include/linux/prandom.h | 20 ++++++++++++++++++++
lib/random32.c | 8 +-------
2 files changed, 21 insertions(+), 7 deletions(-)
This is pretty bikesheddy and I'm not really entirely convinced that my
intuition is correct here, but I thought I should at least ask. Do you
think this would be better called __prandom_u32_state(), where the "__"
is kind of a, "don't use this directly unless you know what you're doing
because it's sort of internal"? It seems like either we make this inline
for everybody, or if there's a good reason for having most users use the
non-inline version, then we should be careful that new users don't use
the inline version. I was thinking the __ would help with that.
I'm certainly happy to do that, if that's your preference. I have to respin this
anyway, given the noinstr issue.
From: David Laight <hidden> Date: 2026-01-28 18:32:55
On Wed, 28 Jan 2026 17:33:19 +0000
Ryan Roberts [off-list ref] wrote:
On 28/01/2026 17:00, Jason A. Donenfeld wrote:
quoted
On Mon, Jan 19, 2026 at 01:01:09PM +0000, Ryan Roberts wrote:
quoted
We will shortly use prandom_u32_state() to implement kstack offset
randomization and some arches need to call it from non-instrumentable
context. So let's implement prandom_u32_state() as an out-of-line
wrapper around a new __always_inline prandom_u32_state_inline(). kstack
offset randomization will use this new version.
Acked-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
---
include/linux/prandom.h | 20 ++++++++++++++++++++
lib/random32.c | 8 +-------
2 files changed, 21 insertions(+), 7 deletions(-)
This is pretty bikesheddy and I'm not really entirely convinced that my
intuition is correct here, but I thought I should at least ask. Do you
think this would be better called __prandom_u32_state(), where the "__"
is kind of a, "don't use this directly unless you know what you're doing
because it's sort of internal"? It seems like either we make this inline
for everybody, or if there's a good reason for having most users use the
non-inline version, then we should be careful that new users don't use
the inline version. I was thinking the __ would help with that.
I'm certainly happy to do that, if that's your preference. I have to respin this
anyway, given the noinstr issue.
Le 28/01/2026 à 18:00, Jason A. Donenfeld a écrit :
On Mon, Jan 19, 2026 at 01:01:09PM +0000, Ryan Roberts wrote:
quoted
We will shortly use prandom_u32_state() to implement kstack offset
randomization and some arches need to call it from non-instrumentable
context. So let's implement prandom_u32_state() as an out-of-line
wrapper around a new __always_inline prandom_u32_state_inline(). kstack
offset randomization will use this new version.
Acked-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
---
include/linux/prandom.h | 20 ++++++++++++++++++++
lib/random32.c | 8 +-------
2 files changed, 21 insertions(+), 7 deletions(-)
This is pretty bikesheddy and I'm not really entirely convinced that my
intuition is correct here, but I thought I should at least ask. Do you
think this would be better called __prandom_u32_state(), where the "__"
is kind of a, "don't use this directly unless you know what you're doing
because it's sort of internal"? It seems like either we make this inline
for everybody, or if there's a good reason for having most users use the
non-inline version, then we should be careful that new users don't use
the inline version. I was thinking the __ would help with that.
I looked into kernel sources and there are several functions named
something_something_else_inline() and it doesn't mean those functions
get inlined, so I would also prefer __prandom_u32_state() which means
"If you use it you know what you are doing", just like __get_user() for
instance.
However maybe we could also reconsider making it inline for everyone. We
have spotted half a dozen of places where the code size increases a lot
when forcing it inline, but those places deserve a local trampoline to
avoid code duplication, and then the compiler decides to inline or not.
Because there are also several places that benefit from the inlining
because it allows GCC to simplify the calculation, for instance when
some calculation is performed with the result like with
(prandom_u32_state(rng) % ceil) where ceil is 2 or 4.
That can of course be done as a followup patch but it means at the end
we will have to rename all __prandom_u32_state() to prandom_u32_state().
Or should we do the other way round ? Make __prandom_u32_state() the
out-of-line version and just change the few places where the size
explodes like drm_test_buddy_alloc_range_bias(), loss_gilb_ell(),
generate_random_testvec_config(), generate_random_sgl_divisions(),
mutate_buffer(), ... ?
Christophe
From: Ryan Roberts <ryan.roberts@arm.com> Date: 2026-01-19 13:01:46
Previously different architectures were using random sources of
differing strength and cost to decide the random kstack offset. A number
of architectures (loongarch, powerpc, s390, x86) were using their
timestamp counter, at whatever the frequency happened to be. Other
arches (arm64, riscv) were using entropy from the crng via
get_random_u16().
There have been concerns that in some cases the timestamp counters may
be too weak, because they can be easily guessed or influenced by user
space. And get_random_u16() has been shown to be too costly for the
level of protection kstack offset randomization provides.
So let's use a common, architecture-agnostic source of entropy; a
per-cpu prng, seeded at boot-time from the crng. This has a few
benefits:
- We can remove choose_random_kstack_offset(); That was only there to
try to make the timestamp counter value a bit harder to influence
from user space [*].
- The architecture code is simplified. All it has to do now is call
add_random_kstack_offset() in the syscall path.
- The strength of the randomness can be reasoned about independently
of the architecture.
- Arches previously using get_random_u16() now have much faster
syscall paths, see below results.
[*] Additionally, this gets rid of some redundant work on s390 and x86.
Before this patch, those architectures called
choose_random_kstack_offset() under arch_exit_to_user_mode_prepare(),
which is also called for exception returns to userspace which were *not*
syscalls (e.g. regular interrupts). Getting rid of
choose_random_kstack_offset() avoids a small amount of redundant work
for the non-syscall cases.
There have been some claims that a prng may be less strong than the
timestamp counter if not regularly reseeded. But the prng has a period
of about 2^113. So as long as the prng state remains secret, it should
not be possible to guess. If the prng state can be accessed, we have
bigger problems.
Additionally, we are only consuming 6 bits to randomize the stack, so
there are only 64 possible random offsets. I assert that it would be
trivial for an attacker to brute force by repeating their attack and
waiting for the random stack offset to be the desired one. The prng
approach seems entirely proportional to this level of protection.
Performance data are provided below. The baseline is v6.18 with rndstack
on for each respective arch. (I)/(R) indicate statistically significant
improvement/regression. arm64 platform is AWS Graviton3 (m7g.metal).
x86_64 platform is AWS Sapphire Rapids (m7i.24xlarge):
+-----------------+--------------+---------------+---------------+
| Benchmark | Result Class | per-task-prng | per-task-prng |
| | | arm64 (metal) | x86_64 (VM) |
+=================+==============+===============+===============+
| syscall/getpid | mean (ns) | (I) -9.50% | (I) -17.65% |
| | p99 (ns) | (I) -59.24% | (I) -24.41% |
| | p99.9 (ns) | (I) -59.52% | (I) -28.52% |
+-----------------+--------------+---------------+---------------+
| syscall/getppid | mean (ns) | (I) -9.52% | (I) -19.24% |
| | p99 (ns) | (I) -59.25% | (I) -25.03% |
| | p99.9 (ns) | (I) -59.50% | (I) -28.17% |
+-----------------+--------------+---------------+---------------+
| syscall/invalid | mean (ns) | (I) -10.31% | (I) -18.56% |
| | p99 (ns) | (I) -60.79% | (I) -20.06% |
| | p99.9 (ns) | (I) -61.04% | (I) -25.04% |
+-----------------+--------------+---------------+---------------+
I tested an earlier version of this change on x86 bare metal and it
showed a smaller but still significant improvement. The bare metal
system wasn't available this time around so testing was done in a VM
instance. I'm guessing the cost of rdtsc is higher for VMs.
Acked-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
---
arch/Kconfig | 5 ++-
arch/arm64/kernel/syscall.c | 11 ------
arch/loongarch/kernel/syscall.c | 11 ------
arch/powerpc/kernel/syscall.c | 12 -------
arch/riscv/kernel/traps.c | 12 -------
arch/s390/include/asm/entry-common.h | 8 -----
arch/x86/include/asm/entry-common.h | 12 -------
include/linux/randomize_kstack.h | 52 +++++++++-------------------
include/linux/sched.h | 4 ---
init/main.c | 8 +++++
kernel/fork.c | 1 -
11 files changed, 27 insertions(+), 109 deletions(-)
@@ -56,47 +70,15 @@ DECLARE_STATIC_KEY_MAYBE(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,#define add_random_kstack_offset() do { \if(static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,\&randomize_kstack_offset)){\-u32offset=current->kstack_offset;\+u32offset=get_kstack_offset();\u8*ptr=__kstack_alloca(KSTACK_OFFSET_MAX(offset));\/* Keep allocation even after "ptr" loses scope. */\asmvolatile(""::"r"(ptr):"memory");\}\}while(0)-/**-*choose_random_kstack_offset-Choosetherandomoffsetforthenext-*add_random_kstack_offset()-*-*Thisshouldonlybeusedduringsyscallexit.Preemptionmaybeenabled.This-*positioninthesyscallflowisdonetofrustrateattacksfromuserspace-*attemptingtolearnthenextoffset:-*-Maximizethetiminguncertaintyvisiblefromuserspace:ifthe-*offsetischosenatsyscallentry,userspacehasmuchmorecontrol-*overthetimingbetweenchoosingoffsets."How long will we be in-*kernelmode?" tends to be more difficult to predict than "howlong-*willwebeinusermode?"-*-Reducethelifetimeofthenewoffsetsittinginmemoryduring-*kernelmodeexecution.Exposureof"thread-local"memorycontent-*(e.g.current,percpu,etc)tendstobeeasierthanarbitrary-*locationmemoryexposure.-*/-#define choose_random_kstack_offset(rand) do { \-if(static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,\-&randomize_kstack_offset)){\-u32offset=current->kstack_offset;\-offset=ror32(offset,5)^(rand);\-current->kstack_offset=offset;\-}\-}while(0)--staticinlinevoidrandom_kstack_task_init(structtask_struct*tsk)-{-tsk->kstack_offset=0;-}#else /* CONFIG_RANDOMIZE_KSTACK_OFFSET */#define add_random_kstack_offset() do { } while (0)-#define choose_random_kstack_offset(rand) do { } while (0)-#define random_kstack_task_init(tsk) do { } while (0)#endif /* CONFIG_RANDOMIZE_KSTACK_OFFSET */#endif
vmlinux.o: warning: objtool: do_syscall_64+0x2c: call to preempt_count_add() leaves .noinstr.text section
vmlinux.o: warning: objtool: __do_fast_syscall_32+0x3d: call to preempt_count_add() leaves .noinstr.text section
Hmm, clearly Dave was correct not to rush this through... yuck. I'll take a
look, but I guess there is no rush if this won't go into -next until shortly
after -rc1.
Thanks,
Ryan
vmlinux.o: warning: objtool: do_syscall_64+0x2c: call to preempt_count_add() leaves .noinstr.text section
vmlinux.o: warning: objtool: __do_fast_syscall_32+0x3d: call to preempt_count_add() leaves .noinstr.text section
Hmm, clearly Dave was correct not to rush this through... yuck. I'll take a
look, but I guess there is no rush if this won't go into -next until shortly
after -rc1.
Sorry, I should have checked the entry sequencing more thoroughly when I
reviewed this,.
From a quick look, I suspect the right thing to do is to pull the call
to add_random_kstack_offset() a bit later in a few cases; after the
entry logic has run, and after instrumentation_begin() (if the arch code
uses that), such that it doesn't matter if this gets instrumented.
Considering the callers of add_random_kstack_offset(), if we did that:
* arm64 is fine as-is.
* loongarch is fine as-is.
* powerpc's system_call_exception() would need this moved after the
user_exit_irqoff(). Given that function is notrace rather than
noinstr, it looks like there are bigger extant issues here.
* riscv is fine as-is.
* s390's __do_syscall() would need this moved after
enter_from_user_mode().
* On x86:
- do_int80_emulation() is fine as-is.
- int80_emulation() is fine as-is.
- do_int80_syscall_32() would need this moved after
instrumentation_begin().
- __do_fast_syscall_32() would need this moved after
instrumentation_begin().
- do_syscall_64() would need this moved after instrumentation_begin().
Mark.
vmlinux.o: warning: objtool: do_syscall_64+0x2c: call to preempt_count_add() leaves .noinstr.text section
vmlinux.o: warning: objtool: __do_fast_syscall_32+0x3d: call to preempt_count_add() leaves .noinstr.text section
Hmm, clearly Dave was correct not to rush this through... yuck. I'll take a
look, but I guess there is no rush if this won't go into -next until shortly
after -rc1.
Sorry, I should have checked the entry sequencing more thoroughly when I
reviewed this,.
From a quick look, I suspect the right thing to do is to pull the call
to add_random_kstack_offset() a bit later in a few cases; after the
entry logic has run, and after instrumentation_begin() (if the arch code
uses that), such that it doesn't matter if this gets instrumented.
Considering the callers of add_random_kstack_offset(), if we did that:
* arm64 is fine as-is.
* loongarch is fine as-is.
* powerpc's system_call_exception() would need this moved after the
user_exit_irqoff(). Given that function is notrace rather than
noinstr, it looks like there are bigger extant issues here.
* riscv is fine as-is.
* s390's __do_syscall() would need this moved after
enter_from_user_mode().
* On x86:
- do_int80_emulation() is fine as-is.
- int80_emulation() is fine as-is.
- do_int80_syscall_32() would need this moved after
instrumentation_begin().
- __do_fast_syscall_32() would need this moved after
instrumentation_begin().
- do_syscall_64() would need this moved after instrumentation_begin().
Thanks for the detailed suggestions, Mark. I've taken this approach, and
assuming perf testing doesn't throw up any issue, I'm going to revert back to
using the out-of-line version of prandom_u32_state() and will drop patch 2.
Thanks,
Ryan
vmlinux.o: warning: objtool: do_syscall_64+0x2c: call to preempt_count_add() leaves .noinstr.text section
vmlinux.o: warning: objtool: __do_fast_syscall_32+0x3d: call to preempt_count_add() leaves .noinstr.text section
When CONFIG_DEBUG_PREEMPT or CONFIG_TRACE_PREEMP_TOGGLE is set
the preempt_count_[en|dis]able() calls inside [put|get]_cpu_var()
become real functions.
Maybe __preempt_count_[inc|dec]() can be called (with this_cpu_ptr()).
David
vmlinux.o: warning: objtool: do_syscall_64+0x2c: call to preempt_count_add() leaves .noinstr.text section
vmlinux.o: warning: objtool: __do_fast_syscall_32+0x3d: call to preempt_count_add() leaves .noinstr.text section
When CONFIG_DEBUG_PREEMPT or CONFIG_TRACE_PREEMP_TOGGLE is set
the preempt_count_[en|dis]able() calls inside [put|get]_cpu_var()
become real functions.
Maybe __preempt_count_[inc|dec]() can be called (with this_cpu_ptr()).
Or the code could just use the per-cpu data without disabling preemption.
Usually that isn't a good idea at all, but it can't matter in this case.
Might give a noticeable performance gain, disabling preemption is
non-trivial and/or an atomic operation on some architectures.
If anyone is worried about preemption causing the output be repeated, that
would be (mostly) mitigated by checking that s[1234] haven't changed prior
to writing the new values.
I think a 'not locked at all' compare of two of the four values will
stop everything except two threads doing system calls at the same time
getting the same output from the prng.
The whole thing is very unlikely and there will be much easier ways
to break the prng.
Provided s[1234] are only written with valid values (ie ones which aren't
effectively zero) it will continue generating numbers.
David
From: Thomas Gleixner <tglx@kernel.org> Date: 2026-02-22 21:34:30
On Mon, Jan 19 2026 at 13:01, Ryan Roberts wrote:
I tested an earlier version of this change on x86 bare metal and it
showed a smaller but still significant improvement. The bare metal
system wasn't available this time around so testing was done in a VM
instance. I'm guessing the cost of rdtsc is higher for VMs.
No it's not, unless the hypervisor traps RDTSC, which would be insane as
that would cause massive regressions all over the place.
So guessing is not really helpful if you want to argue performance.
Thanks,
tglx
From: David Laight <hidden> Date: 2026-02-23 09:42:02
On Sun, 22 Feb 2026 22:34:26 +0100
Thomas Gleixner [off-list ref] wrote:
On Mon, Jan 19 2026 at 13:01, Ryan Roberts wrote:
quoted
I tested an earlier version of this change on x86 bare metal and it
showed a smaller but still significant improvement. The bare metal
system wasn't available this time around so testing was done in a VM
instance. I'm guessing the cost of rdtsc is higher for VMs.
No it's not, unless the hypervisor traps RDTSC, which would be insane as
that would cause massive regressions all over the place.
So guessing is not really helpful if you want to argue performance.
The cost of rdtsc will depend on the cpu architecture.
To get valid comparisons you need to run on identical systems.
Regardless, the cost of rdtsc could easily be larger than the
cost of the prandom_u32_state() code (especially if inlined or
without all the return thunk 'crap').
David
From: Ryan Roberts <ryan.roberts@arm.com> Date: 2026-03-03 14:43:09
On 22/02/2026 21:34, Thomas Gleixner wrote:
On Mon, Jan 19 2026 at 13:01, Ryan Roberts wrote:
quoted
I tested an earlier version of this change on x86 bare metal and it
showed a smaller but still significant improvement. The bare metal
system wasn't available this time around so testing was done in a VM
instance. I'm guessing the cost of rdtsc is higher for VMs.
No it's not, unless the hypervisor traps RDTSC, which would be insane as
that would cause massive regressions all over the place.
So guessing is not really helpful if you want to argue performance.
Sorry for the slow response. I no longer have access to a recent bare metal x86
system that I can do performance testing on. All I have is the Sapphire Rapids
(m7i.24xlarge) VM.
My original testing was on bare metal Sapphire Rapids (same number of CPUs and
RAM as the VM).
Just to be clear, these are the results I got with bare metal vs vm. Negative is
an improvement (less time). (I)/(R) means statistically significant
improvement/regression:
+-----------------+--------------+---------------+---------------+
| Benchmark | Result Class | x86_64 | x86_64 |
| | | bare metal | VM |
+=================+==============+===============+===============+
| syscall/getpid | mean (ns) | (I) -7.69% | (I) -17.65% |
| | p99 (ns) | 4.14% | (I) -24.41% |
| | p99.9 (ns) | 2.68% | (I) -28.52% |
+-----------------+--------------+---------------+---------------+
| syscall/getppid | mean (ns) | (I) -5.98% | (I) -19.24% |
| | p99 (ns) | -3.11% | (I) -25.03% |
| | p99.9 (ns) | (R) 9.84% | (I) -28.17% |
+-----------------+--------------+---------------+---------------+
| syscall/invalid | mean (ns) | (I) -6.94% | (I) -18.56% |
| | p99 (ns) | (I) -5.57% | (I) -20.06% |
| | p99.9 (ns) | (R) 10.53% | (I) -25.04% |
+-----------------+--------------+---------------+---------------+
So both sets of results represent an improvement, I would say.
Given the level of review that the series has had, I propose to repost today,
then hopefully Kees will be happy to put it in his branch so that it can get
plenty of linux-next soak testing and if there are any x86 regressions lurking,
hopefully ZeroDay will spot them?
Thanks,
Ryan
Like you noted, this is surprising. This would be a good thing to make
sure it goes in very early after -rc1 and gets plenty of wide testing.
But I don't see any problems with the approach, and the move to common
code looks like a big win as well:
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Like you noted, this is surprising. This would be a good thing to make
sure it goes in very early after -rc1 and gets plenty of wide testing.
Right, we are pretty late in the dev cycle (rc6). It would be prudent to get this into -next after the coming rc1 (1 month from now).
On the other hand, the changes are pretty "binary" in the sense that mistakes should be VERY visible right away. Would it be better to take this into -next immediately instead?
But I don't see any problems with the approach, and the move to common
code looks like a big win as well:
From: Dave Hansen <hidden> Date: 2026-01-19 16:51:17
On 1/19/26 08:44, Kees Cook wrote:
quoted
Like you noted, this is surprising. This would be a good thing to
make sure it goes in very early after -rc1 and gets plenty of wide
testing.
Right, we are pretty late in the dev cycle (rc6). It would be
prudent to get this into -next after the coming rc1 (1 month from
now).
On the other hand, the changes are pretty "binary" in the sense that
mistakes should be VERY visible right away. Would it be better to
take this into -next immediately instead?
I think it can go into -next ASAP. It's just a matter of when it goes to
Linus.
Like you noted, this is surprising. This would be a good thing to make
sure it goes in very early after -rc1 and gets plenty of wide testing.
Right, we are pretty late in the dev cycle (rc6). It would be prudent to get this into -next after the coming rc1 (1 month from now).
On the other hand, the changes are pretty "binary" in the sense that mistakes should be VERY visible right away. Would it be better to take this into -next immediately instead?
I don't think this question was really addressed to me, but I'll give my opinion
anyway; I agree it's pretty binary - it will either work or it will explode.
I've tested on arm64 and x86_64 so I have high confidence that it works. If you
get it into -next ASAP it has 3 weeks to soak before the merge window opens
right? (Linus said he would do an -rc8 this cycle). That feels like enough time
to me. But it's your tree ;-)
Thanks,
Ryan
quoted
But I don't see any problems with the approach, and the move to common
code looks like a big win as well:
From: Dave Hansen <hidden> Date: 2026-01-20 16:37:46
On 1/20/26 08:32, Ryan Roberts wrote:
I don't think this question was really addressed to me, but I'll give my opinion
anyway; I agree it's pretty binary - it will either work or it will explode.
I've tested on arm64 and x86_64 so I have high confidence that it works. If you
get it into -next ASAP it has 3 weeks to soak before the merge window opens
right? (Linus said he would do an -rc8 this cycle). That feels like enough time
to me. But it's your tree 😉
First of all, thank you for testing it on x86! Having that one data
point where it helped performance is super valuable.
I'm more worried that it's going to regress performance somewhere and
then it's going to be a pain to back out. I'm not super worried about
functional regressions.
From: Ryan Roberts <ryan.roberts@arm.com> Date: 2026-01-20 16:45:38
On 20/01/2026 16:37, Dave Hansen wrote:
On 1/20/26 08:32, Ryan Roberts wrote:
quoted
I don't think this question was really addressed to me, but I'll give my opinion
anyway; I agree it's pretty binary - it will either work or it will explode.
I've tested on arm64 and x86_64 so I have high confidence that it works. If you
get it into -next ASAP it has 3 weeks to soak before the merge window opens
right? (Linus said he would do an -rc8 this cycle). That feels like enough time
to me. But it's your tree 😉
First of all, thank you for testing it on x86! Having that one data
point where it helped performance is super valuable.
I'm more worried that it's going to regress performance somewhere and
then it's going to be a pain to back out. I'm not super worried about
functional regressions.
From: David Laight <hidden> Date: 2026-01-20 18:45:44
On Tue, 20 Jan 2026 08:37:43 -0800
Dave Hansen [off-list ref] wrote:
On 1/20/26 08:32, Ryan Roberts wrote:
quoted
I don't think this question was really addressed to me, but I'll give my opinion
anyway; I agree it's pretty binary - it will either work or it will explode.
I've tested on arm64 and x86_64 so I have high confidence that it works. If you
get it into -next ASAP it has 3 weeks to soak before the merge window opens
right? (Linus said he would do an -rc8 this cycle). That feels like enough time
to me. But it's your tree 😉
First of all, thank you for testing it on x86! Having that one data
point where it helped performance is super valuable.
I'm more worried that it's going to regress performance somewhere and
then it's going to be a pain to back out. I'm not super worried about
functional regressions.
Unlikely, on x86 the 'rdtsc' is ~20 clocks on Intel cpu and even slower
on amd (according to Agner).
(That is serialised against another rdtsc rather than other instructions.)
Whereas the four TAUSWORTHE() are independent so can execute in parallel.
IIRC each is a memory read and 5 ALU instructions - not much at all.
The slow bit will be the cache miss on the per-cpu data.
You lose a clock at the end because gcc will compile the a | b | c | d
as (((a | b) | c) | d) not ((a | b) | (c | d)).
I think someone reported the 'new' version being faster on x86,
that might be why.
David
On Mon, Jan 19, 2026 at 01:01:07PM +0000, Ryan Roberts wrote:
As I reported at [1], kstack offset randomisation suffers from a couple of bugs
and, on arm64 at least, the performance is poor. This series attempts to fix
both; patch 1 provides back-portable fixes for the functional bugs. Patches 2-3
propose a performance improvement approach.
I've looked at a few different options but ultimately decided that Jeremy's
original prng approach is the fastest. I made the argument that this approach is
secure "enough" in the RFC [2] and the responses indicated agreement.
More details in the commit logs.
Performance
===========
Mean and tail performance of 3 "small" syscalls was measured. syscall was made
10 million times and each individually measured and binned. These results have
low noise so I'm confident that they are trustworthy.
The baseline is v6.18-rc5 with stack randomization turned *off*. So I'm showing
performance cost of turning it on without any changes to the implementation,
then the reduced performance cost of turning it on with my changes applied.
This adds 16 instructions to the system call fast path on s390, however
some quick measurements show that executing this extra code is within
noise ratio performance wise.
Acked-by: Heiko Carstens <hca@linux.ibm.com> # s390