arch/*/include/asm/stackprotector.h: Use get_random_canary() consistently
Subsystems (7)
RFC PATCH 40/50 arch/*/include/asm/stackprotector.h: Use get_random_canary() consistently
... in boot_init_stack_canary().
This is the archetypical example of where the extra security of get_random_bytes() is wasted. The canary is only important as long as it's stored in __stack_chk_guard.
It's also a great example of code that has been copied around a lot and not updated.
Remove the XOR with LINUX_VERSION_CODE as it's pointless; the inclusion of utsname() in init_std_data in the random seeding obviates it.
The XOR with the TSC on x86 and mtfb() on powerPC were left in, as I haven't proved them redundant yet. For those, we call get_random_long(), xor, and mask manually.
FUNCTIONAL CHANGE: mips and xtensa were changed from 64-bit get_random_long() to 56-bit get_random_canary() to match the others, in accordance with the logic in CANARY_MASK.
(We could do 1 bit better and zero *one* of the two high bytes.)
--- arch/arm/include/asm/stackprotector.h | 9 +++------ arch/arm64/include/asm/stackprotector.h | 8 ++------ arch/mips/include/asm/stackprotector.h | 7 ++----- arch/powerpc/include/asm/stackprotector.h | 6 ++---- arch/sh/include/asm/stackprotector.h | 8 ++------ arch/x86/include/asm/stackprotector.h | 4 ++-- arch/xtensa/include/asm/stackprotector.h | 7 ++----- 7 files changed, 15 insertions(+), 34 deletions(-)
arch/arm/include/asm/stackprotector.h +3 -6
@@ -30,17 +30,14 @@ extern unsigned long __stack_chk_guard;*/ static __always_inline void boot_init_stack_canary(void) { -unsigned long canary; -/* Try to get a semi random initial value. */ -get_random_bytes (&canary,sizeof (canary)); - canary ^=LINUX_VERSION_CODE ; +unsigned long canary =get_random_canary (); current->stack_canary = canary;#ifndef CONFIG_STACKPROTECTOR_PER_TASK - __stack_chk_guard = current->stack_canary; + __stack_chk_guard = canary;#else -current_thread_info ()->stack_canary = current->stack_canary; +current_thread_info ()->stack_canary = canary;#endif }
arch/arm64/include/asm/stackprotector.h +2 -6
@@ -26,16 +26,12 @@ extern unsigned long __stack_chk_guard;*/ static __always_inline void boot_init_stack_canary(void) { -unsigned long canary; -/* Try to get a semi random initial value. */ -get_random_bytes (&canary,sizeof (canary)); - canary ^=LINUX_VERSION_CODE ; - canary &=CANARY_MASK ; +unsigned long canary =get_random_canary (); current->stack_canary = canary;if (!IS_ENABLED (CONFIG_STACKPROTECTOR_PER_TASK )) - __stack_chk_guard = current->stack_canary; + __stack_chk_guard = canary; }#endif /* _ASM_STACKPROTECTOR_H */
arch/mips/include/asm/stackprotector.h +2 -5
@@ -28,14 +28,11 @@ extern unsigned long __stack_chk_guard;*/ static __always_inline void boot_init_stack_canary(void) { -unsigned long canary; -/* Try to get a semi random initial value. */ -get_random_bytes (&canary,sizeof (canary)); - canary ^=LINUX_VERSION_CODE ; +unsigned long canary =get_random_canary (); current->stack_canary = canary; - __stack_chk_guard = current->stack_canary; + __stack_chk_guard = canary; }#endif /* _ASM_STACKPROTECTOR_H */
arch/powerpc/include/asm/stackprotector.h +2 -4
@@ -21,12 +21,10 @@*/ static __always_inline void boot_init_stack_canary(void) { -unsigned long canary; -/* Try to get a semi random initial value. */ - canary =get_random_canary (); +unsigned long canary =get_random_long (); + canary ^=mftb (); - canary ^=LINUX_VERSION_CODE ; canary &=CANARY_MASK ; current->stack_canary = canary;
arch/sh/include/asm/stackprotector.h +2 -6
@@ -15,15 +15,11 @@ extern unsigned long __stack_chk_guard;*/ static __always_inline void boot_init_stack_canary(void) { -unsigned long canary; -/* Try to get a semi random initial value. */ -get_random_bytes (&canary,sizeof (canary)); - canary ^=LINUX_VERSION_CODE ; - canary &=CANARY_MASK ; +unsigned long canary =get_random_canary (); current->stack_canary = canary; - __stack_chk_guard = current->stack_canary; + __stack_chk_guard = canary; }#endif /* __ASM_SH_STACKPROTECTOR_H */
arch/x86/include/asm/stackprotector.h +2 -2
@@ -72,9 +72,9 @@ static __always_inline void boot_init_stack_canary(void)* there it already has some randomness on most systems. Later * on during the bootup the random pool has true entropy too. */ -get_random_bytes (&canary,sizeof (canary)); + canary =get_random_u64 (); tsc =rdtsc (); - canary += tsc + (tsc <<32UL ); + canary += tsc + (tsc <<32 ); canary &=CANARY_MASK ; current->stack_canary = canary;
arch/xtensa/include/asm/stackprotector.h +2 -5
@@ -27,14 +27,11 @@ extern unsigned long __stack_chk_guard;*/ static __always_inline void boot_init_stack_canary(void) { -unsigned long canary; -/* Try to get a semi random initial value. */ -get_random_bytes (&canary,sizeof (canary)); - canary ^=LINUX_VERSION_CODE ; +unsigned long canary =get_random_canary (); current->stack_canary = canary; - __stack_chk_guard = current->stack_canary; + __stack_chk_guard = canary; }#endif /* _ASM_STACKPROTECTOR_H */
Signature · 1 line
2.26.0
Will Deacon
maintainerQuoted 46 lines · George Spelvin
... in
boot_init_stack_canary().This is the archetypical example of where the extra security of
get_random_bytes()is wasted. The canary is only important as long as it's stored in__stack_chk_guard.It's also a great example of code that has been copied around a lot and not updated.
Remove the XOR with
LINUX_VERSION_CODEas it's pointless; the inclusion ofutsname()ininit_std_datain the random seeding obviates it.The XOR with the TSC on x86 and
mtfb()on powerPC were left in, as I haven't proved them redundant yet. For those, we callget_random_long(), xor, and mask manually.FUNCTIONAL CHANGE: mips and xtensa were changed from 64-bit
get_random_long()to 56-bitget_random_canary()to match the others, in accordance with the logic inCANARY_MASK.(We could do 1 bit better and zero *one* of the two high bytes.)
Signed-off-by George Spelvin <[email protected]>Cc Russell King <[email protected]>Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Ralf Baechle <[email protected]>Cc Paul Burton <[email protected]>Cc James Hogan <[email protected]>Cc Benjamin Herrenschmidt <[email protected]>Cc Paul Mackerras <[email protected]>Cc Michael Ellerman <[email protected]>Cc Yoshinori Sato <[email protected]>Cc Rich Felker <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc Borislav Petkov <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Chris Zankel <[email protected]>Cc Max Filippov <[email protected]>
Quoted 9 lines
--- arch/arm/include/asm/stackprotector.h | 9 +++------ arch/arm64/include/asm/stackprotector.h | 8 ++------ arch/mips/include/asm/stackprotector.h | 7 ++----- arch/powerpc/include/asm/stackprotector.h | 6 ++---- arch/sh/include/asm/stackprotector.h | 8 ++------ arch/x86/include/asm/stackprotector.h | 4 ++-- arch/xtensa/include/asm/stackprotector.h | 7 ++----- 7 files changed, 15 insertions(+), 34 deletions(-)
Just found this kicking around in the depths of my inbox. Is the series dead?
Will