From 083b2b0e3a5084f97f035bf214c305f6e32c55ef Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Thu, 26 Oct 2023 11:28:33 -0300 Subject: [PATCH] in_calyptia_fleet: improve return value and parameter checking. Signed-off-by: Phillip Whelan --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 93 ++++++++++++++++++- 1 file changed, 89 insertions(+), 4 deletions(-) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index caeb0d1b8fa..af46b758c10 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -226,6 +226,10 @@ static flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx, flb_sds_t cfgname = NULL; flb_sds_t ret; + if (ctx == NULL || fname == NULL) { + return NULL; + } + if (generate_base_fleet_directory(ctx, &cfgname) == NULL) { return NULL; } @@ -257,11 +261,19 @@ static int is_new_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct int ret = FLB_FALSE; + if (cfg == NULL) { + return FLB_FALSE; + } + if (cfg->conf_path_file == NULL) { return FLB_FALSE; } cfgnewname = new_fleet_config_filename(ctx); + if (cfgnewname == NULL) { + flb_plg_error(ctx->ins, "unable to allocate configuration name"); + return FLB_FALSE; + } if (strcmp(cfgnewname, cfg->conf_path_file) == 0) { ret = FLB_TRUE; @@ -277,12 +289,19 @@ static int is_cur_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct flb_sds_t cfgcurname; int ret = FLB_FALSE; + if (cfg == NULL) { + return FLB_FALSE; + } if (cfg->conf_path_file == NULL) { return FLB_FALSE; } cfgcurname = cur_fleet_config_filename(ctx); + if (cfgcurname == NULL) { + flb_plg_error(ctx->ins, "unable to allocate configuration name"); + return FLB_FALSE; + } if (strcmp(cfgcurname, cfg->conf_path_file) == 0) { ret = FLB_TRUE; @@ -299,6 +318,10 @@ static int is_timestamped_fleet_config(struct flb_in_calyptia_fleet_config *ctx, char *end; long val; + if (cfg == NULL) { + return FLB_FALSE; + } + if (cfg->conf_path_file == NULL) { return FLB_FALSE; } @@ -329,6 +352,10 @@ static int is_timestamped_fleet_config(struct flb_in_calyptia_fleet_config *ctx, static int is_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct flb_config *cfg) { + if (cfg == NULL) { + return FLB_FALSE; + } + if (cfg->conf_path_file == NULL) { return FLB_FALSE; } @@ -345,6 +372,11 @@ static int exists_new_fleet_config(struct flb_in_calyptia_fleet_config *ctx) cfgnewname = new_fleet_config_filename(ctx); + if (cfgnewname == NULL) { + flb_plg_error(ctx->ins, "unable to allocate configuration name"); + return FLB_FALSE; + } + ret = access(cfgnewname, F_OK) == 0 ? FLB_TRUE : FLB_FALSE; flb_sds_destroy(cfgnewname); @@ -358,6 +390,11 @@ static int exists_cur_fleet_config(struct flb_in_calyptia_fleet_config *ctx) cfgcurname = cur_fleet_config_filename(ctx); + if (cfgcurname == NULL) { + flb_plg_error(ctx->ins, "unable to allocate configuration name"); + return FLB_FALSE; + } + ret = access(cfgcurname, F_OK) == 0 ? FLB_TRUE : FLB_FALSE; flb_sds_destroy(cfgcurname); @@ -371,6 +408,11 @@ static int exists_old_fleet_config(struct flb_in_calyptia_fleet_config *ctx) cfgoldname = old_fleet_config_filename(ctx); + if (cfgoldname == NULL) { + flb_plg_error(ctx->ins, "unable to allocate configuration name"); + return FLB_FALSE; + } + ret = access(cfgoldname, F_OK) == 0 ? FLB_TRUE : FLB_FALSE; flb_sds_destroy(cfgoldname); @@ -381,6 +423,10 @@ static void *do_reload(void *data) { struct reload_ctx *reload = (struct reload_ctx *)data; + if (reload == NULL) { + return NULL; + } + /* avoid reloading the current configuration... just use our new one! */ flb_context_set(reload->flb); reload->flb->config->enable_hot_reload = FLB_TRUE; @@ -401,6 +447,9 @@ static int test_config_is_valid(flb_sds_t cfgpath) struct flb_cf *conf; int ret = FLB_FALSE; + if (cfgpath == NULL) { + return FLB_FALSE; + } config = flb_config_init(); @@ -450,7 +499,7 @@ static int parse_config_name_timestamp(struct flb_in_calyptia_fleet_config *ctx, char *fname; ssize_t len; - if (config_timestamp == NULL || cfgpath == NULL) { + if (ctx == NULL || config_timestamp == NULL || cfgpath == NULL) { return FLB_FALSE; } @@ -486,7 +535,7 @@ static int parse_config_timestamp(struct flb_in_calyptia_fleet_config *ctx, { flb_ctx_t *flb_ctx = flb_context_get(); - if (config_timestamp == NULL) { + if (ctx == NULL || config_timestamp == NULL) { return FLB_FALSE; } @@ -501,7 +550,9 @@ static int execute_reload(struct flb_in_calyptia_fleet_config *ctx, flb_sds_t cf flb_ctx_t *flb = flb_context_get(); - parse_config_name_timestamp(ctx, cfgpath, &ctx->config_timestamp); + if (parse_config_name_timestamp(ctx, cfgpath, &ctx->config_timestamp) != FLB_TRUE) { + return FLB_FALSE; + } if (ctx->collect_fd > 0) { flb_input_collector_pause(ctx->collect_fd, ctx->ins); @@ -614,6 +665,10 @@ static flb_sds_t parse_api_key_json(struct flb_in_calyptia_fleet_config *ctx, msgpack_object *projectID; flb_sds_t project_id = NULL; + if (ctx == NULL || payload == NULL) { + return NULL; + } + /* Initialize packer */ flb_pack_state_init(&pack_state); @@ -667,6 +722,10 @@ static ssize_t parse_fleet_search_json(struct flb_in_calyptia_fleet_config *ctx, msgpack_object *map; msgpack_object *fleet; + if (ctx == NULL || payload == NULL) { + return -1; + } + /* Initialize packer */ flb_pack_state_init(&pack_state); @@ -722,6 +781,10 @@ static flb_sds_t get_project_id_from_api_key(struct flb_in_calyptia_fleet_config size_t elen; int ret; + if (ctx == NULL) { + return NULL; + } + api_token_sep = strchr(ctx->api_key, '.'); if (api_token_sep == NULL) { @@ -757,6 +820,10 @@ static struct flb_http_client *fleet_http_do(struct flb_in_calyptia_fleet_config size_t b_sent; int ret = -1; + if (ctx == NULL || u_conn == NULL || url == NULL) { + return NULL; + } + client = flb_http_client(u_conn, FLB_HTTP_GET, url, NULL, 0, ctx->ins->host.name, ctx->ins->host.port, NULL, 0); @@ -804,6 +871,10 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct flb_sds_t url; flb_sds_t project_id; + if (ctx == NULL || u_conn == NULL || config == NULL) { + return -1; + } + project_id = get_project_id_from_api_key(ctx); if (project_id == NULL) { @@ -1103,6 +1174,9 @@ static int calyptia_config_add(struct flb_in_calyptia_fleet_config *ctx, flb_sds_t cfgoldname; cfgnewname = new_fleet_config_filename(ctx); + if (cfgnewname == NULL) { + return -1; + } if (exists_new_fleet_config(ctx) == FLB_TRUE) { cfgoldname = old_fleet_config_filename(ctx); @@ -1111,7 +1185,10 @@ static int calyptia_config_add(struct flb_in_calyptia_fleet_config *ctx, flb_sds_destroy(cfgoldname); } - symlink(cfgname, cfgnewname); + if (symlink(cfgname, cfgnewname)) { + flb_sds_destroy(cfgnewname); + return -1; + } flb_sds_destroy(cfgnewname); return 0; @@ -1152,6 +1229,10 @@ static int calyptia_config_delete_old_dir(const char *cfgpath) struct cfl_array *files; int idx; + if (cfgpath == NULL) { + return FLB_FALSE; + } + ext = strrchr(cfgpath, '.'); if (ext == NULL) { return FLB_FALSE; @@ -1188,6 +1269,10 @@ static int calyptia_config_delete_old(struct flb_in_calyptia_fleet_config *ctx) flb_sds_t glob_ini = NULL; int idx; + if (ctx == NULL) { + return -1; + } + if (generate_base_fleet_directory(ctx, &glob_ini) == NULL) { flb_sds_destroy(glob_ini); return -1;