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