首页
社区
课程
招聘
[原创][how2heap] 1. fastbin_dup.c 详解 - tcache、fastbin和double free
发表于: 2026-11-10 17:20 1181

[原创][how2heap] 1. fastbin_dup.c 详解 - tcache、fastbin和double free

2026-11-10 17:20
1181

double free 的原理演示。

此为how2heap中的题目,请查看how2heap的GitHub地址

本文是我学堆的第一个示例的笔记,对各种基础概念的介绍和笔记好像有点太多了QAQ,但是非常非常详细。刚开始学堆的小白一个,如有错误还请大佬们指正OwO(大佬们轻喷)

内容索引


本题内容


基础知识(方便obsidian其他地方引用)

详细注释及知识介绍


tcache和fastbin介绍

文件内容详细注释(请放到nano中获取更好的观看体验)

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int main()
{
	/*
	关掉标准输出的缓冲。不加这句的话,printf
	的输出会攒在缓冲区里,在 gdb 里单步或管
	道重定向时看不到实时输出,影响调试。
	*/
	setbuf(stdout, NULL);

	printf("This file demonstrates a simple double-free attack with fastbins.\n");



	/* 填满tcache

	tcache 是 glibc 2.26 引入的一个每线程独立的小缓存,本质是一个数组 + 单链表,
	free 掉的 chunk 会先放在tcache,下次 malloc 相同大小直接从 tcache 拿,不用走
	复杂的 bin 逻辑,速度快很多。

	glibc中,只要 tcache 没满,free 一律优先进 tcache

        tcache 的结构是一个数组,数组的每个位置(叫 bin / 索引)对应一个固定的 chunk 大小档位:
		tcache 索引 0  → 专门存 0x20 大小的 chunk
		tcache 索引 1  → 专门存 0x30 大小的 chunk
		tcache 索引 2  → 专门存 0x40 大小的 chunk
		tcache 索引 3  → 专门存 0x50 大小的 chunk
		...
		tcache 索引 63 → 专门存 0x410 大小的 chunk
	共 64 个 bin
	• 同一个 bin 里,所有 chunk 大小完全一样(都是这一档的大小)
	• 不同 bin 之间,大小不一样(0x20、0x30、0x40... 逐档递增)
	你的 chunk 是多大,free 后就进对应的那一档

	tcache 的每一个 bin 最多放 7 个chunk
	*/
	printf("Fill up tcache first.\n");
	void *ptrs[8];
	for (int i=0; i<8; i++) {
		/* 循环8次,在堆上创建 8 个 0x20 字节的块

		malloc(8) 请求8字节,header需要16字节,8+16=24,chunk要求16字节(0x10)
		对齐,这里对齐到 0x20 ,在堆上获取一个大小为 0x20 的块
		*/
		ptrs[i] = malloc(8);
	}
	for (int i=0; i<7; i++) {
		/* free 其中 7 个块

		前面讲过,tcache 的每个 bin 只能挂 7 个chunk
		这里每个 free 的 chunk 大小均为 0x20,free 后都进 0x20 的 bin 。
		- 每次 free,chunk 进 tcache 的 0x20 索引,计数 +1
		- 第 7 个 free 完,计数 = 7,这个 bin 满了
		所以后面 free 的 0x20 大小的 chunk 不能放在 tcache 中(相应的bin
		满了),后面的 free 无处可去,只能进 fastbin

		所以只 free 了七个 chunk,第八个 chunk 留着不释放。这个 chunk 是冗
		余的,不必管它*/
		free(ptrs[i]);
	}



	/* 使用 calloc 分配三块内存

	这里使用 calloc 而不用 malloc 是因为:
		malloc  分配非全零内存  会先查 tcache,从 tcache 取
		calloc  承诺全零内存    会跳过 tcache
	calloc 承诺返回全零的内存,而 tcache 里的 chunk 是之前 free 掉的、可能残留
	脏数据,glibc 为了「保证清零」,让 calloc 直接不碰 tcache,从 top chunk(堆
	顶的空白区)切新块。

	值得注意的是,calloc 只会跳过 tcache,却不会跳过 fastbin 等。如果其他 bins
	有可用的 chunk,calloc 会将这个 chunk 清零后复用

	这是为了保证 `tcache 已满` 的状态,保证后面的 free 进 fastbin
	如果这里用 malloc,会先把 tcache 里那 7 个 chunk 吐出来,tcache 没有满,后面
	的 free 就会再进 tcache ,和我们学习 fastbin 的目的不符
	*/
	printf("Allocating 3 buffers.\n");
	int *a = calloc(1, 8);
	int *b = calloc(1, 8);
	int *c = calloc(1, 8);

	printf("1st calloc(1, 8): %p\n", a);
	printf("2nd calloc(1, 8): %p\n", b);
	printf("3rd calloc(1, 8): %p\n", c);

	/* 第一次 free
	• 查 tcache:满了(7/7)→ 进不去
	• 于是进 fastbin(0x20 属于 fastbin 范围)
	• fastbin 是单链表 LIFO(后进先出),新 free 的放最前面

	于是 fastbin 的结构变成:
		fastbin[0x20]:  a  (a 是链表头)
	*/
	printf("Freeing the first one...\n");
	free(a);


	/* glibc 会做 fastbin double free 检测:检查要 free 的 chunk 是不是当前链表头?
	如果是,会报错(如 free(a) -> free(a) 报错)。所以如果你解开下面的 free(a) 的注
	释,编译时会报错
	但是fastbin只检查链表头的审查很弱,free(a) -> free(b) -> free(a) 就可以绕过检测*/
	printf("If we free %p again, things will crash because %p is at the top of the free list.\n", a, a);
	// free(a);


	/* 第二次 free

	glibc 检查 b 不是链表头,同理进 fastbin,b 变成新的链表头
		fastbin[0x20]:  b -> a
	*/
	printf("So, instead, we'll free %p.\n", b);
	free(b);


	/* 再次free a,double free

	glibc 做 fastbin double free 检测:要 free 的 a 是不是当前链表头?而当前链表头
	是 b,不是 a 。检测通过,a 再次被塞进链表头
		fastbin[0x20]:  a -> b -> a
	*/
	printf("Now, we can free %p again, since it's not the head of the free list.\n", a);
	free(a);


	/* 三次 calloc,把 fastbin 中的 a→b→a 环消费掉*/
	printf("Now the free list has [ %p, %p, %p ]. If we malloc 3 times, we'll get %p twice!\n", a, b, a, a);
	a = calloc(1, 8);        // a , 剩余链表 b -> a
	b = calloc(1, 8);        // b , 剩余链表 a
	c = calloc(1, 8);        // a , 剩余链表 [空],获取了两次 a
	printf("1st calloc(1, 8): %p\n", a);
	printf("2nd calloc(1, 8): %p\n", b);
	printf("3rd calloc(1, 8): %p\n", c);


	/*
	assert 函数在条件不成立时会立即终止程序并报告错误位置
	这里我们检查 a 和 c 是否指向同一块内存,assert(a == c) 成立

	否则终止程序,证明在当前libc版本下此例子不可用
	*/
	assert(a == c);
}






/* fastbin

(这一段来源对AI信息的整理提纯)

fastbin 是 glibc 里专门给「小块内存」做快速分配/释放的一批链表,比 tcache 早很多(远古
版本就有)。
程序里绝大多数 malloc 都是很小的、频繁分配又频繁释放的(比如一个 16 字节的字符串节点)。
为这些小 chunk 走完整的 bin 逻辑太慢,所以搞了个「快速通道」——fastbin。

和 tcache 一样,fastbin 也是按档位分箱,但范围和档位不同:

fastbin 数组:
  fastbin[0] → 0x20 档
  fastbin[1] → 0x30 档
  fastbin[2] → 0x40 档
  fastbin[3] → 0x50 档
  fastbin[4] → 0x60 档
  fastbin[5] → 0x70 档
  fastbin[6] → 0x80 档
超过 0x80 的 chunk free 之后,就不走 fastbin了,进 unsorted bin 或按大小进 small/large bin。

特点 1:单链表,只用 fd 字段
一个被 free 的 chunk,它头部那 16 字节后面(用户数据区的前 8 字节)会被拿来存 fd,指向
链表里的下一个 chunk。

free 状态的 fastbin chunk:
+------------------+
| prev_size        |
+------------------+
| size             |
+------------------+
| fd  → 下一个chunk |   ← 用户数据区前 8 字节被复用
+------------------+

特点 2:free 后不清除数据、不清 PREV_INUSE 位
这是 fastbin 最特殊的地方。普通 chunk free 之后会合并(consolidate)、会更新标志位,
但 fastbin 为了「快」,free 的时候不合并、不清除下一个 chunk 的 PREV_INUSE 位。
• 好处:快
• 坏处:chunk 上残留的旧数据、旧指针还在,给利用留了空间

特点 3:LIFO,新 free 的放链表头
free 顺序是先 a 后 b,链表就是 b → a(b 在头)。malloc 时从链表头取,所以先取回 b。

四、double free 检测机制(回到那个例子)
glibc 对 fastbin 有一个简单的 double free 检测——注意,它很弱:
    只检查「当前要 free 的 chunk 是不是链表头」
注意它只比对了「链表头」这一块,没扫描整个链表。 所以:
• free(a); free(a) → 第二次 free 时链表头是 a,等于 p → 检测触发,崩
• free(a); free(b); free(a) → 第二次 free(a) 时链表头是 b,不等于 a → 检测不触发,绕过成功
这就是为什么 fastbin_dup 里必须「夹一个 free(b)」。

五、fastbin 和 tcache 的关系(2.26 之后)
现代 glibc 里必须搞懂的一层:
1. tcache 优先级更高:free 一个 chunk,只要 tcache 没满,优先进 tcache,根本不轮到 fastbin。
2. tcache 容量小:每档 7 个,满了就装不下。
3. tcache 被「跳过」的入口:前面说的 calloc 会跳过 tcache,直接操作 fastbin。
所以现代环境里想玩 fastbin,几乎都要先做一件事:填满 tcache(7个),把 free 逼进 fastbin。
这正是 fastbin_dup.c 开头那 7 次 free 的意义。



一句话总结:
fastbin 是「小 chunk 的快速单链表通道」,LIFO、只用 fd、free 不合并,double free 检测只
查链表头——这个「只查头」的弱点,就是 fastbin_dup 的立足点。而在 2.26 之后,想碰到 fastbin,
得先填满 tcache。

• fastbin 是 glibc bin 体系的一部分。它和 small bin、large bin、unsorted bin 一起,被
  malloc/free 的核心分配器(_int_malloc/_int_free)统一管理。它要维护全局的一致性、要
  走那套复杂的 bin 逻辑。

• tcache 是「独立于 bin 体系之外」的一层旁路缓存。它在分配器最前面拦一道,free 先进
  tcache、malloc 先查 tcache,根本不进入核心 bin 逻辑。

一句话:fastbin 是「主路里的一条快速车道」,tcache 是「主路之外单独开的一条捷径」。

  |     维度    |              fastbin              |              tcache              |
  |:----------- |:--------------------------------- |:-------------------------------- |
  |引入版本     |远古                               |2.26                              |
  |大小范围     |0x20 ~ 0x80                        |0x20 ~ 0x410                      |
  |档位数       |7                                  |64                                |
  |每档上限     |无(理论不限)                     |7 个                              |
  |free 优先级  |低(tcache 满了才轮到)            |高(先走它)                      |
  |顺序         |LIFO                               |LIFO                              |
  |归属         |bin 体系内                         |bin 体系外的旁路缓存              |
  |数据结构     |单链表(fd)                       |单链表(fd) + 额外一个 count 计数|
  |大小范围     |0x20  0x80(7 档)                 |0x20  0x410(64 档)              |
  |每档上限     |无                                 |7 个(有计数限制)                |
  |线程         |全局 arena,要加锁                 |每线程独立,基本无锁              |
  |free 时检查  |有 double free 检测(弱)等        |早期几乎零检查(2.29 后才补)     |
  |malloc 时处理|走 _int_malloc 完整流程,有各种边界|拿到就返回,几乎不做处理          |
  |chunk 标志位 |维护 PREV_INUSE 等                 |free 后基本不动标志位             |

### 为什么有了 fastbin 还要 tcache?
一句话:为了多线程性能,把「快」推到极致。
fastbin 虽然叫「fast」,但它有两个开销省不掉:
1. 锁。fastbin 挂在全局 arena 上,多个线程同时 malloc/free 要抢锁。高并发下锁竞争是性能
   杀手。
2. 一致性维护。fastbin 再快,也还是要走 _int_free 那套:检查、维护标志位、处理边界,每一
   步都是 CPU 指令。
而现代程序(尤其浏览器、大型服务端)是重度多线程 + 疯狂小对象分配,fastbin 的这两个开销
被无限放大。

tcache 的解法极其简单粗暴:
• 每个线程一个独立的 tcache,自己的缓存自己用,基本不用加锁 → 锁竞争消失
• free 进去几乎什么都不检查,malloc 拿出来几乎什么都不处理 → 一致性维护省掉
• 用一个 count 计数限制每档 7 个,防止无限堆积
这套设计的来源其实是 2015 年 Google 在 glibc 社区提的一个 tcache 补丁(参考了 jemalloc 等
现代分配器的 per-thread cache 思想),后来被合入 2.26。本质就是「用一点内存换巨大的多线程
性能提升」。

### 这个区别对堆利用意味着什么(重点)
作为学堆利用的人,你要记住的是:
1. tcache 优先级永远高于 fastbin。free 一个 chunk,只要 tcache 没满就先进 tcache,fastbin
   根本轮不到。所以现代环境玩 fastbin 必先填满 tcache——这正是 fastbin_dup.c 开头那 7 次
   free 的由来。
2. tcache 的「零检查」是双刃剑。它为了快省略的检查,正是攻击者的机会。2.262.28 的 tcache
   几乎无防护,tcache poisoning 极其好用;后来 2.29 加了 double free 检测、2.32 加了
   safe-linking 加密 fd,就是官方在慢慢「补上这些省掉的检查」。
3. 理解「旁路」这个概念,后面学 calloc 跳过 tcache、_int_malloc 里的 tcache 提取逻辑(
   tcache_stashing_unlink_attack 就是打这里)时,你会豁然开朗。

一句话总结:
fastbin 是 bin 主路里的小块快速通道,要锁、要维护一致性;tcache 是主路之外每线程独立的旁
路缓存,几乎零检查零锁。引入 tcache 是为了用「省掉锁和检查」来换多线程性能,而省掉的那些
检查,反过来成了堆利用的主战场。
*/

fastbin和tcache的差别(只是把上面的表格搬下来)


fastbin和tcache的差别
维度 fastbin tcache
引入版本 远古 2.26
大小范围 0x20 ~ 0x80 0x20 ~ 0x410
档位数 7 64
每档上限 无(理论不限) 7 个
free 优先级 低(tcache 满了才轮到) 高(先走它)
顺序 LIFO LIFO
归属 bin 体系内 bin 体系外的旁路缓存
数据结构 单链表(fd) 单链表(fd) + 额外一个 count 计数
大小范围 0x20 0x80(7 档) 0x20 0x410(64 档)
每档上限 无 7 个(有计数限制)
线程 全局 arena,要加锁 每线程独立,基本无锁
free 时检查 有 double free 检测(弱)等 早期几乎零检查(2.29 后才补)
malloc 时处理 走 _int_malloc 完整流程,有各种边界 拿到就返回,几乎不做处理
chunk 标志位 维护 PREV_INUSE 等 free 后基本不动标志位

调试,查看tcache和fastbin的变化


安装[[pwndbg]]后,我们逐行调试,理解堆的变化。我们会用到以下pwndbg命令:

  • n 单步步过,执行下一条代码
  • x/20x 以16进制显示某地址的内容
  • vmmap 查看当前进程的虚拟内存布局(这里只用了其简单的功能,也可在cat /proc/[PID]/maps查看比较原始的信息)
  • tcache 查看当前线程的 tcache ,显示的是原始字段(counts 计数和 entries 链表头指针)
  • tcachebins 以易读的形式显示tcache中的内容
  • fastbins 查看 fastbin 的内容

记不得不要紧,后面用到我会讲解的

开始

使用gdb ./fastbin_dup开始调试,这里我安装了pwndbg。

┌──(unknown㉿Kali-ThinkPad)-[~/…/Git/how2heap/glibc_2.31/yunjiao_notes]
└─$ gdb -q ./fastbin_dup 
   run pwndbg: [ source /home/unknown/Desktop/Git/pwndbg/gdbinit.py ]
Reading symbols from ./fastbin_dup...
(gdb) source /home/unknown/Desktop/Git/pwndbg/gdbinit.py
pwndbg> 

在main函数下下断点

pwndbg> # 在main函数下断点,程序在函数入口停止,等待我们调试
pwndbg> b main
Breakpoint 1 at 0x11a1: file glibc_2.31/fastbin_dup.c, line 7.

8次malloc

正在使用的chunk结构

r开始运行程序,使用n不断执行,直到for (int i=0; i<8; i++) { ptrs[i] = malloc(8); }处,查看此时的堆里有什么

──────────────────────────────────────────────────────[ SOURCE (CODE) ]───────────────────────────────────────────────────────
In file: /home/unknown/Desktop/Git/how2heap/glibc_2.31/fastbin_dup.c:13
    7         setbuf(stdout, NULL);
    8 
    9         printf("This file demonstrates a simple double-free attack with fastbins.\n");
   10 
   11         printf("Fill up tcache first.\n");
   12         void *ptrs[8];
 ► 13         for (int i=0; i<8; i++) {
   14                 ptrs[i] = malloc(8);
   15         }
   16         for (int i=0; i<7; i++) {
   17                 free(ptrs[i]);
   18         }
   19 
   20         printf("Allocating 3 buffers.\n");
──────────────────────────────────────────────────────────[ STACK ]───────────────────────────────────────────────────────────
00:0000│ rsp 0x7fffffffd9b0 {ptrs} —▸ 0x555555554040 ◂— 0x400000006
01:0008│-058 0x7fffffffd9b8 {ptrs+0x8} ◂— 0xe
02:0010│-050 0x7fffffffd9c0 {ptrs+0x10} —▸ 0x7fffffffda30 ◂— 0x1f7fbb7a0
03:0018│-048 0x7fffffffd9c8 {ptrs+0x18} —▸ 0x7fffffffdef9 ◂— 0x34365f363878 /* 'x86_64' */
04:0020│-040 0x7fffffffd9d0 {ptrs+0x20} —▸ 0x7ffff7fd15e0 ◂— endbr64
05:0028│-038 0x7fffffffd9d8 {ptrs+0x28} —▸ 0x7ffff7fe0d60 ◂— endbr64
06:0030│-030 0x7fffffffd9e0 {ptrs+0x30} —▸ 0x7ffff7fc42e8 ◂— 0
07:0038│-028 0x7fffffffd9e8 {ptrs+0x38} —▸ 0x7ffff7e19e27 (__cxa_atexit+71) ◂— test rax, rax
────────────────────────────────────────────────────────[ BACKTRACE ]─────────────────────────────────────────────────────────
 ► 0 0x5555555551d3 main+58
   1 0x7ffff7df7083 __libc_start_main+243
   2 0x5555555550d1 _start+33
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────



pwndbg> # 查看当前内存布局,可以看到没有 [heap] 堆
pwndbg> vmmap
LEGEND: STACK | HEAP | CODE | DATA | WX | RODATA
             Start                End Perm     Size  Offset File (set vmmap-prefer-relpaths on)
    0x555555554000     0x555555555000 r--p     1000       0 fastbin_dup
    0x555555555000     0x555555556000 r-xp     1000    1000 fastbin_dup
    0x555555556000     0x555555557000 r--p     1000    2000 fastbin_dup
    0x555555557000     0x555555558000 r--p     1000    2000 fastbin_dup
    0x555555558000     0x555555559000 rw-p     1000    3000 fastbin_dup
    0x7ffff7dd3000     0x7ffff7df5000 r--p    22000       0 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/libc-2.31.so
    0x7ffff7df5000     0x7ffff7f6d000 r-xp   178000   22000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/libc-2.31.so
    0x7ffff7f6d000     0x7ffff7fbb000 r--p    4e000  19a000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/libc-2.31.so
    0x7ffff7fbb000     0x7ffff7fbf000 r--p     4000  1e7000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/libc-2.31.so
    0x7ffff7fbf000     0x7ffff7fc1000 rw-p     2000  1eb000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/libc-2.31.so
    0x7ffff7fc1000     0x7ffff7fc7000 rw-p     6000       0 [anon_7ffff7fc1]
    0x7ffff7fc7000     0x7ffff7fcb000 r--p     4000       0 [vvar]
    0x7ffff7fcb000     0x7ffff7fcd000 r--p     2000       0 [vvar_vclock]
    0x7ffff7fcd000     0x7ffff7fcf000 r-xp     2000       0 [vdso]
    0x7ffff7fcf000     0x7ffff7fd0000 r--p     1000       0 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/ld-2.31.so
    0x7ffff7fd0000     0x7ffff7ff3000 r-xp    23000    1000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/ld-2.31.so
    0x7ffff7ff3000     0x7ffff7ffb000 r--p     8000   24000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/ld-2.31.so
    0x7ffff7ffc000     0x7ffff7ffd000 r--p     1000   2c000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/ld-2.31.so
    0x7ffff7ffd000     0x7ffff7ffe000 rw-p     1000   2d000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/ld-2.31.so
    0x7ffff7ffe000     0x7ffff7fff000 rw-p     1000       0 [anon_7ffff7ffe]
    0x7ffffffdd000     0x7ffffffff000 rw-p    22000       0 [stack]



pwndbg> # 两次n,执行到 malloc(8) 被执行,发现 [heap] 堆段出现
pwndbg> n
pwndbg> n
pwndbg> vmmap
LEGEND: STACK | HEAP | CODE | DATA | WX | RODATA
             Start                End Perm     Size  Offset File (set vmmap-prefer-relpaths on)
    0x555555554000     0x555555555000 r--p     1000       0 fastbin_dup
    0x555555555000     0x555555556000 r-xp     1000    1000 fastbin_dup
    0x555555556000     0x555555557000 r--p     1000    2000 fastbin_dup
    0x555555557000     0x555555558000 r--p     1000    2000 fastbin_dup
    0x555555558000     0x555555559000 rw-p     1000    3000 fastbin_dup
    0x555555559000     0x55555557a000 rw-p    21000       0 [heap]
    0x7ffff7dd3000     0x7ffff7df5000 r--p    22000       0 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/libc-2.31.so
    0x7ffff7df5000     0x7ffff7f6d000 r-xp   178000   22000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/libc-2.31.so
    0x7ffff7f6d000     0x7ffff7fbb000 r--p    4e000  19a000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/libc-2.31.so
    0x7ffff7fbb000     0x7ffff7fbf000 r--p     4000  1e7000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/libc-2.31.so
    0x7ffff7fbf000     0x7ffff7fc1000 rw-p     2000  1eb000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/libc-2.31.so
    0x7ffff7fc1000     0x7ffff7fc7000 rw-p     6000       0 [anon_7ffff7fc1]
    0x7ffff7fc7000     0x7ffff7fcb000 r--p     4000       0 [vvar]
    0x7ffff7fcb000     0x7ffff7fcd000 r--p     2000       0 [vvar_vclock]
    0x7ffff7fcd000     0x7ffff7fcf000 r-xp     2000       0 [vdso]
    0x7ffff7fcf000     0x7ffff7fd0000 r--p     1000       0 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/ld-2.31.so
    0x7ffff7fd0000     0x7ffff7ff3000 r-xp    23000    1000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/ld-2.31.so
    0x7ffff7ff3000     0x7ffff7ffb000 r--p     8000   24000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/ld-2.31.so
    0x7ffff7ffc000     0x7ffff7ffd000 r--p     1000   2c000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/ld-2.31.so
    0x7ffff7ffd000     0x7ffff7ffe000 rw-p     1000   2d000 ../../glibc-all-in-one/libs/2.31-0ubuntu9.18_amd64/ld-2.31.so
    0x7ffff7ffe000     0x7ffff7fff000 rw-p     1000       0 [anon_7ffff7ffe]
    0x7ffffffdd000     0x7ffffffff000 rw-p    22000       0 [stack]



pwndbg> # 查看 ptrs 数组中的值,发现其第一个已经是一个chunk地址,后面都是垃圾值
pwndbg> p ptrs
$1 = {0x5555555592a0, 0xe, 0x7fffffffda50, 0x7fffffffdf19,
  0x7ffff7fd15e0, 0x7ffff7fe0d60, 0x7ffff7fc42e8,
  0x7ffff7e19e27 <__cxa_atexit+71>}



pwndbg> # 在行16下断点,c执行到16行 `for (int i=0; i<7; i++) {free(ptrs[i]);}`
pwndbg> # 处,开始查看 free 的行为
pwndbg> b 16
pwndbg> c

pwndbg> # 查看8次 malloc(8) ptrs 后的值。发现里面已经装满了 chunk 的地址
pwndbg> p ptrs
$2 = {0x5555555592a0, 0x5555555592c0, 0x5555555592e0, 0x555555559300,
  0x555555559320, 0x555555559340, 0x555555559360, 0x555555559380}

pwndbg> # 查看 chunk 中的数据,从上面可以看到第一个 chunk 地址为 0x5555555592a0
pwndbg> x/20x 0x5555555592a0-0x10
0x555555559290: 0x00000000      0x00000000      0x00000021      0x00000000
0x5555555592a0: 0x00000000      0x00000000      0x00000000      0x00000000
0x5555555592b0: 0x00000000      0x00000000      0x00000021      0x00000000
0x5555555592c0: 0x00000000      0x00000000      0x00000000      0x00000000
0x5555555592d0: 0x00000000      0x00000000      0x00000021      0x00000000

pwndbg> # 让我把一个单独的 chunk 分割给你看 \
0x555555559290: 0x00000000      0x00000000      0x00000021      0x00000000\
0x5555555592a0: 0x00000000      0x00000000      0x00000000      0x00000000\

pwndbg> # 一个正在使用中的 chunk 的结构为:
pwndbg> # +---------------------------------+  <- chunk
pwndbg> # |           prev_size             |
pwndbg> # +---------------------------------+
pwndbg> # |  Size of chunk, in bytes  |A|M|P|
pwndbg> # +---------------------------------+
pwndbg> # |  User data ...                  |
pwndbg> # .                                 .
pwndbg> # .                                 .
pwndbg> # +---------------------------------+
pwndbg> # | Next chunk ...                  |  <- 下一个 chunk
pwndbg> # 
pwndbg> #
pwndbg> # chunk header 包括 prev_size、chunk_size、A、M、P 字段,
pwndbg> # 长度为 16 字节。
pwndbg> #
pwndbg> # malloc(8) 返回的指针指向 User data 区域起始,ptrs 中第一个 
pwndbg> # chunk 指针为 0x5555555592a0 ,向上 16 字节才是此 chunk 的
pwndbg> # chunk header。所以第一个 chunk 的起始地址应该为
pwndbg> # 0x5555555592a0 - 0x10 (User data起始 - chunk header长度),
pwndbg> # 这就是上面
pwndbg> #     `x/20x 0x5555555592a0-0x10`
pwndbg> # 而不是
pwndbg> #     `x/20x 0x5555555592a0`
pwndbg> # 的原因
pwndbg> # 
pwndbg> # prev_size 为上一个 chunk 的大小,大小为 8 字节。当上一个 chunk 
pwndbg> # 被使用时,其下一个 chunk 的此字段无效,所以我们看见上面 chunk 的
pwndbg> # 0x555555559290 ~ 0x555555559298 (prev_size)中全是 0 。当
pwndbg> # 然里面也可能是垃圾值。prev_size 仅在前一个 chunk 是 free 的时有
pwndbg> # 意义,方便向后合并
pwndbg> # 
pwndbg> # 字段 chunk_size + A + M + P 的大小也是 8 字节,其中 A、M、P 各
pwndbg> # 只有 1 位(1 比特)。chunk_size 储存的是当前 chunk 的大小,而AMP:
pwndbg> #     - A(NON_MAIN_ARENA)‌:当前 chunk 是否属于非主分配区。
pwndbg> #     - M(IS_MMAPPED)‌:当前 chunk 是否由 mmap 分配。
pwndbg> #     - P(PREV_INUSE)‌:前一个 chunk 是否在使用,1 为使用中,0 空闲
pwndbg> # 
pwndbg> # 所以我们看见上面 chunk header 中有 0x21 ,因为 chunk 被强制 16
pwndbg> # 字节对齐,chunk的大小必须是 0x10 的整数倍(0x10、0x20、0x30 ...)
pwndbg> # ,而无论后面三比特 AMP 如何变化,都不会达到 0x10 影响前面的大小
pwndbg> # 
pwndbg> # 所以我们可以分开来看 0x21 ,其可以分为 0x20 和最后3比特提供的 0x1。
pwndbg> # - 0x20 即是 chunk_size ,标志此 chunk 的大小为 0x20 字节
pwndbg> # - 0x1  意味着 AMP 三比特值分别为 001 (换成16进制数字为0x1),
pwndbg> #        即标志位 A->0, M->0, P->1 ,参照上面标志位表,前一个 chunk
pwndbg> #        正在被使用中(相应的,如 0x2 意味着 AMP -> 010)
pwndbg> # 这个数应该在 0x555555559298~0x0x55555555929f 这一个 8 字节数的
pwndbg> # 末尾,但是我们看见 0x21 被放在中间:
pwndbg> #     0x555555559290: 0x00000021      0x00000000
pwndbg> # 这是因为 amd64 架构采用的小端存储,低位放在低地址,高位放在高地址。这
pwndbg> # 一整个 8 字节被看作一个数字,请类比返回地址放在内存中的形式,这里确实是
pwndbg> # 最低处
pwndbg> # 
pwndbg> # 不过这样说的话,用 x/4gx 看或许更好一点(才发现。。。不想改了)
pwndbg> x/4gx 0x5555555592a0-0x10
0x555555559290: 0x0000000000000000      0x0000000000000021
0x5555555592a0: 0x0000000000000000      0x0000000000000000
pwndbg> # 还有 heap 和 vis-heap-chunks 可以看堆,但这里就不提了

