fuzzing_common.c 4.9 KB

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