fuzzing_common.c 4.2 KB

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