mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master 1/2] tlsf: hardening: unpoison trailing padding before zeroing it
Date: Tue, 22 Apr 2025 07:37:39 +0200	[thread overview]
Message-ID: <20250422053740.3436389-1-a.fatoum@pengutronix.de> (raw)

The actual allocated buffer size can be bigger than what was requested
due to alignment.

When KASAN is enabled, only the requested size is unpoisoned.
This currently leads to problems, because with
CONFIG_INIT_ON_ALLOC_DEFAULT_ON enabled, the whole allocated
buffer will be zeroed.

If we fix that, we will instead run into a problem when freeing the
buffer while CONFIG_INIT_ON_ALLOC_DEFAULT_ON is enabled:
We don't record the actual size for later use and thus trying to zero
all of the buffer will again trip over the poisoned padding at the end.

Fix this by first unpoisoning the whole buffer, zeroing it and then
restoring poisoning of off-limits memory.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/tlsf.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/common/tlsf.c b/common/tlsf.c
index 5504453a9453..01293630dd7c 100644
--- a/common/tlsf.c
+++ b/common/tlsf.c
@@ -607,10 +607,19 @@ static void* block_prepare_used(control_t* control, block_header_t* block,
 
 		kasan_poison_shadow(&block->size, size + 2 * sizeof(size_t),
 			    KASAN_KMALLOC_REDZONE);
-		kasan_unpoison_shadow(p, used);
 
-		if (want_init_on_alloc())
+		if (want_init_on_alloc()) {
+			kasan_unpoison_shadow(p, size);
 			memzero_explicit(p, size);
+			/*
+			 * KASAN doesn't play nicely with poisoning addresses
+			 * that are not granule-aligned, which is why we poison
+			 * the full size and then unpoison the rest.
+			 */
+			kasan_poison_shadow(p, size, 0xff);
+		}
+
+		kasan_unpoison_shadow(p, used);
 	}
 	return p;
 }
@@ -1017,8 +1026,10 @@ void tlsf_free(tlsf_t tlsf, void* ptr)
 		control_t* control = tlsf_cast(control_t*, tlsf);
 		block_header_t* block = block_from_ptr(ptr);
 		tlsf_assert(!block_is_free(block) && "block already marked as free");
-		if (want_init_on_free())
+		if (want_init_on_free()) {
+			kasan_unpoison_shadow(ptr, block_size(block));
 			memzero_explicit(ptr, block_size(block));
+		}
 		kasan_poison_shadow(ptr, block_size(block), 0xff);
 		block_mark_as_free(block);
 		block = block_merge_prev(control, block);
-- 
2.39.5




             reply	other threads:[~2025-04-22  5:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-22  5:37 Ahmad Fatoum [this message]
2025-04-22  5:37 ` [PATCH master 2/2] tlsf: hardening: skip KASAN checks when zeroing memory Ahmad Fatoum
2025-04-22  7:47 ` [PATCH master 1/2] tlsf: hardening: unpoison trailing padding before zeroing it Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250422053740.3436389-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox