test-memwipe.c 5.0 KB

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