Coverage Report

Created: 2026-12-01 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mongoose/test/fuzz.c
Line
Count
Source
1
// https://llvm.org/docs/LibFuzzer.html
2
3
#define MG_ENABLE_SOCKET 0
4
#define MG_ENABLE_LOG 0
5
#define MG_ENABLE_LINES 1
6
#define MG_ENABLE_TCPIP 1
7
6.10k
#define MG_IO_SIZE (32 * 1024 * 1024)  // Big IO size for fast resizes
8
9
#include "mongoose.c"
10
11
#include "driver_mock.c"
12
13
#ifdef __cplusplus
14
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *, size_t);
15
#else
16
int LLVMFuzzerTestOneInput(const uint8_t *, size_t);
17
#endif
18
19
603k
static void fn(struct mg_connection *c, int ev, void *ev_data) {
20
603k
  struct mg_http_serve_opts opts = {.root_dir = "."};
21
603k
  if (ev == MG_EV_HTTP_MSG) {
22
285k
    mg_http_serve_dir(c, (struct mg_http_message *) ev_data, &opts);
23
285k
  }
24
603k
}
25
26
// deduplicate layer-3 mocking among layer-2s
27
struct fuzz_l3_state {
28
  size_t ip_proto, udp_port;
29
  size_t ip6_next, icmp6_type;
30
};
31
32
148
static void fuzz_ip4(void *buf, size_t len, struct fuzz_l3_state *st) {
33
148
  if (len >= sizeof(struct ip)) {
34
148
    uint8_t ip_protos[] = {1, 6, 17}; // ICMP, TCP, UDP
35
148
    struct ip *ip4 = (struct ip *) buf;
36
148
    ip4->ver = (uint8_t) ((ip4->ver & ~0xf0) | (4 << 4));
37
    // send all handled IP protos, then 2 random ones
38
148
    if (st->ip_proto >= (sizeof(ip_protos) / sizeof(ip_protos[0]) + 2)) st->ip_proto = 0;
39
148
    if (st->ip_proto < (sizeof(ip_protos) / sizeof(ip_protos[0]))) ip4->proto = ip_protos[st->ip_proto++];
40
148
    if (ip4->proto == 1) { // ICMP
41
124
    } else if (ip4->proto == 6) { // TCP
42
122
    } else if (ip4->proto == 17 && len >= sizeof(struct ip) + sizeof(struct udp)) { // UDP
43
25
      uint16_t udp_ports[] = {67, 68}; // DHCP server and client
44
25
      struct udp *udp = (struct udp *) (ip4 + 1);
45
      // send all handled UDP ports, then 2 random ones
46
25
      if (st->udp_port >= (sizeof(udp_ports) / sizeof(udp_ports[0]) + 2)) st->udp_port = 0;
47
25
      if (st->udp_port < (sizeof(udp_ports) / sizeof(udp_ports[0]))) udp->dport = mg_htons(udp_ports[st->udp_port++]);
48
25
    }
49
148
  }
50
148
}
51
52
53
static void fuzz_ip6(void *buf, size_t len, struct fuzz_l3_state *st) {
53
53
  if (len >= sizeof(struct ip6)) {
54
53
    uint8_t ip6_protos[] = {6, 17, 58}; // TCP, UDP, ICMPv6
55
53
    struct ip6 *ip6 = (struct ip6 *) buf;
56
53
    ip6->ver = (uint8_t) ((ip6->ver & ~0xf0) | (6 << 4));
57
    // send all handled IPv6 "next headers", then 2 random ones
58
53
    if (st->ip6_next >= (sizeof(ip6_protos) / sizeof(ip6_protos[0]) + 2)) st->ip6_next = 0;
59
53
    if (st->ip6_next < (sizeof(ip6_protos) / sizeof(ip6_protos[0]))) ip6->next = ip6_protos[st->ip6_next++];
60
53
    if (ip6->next == 6) { // TCP
61
51
    } else if (ip6->next == 17) { // UDP
62
49
    } else if (ip6->next == 58 && len >= sizeof(struct ip6) + sizeof(struct icmp6)) { // ICMPv6
63
20
      uint8_t icmp6_types[] = {128, 134, 135, 136}; // Echo Request, RA, NS, NA
64
20
      struct icmp6 *icmp6 = (struct icmp6 *) (ip6 + 1);
65
      // send all handled ICMPv6 types, then 2 random ones
66
20
      if (st->icmp6_type >= (sizeof(icmp6_types) / sizeof(icmp6_types[0]) + 2)) st->icmp6_type = 0;
67
20
      if (st->icmp6_type < (sizeof(icmp6_types) / sizeof(icmp6_types[0]))) icmp6->type = icmp6_types[st->icmp6_type++];
68
20
    }
69
53
  }
70
53
}
71
72
6.10k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
73
6.10k
  mg_log_set(MG_LL_INFO);
