Repository navigation
Guard C++ zone constructor chunk allocation against wraparound - #1207
Merged
Merged
Conversation
The zone chunk_list constructor now throws std::bad_alloc when sizeof(chunk) + chunk_size would wrap (cpp11_zone.hpp, cpp03_zone.hpp and its erb template), as msgpack#1192 did for allocate_align(). Add a test.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## cpp_master #1207 +/- ##
==============================================
- Coverage 85.36% 79.85% -5.52%
==============================================
Files 80 80
Lines 5099 5799 +700
Branches 507 637 +130
==============================================
+ Hits 4353 4631 +278
- Misses 746 1168 +422 🚀 New features to boost your workflow:
|
Contributor
|
@jimaf thank you for sending the PR. It looks good to me. |
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.
The zone chunk_list constructor, reached from msgpack::zone(size_t chunk_size), computes ::malloc(sizeof(chunk) + chunk_size) without a wraparound check. This adds the check and throws std::bad_alloc, the same as the existing malloc failure path, in cpp11_zone.hpp, cpp03_zone.hpp and erb/v1/cpp03_zone.hpp.erb. It uses the std::numeric_limits<size_t>::max() - x form that #1192 used for allocate_align(). cpp03_zone.hpp regenerated from the erb with ./preprocess is identical to the edited file.
Testing
Reproduction
Before this change, an ASan harness that constructs msgpack::zone with a chunk size in [SIZE_MAX-7, SIZE_MAX] (64-bit), built both as C++11 and as C++03, reports a heap-buffer-overflow WRITE of size 8 into the undersized chunk. After the change the constructor throws std::bad_alloc and there is no sanitizer output. I did not demonstrate a path where untrusted input reaches this in a stock build, so this is a defensive guard.
This change was prepared with help from Anthropic's Claude (AI assistance). I reviewed the change; the tests were run in Docker as described above.