Debian Codegraph @

crt/s2n/tests/unit/s2n_mem_test.c

raw symbols SCIP
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 #include "utils/s2n_mem.h"
17
18 #include "s2n_test.h"
19 #include "utils/s2n_safety.h"
20
21 int s2n_strict_mem_free_cb(void *ptr, uint32_t size)
22 {
23 POSIX_ENSURE_REF(ptr);
24 POSIX_ENSURE_GT(size, 0);
25 return S2N_SUCCESS;
26 }
27
28 static bool s2n_failing_free_cb_called = false;
29
30 int s2n_failing_mem_free_cb(void *ptr, uint32_t size)
31 {
32 free(ptr);
33 s2n_failing_free_cb_called = true;
34 return S2N_FAILURE;
35 }
36
37 int main(int argc, char **argv)
38 {
39 BEGIN_TEST();
40
41 /* Test safety of all mem free methods */
42 {
43 /* Test: no-op for empty blob */
44 {
45 struct s2n_blob blob = { 0 };
46 EXPECT_SUCCESS(s2n_free(&blob));
47 EXPECT_SUCCESS(s2n_free_without_wipe(&blob));
48 EXPECT_SUCCESS(s2n_free_or_wipe(&blob));
49 };
50
51 /* Test: no-op for already freed blob */
52 {
53 struct s2n_blob blob = { 0 };
54 EXPECT_SUCCESS(s2n_alloc(&blob, 10));
55 EXPECT_SUCCESS(s2n_free(&blob));
56
57 EXPECT_SUCCESS(s2n_free(&blob));
58 EXPECT_SUCCESS(s2n_free_without_wipe(&blob));
59 EXPECT_SUCCESS(s2n_free_or_wipe(&blob));
60 };
61
62 /* Test: error for NULL */
63 {
64 EXPECT_FAILURE_WITH_ERRNO(s2n_free(NULL), S2N_ERR_NULL);
65 EXPECT_FAILURE_WITH_ERRNO(s2n_free_without_wipe(NULL), S2N_ERR_NULL);
66 EXPECT_FAILURE_WITH_ERRNO(s2n_free_or_wipe(NULL), S2N_ERR_NULL);
67 };
68
69 /* Test: faulty / overly strict free implementation
70 *
71 * A correct implementation of free() should be a no-op for a NULL pointer.
72 * However, we have encountered cases where faulty implementations of free()
73 * seg faulted on NULL: our Rust bindings initially incorrectly assumed that
74 * Rust's dealloc() handled NULLs like C's free().
75 *
76 * As an easy way to avoid this, we should just never call the free mem callback for NULL.
77 */
78 {
79 /* Save real free callback */
80 s2n_mem_init_callback mem_init_cb = NULL;
81 s2n_mem_cleanup_callback mem_cleanup_cb = NULL;
82 s2n_mem_malloc_callback mem_malloc_cb = NULL;
83 s2n_mem_free_callback mem_free_cb = NULL;
84 EXPECT_OK(s2n_mem_get_callbacks(&mem_init_cb, &mem_cleanup_cb, &mem_malloc_cb, &mem_free_cb));
85
86 /* Set callback that won't accepts NULLs / zeros */
87 EXPECT_OK(s2n_mem_override_callbacks(mem_init_cb, mem_cleanup_cb, mem_malloc_cb, s2n_strict_mem_free_cb));
88
89 /* No-op for empty blob */
90 struct s2n_blob blob = { 0 };
91 EXPECT_SUCCESS(s2n_free(&blob));
92 EXPECT_SUCCESS(s2n_free_without_wipe(&blob));
93 EXPECT_SUCCESS(s2n_free_or_wipe(&blob));
94
95 /* Restore real free callback */
96 EXPECT_OK(s2n_mem_override_callbacks(mem_init_cb, mem_cleanup_cb, mem_malloc_cb, mem_free_cb));
97 };
98 };
99
100 /* Test: no double-free when s2n_mem_free_cb fails
101 *
102 * If a custom free callback frees the memory but then returns failure,
103 * s2n_free_without_wipe must zero the blob BEFORE invoking the callback.
104 * This ensures that a subsequent free (e.g. during cleanup via
105 * s2n_connection_free) sees data==NULL and skips the callback,
106 * avoiding a double-free.
107 */
108 {
109 /* Save real callbacks */
110 s2n_mem_init_callback mem_init_cb = NULL;
111 s2n_mem_cleanup_callback mem_cleanup_cb = NULL;
112 s2n_mem_malloc_callback mem_malloc_cb = NULL;
113 s2n_mem_free_callback mem_free_cb = NULL;
114 EXPECT_OK(s2n_mem_get_callbacks(&mem_init_cb, &mem_cleanup_cb, &mem_malloc_cb, &mem_free_cb));
115
116 /* Override with a free callback that frees the memory but returns failure */
117 EXPECT_OK(s2n_mem_override_callbacks(mem_init_cb, mem_cleanup_cb, mem_malloc_cb, s2n_failing_mem_free_cb));
118
119 struct s2n_blob blob = { 0 };
120 EXPECT_SUCCESS(s2n_alloc(&blob, 100));
121 EXPECT_NOT_NULL(blob.data);
122
123 /* First free: callback frees the memory but returns failure.
124 * s2n_free_without_wipe should still zero the blob before calling the callback.
125 */
126 s2n_failing_free_cb_called = false;
127 EXPECT_FAILURE_WITH_ERRNO(s2n_free_without_wipe(&blob), S2N_ERR_CANCELLED);
128 EXPECT_TRUE(s2n_failing_free_cb_called);
129
130 /* The blob must be zeroed despite the callback failure */
131 EXPECT_NULL(blob.data);
132 EXPECT_EQUAL(blob.size, 0);
133 EXPECT_EQUAL(blob.allocated, 0);
134
135 /* Second free: must be a safe no-op, not a double-free */
136 s2n_failing_free_cb_called = false;
137 EXPECT_SUCCESS(s2n_free_without_wipe(&blob));
138 EXPECT_FALSE(s2n_failing_free_cb_called);
139
140 /* Restore real callbacks */
141 EXPECT_OK(s2n_mem_override_callbacks(mem_init_cb, mem_cleanup_cb, mem_malloc_cb, mem_free_cb));
142 };
143
144 END_TEST();
145 }