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