malloc/free: tcache security patch
清水祐太郎
simiyu@shift-crops.net
Fri Apr 20 12:44:00 GMT 2018
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 版のメールから送信
More information about the Libc-help
mailing list