test-memwipe.c 5.4 KB

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