fuzz_consensus.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright (c) 2016-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define SIGCOMMON_PRIVATE
  4. #include "core/or/or.h"
  5. #include "feature/dirparse/ns_parse.h"
  6. #include "feature/dirparse/sigcommon.h"
  7. #include "feature/dirparse/unparseable.h"
  8. #include "feature/nodelist/networkstatus.h"
  9. #include "lib/crypt_ops/crypto_ed25519.h"
  10. #include "feature/nodelist/networkstatus_st.h"
  11. #include "test/fuzz/fuzzing.h"
  12. static void
  13. mock_dump_desc__nodump(const char *desc, const char *type)
  14. {
  15. (void)desc;
  16. (void)type;
  17. }
  18. static int
  19. mock_router_produce_hash_final__nohash(char *digest,
  20. const char *start, size_t len,
  21. digest_algorithm_t alg)
  22. {
  23. (void)start;
  24. (void)len;
  25. /* we could look at start[..] */
  26. if (alg == DIGEST_SHA1)
  27. memset(digest, 0x01, 20);
  28. else
  29. memset(digest, 0x02, 32);
  30. return 0;
  31. }
  32. static int
  33. mock_signed_digest_equals__yes(const uint8_t *d1, const uint8_t *d2,
  34. size_t len)
  35. {
  36. (void) tor_memeq(d1, d2, len);
  37. return 1;
  38. }
  39. int
  40. fuzz_init(void)
  41. {
  42. disable_signature_checking();
  43. MOCK(dump_desc, mock_dump_desc__nodump);
  44. MOCK(router_compute_hash_final, mock_router_produce_hash_final__nohash);
  45. MOCK(signed_digest_equals, mock_signed_digest_equals__yes);
  46. ed25519_init();
  47. return 0;
  48. }
  49. int
  50. fuzz_cleanup(void)
  51. {
  52. return 0;
  53. }
  54. int
  55. fuzz_main(const uint8_t *data, size_t sz)
  56. {
  57. networkstatus_t *ns;
  58. const char *eos = NULL;
  59. networkstatus_type_t tp = NS_TYPE_CONSENSUS;
  60. if (tor_memstr(data, MIN(sz, 1024), "tus vote"))
  61. tp = NS_TYPE_VOTE;
  62. const char *what = (tp == NS_TYPE_CONSENSUS) ? "consensus" : "vote";
  63. ns = networkstatus_parse_vote_from_string((const char *)data,
  64. sz,
  65. &eos,
  66. tp);
  67. if (ns) {
  68. log_debug(LD_GENERAL, "Parsing as %s okay", what);
  69. networkstatus_vote_free(ns);
  70. } else {
  71. log_debug(LD_GENERAL, "Parsing as %s failed", what);
  72. }
  73. return 0;
  74. }