2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
10 #include <openssl/crypto.h>
14 static int test_sec_mem(void)
16 #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
18 char *p = NULL, *q = NULL, *r = NULL, *s = NULL;
20 s = OPENSSL_secure_malloc(20);
21 /* s = non-secure 20 */
23 || !TEST_false(CRYPTO_secure_allocated(s)))
25 r = OPENSSL_secure_malloc(20);
26 /* r = non-secure 20, s = non-secure 20 */
28 || !TEST_true(CRYPTO_secure_malloc_init(4096, 32))
29 || !TEST_false(CRYPTO_secure_allocated(r)))
31 p = OPENSSL_secure_malloc(20);
33 /* r = non-secure 20, p = secure 20, s = non-secure 20 */
34 || !TEST_true(CRYPTO_secure_allocated(p))
35 /* 20 secure -> 32-byte minimum allocation unit */
36 || !TEST_size_t_eq(CRYPTO_secure_used(), 32))
38 q = OPENSSL_malloc(20);
41 /* r = non-secure 20, p = secure 20, q = non-secure 20, s = non-secure 20 */
42 if (!TEST_false(CRYPTO_secure_allocated(q)))
44 OPENSSL_secure_clear_free(s, 20);
45 s = OPENSSL_secure_malloc(20);
47 /* r = non-secure 20, p = secure 20, q = non-secure 20, s = secure 20 */
48 || !TEST_true(CRYPTO_secure_allocated(s))
49 /* 2 * 20 secure -> 64 bytes allocated */
50 || !TEST_size_t_eq(CRYPTO_secure_used(), 64))
52 OPENSSL_secure_clear_free(p, 20);
54 /* 20 secure -> 32 bytes allocated */
55 if (!TEST_size_t_eq(CRYPTO_secure_used(), 32))
59 /* should not complete, as secure memory is still allocated */
60 if (!TEST_false(CRYPTO_secure_malloc_done())
61 || !TEST_true(CRYPTO_secure_malloc_initialized()))
63 OPENSSL_secure_free(s);
65 /* secure memory should now be 0, so done should complete */
66 if (!TEST_size_t_eq(CRYPTO_secure_used(), 0)
67 || !TEST_true(CRYPTO_secure_malloc_done())
68 || !TEST_false(CRYPTO_secure_malloc_initialized()))
71 TEST_info("Possible infinite loop: allocate more than available");
72 if (!TEST_true(CRYPTO_secure_malloc_init(32768, 16)))
74 TEST_ptr_null(OPENSSL_secure_malloc((size_t)-1));
75 TEST_true(CRYPTO_secure_malloc_done());
78 * If init fails, then initialized should be false, if not, this
79 * could cause an infinite loop secure_malloc, but we don't test it
81 if (TEST_false(CRYPTO_secure_malloc_init(16, 16)) &&
82 !TEST_false(CRYPTO_secure_malloc_initialized())) {
83 TEST_true(CRYPTO_secure_malloc_done());
88 * There was also a possible infinite loop when the number of
89 * elements was 1<<31, as |int i| was set to that, which is a
90 * negative number. However, it requires minimum input values:
92 * CRYPTO_secure_malloc_init((size_t)1<<34, (size_t)1<<4);
94 * Which really only works on 64-bit systems, since it took 16 GB
95 * secure memory arena to trigger the problem. It naturally takes
96 * corresponding amount of available virtual and physical memory
97 * for test to be feasible/representative. Since we can't assume
98 * that every system is equipped with that much memory, the test
99 * remains disabled. If the reader of this comment really wants
100 * to make sure that infinite loop is fixed, they can enable the
105 * On Linux and BSD this test has a chance to complete in minimal
106 * time and with minimum side effects, because mlock is likely to
107 * fail because of RLIMIT_MEMLOCK, which is customarily [much]
108 * smaller than 16GB. In other words Linux and BSD users can be
109 * limited by virtual space alone...
111 if (sizeof(size_t) > 4) {
112 TEST_info("Possible infinite loop: 1<<31 limit");
113 if (TEST_true(CRYPTO_secure_malloc_init((size_t)1<<34, (size_t)1<<4) != 0))
114 TEST_true(CRYPTO_secure_malloc_done());
118 /* this can complete - it was not really secure */
121 OPENSSL_secure_free(p);
123 OPENSSL_secure_free(r);
124 OPENSSL_secure_free(s);
128 return TEST_false(CRYPTO_secure_malloc_init(4096, 32));
132 static int test_sec_mem_clear(void)
134 #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
136 unsigned char *p = NULL;
139 if (!TEST_true(CRYPTO_secure_malloc_init(4096, 32))
140 || !TEST_ptr(p = OPENSSL_secure_malloc(size)))
143 for (i = 0; i < size; i++)
144 if (!TEST_uchar_eq(p[i], 0))
147 for (i = 0; i < size; i++)
148 p[i] = (unsigned char)(i + ' ' + 1);
150 OPENSSL_secure_free(p);
153 * A deliberate use after free here to verify that the memory has been
154 * cleared properly. Since secure free doesn't return the memory to
155 * libc's memory pool, it technically isn't freed. However, the header
156 * bytes have to be skipped and these consist of two pointers in the
157 * current implementation.
159 for (i = sizeof(void *) * 2; i < size; i++)
160 if (!TEST_uchar_eq(p[i], 0))
167 OPENSSL_secure_free(p);
168 CRYPTO_secure_malloc_done();
175 int setup_tests(void)
177 ADD_TEST(test_sec_mem);
178 ADD_TEST(test_sec_mem_clear);