Skip to content

Commit 8a66ae8

Browse files
committed
always use non-flat map in secure mode and validate pointers passed to free
1 parent 09c2b7f commit 8a66ae8

File tree

5 files changed

+13
-3
lines changed

5 files changed

+13
-3
lines changed

include/mimalloc/bits.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,17 @@ typedef int32_t mi_ssize_t;
123123

124124
// use a flat page-map (or a 2-level one)
125125
#ifndef MI_PAGE_MAP_FLAT
126-
#if MI_MAX_VABITS <= 40 && !defined(__APPLE__)
126+
#if MI_MAX_VABITS <= 40 && !MI_SECURE && !defined(__APPLE__)
127127
#define MI_PAGE_MAP_FLAT 1
128128
#else
129129
#define MI_PAGE_MAP_FLAT 0
130130
#endif
131131
#endif
132132

133+
#if MI_PAGE_MAP_FLAT && MI_SECURE
134+
#error should not use MI_PAGE_MAP_FLAT with a secure build
135+
#endif
136+
133137

134138
/* --------------------------------------------------------------------------------
135139
Builtin's

include/mimalloc/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ static inline mi_page_t* _mi_checked_ptr_page(const void* p) {
589589

590590
static inline mi_page_t* _mi_ptr_page(const void* p) {
591591
mi_assert_internal(p==NULL || mi_is_in_heap_region(p));
592-
#if MI_DEBUG || defined(__APPLE__)
592+
#if MI_DEBUG || MI_SECURE || defined(__APPLE__)
593593
return _mi_checked_ptr_page(p);
594594
#else
595595
return _mi_unchecked_ptr_page(p);

include/mimalloc/types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ terms of the MIT license. A copy of the license can be found in the file
5050

5151
// Define MI_SECURE to enable security mitigations. Level 1 has minimal performance impact,
5252
// but protects most metadata with guard pages:
53-
// #define MI_SECURE 1 // guard page around metadata
53+
// #define MI_SECURE 1 // guard page around metadata; check pointer validity on free
5454
//
5555
// Level 2 has more performance impact but protect well against various buffer overflows
5656
// by surrounding all mimalloc pages with guard pages:

src/page-map.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,12 @@ bool _mi_page_map_init(void) {
202202
const size_t page_map_size = _mi_align_up( mi_page_map_count * sizeof(mi_page_t**), os_page_size);
203203
const size_t submap_size = MI_PAGE_MAP_SUB_SIZE;
204204
const size_t reserve_size = page_map_size + submap_size;
205+
#if MI_SECURE
206+
const bool commit = true; // the whole page map is valid and we can reliably check any pointer
207+
#else
205208
const bool commit = page_map_size <= 64*MI_KiB ||
206209
mi_option_is_enabled(mi_option_pagemap_commit) || _mi_os_has_overcommit();
210+
#endif
207211
_mi_page_map = (_Atomic(mi_page_t**)*)_mi_os_alloc_aligned(reserve_size, 1, commit, true /* allow large */, &mi_page_map_memid);
208212
if (_mi_page_map==NULL) {
209213
_mi_error_message(ENOMEM, "unable to reserve virtual memory for the page map (%zu KiB)\n", page_map_size / MI_KiB);

test/test-api.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ int main(void) {
8989
CHECK_BODY("malloc-free-null") {
9090
mi_free(NULL);
9191
};
92+
#if MI_INTPTR_BITS >= 64
9293
CHECK_BODY("malloc-free-invalid-low") {
9394
mi_free((void*)(MI_ZU(0x0000000003990080))); // issue #1087
9495
};
96+
#endif
9597
CHECK_BODY("calloc-overflow") {
9698
// use (size_t)&mi_calloc to get some number without triggering compiler warnings
9799
result = (mi_calloc((size_t)&mi_calloc,SIZE_MAX/1000) == NULL);

0 commit comments

Comments
 (0)