This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
malloc/free: tcache security patch
- From: 清水祐太郎 <simiyu at shift-crops dot net>
- To: "libc-help at sourceware dot org" <libc-help at sourceware dot org>
- Date: Fri, 20 Apr 2018 21:44:10 +0900
- Subject: malloc/free: tcache security patch
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 版のメールから送信