fuzz_consensus.c (2074B)
1 /* Copyright (c) 2016-2021, 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 13 static void 14 mock_dump_desc__nodump(const char *desc, const char *type) 15 { 16 (void)desc; 17 (void)type; 18 } 19 20 static int 21 mock_router_produce_hash_final__nohash(char *digest, 22 const char *start, size_t len, 23 digest_algorithm_t alg) 24 { 25 (void)start; 26 (void)len; 27 /* we could look at start[..] */ 28 if (alg == DIGEST_SHA1) 29 memset(digest, 0x01, 20); 30 else 31 memset(digest, 0x02, 32); 32 return 0; 33 } 34 35 static int 36 mock_signed_digest_equals__yes(const uint8_t *d1, const uint8_t *d2, 37 size_t len) 38 { 39 (void) tor_memeq(d1, d2, len); 40 return 1; 41 } 42 43 int 44 fuzz_init(void) 45 { 46 disable_signature_checking(); 47 MOCK(dump_desc, mock_dump_desc__nodump); 48 MOCK(router_compute_hash_final, mock_router_produce_hash_final__nohash); 49 MOCK(signed_digest_equals, mock_signed_digest_equals__yes); 50 ed25519_init(); 51 return 0; 52 } 53 54 int 55 fuzz_cleanup(void) 56 { 57 return 0; 58 } 59 60 int 61 fuzz_main(const uint8_t *data, size_t sz) 62 { 63 networkstatus_t *ns; 64 const char *eos = NULL; 65 networkstatus_type_t tp = NS_TYPE_CONSENSUS; 66 if (tor_memstr(data, MIN(sz, 1024), "tus vote")) 67 tp = NS_TYPE_VOTE; 68 const char *what = (tp == NS_TYPE_CONSENSUS) ? "consensus" : "vote"; 69 ns = networkstatus_parse_vote_from_string((const char *)data, 70 sz, 71 &eos, 72 tp); 73 if (ns) { 74 log_debug(LD_GENERAL, "Parsing as %s okay", what); 75 networkstatus_vote_free(ns); 76 } else { 77 log_debug(LD_GENERAL, "Parsing as %s failed", what); 78 } 79 80 return 0; 81 }