public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: 清水祐太郎 <simiyu@shift-crops.net>
To: "libc-help@sourceware.org" <libc-help@sourceware.org>
Subject: malloc/free: tcache security patch
Date: Fri, 20 Apr 2018 12:44:00 -0000	[thread overview]
Message-ID: <20180420124408.3C16F10E8052@mail.shift-crops.net> (raw)

Hello

I'm Yutaro Shimizu (ShiftCrops).

I want to patch malloc.c.
The mechanism of tcache is very similar to fastbins.
However, I can malloc from arbitrary addresses by tampering the tcache_entry.
That's because there is no chunk verification process.
I think it is an important security issue.

I created a patch as follows.
```
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 9614954..4967fcd 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -2926,8 +2926,13 @@ static __always_inline void
 tcache_put (mchunkptr chunk, size_t tc_idx)
 {
   tcache_entry *e = (tcache_entry *) chunk2mem (chunk);
+  tcache_entry *old = tcache->entries[tc_idx];
+
   assert (tc_idx < TCACHE_MAX_BINS);
-  e->next = tcache->entries[tc_idx];
+  if (__builtin_expect (old == e, 0))
+    malloc_printerr ("free(): double free or corruption (tcache)");
+
+  e->next = old;
   tcache->entries[tc_idx] = e;
   ++(tcache->counts[tc_idx]);
 }
@@ -2938,8 +2943,13 @@ static __always_inline void *
 tcache_get (size_t tc_idx)
 {
   tcache_entry *e = tcache->entries[tc_idx];
+  size_t victim_tc_idx = csize2tidx (chunksize (mem2chunk (e)));
+
   assert (tc_idx < TCACHE_MAX_BINS);
   assert (tcache->entries[tc_idx] > 0);
+  if (__builtin_expect (victim_tc_idx != tc_idx, 0))
+    malloc_printerr ("malloc(): memory corruption (tcache)");
+
   tcache->entries[tc_idx] = e->next;
   --(tcache->counts[tc_idx]);
   return (void *) e;
```
I created a bundle with patchwork, but I can not add this patch.
What should I do?

Sincerely

Windows 10 版のメールから送信

             reply	other threads:[~2018-04-20 12:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-20 12:44 清水祐太郎 [this message]
2018-04-20 21:36 ` Ondřej Bílka
2018-04-21  0:58   ` 清水祐太郎
2018-04-21  2:06     ` Carlos O'Donell
2018-04-21  5:37       ` 清水祐太郎

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=20180420124408.3C16F10E8052@mail.shift-crops.net \
    --to=simiyu@shift-crops.net \
    --cc=libc-help@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).