Coverage Report

Created: 2026-09-14 06:32

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