Skip to content

Commit 2da48de

Browse files
baizhenyubraydonk
authored andcommitted
out_stackdriver: fix multiple memory leaks and potential corruption
- Update fetch_metadata to use flb_sds_t * to handle sds reallocation correctly - Use temporary variable in fetch_metadata to avoid leaking original SDS on failure - Fix error handling in fetch_metadata when flb_sds_copy fails - Fix memory leaks in stackdriver.c by destroying rval and http_request in failure paths - Fix memory leak in stackdriver_conf.c by destroying project_id before overwriting it Signed-off-by: Tim Bai <timbai@google.com>
1 parent 992a4e5 commit 2da48de

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

plugins/out_stackdriver/gce_metadata.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
static int fetch_metadata(struct flb_stackdriver *ctx,
3535
struct flb_upstream *upstream, char *uri,
36-
char *payload)
36+
flb_sds_t *payload)
3737
{
3838
int ret;
3939
int ret_code;
@@ -44,21 +44,30 @@ static int fetch_metadata(struct flb_stackdriver *ctx,
4444
/* If runtime test mode is enabled, add test data */
4545
if (ctx->ins->test_mode == FLB_TRUE) {
4646
if (strcmp(uri, FLB_STD_METADATA_PROJECT_ID_URI) == 0) {
47-
if (!flb_sds_cat(payload, "fluent-bit-test", 15)) {
47+
flb_sds_t tmp;
48+
tmp = flb_sds_cat(*payload, "fluent-bit-test", 15);
49+
if (!tmp) {
4850
return -1;
4951
}
52+
*payload = tmp;
5053
return 0;
5154
}
5255
else if (strcmp(uri, FLB_STD_METADATA_ZONE_URI) == 0) {
53-
if (!flb_sds_cat(payload, "projects/0123456789/zones/fluent", 32)) {
56+
flb_sds_t tmp;
57+
tmp = flb_sds_cat(*payload, "projects/0123456789/zones/fluent", 32);
58+
if (!tmp) {
5459
return -1;
5560
}
61+
*payload = tmp;
5662
return 0;
5763
}
5864
else if (strcmp(uri, FLB_STD_METADATA_INSTANCE_ID_URI) == 0) {
59-
if (!flb_sds_cat(payload, "333222111", 9)) {
65+
flb_sds_t tmp;
66+
tmp = flb_sds_cat(*payload, "333222111", 9);
67+
if (!tmp) {
6068
return -1;
6169
}
70+
*payload = tmp;
6271
return 0;
6372
}
6473
return -1;
@@ -93,8 +102,15 @@ static int fetch_metadata(struct flb_stackdriver *ctx,
93102
/* The request was issued successfully, validate the 'error' field */
94103
flb_plg_debug(ctx->ins, "HTTP Status=%i", c->resp.status);
95104
if (c->resp.status == 200) {
96-
ret_code = 0;
97-
flb_sds_copy(payload, c->resp.payload, c->resp.payload_size);
105+
flb_sds_t tmp;
106+
tmp = flb_sds_copy(*payload, c->resp.payload, c->resp.payload_size);
107+
if (!tmp) {
108+
ret_code = -1;
109+
}
110+
else {
111+
*payload = tmp;
112+
ret_code = 0;
113+
}
98114
}
99115
else {
100116
if (c->resp.payload_size > 0) {
@@ -150,7 +166,7 @@ int gce_metadata_read_token(struct flb_stackdriver *ctx)
150166
}
151167
uri = tmp;
152168

153-
ret = fetch_metadata(ctx, ctx->metadata_u, uri, payload);
169+
ret = fetch_metadata(ctx, ctx->metadata_u, uri, &payload);
154170
if (ret != 0) {
155171
flb_plg_error(ctx->ins, "can't fetch token from the metadata server");
156172
flb_sds_destroy(payload);
@@ -180,7 +196,7 @@ int gce_metadata_read_zone(struct flb_stackdriver *ctx)
180196
flb_sds_t zone = NULL;
181197

182198
ret = fetch_metadata(ctx, ctx->metadata_u, FLB_STD_METADATA_ZONE_URI,
183-
payload);
199+
&payload);
184200
if (ret != 0) {
185201
flb_plg_error(ctx->ins, "can't fetch zone from the metadata server");
186202
flb_sds_destroy(payload);
@@ -226,7 +242,7 @@ int gce_metadata_read_project_id(struct flb_stackdriver *ctx)
226242
flb_sds_t payload = flb_sds_create_size(4096);
227243

228244
ret = fetch_metadata(ctx, ctx->metadata_u,
229-
FLB_STD_METADATA_PROJECT_ID_URI, payload);
245+
FLB_STD_METADATA_PROJECT_ID_URI, &payload);
230246
if (ret != 0) {
231247
flb_plg_error(ctx->ins, "can't fetch project id from the metadata server");
232248
flb_sds_destroy(payload);
@@ -243,7 +259,7 @@ int gce_metadata_read_instance_id(struct flb_stackdriver *ctx)
243259
flb_sds_t payload = flb_sds_create_size(4096);
244260

245261
ret = fetch_metadata(ctx, ctx->metadata_u,
246-
FLB_STD_METADATA_INSTANCE_ID_URI, payload);
262+
FLB_STD_METADATA_INSTANCE_ID_URI, &payload);
247263
if (ret != 0) {
248264
flb_plg_error(ctx->ins, "can't fetch instance id from the metadata server");
249265
flb_sds_destroy(payload);

plugins/out_stackdriver/stackdriver.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,9 @@ static int pack_resource_labels(struct flb_stackdriver *ctx,
11641164
} else {
11651165
flb_plg_warn(ctx->ins, "failed to find a corresponding entry for "
11661166
"resource label entry [%s=%s]", label_kv->key, label_kv->val);
1167+
if (rval) {
1168+
flb_ra_key_value_destroy(rval);
1169+
}
11671170
}
11681171
flb_ra_destroy(ra);
11691172
} else {
@@ -2510,6 +2513,8 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
25102513
flb_sds_destroy(log_name);
25112514
}
25122515

2516+
destroy_http_request(&http_request);
2517+
25132518
flb_log_event_decoder_destroy(&log_decoder);
25142519
msgpack_sbuffer_destroy(&mp_sbuf);
25152520

plugins/out_stackdriver/stackdriver_conf.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ static int read_credentials_file(const char *cred_file, struct flb_stackdriver *
134134
ctx->creds->type = flb_sds_create_len(val, val_len);
135135
}
136136
else if (key_cmp(key, key_len, "project_id") == 0) {
137+
if (ctx->project_id) {
138+
flb_sds_destroy(ctx->project_id);
139+
}
137140
ctx->project_id = flb_sds_create_len(val, val_len);
138141
}
139142
else if (key_cmp(key, key_len, "private_key_id") == 0) {

0 commit comments

Comments
 (0)