From b2c8cb655c8d70dafe67bad8da8099a4e8c6a517 Mon Sep 17 00:00:00 2001 From: emanuele-f Date: Tue, 8 Oct 2019 10:12:41 +0200 Subject: ndpi_load_hostname_category now performs strdup on the name argument This simplifies the API as an application is not required to keep references to the strings to free --- example/ndpiReader.c | 3 +-- src/include/ndpi_api.h | 4 ++-- src/lib/ndpi_main.c | 26 ++++++++++++-------------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 2a1bbd8bb..d44fae0d6 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -1903,9 +1903,8 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle) { ndpi_load_ip_category(ndpi_thread_info[thread_id].workflow->ndpi_struct, name, (ndpi_protocol_category_t)atoi(category)); else { - /* TODO free the strdup */ ndpi_load_hostname_category(ndpi_thread_info[thread_id].workflow->ndpi_struct, - strdup(name), (ndpi_protocol_category_t)atoi(category)); + name, (ndpi_protocol_category_t)atoi(category)); } } } diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index 6bf2d5779..37291d1b7 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -745,9 +745,9 @@ extern "C" { int ndpi_match_string(void *_automa, char *string_to_match); void ndpi_load_ip_category(struct ndpi_detection_module_struct *ndpi_struct, - char *ip_address_and_mask, ndpi_protocol_category_t category); + const char *ip_address_and_mask, ndpi_protocol_category_t category); int ndpi_load_hostname_category(struct ndpi_detection_module_struct *ndpi_struct, - char *name, ndpi_protocol_category_t category); + const char *name_to_add, ndpi_protocol_category_t category); int ndpi_enable_loaded_categories(struct ndpi_detection_module_struct *ndpi_struct); int ndpi_fill_ip_protocol_category(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t saddr, diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index 61fbbcd07..f3a23d90c 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -2411,10 +2411,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, 0); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames.ac_automa, 1 /* free patterns strings memory */); if(ndpi_str->custom_categories.hostnames_shadow.ac_automa != NULL) - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames_shadow.ac_automa, 0); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames_shadow.ac_automa, 1 /* free patterns strings memory */); #endif if(ndpi_str->custom_categories.ipAddresses != NULL) @@ -4233,7 +4233,7 @@ void ndpi_process_extra_packet(struct ndpi_detection_module_struct *ndpi_str, /* ********************************************************************************* */ void ndpi_load_ip_category(struct ndpi_detection_module_struct *ndpi_str, - char *ip_address_and_mask, ndpi_protocol_category_t category) { + const char *ip_address_and_mask, ndpi_protocol_category_t category) { patricia_node_t *node; struct in_addr pin; int bits = 32; @@ -4263,16 +4263,14 @@ void ndpi_load_ip_category(struct ndpi_detection_module_struct *ndpi_str, /* ********************************************************************************* */ -/* - * - * IMPORTANT - * - * The *name pointer MUST be kept allocated until the automa is finalized and it - * cannot be recycled across multiple ndpi_load_hostname_category() calls - * - */ int ndpi_load_hostname_category(struct ndpi_detection_module_struct *ndpi_str, - char *name, ndpi_protocol_category_t category) { + const char *name_to_add, ndpi_protocol_category_t category) { + char *name; + + if(name_to_add == NULL) + return(-1); + + name = ndpi_strdup(name_to_add); if(name == NULL) return(-1); @@ -4286,7 +4284,7 @@ int ndpi_load_hostname_category(struct ndpi_detection_module_struct *ndpi_str, struct hs_list *h = (struct hs_list*)ndpi_malloc(sizeof(struct hs_list)); if(h) { - h->expression = ndpi_strdup(name), h->id = (unsigned int)category; + h->expression = name, h->id = (unsigned int)category; if(h->expression == NULL) { ndpi_free(h); return(-2); @@ -4398,7 +4396,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, 0); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames.ac_automa, 1 /* free patterns strings memory */); /* Finalize */ ac_automata_finalize((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames_shadow.ac_automa); -- cgit v1.2.3 From e939f1c1d1bfa744d430e64305010bf918c5dfbe Mon Sep 17 00:00:00 2001 From: emanuele-f Date: Tue, 8 Oct 2019 11:07:59 +0200 Subject: Fix leaks in ndpi_load_hostname_category when ac_automata_add fails --- src/lib/ndpi_main.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index f3a23d90c..c5f2f6995 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -4285,29 +4285,31 @@ int ndpi_load_hostname_category(struct ndpi_detection_module_struct *ndpi_str, if(h) { h->expression = name, h->id = (unsigned int)category; - if(h->expression == NULL) { - ndpi_free(h); - return(-2); - } - h->next = ndpi_str->custom_categories.to_load; ndpi_str->custom_categories.to_load = h; ndpi_str->custom_categories.num_to_load++; - } else - return(-1); + } else { + free(name); + return(-1); + } } #else AC_PATTERN_t ac_pattern; memset(&ac_pattern, 0, sizeof(ac_pattern)); - if(ndpi_str->custom_categories.hostnames_shadow.ac_automa == NULL) + if(ndpi_str->custom_categories.hostnames_shadow.ac_automa == NULL) { + free(name); return(-1); + } ac_pattern.astring = name, ac_pattern.length = strlen(ac_pattern.astring); ac_pattern.rep.number = (int)category; - ac_automata_add(ndpi_str->custom_categories.hostnames_shadow.ac_automa, &ac_pattern); + if(ac_automata_add(ndpi_str->custom_categories.hostnames_shadow.ac_automa, &ac_pattern) != ACERR_SUCCESS) { + free(name); + return(-1); + } #endif return(0); -- cgit v1.2.3