pack: harden flb_msgpack_to_json buffer handling#12133
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthrough
ChangesJSON truncation handling
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/flb_pack.c`:
- Around line 1169-1175: Guard zero-capacity destinations before the json_size -
1 calculation in the msgpack2json packing path, returning -1 for non-NULL
buffers with json_size == 0 while preserving existing behavior for valid
capacities and NULL destinations. Add a regression case alongside
test_json_pack_truncation() in pack tests to verify zero-size output does not
write or wrap the capacity.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 6eacf2f5-e7d4-4adc-b923-9c0fc040a1ba
📒 Files selected for processing (2)
src/flb_pack.ctests/internal/pack.c
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e95d167e31
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| * distinguish truncation from a successful zero-length write, matching the | ||
| * documented contract above. | ||
| */ | ||
| return ret ? off : -1; |
There was a problem hiding this comment.
Handle JSON overflow at log_key call sites
Returning -1 here sends buffer overflows down the fatal path, but the two production callers that only test < 0 do not recover from it: with log_key enabled, plugins/out_s3/s3.c:3689 and plugins/out_chronicle/chronicle.c:598 just break out of the map loop. If the selected non-string value's JSON encoding exceeds their fixed bytes + bytes / 4 scratch buffer (for example arrays of small integers or nested values that expand when escaped), S3 returns NULL/FLB_ERROR when it is the first value or silently omits that record after earlier values, and Chronicle skips the record. Please resize the scratch buffer or propagate a retryable extraction error at those call sites before changing this API result.
Useful? React with 👍 / 👎.
e95d167 to
ee66240
Compare
|
Agreed on both points.
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/internal/pack.c`:
- Around line 945-952: Check and assert the return value of msgpack_unpack
before calling flb_msgpack_to_json in this test setup. Ensure the converter
receives obj only after successful unpacking, while preserving the existing
buffer-size assertion and cleanup flow.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: d142575b-3237-4e10-9ae5-dbe95bbec3dd
📒 Files selected for processing (2)
src/flb_pack.ctests/internal/pack.c
🚧 Files skipped from review as they are similar to previous changes (1)
- src/flb_pack.c
|
The caller-recovery fix is now up as #12137 ( |
Two buffer-boundary issues in flb_msgpack_to_json(): - On a buffer overflow the function returned 0 (the FLB_FALSE from msgpack2json()), indistinguishable from a successful zero-length write. Return -1 instead, matching the documented contract, so fixed-buffer callers can detect truncation. Callers already testing ret <= 0 are unaffected. - A zero-capacity (non-NULL) buffer made 'json_size - 1' wrap to SIZE_MAX, letting msgpack2json() write past the buffer. Reject json_size == 0 up front. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: v.sinev <vit.phantom@gmail.com>
Verify that a value whose JSON form does not fit returns a negative value (not 0), and that a zero-capacity buffer is rejected rather than wrapping the size computation. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: v.sinev <vit.phantom@gmail.com>
ee66240 to
d025ebe
Compare
|
Addressed — |
|
The unit-test CI failure here is unrelated to this change. The failing test is Could a maintainer re-run the failed jobs? I don't have permission to re-run CI on the upstream repo. Thanks! |
Scope: this is a small, self-contained API contract / buffer-safety fix for
flb_msgpack_to_json(). It does not attempt to fix any caller's overflow recovery — see the note at the bottom.Two boundary issues in
flb_msgpack_to_json()1. Overflow reported as
0instead of a negative value.The function is documented to return "a number of characters on success, or a negative value", but on a buffer overflow it returns
0(theFLB_FALSEfrommsgpack2json()), which is indistinguishable from a successful zero-length write:Return
-1on overflow to match the contract. I audited every caller: the_str/_sdswrappers and the AWS/CloudWatch/Kinesis paths already testret <= 0, so they are unaffected; onlyout_s3/out_chronicletestret < 0(see note).2. Zero-capacity buffer wraps the size computation.
json_size - 1underflows toSIZE_MAXwhen a non-NULL buffer is passed withjson_size == 0, allowingmsgpack2json()to write out of bounds. Rejectjson_size == 0up front.Testing
tests/internal/pack.c::json_pack_truncationasserts overflow returns a negative value and that a zero-capacity buffer is rejected.flb-it-packpasses.Note on callers (out_s3 / out_chronicle)
These two
log_keyextraction paths testret < 0and simplybreak— they do not grow/retry, so an oversizedlog_keyvalue is still dropped (previously it was emitted empty). That is a pre-existing data-loss bug and the real fix belongs in those plugins (grow-and-retry, likeflb_msgpack_to_json_stralready does). It will be sent as a separateout_s3/out_chroniclePR; this PR is intentionally scoped to the core contract/safety only.Surfaced during review of #12129.
Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests