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