Coverage Report

Created: 2026-09-03 07:02

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.3k
#define STR_SIZE 75
24
25
static int init_daemon(FuzzedDataProvider &provider,
26
                       std::vector<void *> &allocs,
27
976
                       std::vector<std::string> &strings) {
28
26.3k
  auto alloc = [&](size_t sz) -> void * {
29
26.3k
    void *p = calloc(1, sz);
30
26.3k
    if (p) allocs.push_back(p);
31
26.3k
    return p;
32
26.3k
  };
33
34
27.3k
  auto make_string = [&](size_t max_len) -> char * {
35
27.3k
    strings.push_back(provider.ConsumeRandomLengthString(max_len));
36
27.3k
    return strings.back().data();
37
27.3k
  };
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
976
  daemon = (struct daemon *)calloc(1, sizeof(struct daemon));
42
976
  if (!daemon) return -1;
43
44
976
  daemon->max_ttl = provider.ConsumeIntegral<int>();
45
976
  daemon->neg_ttl = provider.ConsumeIntegral<int>();
46
976
  daemon->local_ttl = provider.ConsumeIntegral<int>();
47
976
  daemon->min_cache_ttl = provider.ConsumeIntegral<int>();
48
49
976
  daemon->namebuff = make_string(MAXDNAME);
50
51
  // daemon->naptr
52
976
  struct naptr *naptr_ptr = (struct naptr *)alloc(sizeof(struct naptr));
53
976
  if (!naptr_ptr) return -1;
54
976
  naptr_ptr->name = make_string(STR_SIZE);
55
976
  naptr_ptr->replace = make_string(STR_SIZE);
56
976
  naptr_ptr->regexp = make_string(STR_SIZE);
57
976
  naptr_ptr->services = make_string(STR_SIZE);
58
976
  naptr_ptr->flags = make_string(STR_SIZE);
59
976
  daemon->naptr = naptr_ptr;
60
61
  // daemon->int_names
62
976
  struct interface_name *int_namses =
63
976
      (struct interface_name *)alloc(sizeof(struct interface_name));
64
976
  if (!int_namses) return -1;
65
976
  int_namses->name = make_string(STR_SIZE);
66
976
  int_namses->intr = make_string(STR_SIZE);
67
68
976
  struct addrlist *d_addrlist = (struct addrlist *)alloc(sizeof(struct addrlist));
69
976
  if (!d_addrlist) return -1;
70
976
  d_addrlist->flags = provider.ConsumeIntegral<int>();
71
976
  d_addrlist->prefixlen = provider.ConsumeIntegral<int>();
72
976
  int_namses->addr = d_addrlist;
73
976
  daemon->int_names = int_namses;
74
75
  // daemon->addrbuf
76
976
  char *adbuf = (char *)alloc(200);
77
976
  if (!adbuf) return -1;
78
976
  daemon->addrbuff = adbuf;
79
80
  // daemon->auth_zones
81
976
  struct auth_zone *d_az = (struct auth_zone *)alloc(sizeof(struct auth_zone));
82
976
  if (!d_az) return -1;
83
976
  d_az->domain = make_string(STR_SIZE);
84
976
  daemon->auth_zones = d_az;
85
86
  // daemon->mxnames
87
976
  struct mx_srv_record *mx_srv_rec =
88
976
      (struct mx_srv_record *)alloc(sizeof(struct mx_srv_record));
89
976
  if (!mx_srv_rec) return -1;
90
976
  mx_srv_rec->next = daemon->mxnames;
91
976
  daemon->mxnames = mx_srv_rec;
92
976
  mx_srv_rec->name = make_string(STR_SIZE);
93
976
  mx_srv_rec->target = make_string(STR_SIZE);
94
976
  mx_srv_rec->issrv = provider.ConsumeIntegral<int>();
95
976
  mx_srv_rec->weight = provider.ConsumeIntegral<int>();
96
976
  mx_srv_rec->priority = provider.ConsumeIntegral<int>();
97
976
  mx_srv_rec->srvport = provider.ConsumeIntegral<int>();
98
99
  // daemon->txt
100
976
  struct txt_record *txt_record =
101
976
      (struct txt_record *)alloc(sizeof(struct txt_record));
102
976
  if (!txt_record) return -1;
103
976
  txt_record->name = make_string(STR_SIZE);
104
976
  txt_record->txt = (unsigned char *)make_string(STR_SIZE);
105
976
  txt_record->class2 = provider.ConsumeIntegralInRange<short>(0, 9);
106
976
  daemon->txt = txt_record;
107
108
  // daemon->rr
109
976
  struct txt_record *rr_record =
110
976
      (struct txt_record *)alloc(sizeof(struct txt_record));
111
976
  if (!rr_record) return -1;
112
976
  rr_record->name = make_string(STR_SIZE);
113
976
  rr_record->txt = (unsigned char *)make_string(STR_SIZE);
114
976
  rr_record->class2 = provider.ConsumeIntegralInRange<short>(0, 9);
115
976
  daemon->rr = rr_record;
116
117
  // daemon->relay4
118
976
  struct dhcp_relay *dr = (struct dhcp_relay *)alloc(sizeof(struct dhcp_relay));
119
976
  if (!dr) return -1;
120
976
  dr->interface = make_string(STR_SIZE);
121
976
  dr->next = NULL;
122
976
  daemon->relay4 = dr;
123
124
  // daemon->bridges
125
976
  struct dhcp_bridge *db = (struct dhcp_bridge *)alloc(sizeof(struct dhcp_bridge));
126
976
  if (!db) return -1;
127
976
  {
128
976
    std::string iface_str = provider.ConsumeRandomLengthString(IF_NAMESIZE - 1);
129
976
    memcpy(db->iface, iface_str.c_str(), iface_str.size() + 1);
130
976
  }
131
132
976
  struct dhcp_bridge *db_alias =
133
976
      (struct dhcp_bridge *)alloc(sizeof(struct dhcp_bridge));
134
976
  if (!db_alias) return -1;
135
976
  {
136
976
    std::string alias_str = provider.ConsumeRandomLengthString(IF_NAMESIZE - 1);
137
976
    memcpy(db_alias->iface, alias_str.c_str(), alias_str.size() + 1);
138
976
  }
139
976
  db->alias = db_alias;
140
976
  daemon->bridges = db;
141
142
  // daemon->if_names, if_addrs, if_except, dhcp_except, authinterface
143
4.88k
  auto make_iname = [&]() -> struct iname * {
144
4.88k
    struct iname *in = (struct iname *)alloc(sizeof(struct iname));
145
4.88k
    if (!in) return nullptr;
146
4.88k
    in->name = make_string(STR_SIZE);
147
4.88k
    in->next = NULL;
148
4.88k
    return in;
149
4.88k
  };
150
151
976
  daemon->if_names = make_iname();
152
976
  daemon->if_addrs = make_iname();
153
976
  daemon->if_except = make_iname();
154
976
  daemon->dhcp_except = make_iname();
155
976
  daemon->authinterface = make_iname();
156
976
  if (!daemon->if_names || !daemon->if_addrs || !daemon->if_except ||
157
976
      !daemon->dhcp_except || !daemon->authinterface)
158
0
    return -1;
159
160
  // daemon->cnames
161
976
  struct cname *cn = (struct cname *)alloc(sizeof(struct cname));
162
976
  if (!cn) return -1;
163
976
  cn->alias = make_string(STR_SIZE);
164
976
  cn->target = make_string(STR_SIZE);
165
976
  daemon->cnames = cn;
166
167
  // daemon->ptr
168
976
  struct ptr_record *ptr = (struct ptr_record *)alloc(sizeof(struct ptr_record));
169
976
  if (!ptr) return -1;
170
976
  ptr->name = make_string(STR_SIZE);
171
976
  daemon->ptr = ptr;
172
173
  // daemon->dhcp
174
976
  struct dhcp_context *dhcp_c =
175
976
      (struct dhcp_context *)alloc(sizeof(struct dhcp_context));
176
976
  if (!dhcp_c) return -1;
177
976
  dhcp_c->next = NULL;
178
976
  dhcp_c->current = NULL;
179
976
  struct dhcp_netid *dhcp_c_netid =
180
976
      (struct dhcp_netid *)alloc(sizeof(struct dhcp_netid));
181
976
  if (!dhcp_c_netid) return -1;
182
976
  dhcp_c_netid->net = make_string(STR_SIZE);
183
976
  dhcp_c->filter = dhcp_c_netid;
184
976
  dhcp_c->template_interface = make_string(STR_SIZE);
185
976
  daemon->dhcp = dhcp_c;
186
187
  // daemon->dhcp6
188
976
  struct dhcp_context *dhcp6_c =
189
976
      (struct dhcp_context *)alloc(sizeof(struct dhcp_context));
190
976
  if (!dhcp6_c) return -1;
191
976
  dhcp6_c->next = NULL;
192
976
  dhcp6_c->current = NULL;
193
976
  struct dhcp_netid *dhcp6_c_netid =
194
976
      (struct dhcp_netid *)alloc(sizeof(struct dhcp_netid));
195
976
  if (!dhcp6_c_netid) return -1;
196
976
  dhcp6_c_netid->net = make_string(STR_SIZE);
197
976
  dhcp6_c->filter = dhcp6_c_netid;
198
976
  dhcp6_c->template_interface = make_string(STR_SIZE);
199
976
  daemon->dhcp6 = dhcp6_c;
200
201
976
  daemon->doing_dhcp6 = 1;
202
203
  // daemon->dhcp_buffs
204
976
  daemon->dhcp_buff = (char *)alloc(DHCP_BUFF_SZ);
205
976
  daemon->dhcp_buff2 = (char *)alloc(DHCP_BUFF_SZ);
206
976
  daemon->dhcp_buff3 = (char *)alloc(DHCP_BUFF_SZ);
207
976
  if (!daemon->dhcp_buff || !daemon->dhcp_buff2 || !daemon->dhcp_buff3)
208
0
    return -1;
209
210
  // daemon->ignore_addr
211
976
  struct bogus_addr *bb = (struct bogus_addr *)alloc(sizeof(struct bogus_addr));
212
976
  if (!bb) return -1;
213
976
  daemon->ignore_addr = bb;
214
215
  // daemon->doctors
216
976
  struct doctor *doctors = (struct doctor *)alloc(sizeof(struct doctor));
217
976
  if (!doctors) return -1;
218
976
  doctors->next = NULL;
219
976
  daemon->doctors = doctors;
220
221
976
  return 0;
222
976
}
223
224
976
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
225
976
  FuzzedDataProvider provider(data, size);
