Conversation
A scanner flagged the count * sizeof(struct json_value) and count * sizeof(struct json_kv) sites in lib/json.c as a heap overflow risk: if count were attacker-controlled and approached SZ_MAX / sizeof(...), the multiplication would wrap, the arena allocation would be undersized, and the following memcpy would overflow the heap. The premise does not actually hold in this code path. Both call sites sit after a loop that bounds count at JSON_CONTAINER_MAX=64 before each increment, so the multiplication is at most 64*sizeof(struct json_value) inside sz. Adding a runtime "count > SZ_MAX / sizeof(...)" guard would produce dead code on every supported platform. Instead, anchor the invariant at the bound itself with 2 static_assert lines next to the #define JSON_CONTAINER_MAX. Any future bump that would break the no-wrap property fails at build time, not at runtime, and the surrounding parser stays free of unreachable branches. Reported-by: orbisai0security <mediratta01.pally@gmail.com>
There was a problem hiding this comment.
No issues found across 1 file
Tip: cubic could auto-approve low-risk PRs like this, if it thinks it's safe to merge. Learn more
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A scanner flagged the count * sizeof(struct json_value) and count * sizeof(struct json_kv) sites in lib/json.c as a heap overflow risk: if count were attacker-controlled and approached SZ_MAX / sizeof(...), the multiplication would wrap, the arena allocation would be undersized, and the following memcpy would overflow the heap.
The premise does not actually hold in this code path. Both call sites sit after a loop that bounds count at JSON_CONTAINER_MAX=64 before each increment, so the multiplication is at most 64*sizeof(struct json_value) inside sz. Adding a runtime "count > SZ_MAX / sizeof(...)" guard would produce dead code on every supported platform.
Instead, anchor the invariant at the bound itself with 2 static_assert lines next to the #define JSON_CONTAINER_MAX. Any future bump that would break the no-wrap property fails at build time, not at runtime, and the surrounding parser stays free of unreachable branches.
Summary by cubic
Add compile-time guards in
lib/json.cto ensure JSON container counts cannot overflow allocation math. Twostatic_asserts next toJSON_CONTAINER_MAXguaranteecount * sizeof(...)fits insz, avoiding dead runtime checks and making unsafe bumps fail at build time.Written for commit 9a2366d. Summary will update on new commits.