]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/mem_sec.c
Fix build with no-threads no-ec
[thirdparty/openssl.git] / crypto / mem_sec.c
CommitLineData
4f22f405 1/*
70e14ffb 2 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
4f22f405
RS
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
74924dcb
RS
10/*
11 * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
12 * This file is distributed under the terms of the OpenSSL license.
13 */
14
15/*
16 * This file is in two halves. The first half implements the public API
17 * to be used by external consumers, and to be used by OpenSSL to store
18 * data in a "secure arena." The second half implements the secure arena.
19 * For details on that implementation, see below (look for uppercase
20 * "SECURE HEAP IMPLEMENTATION").
21 */
22#include <openssl/crypto.h>
23#include <e_os.h>
74924dcb 24
183733f8
RL
25#include <string.h>
26
74924dcb
RS
27#if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
28# define IMPLEMENTED
d4dfb0ba 29# include <stdlib.h>
d4dfb0ba
RS
30# include <assert.h>
31# include <unistd.h>
27186da7 32# include <sys/types.h>
74924dcb 33# include <sys/mman.h>
9dfc5b96
TS
34# if defined(OPENSSL_SYS_LINUX)
35# include <sys/syscall.h>
36# include <linux/mman.h>
37# include <errno.h>
38# endif
74924dcb 39# include <sys/param.h>
27186da7
AP
40# include <sys/stat.h>
41# include <fcntl.h>
74924dcb
RS
42#endif
43
74924dcb 44#define CLEAR(p, s) OPENSSL_cleanse(p, s)
34750dc2
BL
45#ifndef PAGE_SIZE
46# define PAGE_SIZE 4096
47#endif
74924dcb
RS
48
49#ifdef IMPLEMENTED
df2ee0e2 50static size_t secure_mem_used;
74924dcb
RS
51
52static int secure_mem_initialized;
74924dcb 53
9471f776
MC
54static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
55
74924dcb
RS
56/*
57 * These are the functions that must be implemented by a secure heap (sh).
58 */
59static int sh_init(size_t size, int minsize);
332dc4fa
RS
60static void *sh_malloc(size_t size);
61static void sh_free(void *ptr);
74924dcb 62static void sh_done(void);
e8408681 63static size_t sh_actual_size(char *ptr);
74924dcb
RS
64static int sh_allocated(const char *ptr);
65#endif
66
67int CRYPTO_secure_malloc_init(size_t size, int minsize)
68{
69#ifdef IMPLEMENTED
70 int ret = 0;
71
74924dcb 72 if (!secure_mem_initialized) {
9471f776
MC
73 sec_malloc_lock = CRYPTO_THREAD_lock_new();
74 if (sec_malloc_lock == NULL)
75 return 0;
7031ddac
TS
76 if ((ret = sh_init(size, minsize)) != 0) {
77 secure_mem_initialized = 1;
78 } else {
79 CRYPTO_THREAD_lock_free(sec_malloc_lock);
80 sec_malloc_lock = NULL;
81 }
74924dcb 82 }
9471f776 83
74924dcb
RS
84 return ret;
85#else
86 return 0;
87#endif /* IMPLEMENTED */
88}
89
e8408681 90int CRYPTO_secure_malloc_done()
74924dcb
RS
91{
92#ifdef IMPLEMENTED
e8408681
TS
93 if (secure_mem_used == 0) {
94 sh_done();
95 secure_mem_initialized = 0;
96 CRYPTO_THREAD_lock_free(sec_malloc_lock);
7031ddac 97 sec_malloc_lock = NULL;
e8408681
TS
98 return 1;
99 }
74924dcb 100#endif /* IMPLEMENTED */
e8408681 101 return 0;
74924dcb
RS
102}
103
104int CRYPTO_secure_malloc_initialized()
105{
106#ifdef IMPLEMENTED
107 return secure_mem_initialized;
108#else
109 return 0;
110#endif /* IMPLEMENTED */
111}
112
ff842856 113void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
74924dcb
RS
114{
115#ifdef IMPLEMENTED
116 void *ret;
117 size_t actual_size;
118
119 if (!secure_mem_initialized) {
74924dcb
RS
120 return CRYPTO_malloc(num, file, line);
121 }
9471f776 122 CRYPTO_THREAD_write_lock(sec_malloc_lock);
74924dcb
RS
123 ret = sh_malloc(num);
124 actual_size = ret ? sh_actual_size(ret) : 0;
125 secure_mem_used += actual_size;
9471f776 126 CRYPTO_THREAD_unlock(sec_malloc_lock);
74924dcb
RS
127 return ret;
128#else
129 return CRYPTO_malloc(num, file, line);
130#endif /* IMPLEMENTED */
131}
132
3538c7da
RS
133void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
134{
135 void *ret = CRYPTO_secure_malloc(num, file, line);
136
137 if (ret != NULL)
138 memset(ret, 0, num);
139 return ret;
140}
141
05c7b163 142void CRYPTO_secure_free(void *ptr, const char *file, int line)
74924dcb
RS
143{
144#ifdef IMPLEMENTED
145 size_t actual_size;
146
147 if (ptr == NULL)
148 return;
e8408681 149 if (!CRYPTO_secure_allocated(ptr)) {
05c7b163 150 CRYPTO_free(ptr, file, line);
74924dcb
RS
151 return;
152 }
9471f776 153 CRYPTO_THREAD_write_lock(sec_malloc_lock);
74924dcb
RS
154 actual_size = sh_actual_size(ptr);
155 CLEAR(ptr, actual_size);
156 secure_mem_used -= actual_size;
157 sh_free(ptr);
9471f776 158 CRYPTO_THREAD_unlock(sec_malloc_lock);
74924dcb 159#else
6a78ae28 160 CRYPTO_free(ptr, file, line);
74924dcb
RS
161#endif /* IMPLEMENTED */
162}
163
164int CRYPTO_secure_allocated(const void *ptr)
165{
166#ifdef IMPLEMENTED
167 int ret;
168
169 if (!secure_mem_initialized)
170 return 0;
9471f776 171 CRYPTO_THREAD_write_lock(sec_malloc_lock);
74924dcb 172 ret = sh_allocated(ptr);
9471f776 173 CRYPTO_THREAD_unlock(sec_malloc_lock);
74924dcb
RS
174 return ret;
175#else
176 return 0;
177#endif /* IMPLEMENTED */
178}
179
bbd86bf5
RS
180size_t CRYPTO_secure_used()
181{
182#ifdef IMPLEMENTED
183 return secure_mem_used;
184#else
185 return 0;
186#endif /* IMPLEMENTED */
187}
188
d594199b
RS
189size_t CRYPTO_secure_actual_size(void *ptr)
190{
191#ifdef IMPLEMENTED
192 size_t actual_size;
193
9471f776 194 CRYPTO_THREAD_write_lock(sec_malloc_lock);
d594199b 195 actual_size = sh_actual_size(ptr);
9471f776 196 CRYPTO_THREAD_unlock(sec_malloc_lock);
d594199b
RS
197 return actual_size;
198#else
199 return 0;
200#endif
201}
74924dcb
RS
202/* END OF PAGE ...
203
204 ... START OF PAGE */
205
206/*
207 * SECURE HEAP IMPLEMENTATION
208 */
209#ifdef IMPLEMENTED
210
211
212/*
213 * The implementation provided here uses a fixed-sized mmap() heap,
214 * which is locked into memory, not written to core files, and protected
215 * on either side by an unmapped page, which will catch pointer overruns
216 * (or underruns) and an attempt to read data out of the secure heap.
217 * Free'd memory is zero'd or otherwise cleansed.
218 *
219 * This is a pretty standard buddy allocator. We keep areas in a multiple
220 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
221 * so all (and only) data is kept in the mmap'd heap.
222 *
223 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
224 * place.
225 */
226
e8408681
TS
227#define ONE ((size_t)1)
228
229# define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
230# define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
231# define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
74924dcb
RS
232
233#define WITHIN_ARENA(p) \
234 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
235#define WITHIN_FREELIST(p) \
236 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
237
238
239typedef struct sh_list_st
240{
241 struct sh_list_st *next;
242 struct sh_list_st **p_next;
243} SH_LIST;
244
245typedef struct sh_st
246{
247 char* map_result;
248 size_t map_size;
249 char *arena;
e8408681 250 size_t arena_size;
74924dcb 251 char **freelist;
e8408681
TS
252 ossl_ssize_t freelist_size;
253 size_t minsize;
74924dcb
RS
254 unsigned char *bittable;
255 unsigned char *bitmalloc;
e8408681 256 size_t bittable_size; /* size in bits */
74924dcb
RS
257} SH;
258
259static SH sh;
260
e8408681 261static size_t sh_getlist(char *ptr)
74924dcb 262{
e8408681
TS
263 ossl_ssize_t list = sh.freelist_size - 1;
264 size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
74924dcb
RS
265
266 for (; bit; bit >>= 1, list--) {
267 if (TESTBIT(sh.bittable, bit))
268 break;
269 OPENSSL_assert((bit & 1) == 0);
270 }
271
272 return list;
273}
274
275
276static int sh_testbit(char *ptr, int list, unsigned char *table)
277{
e8408681 278 size_t bit;
74924dcb
RS
279
280 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
281 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
e8408681 282 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
74924dcb
RS
283 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
284 return TESTBIT(table, bit);
285}
286
287static void sh_clearbit(char *ptr, int list, unsigned char *table)
288{
e8408681 289 size_t bit;
74924dcb
RS
290
291 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
292 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
e8408681 293 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
74924dcb
RS
294 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
295 OPENSSL_assert(TESTBIT(table, bit));
296 CLEARBIT(table, bit);
297}
298
299static void sh_setbit(char *ptr, int list, unsigned char *table)
300{
e8408681 301 size_t bit;
74924dcb
RS
302
303 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
304 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
e8408681 305 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
74924dcb
RS
306 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
307 OPENSSL_assert(!TESTBIT(table, bit));
308 SETBIT(table, bit);
309}
310
311static void sh_add_to_list(char **list, char *ptr)
312{
313 SH_LIST *temp;
314
315 OPENSSL_assert(WITHIN_FREELIST(list));
316 OPENSSL_assert(WITHIN_ARENA(ptr));
317
318 temp = (SH_LIST *)ptr;
319 temp->next = *(SH_LIST **)list;
320 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
321 temp->p_next = (SH_LIST **)list;
322
323 if (temp->next != NULL) {
324 OPENSSL_assert((char **)temp->next->p_next == list);
325 temp->next->p_next = &(temp->next);
326 }
327
328 *list = ptr;
329}
330
a773b52a 331static void sh_remove_from_list(char *ptr)
74924dcb
RS
332{
333 SH_LIST *temp, *temp2;
334
335 temp = (SH_LIST *)ptr;
336 if (temp->next != NULL)
337 temp->next->p_next = temp->p_next;
338 *temp->p_next = temp->next;
339 if (temp->next == NULL)
340 return;
341
342 temp2 = temp->next;
343 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
344}
345
346
347static int sh_init(size_t size, int minsize)
348{
7031ddac
TS
349 int ret;
350 size_t i;
74924dcb
RS
351 size_t pgsize;
352 size_t aligned;
353
354 memset(&sh, 0, sizeof sh);
355
356 /* make sure size and minsize are powers of 2 */
357 OPENSSL_assert(size > 0);
358 OPENSSL_assert((size & (size - 1)) == 0);
359 OPENSSL_assert(minsize > 0);
360 OPENSSL_assert((minsize & (minsize - 1)) == 0);
361 if (size <= 0 || (size & (size - 1)) != 0)
362 goto err;
363 if (minsize <= 0 || (minsize & (minsize - 1)) != 0)
364 goto err;
365
70e14ffb
P
366 while (minsize < (int)sizeof(SH_LIST))
367 minsize *= 2;
368
74924dcb
RS
369 sh.arena_size = size;
370 sh.minsize = minsize;
371 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
372
7f07149d
GV
373 /* Prevent allocations of size 0 later on */
374 if (sh.bittable_size >> 3 == 0)
375 goto err;
376
74924dcb
RS
377 sh.freelist_size = -1;
378 for (i = sh.bittable_size; i; i >>= 1)
379 sh.freelist_size++;
380
b51bce94 381 sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof (char *));
74924dcb
RS
382 OPENSSL_assert(sh.freelist != NULL);
383 if (sh.freelist == NULL)
384 goto err;
74924dcb 385
b51bce94 386 sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
74924dcb
RS
387 OPENSSL_assert(sh.bittable != NULL);
388 if (sh.bittable == NULL)
389 goto err;
74924dcb 390
b51bce94 391 sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
74924dcb
RS
392 OPENSSL_assert(sh.bitmalloc != NULL);
393 if (sh.bitmalloc == NULL)
394 goto err;
74924dcb
RS
395
396 /* Allocate space for heap, and two extra pages as guards */
9ae720b4
MC
397#if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
398 {
399# if defined(_SC_PAGE_SIZE)
400 long tmppgsize = sysconf(_SC_PAGE_SIZE);
401# else
402 long tmppgsize = sysconf(_SC_PAGESIZE);
403# endif
404 if (tmppgsize < 1)
405 pgsize = PAGE_SIZE;
406 else
407 pgsize = (size_t)tmppgsize;
408 }
74924dcb
RS
409#else
410 pgsize = PAGE_SIZE;
411#endif
412 sh.map_size = pgsize + sh.arena_size + pgsize;
27186da7
AP
413 if (1) {
414#ifdef MAP_ANON
415 sh.map_result = mmap(NULL, sh.map_size,
416 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
417 } else {
418#endif
419 int fd;
420
421 sh.map_result = MAP_FAILED;
422 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
423 sh.map_result = mmap(NULL, sh.map_size,
424 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
425 close(fd);
426 }
427 }
74924dcb
RS
428 if (sh.map_result == MAP_FAILED)
429 goto err;
430 sh.arena = (char *)(sh.map_result + pgsize);
431 sh_setbit(sh.arena, 0, sh.bittable);
432 sh_add_to_list(&sh.freelist[0], sh.arena);
433
434 /* Now try to add guard pages and lock into memory. */
435 ret = 1;
436
437 /* Starting guard is already aligned from mmap. */
438 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
439 ret = 2;
440
441 /* Ending guard page - need to round up to page boundary */
442 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
443 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
444 ret = 2;
445
9dfc5b96
TS
446#if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
447 if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
448 if (errno == ENOSYS) {
449 if (mlock(sh.arena, sh.arena_size) < 0)
450 ret = 2;
451 } else {
452 ret = 2;
453 }
454 }
455#else
74924dcb
RS
456 if (mlock(sh.arena, sh.arena_size) < 0)
457 ret = 2;
9dfc5b96 458#endif
74924dcb
RS
459#ifdef MADV_DONTDUMP
460 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
461 ret = 2;
462#endif
463
464 return ret;
465
466 err:
467 sh_done();
468 return 0;
469}
470
471static void sh_done()
472{
473 OPENSSL_free(sh.freelist);
474 OPENSSL_free(sh.bittable);
475 OPENSSL_free(sh.bitmalloc);
476 if (sh.map_result != NULL && sh.map_size)
477 munmap(sh.map_result, sh.map_size);
478 memset(&sh, 0, sizeof sh);
479}
480
481static int sh_allocated(const char *ptr)
482{
483 return WITHIN_ARENA(ptr) ? 1 : 0;
484}
485
486static char *sh_find_my_buddy(char *ptr, int list)
487{
e8408681 488 size_t bit;
74924dcb
RS
489 char *chunk = NULL;
490
e8408681 491 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
74924dcb
RS
492 bit ^= 1;
493
494 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
e8408681 495 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
74924dcb
RS
496
497 return chunk;
498}
499
332dc4fa 500static void *sh_malloc(size_t size)
74924dcb 501{
e8408681 502 ossl_ssize_t list, slist;
74924dcb
RS
503 size_t i;
504 char *chunk;
505
7031ddac
TS
506 if (size > sh.arena_size)
507 return NULL;
508
74924dcb
RS
509 list = sh.freelist_size - 1;
510 for (i = sh.minsize; i < size; i <<= 1)
511 list--;
512 if (list < 0)
513 return NULL;
514
515 /* try to find a larger entry to split */
516 for (slist = list; slist >= 0; slist--)
517 if (sh.freelist[slist] != NULL)
518 break;
519 if (slist < 0)
520 return NULL;
521
522 /* split larger entry */
523 while (slist != list) {
524 char *temp = sh.freelist[slist];
525
526 /* remove from bigger list */
527 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
528 sh_clearbit(temp, slist, sh.bittable);
a773b52a 529 sh_remove_from_list(temp);
74924dcb
RS
530 OPENSSL_assert(temp != sh.freelist[slist]);
531
532 /* done with bigger list */
533 slist++;
534
535 /* add to smaller list */
536 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
537 sh_setbit(temp, slist, sh.bittable);
538 sh_add_to_list(&sh.freelist[slist], temp);
539 OPENSSL_assert(sh.freelist[slist] == temp);
540
541 /* split in 2 */
542 temp += sh.arena_size >> slist;
543 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
544 sh_setbit(temp, slist, sh.bittable);
545 sh_add_to_list(&sh.freelist[slist], temp);
546 OPENSSL_assert(sh.freelist[slist] == temp);
547
548 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
549 }
550
551 /* peel off memory to hand back */
552 chunk = sh.freelist[list];
553 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
554 sh_setbit(chunk, list, sh.bitmalloc);
a773b52a 555 sh_remove_from_list(chunk);
74924dcb
RS
556
557 OPENSSL_assert(WITHIN_ARENA(chunk));
558
559 return chunk;
560}
561
332dc4fa 562static void sh_free(void *ptr)
74924dcb 563{
e8408681 564 size_t list;
332dc4fa 565 void *buddy;
74924dcb
RS
566
567 if (ptr == NULL)
568 return;
569 OPENSSL_assert(WITHIN_ARENA(ptr));
570 if (!WITHIN_ARENA(ptr))
571 return;
572
573 list = sh_getlist(ptr);
574 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
575 sh_clearbit(ptr, list, sh.bitmalloc);
576 sh_add_to_list(&sh.freelist[list], ptr);
577
578 /* Try to coalesce two adjacent free areas. */
579 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
580 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
581 OPENSSL_assert(ptr != NULL);
582 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
583 sh_clearbit(ptr, list, sh.bittable);
a773b52a 584 sh_remove_from_list(ptr);
74924dcb
RS
585 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
586 sh_clearbit(buddy, list, sh.bittable);
a773b52a 587 sh_remove_from_list(buddy);
74924dcb
RS
588
589 list--;
590
591 if (ptr > buddy)
592 ptr = buddy;
593
594 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
595 sh_setbit(ptr, list, sh.bittable);
596 sh_add_to_list(&sh.freelist[list], ptr);
597 OPENSSL_assert(sh.freelist[list] == ptr);
598 }
599}
600
e8408681 601static size_t sh_actual_size(char *ptr)
74924dcb
RS
602{
603 int list;
604
605 OPENSSL_assert(WITHIN_ARENA(ptr));
606 if (!WITHIN_ARENA(ptr))
607 return 0;
608 list = sh_getlist(ptr);
609 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
e8408681 610 return sh.arena_size / (ONE << list);
74924dcb
RS
611}
612#endif /* IMPLEMENTED */