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