fuzz_consensus.c 2.0 KB

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