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