tools/nolibc: ensure stack protector guard is never zero
authorThomas Weißschuh <linux@weissschuh.net>
Sun, 21 May 2023 09:36:31 +0000 (11:36 +0200)
committerPaul E. McKenney <paulmck@kernel.org>
Fri, 9 Jun 2023 18:46:08 +0000 (11:46 -0700)
The all-zero pattern is one of the more probable out-of-bound writes so
add a special case to not accidentally accept it.

Also it enables the reliable detection of stack protector initialization
during testing.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
tools/include/nolibc/stackprotector.h

index 77e5251c4490b7ae7a3cb7fb8abed18e34e483e0..b0156fc077a0270b764077c7cf307908aa330649 100644 (file)
@@ -45,8 +45,9 @@ __attribute__((weak,no_stack_protector,section(".text.nolibc_stack_chk")))
 void __stack_chk_init(void)
 {
        my_syscall3(__NR_getrandom, &__stack_chk_guard, sizeof(__stack_chk_guard), 0);
-       /* a bit more randomness in case getrandom() fails */
-       __stack_chk_guard ^= (uintptr_t) &__stack_chk_guard;
+       /* a bit more randomness in case getrandom() fails, ensure the guard is never 0 */
+       if (__stack_chk_guard != (uintptr_t) &__stack_chk_guard)
+               __stack_chk_guard ^= (uintptr_t) &__stack_chk_guard;
 }
 #endif /* defined(NOLIBC_STACKPROTECTOR) */