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