Repository navigation
out_s3, out_chronicle: grow log_key buffer to avoid dropping oversized values #12137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4475,9 +4475,12 @@ static flb_sds_t flb_pack_msgpack_extract_log_key(void *out_context, const char | |
| int alloc_error = 0; | ||
| struct flb_s3 *ctx = out_context; | ||
| char *val_buf; | ||
| char *tmp_buf; | ||
| char *key_str = NULL; | ||
| size_t key_str_size = 0; | ||
| size_t msgpack_size = bytes + bytes / 4; | ||
| size_t new_size; | ||
| size_t avail; | ||
| size_t val_offset = 0; | ||
| flb_sds_t out_buf; | ||
| msgpack_object map; | ||
|
|
@@ -4570,10 +4573,33 @@ static flb_sds_t flb_pack_msgpack_extract_log_key(void *out_context, const char | |
| val_offset++; | ||
| } | ||
| else { | ||
| ret = flb_msgpack_to_json(val_buf + val_offset, | ||
| msgpack_size - val_offset, &val, | ||
| config->json_escape_unicode); | ||
| if (ret < 0) { | ||
| /* | ||
| * Serialize the value as JSON, growing the scratch | ||
| * buffer and retrying if it does not fit, so an | ||
| * oversized value is neither truncated nor dropped. | ||
| */ | ||
| ret = -1; | ||
| while (1) { | ||
| avail = msgpack_size - val_offset; | ||
| if (avail > 1) { | ||
| ret = flb_msgpack_to_json(val_buf + val_offset, | ||
| avail, &val, | ||
| config->json_escape_unicode); | ||
| if (ret > 0) { | ||
| break; | ||
| } | ||
| } | ||
| new_size = msgpack_size * 2; | ||
| tmp_buf = flb_realloc(val_buf, new_size); | ||
| if (tmp_buf == NULL) { | ||
| flb_errno(); | ||
| alloc_error = 1; | ||
| break; | ||
|
Comment on lines
+4596
to
+4597
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For S3 chunks using Useful? React with 👍 / 👎. |
||
| } | ||
| val_buf = tmp_buf; | ||
| msgpack_size = new_size; | ||
| } | ||
| if (alloc_error == 1) { | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| break; | ||
| } | ||
| val_offset += ret; | ||
|
|
@@ -4600,6 +4626,15 @@ static flb_sds_t flb_pack_msgpack_extract_log_key(void *out_context, const char | |
|
|
||
| flb_log_event_decoder_destroy(&log_decoder); | ||
|
|
||
| /* | ||
| * If the scratch buffer could not be grown mid-chunk, do not return a | ||
| * partial payload: fail so the chunk can be retried as a whole. | ||
| */ | ||
| if (alloc_error == 1) { | ||
| flb_free(val_buf); | ||
| return NULL; | ||
| } | ||
|
|
||
| /* If nothing was read, destroy buffer */ | ||
| if (val_offset == 0) { | ||
| flb_free(val_buf); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: fluent/fluent-bit
Length of output: 41336
🤖 get_repo_knowledge executed:
get_repo_knowledge fluent/fluent-bit /tmp/coderabbit-repo-knowledge/fluent-fluent-bit-7a9ccb2b/learnings /tmp/coderabbit-repo-knowledge/fluent-fluent-bit-7a9ccb2b/conventionsLength of output: 11467
🏁 Script executed:
Repository: fluent/fluent-bit
Length of output: 39983
🏁 Script executed:
Repository: fluent/fluent-bit
Length of output: 14650
Reject unrepresentable scratch-buffer sizes before allocation. Both extractors compute
bytes + bytes / 4and doublemsgpack_sizewithout checked arithmetic. If either calculation wraps, the buffer can be smaller than the data that the extractor copies or serializes.The S3 extractor receives
event_chunk->size, notupload_chunk_size, so the configured upload limits do not bound this calculation. S3 also accumulatesval_offsetacross records. A wrapped growth size can make the nextmsgpack_size - val_offsetcalculation wrap and pass an invalid destination pointer and length toflb_msgpack_to_json.Reject the initial capacity when
bytes + bytes / 4is not representable. Before eachflb_realloc, reject growth whenmsgpack_size > SIZE_MAX / 2. Apply both checks inplugins/out_chronicle/chronicle.candplugins/out_s3/s3.c.📍 Affects 2 files
plugins/out_chronicle/chronicle.c#L623-L624(this comment)plugins/out_s3/s3.c#L4592-L4593🤖 Prompt for AI Agents
Source: Learnings