Coverage Report

Created: 2026-01-05 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ndpi/fuzz/fuzz_ds_address_cache.cpp
Line
Count
Source
1
#include "ndpi_api.h"
2
#include "ndpi_private.h"
3
4
#include "fuzz_common_code.h"
5
6
#include <stdint.h>
7
#include <stdio.h>
8
#include <assert.h>
9
#include "fuzzer/FuzzedDataProvider.h"
10
11
12
591
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
13
591
  FuzzedDataProvider fuzzed_data(data, size);
14
591
  u_int16_t i, num_iteration;
15
591
  int is_added = 0;
16
591
  struct ndpi_detection_module_struct ndpi_struct; /*Opaque; we don't really need to initialize it */
17
591
  ndpi_ip_addr_t ip_addr, ip_addr_added;
18
591
  char *hostname, *hostname2;
19
591
  u_int32_t epoch_now;
20
591
  u_int32_t ttl;
21
591
  bool rc;
22
591
  char path[] = "random.dump";
23
24
25
  /* Just to have some data */
26
591
  if (fuzzed_data.remaining_bytes() < 1024)
27
18
    return -1;
28
29
  /* To allow memory allocation failures */
30
573
  fuzz_set_alloc_callbacks_and_seed(size);
31
32
33
573
  memset(&ndpi_struct, '\0', sizeof(struct ndpi_detection_module_struct));
34
573
  ndpi_struct.cfg.address_cache_size = fuzzed_data.ConsumeIntegral<u_int8_t>();
35
36
573
  epoch_now = 1;
37
38
  /* Random insert */
39
573
  num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
40
61.2k
  for (i = 0; i < num_iteration; i++) {
41
60.6k
    if (fuzzed_data.ConsumeBool()) {
42
12.9k
      if(fuzzed_data.remaining_bytes() > 16) {
43
12.7k
        memcpy(&ip_addr.ipv6, fuzzed_data.ConsumeBytes<u_int8_t>(16).data(), 16);
44
12.7k
      } else {
45
181
        continue;
46
181
      }
47
47.6k
    } else {
48
47.6k
      memset(&ip_addr, '\0', sizeof(ip_addr));
49
47.6k
      ip_addr.ipv4 = fuzzed_data.ConsumeIntegral<u_int32_t>();
50
47.6k
    }
51
60.4k
    hostname = strdup(fuzzed_data.ConsumeRandomLengthString(32).c_str());
52
60.4k
    ttl = fuzzed_data.ConsumeIntegral<u_int8_t>();
53
60.4k
    epoch_now += fuzzed_data.ConsumeIntegral<u_int8_t>();
54
55
60.4k
    rc = ndpi_cache_address(&ndpi_struct, ip_addr, hostname, epoch_now, ttl);
56
60.4k
    if (rc == true) {
57
49.3k
      if(is_added == 0 && fuzzed_data.ConsumeBool()) {
58
        /* Keep one random node really added */
59
410
        is_added = 1;
60
410
        ip_addr_added = ip_addr;
61
48.9k
      } else if(fuzzed_data.ConsumeBool()) {
62
        /* Add also same ip with different hostname */
63
12.8k
        hostname2 = ndpi_strdup(fuzzed_data.ConsumeRandomLengthString(32).c_str());
64
12.8k
        ndpi_cache_address(&ndpi_struct, ip_addr, hostname2, epoch_now, ttl);
65
12.8k
        ndpi_free(hostname2);
66
12.8k
      }
67
49.3k
    }
68
60.4k
    ndpi_free(hostname);
69
60.4k
  }
70
71
  /* "Random" search */
72
573
  num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
73
30.0k
  for (i = 0; i < num_iteration; i++) {
74
29.4k
    if (fuzzed_data.ConsumeBool()) {
75
11.2k
      if(fuzzed_data.remaining_bytes() > 16) {
76
10.9k
        memcpy(&ip_addr.ipv6, fuzzed_data.ConsumeBytes<u_int8_t>(16).data(), 16);
77
10.9k
      } else {
78
322
        continue;
79
322
      }
80
18.2k
    } else {
81
18.2k
      memset(&ip_addr, '\0', sizeof(ip_addr));
82
18.2k
      ip_addr.ipv4 = fuzzed_data.ConsumeIntegral<u_int32_t>();
83
18.2k
    }
84
85
29.1k
    ndpi_cache_address_find(&ndpi_struct, ip_addr);
86
29.1k
  }
87
  /* Search of an added entry */
88
573
  if(is_added)
89
410
    ndpi_cache_address_find(&ndpi_struct, ip_addr_added);
90
91
573
  if(fuzzed_data.ConsumeBool()) {
92
129
    epoch_now += fuzzed_data.ConsumeIntegral<u_int8_t>();
93
129
    ndpi_cache_address_flush_expired(&ndpi_struct, epoch_now);
94
129
  }
95
96
573
  epoch_now += fuzzed_data.ConsumeIntegral<u_int8_t>();
97
573
  ndpi_cache_address_dump(&ndpi_struct, path, epoch_now);
98
573
  epoch_now += fuzzed_data.ConsumeIntegral<u_int8_t>();
99
573
  ndpi_cache_address_restore(&ndpi_struct, path, epoch_now);
100
101
573
  ndpi_cache_enable(&ndpi_struct);
102
573
  ndpi_cache_hostname_ip_swap(&ndpi_struct);
103
573
  ndpi_cache_hostname_ip_swap(&ndpi_struct);
104
105
573
  ndpi_term_address_cache(ndpi_struct.address_cache);
106
573
  ndpi_filter_free(ndpi_struct.dns_hostname.cache);
107
573
  ndpi_filter_free(ndpi_struct.dns_hostname.cache_shadow);
108
109
573
  return 0;
110
591
}