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