diff options
author | emanuele-f <black.silver@hotmail.it> | 2019-10-02 14:02:19 +0200 |
---|---|---|
committer | emanuele-f <black.silver@hotmail.it> | 2019-10-02 14:02:19 +0200 |
commit | b19bfa1e207a8d4972bfc701fde5d5c014f95383 (patch) | |
tree | 1f1b6a375ae2698ffafb8b51af33effbb2a51212 | |
parent | 6a22bee2ca4675b01d976b2d41d7eba5de3bb8be (diff) |
Fixes leaks in ndpi_add_host_url_subprotocol
It is now possible to deallocate strings in ac_automata_release via
an additional parameter
-rw-r--r-- | src/lib/ndpi_main.c | 42 | ||||
-rw-r--r-- | src/lib/third_party/include/ahocorasick.h | 2 | ||||
-rw-r--r-- | src/lib/third_party/include/node.h | 2 | ||||
-rw-r--r-- | src/lib/third_party/src/ahocorasick.c | 5 | ||||
-rw-r--r-- | src/lib/third_party/src/node.c | 7 |
5 files changed, 29 insertions, 29 deletions
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index feddae6e2..4db0fdaa9 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -447,42 +447,36 @@ static int ndpi_string_to_automa(struct ndpi_detection_module_struct *ndpi_str, else ac_pattern.length = strlen(ac_pattern.astring); - ac_automata_add(((AC_AUTOMATA_t*)automa->ac_automa), &ac_pattern); + if(ac_automata_add(((AC_AUTOMATA_t*)automa->ac_automa), &ac_pattern) != ACERR_SUCCESS) + return(-2); return(0); } /* ****************************************************** */ -/* - TODO - This function should free the memory during program termination - - */ -static void ndpi_free_memory_at_termination(struct ndpi_detection_module_struct *ndpi_str, char *buf) { - - -} - -/* ****************************************************** */ - static int ndpi_add_host_url_subprotocol(struct ndpi_detection_module_struct *ndpi_str, char *_value, int protocol_id, ndpi_protocol_category_t category, ndpi_protocol_breed_t breed) { + int rv; char *value = ndpi_strdup(_value); - if(!value) return(-1); else ndpi_free_memory_at_termination(ndpi_str, value); + if(!value) return(-1); #ifdef DEBUG NDPI_LOG_DEBUG2(ndpi_str, "[NDPI] Adding [%s][%d]\n", value, protocol_id); #endif - return(ndpi_string_to_automa(ndpi_str, + rv = ndpi_string_to_automa(ndpi_str, &ndpi_str->host_automa, value, protocol_id, - category, breed)); + category, breed); + + if(rv != 0) ndpi_free(value); + + return(rv); } /* ****************************************************** */ @@ -2207,7 +2201,7 @@ int ndpi_add_string_to_automa(void *_automa, char *str) { return(ndpi_add_string_value_to_automa(_automa, str, 1)); } -void ndpi_free_automa(void *_automa) { ac_automata_release((AC_AUTOMATA_t*)_automa); } +void ndpi_free_automa(void *_automa) { ac_automata_release((AC_AUTOMATA_t*)_automa, 0); } void ndpi_finalize_automa(void *_automa) { ac_automata_finalize((AC_AUTOMATA_t*)_automa); } /* ****************************************************** */ @@ -2385,16 +2379,16 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_str) { ndpi_tdestroy(ndpi_str->tcpRoot, ndpi_free); if(ndpi_str->host_automa.ac_automa != NULL) - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->host_automa.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->host_automa.ac_automa, 1 /* free patterns strings memory */); if(ndpi_str->content_automa.ac_automa != NULL) - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->content_automa.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->content_automa.ac_automa, 0); if(ndpi_str->bigrams_automa.ac_automa != NULL) - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->bigrams_automa.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->bigrams_automa.ac_automa, 0); if(ndpi_str->impossible_bigrams_automa.ac_automa != NULL) - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->impossible_bigrams_automa.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->impossible_bigrams_automa.ac_automa, 0); #ifdef HAVE_HYPERSCAN destroy_hyperscan(ndpi_str); @@ -2410,10 +2404,10 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_str) { free_hyperscan_memory(ndpi_str->custom_categories.hostnames); #else if(ndpi_str->custom_categories.hostnames.ac_automa != NULL) - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames.ac_automa, 0); if(ndpi_str->custom_categories.hostnames_shadow.ac_automa != NULL) - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames_shadow.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames_shadow.ac_automa, 0); #endif if(ndpi_str->custom_categories.ipAddresses != NULL) @@ -4375,7 +4369,7 @@ int ndpi_enable_loaded_categories(struct ndpi_detection_module_struct *ndpi_str) } #else /* Free */ - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames.ac_automa, 0); /* Finalize */ ac_automata_finalize((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames_shadow.ac_automa); diff --git a/src/lib/third_party/include/ahocorasick.h b/src/lib/third_party/include/ahocorasick.h index 192a0e728..74812bef1 100644 --- a/src/lib/third_party/include/ahocorasick.h +++ b/src/lib/third_party/include/ahocorasick.h @@ -63,7 +63,7 @@ AC_ERROR_t ac_automata_add (AC_AUTOMATA_t * thiz, AC_PATTERN_t * str); void ac_automata_finalize (AC_AUTOMATA_t * thiz); int ac_automata_search (AC_AUTOMATA_t * thiz, AC_TEXT_t * str, AC_REP_t * param); void ac_automata_reset (AC_AUTOMATA_t * thiz); -void ac_automata_release (AC_AUTOMATA_t * thiz); +void ac_automata_release (AC_AUTOMATA_t * thiz, u_int8_t free_pattern); void ac_automata_display (AC_AUTOMATA_t * thiz, char repcast); #endif diff --git a/src/lib/third_party/include/node.h b/src/lib/third_party/include/node.h index 13cf2ff11..30836df6a 100644 --- a/src/lib/third_party/include/node.h +++ b/src/lib/third_party/include/node.h @@ -59,7 +59,7 @@ void node_register_matchstr (AC_NODE_t * thiz, AC_PATTERN_t * str); void node_register_outgoing (AC_NODE_t * thiz, AC_NODE_t * next, AC_ALPHABET_t alpha); AC_NODE_t * node_find_next (AC_NODE_t * thiz, AC_ALPHABET_t alpha); AC_NODE_t * node_findbs_next (AC_NODE_t * thiz, AC_ALPHABET_t alpha); -void node_release (AC_NODE_t * thiz); +void node_release (AC_NODE_t * thiz, u_int8_t free_pattern); void node_assign_id (AC_NODE_t * thiz); void node_sort_edges (AC_NODE_t * thiz); diff --git a/src/lib/third_party/src/ahocorasick.c b/src/lib/third_party/src/ahocorasick.c index 371dc06f8..db398cfcd 100644 --- a/src/lib/third_party/src/ahocorasick.c +++ b/src/lib/third_party/src/ahocorasick.c @@ -227,8 +227,9 @@ void ac_automata_reset (AC_AUTOMATA_t * thiz) * Release all allocated memories to the automata * PARAMS: * AC_AUTOMATA_t * thiz: the pointer to the automata + * uint8_t free_pattern: if true, deallocate the patterns strings ******************************************************************************/ -void ac_automata_release (AC_AUTOMATA_t * thiz) +void ac_automata_release (AC_AUTOMATA_t * thiz, u_int8_t free_pattern) { unsigned int i; AC_NODE_t * n; @@ -236,7 +237,7 @@ void ac_automata_release (AC_AUTOMATA_t * thiz) for (i=0; i < thiz->all_nodes_num; i++) { n = thiz->all_nodes[i]; - node_release(n); + node_release(n, free_pattern); } ndpi_free(thiz->all_nodes); ndpi_free(thiz); diff --git a/src/lib/third_party/src/node.c b/src/lib/third_party/src/node.c index 4da04de9b..3eda50461 100644 --- a/src/lib/third_party/src/node.c +++ b/src/lib/third_party/src/node.c @@ -73,8 +73,13 @@ void node_init(AC_NODE_t * thiz) * FUNCTION: node_release * Release node ******************************************************************************/ -void node_release(AC_NODE_t * thiz) +void node_release(AC_NODE_t * thiz, u_int8_t free_pattern) { + if(free_pattern) { + for(int i=0; i<thiz->matched_patterns_num; i++) + ndpi_free(thiz->matched_patterns[i].astring); + } + ndpi_free(thiz->matched_patterns); ndpi_free(thiz->outgoing); ndpi_free(thiz); |