mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH master 1/4] KASan: fix possible breakage with CONFIG_INIT_STACK_ALL
@ 2025-04-22  7:55 Ahmad Fatoum
  2025-04-22  7:55 ` [PATCH master 2/4] ARM: Layerscape: fix compilation of LS1028 without LS1046 Ahmad Fatoum
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Ahmad Fatoum @ 2025-04-22  7:55 UTC (permalink / raw)
  To: barebox

From: Ahmad Fatoum <a.fatoum@barebox.org>

With CONFIG_INIT_STACK_ALL_PATTERN or CONFIG_INIT_STACK_ALL_ZERO,
the compiler emits calls to memset to initialize variables on the stack.

With kasan enabled, this would end up calling KASAN memset, which can
lead to access of global variables prior to relocation.

Brace ourselves for this situation by sidestepping KASAN prior to
relocation.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 lib/kasan/generic.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/lib/kasan/generic.c b/lib/kasan/generic.c
index 66eebac15600..dda80f790151 100644
--- a/lib/kasan/generic.c
+++ b/lib/kasan/generic.c
@@ -17,6 +17,7 @@
 #define pr_fmt(fmt) "kasan: generic: " fmt
 
 #include <common.h>
+#include <asm/reloc.h>
 
 #include "kasan.h"
 
@@ -160,6 +161,16 @@ static __always_inline bool check_memory_region_inline(unsigned long addr,
 						size_t size, bool write,
 						unsigned long ret_ip)
 {
+	/*
+	 * Hardening options like -ftrivial-auto-var-init=zero can end up
+	 * emitting memset calls to initialize stack variables.
+	 * This can lead to this function reached before relocation.
+	 *
+	 * Play it safe by ensuring we are relocated before proceeding.
+	 */
+	if (global_variable_offset() != 0)
+		return true;
+
 	if (!kasan_initialized)
 		return true;
 
-- 
2.39.5




^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2025-04-22 15:49 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-22  7:55 [PATCH master 1/4] KASan: fix possible breakage with CONFIG_INIT_STACK_ALL Ahmad Fatoum
2025-04-22  7:55 ` [PATCH master 2/4] ARM: Layerscape: fix compilation of LS1028 without LS1046 Ahmad Fatoum
2025-04-22 14:14   ` Sascha Hauer
2025-04-22 14:18     ` Ahmad Fatoum
2025-04-22  7:55 ` [PATCH master 3/4] ARM: k3: define debug_ll_write_reg only on CONFIG_DEBUG_AM62X_UART=y Ahmad Fatoum
2025-04-22 14:23   ` Sascha Hauer
2025-04-22 14:29     ` Ahmad Fatoum
2025-04-22 14:34       ` Sascha Hauer
2025-04-22  7:55 ` [PATCH master 4/4] memory: correct return value in request_barebox_region stub Ahmad Fatoum
2025-04-22  9:29 ` [PATCH master 1/4] KASan: fix possible breakage with CONFIG_INIT_STACK_ALL Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox