内存管理
一、漏洞案例
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 32
int main(void) {
char* buf1;
char* buf2;
buf1 = (char*)malloc(SIZE);
printf("buf2: 0x%p\n\n", buf1);
free(buf1);
// 分配buf2 去 占坑buf1 的内存地址
buf2 = (char*)malloc(SIZE);
printf("buf2: 0x%p\n\n", buf2);
memset(buf2, 0, SIZE);
printf("buf2: %d\n", buf2);
// 重复引用已释放的buf指针,但却导致buf2值被篡改
printf("===== Use After Free =====\n");
strncpy(buf1, "hack", 5);
printf("buf2:%s\n\n", buf2);
free(buf2);
}
输出
buf2: 0x0x16f6010
buf2: 0x0x16f6010
buf2: 24076304
===== Use After Free =====
buf2:hack