fuzzing_common.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CRYPTO_ED25519_PRIVATE
  4. #include "orconfig.h"
  5. #include "or.h"
  6. #include "backtrace.h"
  7. #include "config.h"
  8. #include "fuzzing.h"
  9. #include "crypto.h"
  10. #include "crypto_ed25519.h"
  11. extern const char tor_git_revision[];
  12. const char tor_git_revision[] = "";
  13. static or_options_t *mock_options = NULL;
  14. static const or_options_t *
  15. mock_get_options(void)
  16. {
  17. return mock_options;
  18. }
  19. static int
  20. mock_crypto_pk_public_checksig__nocheck(const crypto_pk_t *env, char *to,
  21. size_t tolen,
  22. const char *from, size_t fromlen)
  23. {
  24. tor_assert(env && to && from);
  25. (void)fromlen;
  26. /* We could look at from[0..fromlen-1] ... */
  27. tor_assert(tolen >= crypto_pk_keysize(env));
  28. memset(to, 0x01, 20);
  29. return 20;
  30. }
  31. static int
  32. mock_crypto_pk_public_checksig_digest__nocheck(crypto_pk_t *env,
  33. const char *data,
  34. size_t datalen,
  35. const char *sig,
  36. size_t siglen)
  37. {
  38. tor_assert(env && data && sig);
  39. (void)datalen;
  40. (void)siglen;
  41. /* We could look at data[..] and sig[..] */
  42. return 0;
  43. }
  44. static int
  45. mock_ed25519_checksig__nocheck(const ed25519_signature_t *signature,
  46. const uint8_t *msg, size_t len,
  47. const ed25519_public_key_t *pubkey)
  48. {
  49. tor_assert(signature && msg && pubkey);
  50. /* We could look at msg[0..len-1] ... */
  51. (void)len;
  52. return 0;
  53. }
  54. static int
  55. mock_ed25519_checksig_batch__nocheck(int *okay_out,
  56. const ed25519_checkable_t *checkable,
  57. int n_checkable)
  58. {
  59. tor_assert(checkable);
  60. int i;
  61. for (i = 0; i < n_checkable; ++i) {
  62. /* We could look at messages and signatures XXX */
  63. tor_assert(checkable[i].pubkey);
  64. tor_assert(checkable[i].msg);
  65. if (okay_out)
  66. okay_out[i] = 1;
  67. }
  68. return 0;
  69. }
  70. static int
  71. mock_ed25519_impl_spot_check__nocheck(void)
  72. {
  73. return 0;
  74. }
  75. void
  76. disable_signature_checking(void)
  77. {
  78. MOCK(crypto_pk_public_checksig,
  79. mock_crypto_pk_public_checksig__nocheck);
  80. MOCK(crypto_pk_public_checksig_digest,
  81. mock_crypto_pk_public_checksig_digest__nocheck);
  82. MOCK(ed25519_checksig, mock_ed25519_checksig__nocheck);
  83. MOCK(ed25519_checksig_batch, mock_ed25519_checksig_batch__nocheck);
  84. MOCK(ed25519_impl_spot_check, mock_ed25519_impl_spot_check__nocheck);
  85. }
  86. static void
  87. global_init(void)
  88. {
  89. tor_threads_init();
  90. tor_compress_init();
  91. {
  92. struct sipkey sipkey = { 1337, 7331 };
  93. siphash_set_global_key(&sipkey);
  94. }
  95. /* Initialise logging first */
  96. init_logging(1);
  97. configure_backtrace_handler(get_version());
  98. /* set up the options. */
  99. mock_options = tor_malloc(sizeof(or_options_t));
  100. MOCK(get_options, mock_get_options);
  101. /* Make BUG() and nonfatal asserts crash */
  102. tor_set_failed_assertion_callback(abort);
  103. }
  104. #ifdef LLVM_FUZZ
  105. int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
  106. int
  107. LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
  108. {
  109. static int initialized = 0;
  110. if (!initialized) {
  111. global_init();
  112. if (fuzz_init() < 0)
  113. abort();
  114. initialized = 1;
  115. }
  116. return fuzz_main(Data, Size);
  117. }
  118. #else /* Not LLVM_FUZZ, so AFL. */
  119. int
  120. main(int argc, char **argv)
  121. {
  122. size_t size;
  123. global_init();
  124. /* Disable logging by default to speed up fuzzing. */
  125. int loglevel = LOG_ERR;
  126. for (int i = 1; i < argc; ++i) {
  127. if (!strcmp(argv[i], "--warn")) {
  128. loglevel = LOG_WARN;
  129. } else if (!strcmp(argv[i], "--notice")) {
  130. loglevel = LOG_NOTICE;
  131. } else if (!strcmp(argv[i], "--info")) {
  132. loglevel = LOG_INFO;
  133. } else if (!strcmp(argv[i], "--debug")) {
  134. loglevel = LOG_DEBUG;
  135. }
  136. }
  137. {
  138. log_severity_list_t s;
  139. memset(&s, 0, sizeof(s));
  140. set_log_severity_config(loglevel, LOG_ERR, &s);
  141. /* ALWAYS log bug warnings. */
  142. s.masks[LOG_WARN-LOG_ERR] |= LD_BUG;
  143. add_stream_log(&s, "", fileno(stdout));
  144. }
  145. if (fuzz_init() < 0)
  146. abort();
  147. #ifdef __AFL_HAVE_MANUAL_CONTROL
  148. /* Tell AFL to pause and fork here - ignored if not using AFL */
  149. __AFL_INIT();
  150. #endif
  151. #define MAX_FUZZ_SIZE (128*1024)
  152. char *input = read_file_to_str_until_eof(0, MAX_FUZZ_SIZE, &size);
  153. tor_assert(input);
  154. char *raw = tor_memdup(input, size); /* Because input is nul-terminated */
  155. tor_free(input);
  156. fuzz_main((const uint8_t*)raw, size);
  157. tor_free(raw);
  158. if (fuzz_cleanup() < 0)
  159. abort();
  160. tor_free(mock_options);
  161. UNMOCK(get_options);
  162. return 0;
  163. }
  164. #endif