From be7b38717d0b35e0bc752e871c3fb2e7d349551a Mon Sep 17 00:00:00 2001 From: Wesley Pettit Date: Thu, 9 Mar 2023 15:25:05 -0800 Subject: [PATCH] aws_util: memory fixes aws_util: always use flb_calloc, never flb_malloc Signed-off-by: Wesley Pettit out_s3: always use flb_calloc, never flb_malloc Signed-off-by: Wesley Pettit aws_util: fix tmp leak and fix formatting Signed-off-by: David Korczynski config: fix possible null-dereference flb_env_create can fail which causes config->env to be NULL, and this can cause NULL-dereferences further in the process. Bail if environment creation failed. Consequently, we need to add a safeguard for cleaning up multiline parsers as these have not been initialized yet. Signed-off-by: David Korczynski --- plugins/out_s3/s3.c | 10 +++++----- src/aws/flb_aws_util.c | 11 +++++++---- src/flb_config.c | 5 +++++ src/multiline/flb_ml_parser.c | 5 +++++ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/plugins/out_s3/s3.c b/plugins/out_s3/s3.c index 8840f82821e..b4556158000 100644 --- a/plugins/out_s3/s3.c +++ b/plugins/out_s3/s3.c @@ -103,7 +103,7 @@ static char *mock_error_response(char *error_env_var) err_val = getenv(error_env_var); if (err_val != NULL && strlen(err_val) > 0) { - error = flb_malloc(strlen(err_val) + sizeof(char)); + error = flb_calloc(strlen(err_val) + 1, sizeof(char)); if (error == NULL) { flb_errno(); return NULL; @@ -156,7 +156,7 @@ int create_headers(struct flb_s3 *ctx, char *body_md5, return 0; } - s3_headers = flb_malloc(sizeof(struct flb_aws_header) * headers_len); + s3_headers = flb_calloc(headers_len, sizeof(struct flb_aws_header)); if (s3_headers == NULL) { flb_errno(); return -1; @@ -241,7 +241,7 @@ struct flb_http_client *mock_s3_call(char *error_env_var, char *api) "Server: AmazonS3"; /* since etag is in the headers, this code uses resp.data */ len = strlen(resp); - c->resp.data = flb_malloc(len + 1); + c->resp.data = flb_calloc(len + 1, sizeof(char)); if (!c->resp.data) { flb_errno(); return NULL; @@ -1558,7 +1558,7 @@ static int add_to_queue(struct flb_s3 *ctx, struct s3_file *upload_file, flb_sds_t tag_cpy; /* Create upload contents object and add to upload queue */ - upload_contents = flb_malloc(sizeof(struct upload_queue)); + upload_contents = flb_calloc(1, sizeof(struct upload_queue)); if (upload_contents == NULL) { flb_plg_error(ctx->ins, "Error allocating memory for upload_queue entry"); flb_errno(); @@ -1888,7 +1888,7 @@ static flb_sds_t flb_pack_msgpack_extract_log_key(void *out_context, const char } /* Allocate buffer to store log_key contents */ - val_buf = flb_malloc(msgpack_size); + val_buf = flb_calloc(1, msgpack_size); if (val_buf == NULL) { flb_plg_error(ctx->ins, "Could not allocate enough " "memory to read record"); diff --git a/src/aws/flb_aws_util.c b/src/aws/flb_aws_util.c index 61d09bf5a13..cc78c0cdeb4 100644 --- a/src/aws/flb_aws_util.c +++ b/src/aws/flb_aws_util.c @@ -94,7 +94,7 @@ char *flb_aws_endpoint(char* service, char* region) len += strlen(region); len++; /* null byte */ - endpoint = flb_malloc(len); + endpoint = flb_calloc(len, sizeof(char)); if (!endpoint) { flb_errno(); return NULL; @@ -136,7 +136,7 @@ int flb_read_file(const char *path, char **out_buf, size_t *out_size) return -1; } - buf = flb_malloc(st.st_size + sizeof(char)); + buf = flb_calloc(st.st_size + 1, sizeof(char)); if (!buf) { flb_errno(); close(fd); @@ -851,7 +851,7 @@ flb_sds_t flb_get_s3_key(const char *format, time_t time, const char *tag, /* Find all occurences of $INDEX and replace with the appropriate index. */ if (strstr((char *) format, INDEX_STRING)) { seq_index_len = snprintf(NULL, 0, "%"PRIu64, seq_index); - seq_index_str = flb_malloc(seq_index_len + 1); + seq_index_str = flb_calloc(seq_index_len + 1, sizeof(char)); if (seq_index_str == NULL) { goto error; } @@ -859,7 +859,10 @@ flb_sds_t flb_get_s3_key(const char *format, time_t time, const char *tag, sprintf(seq_index_str, "%"PRIu64, seq_index); seq_index_str[seq_index_len] = '\0'; tmp_key = replace_uri_tokens(s3_key, INDEX_STRING, seq_index_str); - + if (tmp_key == NULL) { + flb_free(seq_index_str); + goto error; + } if (strlen(tmp_key) > S3_KEY_SIZE) { flb_warn("[s3_key] Object key length is longer than the 1024 character limit."); } diff --git a/src/flb_config.c b/src/flb_config.c index c6782ad3654..6ecfce81816 100644 --- a/src/flb_config.c +++ b/src/flb_config.c @@ -287,6 +287,11 @@ struct flb_config *flb_config_init() /* Environment */ config->env = flb_env_create(); + if (!config->env) { + flb_error("[config] environment creation failed"); + flb_config_exit(config); + return NULL; + } /* Multiline core */ mk_list_init(&config->multiline_parsers); diff --git a/src/multiline/flb_ml_parser.c b/src/multiline/flb_ml_parser.c index 2a672605691..441f9caf578 100644 --- a/src/multiline/flb_ml_parser.c +++ b/src/multiline/flb_ml_parser.c @@ -333,6 +333,11 @@ void flb_ml_parser_destroy_all(struct mk_list *list) struct mk_list *head; struct flb_ml_parser *parser; + /* Ensure list is initialized */ + if (list->next == NULL) { + return; + } + mk_list_foreach_safe(head, tmp, list) { parser = mk_list_entry(head, struct flb_ml_parser, _head); flb_ml_parser_destroy(parser);