Coverage Report

Created: 2026-08-03 07:15

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
644
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
13
644
  FuzzedDataProvider fuzzed_data(data, size);
14
644
  u_int16_t i, num_iteration;
15
644
  int is_added = 0;
16
644
  struct ndpi_detection_module_struct ndpi_struct; /*Opaque; we don't really need to initialize it */
17
644
  ndpi_ip_addr_t ip_addr, ip_addr_added;
18
644
  char *hostname, *hostname2;
19
644
  u_int32_t epoch_now;
20
644
  u_int32_t ttl;
21
644
  bool rc;
22
644
  char path[] = "random.dump";
23
24
25
  /* Just to have some data */
26
644
  if (fuzzed_data.remaining_bytes() < 1024)
27
18
    return -1;
28
29
  /* To allow memory allocation failures */
30
626
  fuzz_set_alloc_callbacks_and_seed(size);
31
32
33
626
  memset(&ndpi_struct, '\0', sizeof(struct ndpi_detection_module_struct));
34
626
  ndpi_struct.cfg.address_cache_size = fuzzed_data.ConsumeIntegral<u_int8_t>();
35
36
626
  epoch_now = 1;
37
38
  /* Random insert */
39
626
  num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
40
63.8k
  for (i = 0; i < num_iteration; i++) {
41
63.1k
    if (fuzzed_data.ConsumeBool()) {
42
14.7k
      if(fuzzed_data.remaining_bytes() > 16) {
43
14.5k
        memcpy(&ip_addr.ipv6, fuzzed_data.ConsumeBytes<u_int8_t>(16).data(), 16);
44
14.5k
      } else {
45
204
        continue;
46
204
      }
47
48.4k
    } else {
48
48.4k
      memset(&ip_addr, '\0', sizeof(ip_addr));
49
48.4k
      ip_addr.ipv4 = fuzzed_data.ConsumeIntegral<u_int32_t>();
50
48.4k
    }
51
62.9k
    hostname = strdup(fuzzed_data.ConsumeRandomLengthString(32).c_str());
52
62.9k
    ttl = fuzzed_data.ConsumeIntegral<u_int8_t>();
53
62.9k
    epoch_now += fuzzed_data.ConsumeIntegral<u_int8_t>();
54
55
62.9k
    rc = ndpi_cache_address(&ndpi_struct, ip_addr, hostname, epoch_now, ttl);
56
62.9k
    if (rc == true) {
57
52.8k
      if(is_added == 0 && fuzzed_data.ConsumeBool()) {
58
        /* Keep one random node really added */
59
454
        is_added = 1;
60
454
        ip_addr_added = ip_addr;
61
52.4k
      } else if(fuzzed_data.ConsumeBool()) {
62
        /* Add also same ip with different hostname */
63
14.1k
        hostname2 = ndpi_strdup(fuzzed_data.ConsumeRandomLengthString(32).c_str());
64
14.1k
        ndpi_cache_address(&ndpi_struct, ip_addr, hostname2, epoch_now, ttl);
65
14.1k
        ndpi_free(hostname2);
66
14.1k
      }
67
52.8k
    }
68
62.9k
    ndpi_free(hostname);
69
62.9k
  }
70
71
  /* "Random" search */
72
626
  num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
73
33.1k
  for (i = 0; i < num_iteration; i++) {
74
32.5k
    if (fuzzed_data.ConsumeBool()) {
75
15.1k
      if(fuzzed_data.remaining_bytes() > 16) {
76
14.8k
        memcpy(&ip_addr.ipv6, fuzzed_data.ConsumeBytes<u_int8_t>(16).data(), 16);
77
14.8k
      } else {
78
301
        continue;
79
301
      }
80
17.3k
    } else {
81
17.3k
      memset(&ip_addr, '\0', sizeof(ip_addr));
82
17.3k
      ip_addr.ipv4 = fuzzed_data.ConsumeIntegral<u_int32_t>();
83
17.3k
    }
84
85
32.2k
    ndpi_cache_address_find(&ndpi_struct, ip_addr);
86
32.2k
  }
87
  /* Search of an added entry */
88
626
  if(is_added)
89
454
    ndpi_cache_address_find(&ndpi_struct, ip_addr_added);
90
91
626
  if(fuzzed_data.ConsumeBool()) {
92
154
    epoch_now += fuzzed_data.ConsumeIntegral<u_int8_t>();
93
154
    ndpi_cache_address_flush_expired(&ndpi_struct, epoch_now);
94
154
  }
95
96
626
  epoch_now += fuzzed_data.ConsumeIntegral<u_int8_t>();
97
626
  ndpi_cache_address_dump(&ndpi_struct, path, epoch_now);
98
626
  epoch_now += fuzzed_data.ConsumeIntegral<u_int8_t>();
99
626
  ndpi_cache_address_restore(&ndpi_struct, path, epoch_now);
100
101
626
  ndpi_cache_enable(&ndpi_struct);
102
626
  ndpi_cache_hostname_ip_swap(&ndpi_struct);
103
626
  ndpi_cache_hostname_ip_swap(&ndpi_struct);
104
105
626
  ndpi_term_address_cache(ndpi_struct.address_cache);
106
626
  ndpi_filter_free(ndpi_struct.dns_hostname.cache);
107
626
  ndpi_filter_free(ndpi_struct.dns_hostname.cache_shadow);
108
109
626
  return 0;
110
644
}