From 98356f4072186f8644b2fb747c86a84d4f6933bb Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Fri, 3 Nov 2023 10:47:56 -0300 Subject: [PATCH 1/5] in_calyptia_fleet: fix memory leaks when loading new configurations. Free memory during error handling when attempting to load new configurations. Remove code that initializes configurations during testing since it is impossible to free that memory later without crashing fluent-bit. This is due to the use of TLS variables for the co-routine parameters for outputs and how they are freed in flb_output_exit. Signed-off-by: Phillip Whelan --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 24 ++++--------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index 1039a68d20c..a9ca71d0ee1 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -375,6 +375,7 @@ static void *do_reload(void *data) reload->flb->config->enable_hot_reload = FLB_TRUE; reload->flb->config->conf_path_file = reload->cfg_path; + flb_free(reload); sleep(5); #ifndef FLB_SYSTEM_WINDOWS kill(getpid(), SIGHUP); @@ -386,21 +387,14 @@ static void *do_reload(void *data) static int test_config_is_valid(flb_sds_t cfgpath) { - struct flb_config *config; struct flb_cf *conf; int ret = FLB_FALSE; - config = flb_config_init(); - - if (config == NULL) { - goto config_init_error; - } - conf = flb_cf_create(); if (conf == NULL) { - goto cf_create_error; + goto config_init_error; } conf = flb_cf_create_from_file(conf, cfgpath); @@ -409,22 +403,10 @@ static int test_config_is_valid(flb_sds_t cfgpath) goto cf_create_from_file_error; } - if (flb_config_load_config_format(config, conf)) { - goto cf_load_config_format_error; - } - - if (flb_reload_property_check_all(config)) { - goto cf_property_check_error; - } - ret = FLB_TRUE; -cf_property_check_error: -cf_load_config_format_error: cf_create_from_file_error: flb_cf_destroy(conf); -cf_create_error: - flb_config_exit(config); config_init_error: return ret; } @@ -447,6 +429,7 @@ static int execute_reload(struct flb_in_calyptia_fleet_config *ctx, flb_sds_t cf flb_input_collector_resume(ctx->collect_fd, ctx->ins); } + flb_sds_destroy(cfgpath); return FLB_FALSE; } @@ -463,6 +446,7 @@ static int execute_reload(struct flb_in_calyptia_fleet_config *ctx, flb_sds_t cf flb_input_collector_resume(ctx->collect_fd, ctx->ins); } + flb_sds_destroy(cfgpath); return FLB_FALSE; } From 3feb50261b646b2165d736db1f10bdb8e778ad6d Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Fri, 3 Nov 2023 10:55:42 -0300 Subject: [PATCH 2/5] in_calyptia_fleet: remove premature return which leads to a memory leak. Signed-off-by: Phillip Whelan --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index a9ca71d0ee1..0815b895785 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -938,9 +938,6 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, flb_sds_destroy(cfgoldname); goto reload_error; } - else { - FLB_INPUT_RETURN(0); - } } ret = 0; From 160cc5b41a986e85c72381a5fe82e77d15536f70 Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Fri, 3 Nov 2023 12:04:49 -0300 Subject: [PATCH 3/5] in_calyptia_fleet: free fleet_url and fleet_id. Signed-off-by: Phillip Whelan --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index 0815b895785..ca498b48a3e 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -67,7 +67,11 @@ struct flb_in_calyptia_fleet_config { long config_timestamp; flb_sds_t api_key; + flb_sds_t fleet_id; + /* flag used to mark fleet_id for release when found automatically. */ + int fleet_id_found; + flb_sds_t fleet_name; flb_sds_t machine_id; flb_sds_t config_dir; @@ -593,6 +597,7 @@ static ssize_t parse_fleet_search_json(struct flb_in_calyptia_fleet_config *ctx, return -1; } + ctx->fleet_id_found = 1; ctx->fleet_id = flb_sds_create_len(cur->val.via.str.ptr, cur->val.via.str.size); break; @@ -1223,6 +1228,14 @@ static int in_calyptia_fleet_exit(void *data, struct flb_config *config) (void) *config; struct flb_in_calyptia_fleet_config *ctx = (struct flb_in_calyptia_fleet_config *)data; + if (ctx->fleet_url) { + flb_sds_destroy(ctx->fleet_url); + } + + if (ctx->fleet_id && ctx->fleet_id_found) { + flb_sds_destroy(ctx->fleet_id); + } + flb_input_collector_delete(ctx->collect_fd, ctx->ins); flb_upstream_destroy(ctx->u); flb_free(ctx); From c5aebcee4e6c67707576416c52549784ee60b6e2 Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Fri, 3 Nov 2023 12:05:35 -0300 Subject: [PATCH 4/5] in_calyptia_fleet: free http client when retrieving fleet_id from fleet_name. Signed-off-by: Phillip Whelan --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index ca498b48a3e..d81628e5772 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -686,27 +686,34 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct if (ret != 0) { flb_plg_error(ctx->ins, "http do error"); + flb_http_client_destroy(client); return -1; } if (client->resp.status != 200) { flb_plg_error(ctx->ins, "search http status code error: %d", client->resp.status); + flb_http_client_destroy(client); return -1; } if (client->resp.payload_size <= 0) { flb_plg_error(ctx->ins, "empty response"); + flb_http_client_destroy(client); return -1; } if (parse_fleet_search_json(ctx, client->resp.payload, client->resp.payload_size) == -1) { flb_plg_error(ctx->ins, "unable to find fleet: %s", ctx->fleet_name); + flb_http_client_destroy(client); return -1; } if (ctx->fleet_id == NULL) { + flb_http_client_destroy(client); return -1; } + + flb_http_client_destroy(client); return 0; } From 6d9d9bee580c93a4b80951d9d8a804becd4bcbcb Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Fri, 3 Nov 2023 12:41:02 -0300 Subject: [PATCH 5/5] in_calyptia_fleet: free up filenames and data during collect callback. Signed-off-by: Phillip Whelan --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index d81628e5772..b2f48ea6e1a 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -851,6 +851,7 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, if (cfgfp == NULL) { flb_plg_error(ctx->ins, "unable to open configuration file: %s", cfgname); + flb_sds_destroy(cfgname); goto payload_error; } @@ -900,12 +901,14 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, } if (hdr == NULL) { fclose(cfgfp); + flb_sds_destroy(cfgname); goto header_error; } if (ctx->machine_id) { hdr = flb_sds_printf(&header, " machine_id %s\n", ctx->machine_id); if (hdr == NULL) { fclose(cfgfp); + flb_sds_destroy(cfgname); goto header_error; } } @@ -934,13 +937,13 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, flb_errno(); #endif } + + flb_sds_destroy(cfgnewname); } if (ctx->config_timestamp < time_last_modified) { flb_plg_debug(ctx->ins, "new configuration is newer than current: %ld < %ld", ctx->config_timestamp, time_last_modified); - flb_sds_destroy(data); - data = NULL; if (execute_reload(ctx, cfgname) == FLB_FALSE) { cfgoldname = old_fleet_config_filename(ctx); @@ -948,9 +951,13 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, rename(cfgoldname, cfgcurname); flb_sds_destroy(cfgcurname); flb_sds_destroy(cfgoldname); + flb_sds_destroy(cfgname); goto reload_error; } } + else { + flb_sds_destroy(cfgname); + } ret = 0; @@ -965,6 +972,7 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, } http_error: + flb_plg_debug(ctx->ins, "freeing http client in fleet collect"); flb_http_client_destroy(client); client_error: flb_upstream_conn_release(u_conn);