Fixes leaks in ndpi_add_host_url_subprotocol

It is now possible to deallocate strings in ac_automata_release via
an additional parameter
This commit is contained in:
emanuele-f
2019-10-02 14:02:19 +02:00
parent 6a22bee2ca
commit b19bfa1e20
5 changed files with 29 additions and 29 deletions
+18 -24
View File
@@ -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);
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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);
+3 -2
View File
@@ -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);
+6 -1
View File
@@ -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);