]>
Commit | Line | Data |
---|---|---|
440e5d80 | 1 | /* |
789dfc47 | 2 | * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved. |
440e5d80 RS |
3 | * |
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 | |
8 | */ | |
74924dcb RS |
9 | |
10 | #include <openssl/crypto.h> | |
11 | ||
789dfc47 | 12 | #include "testutil.h" |
e8408681 | 13 | |
789dfc47 | 14 | static int test_sec_mem(void) |
74924dcb RS |
15 | { |
16 | #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX) | |
789dfc47 | 17 | int testresult = 0; |
e8408681 | 18 | char *p = NULL, *q = NULL, *r = NULL, *s = NULL; |
74924dcb | 19 | |
8bf2d930 BE |
20 | s = OPENSSL_secure_malloc(20); |
21 | /* s = non-secure 20 */ | |
22 | if (!TEST_ptr(s) | |
23 | || !TEST_false(CRYPTO_secure_allocated(s))) | |
24 | goto end; | |
e8408681 | 25 | r = OPENSSL_secure_malloc(20); |
8bf2d930 | 26 | /* r = non-secure 20, s = non-secure 20 */ |
789dfc47 P |
27 | if (!TEST_ptr(r) |
28 | || !TEST_true(CRYPTO_secure_malloc_init(4096, 32)) | |
29 | || !TEST_false(CRYPTO_secure_allocated(r))) | |
30 | goto end; | |
74924dcb | 31 | p = OPENSSL_secure_malloc(20); |
789dfc47 | 32 | if (!TEST_ptr(p) |
8bf2d930 | 33 | /* r = non-secure 20, p = secure 20, s = non-secure 20 */ |
789dfc47 | 34 | || !TEST_true(CRYPTO_secure_allocated(p)) |
44e69951 | 35 | /* 20 secure -> 32-byte minimum allocation unit */ |
789dfc47 P |
36 | || !TEST_size_t_eq(CRYPTO_secure_used(), 32)) |
37 | goto end; | |
74924dcb | 38 | q = OPENSSL_malloc(20); |
789dfc47 P |
39 | if (!TEST_ptr(q)) |
40 | goto end; | |
8bf2d930 | 41 | /* r = non-secure 20, p = secure 20, q = non-secure 20, s = non-secure 20 */ |
789dfc47 P |
42 | if (!TEST_false(CRYPTO_secure_allocated(q))) |
43 | goto end; | |
8bf2d930 | 44 | OPENSSL_secure_clear_free(s, 20); |
e8408681 | 45 | s = OPENSSL_secure_malloc(20); |
789dfc47 P |
46 | if (!TEST_ptr(s) |
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)) | |
51 | goto end; | |
8bf2d930 | 52 | OPENSSL_secure_clear_free(p, 20); |
789dfc47 | 53 | p = NULL; |
e8408681 | 54 | /* 20 secure -> 32 bytes allocated */ |
789dfc47 P |
55 | if (!TEST_size_t_eq(CRYPTO_secure_used(), 32)) |
56 | goto end; | |
d6b55fac | 57 | OPENSSL_free(q); |
789dfc47 | 58 | q = NULL; |
e8408681 | 59 | /* should not complete, as secure memory is still allocated */ |
789dfc47 P |
60 | if (!TEST_false(CRYPTO_secure_malloc_done()) |
61 | || !TEST_true(CRYPTO_secure_malloc_initialized())) | |
62 | goto end; | |
e8408681 | 63 | OPENSSL_secure_free(s); |
789dfc47 | 64 | s = NULL; |
e8408681 | 65 | /* secure memory should now be 0, so done should complete */ |
789dfc47 P |
66 | if (!TEST_size_t_eq(CRYPTO_secure_used(), 0) |
67 | || !TEST_true(CRYPTO_secure_malloc_done()) | |
68 | || !TEST_false(CRYPTO_secure_malloc_initialized())) | |
69 | goto end; | |
7031ddac TS |
70 | |
71 | TEST_info("Possible infinite loop: allocate more than available"); | |
72 | if (!TEST_true(CRYPTO_secure_malloc_init(32768, 16))) | |
73 | goto end; | |
74 | TEST_ptr_null(OPENSSL_secure_malloc((size_t)-1)); | |
75 | TEST_true(CRYPTO_secure_malloc_done()); | |
76 | ||
fee423bb TS |
77 | /* |
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 | |
80 | */ | |
81 | if (TEST_false(CRYPTO_secure_malloc_init(16, 16)) && | |
82 | !TEST_false(CRYPTO_secure_malloc_initialized())) { | |
83 | TEST_true(CRYPTO_secure_malloc_done()); | |
7031ddac | 84 | goto end; |
fee423bb | 85 | } |
7031ddac | 86 | |
c8e89d58 TS |
87 | /*- |
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: | |
91 | * | |
92 | * CRYPTO_secure_malloc_init((size_t)1<<34, (size_t)1<<4); | |
93 | * | |
a486561b AP |
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 | |
101 | * code below. | |
c8e89d58 TS |
102 | */ |
103 | # if 0 | |
a486561b AP |
104 | /*- |
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... | |
110 | */ | |
7031ddac TS |
111 | if (sizeof(size_t) > 4) { |
112 | TEST_info("Possible infinite loop: 1<<31 limit"); | |
c8e89d58 TS |
113 | if (TEST_true(CRYPTO_secure_malloc_init((size_t)1<<34, (size_t)1<<4) != 0)) |
114 | TEST_true(CRYPTO_secure_malloc_done()); | |
7031ddac | 115 | } |
c8e89d58 | 116 | # endif |
fee423bb | 117 | |
e8408681 | 118 | /* this can complete - it was not really secure */ |
789dfc47 P |
119 | testresult = 1; |
120 | end: | |
121 | OPENSSL_secure_free(p); | |
122 | OPENSSL_free(q); | |
e8408681 | 123 | OPENSSL_secure_free(r); |
789dfc47 P |
124 | OPENSSL_secure_free(s); |
125 | return testresult; | |
74924dcb RS |
126 | #else |
127 | /* Should fail. */ | |
789dfc47 | 128 | return TEST_false(CRYPTO_secure_malloc_init(4096, 32)); |
74924dcb | 129 | #endif |
789dfc47 P |
130 | } |
131 | ||
ad887416 | 132 | int setup_tests(void) |
789dfc47 P |
133 | { |
134 | ADD_TEST(test_sec_mem); | |
ad887416 | 135 | return 1; |
74924dcb | 136 | } |