secmemtest.c 5.9 KB

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