• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * unbound-fuzzme.c - parse a packet provided on stdin (for fuzzing).
3  *
4  */
5 #include "config.h"
6 #include "util/regional.h"
7 #include "util/module.h"
8 #include "util/config_file.h"
9 #include "iterator/iterator.h"
10 #include "iterator/iter_priv.h"
11 #include "iterator/iter_scrub.h"
12 #include "util/log.h"
13 #include "util/netevent.h"
14 #include "util/alloc.h"
15 #include "sldns/sbuffer.h"
16 #include "services/cache/rrset.h"
17 
LLVMFuzzerTestOneInput(const uint8_t * buf,size_t nr)18 int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t nr) {
19   log_init("/tmp/foo", 0, NULL);
20   struct regional* reg;
21 
22   struct sldns_buffer *pkt = sldns_buffer_new(1);
23   sldns_buffer_new_frm_data(pkt, buf, nr);
24 
25   reg = regional_create();
26 
27   struct msg_parse msg;
28   struct edns_data edns;
29   memset(&msg, 0, sizeof(struct msg_parse));
30   memset(&edns, 0, sizeof(edns));
31 
32   struct query_info qinfo_out;
33   memset(&qinfo_out, 0, sizeof(struct query_info));
34   qinfo_out.qname = (unsigned char *) "\03nic\02de";
35   uint8_t *peter = (unsigned char *) "\02de";   // zonename
36   struct module_env env;
37   memset(&env, 0, sizeof(struct module_env));
38   struct config_file cfg;
39   memset(&cfg, 0, sizeof(struct config_file));
40 
41   cfg.harden_glue = 0;    // crashes now, want to remove that later
42   env.cfg = &cfg;
43   cfg.rrset_cache_slabs = HASH_DEFAULT_SLABS;
44   cfg.rrset_cache_size = HASH_DEFAULT_MAXMEM;
45 
46   struct comm_base* base = comm_base_create(0);
47   comm_base_timept(base, &env.now, &env.now_tv);
48 
49   env.alloc = malloc(sizeof(struct alloc_cache));
50   alloc_init(env.alloc, NULL, 0);
51 
52   env.rrset_cache = rrset_cache_create(env.cfg, env.alloc);
53 
54 
55   struct iter_env ie;
56   memset(&ie, 0, sizeof(struct iter_env));
57 
58   struct iter_priv priv;
59   memset(&priv, 0, sizeof(struct iter_priv));
60   ie.priv = &priv;
61 
62 
63   if (parse_packet(pkt, &msg, reg) != LDNS_RCODE_NOERROR) {
64     goto out;
65   }
66   if (parse_extract_edns(&msg, &edns, reg) != LDNS_RCODE_NOERROR) {
67     goto out;
68   }
69 
70 
71   scrub_message(pkt, &msg, &qinfo_out, peter, reg, &env, &ie);
72 
73 out:
74   rrset_cache_delete(env.rrset_cache);
75   alloc_clear(env.alloc);
76   free(env.alloc);
77   comm_base_delete(base);
78   regional_destroy(reg);
79   sldns_buffer_free(pkt);
80   return 0;
81 }
82