8次free

tcache中的chunk和单链表

然后我们来看看free时发生了什么

──────────────────────────────────────────────[ SOURCE (CODE) ]───────────────────────────────────────────────
In file: /home/unknown/Desktop/Git/how2heap/glibc_2.31/fastbin_dup.c:17
   11         printf("Fill up tcache first.\n");
   12         void *ptrs[8];
   13         for (int i=0; i<8; i++) {
   14                 ptrs[i] = malloc(8);
   15         }
   16         for (int i=0; i<7; i++) {
 ► 17                 free(ptrs[i]);
   18         }
   19 
   20         printf("Allocating 3 buffers.\n");
   21         int *a = calloc(1, 8);
   22         int *b = calloc(1, 8);
   23         int *c = calloc(1, 8);
   24 
──────────────────────────────────────────────────────────────────────────────────────────────────────────────


pwndbg> # 这里我们才刚到 17 行,还没有执行 free,让我们先来看看 tcache

pwndbg> # 查看 tcache 中的信息,现在里面是空的(empty)
pwndbg> tcachebins 
tcachebins
empty

pwndbg> # 查看 tcache 原始信息,现在里面什么都没有
pwndbg> tcache
tcache is pointing to: 0x555555559010 for thread 1
{
  counts = {0x0 <repeats 64 times>},
  entries = {0x0 <repeats 64 times>},
}



pwndbg> # 向前两步,执行到第一个 free 结束
pwndbg> n
pwndbg> n



pwndbg> # 第一次 free 后,我们看看 tcache 有了什么变化

pwndbg> # 查看 tcache 中的信息,现在里面不是空的了,保存 0x20 大小 chunk 的
pwndbg> # bin 中挂了一个 chunk,其值为 ptrs[0]
pwndbg> tcachebins 
tcachebins
0x20 [  1]:  0x5555555592a0 ◂— 0

pwndbg> # 查看 tcache 原始信息,计数变为 1
pwndbg> tcache
tcache is pointing to: 0x555555559010 for thread 1
{
  counts = {0x1, 0x0 <repeats 63 times>},
  entries = {0x5555555592a0, 0x0 <repeats 63 times>},
}



pwndbg> # 我们再 free 一次,再次查看 tcache 的变化
pwndbg> n
pwndbg> n

pwndbg> tcachebins 
tcachebins
0x20 [  2]:  0x5555555592c0 —▸ 0x5555555592a0 ◂— 0

pwndbg> tcache
tcache is pointing to: 0x555555559010 for thread 1
{
  counts = {0x2, 0x0 <repeats 63 times>},
  entries = {0x5555555592c0, 0x0 <repeats 63 times>},
}



pwndbg> # 运行出 for 循环,查看 7 次 free 后的 tcache
pwndbg> b 20
pwndbg> c

pwndbg> # 计数为 7 ,根据我们在前面源码注释中的知识,tcache 满了
pwndbg> tcachebins 
tcachebins
0x20 [  7]:  0x555555559360 —▸ 0x555555559340 —▸ 0x555555559320 —▸ 
0x555555559300 —▸ 0x5555555592e0 —▸ 0x5555555592c0 —▸ 0x5555555592a0 ◂— 0                            
pwndbg> tcache
tcache is pointing to: 0x555555559010 for thread 1
{
  counts = {0x7, 0x0 <repeats 63 times>},
  entries = {0x555555559360, 0x0 <repeats 63 times>},
}

pwndbg> # 查看 tcache 中的数据
pwndbg> x/20gx 0x5555555592a0-0x10
0x555555559290: 0x0000000000000000      0x0000000000000021
0x5555555592a0: 0x0000000000000000      0x0000555555559010
0x5555555592b0: 0x0000000000000000      0x0000000000000021
0x5555555592c0: 0x00005555555592a0      0x0000555555559010
0x5555555592d0: 0x0000000000000000      0x0000000000000021
0x5555555592e0: 0x00005555555592c0      0x0000555555559010
0x5555555592f0: 0x0000000000000000      0x0000000000000021
0x555555559300: 0x00005555555592e0      0x0000555555559010
0x555555559310: 0x0000000000000000      0x0000000000000021
0x555555559320: 0x0000555555559300      0x0000555555559010

