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