2019-07-22 11:36:16 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2019-10-19 04:11:04 +03:00
|
|
|
#include <stdint.h>
|
2019-07-22 11:36:16 +03:00
|
|
|
|
|
|
|
#include <mimalloc.h>
|
|
|
|
#include <mimalloc-override.h> // redefines malloc etc.
|
|
|
|
|
2019-10-19 18:34:18 +03:00
|
|
|
static void double_free1();
|
|
|
|
static void double_free2();
|
2019-10-29 01:54:33 +03:00
|
|
|
static void corrupt_free();
|
2020-01-30 04:10:57 +03:00
|
|
|
static void block_overflow1();
|
2020-07-21 00:33:03 +03:00
|
|
|
static void invalid_free();
|
2019-10-19 04:11:04 +03:00
|
|
|
|
2019-07-22 11:36:16 +03:00
|
|
|
int main() {
|
|
|
|
mi_version();
|
2020-01-15 21:53:54 +03:00
|
|
|
|
2019-10-29 01:54:33 +03:00
|
|
|
// detect double frees and heap corruption
|
2019-11-22 02:21:23 +03:00
|
|
|
// double_free1();
|
|
|
|
// double_free2();
|
|
|
|
// corrupt_free();
|
2020-07-21 00:33:03 +03:00
|
|
|
// block_overflow1();
|
|
|
|
invalid_free();
|
2019-10-29 01:54:33 +03:00
|
|
|
|
2019-07-22 11:36:16 +03:00
|
|
|
void* p1 = malloc(78);
|
|
|
|
void* p2 = malloc(24);
|
|
|
|
free(p1);
|
2020-02-02 04:29:30 +03:00
|
|
|
p1 = mi_malloc(8);
|
2019-07-22 11:36:16 +03:00
|
|
|
//char* s = strdup("hello\n");
|
|
|
|
free(p2);
|
|
|
|
p2 = malloc(16);
|
|
|
|
p1 = realloc(p1, 32);
|
|
|
|
free(p1);
|
|
|
|
free(p2);
|
|
|
|
//free(s);
|
|
|
|
//mi_collect(true);
|
|
|
|
|
|
|
|
/* now test if override worked by allocating/freeing across the api's*/
|
|
|
|
//p1 = mi_malloc(32);
|
|
|
|
//free(p1);
|
|
|
|
//p2 = malloc(32);
|
|
|
|
//mi_free(p2);
|
|
|
|
mi_stats_print(NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-10-19 04:11:04 +03:00
|
|
|
|
2020-07-21 00:33:03 +03:00
|
|
|
static void invalid_free() {
|
|
|
|
free((void*)0xBADBEEF);
|
|
|
|
realloc((void*)0xBADBEEF,10);
|
|
|
|
}
|
|
|
|
|
2020-01-30 04:10:57 +03:00
|
|
|
static void block_overflow1() {
|
2020-02-01 10:39:51 +03:00
|
|
|
uint8_t* p = (uint8_t*)mi_malloc(17);
|
|
|
|
p[18] = 0;
|
2020-01-30 04:10:57 +03:00
|
|
|
free(p);
|
|
|
|
}
|
2019-10-29 01:54:33 +03:00
|
|
|
|
|
|
|
// The double free samples come ArcHeap [1] by Insu Yun (issue #161)
|
|
|
|
// [1]: https://arxiv.org/pdf/1903.00503.pdf
|
|
|
|
|
2019-10-19 18:34:18 +03:00
|
|
|
static void double_free1() {
|
2019-10-19 04:11:04 +03:00
|
|
|
void* p[256];
|
2019-10-29 01:54:33 +03:00
|
|
|
//uintptr_t buf[256];
|
2019-10-19 04:11:04 +03:00
|
|
|
|
|
|
|
p[0] = mi_malloc(622616);
|
|
|
|
p[1] = mi_malloc(655362);
|
|
|
|
p[2] = mi_malloc(786432);
|
|
|
|
mi_free(p[2]);
|
|
|
|
// [VULN] Double free
|
|
|
|
mi_free(p[2]);
|
|
|
|
p[3] = mi_malloc(786456);
|
|
|
|
// [BUG] Found overlap
|
|
|
|
// p[3]=0x429b2ea2000 (size=917504), p[1]=0x429b2e42000 (size=786432)
|
|
|
|
fprintf(stderr, "p3: %p-%p, p1: %p-%p, p2: %p\n", p[3], (uint8_t*)(p[3]) + 786456, p[1], (uint8_t*)(p[1]) + 655362, p[2]);
|
|
|
|
}
|
2019-10-19 18:34:18 +03:00
|
|
|
|
|
|
|
static void double_free2() {
|
|
|
|
void* p[256];
|
2019-10-29 01:54:33 +03:00
|
|
|
//uintptr_t buf[256];
|
2019-10-19 18:34:18 +03:00
|
|
|
// [INFO] Command buffer: 0x327b2000
|
|
|
|
// [INFO] Input size: 182
|
|
|
|
p[0] = malloc(712352);
|
|
|
|
p[1] = malloc(786432);
|
|
|
|
free(p[0]);
|
|
|
|
// [VULN] Double free
|
|
|
|
free(p[0]);
|
|
|
|
p[2] = malloc(786440);
|
|
|
|
p[3] = malloc(917504);
|
|
|
|
p[4] = malloc(786440);
|
|
|
|
// [BUG] Found overlap
|
|
|
|
// p[4]=0x433f1402000 (size=917504), p[1]=0x433f14c2000 (size=786432)
|
|
|
|
fprintf(stderr, "p1: %p-%p, p2: %p-%p\n", p[4], (uint8_t*)(p[4]) + 917504, p[1], (uint8_t*)(p[1]) + 786432);
|
|
|
|
}
|
2019-10-29 01:54:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
// Try to corrupt the heap through buffer overflow
|
|
|
|
#define N 256
|
|
|
|
#define SZ 64
|
|
|
|
|
|
|
|
static void corrupt_free() {
|
|
|
|
void* p[N];
|
|
|
|
// allocate
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
p[i] = malloc(SZ);
|
|
|
|
}
|
|
|
|
// free some
|
|
|
|
for (int i = 0; i < N; i += (N/10)) {
|
|
|
|
free(p[i]);
|
|
|
|
p[i] = NULL;
|
|
|
|
}
|
|
|
|
// try to corrupt the free list
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
if (p[i] != NULL) {
|
|
|
|
memset(p[i], 0, SZ+8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// allocate more.. trying to trigger an allocation from a corrupted entry
|
|
|
|
// this may need many allocations to get there (if at all)
|
|
|
|
for (int i = 0; i < 4096; i++) {
|
|
|
|
malloc(SZ);
|
|
|
|
}
|
2020-01-15 21:53:54 +03:00
|
|
|
}
|