Swap the check in ssl3_write_pending to avoid using
[openssl.git] / test / secmemtest.c
1 /*
2  * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
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  */
9
10 #include <openssl/crypto.h>
11
12 #include "testutil.h"
13
14 static int test_sec_mem(void)
15 {
16 #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
17     int testresult = 0;
18     char *p = NULL, *q = NULL, *r = NULL, *s = NULL;
19
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;
25     r = OPENSSL_secure_malloc(20);
26     /* r = non-secure 20, s = non-secure 20 */
27     if (!TEST_ptr(r)
28         || !TEST_true(CRYPTO_secure_malloc_init(4096, 32))
29         || !TEST_false(CRYPTO_secure_allocated(r)))
30         goto end;
31     p = OPENSSL_secure_malloc(20);
32     if (!TEST_ptr(p)
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))
37         goto end;
38     q = OPENSSL_malloc(20);
39     if (!TEST_ptr(q))
40         goto end;
41     /* r = non-secure 20, p = secure 20, q = non-secure 20, s = non-secure 20 */
42     if (!TEST_false(CRYPTO_secure_allocated(q)))
43         goto end;
44     OPENSSL_secure_clear_free(s, 20);
45     s = OPENSSL_secure_malloc(20);
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;
52     OPENSSL_secure_clear_free(p, 20);
53     p = NULL;
54     /* 20 secure -> 32 bytes allocated */
55     if (!TEST_size_t_eq(CRYPTO_secure_used(), 32))
56         goto end;
57     OPENSSL_free(q);
58     q = NULL;
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()))
62         goto end;
63     OPENSSL_secure_free(s);
64     s = NULL;
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()))
69         goto end;
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
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());
84         goto end;
85     }
86
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      *
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.
102      */
103 # if 0
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      */
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());
115     }
116 # endif
117
118     /* this can complete - it was not really secure */
119     testresult = 1;
120  end:
121     OPENSSL_secure_free(p);
122     OPENSSL_free(q);
123     OPENSSL_secure_free(r);
124     OPENSSL_secure_free(s);
125     return testresult;
126 #else
127     /* Should fail. */
128     return TEST_false(CRYPTO_secure_malloc_init(4096, 32));
129 #endif
130 }
131
132 int setup_tests(void)
133 {
134     ADD_TEST(test_sec_mem);
135     return 1;
136 }