mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] test: self: malloc: fix double free after expect_alloc_fail
@ 2025-10-21 14:54 Ahmad Fatoum
  2025-10-21 14:54 ` [PATCH 2/2] test: self: malloc: fix false positive when malloc store is registered Ahmad Fatoum
  2025-10-22  6:27 ` [PATCH 1/2] test: self: malloc: fix double free after expect_alloc_fail Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2025-10-21 14:54 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

If the realloc call unexpectedly succeeds, the p pointer is invalidated,
but it's free'd unconditionally later on, which triggers a
user-after-free.

Account for this by zeroing p when it becomes stale.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 test/self/malloc.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/test/self/malloc.c b/test/self/malloc.c
index 52f9fc344c1a..a60e95b2233f 100644
--- a/test/self/malloc.c
+++ b/test/self/malloc.c
@@ -109,9 +109,15 @@ static void test_malloc(void)
 
 	if (mem_malloc_size) {
 		tmp = expect_alloc_fail(realloc(p, mem_malloc_size));
+		if (tmp)
+			p = NULL;
+
 		free(tmp);
 
 		tmp = expect_alloc_fail(realloc(p, RELOC_HIDE(MALLOC_MAX_SIZE, -1)));
+		if (tmp)
+			p = NULL;
+
 		free(tmp);
 	} else {
 		skipped_tests += 2;
-- 
2.47.3




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

end of thread, other threads:[~2025-10-22  6:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-10-21 14:54 [PATCH 1/2] test: self: malloc: fix double free after expect_alloc_fail Ahmad Fatoum
2025-10-21 14:54 ` [PATCH 2/2] test: self: malloc: fix false positive when malloc store is registered Ahmad Fatoum
2025-10-22  6:27 ` [PATCH 1/2] test: self: malloc: fix double free after expect_alloc_fail Sascha Hauer

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