Skip to content

tcache double free

复制本地路径 | 在线编辑

攻击方式

和 fastbin double free 一样,这里就直接放 fastbin 的图。一开始甚至不需要中间间隔,即 free(A); free(A) 都可以。

防御:增加 key 字段

在 glibc-2.33 之后,多加了一个 key 字段:

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

有这个全局变量

static uintptr_t tcache_key;

  • 进程级全局随机值
  • 初始化于 ptmalloc_init
  • 通常来自 getrandom() 或 ASLR entropy

free 的时候,进行 e->key = tcache_key;,在 malloc 的时候,会进行置 0 的操作。每次 free 的时候都会检查:

    if (__glibc_unlikely (e->key == tcache_key))

通过这样的 O(1) 操作,只要被 free 掉,下次再 free 就会报错。

问题:可不可以泄露 key

可以,但知道 key 也没办法绕过检查呀... free 的时候会检查是否相等,所以知道 key 也没啥用。

除非是能把 free 掉的内存对于的 key 改写(不需要清空),这样才能过掉检查,但这个有点太难了,因为能这样做说明已经有 UAF 写 能力了。

Comments