secmemtest.c 4.7 KB

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