pwndbg > # 在普通 chunk free 中,glibc 会
pwndbg > # 1. 检查前后邻居是否 free
pwndbg > # 2. 如果前一个 chunk 是 free 的,就通过 prev_size 把两个
pwndbg > #    chunk 合并
pwndbg > # 3. 合并后,将合并后的当前 chunk 的大小写入下一个 chunk 的
pwndbg > #    prev_size,并把下一个 chunk 的 P(PREV_INUSE)‌ 标志位
pwndbg > #    清为 0
pwndbg > # 
pwndbg > # 而为了效率,fastbin 和 tcache 不会修改 chunk header,也
pwndbg > # 不合并邻居,只会在用户数据区写数据。所以我们看见上面的
pwndbg > # chunk header 完全没变(包括P标志位)。因此 fastbin 和
pwndbg > # tcache 中的 chunk 仍然 “假装自己还在使用”
pwndbg > # 
pwndbg > # fastbin 在写用户数据区时只写前 8 字节 fd,而 tcache 会写前
pwndbg > # 16 字节(fd 和 key)。
pwndbg > #   - fd : 指向 tcache 链表中的下一个 chunk (指向的是用户区
pwndbg > #          起始地址),是单链表的链接
pwndbg > #   - key: (fastbin 没有)glibc 2.29 引入的 double free
pwndbg > #          检测标记。其值是 tcache_perthread_struct 的地址
pwndbg > #          (tcache 的管理结构,存在堆的最开头)。每次 free 
pwndbg > #          进 tcache 前,会检查这个 chunk 的 key 值是否等于
pwndbg > #          这个地址。如果等于,则此 chunk 的 key 已经被 tcache
pwndbg > #          修改过,判定 double free 崩掉;
pwndbg > #          不等于则放行入 tcache 并将 key 改为此特定地址
pwndbg > # 
pwndbg > # 下面我解析一下上面 tcache 的具体结构: \
第一个 chunk :\
0x555555559290: 0x0000000000000000      0x0000000000000021 \
0x5555555592a0: 0x0000000000000000      0x0000555555559010 \

pwndbg > # 这里的 prev_size(0x555555559290) 和 size(0x555555559298)
pwndbg > # 没有任何改变。
pwndbg > # fd 在 0x5555555592a0,这里 fd = 0x0。我们发现只有这一个 chunk
pwndbg > # 的 fd 为 0x0 。因为 tcache 是 LIFO(后入先出)单链表,第一个
pwndbg > # 被 free 进入的 chunk 会变为 `链尾` 。它后面什么都没有,所以
pwndbg > # fd = NULL(0) 
pwndbg > # key 在 0x5555555592a8 处,这里 key = 0x0000555555559010
pwndbg > # 
pwndbg > # 第二个 chunk: \
0x5555555592b0: 0x0000000000000000      0x0000000000000021 \
0x5555555592c0: 0x00005555555592a0      0x0000555555559010 \

pwndbg > # chunk header 没有变化
pwndbg > # fd = 0x00005555555592a0,指向我们前面讲的那一个 chunk 的用户段
pwndbg > # key = 0x0000555555559010,与其他 tcache 中的 chunk 一致
pwndbg > # 
pwndbg > # 同理,我们发现 tcache 中的每一个 chunk,其 fd 指向下一个 chunk
pwndbg > # 的用户数据段,而链尾的 fd 为 0x0 。每一个 chunk 都有 fd 指向下一
pwndbg > # 个链表,可以从头往后,而不能从后往头找 ———— 这就是单链表

3次calloc

pwndbg > # 我们现在还只是在
pwndbg > #     20 |  printf("Allocating 3 buffers.\n");
pwndbg > # 这里。接下来就要 calloc 3 个 chunk。
pwndbg > n
pwndbg > n
pwndbg > n
pwndbg > n

pwndbg > # 现在程序已经运行到
pwndbg > #     25 |  printf("1st calloc(1, 8): %p\n", a);
pwndbg > # 前面的三个
pwndbg > #     21 |  int *a = calloc(1, 8);
pwndbg > #     22 |  int *b = calloc(1, 8);
pwndbg > #     23 |  int *c = calloc(1, 8);
pwndbg > # 已经执行完毕,让我们来看一下 a、b、c的值

pwndbg> p a
$3 = (int *) 0x5555555593a0
pwndbg> p b
$4 = (int *) 0x5555555593c0
pwndbg> p c
$5 = (int *) 0x5555555593e0

pwndbg > # 我们前面已经讲了(源码注释那里),calloc 保证返回全 0 
pwndbg > # 的干净内存,所以它会忽略可能留存脏数据的 tcache,从堆顶
pwndbg > # 切新内存。这可以防止我们放到 tcache 中的 chunk 被拿出
pwndbg > # 来,让 tcache 一直是满的。
pwndbg > # 所以这里我们看见a、b、c持有的都是更高处新地址。如果你使用
pwndbg > # x/12gx 查看他们的内容,会发现和前面的 malloc 获取的
pwndbg > # chunk 一致

pwndbg > # 使用 heap 命令看堆,我们发现多出了三个新 chunk
pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x555555559000
Size: 0x290 (with flag bits: 0x291)

... ... 这里省略 tcache 中第七到第二个 chunk ... ...

Free chunk (tcachebins) | PREV_INUSE  <- 看到是 free 的 chunk
Addr: 0x555555559350                  <- 我们认出是 tcache 的链表头 chunk
Size: 0x20 (with flag bits: 0x21)      - 而且 Addr 是 header 的地址 (
fd: 0x555555559340                     - user_data起始地址 - 0x10)
                                      <- fd 也会显示

Allocated chunk | PREV_INUSE     <- 在使用的 chunk
Addr: 0x555555559370             <- 这里其实是没释放的 ptrs[7] 呢
Size: 0x20 (with flag bits: 0x21)

Allocated chunk | PREV_INUSE     <- 如果你前面看过 heap,会发现这是新chunk
Addr: 0x555555559390             <- a,  a user_data: 0x5555555593a0
Size: 0x20 (with flag bits: 0x21)

Allocated chunk | PREV_INUSE
Addr: 0x5555555593b0             <- b,  b user_data: 0x5555555593c0
Size: 0x20 (with flag bits: 0x21)

Allocated chunk | PREV_INUSE
Addr: 0x5555555593d0             <- c,  c user_data: 0x5555555593e0
Size: 0x20 (with flag bits: 0x21)

Top chunk | PREV_INUSE
Addr: 0x5555555593f0
Size: 0x20c10 (with flag bits: 0x20c11)

3次free----double_free

