test-memwipe.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Copyright (c) 2015-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include "lib/crypt_ops/crypto_util.h"
  5. #include "lib/intmath/cmp.h"
  6. #include "lib/malloc/malloc.h"
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <stdlib.h>
  11. #ifdef HAVE_SYS_PARAM_H
  12. #include <sys/param.h>
  13. #endif
  14. static unsigned fill_a_buffer_memset(void) __attribute__((noinline));
  15. static unsigned fill_a_buffer_memwipe(void) __attribute__((noinline));
  16. static unsigned fill_a_buffer_nothing(void) __attribute__((noinline));
  17. static unsigned fill_heap_buffer_memset(void) __attribute__((noinline));
  18. static unsigned fill_heap_buffer_memwipe(void) __attribute__((noinline));
  19. static unsigned fill_heap_buffer_nothing(void) __attribute__((noinline));
  20. static unsigned check_a_buffer(void) __attribute__((noinline));
  21. extern const char *s; /* Make the linkage global */
  22. const char *s = NULL;
  23. #define BUF_LEN 2048
  24. #define FILL_BUFFER_IMPL() \
  25. unsigned int i; \
  26. unsigned sum = 0; \
  27. \
  28. /* Fill up a 1k buffer with a recognizable pattern. */ \
  29. for (i = 0; i < BUF_LEN; i += strlen(s)) { \
  30. memcpy(buf+i, s, MIN(strlen(s), BUF_LEN-i)); \
  31. } \
  32. \
  33. /* Use the buffer as input to a computation so the above can't get */ \
  34. /* optimized away. */ \
  35. for (i = 0; i < BUF_LEN; ++i) { \
  36. sum += (unsigned char)buf[i]; \
  37. }
  38. #ifdef OpenBSD
  39. /* Disable some of OpenBSD's malloc protections for this test. This helps
  40. * us do bad things, such as access freed buffers, without crashing. */
  41. const char *malloc_options="sufjj";
  42. #endif
  43. static unsigned
  44. fill_a_buffer_memset(void)
  45. {
  46. char buf[BUF_LEN];
  47. FILL_BUFFER_IMPL()
  48. memset(buf, 0, sizeof(buf));
  49. return sum;
  50. }
  51. static unsigned
  52. fill_a_buffer_memwipe(void)
  53. {
  54. char buf[BUF_LEN];
  55. FILL_BUFFER_IMPL()
  56. memwipe(buf, 0, sizeof(buf));
  57. return sum;
  58. }
  59. static unsigned
  60. fill_a_buffer_nothing(void)
  61. {
  62. char buf[BUF_LEN];
  63. FILL_BUFFER_IMPL()
  64. return sum;
  65. }
  66. static inline int
  67. vmemeq(volatile char *a, const char *b, size_t n)
  68. {
  69. while (n--) {
  70. if (*a++ != *b++)
  71. return 0;
  72. }
  73. return 1;
  74. }
  75. static unsigned
  76. check_a_buffer(void)
  77. {
  78. unsigned int i;
  79. volatile char buf[BUF_LEN];
  80. unsigned sum = 0;
  81. /* See if this buffer has the string in it.
  82. YES, THIS DOES INVOKE UNDEFINED BEHAVIOR BY READING FROM AN UNINITIALIZED
  83. BUFFER.
  84. If you know a better way to figure out whether the compiler eliminated
  85. the memset/memwipe calls or not, please let me know.
  86. */
  87. for (i = 0; i < BUF_LEN - strlen(s); ++i) {
  88. if (vmemeq(buf+i, s, strlen(s)))
  89. ++sum;
  90. }
  91. return sum;
  92. }
  93. static char *heap_buf = NULL;
  94. static unsigned
  95. fill_heap_buffer_memset(void)
  96. {
  97. char *buf = heap_buf = raw_malloc(BUF_LEN);
  98. FILL_BUFFER_IMPL()
  99. memset(buf, 0, BUF_LEN);
  100. raw_free(buf);
  101. return sum;
  102. }
  103. static unsigned
  104. fill_heap_buffer_memwipe(void)
  105. {
  106. char *buf = heap_buf = raw_malloc(BUF_LEN);
  107. FILL_BUFFER_IMPL()
  108. memwipe(buf, 0, BUF_LEN);
  109. raw_free(buf);
  110. return sum;
  111. }
  112. static unsigned
  113. fill_heap_buffer_nothing(void)
  114. {
  115. char *buf = heap_buf = raw_malloc(BUF_LEN);
  116. FILL_BUFFER_IMPL()
  117. raw_free(buf);
  118. return sum;
  119. }
  120. static unsigned
  121. check_heap_buffer(void)
  122. {
  123. unsigned int i;
  124. unsigned sum = 0;
  125. volatile char *buf = heap_buf;
  126. /* See if this buffer has the string in it.
  127. YES, THIS DOES INVOKE UNDEFINED BEHAVIOR BY READING FROM A FREED BUFFER.
  128. If you know a better way to figure out whether the compiler eliminated
  129. the memset/memwipe calls or not, please let me know.
  130. */
  131. for (i = 0; i < BUF_LEN - strlen(s); ++i) {
  132. if (vmemeq(buf+i, s, strlen(s)))
  133. ++sum;
  134. }
  135. return sum;
  136. }
  137. static struct testcase {
  138. const char *name;
  139. /* this spacing satisfies make check-spaces */
  140. unsigned
  141. (*fill_fn)(void);
  142. unsigned
  143. (*check_fn)(void);
  144. } testcases[] = {
  145. { "nil", fill_a_buffer_nothing, check_a_buffer },
  146. { "nil-heap", fill_heap_buffer_nothing, check_heap_buffer },
  147. { "memset", fill_a_buffer_memset, check_a_buffer },
  148. { "memset-heap", fill_heap_buffer_memset, check_heap_buffer },
  149. { "memwipe", fill_a_buffer_memwipe, check_a_buffer },
  150. { "memwipe-heap", fill_heap_buffer_memwipe, check_heap_buffer },
  151. { NULL, NULL, NULL }
  152. };
  153. int
  154. main(int argc, char **argv)
  155. {
  156. unsigned x, x2;
  157. int i;
  158. int working = 1;
  159. unsigned found[6];
  160. (void) argc; (void) argv;
  161. s = "squamous haberdasher gallimaufry";
  162. memset(found, 0, sizeof(found));
  163. for (i = 0; testcases[i].name; ++i) {
  164. x = testcases[i].fill_fn();
  165. found[i] = testcases[i].check_fn();
  166. x2 = fill_a_buffer_nothing();
  167. if (x != x2) {
  168. working = 0;
  169. }
  170. }
  171. if (!working || !found[0] || !found[1]) {
  172. printf("It appears that this test case may not give you reliable "
  173. "information. Sorry.\n");
  174. }
  175. if (!found[2] && !found[3]) {
  176. printf("It appears that memset is good enough on this platform. Good.\n");
  177. }
  178. if (found[4] || found[5]) {
  179. printf("ERROR: memwipe does not wipe data!\n");
  180. return 1;
  181. } else {
  182. printf("OKAY: memwipe seems to work.\n");
  183. return 0;
  184. }
  185. }