而我们一般所指的 Use After Free 漏洞主要是后两种。此外,我们一般称被释放后没有被设置为NULL的内存指针为dangling pointer。
这里给出一个简单的例子
#include <stdio.h>#include <stdlib.h>typedefstructname{char*myname;void(*func)(char*str);}NAME;voidmyprint(char*str){printf("%s\n", str);}voidprintmyname(){printf("call print my name\n");}intmain(){ NAME *a; a =(NAME *)malloc(sizeof(struct name));a->func= myprint;a->myname="I can also use it";a->func("this is my function");// free without modifyfree(a);a->func("I can also use it");// free with modifya->func= printmyname;a->func("this is my function");// set NULL a =NULL;printf("this pogram will crash...\n");a->func("can not be printed...");}
delete_note 会根据给定的索引来释放对应的note。但是值得注意的是,在 删除的时候,只是单纯进行了free,而没有设置为NULL,那么显然,这里是存在Use After Free的情况的。
利用分析
我们可以看到 Use After Free 的情况确实可能会发生,那么怎么可以让它发生并且进行利用呢?需要同时注意的是,这个程序中还有一个magic函数,我们有没有可能来通过use after free 来使得这个程序执行magic函数呢?一个很直接的想法是修改note的printnote字段为magic函数的地址,从而实现在执行printnote 的时候执行magic函数。 那么该怎么执行呢?
➜ use_after_free git:(use_after_free) ✗ ./use_after_free
this is my function
I can also use it
call print my name
this pogram will crash...
[1] 38738 segmentation fault (core dumped) ./use_after_free