test-memwipe.c 5.0 KB

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