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