Repository navigation
Conversation
A map32/array32 header declares a 32-bit element count that is passed straight to the backing-store allocation (n * sizeof(msgpack_object[_kv])) before any element byte is read. A 5-byte input such as 0xdf 0xff 0x00 0x00 0x00 (map32, count 0xff000000) therefore requests ~205 GB up front, making a resource-exhaustion attack very cheap (issue msgpack#1013). The two sites are the ones already marked /* FIXME security guard */. Add two compile-time limits, MSGPACK_UNPACK_ARRAY_LIMIT and MSGPACK_UNPACK_MAP_LIMIT, checked against the declared count before the allocation. Both default to 0 (no limit): with the default the checks compile out entirely, so existing behavior is preserved byte for byte. Defining either to a non-zero value rejects an over-limit array/map with MSGPACK_UNPACK_PARSE_ERROR. Only arrays and maps are guarded: str/bin/ext point into the parse buffer (zero-copy), so their length is already bounded by the bytes present and is not an up-front amplification.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## c_master #1220 +/- ##
============================================
+ Coverage 57.47% 62.66% +5.18%
============================================
Files 10 10
Lines 1124 1149 +25
Branches 129 141 +12
============================================
+ Hits 646 720 +74
+ Misses 478 429 -49 🚀 New features to boost your workflow:
|
This branch has not been deployed
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.
msgpack-c PR draft (array/map unpack element limits)
Target:
msgpack/msgpack-c, branch offc_master. Addresses issue #1013.One commit, one file (
src/unpack.c), +32 / -0.Commit message
PR description
Follow-up to #1013 for the C library.
Problem. In
template_callback_array/template_callback_map(
src/unpack.c), the element countncomes straight from the wire(
array32/map32carry a full 32-bit count) and is used to size thezone allocation
n * sizeof(...)before a single element is parsed. A5-byte message can thus force a ~100 GB (array) / ~205 GB (map)
allocation. These are the two lines already flagged
/* FIXME security guard */.Approach. Per the guidance in #1013 (preserve current behavior,
selectable by a default-disabled preprocessor macro), this adds:
and, right after the existing 32-bit overflow guard in each callback:
With the default
0the#ifis false and the check is not compiled in,so behavior is unchanged. A build that opts in, e.g.
-DMSGPACK_UNPACK_MAP_LIMIT=1000000, rejects any larger array/map withMSGPACK_UNPACK_PARSE_ERROR. This mirrors the intent of the C++binding's element-count limit, which the C library currently lacks.
Only arrays and maps are limited;
str/bin/extare zero-copyreferences into the parse buffer, so their size is already bounded by the
input and does not allocate up front.
Testing. Built the library both ways (Debug). With limits unset, a
benign map unpacks to
SUCCESSand amap32 count=2000header returnsCONTINUEas before. With-DMSGPACK_UNPACK_ARRAY_LIMIT=1000 -DMSGPACK_UNPACK_MAP_LIMIT=1000, the benign map still unpacks toSUCCESS,map32 count=2000returnsPARSE_ERROR, and the #1013 inputdf ff 00 00 00returnsPARSE_ERRORwith no large allocation.Happy to add a CI job building the test suite with the limits set, or to
switch the rejection code to
MSGPACK_UNPACK_NOMEM_ERRORif that readsbetter alongside the existing overflow guard.
Before you post
c_master(notcpp_master).Signed-off-by:line (add-soncommit if so). msgpack-c has no CLA bot.