226
227
976
  std::vector<void *> allocs;
228
976
  std::vector<std::string> strings;
229
976
  strings.reserve(40);
230
231
976
  int succ = init_daemon(provider, allocs, strings);
232
976
  if (succ == 0) {
233
976
    std::string t1_str = provider.ConsumeRandomLengthString(MAXDNAME);
234
976
    std::string t2_str = provider.ConsumeRandomLengthString(MAXDNAME);
235
976
    if (t1_str.empty() || t2_str.empty())
236
426
      goto cleanup;
237
238
550
    char *t1 = t1_str.data();
239
550
    char *t2 = t2_str.data();
240
241
    // Util logic
242
550
    hostname_isequal(t1, t2);
243
244
550
    legal_hostname(t1);
245
550
    char *tmp = canonicalise(t2, NULL);
246
550
    if (tmp != NULL) {
247
263
      free(tmp);
248
263
    }
249
250
550
    char *tmp_out = (char *)malloc(30);
251
550
    if (tmp_out) {
252
550
      int mac_type;
253
550
      parse_hex(t1, (unsigned char *)tmp_out, 30, NULL, NULL);
254
550
      parse_hex(t1, (unsigned char *)tmp_out, 30, NULL, &mac_type);
255
550
      free(tmp_out);
256
550
    }
257
258
550
    wildcard_match(t1, t2);
259
550
    if (t1_str.size() < t2_str.size()) {
260
127
      wildcard_matchn(t1, t2, t1_str.size());
261
423
    } else {
262
423
      wildcard_matchn(t1, t2, t2_str.size());
263
423
    }
264
550
    hostname_issubdomain(t1, t2);
265
266
550
    union all_addr addr1;
267
550
    memset(&addr1, 0, sizeof(union all_addr));
268
550
    is_name_synthetic(0, t1, &addr1);
269
550
  }
270
271
976
cleanup:
272
976
  for (void *p : allocs)
273
26.3k
    free(p);
274
  // Free daemon last since dnsmasq's free_real() dereferences it.
275
976
  free(daemon);
276
976
  daemon = NULL;
277
278
976
  return 0;
279
976
}