Address Coverity issues.
[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     r = OPENSSL_secure_malloc(20);
21     /* r = non-secure 20 */
22     if (!TEST_ptr(r)
23         || !TEST_true(CRYPTO_secure_malloc_init(4096, 32))
24         || !TEST_false(CRYPTO_secure_allocated(r)))
25         goto end;
26     p = OPENSSL_secure_malloc(20);
27     if (!TEST_ptr(p)
28         /* r = non-secure 20, p = secure 20 */
29         || !TEST_true(CRYPTO_secure_allocated(p))
30         /* 20 secure -> 32-byte minimum allocaton unit */
31         || !TEST_size_t_eq(CRYPTO_secure_used(), 32))
32         goto end;
33     q = OPENSSL_malloc(20);
34     if (!TEST_ptr(q))
35         goto end;
36     /* r = non-secure 20, p = secure 20, q = non-secure 20 */
37     if (!TEST_false(CRYPTO_secure_allocated(q)))
38         goto end;
39     s = OPENSSL_secure_malloc(20);
40     if (!TEST_ptr(s)
41         /* r = non-secure 20, p = secure 20, q = non-secure 20, s = secure 20 */
42         || !TEST_true(CRYPTO_secure_allocated(s))
43         /* 2 * 20 secure -> 64 bytes allocated */
44         || !TEST_size_t_eq(CRYPTO_secure_used(), 64))
45         goto end;
46     OPENSSL_secure_free(p);
47     p = NULL;
48     /* 20 secure -> 32 bytes allocated */
49     if (!TEST_size_t_eq(CRYPTO_secure_used(), 32))
50         goto end;
51     OPENSSL_free(q);
52     q = NULL;
53     /* should not complete, as secure memory is still allocated */
54     if (!TEST_false(CRYPTO_secure_malloc_done())
55         || !TEST_true(CRYPTO_secure_malloc_initialized()))
56         goto end;
57     OPENSSL_secure_free(s);
58     s = NULL;
59     /* secure memory should now be 0, so done should complete */
60     if (!TEST_size_t_eq(CRYPTO_secure_used(), 0)
61         || !TEST_true(CRYPTO_secure_malloc_done())
62         || !TEST_false(CRYPTO_secure_malloc_initialized()))
63         goto end;
64
65     TEST_info("Possible infinite loop: allocate more than available");
66     if (!TEST_true(CRYPTO_secure_malloc_init(32768, 16)))
67         goto end;
68     TEST_ptr_null(OPENSSL_secure_malloc((size_t)-1));
69     TEST_true(CRYPTO_secure_malloc_done());
70
71     /*
72      * If init fails, then initialized should be false, if not, this
73      * could cause an infinite loop secure_malloc, but we don't test it
74      */
75     if (TEST_false(CRYPTO_secure_malloc_init(16, 16)) &&
76         !TEST_false(CRYPTO_secure_malloc_initialized())) {
77         TEST_true(CRYPTO_secure_malloc_done());
78         goto end;
79     }
80
81     /*-
82      * There was also a possible infinite loop when the number of
83      * elements was 1<<31, as |int i| was set to that, which is a
84      * negative number. However, it requires minimum input values:
85      *
86      * CRYPTO_secure_malloc_init((size_t)1<<34, (size_t)1<<4);
87      *
88      * Which really only works on 64-bit systems, since it took 16 GB
89      * secure memory arena to trigger the problem. It naturally takes
90      * corresponding amount of available virtual and physical memory
91      * for test to be feasible/representative. Since we can't assume
92      * that every system is equipped with that much memory, the test
93      * remains disabled. If the reader of this comment really wants
94      * to make sure that infinite loop is fixed, they can enable the
95      * code below.
96      */
97 # if 0
98     /*-
99      * On Linux and BSD this test has a chance to complete in minimal
100      * time and with minimum side effects, because mlock is likely to
101      * fail because of RLIMIT_MEMLOCK, which is customarily [much]
102      * smaller than 16GB. In other words Linux and BSD users can be
103      * limited by virtual space alone...
104      */
105     if (sizeof(size_t) > 4) {
106         TEST_info("Possible infinite loop: 1<<31 limit");
107         if (TEST_true(CRYPTO_secure_malloc_init((size_t)1<<34, (size_t)1<<4) != 0))
108             TEST_true(CRYPTO_secure_malloc_done());
109     }
110 # endif
111
112     /* this can complete - it was not really secure */
113     testresult = 1;
114  end:
115     OPENSSL_secure_free(p);
116     OPENSSL_free(q);
117     OPENSSL_secure_free(r);
118     OPENSSL_secure_free(s);
119     return testresult;
120 #else
121     /* Should fail. */
122     return TEST_false(CRYPTO_secure_malloc_init(4096, 32));
123 #endif
124 }
125
126 void register_tests(void)
127 {
128     ADD_TEST(test_sec_mem);
129 }