74
75
6.10k
  struct mg_dns_message dm;
76
6.10k
  mg_dns_parse(data, size, &dm);
77
6.10k
  mg_dns_parse(NULL, 0, &dm);
78
79
6.10k
  struct mg_http_message hm;
80
6.10k
  if (mg_http_parse((const char *) data, size, &hm) > 0) {
81
2.23k
    mg_crc32(0, hm.method.buf, hm.method.len);
82
2.23k
    mg_crc32(0, hm.uri.buf, hm.uri.len);
83
2.23k
    mg_crc32(0, hm.uri.buf, hm.uri.len);
84
69.3k
    for (size_t i = 0; i < sizeof(hm.headers) / sizeof(hm.headers[0]); i++) {
85
67.1k
      struct mg_str *k = &hm.headers[i].name, *v = &hm.headers[i].value;
86
67.1k
      mg_crc32(0, k->buf, k->len);
87
67.1k
      mg_crc32(0, v->buf, v->len);
88
67.1k
    }
89
2.23k
  }
90
6.10k
  mg_http_parse(NULL, 0, &hm);
91
92
6.10k
  struct mg_str body = mg_str_n((const char *) data, size);
93
6.10k
  char tmp[256];
94
6.10k
  mg_http_get_var(&body, "key", tmp, sizeof(tmp));
95
6.10k
  mg_http_get_var(&body, "key", NULL, 0);
96
6.10k
  mg_url_decode((char *) data, size, tmp, sizeof(tmp), 1);
97
6.10k
  mg_url_decode((char *) data, size, tmp, 1, 1);
98
6.10k
  mg_url_decode(NULL, 0, tmp, 1, 1);
99
100
6.10k
  struct mg_mqtt_message mm;
101
6.10k
  if (mg_mqtt_parse(data, size, 0, &mm) == MQTT_OK) {
102
2.38k
    mg_crc32(0, mm.topic.buf, mm.topic.len);
103
2.38k
    mg_crc32(0, mm.data.buf, mm.data.len);
104
2.38k
    mg_crc32(0, mm.dgram.buf, mm.dgram.len);
105
2.38k
  }
106
6.10k
  mg_mqtt_parse(NULL, 0, 0, &mm);
107
6.10k
  if (mg_mqtt_parse(data, size, 5, &mm) == MQTT_OK) {
108
2.23k
    mg_crc32(0, mm.topic.buf, mm.topic.len);
109
2.23k
    mg_crc32(0, mm.data.buf, mm.data.len);
110
2.23k
    mg_crc32(0, mm.dgram.buf, mm.dgram.len);
111
2.23k
    {
112
2.23k
      struct mg_mqtt_prop prop;
113
2.23k
      size_t ofs = 0;
114
9.84k
      while ((ofs = mg_mqtt_next_prop(&mm, &prop, ofs)) > 0) {
115
7.60k
          mg_crc32(0, prop.key.buf, prop.key.len);
116
7.60k
          mg_crc32(0, prop.val.buf, prop.val.len);
117
7.60k
      }
118
2.23k
    }
119
2.23k
  }
120
6.10k
  mg_mqtt_parse(NULL, 0, 5, &mm);
121
122
6.10k
  mg_sntp_parse(data, size);
123
6.10k
  mg_sntp_parse(NULL, 0);
124
125
6.10k
  char buf[size * 4 / 3 + 5];  // At least 4 chars and nul termination
126
6.10k
  mg_base64_decode((char *) data, size, buf, sizeof(buf));
