tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

fuzz_vrs.c (2681B)


      1 /* Copyright (c) 2016-2021, The Tor Project, Inc. */
      2 /* See LICENSE for licensing information */
      3 #define NS_PARSE_PRIVATE
      4 #define NETWORKSTATUS_PRIVATE
      5 #include "core/or/or.h"
      6 #include "feature/dirauth/dirvote.h"
      7 #include "feature/dirparse/ns_parse.h"
      8 #include "feature/dirparse/unparseable.h"
      9 #include "lib/memarea/memarea.h"
     10 #include "feature/nodelist/microdesc.h"
     11 #include "feature/nodelist/networkstatus.h"
     12 
     13 #include "feature/nodelist/networkstatus_st.h"
     14 #include "feature/nodelist/vote_routerstatus_st.h"
     15 #include "lib/crypt_ops/crypto_ed25519.h"
     16 
     17 #include "test/fuzz/fuzzing.h"
     18 
     19 static void
     20 mock_dump_desc__nodump(const char *desc, const char *type)
     21 {
     22  (void)desc;
     23  (void)type;
     24 }
     25 
     26 static networkstatus_t *dummy_vote = NULL;
     27 static memarea_t *area = NULL;
     28 
     29 int
     30 fuzz_init(void)
     31 {
     32  disable_signature_checking();
     33  MOCK(dump_desc, mock_dump_desc__nodump);
     34  ed25519_init();
     35  area = memarea_new();
     36  dummy_vote = tor_malloc_zero(sizeof(*dummy_vote));
     37  dummy_vote->known_flags = smartlist_new();
     38  smartlist_split_string(dummy_vote->known_flags,
     39                         DIRVOTE_UNIVERSAL_FLAGS,
     40                         " ", 0, 0);
     41  smartlist_split_string(dummy_vote->known_flags,
     42                         DIRVOTE_OPTIONAL_FLAGS,
     43                         " ", 0, 0);
     44  smartlist_sort_strings(dummy_vote->known_flags);
     45  return 0;
     46 }
     47 
     48 int
     49 fuzz_cleanup(void)
     50 {
     51  SMARTLIST_FOREACH(dummy_vote->known_flags, char *, cp, tor_free(cp));
     52  smartlist_free(dummy_vote->known_flags);
     53  tor_free(dummy_vote);
     54  return 0;
     55 }
     56 
     57 int
     58 fuzz_main(const uint8_t *data, size_t sz)
     59 {
     60  const char *s;
     61  routerstatus_t *rs_ns = NULL, *rs_md = NULL, *rs_vote = NULL;
     62  vote_routerstatus_t *vrs = tor_malloc_zero(sizeof(*vrs));
     63  smartlist_t *tokens = smartlist_new();
     64  const char *eos = (const char *)data + sz;
     65 
     66  s = (const char *)data;
     67  rs_ns = routerstatus_parse_entry_from_string(area, &s, eos, tokens,
     68                                               NULL, NULL, 26, FLAV_NS);
     69  tor_assert(smartlist_len(tokens) == 0);
     70 
     71  s = (const char *)data;
     72  rs_md = routerstatus_parse_entry_from_string(area, &s, eos, tokens,
     73                                               NULL, NULL, 26, FLAV_MICRODESC);
     74  tor_assert(smartlist_len(tokens) == 0);
     75 
     76  s = (const char *)data;
     77  rs_vote = routerstatus_parse_entry_from_string(area, &s, eos, tokens,
     78                                              dummy_vote, vrs, 26, FLAV_NS);
     79  tor_assert(smartlist_len(tokens) == 0);
     80 
     81  log_debug(LD_GENERAL,
     82            "ns=%p, md=%p, vote=%p", rs_ns, rs_md, rs_vote);
     83 
     84  routerstatus_free(rs_md);
     85  routerstatus_free(rs_ns);
     86  vote_routerstatus_free(vrs);
     87  memarea_clear(area);
     88  smartlist_free(tokens);
     89 
     90  return 0;
     91 }