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