test-memwipe.c 5.3 KB

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