Coverage Report

Created: 2026-07-16 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fuzz_util.cc
Line
Count
Source
1
/* Copyright 2026 Google LLC
2
Licensed under the Apache License, Version 2.0 (the "License");
3
you may not use this file except in compliance with the License.
4
You may obtain a copy of the License at
5
      http://www.apache.org/licenses/LICENSE-2.0
6
Unless required by applicable law or agreed to in writing, software
7
distributed under the License is distributed on an "AS IS" BASIS,
8
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
See the License for the specific language governing permissions and
10
limitations under the License.
11
*/
12
13
extern "C" {
14
#include "dnsmasq.h"
15
}
16
17
#include <fuzzer/FuzzedDataProvider.h>
18
#include <cstdlib>
19
#include <cstring>
20
#include <string>
21
#include <vector>
22
23
26.5k
#define STR_SIZE 75
24
25
static int init_daemon(FuzzedDataProvider &provider,
26
                       std::vector<void *> &allocs,
27
982
                       std::vector<std::string> &strings) {
28
26.5k
  auto alloc = [&](size_t sz) -> void * {
29
26.5k
    void *p = calloc(1, sz);
30
26.5k
    if (p) allocs.push_back(p);
31
26.5k
    return p;
32
26.5k
  };
33
34
27.4k
  auto make_string = [&](size_t max_len) -> char * {
35
27.4k
    strings.push_back(provider.ConsumeRandomLengthString(max_len));
36
27.4k
    return strings.back().data();
37
27.4k
  };
38
39
  // Allocate daemon separately (not via alloc) so it can be freed last.
40
  // dnsmasq's free_real() dereferences daemon for metrics tracking.
41
982
  daemon = (struct daemon *)calloc(1, sizeof(struct daemon));
42
982
  if (!daemon) return -1;
43
44
982
  daemon->max_ttl = provider.ConsumeIntegral<int>();
45
982
  daemon->neg_ttl = provider.ConsumeIntegral<int>();
46
982
  daemon->local_ttl = provider.ConsumeIntegral<int>();
47
982
  daemon->min_cache_ttl = provider.ConsumeIntegral<int>();
48
49
982
  daemon->namebuff = make_string(MAXDNAME);
50
51
  // daemon->naptr
52
982
  struct naptr *naptr_ptr = (struct naptr *)alloc(sizeof(struct naptr));
53
982
  if (!naptr_ptr) return -1;
54
982
  naptr_ptr->name = make_string(STR_SIZE);
55
982
  naptr_ptr->replace = make_string(STR_SIZE);
56
982
  naptr_ptr->regexp = make_string(STR_SIZE);
57
982
  naptr_ptr->services = make_string(STR_SIZE);
58
982
  naptr_ptr->flags = make_string(STR_SIZE);
59
982
  daemon->naptr = naptr_ptr;
60
61
  // daemon->int_names
62
982
  struct interface_name *int_namses =
63
982
      (struct interface_name *)alloc(sizeof(struct interface_name));
64
982
  if (!int_namses) return -1;
65
982
  int_namses->name = make_string(STR_SIZE);
66
982
  int_namses->intr = make_string(STR_SIZE);
67
68
982
  struct addrlist *d_addrlist = (struct addrlist *)alloc(sizeof(struct addrlist));
69
982
  if (!d_addrlist) return -1;
70
982
  d_addrlist->flags = provider.ConsumeIntegral<int>();
71
982
  d_addrlist->prefixlen = provider.ConsumeIntegral<int>();
72
982
  int_namses->addr = d_addrlist;
73
982
  daemon->int_names = int_namses;
74
75
  // daemon->addrbuf
76
982
  char *adbuf = (char *)alloc(200);
77
982
  if (!adbuf) return -1;
78
982
  daemon->addrbuff = adbuf;
79
80
  // daemon->auth_zones
81
982
  struct auth_zone *d_az = (struct auth_zone *)alloc(sizeof(struct auth_zone));
82
982
  if (!d_az) return -1;
83
982
  d_az->domain = make_string(STR_SIZE);
84
982
  daemon->auth_zones = d_az;
85
86
  // daemon->mxnames
87
982
  struct mx_srv_record *mx_srv_rec =
88
982
      (struct mx_srv_record *)alloc(sizeof(struct mx_srv_record));
89
982
  if (!mx_srv_rec) return -1;
90
982
  mx_srv_rec->next = daemon->mxnames;
91
982
  daemon->mxnames = mx_srv_rec;
92
982
  mx_srv_rec->name = make_string(STR_SIZE);
93
982
  mx_srv_rec->target = make_string(STR_SIZE);
94
982
  mx_srv_rec->issrv = provider.ConsumeIntegral<int>();
95
982
  mx_srv_rec->weight = provider.ConsumeIntegral<int>();
96
982
  mx_srv_rec->priority = provider.ConsumeIntegral<int>();
97
982
  mx_srv_rec->srvport = provider.ConsumeIntegral<int>();
98
99
  // daemon->txt
100
982
  struct txt_record *txt_record =
101
982
      (struct txt_record *)alloc(sizeof(struct txt_record));
102
982
  if (!txt_record) return -1;
103
982
  txt_record->name = make_string(STR_SIZE);
104
982
  txt_record->txt = (unsigned char *)make_string(STR_SIZE);
105
982
  txt_record->class2 = provider.ConsumeIntegralInRange<short>(0, 9);
106
982
  daemon->txt = txt_record;
107
108
  // daemon->rr
109
982
  struct txt_record *rr_record =
110
982
      (struct txt_record *)alloc(sizeof(struct txt_record));
111
982
  if (!rr_record) return -1;
112
982
  rr_record->name = make_string(STR_SIZE);
113
982
  rr_record->txt = (unsigned char *)make_string(STR_SIZE);
114
982
  rr_record->class2 = provider.ConsumeIntegralInRange<short>(0, 9);
115
982
  daemon->rr = rr_record;
116
117
  // daemon->relay4
118
982
  struct dhcp_relay *dr = (struct dhcp_relay *)alloc(sizeof(struct dhcp_relay));
119
982
  if (!dr) return -1;
120
982
  dr->interface = make_string(STR_SIZE);
121
982
  dr->next = NULL;
122
982
  daemon->relay4 = dr;
123
124
  // daemon->bridges
125
982
  struct dhcp_bridge *db = (struct dhcp_bridge *)alloc(sizeof(struct dhcp_bridge));
126
982
  if (!db) return -1;
127
982
  {
128
982
    std::string iface_str = provider.ConsumeRandomLengthString(IF_NAMESIZE - 1);
129
982
    memcpy(db->iface, iface_str.c_str(), iface_str.size() + 1);
130
982
  }
131
132
982
  struct dhcp_bridge *db_alias =
133
982
      (struct dhcp_bridge *)alloc(sizeof(struct dhcp_bridge));
134
982
  if (!db_alias) return -1;
135
982
  {
136
982
    std::string alias_str = provider.ConsumeRandomLengthString(IF_NAMESIZE - 1);
137
982
    memcpy(db_alias->iface, alias_str.c_str(), alias_str.size() + 1);
138
982
  }
139
982
  db->alias = db_alias;
140
982
  daemon->bridges = db;
141
142
  // daemon->if_names, if_addrs, if_except, dhcp_except, authinterface
143
4.91k
  auto make_iname = [&]() -> struct iname * {
144
4.91k
    struct iname *in = (struct iname *)alloc(sizeof(struct iname));
145
4.91k
    if (!in) return nullptr;
146
4.91k
    in->name = make_string(STR_SIZE);
147
4.91k
    in->next = NULL;
148
4.91k
    return in;
149
4.91k
  };
150
151
982
  daemon->if_names = make_iname();
152
982
  daemon->if_addrs = make_iname();
153
982
  daemon->if_except = make_iname();
154
982
  daemon->dhcp_except = make_iname();
155
982
  daemon->authinterface = make_iname();
156
982
  if (!daemon->if_names || !daemon->if_addrs || !daemon->if_except ||
157
982
      !daemon->dhcp_except || !daemon->authinterface)
158
0
    return -1;
159
160
  // daemon->cnames
161
982
  struct cname *cn = (struct cname *)alloc(sizeof(struct cname));
162
982
  if (!cn) return -1;
163
982
  cn->alias = make_string(STR_SIZE);
164
982
  cn->target = make_string(STR_SIZE);
165
982
  daemon->cnames = cn;
166
167
  // daemon->ptr
168
982
  struct ptr_record *ptr = (struct ptr_record *)alloc(sizeof(struct ptr_record));
169
982
  if (!ptr) return -1;
170
982
  ptr->name = make_string(STR_SIZE);
171
982
  daemon->ptr = ptr;
172
173
  // daemon->dhcp
174
982
  struct dhcp_context *dhcp_c =
175
982
      (struct dhcp_context *)alloc(sizeof(struct dhcp_context));
176
982
  if (!dhcp_c) return -1;
177
982
  dhcp_c->next = NULL;
178
982
  dhcp_c->current = NULL;
179
982
  struct dhcp_netid *dhcp_c_netid =
180
982
      (struct dhcp_netid *)alloc(sizeof(struct dhcp_netid));
181
982
  if (!dhcp_c_netid) return -1;
182
982
  dhcp_c_netid->net = make_string(STR_SIZE);
183
982
  dhcp_c->filter = dhcp_c_netid;
184
982
  dhcp_c->template_interface = make_string(STR_SIZE);
185
982
  daemon->dhcp = dhcp_c;
186
187
  // daemon->dhcp6
188
982
  struct dhcp_context *dhcp6_c =
189
982
      (struct dhcp_context *)alloc(sizeof(struct dhcp_context));
190
982
  if (!dhcp6_c) return -1;
191
982
  dhcp6_c->next = NULL;
192
982
  dhcp6_c->current = NULL;
193
982
  struct dhcp_netid *dhcp6_c_netid =
194
982
      (struct dhcp_netid *)alloc(sizeof(struct dhcp_netid));
195
982
  if (!dhcp6_c_netid) return -1;
196
982
  dhcp6_c_netid->net = make_string(STR_SIZE);
197
982
  dhcp6_c->filter = dhcp6_c_netid;
198
982
  dhcp6_c->template_interface = make_string(STR_SIZE);
199
982
  daemon->dhcp6 = dhcp6_c;
200
201
982
  daemon->doing_dhcp6 = 1;
202
203
  // daemon->dhcp_buffs
204
982
  daemon->dhcp_buff = (char *)alloc(DHCP_BUFF_SZ);
205
982
  daemon->dhcp_buff2 = (char *)alloc(DHCP_BUFF_SZ);
206
982
  daemon->dhcp_buff3 = (char *)alloc(DHCP_BUFF_SZ);
207
982
  if (!daemon->dhcp_buff || !daemon->dhcp_buff2 || !daemon->dhcp_buff3)
208
0
    return -1;
209
210
  // daemon->ignore_addr
211
982
  struct bogus_addr *bb = (struct bogus_addr *)alloc(sizeof(struct bogus_addr));
212
982
  if (!bb) return -1;
213
982
  daemon->ignore_addr = bb;
214
215
  // daemon->doctors
216
982
  struct doctor *doctors = (struct doctor *)alloc(sizeof(struct doctor));
217
982
  if (!doctors) return -1;
218
982
  doctors->next = NULL;
219
982
  daemon->doctors = doctors;
220
221
982
  return 0;
222
982
}
223
224
982
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
225
982
  FuzzedDataProvider provider(data, size);
