From 0fa3f7922b65aecb69e224fa8d858996dfd26301 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 377a8367638..179124b4c1a 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -224,6 +224,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; } @@ -255,11 +259,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; @@ -275,12 +287,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; @@ -326,6 +345,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; } @@ -356,6 +379,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; } @@ -373,6 +400,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); @@ -386,6 +418,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); @@ -399,6 +436,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); @@ -409,6 +451,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; @@ -429,6 +475,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(); @@ -478,7 +527,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; } @@ -514,7 +563,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; } @@ -529,7 +578,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); @@ -642,6 +693,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); @@ -695,6 +750,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); @@ -750,6 +809,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) { @@ -785,6 +848,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); @@ -832,6 +899,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) { @@ -1131,6 +1202,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); @@ -1139,7 +1213,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; @@ -1180,6 +1257,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; @@ -1216,6 +1297,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;