127
6.10k
  mg_base64_decode(NULL, 0, buf, sizeof(buf));
128
6.10k
  mg_base64_encode(data, size, buf, sizeof(buf));
129
6.10k
  mg_base64_encode(NULL, 0, buf, sizeof(buf));
130
131
6.10k
  mg_match(mg_str_n((char *) data, size), mg_str_n((char *) data, size), NULL);
132
133
6.10k
  struct mg_str entry, s = mg_str_n((char *) data, size);
134
1.19M
  while (mg_span(s, &entry, &s, ',')) entry.len = 0;
135
136
6.10k
  int n;
137
6.10k
  mg_json_get(mg_str_n((char *) data, size), "$", &n);
138
6.10k
  mg_json_get(mg_str_n((char *) data, size), "$.a.b", &n);
139
6.10k
  mg_json_get(mg_str_n((char *) data, size), "$[0]", &n);
140
141
  // Test built-in TCP/IP stack
142
6.10k
  if (size > 0) {
143
6.10k
    static size_t t;
144
6.10k
    struct mg_tcpip_if mif = {.ip = 1,
145
6.10k
                              .mask = 255,
146
6.10k
                              .gw = 1,
147
6.10k
                              .gw_ready = true,
148
6.10k
                              .state = MG_TCPIP_STATE_READY,
149
#if MG_ENABLE_IPV6
150
                              .ip6[0] = 1,
151
                              .prefix[0] = 1,
152
                              .prefix_len = 64,
153
                              .gw6[0] = 1,
154
                              .gw6_ready = true,
155
                              .state6 = MG_TCPIP_STATE_READY,  // so mg_send() works and RS stops
156
#endif
157
6.10k
                              .driver = &mg_tcpip_driver_mock};
158
6.10k
    struct mg_mgr mgr;
159
160
6.10k
    if (t >= MG_TCPIP_L2_USER) t = 0; // defined in l2.h
161
6.10k
    mif.l2type = (enum mg_l2type) t++; // configure to a L2 type
162
163
6.10k
    mg_mgr_init(&mgr);
164
6.10k
    mg_tcpip_init(&mgr, &mif);
165
166
    // Make a copy of the random data, in order to modify it
167
6.10k
    void *pkt = malloc(size);
168
6.10k
    struct eth *eth = (struct eth *) pkt;
169
6.10k
    memcpy(pkt, data, size);
170
6.10k
    if ((mif.l2type == MG_TCPIP_L2_ETH || mif.l2type == MG_TCPIP_L2_PPPoE) && size > sizeof(*eth)) {
171
2.83k
      static size_t i, v;
172
2.83k
      static struct fuzz_l3_state l3;
173
2.83k
      uint8_t *ethpay = (uint8_t *) (eth + 1);
174
2.83k
      size_t ethhdrsz = sizeof(*eth);
175
2.83k
      uint16_t etype;
176
      // eth_types[] exists in l2_eth.c
177
2.83k
      memcpy(eth->dst, mif.mac, 6);  // Set valid destination MAC
178
      // send all handled eth types, then 2 random ones
179
2.83k
      if (i >= (sizeof(eth_types) / sizeof(eth_types[0]) + 2)) i = 0;
180
2.83k
      if (i < (sizeof(eth_types) / sizeof(eth_types[0]))) eth->type = mg_htons(eth_types[i++]);
181
2.83k
      etype = mg_ntohs(eth->type);
182
2.83k
      if (v >= 10) v = 0; // 802.1Q tag 3 out of 10 frames
183
2.83k
      if (v < 3) mif.l2data.eth.vlan_id = 123;
184
2.83k
      if (size > sizeof(*eth) + sizeof(struct qtag)) {
185
2.55k
        struct qtag *qtag = (struct qtag *) &eth->type;
186
2.55k
        if (v == 0) {
187
          // v=0: configured for VLAN 123, untagged frame, discard path
188
2.30k
        } else if (v == 1) {
189
          // v=1: configured for VLAN 123, tagged VLAN 123, accept path
190
256
          qtag->tpid = mg_htons(0x8100);
191
256
          qtag->tci = mg_htons(123);
192
256
          MG_STORE_BE16(qtag + 1, etype);
193
256
          ethhdrsz += sizeof(*qtag);
194
256
          ethpay += sizeof(*qtag);
195
2.04k
        } else if (v == 2) {
196
          // v=2: configured for VLAN 123, tagged random VLAN, mismatch path
197
255
          qtag->tpid = mg_htons(0x8100);
198
255
          MG_STORE_BE16(qtag + 1, etype);
199
255
          ethhdrsz += sizeof(*qtag);
200
255
          ethpay += sizeof(*qtag);
201
1.78k
        } else if (v == 3) {
202
          // v=3: unconfigured VLAN, tagged frame, discard path
203
255
          qtag->tpid = mg_htons(0x8100);
204
255
          MG_STORE_BE16(qtag + 1, etype);
205
255
          ethhdrsz += sizeof(*qtag);
206
255
          ethpay += sizeof(*qtag);
207
255
        } // else unconfigured VLAN, untagged frame, normal path
208
2.55k
      }
209
2.83k
      ++v;
210
2.83k
      if (etype == 0x8863 && size > (ethhdrsz + sizeof(struct pppoe))) { // PPPoE disc
211
68
        static size_t j;
212
68
        uint8_t pppoe_codes[] = {MG_PPPoE_PADO, MG_PPPoE_PADS, MG_PPPoE_PADT}; // defined in l2_ppp.c
213
68
        struct pppoe *pppoe = (struct pppoe *) ethpay;
214
68
        pppoe->vertype = 0x11;
215
        // send all handled PPPoE codes, then 2 random ones
216
68
        if (j >= (sizeof(pppoe_codes) / sizeof(pppoe_codes[0]) + 2)) j = 0;
217
68
        if (j < (sizeof(pppoe_codes) / sizeof(pppoe_codes[0]))) pppoe->code = (pppoe_codes[j++]);
218
68
        if (pppoe->code == MG_PPPoE_PADO) {
219
8
          static size_t k;
220
          // send id=0, then 2 random ids
221
8
          if (k >= 3) k = 0;
222
8
          if (k == 0) pppoe->id = 0;
223
8
          ++k;
224
60
        } else if (pppoe->code == MG_PPPoE_PADS) {
225
4
          static size_t k;
226
          // send id=x, then 2 random ids
227
4
          if (k >= 3) k = 0;
228
4
          if (k == 0) pppoe->id = 0x1234;
229
4
          ++k;
230
56
        } else if (pppoe->code == MG_PPPoE_PADT) { // keep separate so they do not correlate too much
231
8
          static size_t k;
232
          // send id=x, then 2 random ids
233
8
          if (k >= 3) k = 0;
234
8
          if (k == 0) pppoe->id = 0x1234;
235
8
          ++k;
236
8
        }
237
2.76k
      } else if (etype == 0x8864 && size > (ethhdrsz + sizeof(struct pppoe))) { // PPPoE sess
238
        // PPP rx is covered via HDLC too; PPPoE tx wrapping may remain unexercised.
239
42
        struct pppoe *pppoe = (struct pppoe *) ethpay;
240
42
        static size_t k, l;
241
        // send code=0, then 2 random codes
242
42
        if (k >= 3) k = 0;
243
42
        if (k == 0)  {
244
14
          static size_t m;
245
14
          pppoe->code = 0;
246
          // send id=x, then 2 random ids
247
14
          if (m >= 3) m = 0;
248
14
          if (m == 0) pppoe->id = 0x1234;
249
14
          ++m;
250
14
        }
251
42
        ++k;
252
        // set s_state to valid and invalid
253
42
        if (l >= 2) l = 0;
254
42
        if (l == 0) s_state = MG_PPPoE_ST_SESS; // defined in l2_ppp.c
255
42
        if (l == 1) s_state = MG_PPPoE_ST_REQ;
256
42
        ++l;
257
        // PPP exercised in other block
258
42
      }
259
      // build proper layer-3 datagrams, to be able to exercise layers above
260
2.72k
      else if (etype == 0x800 && size > (ethhdrsz + sizeof(struct ip))) {         // IPv4
261
112
        fuzz_ip4(ethpay, size - ethhdrsz, &l3);
262
2.61k
      } else if (etype == 0x806) {      // ARP
263
264
2.55k
      } else if (etype == 0x86dd && size > (ethhdrsz + sizeof(struct ip6))) {     // IPv6
265
23
        fuzz_ip6(ethpay, size - ethhdrsz, &l3);
266
23
      }
267
3.26k
    } else if (mif.l2type == MG_TCPIP_L2_PPP && size > (sizeof(struct hdlc_) + sizeof(struct ppp) + 2)) {
268
1.69k
      static size_t i;
269
1.69k
      static struct fuzz_l3_state l3;
270
1.69k
      uint16_t ppp_protos[] = {
271
1.69k
          MG_PPP_PROTO_LCP, MG_PPP_PROTO_IPCP, MG_PPP_PROTO_IP, MG_PPP_PROTO_IPV6CP, MG_PPP_PROTO_IPV6
272
1.69k
      };
273
1.69k
      struct hdlc_ *hdlc = (struct hdlc_ *) pkt;
274
1.69k
      struct ppp *ppp = (struct ppp *) (hdlc + 1);
275
1.69k
      size_t len = size - sizeof(*hdlc) - sizeof(*ppp) - 2;
276
1.69k
      hdlc->addr = MG_PPP_ADDR;
277
1.69k
      hdlc->ctrl = MG_PPP_CTRL;
278
1.69k
      s_lcpup = true;
279
      // send all handled PPP protocols, then 2 random ones
280
1.69k
      if (i >= (sizeof(ppp_protos) / sizeof(ppp_protos[0]) + 2)) i = 0;
281
1.69k
      if (i < (sizeof(ppp_protos) / sizeof(ppp_protos[0]))) ppp->proto = mg_htons(ppp_protos[i++]);
282
1.69k
      if (ppp->proto == mg_htons(MG_PPP_PROTO_LCP) && len >= sizeof(struct lcp)) {
283
35
        static size_t j, k;
284
35
        uint8_t lcp_codes[] = {MG_PPP_LCP_CFG_REQ, MG_PPP_LCP_CFG_ACK, MG_PPP_LCP_CFG_TERM_REQ, MG_PPP_LCP_ECHO_REQ};
285
35
        struct lcp *lcp = (struct lcp *) (ppp + 1);
286
        // send all handled LCP codes, then 2 random ones
287
35
        if (j >= (sizeof(lcp_codes) / sizeof(lcp_codes[0]) + 2)) j = 0;
288
35
        if (j < (sizeof(lcp_codes) / sizeof(lcp_codes[0]))) lcp->code = lcp_codes[j++];
289
        // send valid length, then 2 random lengths
290
35
        if (k >= 3) k = 0;
291
35
        if (k == 0) lcp->len = mg_htons((uint16_t) sizeof(*lcp));
292
35
        ++k;
293
1.65k
      } else if (ppp->proto == mg_htons(MG_PPP_PROTO_IPCP) && len >= 10) {
294
35
        static size_t j, k, m;
295
35
        uint8_t ipcp_codes[] = {MG_PPP_IPCP_CFG_REQ, MG_PPP_IPCP_CFG_ACK, MG_PPP_IPCP_CFG_NACK, MG_PPP_IPCP_CFG_REJECT};
296
35
        struct ipcp *ipcp = (struct ipcp *) (ppp + 1);
297
35
        uint8_t *opts = (uint8_t *) (ipcp + 1);
298
        // send all handled IPCP codes, then 2 random ones
299
35
        if (j >= (sizeof(ipcp_codes) / sizeof(ipcp_codes[0]) + 2)) j = 0;
300
35
        if (j < (sizeof(ipcp_codes) / sizeof(ipcp_codes[0]))) ipcp->code = ipcp_codes[j++];
301
        // send valid length, then 2 random lengths
302
35
        if (m >= 3) m = 0;
303
35
        if (m == 0) ipcp->len = mg_htons(10);
304
35
        ++m;
305
        // send valid IPADDR option, then 2 random option types
306
35
        if (k >= 3) k = 0;
307
35
        if (k == 0) {
308
12
          static size_t l;
309
12
          opts[0] = MG_PPP_IPCP_OPT_IPADDR;
310
          // for valid option type, send valid length, then random length
311
12
          if (l >= 2) l = 0;
312
12
          if (l == 0) opts[1] = 6;
313
12
          ++l;
314
12
        }
315
35
        ++k;
316
1.62k
      } else if (ppp->proto == mg_htons(MG_PPP_PROTO_IP) && len >= sizeof(struct ip)) {
317
36
        fuzz_ip4(ppp + 1, len, &l3);
318
1.58k
      } else if (ppp->proto == mg_htons(MG_PPP_PROTO_IPV6CP) && len >= 14) {
319
10
        static size_t j, k, m;
320
10
        uint8_t ipv6cp_codes[] = {MG_PPP_IPV6CP_CFG_REQ, MG_PPP_IPV6CP_CFG_ACK, MG_PPP_IPV6CP_CFG_NACK, MG_PPP_IPV6CP_CFG_REJECT};
321
10
        struct ipv6cp *ipv6cp = (struct ipv6cp *) (ppp + 1);
322
10
        uint8_t *opts = (uint8_t *) (ipv6cp + 1);
323
        // send all handled IPV6CP codes, then 2 random ones
324
10
        if (j >= (sizeof(ipv6cp_codes) / sizeof(ipv6cp_codes[0]) + 2)) j = 0;
325
10
        if (j < (sizeof(ipv6cp_codes) / sizeof(ipv6cp_codes[0]))) ipv6cp->code = ipv6cp_codes[j++];
326
        // send valid length, then 2 random lengths
327
10
        if (m >= 3) m = 0;
328
10
        if (m == 0) ipv6cp->len = mg_htons(14);
329
10
        ++m;
330
        // send valid IFCID option, then 2 random option types
331
10
        if (k >= 3) k = 0;
332
10
        if (k == 0) {
333
4
          static size_t l;
334
4
          opts[0] = MG_PPP_IPV6CP_OPT_IFCID;
335
          // for valid option type, send valid length, then random length
336
4
          if (l >= 2) l = 0;
337
4
          if (l == 0) {
338
2
            static size_t o;
339
2
            opts[1] = 10;
340
            // for valid option length, send non-zero IFCID, then 2 random IFCIDs
341
2
            if (o >= 3) o = 0;
342
2
            if (o == 0) opts[9] = 1;
343
2
            ++o;
344
2
          }
345
4
          ++l;
346
4
        }
347
10
        ++k;
348
1.57k
      } else if (ppp->proto == mg_htons(MG_PPP_PROTO_IPV6) && len >= sizeof(struct ip6)) {
349
30
        fuzz_ip6(ppp + 1, len, &l3);
350
30
      }
351
1.69k
    }
352
353
#if defined(MAIN)
354
    printf("Sending to net_builtin:\n");
355
    mg_hexdump(pkt, size);
356
#endif
357
6.10k
    mg_tcpip_rx(&mif, pkt, size);
358
359
    // prior actions may have had consequences on link state
360
6.10k
    mif.state = MG_TCPIP_STATE_READY;
361
    // Test HTTP serving (via our built-in TCP/IP stack)
362
6.10k
    const char *url = "http://localhost:12345"; // internally resolves
363
    // creates a connection and sends SYN via the router, unless OOM
364
6.10k
    struct mg_connection *c = mg_http_connect(&mgr, url, fn, NULL);
365
6.10k
    mg_iobuf_add(&c->recv, 0, data, size);
366
6.10k
    c->pfn(c, MG_EV_READ, NULL); // manually invoke protocol event handler
367
368
6.10k
    free(pkt);
369
6.10k
    mg_mgr_free(&mgr);
370
6.10k
  }
371
372
6.10k
  return 0;
373
6.10k
}
374
375
#if defined(MAIN)
376
int main(int argc, char *argv[]) {
377
  int res = EXIT_FAILURE;
378
  if (argc > 1) {
379
    struct mg_str data = mg_file_read(&mg_fs_posix, argv[1]);
380
    if (data.buf != NULL) {
381
      LLVMFuzzerTestOneInput((uint8_t *) data.buf, data.len);
382
      res = EXIT_SUCCESS;
383
    }
384
    free(data.buf);
385
  }
386
  return res;
387
}
388
#endif