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