]>
Commit | Line | Data |
---|---|---|
4f22f405 | 1 | /* |
da1c088f | 2 | * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved. |
624265c6 | 3 | * Copyright 2004-2014, Akamai Technologies. All Rights Reserved. |
4f22f405 | 4 | * |
0e9725bc | 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
4f22f405 RS |
6 | * this file except in compliance with the License. You can obtain a copy |
7 | * in the file LICENSE in the source distribution or at | |
8 | * https://www.openssl.org/source/license.html | |
9 | */ | |
10 | ||
74924dcb RS |
11 | /* |
12 | * This file is in two halves. The first half implements the public API | |
13 | * to be used by external consumers, and to be used by OpenSSL to store | |
14 | * data in a "secure arena." The second half implements the secure arena. | |
15 | * For details on that implementation, see below (look for uppercase | |
16 | * "SECURE HEAP IMPLEMENTATION"). | |
17 | */ | |
d5f9166b | 18 | #include "internal/e_os.h" |
07016a8a | 19 | #include <openssl/crypto.h> |
9167a47f | 20 | #include <openssl/err.h> |
74924dcb | 21 | |
183733f8 RL |
22 | #include <string.h> |
23 | ||
6943335e | 24 | #ifndef OPENSSL_NO_SECURE_MEMORY |
f31ac320 JG |
25 | # if defined(_WIN32) |
26 | # include <windows.h> | |
a509b97d TM |
27 | # if defined(WINAPI_FAMILY_PARTITION) |
28 | # if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM) | |
ff1efe6e CM |
29 | /* |
30 | * While VirtualLock is available under the app partition (e.g. UWP), | |
31 | * the headers do not define the API. Define it ourselves instead. | |
32 | */ | |
33 | WINBASEAPI | |
34 | BOOL | |
35 | WINAPI | |
36 | VirtualLock( | |
37 | _In_ LPVOID lpAddress, | |
38 | _In_ SIZE_T dwSize | |
39 | ); | |
a509b97d | 40 | # endif |
ff1efe6e | 41 | # endif |
f31ac320 | 42 | # endif |
d4dfb0ba | 43 | # include <stdlib.h> |
d4dfb0ba | 44 | # include <assert.h> |
f31ac320 JG |
45 | # if defined(OPENSSL_SYS_UNIX) |
46 | # include <unistd.h> | |
47 | # endif | |
27186da7 | 48 | # include <sys/types.h> |
f31ac320 JG |
49 | # if defined(OPENSSL_SYS_UNIX) |
50 | # include <sys/mman.h> | |
27db6118 DC |
51 | # if defined(__FreeBSD__) |
52 | # define MADV_DONTDUMP MADV_NOCORE | |
53 | # endif | |
8d4b5260 DC |
54 | # if !defined(MAP_CONCEAL) |
55 | # define MAP_CONCEAL 0 | |
56 | # endif | |
f31ac320 | 57 | # endif |
9dfc5b96 TS |
58 | # if defined(OPENSSL_SYS_LINUX) |
59 | # include <sys/syscall.h> | |
f1c00b93 AP |
60 | # if defined(SYS_mlock2) |
61 | # include <linux/mman.h> | |
62 | # include <errno.h> | |
63 | # endif | |
61783db5 | 64 | # include <sys/param.h> |
9dfc5b96 | 65 | # endif |
27186da7 AP |
66 | # include <sys/stat.h> |
67 | # include <fcntl.h> | |
74924dcb | 68 | #endif |
78634e8a JK |
69 | #ifndef HAVE_MADVISE |
70 | # if defined(MADV_DONTDUMP) | |
71 | # define HAVE_MADVISE 1 | |
72 | # else | |
73 | # define HAVE_MADVISE 0 | |
74 | # endif | |
75 | #endif | |
76 | #if HAVE_MADVISE | |
77 | # undef NO_MADVISE | |
78 | #else | |
79 | # define NO_MADVISE | |
80 | #endif | |
74924dcb | 81 | |
74924dcb | 82 | #define CLEAR(p, s) OPENSSL_cleanse(p, s) |
34750dc2 BL |
83 | #ifndef PAGE_SIZE |
84 | # define PAGE_SIZE 4096 | |
85 | #endif | |
014cc4b2 AP |
86 | #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS) |
87 | # define MAP_ANON MAP_ANONYMOUS | |
88 | #endif | |
74924dcb | 89 | |
6943335e | 90 | #ifndef OPENSSL_NO_SECURE_MEMORY |
df2ee0e2 | 91 | static size_t secure_mem_used; |
74924dcb RS |
92 | |
93 | static int secure_mem_initialized; | |
74924dcb | 94 | |
9471f776 MC |
95 | static CRYPTO_RWLOCK *sec_malloc_lock = NULL; |
96 | ||
74924dcb RS |
97 | /* |
98 | * These are the functions that must be implemented by a secure heap (sh). | |
99 | */ | |
34b16762 | 100 | static int sh_init(size_t size, size_t minsize); |
332dc4fa RS |
101 | static void *sh_malloc(size_t size); |
102 | static void sh_free(void *ptr); | |
74924dcb | 103 | static void sh_done(void); |
e8408681 | 104 | static size_t sh_actual_size(char *ptr); |
74924dcb RS |
105 | static int sh_allocated(const char *ptr); |
106 | #endif | |
107 | ||
34b16762 | 108 | int CRYPTO_secure_malloc_init(size_t size, size_t minsize) |
74924dcb | 109 | { |
6943335e | 110 | #ifndef OPENSSL_NO_SECURE_MEMORY |
74924dcb RS |
111 | int ret = 0; |
112 | ||
74924dcb | 113 | if (!secure_mem_initialized) { |
63ab5ea1 | 114 | sec_malloc_lock = CRYPTO_THREAD_lock_new(); |
9471f776 MC |
115 | if (sec_malloc_lock == NULL) |
116 | return 0; | |
7031ddac TS |
117 | if ((ret = sh_init(size, minsize)) != 0) { |
118 | secure_mem_initialized = 1; | |
119 | } else { | |
120 | CRYPTO_THREAD_lock_free(sec_malloc_lock); | |
121 | sec_malloc_lock = NULL; | |
122 | } | |
74924dcb | 123 | } |
9471f776 | 124 | |
74924dcb RS |
125 | return ret; |
126 | #else | |
127 | return 0; | |
6943335e | 128 | #endif /* OPENSSL_NO_SECURE_MEMORY */ |
74924dcb RS |
129 | } |
130 | ||
3cb7c5cf | 131 | int CRYPTO_secure_malloc_done(void) |
74924dcb | 132 | { |
6943335e | 133 | #ifndef OPENSSL_NO_SECURE_MEMORY |
e8408681 TS |
134 | if (secure_mem_used == 0) { |
135 | sh_done(); | |
136 | secure_mem_initialized = 0; | |
137 | CRYPTO_THREAD_lock_free(sec_malloc_lock); | |
7031ddac | 138 | sec_malloc_lock = NULL; |
e8408681 TS |
139 | return 1; |
140 | } | |
6943335e | 141 | #endif /* OPENSSL_NO_SECURE_MEMORY */ |
e8408681 | 142 | return 0; |
74924dcb RS |
143 | } |
144 | ||
3cb7c5cf | 145 | int CRYPTO_secure_malloc_initialized(void) |
74924dcb | 146 | { |
6943335e | 147 | #ifndef OPENSSL_NO_SECURE_MEMORY |
74924dcb RS |
148 | return secure_mem_initialized; |
149 | #else | |
150 | return 0; | |
6943335e | 151 | #endif /* OPENSSL_NO_SECURE_MEMORY */ |
74924dcb RS |
152 | } |
153 | ||
ff842856 | 154 | void *CRYPTO_secure_malloc(size_t num, const char *file, int line) |
74924dcb | 155 | { |
6943335e | 156 | #ifndef OPENSSL_NO_SECURE_MEMORY |
9167a47f | 157 | void *ret = NULL; |
74924dcb | 158 | size_t actual_size; |
9167a47f | 159 | int reason = CRYPTO_R_SECURE_MALLOC_FAILURE; |
74924dcb RS |
160 | |
161 | if (!secure_mem_initialized) { | |
74924dcb RS |
162 | return CRYPTO_malloc(num, file, line); |
163 | } | |
9167a47f RL |
164 | if (!CRYPTO_THREAD_write_lock(sec_malloc_lock)) { |
165 | reason = ERR_R_CRYPTO_LIB; | |
166 | goto err; | |
167 | } | |
74924dcb RS |
168 | ret = sh_malloc(num); |
169 | actual_size = ret ? sh_actual_size(ret) : 0; | |
170 | secure_mem_used += actual_size; | |
9471f776 | 171 | CRYPTO_THREAD_unlock(sec_malloc_lock); |
9167a47f RL |
172 | err: |
173 | if (ret == NULL && (file != NULL || line != 0)) { | |
174 | ERR_new(); | |
175 | ERR_set_debug(file, line, NULL); | |
176 | ERR_set_error(ERR_LIB_CRYPTO, reason, NULL); | |
177 | } | |
74924dcb RS |
178 | return ret; |
179 | #else | |
180 | return CRYPTO_malloc(num, file, line); | |
6943335e | 181 | #endif /* OPENSSL_NO_SECURE_MEMORY */ |
74924dcb RS |
182 | } |
183 | ||
3538c7da RS |
184 | void *CRYPTO_secure_zalloc(size_t num, const char *file, int line) |
185 | { | |
6943335e | 186 | #ifndef OPENSSL_NO_SECURE_MEMORY |
3b8e97ab P |
187 | if (secure_mem_initialized) |
188 | /* CRYPTO_secure_malloc() zeroes allocations when it is implemented */ | |
189 | return CRYPTO_secure_malloc(num, file, line); | |
190 | #endif | |
191 | return CRYPTO_zalloc(num, file, line); | |
3538c7da RS |
192 | } |
193 | ||
05c7b163 | 194 | void CRYPTO_secure_free(void *ptr, const char *file, int line) |
74924dcb | 195 | { |
6943335e | 196 | #ifndef OPENSSL_NO_SECURE_MEMORY |
74924dcb RS |
197 | size_t actual_size; |
198 | ||
199 | if (ptr == NULL) | |
200 | return; | |
e8408681 | 201 | if (!CRYPTO_secure_allocated(ptr)) { |
05c7b163 | 202 | CRYPTO_free(ptr, file, line); |
74924dcb RS |
203 | return; |
204 | } | |
cd3f8c1b RS |
205 | if (!CRYPTO_THREAD_write_lock(sec_malloc_lock)) |
206 | return; | |
74924dcb RS |
207 | actual_size = sh_actual_size(ptr); |
208 | CLEAR(ptr, actual_size); | |
209 | secure_mem_used -= actual_size; | |
210 | sh_free(ptr); | |
9471f776 | 211 | CRYPTO_THREAD_unlock(sec_malloc_lock); |
74924dcb | 212 | #else |
6a78ae28 | 213 | CRYPTO_free(ptr, file, line); |
6943335e | 214 | #endif /* OPENSSL_NO_SECURE_MEMORY */ |
74924dcb RS |
215 | } |
216 | ||
4dae7cd3 BE |
217 | void CRYPTO_secure_clear_free(void *ptr, size_t num, |
218 | const char *file, int line) | |
219 | { | |
6943335e | 220 | #ifndef OPENSSL_NO_SECURE_MEMORY |
4dae7cd3 BE |
221 | size_t actual_size; |
222 | ||
223 | if (ptr == NULL) | |
224 | return; | |
225 | if (!CRYPTO_secure_allocated(ptr)) { | |
226 | OPENSSL_cleanse(ptr, num); | |
227 | CRYPTO_free(ptr, file, line); | |
228 | return; | |
229 | } | |
cd3f8c1b RS |
230 | if (!CRYPTO_THREAD_write_lock(sec_malloc_lock)) |
231 | return; | |
4dae7cd3 BE |
232 | actual_size = sh_actual_size(ptr); |
233 | CLEAR(ptr, actual_size); | |
234 | secure_mem_used -= actual_size; | |
235 | sh_free(ptr); | |
236 | CRYPTO_THREAD_unlock(sec_malloc_lock); | |
237 | #else | |
238 | if (ptr == NULL) | |
239 | return; | |
240 | OPENSSL_cleanse(ptr, num); | |
241 | CRYPTO_free(ptr, file, line); | |
6943335e | 242 | #endif /* OPENSSL_NO_SECURE_MEMORY */ |
4dae7cd3 BE |
243 | } |
244 | ||
74924dcb RS |
245 | int CRYPTO_secure_allocated(const void *ptr) |
246 | { | |
6943335e | 247 | #ifndef OPENSSL_NO_SECURE_MEMORY |
74924dcb RS |
248 | if (!secure_mem_initialized) |
249 | return 0; | |
44e82b81 P |
250 | /* |
251 | * Only read accesses to the arena take place in sh_allocated() and this | |
252 | * is only changed by the sh_init() and sh_done() calls which are not | |
253 | * locked. Hence, it is safe to make this check without a lock too. | |
254 | */ | |
255 | return sh_allocated(ptr); | |
74924dcb RS |
256 | #else |
257 | return 0; | |
6943335e | 258 | #endif /* OPENSSL_NO_SECURE_MEMORY */ |
74924dcb RS |
259 | } |
260 | ||
3cb7c5cf | 261 | size_t CRYPTO_secure_used(void) |
bbd86bf5 | 262 | { |
6943335e | 263 | #ifndef OPENSSL_NO_SECURE_MEMORY |
bbd86bf5 RS |
264 | return secure_mem_used; |
265 | #else | |
266 | return 0; | |
6943335e | 267 | #endif /* OPENSSL_NO_SECURE_MEMORY */ |
bbd86bf5 RS |
268 | } |
269 | ||
d594199b RS |
270 | size_t CRYPTO_secure_actual_size(void *ptr) |
271 | { | |
6943335e | 272 | #ifndef OPENSSL_NO_SECURE_MEMORY |
d594199b RS |
273 | size_t actual_size; |
274 | ||
cd3f8c1b RS |
275 | if (!CRYPTO_THREAD_write_lock(sec_malloc_lock)) |
276 | return 0; | |
d594199b | 277 | actual_size = sh_actual_size(ptr); |
9471f776 | 278 | CRYPTO_THREAD_unlock(sec_malloc_lock); |
d594199b RS |
279 | return actual_size; |
280 | #else | |
281 | return 0; | |
282 | #endif | |
283 | } | |
74924dcb RS |
284 | |
285 | /* | |
286 | * SECURE HEAP IMPLEMENTATION | |
287 | */ | |
6943335e | 288 | #ifndef OPENSSL_NO_SECURE_MEMORY |
74924dcb RS |
289 | |
290 | ||
291 | /* | |
292 | * The implementation provided here uses a fixed-sized mmap() heap, | |
293 | * which is locked into memory, not written to core files, and protected | |
294 | * on either side by an unmapped page, which will catch pointer overruns | |
295 | * (or underruns) and an attempt to read data out of the secure heap. | |
296 | * Free'd memory is zero'd or otherwise cleansed. | |
297 | * | |
298 | * This is a pretty standard buddy allocator. We keep areas in a multiple | |
299 | * of "sh.minsize" units. The freelist and bitmaps are kept separately, | |
300 | * so all (and only) data is kept in the mmap'd heap. | |
301 | * | |
302 | * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the | |
303 | * place. | |
304 | */ | |
305 | ||
e8408681 TS |
306 | #define ONE ((size_t)1) |
307 | ||
308 | # define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7))) | |
309 | # define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7))) | |
310 | # define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7)))) | |
74924dcb RS |
311 | |
312 | #define WITHIN_ARENA(p) \ | |
313 | ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size]) | |
314 | #define WITHIN_FREELIST(p) \ | |
315 | ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size]) | |
316 | ||
317 | ||
318 | typedef struct sh_list_st | |
319 | { | |
320 | struct sh_list_st *next; | |
321 | struct sh_list_st **p_next; | |
322 | } SH_LIST; | |
323 | ||
324 | typedef struct sh_st | |
325 | { | |
326 | char* map_result; | |
327 | size_t map_size; | |
328 | char *arena; | |
e8408681 | 329 | size_t arena_size; |
74924dcb | 330 | char **freelist; |
e8408681 TS |
331 | ossl_ssize_t freelist_size; |
332 | size_t minsize; | |
74924dcb RS |
333 | unsigned char *bittable; |
334 | unsigned char *bitmalloc; | |
e8408681 | 335 | size_t bittable_size; /* size in bits */ |
74924dcb RS |
336 | } SH; |
337 | ||
338 | static SH sh; | |
339 | ||
e8408681 | 340 | static size_t sh_getlist(char *ptr) |
74924dcb | 341 | { |
e8408681 TS |
342 | ossl_ssize_t list = sh.freelist_size - 1; |
343 | size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize; | |
74924dcb RS |
344 | |
345 | for (; bit; bit >>= 1, list--) { | |
346 | if (TESTBIT(sh.bittable, bit)) | |
347 | break; | |
348 | OPENSSL_assert((bit & 1) == 0); | |
349 | } | |
350 | ||
351 | return list; | |
352 | } | |
353 | ||
354 | ||
355 | static int sh_testbit(char *ptr, int list, unsigned char *table) | |
356 | { | |
e8408681 | 357 | size_t bit; |
74924dcb RS |
358 | |
359 | OPENSSL_assert(list >= 0 && list < sh.freelist_size); | |
360 | OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0); | |
e8408681 | 361 | bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list)); |
74924dcb RS |
362 | OPENSSL_assert(bit > 0 && bit < sh.bittable_size); |
363 | return TESTBIT(table, bit); | |
364 | } | |
365 | ||
366 | static void sh_clearbit(char *ptr, int list, unsigned char *table) | |
367 | { | |
e8408681 | 368 | size_t bit; |
74924dcb RS |
369 | |
370 | OPENSSL_assert(list >= 0 && list < sh.freelist_size); | |
371 | OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0); | |
e8408681 | 372 | bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list)); |
74924dcb RS |
373 | OPENSSL_assert(bit > 0 && bit < sh.bittable_size); |
374 | OPENSSL_assert(TESTBIT(table, bit)); | |
375 | CLEARBIT(table, bit); | |
376 | } | |
377 | ||
378 | static void sh_setbit(char *ptr, int list, unsigned char *table) | |
379 | { | |
e8408681 | 380 | size_t bit; |
74924dcb RS |
381 | |
382 | OPENSSL_assert(list >= 0 && list < sh.freelist_size); | |
383 | OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0); | |
e8408681 | 384 | bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list)); |
74924dcb RS |
385 | OPENSSL_assert(bit > 0 && bit < sh.bittable_size); |
386 | OPENSSL_assert(!TESTBIT(table, bit)); | |
387 | SETBIT(table, bit); | |
388 | } | |
389 | ||
390 | static void sh_add_to_list(char **list, char *ptr) | |
391 | { | |
392 | SH_LIST *temp; | |
393 | ||
394 | OPENSSL_assert(WITHIN_FREELIST(list)); | |
395 | OPENSSL_assert(WITHIN_ARENA(ptr)); | |
396 | ||
397 | temp = (SH_LIST *)ptr; | |
398 | temp->next = *(SH_LIST **)list; | |
399 | OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next)); | |
400 | temp->p_next = (SH_LIST **)list; | |
401 | ||
402 | if (temp->next != NULL) { | |
403 | OPENSSL_assert((char **)temp->next->p_next == list); | |
404 | temp->next->p_next = &(temp->next); | |
405 | } | |
406 | ||
407 | *list = ptr; | |
408 | } | |
409 | ||
a773b52a | 410 | static void sh_remove_from_list(char *ptr) |
74924dcb RS |
411 | { |
412 | SH_LIST *temp, *temp2; | |
413 | ||
414 | temp = (SH_LIST *)ptr; | |
415 | if (temp->next != NULL) | |
416 | temp->next->p_next = temp->p_next; | |
417 | *temp->p_next = temp->next; | |
418 | if (temp->next == NULL) | |
419 | return; | |
420 | ||
421 | temp2 = temp->next; | |
422 | OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next)); | |
423 | } | |
424 | ||
425 | ||
34b16762 | 426 | static int sh_init(size_t size, size_t minsize) |
74924dcb | 427 | { |
7031ddac TS |
428 | int ret; |
429 | size_t i; | |
74924dcb RS |
430 | size_t pgsize; |
431 | size_t aligned; | |
f31ac320 JG |
432 | #if defined(_WIN32) |
433 | DWORD flOldProtect; | |
434 | SYSTEM_INFO systemInfo; | |
435 | #endif | |
74924dcb | 436 | |
cbe29648 | 437 | memset(&sh, 0, sizeof(sh)); |
74924dcb | 438 | |
a998ec0e | 439 | /* make sure size is a powers of 2 */ |
74924dcb RS |
440 | OPENSSL_assert(size > 0); |
441 | OPENSSL_assert((size & (size - 1)) == 0); | |
d27fd991 | 442 | if (size == 0 || (size & (size - 1)) != 0) |
74924dcb | 443 | goto err; |
74924dcb | 444 | |
a998ec0e P |
445 | if (minsize <= sizeof(SH_LIST)) { |
446 | OPENSSL_assert(sizeof(SH_LIST) <= 65536); | |
447 | /* | |
448 | * Compute the minimum possible allocation size. | |
449 | * This must be a power of 2 and at least as large as the SH_LIST | |
450 | * structure. | |
451 | */ | |
452 | minsize = sizeof(SH_LIST) - 1; | |
453 | minsize |= minsize >> 1; | |
454 | minsize |= minsize >> 2; | |
455 | if (sizeof(SH_LIST) > 16) | |
456 | minsize |= minsize >> 4; | |
457 | if (sizeof(SH_LIST) > 256) | |
458 | minsize |= minsize >> 8; | |
459 | minsize++; | |
460 | } else { | |
461 | /* make sure minsize is a powers of 2 */ | |
462 | OPENSSL_assert((minsize & (minsize - 1)) == 0); | |
463 | if ((minsize & (minsize - 1)) != 0) | |
464 | goto err; | |
465 | } | |
70e14ffb | 466 | |
74924dcb RS |
467 | sh.arena_size = size; |
468 | sh.minsize = minsize; | |
469 | sh.bittable_size = (sh.arena_size / sh.minsize) * 2; | |
470 | ||
7f07149d GV |
471 | /* Prevent allocations of size 0 later on */ |
472 | if (sh.bittable_size >> 3 == 0) | |
473 | goto err; | |
474 | ||
74924dcb RS |
475 | sh.freelist_size = -1; |
476 | for (i = sh.bittable_size; i; i >>= 1) | |
477 | sh.freelist_size++; | |
478 | ||
cbe29648 | 479 | sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *)); |
74924dcb RS |
480 | OPENSSL_assert(sh.freelist != NULL); |
481 | if (sh.freelist == NULL) | |
482 | goto err; | |
74924dcb | 483 | |
b51bce94 | 484 | sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3); |
74924dcb RS |
485 | OPENSSL_assert(sh.bittable != NULL); |
486 | if (sh.bittable == NULL) | |
487 | goto err; | |
74924dcb | 488 | |
b51bce94 | 489 | sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3); |
74924dcb RS |
490 | OPENSSL_assert(sh.bitmalloc != NULL); |
491 | if (sh.bitmalloc == NULL) | |
492 | goto err; | |
74924dcb RS |
493 | |
494 | /* Allocate space for heap, and two extra pages as guards */ | |
9ae720b4 MC |
495 | #if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE) |
496 | { | |
497 | # if defined(_SC_PAGE_SIZE) | |
498 | long tmppgsize = sysconf(_SC_PAGE_SIZE); | |
499 | # else | |
500 | long tmppgsize = sysconf(_SC_PAGESIZE); | |
501 | # endif | |
502 | if (tmppgsize < 1) | |
503 | pgsize = PAGE_SIZE; | |
504 | else | |
505 | pgsize = (size_t)tmppgsize; | |
506 | } | |
f31ac320 JG |
507 | #elif defined(_WIN32) |
508 | GetSystemInfo(&systemInfo); | |
509 | pgsize = (size_t)systemInfo.dwPageSize; | |
74924dcb RS |
510 | #else |
511 | pgsize = PAGE_SIZE; | |
512 | #endif | |
513 | sh.map_size = pgsize + sh.arena_size + pgsize; | |
d27fd991 | 514 | |
f31ac320 JG |
515 | #if !defined(_WIN32) |
516 | # ifdef MAP_ANON | |
d27fd991 | 517 | sh.map_result = mmap(NULL, sh.map_size, |
8d4b5260 | 518 | PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|MAP_CONCEAL, -1, 0); |
f31ac320 | 519 | # else |
d27fd991 | 520 | { |
27186da7 AP |
521 | int fd; |
522 | ||
523 | sh.map_result = MAP_FAILED; | |
524 | if ((fd = open("/dev/zero", O_RDWR)) >= 0) { | |
525 | sh.map_result = mmap(NULL, sh.map_size, | |
526 | PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); | |
527 | close(fd); | |
528 | } | |
529 | } | |
f31ac320 | 530 | # endif |
74924dcb RS |
531 | if (sh.map_result == MAP_FAILED) |
532 | goto err; | |
f31ac320 JG |
533 | #else |
534 | sh.map_result = VirtualAlloc(NULL, sh.map_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); | |
535 | ||
536 | if (sh.map_result == NULL) | |
537 | goto err; | |
538 | #endif | |
539 | ||
74924dcb RS |
540 | sh.arena = (char *)(sh.map_result + pgsize); |
541 | sh_setbit(sh.arena, 0, sh.bittable); | |
542 | sh_add_to_list(&sh.freelist[0], sh.arena); | |
543 | ||
544 | /* Now try to add guard pages and lock into memory. */ | |
545 | ret = 1; | |
546 | ||
f31ac320 | 547 | #if !defined(_WIN32) |
74924dcb RS |
548 | /* Starting guard is already aligned from mmap. */ |
549 | if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0) | |
550 | ret = 2; | |
f31ac320 JG |
551 | #else |
552 | if (VirtualProtect(sh.map_result, pgsize, PAGE_NOACCESS, &flOldProtect) == FALSE) | |
553 | ret = 2; | |
554 | #endif | |
74924dcb RS |
555 | |
556 | /* Ending guard page - need to round up to page boundary */ | |
557 | aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1); | |
f31ac320 | 558 | #if !defined(_WIN32) |
74924dcb RS |
559 | if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0) |
560 | ret = 2; | |
f31ac320 JG |
561 | #else |
562 | if (VirtualProtect(sh.map_result + aligned, pgsize, PAGE_NOACCESS, &flOldProtect) == FALSE) | |
563 | ret = 2; | |
564 | #endif | |
74924dcb | 565 | |
9dfc5b96 TS |
566 | #if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2) |
567 | if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) { | |
568 | if (errno == ENOSYS) { | |
569 | if (mlock(sh.arena, sh.arena_size) < 0) | |
570 | ret = 2; | |
571 | } else { | |
572 | ret = 2; | |
573 | } | |
574 | } | |
f31ac320 JG |
575 | #elif defined(_WIN32) |
576 | if (VirtualLock(sh.arena, sh.arena_size) == FALSE) | |
577 | ret = 2; | |
9dfc5b96 | 578 | #else |
74924dcb RS |
579 | if (mlock(sh.arena, sh.arena_size) < 0) |
580 | ret = 2; | |
9dfc5b96 | 581 | #endif |
78634e8a | 582 | #ifndef NO_MADVISE |
74924dcb RS |
583 | if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0) |
584 | ret = 2; | |
585 | #endif | |
586 | ||
587 | return ret; | |
588 | ||
589 | err: | |
590 | sh_done(); | |
591 | return 0; | |
592 | } | |
593 | ||
3cb7c5cf | 594 | static void sh_done(void) |
74924dcb RS |
595 | { |
596 | OPENSSL_free(sh.freelist); | |
597 | OPENSSL_free(sh.bittable); | |
598 | OPENSSL_free(sh.bitmalloc); | |
f31ac320 | 599 | #if !defined(_WIN32) |
1d78129d | 600 | if (sh.map_result != MAP_FAILED && sh.map_size) |
74924dcb | 601 | munmap(sh.map_result, sh.map_size); |
f31ac320 JG |
602 | #else |
603 | if (sh.map_result != NULL && sh.map_size) | |
604 | VirtualFree(sh.map_result, 0, MEM_RELEASE); | |
605 | #endif | |
cbe29648 | 606 | memset(&sh, 0, sizeof(sh)); |
74924dcb RS |
607 | } |
608 | ||
609 | static int sh_allocated(const char *ptr) | |
610 | { | |
611 | return WITHIN_ARENA(ptr) ? 1 : 0; | |
612 | } | |
613 | ||
614 | static char *sh_find_my_buddy(char *ptr, int list) | |
615 | { | |
e8408681 | 616 | size_t bit; |
74924dcb RS |
617 | char *chunk = NULL; |
618 | ||
e8408681 | 619 | bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list); |
74924dcb RS |
620 | bit ^= 1; |
621 | ||
622 | if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit)) | |
e8408681 | 623 | chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list)); |
74924dcb RS |
624 | |
625 | return chunk; | |
626 | } | |
627 | ||
332dc4fa | 628 | static void *sh_malloc(size_t size) |
74924dcb | 629 | { |
e8408681 | 630 | ossl_ssize_t list, slist; |
74924dcb RS |
631 | size_t i; |
632 | char *chunk; | |
633 | ||
7031ddac TS |
634 | if (size > sh.arena_size) |
635 | return NULL; | |
636 | ||
74924dcb RS |
637 | list = sh.freelist_size - 1; |
638 | for (i = sh.minsize; i < size; i <<= 1) | |
639 | list--; | |
640 | if (list < 0) | |
641 | return NULL; | |
642 | ||
643 | /* try to find a larger entry to split */ | |
644 | for (slist = list; slist >= 0; slist--) | |
645 | if (sh.freelist[slist] != NULL) | |
646 | break; | |
647 | if (slist < 0) | |
648 | return NULL; | |
649 | ||
650 | /* split larger entry */ | |
651 | while (slist != list) { | |
652 | char *temp = sh.freelist[slist]; | |
653 | ||
654 | /* remove from bigger list */ | |
655 | OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc)); | |
656 | sh_clearbit(temp, slist, sh.bittable); | |
a773b52a | 657 | sh_remove_from_list(temp); |
74924dcb RS |
658 | OPENSSL_assert(temp != sh.freelist[slist]); |
659 | ||
660 | /* done with bigger list */ | |
661 | slist++; | |
662 | ||
663 | /* add to smaller list */ | |
664 | OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc)); | |
665 | sh_setbit(temp, slist, sh.bittable); | |
666 | sh_add_to_list(&sh.freelist[slist], temp); | |
667 | OPENSSL_assert(sh.freelist[slist] == temp); | |
668 | ||
669 | /* split in 2 */ | |
670 | temp += sh.arena_size >> slist; | |
671 | OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc)); | |
672 | sh_setbit(temp, slist, sh.bittable); | |
673 | sh_add_to_list(&sh.freelist[slist], temp); | |
674 | OPENSSL_assert(sh.freelist[slist] == temp); | |
675 | ||
676 | OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist)); | |
677 | } | |
678 | ||
679 | /* peel off memory to hand back */ | |
680 | chunk = sh.freelist[list]; | |
681 | OPENSSL_assert(sh_testbit(chunk, list, sh.bittable)); | |
682 | sh_setbit(chunk, list, sh.bitmalloc); | |
a773b52a | 683 | sh_remove_from_list(chunk); |
74924dcb RS |
684 | |
685 | OPENSSL_assert(WITHIN_ARENA(chunk)); | |
686 | ||
3b8e97ab P |
687 | /* zero the free list header as a precaution against information leakage */ |
688 | memset(chunk, 0, sizeof(SH_LIST)); | |
689 | ||
74924dcb RS |
690 | return chunk; |
691 | } | |
692 | ||
332dc4fa | 693 | static void sh_free(void *ptr) |
74924dcb | 694 | { |
e8408681 | 695 | size_t list; |
332dc4fa | 696 | void *buddy; |
74924dcb RS |
697 | |
698 | if (ptr == NULL) | |
699 | return; | |
700 | OPENSSL_assert(WITHIN_ARENA(ptr)); | |
701 | if (!WITHIN_ARENA(ptr)) | |
702 | return; | |
703 | ||
704 | list = sh_getlist(ptr); | |
705 | OPENSSL_assert(sh_testbit(ptr, list, sh.bittable)); | |
706 | sh_clearbit(ptr, list, sh.bitmalloc); | |
707 | sh_add_to_list(&sh.freelist[list], ptr); | |
708 | ||
709 | /* Try to coalesce two adjacent free areas. */ | |
710 | while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) { | |
711 | OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list)); | |
712 | OPENSSL_assert(ptr != NULL); | |
713 | OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc)); | |
714 | sh_clearbit(ptr, list, sh.bittable); | |
a773b52a | 715 | sh_remove_from_list(ptr); |
74924dcb RS |
716 | OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc)); |
717 | sh_clearbit(buddy, list, sh.bittable); | |
a773b52a | 718 | sh_remove_from_list(buddy); |
74924dcb RS |
719 | |
720 | list--; | |
721 | ||
3b8e97ab P |
722 | /* Zero the higher addressed block's free list pointers */ |
723 | memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST)); | |
74924dcb RS |
724 | if (ptr > buddy) |
725 | ptr = buddy; | |
726 | ||
727 | OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc)); | |
728 | sh_setbit(ptr, list, sh.bittable); | |
729 | sh_add_to_list(&sh.freelist[list], ptr); | |
730 | OPENSSL_assert(sh.freelist[list] == ptr); | |
731 | } | |
732 | } | |
733 | ||
e8408681 | 734 | static size_t sh_actual_size(char *ptr) |
74924dcb RS |
735 | { |
736 | int list; | |
737 | ||
738 | OPENSSL_assert(WITHIN_ARENA(ptr)); | |
739 | if (!WITHIN_ARENA(ptr)) | |
740 | return 0; | |
741 | list = sh_getlist(ptr); | |
742 | OPENSSL_assert(sh_testbit(ptr, list, sh.bittable)); | |
e8408681 | 743 | return sh.arena_size / (ONE << list); |
74924dcb | 744 | } |
6943335e | 745 | #endif /* OPENSSL_NO_SECURE_MEMORY */ |