fuzzing_common.c 3.9 KB

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