]> git.itanic.dy.fi Git - linux-stable/commitdiff
stackprotector: actually use get_random_canary()
authorJason A. Donenfeld <Jason@zx2c4.com>
Sun, 23 Oct 2022 20:14:23 +0000 (22:14 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Fri, 18 Nov 2022 01:18:10 +0000 (02:18 +0100)
The RNG always mixes in the Linux version extremely early in boot. It
also always includes a cycle counter, not only during early boot, but
each and every time it is invoked prior to being fully initialized.
Together, this means that the use of additional xors inside of the
various stackprotector.h files is superfluous and over-complicated.
Instead, we can get exactly the same thing, but better, by just calling
`get_random_canary()`.

Acked-by: Guo Ren <guoren@kernel.org> # for csky
Acked-by: Catalin Marinas <catalin.marinas@arm.com> # for arm64
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
arch/arm/include/asm/stackprotector.h
arch/arm64/include/asm/stackprotector.h
arch/csky/include/asm/stackprotector.h
arch/mips/include/asm/stackprotector.h
arch/powerpc/include/asm/stackprotector.h
arch/riscv/include/asm/stackprotector.h
arch/sh/include/asm/stackprotector.h
arch/x86/include/asm/stackprotector.h
arch/xtensa/include/asm/stackprotector.h

index 088d03161be5ca3ab2b52fb05a281136ba5a51ae..0bd4979759f17224ada83ffa2c1fdc7589149f79 100644 (file)
@@ -15,9 +15,6 @@
 #ifndef _ASM_STACKPROTECTOR_H
 #define _ASM_STACKPROTECTOR_H 1
 
-#include <linux/random.h>
-#include <linux/version.h>
-
 #include <asm/thread_info.h>
 
 extern unsigned long __stack_chk_guard;
@@ -30,11 +27,7 @@ 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
index 33f1bb4531509ceb6665d98488892f6da30ddac6..ae3ad80f51fe190d0c9bad22c54d95b8741a7910 100644 (file)
@@ -13,8 +13,6 @@
 #ifndef __ASM_STACKPROTECTOR_H
 #define __ASM_STACKPROTECTOR_H
 
-#include <linux/random.h>
-#include <linux/version.h>
 #include <asm/pointer_auth.h>
 
 extern unsigned long __stack_chk_guard;
@@ -28,12 +26,7 @@ extern unsigned long __stack_chk_guard;
 static __always_inline void boot_init_stack_canary(void)
 {
 #if defined(CONFIG_STACKPROTECTOR)
-       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))
index d7cd4e51edd965e0a3a67e62214074b799be701c..d23747447166d1a9a1f5d2784e07c066d0bb56d0 100644 (file)
@@ -2,9 +2,6 @@
 #ifndef _ASM_STACKPROTECTOR_H
 #define _ASM_STACKPROTECTOR_H 1
 
-#include <linux/random.h>
-#include <linux/version.h>
-
 extern unsigned long __stack_chk_guard;
 
 /*
@@ -15,12 +12,7 @@ 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;
index 68d4be9e125471562869d85b9973533ed534300d..518c192ad982054754ad355b9d8eba7dc461b76a 100644 (file)
@@ -15,9 +15,6 @@
 #ifndef _ASM_STACKPROTECTOR_H
 #define _ASM_STACKPROTECTOR_H 1
 
-#include <linux/random.h>
-#include <linux/version.h>
-
 extern unsigned long __stack_chk_guard;
 
 /*
@@ -28,11 +25,7 @@ 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;
index 1c8460e235838ccfe7ee23c59a1854f438d5efa0..283c346478565c22a6f49c7d3ca2af36cdf10841 100644 (file)
@@ -7,8 +7,6 @@
 #ifndef _ASM_STACKPROTECTOR_H
 #define _ASM_STACKPROTECTOR_H
 
-#include <linux/random.h>
-#include <linux/version.h>
 #include <asm/reg.h>
 #include <asm/current.h>
 #include <asm/paca.h>
  */
 static __always_inline void boot_init_stack_canary(void)
 {
-       unsigned long canary;
-
-       /* Try to get a semi random initial value. */
-       canary = get_random_canary();
-       canary ^= mftb();
-       canary ^= LINUX_VERSION_CODE;
-       canary &= CANARY_MASK;
+       unsigned long canary = get_random_canary();
 
        current->stack_canary = canary;
 #ifdef CONFIG_PPC64
index 09093af46565e5674a34e21e59808d4c3f36e5fc..43895b90fe3f60825b9ce09b6a1c20a690262e50 100644 (file)
@@ -3,9 +3,6 @@
 #ifndef _ASM_RISCV_STACKPROTECTOR_H
 #define _ASM_RISCV_STACKPROTECTOR_H
 
-#include <linux/random.h>
-#include <linux/version.h>
-
 extern unsigned long __stack_chk_guard;
 
 /*
@@ -16,12 +13,7 @@ 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))
index 35616841d0a1c8cb6a4082590cfe5aa5f6d78494..665dafac376f8d0006fafef21b774eed05df7afd 100644 (file)
@@ -2,9 +2,6 @@
 #ifndef __ASM_SH_STACKPROTECTOR_H
 #define __ASM_SH_STACKPROTECTOR_H
 
-#include <linux/random.h>
-#include <linux/version.h>
-
 extern unsigned long __stack_chk_guard;
 
 /*
@@ -15,12 +12,7 @@ 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;
index 24a8d6c4fb185d98e5ac7d639474300d28b2f096..00473a650f512b2e47110e21726756bb2d018cd3 100644 (file)
@@ -34,7 +34,6 @@
 #include <asm/percpu.h>
 #include <asm/desc.h>
 
-#include <linux/random.h>
 #include <linux/sched.h>
 
 /*
  */
 static __always_inline void boot_init_stack_canary(void)
 {
-       u64 canary;
-       u64 tsc;
+       unsigned long canary = get_random_canary();
 
 #ifdef CONFIG_X86_64
        BUILD_BUG_ON(offsetof(struct fixed_percpu_data, stack_canary) != 40);
 #endif
-       /*
-        * We both use the random pool and the current TSC as a source
-        * of randomness. The TSC only matters for very early init,
-        * 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));
-       tsc = rdtsc();
-       canary += tsc + (tsc << 32UL);
-       canary &= CANARY_MASK;
 
        current->stack_canary = canary;
 #ifdef CONFIG_X86_64
index e368f94fd2af39ca9052641ad668b553ca25fc82..dd10279a2378d13dff221d2034dd181d3d28c975 100644 (file)
@@ -14,9 +14,6 @@
 #ifndef _ASM_STACKPROTECTOR_H
 #define _ASM_STACKPROTECTOR_H 1
 
-#include <linux/random.h>
-#include <linux/version.h>
-
 extern unsigned long __stack_chk_guard;
 
 /*
@@ -27,11 +24,7 @@ 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;