学逆向论坛

找回密码
立即注册

只需一步,快速开始

发新帖

2万

积分

41

好友

1157

主题
发表于 2020-9-2 09:50:59 | 查看: 1689| 回复: 0

相关题目:

源码
#include 
  #include 
  #include 
  int main() {
  void* p1 = malloc(0x40);
  void* p2 = malloc(0x40);
  fprintf(stderr, "Allocated two fastbins: p1=%p p2=%p\n", p1, p2);
  fprintf(stderr, "Now free p1!\n");
  free(p1);
  void* p3 = malloc(0x400);
  fprintf(stderr, "Allocated large bin to trigger malloc_consolidate(): p3=%p\n", p3);
  fprintf(stderr, "In malloc_consolidate(), p1 is moved to the unsorted bin.\n");
  free(p1);
  fprintf(stderr, "Trigger the double free vulnerability!\n");
  fprintf(stderr, "We can pass the check in malloc() since p1 is not fast top.\n");
  fprintf(stderr, "Now p1 is in unsorted bin and fast bin. So we'will get it twice: %p %p\n", malloc(0x40), malloc(0x40));
  }

运行结果

fastbin_dup_consolidate

fastbin_dup_consolidate
分析&调试
流程就是先malloc(0x40)*2,之后free掉一个
然后malloc(0x400)
由于这个很大,fastbin中的chunk不适合,就会触发malloc_consolidate
合并fastbin中的chunk之后放到unsorted bin中,之后根据大小再放到small bin、large bin里
那么刚刚free的chunk就到了small bins中
那么再次free同样的指针,那么这同一块chunk就到了fast bin中,同时存在两个不同的bin中了
然后两次malloc,就分别取出了fast bin、small bin中的同一块chunk
调试:
malloc两次之后:

fastbin_dup_consolidate

fastbin_dup_consolidate
free一次后
加到了fast bin中:

fastbin_dup_consolidate

fastbin_dup_consolidate
之后malloc一个大一点的数:
可以发现chunk从fast  bin变成了small bin中,并且fd、bk都被修改了(指向头节点)

fastbin_dup_consolidate

fastbin_dup_consolidate
再次free p1:
又加入了fast bin,现在存在于两个bin中了!
同时fd指针也被修改了

fastbin_dup_consolidate

fastbin_dup_consolidate
之后两次malloc就会得到相同的chunk了
总结
  • 申请一个较大的chunk时,会触发malloc_consolidate,会合并fast bin中的chunk放到unsorted bin中,之后还会根据大小分类放到small bins、large bins中
  • chunk链入相应的bins中时,会自动更改一些指针的值。
  • 放在不同的bins中时连续两次free检测不到


温馨提示:
1.如果您喜欢这篇帖子,请给作者点赞评分,点赞会增加帖子的热度,评分会给作者加学币。(评分不会扣掉您的积分,系统每天都会重置您的评分额度)。
2.回复帖子不仅是对作者的认可,还可以获得学币奖励,请尊重他人的劳动成果,拒绝做伸手党!
3.发广告、灌水回复等违规行为一经发现直接禁言,如果本帖内容涉嫌违规,请点击论坛底部的举报反馈按钮,也可以在【投诉建议】板块发帖举报。
论坛交流群:672619046

小黑屋|手机版|站务邮箱|学逆向论坛 ( 粤ICP备2021023307号 )|网站地图

GMT+8, 2024-4-16 22:46 , Processed in 0.093765 second(s), 38 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表