fastbin_double_free和fastbin-tcache的fd的差别
pwndbg > # 现在我们终于要来看fastbin下的 double free 了,先让我们
pwndbg > # 运行到第一个 free 这里  (  30 |  free(a);  )
pwndbg > b 30
pwndbg > c

pwndbg > # 我们先使用 bins 看一下所有的 bin
pwndbg> bins
tcachebins
0x20 [  7]:  0x555555559360 —▸ 0x555555559340 —▸ 0x555555559320 —▸ 0x5
55555559300 —▸ 0x5555555592e0 —▸ 0x5555555592c0 —▸ 0x5555555592a0 ◂— 0

fastbins
empty

unsortedbin
empty

smallbins
empty

largebins
empty

pwndbg > # 这里我们看见 fastbins 是空的(empty),而 tcache 的 0x20
pwndbg > # 大小的 bin 已经满了(tcache 每个大小的 bin 最多挂 7 个 chunk)。
pwndbg > # 所以,当我们继续 free 0x20 大小的 chunk 时,这个 chunk 只能进
pwndbg > # 入 fastbin。
pwndbg > # (tcache 没满的情况下,现代 glibc 永远先放入 tcache)

pwndbg > # 第一个 free (30| free(a);),然后看 fastbin
pwndbg > n
pwndbg > fastbins
fastbins
0x20:        0x555555559390 ◂— 0
pwndbg > # 咦?前面 p a 的时候明明显示的是 0x5555555593a0,这个 93a0
pwndbg > # 指向的是用户数据段的起始地址 mem。但是这里显示的 fastbin fd
pwndbg > # 指针指向 0x555555559390 = 0x5555555593a0 - 0x10
pwndbg > #######################################################
pwndbg > # fastbin 和 tcache 之间的 fd 指针是有区别的,tcache 的 fd #
pwndbg > # 指向链表尾部方向下一个 chunk 的 mem(user data地址),而   #
pwndbg > # fastbin 的 fd 指向下一个 chunk 的 header 地址           #
pwndbg > ########################################################

pwndbg > # 第二次 free (30| free(b);)
pwndbg> n
pwndbg> n
pwndbg> fastbins 
fastbins
0x20:        0x5555555593b0 —▸ 0x555555559390 ◂— 0

pwndbg > # 第三次 free (30| free(a);),再次 free(a),doubul free!
pwndbg> fastbins 
fastbins
0x20:        0x555555559390 —▸ 0x5555555593b0 ◂— 0x555555559390

pwndbg > # 这里我们发现,fastbin 里出现了两个相同的 chunk 地址,并且其
pwndbg > # fd 都指向同一个 chunk,构成一个 A->B->A 环。这就产生了
pwndbg > # double free

pwndbg > # 看一下 fastbin 中 chunk 的原始数据
pwndbg> x/12gx 0x555555559390
0x555555559390: 0x0000000000000000      0x0000000000000021  <- a
0x5555555593a0: 0x00005555555593b0      0x0000000000000000
0x5555555593b0: 0x0000000000000000      0x0000000000000021  <- b
0x5555555593c0: 0x0000555555559390      0x0000000000000000
0x5555555593d0: 0x0000000000000000      0x0000000000000021  <- 这里是前面没
0x5555555593e0: 0x0000000000000000      0x0000000000000000   - free 的 c哦
pwndbg > # 我们看见 chunk a 的 fd 指向 chunk b 的 header
pwndbg > #         chunk b 的 fd 指向 chunk a 的 header
pwndbg > # 值得一提的是,fastbin 里实际只有两个 chunk ,他们互相指向,
pwndbg > # 构成一个环,没有链尾。因为我们只 free(a) -> free(b) -> free(a),
pwndbg > # 而前面的 c 没有 free,0x5555555593d0 这里是还在使用的 c

从fastbin中calloc_chunk

pwndbg > # 接下来我们执行到这里
pwndbg > #     42 |  a = calloc(1, 8);
pwndbg > #     43 |  b = calloc(1, 8);
pwndbg > #     44 |  c = calloc(1, 8);
pwndbg > b 42
pwndbg > c



pwndbg > # 再给你瞟一眼 fastbin 
pwndbg > fastbin
fastbins
0x20:        0x555555559390 —▸ 0x5555555593b0 ◂— 0x555555559390



pwndbg > # a = calloc(1, 8)

pwndbg> n
pwndbg> p a
$11 = (int *) 0x5555555593a0

pwndbg> # chunk 被 calloc 清空了
pwndbg> x/4gx 0x5555555593a0-0x10
0x555555559390: 0x0000000000000000      0x0000000000000021
0x5555555593a0: 0x0000000000000000      0x0000000000000000

pwndbg> # fastbin 的一个 chunk 被使用了
pwndbg> fastbins
fastbins
0x20:        0x5555555593b0 —▸ 0x555555559390 ◂— 0



pwndbg > # b = calloc(1, 8)

pwndbg> n
pwndbg> p b
$12 = (int *) 0x5555555593c0
pwndbg> x/4gx 0x5555555593c0-0x10
0x5555555593b0: 0x0000000000000000      0x0000000000000021
0x5555555593c0: 0x0000000000000000      0x0000000000000000
pwndbg> fastbin
fastbins
0x20:        0x555555559390 ◂— 0



pwndbg > # c = calloc(1, 8)

pwndbg> # 查看 c 指针的值
pwndbg> p c
$14 = (int *) 0x5555555593a0

pwndbg> # fastbin 空了呢
pwndbg> fastbins 
fastbins
empty

pwndbg> # 查看 a 指针的值,发现 a == c,它们指向同一个 chunk
pwndbg> p a
$15 = (int *) 0x5555555593a0

a 和 c 中的地址一致,指向同一块 chunk,assert(a == c);通过


冰与火的战歌:Windows内核攻防实战高级班!从零到实战,融合AI与Windows内核攻防全技术栈,打造具备自动化能力的内核开发高手。

收藏
点赞 ・0
打赏
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回