xref: /src/crypto/openssl/test/secmemtest.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d) !
1 /*
2  * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <openssl/crypto.h>
11 
12 #include "testutil.h"
13 #include "internal/e_os.h"
14 
test_sec_mem(void)15 static int test_sec_mem(void)
16 {
17 #ifndef OPENSSL_NO_SECURE_MEMORY
18     int testresult = 0;
19     char *p = NULL, *q = NULL, *r = NULL, *s = NULL;
20 
21     TEST_info("Secure memory is implemented.");
22 
23     s = OPENSSL_secure_malloc(20);
24     /* s = non-secure 20 */
25     if (!TEST_ptr(s)
26         || !TEST_false(CRYPTO_secure_allocated(s)))
27         goto end;
28     r = OPENSSL_secure_malloc(20);
29     /* r = non-secure 20, s = non-secure 20 */
30     if (!TEST_ptr(r)
31         || !TEST_true(CRYPTO_secure_malloc_init(4096, 32))
32         || !TEST_false(CRYPTO_secure_allocated(r)))
33         goto end;
34     p = OPENSSL_secure_malloc(20);
35     if (!TEST_ptr(p)
36         /* r = non-secure 20, p = secure 20, s = non-secure 20 */
37         || !TEST_true(CRYPTO_secure_allocated(p))
38         /* 20 secure -> 32-byte minimum allocation unit */
39         || !TEST_size_t_eq(CRYPTO_secure_used(), 32))
40         goto end;
41     q = OPENSSL_malloc(20);
42     if (!TEST_ptr(q))
43         goto end;
44     /* r = non-secure 20, p = secure 20, q = non-secure 20, s = non-secure 20 */
45     if (!TEST_false(CRYPTO_secure_allocated(q)))
46         goto end;
47     OPENSSL_secure_clear_free(s, 20);
48     s = OPENSSL_secure_malloc(20);
49     if (!TEST_ptr(s)
50         /* r = non-secure 20, p = secure 20, q = non-secure 20, s = secure 20 */
51         || !TEST_true(CRYPTO_secure_allocated(s))
52         /* 2 * 20 secure -> 64 bytes allocated */
53         || !TEST_size_t_eq(CRYPTO_secure_used(), 64))
54         goto end;
55     OPENSSL_secure_clear_free(p, 20);
56     p = NULL;
57     /* 20 secure -> 32 bytes allocated */
58     if (!TEST_size_t_eq(CRYPTO_secure_used(), 32))
59         goto end;
60     OPENSSL_free(q);
61     q = NULL;
62     /* should not complete, as secure memory is still allocated */
63     if (!TEST_false(CRYPTO_secure_malloc_done())
64         || !TEST_true(CRYPTO_secure_malloc_initialized()))
65         goto end;
66     OPENSSL_secure_free(s);
67     s = NULL;
68     /* secure memory should now be 0, so done should complete */
69     if (!TEST_size_t_eq(CRYPTO_secure_used(), 0)
70         || !TEST_true(CRYPTO_secure_malloc_done())
71         || !TEST_false(CRYPTO_secure_malloc_initialized()))
72         goto end;
73 
74     TEST_info("Possible infinite loop: allocate more than available");
75     if (!TEST_true(CRYPTO_secure_malloc_init(32768, 16)))
76         goto end;
77     TEST_ptr_null(OPENSSL_secure_malloc((size_t)-1));
78     TEST_true(CRYPTO_secure_malloc_done());
79 
80     /*
81      * If init fails, then initialized should be false, if not, this
82      * could cause an infinite loop secure_malloc, but we don't test it
83      */
84     if (TEST_false(CRYPTO_secure_malloc_init(16, 16)) && !TEST_false(CRYPTO_secure_malloc_initialized())) {
85         TEST_true(CRYPTO_secure_malloc_done());
86         goto end;
87     }
88 
89     /*-
90      * There was also a possible infinite loop when the number of
91      * elements was 1<<31, as |int i| was set to that, which is a
92      * negative number. However, it requires minimum input values:
93      *
94      * CRYPTO_secure_malloc_init((size_t)1<<34, 1<<4);
95      *
96      * Which really only works on 64-bit systems, since it took 16 GB
97      * secure memory arena to trigger the problem. It naturally takes
98      * corresponding amount of available virtual and physical memory
99      * for test to be feasible/representative. Since we can't assume
100      * that every system is equipped with that much memory, the test
101      * remains disabled. If the reader of this comment really wants
102      * to make sure that infinite loop is fixed, they can enable the
103      * code below.
104      */
105 #if 0
106     /*-
107      * On Linux and BSD this test has a chance to complete in minimal
108      * time and with minimum side effects, because mlock is likely to
109      * fail because of RLIMIT_MEMLOCK, which is customarily [much]
110      * smaller than 16GB. In other words Linux and BSD users can be
111      * limited by virtual space alone...
112      */
113     if (sizeof(size_t) > 4) {
114         TEST_info("Possible infinite loop: 1<<31 limit");
115         if (TEST_true(CRYPTO_secure_malloc_init((size_t)1<<34, 1<<4) != 0))
116             TEST_true(CRYPTO_secure_malloc_done());
117     }
118 #endif
119 
120     /* this can complete - it was not really secure */
121     testresult = 1;
122 end:
123     OPENSSL_secure_free(p);
124     OPENSSL_free(q);
125     OPENSSL_secure_free(r);
126     OPENSSL_secure_free(s);
127     return testresult;
128 #else
129     TEST_info("Secure memory is *not* implemented.");
130     /* Should fail. */
131     return TEST_false(CRYPTO_secure_malloc_init(4096, 32));
132 #endif
133 }
134 
test_sec_mem_clear(void)135 static int test_sec_mem_clear(void)
136 {
137 #ifndef OPENSSL_NO_SECURE_MEMORY
138     const int size = 64;
139     unsigned char *p = NULL;
140     int i, res = 0;
141 
142     if (!TEST_true(CRYPTO_secure_malloc_init(4096, 32))
143         || !TEST_ptr(p = OPENSSL_secure_malloc(size)))
144         goto err;
145 
146     for (i = 0; i < size; i++)
147         if (!TEST_uchar_eq(p[i], 0))
148             goto err;
149 
150     for (i = 0; i < size; i++)
151         p[i] = (unsigned char)(i + ' ' + 1);
152 
153     OPENSSL_secure_free(p);
154 
155     /*
156      * A deliberate use after free here to verify that the memory has been
157      * cleared properly.  Since secure free doesn't return the memory to
158      * libc's memory pool, it technically isn't freed.  However, the header
159      * bytes have to be skipped and these consist of two pointers in the
160      * current implementation.
161      */
162     for (i = sizeof(void *) * 2; i < size; i++)
163         if (!TEST_uchar_eq(p[i], 0))
164             return 0;
165 
166     res = 1;
167     p = NULL;
168 err:
169     OPENSSL_secure_free(p);
170     CRYPTO_secure_malloc_done();
171     return res;
172 #else
173     return 1;
174 #endif
175 }
176 
setup_tests(void)177 int setup_tests(void)
178 {
179     ADD_TEST(test_sec_mem);
180     ADD_TEST(test_sec_mem_clear);
181     return 1;
182 }
183