test-memwipe.c 5.4 KB

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