roger 发表于 2020-9-2 09:49:52

firstbin_dup

源码#include
  #include
  int main()
  {
  fprintf(stderr, "This file demonstrates a simple double-free attack with fastbins.\n");
  fprintf(stderr, "Allocating 3 buffers.\n");
  int *a = malloc(8);
  int *b = malloc(8);
  int *c = malloc(8);
  fprintf(stderr, "1st malloc(8): %p\n", a);
  fprintf(stderr, "2nd malloc(8): %p\n", b);
  fprintf(stderr, "3rd malloc(8): %p\n", c);
  fprintf(stderr, "Freeing the first one...\n");
  free(a);
  fprintf(stderr, "If we free %p again, things will crash because %p is at the top of the free list.\n", a, a);
  // free(a);
  fprintf(stderr, "So, instead, we'll free %p.\n", b);
  free(b);
  fprintf(stderr, "Now, we can free %p again, since it's not the head of the free list.\n", a);
  free(a);
  fprintf(stderr, "Now the free list has [ %p, %p, %p ]. If we malloc 3 times, we'll get %p twice!\n", a, b, a, a);
  fprintf(stderr, "1st malloc(8): %p\n", malloc(8));
  fprintf(stderr, "2nd malloc(8): %p\n", malloc(8));
  fprintf(stderr, "3rd malloc(8): %p\n", malloc(8));
  }
  
输出结果分析&调试流程很简单,malloc(8)分别给a,b,c然后free(a)->free(b)->free(a)(若是直接free(a)*2会检测到并报错)之后再malloc就会得到a、b、a chunk两次malloc的指针指向了同一块chunk调试:  三次malloc之后:
第一次free:可以看到free的chunk被链入了fastbin中,但是prev_size低位还是1(防止被合并)第二次free:可以看到第二次free的chunk从头进入了fastbin,fd指针指向了第一个chunk第三次free:可以看到a的chunk又一次从头部链入fastbin,并将其fd改成了chunk2的地址之后三次malloc就依次从链表头取chunk(先进后出),第一次和第三次取出的chunk一样总结
[*]依旧需要防止和top chunk合并
[*]fastbin的use位一直是1
[*]fastbin插入从头部插入
[*]fastbin是先进后出的

页: [1]
查看完整版本: firstbin_dup