diff options
author | Nardi Ivan <nardi.ivan@gmail.com> | 2023-10-03 09:13:43 +0200 |
---|---|---|
committer | Ivan Nardi <12729895+IvanNardi@users.noreply.github.com> | 2023-10-07 13:34:37 +0200 |
commit | 86115a8a65c98d0665100b5ae85cc661d1404783 (patch) | |
tree | 44e8a5617892586a224e40d4ef90392324035523 /example | |
parent | 8ca842edecbe8ff5db155f9c1c00edadae17120d (diff) |
fuzz: extend fuzzing coverage
Diffstat (limited to 'example')
-rw-r--r-- | example/ndpiReader.c | 19 | ||||
-rw-r--r-- | example/reader_util.c | 38 | ||||
-rw-r--r-- | example/reader_util.h | 3 |
3 files changed, 30 insertions, 30 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 2a7c3b232..3601978c0 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -31,6 +31,7 @@ #include <stdio.h> #include <stdlib.h> #include <getopt.h> +#include <float.h> /* FLT_EPSILON */ #ifdef WIN32 #include <winsock2.h> /* winsock.h is included automatically */ #include <windows.h> @@ -407,6 +408,22 @@ static void ndpiCheckIPMatch(char *testChar) { /********************** FUNCTIONS ********************* */ +static double ndpi_flow_get_byte_count_entropy(const uint32_t byte_count[256], + unsigned int num_bytes) +{ + int i; + double sum = 0.0; + + for(i=0; i<256; i++) { + double tmp = (double) byte_count[i] / (double) num_bytes; + + if(tmp > FLT_EPSILON) { + sum -= tmp * logf(tmp); + } + } + return(sum / log(2.0)); +} + /** * @brief Set main components necessary to the detection */ @@ -433,6 +450,8 @@ flowGetBDMeanandVariance(struct ndpi_flow_info* flow) { * Sum up the byte_count array for outbound and inbound flows, * if this flow is bidirectional */ + /* TODO: we could probably use ndpi_data_* generic functions to simplify the code and + to get rid of `ndpi_flow_get_byte_count_entropy()` */ if (!flow->bidirectional) { array = last_entropy->src2dst_byte_count; num_bytes = last_entropy->src2dst_l4_bytes; diff --git a/example/reader_util.c b/example/reader_util.c index 86c45b351..566c64607 100644 --- a/example/reader_util.c +++ b/example/reader_util.c @@ -220,10 +220,7 @@ void ndpi_payload_analyzer(struct ndpi_flow_info *flow, for(j=min_pattern_len; j <= max_pattern_len; j++) { if((i+j) < payload_len) { if(ndpi_analyze_payload(flow, src_to_dst_direction, &payload[i], j, packet_id) == -1) { -#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - /* Avoid too much logging while fuzzing */ LOG(NDPI_LOG_ERROR, "Error ndpi_analyze_payload (allocation failure)\n"); -#endif } } } @@ -462,8 +459,11 @@ struct ndpi_workflow* ndpi_workflow_init(const struct ndpi_workflow_prefs * pref static NDPI_PROTOCOL_BITMASK debug_bitmask; static int _debug_protocols_ok = 0; + /* On some fuzzers we don't want to use these memory allocators, but some custom ones */ +#ifndef DISABLE_CUSTOM_ALLOCATOR_ON_READERUTILS set_ndpi_malloc(ndpi_malloc_wrapper), set_ndpi_free(free_wrapper); set_ndpi_flow_malloc(NULL), set_ndpi_flow_free(NULL); +#endif /* TODO: just needed here to init ndpi ndpi_malloc wrapper */ module = ndpi_init_detection_module(init_prefs); @@ -500,8 +500,14 @@ struct ndpi_workflow* ndpi_workflow_init(const struct ndpi_workflow_prefs * pref if(_debug_protocols_ok) ndpi_set_debug_bitmask(module, debug_bitmask); - if(do_init_flows_root) + if(do_init_flows_root) { workflow->ndpi_flows_root = ndpi_calloc(workflow->prefs.num_roots, sizeof(void *)); + if(!workflow->ndpi_flows_root) { + ndpi_exit_detection_module(module); + ndpi_free(workflow); + return NULL; + } + } workflow->ndpi_serialization_format = serialization_format; @@ -742,24 +748,6 @@ ndpi_flow_update_byte_dist_mean_var(ndpi_flow_info_t *flow, const void *x, /* ***************************************************** */ -double ndpi_flow_get_byte_count_entropy(const uint32_t byte_count[256], - unsigned int num_bytes) -{ - int i; - double sum = 0.0; - - for(i=0; i<256; i++) { - double tmp = (double) byte_count[i] / (double) num_bytes; - - if(tmp > FLT_EPSILON) { - sum -= tmp * logf(tmp); - } - } - return(sum / log(2.0)); -} - -/* ***************************************************** */ - static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow, const u_int8_t version, u_int16_t vlan_id, @@ -908,10 +896,7 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow struct ndpi_flow_info *newflow = (struct ndpi_flow_info*)ndpi_malloc(sizeof(struct ndpi_flow_info)); if(newflow == NULL) { -#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - /* Avoid too much logging while fuzzing */ LOG(NDPI_LOG_ERROR, "[NDPI] %s(1): not enough memory\n", __FUNCTION__); -#endif return(NULL); } else workflow->num_allocated_flows++; @@ -952,10 +937,7 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow } if((newflow->ndpi_flow = ndpi_flow_malloc(SIZEOF_FLOW_STRUCT)) == NULL) { -#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - /* Avoid too much logging while fuzzing */ LOG(NDPI_LOG_ERROR, "[NDPI] %s(2): not enough memory\n", __FUNCTION__); -#endif ndpi_flow_info_free_data(newflow); ndpi_free(newflow); return(NULL); diff --git a/example/reader_util.h b/example/reader_util.h index 6466d9d90..f152c2ecd 100644 --- a/example/reader_util.h +++ b/example/reader_util.h @@ -414,12 +414,11 @@ void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_fl void ndpi_flow_info_free_data(struct ndpi_flow_info *flow); void ndpi_flow_info_freer(void *node); const char* print_cipher_id(u_int32_t cipher); -double ndpi_flow_get_byte_count_entropy(const uint32_t byte_count[256], unsigned int num_bytes); int parse_proto_name_list(char *str, NDPI_PROTOCOL_BITMASK *bitmask, int inverted_logic); extern int nDPI_LogLevel; -#ifdef NDPI_ENABLE_DEBUG_MESSAGES +#if defined(NDPI_ENABLE_DEBUG_MESSAGES) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) #define LOG(log_level, args...) \ { \ if(log_level <= nDPI_LogLevel) \ |