Skip to content

tcache house of spirit

复制本地路径 | 在线编辑

fastbin hosue of spirit 文章: 链接

house of spirit,即伪造一个块的结构,然后让它能 free 到某个链表上去。在 glibc 没有 tcache 的时候就有了,那时用的 fastbin。现在 glibc 有 tcache,理所当然地,tcache 也有这样的方法。

建议直接和 fastbin house of spirit 对照着看。比 fastbin 少一个要求。

简单说明

直接准备 fakechunk,只需要构造下面 fakechunk->size,想要 free 到哪里那就写上对应的大小。对于 size 上的 FLAG 也有需求,低 3 个 flag 位中:PREV_INUSE 位(最低位)可以随便,但 IS_MMAPPED、NON_MAIN_ARENA 必须是 0。

// 假设我们想 free 到 0x40 的 tcachebin 上面
fakechunk[1] = 0x40;

然后我们就可以 free 这个块,即 free(&fakechunk[2]),之所以是 fakechunk[2],是因为 free 的参数是数据区开头,而不是整体的头部。我们的构造块如下:

+++++++++++++ <-- fakechunk
+           +
+   0x40    +
+++++++++++++ <-- fakechunk[2] (fakechunk->data)
+    fd     +
+           +
+           +
+   .....   +
+           +
+           +
+-----------+

最终 fakechunk 会落入 fastbin 中,我们再先多次 malloc 把 tcache 清空,最后再来一次就得到 fakechunk->data 了。

想一想,对于某个我们想控制的地址 target(这里是 fakechunk->data):

  1. 我们需要能写 target 前面 8byte 的内容(对应于上面的 fakechunk[1]
  2. target 的地址要是 16 的整数倍,这个不多解释了,基本上这个条件都能满足

最难办的还是第一条,我们想要控制某个地址,必须要控制这个地址前面那一块的内容。比 fastbin 好的是,fastbin 的 house of spirit 还需要控制下一个块的 size

Comments