test-memwipe.c 5.0 KB

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