2024-03-09 20:25:02

by Kees Cook

[permalink] [raw]
Subject: [PATCH] randomize_kstack: Improve entropy diffusion

The kstack_offset variable was really only ever using the low bits for
kernel stack offset entropy. Add a ror32() to increase bit diffusion.

Suggested-by: Arnd Bergmann <[email protected]>
Fixes: 39218ff4c625 ("stack: Optionally randomize kernel stack offset each syscall")
Signed-off-by: Kees Cook <[email protected]>
---
Cc: Jeremy Linton <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: [email protected]
---
include/linux/randomize_kstack.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/randomize_kstack.h b/include/linux/randomize_kstack.h
index 5d868505a94e..6d92b68efbf6 100644
--- a/include/linux/randomize_kstack.h
+++ b/include/linux/randomize_kstack.h
@@ -80,7 +80,7 @@ DECLARE_PER_CPU(u32, kstack_offset);
if (static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT, \
&randomize_kstack_offset)) { \
u32 offset = raw_cpu_read(kstack_offset); \
- offset ^= (rand); \
+ offset = ror32(offset, 5) ^ (rand); \
raw_cpu_write(kstack_offset, offset); \
} \
} while (0)
--
2.34.1



2024-04-03 22:01:07

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH] randomize_kstack: Improve entropy diffusion

On Sat, 09 Mar 2024 12:24:48 -0800, Kees Cook wrote:
> The kstack_offset variable was really only ever using the low bits for
> kernel stack offset entropy. Add a ror32() to increase bit diffusion.
>
>

Applied to for-next/hardening:

[1/1] randomize_kstack: Improve entropy diffusion
https://git.kernel.org/kees/c/9c573cd31343

--
Kees Cook