public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Ben Kallus <benjamin.p.kallus.gr@dartmouth.edu>
To: libc-alpha@sourceware.org
Cc: Ben Kallus <benjamin.p.kallus.gr@dartmouth.edu>
Subject: [PATCH] malloc: Add size check when moving fastbin->tcache
Date: Thu,  6 Feb 2025 16:37:10 -0500	[thread overview]
Message-ID: <20250206213709.2394624-2-benjamin.p.kallus.gr@dartmouth.edu> (raw)

By overwriting a forward link in a fastbin chunk that is subsequently
moved into the tcache, it's possible to get malloc to return an
arbitrary address [0].

When a chunk is fetched from a fastbin, its size is checked against the
expected chunk size for that fastbin (see malloc.c:3991). This patch
adds a similar check for chunks being moved from a fastbin to tcache,
which renders obsolete the exploitation technique described above.

[0]: https://github.com/shellphish/how2heap/blob/master/glibc_2.39/fastbin_reverse_into_tcache.c

Signed-off-by: Ben Kallus <benjamin.p.kallus.gr@dartmouth.edu>
---
 malloc/malloc.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 27dfd1eb90..3e8a20ae87 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -4005,6 +4005,9 @@ _int_malloc (mstate av, size_t bytes)
 		    {
 		      if (__glibc_unlikely (misaligned_chunk (tc_victim)))
 			malloc_printerr ("malloc(): unaligned fastbin chunk detected 3");
+		      size_t victim_tc_idx = csize2tidx (chunksize (tc_victim));
+		      if (__builtin_expect (tc_idx != victim_tc_idx, 0))
+			malloc_printerr ("malloc(): chunk size mismatch in fastbin");
 		      if (SINGLE_THREAD_P)
 			*fb = REVEAL_PTR (tc_victim->fd);
 		      else
-- 
2.48.1


             reply	other threads:[~2025-02-06 21:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-06 21:37 Ben Kallus [this message]
2025-02-11 18:16 ` Adhemerval Zanella Netto
2025-02-11 19:29 Ben Kallus
2025-02-13 17:56 ` Adhemerval Zanella Netto

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=20250206213709.2394624-2-benjamin.p.kallus.gr@dartmouth.edu \
    --to=benjamin.p.kallus.gr@dartmouth.edu \
    --cc=libc-alpha@sourceware.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).