aboutsummaryrefslogtreecommitdiff
path: root/fuzz/fuzz_ndpi_reader.c
diff options
context:
space:
mode:
authorLuca Deri <lucaderi@users.noreply.github.com>2020-06-29 19:15:46 +0200
committerGitHub <noreply@github.com>2020-06-29 19:15:46 +0200
commit07615903e20cffc59e674ebae235b78886c9efbb (patch)
tree78b1d73db04724404f198397a2fe6d8a475a1212 /fuzz/fuzz_ndpi_reader.c
parent93a4a4387af951a4680061bf4e1effa87fa65406 (diff)
parent4a6c525db845fc8f6564ce18963b91d153a5a354 (diff)
Merge pull request #939 from lnslbrty/improved/fuzzy-reader-smp
Improved fuzz_ndpi_reader which supports now SMP/MT w/o race-coniditi…
Diffstat (limited to 'fuzz/fuzz_ndpi_reader.c')
-rw-r--r--fuzz/fuzz_ndpi_reader.c69
1 files changed, 65 insertions, 4 deletions
diff --git a/fuzz/fuzz_ndpi_reader.c b/fuzz/fuzz_ndpi_reader.c
index 7de3d45f4..32318718f 100644
--- a/fuzz/fuzz_ndpi_reader.c
+++ b/fuzz/fuzz_ndpi_reader.c
@@ -21,13 +21,13 @@ int bufferToFile(const char * name, const uint8_t *Data, size_t Size) {
FILE * fd;
if (remove(name) != 0) {
if (errno != ENOENT) {
- printf("failed remove, errno=%d\n", errno);
+ perror("remove failed");
return -1;
}
}
fd = fopen(name, "wb");
if (fd == NULL) {
- printf("failed open, errno=%d\n", errno);
+ perror("open failed");
return -2;
}
if (fwrite (Data, 1, Size, fd) != Size) {
@@ -45,6 +45,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
int r;
char errbuf[PCAP_ERRBUF_SIZE];
NDPI_PROTOCOL_BITMASK all;
+ char * pcap_path = tempnam("/tmp", "fuzz-ndpi-reader");
if (prefs == NULL) {
prefs = calloc(sizeof(struct ndpi_workflow_prefs), 1);
@@ -57,10 +58,12 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
prefs->max_ndpi_flows = 1024;
prefs->quiet_mode = 0;
}
- bufferToFile("/tmp/fuzz.pcap", Data, Size);
+ bufferToFile(pcap_path, Data, Size);
- pkts = pcap_open_offline("/tmp/fuzz.pcap", errbuf);
+ pkts = pcap_open_offline(pcap_path, errbuf);
if (pkts == NULL) {
+ remove(pcap_path);
+ free(pcap_path);
return 0;
}
struct ndpi_workflow * workflow = ndpi_workflow_init(prefs, pkts);
@@ -87,5 +90,63 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ndpi_workflow_free(workflow);
pcap_close(pkts);
+ remove(pcap_path);
+ free(pcap_path);
+
return 0;
}
+
+#ifdef BUILD_MAIN
+int main(int argc, char ** argv)
+{
+ FILE * pcap_file;
+ long pcap_file_size;
+ uint8_t * pcap_buffer;
+ int test_retval;
+
+ if (argc != 2) {
+ fprintf(stderr, "usage: %s: [pcap-file]\n",
+ (argc > 0 ? argv[0] : "fuzz_ndpi_reader_with_main"));
+ return 1;
+ }
+
+ pcap_file = fopen(argv[1], "r");
+ if (pcap_file == NULL) {
+ perror("fopen failed");
+ return 1;
+ }
+
+ if (fseek(pcap_file, 0, SEEK_END) != 0) {
+ perror("fseek(SEEK_END) failed");
+ return 1;
+ }
+
+ pcap_file_size = ftell(pcap_file);
+ if (pcap_file_size < 0) {
+ perror("ftell failed");
+ return 1;
+ }
+
+ if (fseek(pcap_file, 0, SEEK_SET) != 0) {
+ perror("fseek(0, SEEK_SET) failed");
+ return 1;
+ }
+
+ pcap_buffer = malloc(pcap_file_size);
+ if (pcap_buffer == NULL) {
+ perror("malloc failed");
+ return 1;
+ }
+
+ if (fread(pcap_buffer, sizeof(*pcap_buffer), pcap_file_size, pcap_file) != pcap_file_size) {
+ perror("fread failed");
+ return 1;
+ }
+
+ test_retval = LLVMFuzzerTestOneInput(pcap_buffer, pcap_file_size);
+ fclose(pcap_file);
+ free(pcap_buffer);
+
+ return test_retval;
+}
+#endif