This commit add (optional) support for Link-Time-Optimization and Gold linker. This is the first, mandatory step needed to make nDPI compliant with "introspector" sanitizer requirements in OSS-Fuzz: see https://github.com/google/oss-fuzz/issues/8939 Gold linker is not supported by Windows and by macOS, so this feature is disabled by default. It has been enable in CI in two linux targets ("latest" gcc and clang). Fix some warnings triggered by LTO. The changes in `src/lib/ndpi_serializer.c` seams reasonable. However, the change in `tests/unit/unit.c` is due to the following warning, which seems to be a false positive. ``` unit.c: In function ‘serializerUnitTest’: ndpi_serializer.c:2258:13: error: ‘MEM[(struct ndpi_private_serializer *)&deserializer].buffer.size’ may be used uninitialized in this function [-Werror=maybe-uninitialized] unit.c:67:31: note: ‘MEM[(struct ndpi_private_serializer *)&deserializer].buffer.size’ was declared here 67 | ndpi_serializer serializer, deserializer; | ^ ndpi_serializer.c:2605:10: error: ‘MEM[(struct ndpi_private_serializer *)&deserializer].status.buffer.size_used’ may be used uninitialized in this function [-Werror=maybe-uninitialized] unit.c:67:31: note: ‘MEM[(struct ndpi_private_serializer *)&deserializer].status.buffer.size_used’ was declared here 67 | ndpi_serializer serializer, deserializer; ``` Since this warning is triggered only with an old version of gcc and `tests/unit/unit.c` is used only during the tests, the easiest fix has been applied. Some (unknown to me) combinations of OS and compiler trigger the following warnings at linker time (with sanitizer and gold linker) ``` /usr/bin/ld.gold: warning: Cannot export local symbol '__asan_report_load1_asm' /usr/bin/ld.gold: warning: Cannot export local symbol '__asan_report_load2_asm' /usr/bin/ld.gold: warning: Cannot export local symbol '__asan_report_load4_asm' /usr/bin/ld.gold: warning: Cannot export local symbol '__asan_report_load8_asm' /usr/bin/ld.gold: warning: Cannot export local symbol '__asan_report_load16_asm' /usr/bin/ld.gold: warning: Cannot export local symbol '__asan_report_store1_asm' /usr/bin/ld.gold: warning: Cannot export local symbol '__asan_report_store2_asm' /usr/bin/ld.gold: warning: Cannot export local symbol '__asan_report_store4_asm' [..] ``` I have not found any references to this kind of message, with the only exception of https://sourceware.org/bugzilla/show_bug.cgi?id=25975 which seems to suggest that these messages can be safely ignored. In any case, the compilation results are sound. Fix `clean` target in the Makefile in the `example` directory. In OSS-Fuzz enviroments, `fuzz_ndpi_reader` reports a strange link error (as always, when the gold linker is involved...). It's come out that the culprit was the `tempnam` function: the code has been changed to use `tmpfile` instead. No sure why... :( Fuzzing target `fuzz_ndpi_reader.c` doesn't use `libndpiReader.a` anymore: this way we can use `--with-only-libndpi` flag on Oss-Fuzz builds as workaround for the "missing dependencies errors" described in https://github.com/google/oss-fuzz/issues/8939
121 lines
3.3 KiB
C
121 lines
3.3 KiB
C
#include "reader_util.h"
|
|
#include "ndpi_api.h"
|
|
|
|
#include <pcap/pcap.h>
|
|
|
|
#include <errno.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
|
|
struct ndpi_workflow_prefs *prefs = NULL;
|
|
struct ndpi_workflow *workflow = NULL;
|
|
|
|
int nDPI_LogLevel = 0;
|
|
char *_debug_protocols = NULL;
|
|
u_int32_t current_ndpi_memory = 0, max_ndpi_memory = 0;
|
|
u_int8_t enable_protocol_guess = 1, enable_payload_analyzer = 0;
|
|
u_int8_t enable_flow_stats = 0;
|
|
u_int8_t human_readeable_string_len = 5;
|
|
u_int8_t max_num_udp_dissected_pkts = 16 /* 8 is enough for most protocols, Signal requires more */, max_num_tcp_dissected_pkts = 80 /* due to telnet */;
|
|
ndpi_init_prefs init_prefs = ndpi_track_flow_payload | ndpi_enable_ja3_plus;
|
|
int enable_malloc_bins = 0;
|
|
int malloc_size_stats = 0;
|
|
int max_malloc_bins = 0;
|
|
struct ndpi_bin malloc_bins; /* unused */
|
|
|
|
FILE *bufferToFile(const uint8_t *Data, size_t Size) {
|
|
FILE *fd;
|
|
fd = tmpfile();
|
|
if (fd == NULL) {
|
|
perror("Error tmpfile");
|
|
return NULL;
|
|
}
|
|
if (fwrite (Data, 1, Size, fd) != Size) {
|
|
perror("Error fwrite");
|
|
fclose(fd);
|
|
return NULL;
|
|
}
|
|
rewind(fd);
|
|
return fd;
|
|
}
|
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
|
|
pcap_t * pkts;
|
|
const u_char *pkt;
|
|
struct pcap_pkthdr *header;
|
|
int r;
|
|
char errbuf[PCAP_ERRBUF_SIZE];
|
|
NDPI_PROTOCOL_BITMASK all;
|
|
u_int i;
|
|
FILE *fd;
|
|
|
|
if (prefs == NULL) {
|
|
prefs = calloc(sizeof(struct ndpi_workflow_prefs), 1);
|
|
if (prefs == NULL) {
|
|
//should not happen
|
|
return 1;
|
|
}
|
|
prefs->decode_tunnels = 1;
|
|
prefs->num_roots = 16;
|
|
prefs->max_ndpi_flows = 16 * 1024 * 1024;
|
|
prefs->quiet_mode = 0;
|
|
|
|
workflow = ndpi_workflow_init(prefs, NULL /* pcap handler will be set later */, 0, ndpi_serialization_format_json);
|
|
// enable all protocols
|
|
NDPI_BITMASK_SET_ALL(all);
|
|
ndpi_set_protocol_detection_bitmask2(workflow->ndpi_struct, &all);
|
|
memset(workflow->stats.protocol_counter, 0,
|
|
sizeof(workflow->stats.protocol_counter));
|
|
memset(workflow->stats.protocol_counter_bytes, 0,
|
|
sizeof(workflow->stats.protocol_counter_bytes));
|
|
memset(workflow->stats.protocol_flows, 0,
|
|
sizeof(workflow->stats.protocol_flows));
|
|
ndpi_finalize_initialization(workflow->ndpi_struct);
|
|
}
|
|
|
|
fd = bufferToFile(Data, Size);
|
|
if (fd == NULL)
|
|
return 0;
|
|
|
|
pkts = pcap_fopen_offline(fd, errbuf);
|
|
if (pkts == NULL) {
|
|
fclose(fd);
|
|
return 0;
|
|
}
|
|
if (ndpi_is_datalink_supported(pcap_datalink(pkts)) == 0)
|
|
{
|
|
/* Do not fail if the datalink type is not supported (may happen often during fuzzing). */
|
|
pcap_close(pkts);
|
|
return 0;
|
|
}
|
|
|
|
workflow->pcap_handle = pkts;
|
|
/* Init flow tree */
|
|
workflow->ndpi_flows_root = ndpi_calloc(workflow->prefs.num_roots, sizeof(void *));
|
|
|
|
header = NULL;
|
|
r = pcap_next_ex(pkts, &header, &pkt);
|
|
while (r > 0) {
|
|
/* allocate an exact size buffer to check overflows */
|
|
uint8_t *packet_checked = malloc(header->caplen);
|
|
|
|
if(packet_checked) {
|
|
ndpi_risk flow_risk;
|
|
|
|
memcpy(packet_checked, pkt, header->caplen);
|
|
ndpi_workflow_process_packet(workflow, header, packet_checked, &flow_risk);
|
|
free(packet_checked);
|
|
}
|
|
|
|
r = pcap_next_ex(pkts, &header, &pkt);
|
|
}
|
|
pcap_close(pkts);
|
|
|
|
/* Free flow trees */
|
|
for(i = 0; i < workflow->prefs.num_roots; i++)
|
|
ndpi_tdestroy(workflow->ndpi_flows_root[i], ndpi_flow_info_freer);
|
|
ndpi_free(workflow->ndpi_flows_root);
|
|
|
|
return 0;
|
|
}
|