226
227
982
  std::vector<void *> allocs;
228
982
  std::vector<std::string> strings;
229
982
  strings.reserve(40);
230
231
982
  int succ = init_daemon(provider, allocs, strings);
232
982
  if (succ == 0) {
233
982
    std::string t1_str = provider.ConsumeRandomLengthString(MAXDNAME);
234
982
    std::string t2_str = provider.ConsumeRandomLengthString(MAXDNAME);
235
982
    if (t1_str.empty() || t2_str.empty())
236
395
      goto cleanup;
237
238
587
    char *t1 = t1_str.data();
239
587
    char *t2 = t2_str.data();
240
241
    // Util logic
242
587
    hostname_isequal(t1, t2);
243
244
587
    legal_hostname(t1);
245
587
    char *tmp = canonicalise(t2, NULL);
246
587
    if (tmp != NULL) {
247
277
      free(tmp);
248
277
    }
249
250
587
    char *tmp_out = (char *)malloc(30);
251
587
    if (tmp_out) {
252
587
      int mac_type;
253
587
      parse_hex(t1, (unsigned char *)tmp_out, 30, NULL, NULL);
254
587
      parse_hex(t1, (unsigned char *)tmp_out, 30, NULL, &mac_type);
255
587
      free(tmp_out);
256
587
    }
257
258
587
    wildcard_match(t1, t2);
259
587
    if (t1_str.size() < t2_str.size()) {
260
130
      wildcard_matchn(t1, t2, t1_str.size());
261
457
    } else {
262
457
      wildcard_matchn(t1, t2, t2_str.size());
263
457
    }
264
587
    hostname_issubdomain(t1, t2);
265
266
587
    union all_addr addr1;
267
587
    memset(&addr1, 0, sizeof(union all_addr));
268
587
    is_name_synthetic(0, t1, &addr1);
269
587
  }
270
271
982
cleanup:
272
982
  for (void *p : allocs)
273
26.5k
    free(p);
274
  // Free daemon last since dnsmasq's free_real() dereferences it.
275
982
  free(daemon);
276
982
  daemon = NULL;
277
278
982
  return 0;
279
982
}