Repository navigation
Conversation
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## c_master #1220 +/- ##
============================================
+ Coverage 57.47% 63.73% +6.26%
============================================
Files 10 10
Lines 1124 1150 +26
Branches 129 142 +13
============================================
+ Hits 646 733 +87
+ Misses 478 417 -61 🚀 New features to boost your workflow:
|
|
Thanks for the PR, and sorry for the delay. The approach follows what I Please make the following changes in this PR, squash them into the single
Also, the PR description still contains your drafting notes (the Once CI is green on the updated branch, I will merge. |
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). 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. A new linux CI pattern (9: pattern 4 plus both limits set to 100000) builds and runs the test suite with the checks compiled in.
695a371 to
8a2b51f
Compare
|
Thanks for checking it locally. I made the changes and force-pushed:
I also cut the description down to the PR text and removed the FIXME sentence there. |
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.
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.
CI. New linux pattern 9 (gcc, 64-bit, shared, ASan and UBSan, like
pattern 4) builds and runs the test suite with
-DMSGPACK_UNPACK_ARRAY_LIMIT=100000 -DMSGPACK_UNPACK_MAP_LIMIT=100000in
CFLAGS.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.