Branch data Line data Source code
1 : : /*
2 : : * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License").
5 : : * You may not use this file except in compliance with the License.
6 : : * A copy of the License is located at
7 : : *
8 : : * http://aws.amazon.com/apache2.0
9 : : *
10 : : * or in the "license" file accompanying this file. This file is distributed
11 : : * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 : : * express or implied. See the License for the specific language governing
13 : : * permissions and limitations under the License.
14 : : */
15 : :
16 : : #define _DEFAULT_SOURCE 1
17 : : #if defined(S2N_FEATURES_AVAILABLE)
18 : : #include <features.h>
19 : : #endif
20 : :
21 : : #include <limits.h>
22 : : #include <stdint.h>
23 : : #include <stdlib.h>
24 : : #ifndef _WIN32
25 : : #include <sys/mman.h>
26 : : #include <sys/param.h>
27 : : #include <unistd.h>
28 : : #endif
29 : :
30 : : #include "error/s2n_errno.h"
31 : : #include "utils/s2n_blob.h"
32 : : #include "utils/s2n_mem.h"
33 : : #include "utils/s2n_safety.h"
34 : :
35 : : static uint32_t page_size = 4096;
36 : : static bool initialized = false;
37 : :
38 : : static int s2n_mem_init_impl(void);
39 : : static int s2n_mem_cleanup_impl(void);
40 : :
41 : : #ifdef _WIN32
42 : : static int s2n_windows_mem_free_impl(void *ptr, uint32_t size);
43 : : static int s2n_windows_mem_malloc_impl(void **ptr, uint32_t requested, uint32_t *allocated);
44 : : #else
45 : : static int s2n_mem_free_no_mlock_impl(void *ptr, uint32_t size);
46 : : static int s2n_mem_malloc_no_mlock_impl(void **ptr, uint32_t requested, uint32_t *allocated);
47 : : static int s2n_mem_free_mlock_impl(void *ptr, uint32_t size);
48 : : static int s2n_mem_malloc_mlock_impl(void **ptr, uint32_t requested, uint32_t *allocated);
49 : : #endif
50 : :
51 : : static s2n_mem_init_callback s2n_mem_init_cb = s2n_mem_init_impl;
52 : : static s2n_mem_cleanup_callback s2n_mem_cleanup_cb = s2n_mem_cleanup_impl;
53 : : #ifdef _WIN32
54 : : static s2n_mem_malloc_callback s2n_mem_malloc_cb = s2n_windows_mem_malloc_impl;
55 : : static s2n_mem_free_callback s2n_mem_free_cb = s2n_windows_mem_free_impl;
56 : : #else
57 : : static s2n_mem_malloc_callback s2n_mem_malloc_cb = s2n_mem_malloc_mlock_impl;
58 : : static s2n_mem_free_callback s2n_mem_free_cb = s2n_mem_free_mlock_impl;
59 : : #endif
60 : :
61 : : static int s2n_mem_init_impl(void)
62 : 348 : {
63 : 348 : #ifndef _WIN32
64 : 348 : long sysconf_rc = sysconf(_SC_PAGESIZE);
65 : :
66 : : /* sysconf must not error, and page_size cannot be 0 */
67 [ - + ][ # # ]: 348 : POSIX_ENSURE_GT(sysconf_rc, 0);
68 : :
69 : : /* page_size must be a valid uint32 */
70 : 348 : long max_page_size = S2N_MIN(UINT32_MAX, LONG_MAX);
71 [ - + ][ # # ]: 348 : POSIX_ENSURE_LTE(sysconf_rc, max_page_size);
72 : 348 : page_size = (uint32_t) sysconf_rc;
73 : :
74 [ + - ][ # # ]: 348 : if (getenv("S2N_DONT_MLOCK") || s2n_in_unit_test()) {
75 : 348 : s2n_mem_malloc_cb = s2n_mem_malloc_no_mlock_impl;
76 : 348 : s2n_mem_free_cb = s2n_mem_free_no_mlock_impl;
77 : 348 : }
78 : 348 : #endif
79 : 348 : return S2N_SUCCESS;
80 : 348 : }
81 : :
82 : : static int s2n_mem_cleanup_impl(void)
83 : 348 : {
84 : 348 : page_size = 4096;
85 : : #ifdef _WIN32
86 : : s2n_mem_malloc_cb = s2n_windows_mem_malloc_impl;
87 : : s2n_mem_free_cb = s2n_windows_mem_free_impl;
88 : : #else
89 : 348 : s2n_mem_malloc_cb = s2n_mem_malloc_no_mlock_impl;
90 : 348 : s2n_mem_free_cb = s2n_mem_free_no_mlock_impl;
91 : 348 : #endif
92 : 348 : return S2N_SUCCESS;
93 : 348 : }
94 : :
95 : : #ifdef _WIN32
96 : : /* mlock is not supported on Windows, so these use plain malloc/free without memory locking. */
97 : :
98 : : static int s2n_windows_mem_malloc_impl(void **ptr, uint32_t requested, uint32_t *allocated)
99 : : {
100 : : *ptr = malloc(requested);
101 : : POSIX_ENSURE(*ptr != NULL, S2N_ERR_ALLOC);
102 : : *allocated = requested;
103 : :
104 : : return S2N_SUCCESS;
105 : : }
106 : :
107 : : static int s2n_windows_mem_free_impl(void *ptr, uint32_t size)
108 : : {
109 : : free(ptr);
110 : : return S2N_SUCCESS;
111 : : }
112 : :
113 : : #else
114 : :
115 : : static int s2n_mem_free_mlock_impl(void *ptr, uint32_t size)
116 : 0 : {
117 : : /* Perform a best-effort `munlock`: ignore any errors during unlocking. */
118 : 0 : munlock(ptr, size);
119 : : /*
120 : : ** Reverse the MADV_DONTDUMP applied in s2n_mem_malloc_mlock_impl before
121 : : ** returning the pages to the allocator. Without this, freed pages keep the
122 : : ** VM_DONTDUMP flag; the kernel cannot merge adjacent VMAs whose flags
123 : : ** differ, so the process VMA count grows with connection churn until it
124 : : ** hits vm.max_map_count and new madvise/mlock calls fail (resource
125 : : ** exhaustion). Best-effort like munlock: the memory is being freed
126 : : ** regardless, so a failure here must not fail the free.
127 : : */
128 : 0 : #if defined(MADV_DONTDUMP) && !defined(S2N_ADDRESS_SANITIZER) && !defined(S2N_FUZZ_TESTING)
129 : 0 : madvise(ptr, size, MADV_DODUMP);
130 : 0 : #endif
131 : 0 : free(ptr);
132 : 0 : return S2N_SUCCESS;
133 : 0 : }
134 : :
135 : : static int s2n_mem_free_no_mlock_impl(void *ptr, uint32_t size)
136 : 7723312 : {
137 : 7723312 : free(ptr);
138 : :
139 : 7723312 : return S2N_SUCCESS;
140 : 7723312 : }
141 : :
142 : : static int s2n_mem_malloc_mlock_impl(void **ptr, uint32_t requested, uint32_t *allocated)
143 : 0 : {
144 [ # # ][ # # ]: 0 : POSIX_ENSURE_REF(ptr);
145 : :
146 : : /* Page aligned allocation required for mlock */
147 : 0 : uint32_t allocate = 0;
148 : :
149 [ # # ]: 0 : POSIX_GUARD(s2n_align_to(requested, page_size, &allocate));
150 : :
151 : 0 : *ptr = NULL;
152 [ # # ][ # # ]: 0 : POSIX_ENSURE(posix_memalign(ptr, page_size, allocate) == 0, S2N_ERR_ALLOC);
153 : 0 : *allocated = allocate;
154 : :
155 : : /*
156 : : ** We disable MAD_DONTDUMP when fuzz-testing or using the address sanitizer because
157 : : ** both need to be able to dump pages to function. It's how they map heap output.
158 : : */
159 : 0 : #if defined(MADV_DONTDUMP) && !defined(S2N_ADDRESS_SANITIZER) && !defined(S2N_FUZZ_TESTING)
160 [ # # ]: 0 : if (madvise(*ptr, *allocated, MADV_DONTDUMP) != 0) {
161 [ # # ]: 0 : POSIX_GUARD(s2n_mem_free_no_mlock_impl(*ptr, *allocated));
162 [ # # ]: 0 : POSIX_BAIL(S2N_ERR_MADVISE);
163 : 0 : }
164 : 0 : #endif
165 : :
166 [ # # ]: 0 : if (mlock(*ptr, *allocated) != 0) {
167 : : /* When mlock fails, no memory will be locked, so we don't use munlock on free */
168 [ # # ]: 0 : POSIX_GUARD(s2n_mem_free_no_mlock_impl(*ptr, *allocated));
169 [ # # ]: 0 : POSIX_BAIL(S2N_ERR_MLOCK);
170 : 0 : }
171 : :
172 [ # # ][ # # ]: 0 : POSIX_ENSURE(*ptr != NULL, S2N_ERR_ALLOC);
173 : :
174 : 0 : return S2N_SUCCESS;
175 : 0 : }
176 : :
177 : : static int s2n_mem_malloc_no_mlock_impl(void **ptr, uint32_t requested, uint32_t *allocated)
178 : 7723313 : {
179 : 7723313 : *ptr = malloc(requested);
180 [ # # ][ - + ]: 7723313 : POSIX_ENSURE(*ptr != NULL, S2N_ERR_ALLOC);
181 : 7723313 : *allocated = requested;
182 : :
183 : 7723313 : return S2N_SUCCESS;
184 : 7723313 : }
185 : :
186 : : #endif
187 : :
188 : : int s2n_mem_set_callbacks(s2n_mem_init_callback mem_init_callback, s2n_mem_cleanup_callback mem_cleanup_callback,
189 : : s2n_mem_malloc_callback mem_malloc_callback, s2n_mem_free_callback mem_free_callback)
190 : 2 : {
191 [ + + ][ + - ]: 2 : POSIX_ENSURE(!initialized, S2N_ERR_INITIALIZED);
192 [ - + ]: 1 : POSIX_GUARD_RESULT(s2n_mem_override_callbacks(mem_init_callback, mem_cleanup_callback,
193 : 1 : mem_malloc_callback, mem_free_callback));
194 : 1 : return S2N_SUCCESS;
195 : 1 : }
196 : :
197 : : S2N_RESULT s2n_mem_override_callbacks(s2n_mem_init_callback mem_init_callback, s2n_mem_cleanup_callback mem_cleanup_callback,
198 : : s2n_mem_malloc_callback mem_malloc_callback, s2n_mem_free_callback mem_free_callback)
199 : 338 : {
200 [ # # ][ - + ]: 338 : RESULT_ENSURE_REF(mem_init_callback);
201 [ - + ][ # # ]: 338 : RESULT_ENSURE_REF(mem_cleanup_callback);
202 [ - + ][ # # ]: 338 : RESULT_ENSURE_REF(mem_malloc_callback);
203 [ - + ][ # # ]: 338 : RESULT_ENSURE_REF(mem_free_callback);
204 : :
205 : 338 : s2n_mem_init_cb = mem_init_callback;
206 : 338 : s2n_mem_cleanup_cb = mem_cleanup_callback;
207 : 338 : s2n_mem_malloc_cb = mem_malloc_callback;
208 : 338 : s2n_mem_free_cb = mem_free_callback;
209 : :
210 : 338 : return S2N_RESULT_OK;
211 : 338 : }
212 : :
213 : : S2N_RESULT s2n_mem_get_callbacks(s2n_mem_init_callback *mem_init_callback, s2n_mem_cleanup_callback *mem_cleanup_callback,
214 : : s2n_mem_malloc_callback *mem_malloc_callback, s2n_mem_free_callback *mem_free_callback)
215 : 334 : {
216 [ # # ][ - + ]: 334 : RESULT_ENSURE_REF(mem_init_callback);
217 [ - + ][ # # ]: 334 : RESULT_ENSURE_REF(mem_cleanup_callback);
218 [ - + ][ # # ]: 334 : RESULT_ENSURE_REF(mem_malloc_callback);
219 [ - + ][ # # ]: 334 : RESULT_ENSURE_REF(mem_free_callback);
220 : :
221 : 334 : *mem_init_callback = s2n_mem_init_cb;
222 : 334 : *mem_cleanup_callback = s2n_mem_cleanup_cb;
223 : 334 : *mem_malloc_callback = s2n_mem_malloc_cb;
224 : 334 : *mem_free_callback = s2n_mem_free_cb;
225 : :
226 : 334 : return S2N_RESULT_OK;
227 : 334 : }
228 : :
229 : : /**
230 : : * Allocate a new blob on the heap.
231 : : *
232 : : * The blob will be _growable_.
233 : : *
234 : : * This blob owns the underlying memory, which will be freed when `s2n_free` is
235 : : * called on `b`.
236 : : */
237 : : int s2n_alloc(struct s2n_blob *b, uint32_t size)
238 : 7854798 : {
239 [ # # ][ - + ]: 7854798 : POSIX_ENSURE(initialized, S2N_ERR_NOT_INITIALIZED);
240 [ - + ][ # # ]: 7854798 : POSIX_ENSURE_REF(b);
241 : 7854798 : const struct s2n_blob temp = { 0 };
242 : 7854798 : *b = temp;
243 [ + + ]: 7854798 : POSIX_GUARD(s2n_realloc(b, size));
244 : 7854797 : return S2N_SUCCESS;
245 : 7854798 : }
246 : :
247 : : /* A blob is growable if it is either explicitly marked as such, or if it contains no data */
248 : : bool s2n_blob_is_growable(const struct s2n_blob *b)
249 : 84602310 : {
250 [ + + ][ + + ]: 84602310 : return b && (b->growable || (b->data == NULL && b->size == 0 && b->allocated == 0));
[ + + ][ + - ]
[ + - ]
251 : 84602310 : }
252 : :
253 : : /**
254 : : * Resize a blob to `size`. The blob must be allocated or empty.
255 : : *
256 : : * This will allocate more memory if necessary, or reuse the existing allocation
257 : : * if the requested size is smaller than the current size.
258 : : *
259 : : * If failed, *b remains unchanged.
260 : : */
261 : : int s2n_realloc(struct s2n_blob *b, uint32_t size)
262 : 18745950 : {
263 [ - + ][ # # ]: 18745950 : POSIX_ENSURE(initialized, S2N_ERR_NOT_INITIALIZED);
264 [ + - ][ + + ]: 18745950 : POSIX_ENSURE_REF(b);
265 [ + + ][ + - ]: 18745949 : POSIX_ENSURE(s2n_blob_is_growable(b), S2N_ERR_RESIZE_STATIC_BLOB);
266 [ + + ]: 18745948 : if (size == 0) {
267 : 11018788 : return s2n_free(b);
268 : 11018788 : }
269 : :
270 : : /* blob already has space for the request */
271 [ + + ]: 7727160 : if (size <= b->allocated) {
272 [ + + ]: 2241 : if (size < b->size) {
273 : : /* Zero the existing blob memory before the we release it */
274 : 63 : struct s2n_blob slice = { 0 };
275 [ - + ]: 63 : POSIX_GUARD(s2n_blob_slice(b, &slice, size, b->size - size));
276 [ - + ]: 63 : POSIX_GUARD(s2n_blob_zero(&slice));
277 : 63 : }
278 : :
279 : 2241 : b->size = size;
280 : 2241 : return S2N_SUCCESS;
281 : 2241 : }
282 : :
283 : 7724919 : struct s2n_blob new_memory = { .data = NULL, .size = size, .allocated = 0, .growable = 1 };
284 [ + + ]: 7724919 : if (s2n_mem_malloc_cb((void **) &new_memory.data, new_memory.size, &new_memory.allocated) != 0) {
285 : 1 : S2N_ERROR_PRESERVE_ERRNO();
286 : 1 : }
287 : :
288 [ - + ][ # # ]: 7724918 : POSIX_ENSURE(new_memory.allocated >= new_memory.size, S2N_ERR_ALLOC);
289 [ - + ][ # # ]: 7724918 : POSIX_ENSURE(new_memory.data != NULL, S2N_ERR_ALLOC);
290 : :
291 [ + + ]: 7724918 : if (b->size) {
292 [ # # ][ - + ]: 10806 : POSIX_CHECKED_MEMCPY(new_memory.data, b->data, b->size);
[ + - ]
293 : 10806 : }
294 : :
295 [ + + ]: 7724918 : if (b->allocated) {
296 [ - + ]: 10807 : POSIX_GUARD(s2n_free(b));
297 : 10807 : }
298 : :
299 : 7724918 : *b = new_memory;
300 : 7724918 : return S2N_SUCCESS;
301 : 7724918 : }
302 : :
303 : : int s2n_free_object(uint8_t **p_data, uint32_t size)
304 : 674588 : {
305 [ - + ][ # # ]: 674588 : POSIX_ENSURE_REF(p_data);
306 : :
307 [ + + ]: 674588 : if (*p_data == NULL) {
308 : 4127 : return S2N_SUCCESS;
309 : 4127 : }
310 : :
311 [ # # ][ - + ]: 670461 : POSIX_ENSURE(initialized, S2N_ERR_NOT_INITIALIZED);
312 : 670461 : struct s2n_blob b = { .data = *p_data, .allocated = size, .size = size, .growable = 1 };
313 : :
314 : : /* s2n_free() will call free() even if it returns error (for a growable blob).
315 : : ** This makes sure *p_data is not used after free() */
316 : 670461 : *p_data = NULL;
317 : :
318 : 670461 : return s2n_free(&b);
319 : 670461 : }
320 : :
321 : : /**
322 : : * Allocate enough memory for `to` to contain all the data in `from`, then copy
323 : : * the data in `from` to `to`.
324 : : */
325 : : int s2n_dup(struct s2n_blob *from, struct s2n_blob *to)
326 : 41230 : {
327 [ - + ][ # # ]: 41230 : POSIX_ENSURE(initialized, S2N_ERR_NOT_INITIALIZED);
328 [ # # ][ - + ]: 41230 : POSIX_ENSURE_REF(to);
329 [ - + ][ # # ]: 41230 : POSIX_ENSURE_REF(from);
330 [ # # ][ - + ]: 41230 : POSIX_ENSURE_EQ(to->size, 0);
331 [ - + ][ # # ]: 41230 : POSIX_ENSURE_EQ(to->data, NULL);
332 [ + - ][ + + ]: 41230 : POSIX_ENSURE_NE(from->size, 0);
333 [ - + ][ # # ]: 41228 : POSIX_ENSURE_NE(from->data, NULL);
334 : :
335 [ + + ]: 41228 : POSIX_GUARD(s2n_alloc(to, from->size));
336 : :
337 [ - + ][ # # ]: 41227 : POSIX_CHECKED_MEMCPY(to->data, from->data, to->size);
[ + - ]
338 : :
339 : 41227 : return S2N_SUCCESS;
340 : 41227 : }
341 : :
342 : : int s2n_mem_init(void)
343 : 349 : {
344 [ # # ][ - + ]: 349 : POSIX_ENSURE(s2n_mem_init_cb() >= S2N_SUCCESS, S2N_ERR_CANCELLED);
345 : :
346 : 349 : initialized = true;
347 : :
348 : 349 : return S2N_SUCCESS;
349 : 349 : }
350 : :
351 : : bool s2n_mem_is_init(void)
352 : 0 : {
353 : 0 : return initialized;
354 : 0 : }
355 : :
356 : : uint32_t s2n_mem_get_page_size(void)
357 : 0 : {
358 : 0 : return page_size;
359 : 0 : }
360 : :
361 : : int s2n_mem_cleanup(void)
362 : 376 : {
363 [ + + ][ + - ]: 376 : POSIX_ENSURE(initialized, S2N_ERR_NOT_INITIALIZED);
364 [ - + ][ # # ]: 349 : POSIX_ENSURE(s2n_mem_cleanup_cb() >= S2N_SUCCESS, S2N_ERR_CANCELLED);
365 : :
366 : 349 : initialized = false;
367 : :
368 : 349 : return S2N_SUCCESS;
369 : 349 : }
370 : :
371 : : int s2n_free(struct s2n_blob *b)
372 : 65844692 : {
373 : : /* To avoid memory leaks, don't exit the function until the memory
374 : : has been freed */
375 : 65844692 : int zero_rc = s2n_blob_zero(b);
376 [ + + ]: 65844692 : POSIX_GUARD(s2n_free_without_wipe(b));
377 : 65844608 : return zero_rc;
378 : 65844692 : }
379 : :
380 : : int s2n_free_without_wipe(struct s2n_blob *b)
381 : 65856439 : {
382 [ + + ][ + + ]: 65856439 : POSIX_PRECONDITION(s2n_blob_validate(b));
383 : :
384 [ + + ][ + - ]: 65856436 : POSIX_ENSURE(initialized, S2N_ERR_NOT_INITIALIZED);
385 [ + + ][ + - ]: 65856355 : POSIX_ENSURE(s2n_blob_is_growable(b), S2N_ERR_FREE_STATIC_BLOB);
386 : :
387 [ + + ]: 65856354 : if (b->data) {
388 : 7724918 : void *data = b->data;
389 : 7724918 : uint32_t allocated = b->allocated;
390 : : /* Set data point to NULL first to prevent potential double-free on s2n_mem_free_cb error path */
391 : 7724918 : *b = (struct s2n_blob){ 0 };
392 [ + + ][ + - ]: 7724918 : POSIX_ENSURE(s2n_mem_free_cb(data, allocated) >= S2N_SUCCESS, S2N_ERR_CANCELLED);
393 : 7724918 : }
394 : :
395 : 65856353 : *b = (struct s2n_blob){ 0 };
396 : :
397 : 65856353 : return S2N_SUCCESS;
398 : 65856354 : }
399 : :
400 : : int s2n_free_or_wipe(struct s2n_blob *b)
401 : 32068690 : {
402 [ + + ][ + - ]: 32068690 : POSIX_ENSURE_REF(b);
403 : 32068689 : int zero_rc = s2n_blob_zero(b);
404 [ + + ]: 32068689 : if (b->allocated) {
405 [ - + ]: 11731 : POSIX_GUARD(s2n_free_without_wipe(b));
406 : 11731 : }
407 : 32068689 : return zero_rc;
408 : 32068689 : }
|