Coverage Report

Created: 2025-11-20 06:48

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