Skip to content

C unpack: optional compile-time array/map element limits - #1220

Open
aravindor wants to merge 1 commit into
msgpack:c_masterfrom
aravindor:harden/unpack-array-map-limit
Open

aravindor wants to merge 1 commit into
msgpack:c_masterfrom
aravindor:harden/unpack-array-map-limit

Conversation

@aravindor

@aravindor aravindor commented Nov 16, 2026 •

Copy link
Copy Markdown

Follow-up to #1013 for the C library.

Problem. In template_callback_array / template_callback_map
(src/unpack.c), the element count n comes straight from the wire
(array32/map32 carry a full 32-bit count) and is used to size the
zone allocation n * sizeof(...) before a single element is parsed. A
5-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:

#ifndef MSGPACK_UNPACK_ARRAY_LIMIT
#define MSGPACK_UNPACK_ARRAY_LIMIT 0
#endif
#ifndef MSGPACK_UNPACK_MAP_LIMIT
#define MSGPACK_UNPACK_MAP_LIMIT 0
#endif

and, right after the existing 32-bit overflow guard in each callback:

#if MSGPACK_UNPACK_ARRAY_LIMIT
    if (n > MSGPACK_UNPACK_ARRAY_LIMIT)
        return MSGPACK_UNPACK_PARSE_ERROR;
#endif

With the default 0 the #if is 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 with
MSGPACK_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/ext are zero-copy
references 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=100000
in CFLAGS.

Testing. Built the library both ways (Debug). With limits unset, a
benign map unpacks to SUCCESS and a map32 count=2000 header returns
CONTINUE as before. With -DMSGPACK_UNPACK_ARRAY_LIMIT=1000 -DMSGPACK_UNPACK_MAP_LIMIT=1000, the benign map still unpacks to
SUCCESS, map32 count=2000 returns PARSE_ERROR, and the #1013 input
df ff 00 00 00 returns PARSE_ERROR with no large allocation.

@codecov-commenter

codecov-commenter commented Nov 16, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 63.73%. Comparing base (eabfe29) to head (8a2b51f).
⚠️ Report is 31 commits behind head on c_master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aravindor

Copy link
Copy Markdown
Author

@redboltz, a gentle ping on this one when you have time. It still merges cleanly and CI is green. Both limits default to 0, so nothing changes unless a build defines them. If you would rather handle #1013 another way, I'm happy to rework it.

@redboltz

Copy link
Copy Markdown
Contributor

Thanks for the PR, and sorry for the delay. The approach follows what I
suggested in #1013 (disabled by default, opt-in via preprocessor macros,
existing behavior preserved), and I have verified it locally with and
without the limits on top of the current c_master. MSGPACK_UNPACK_PARSE_ERROR
is the right code; it matches the parse-type exceptions the C++ binding
throws for the same condition.

Please make the following changes in this PR, squash them into the single
commit, and force-push the branch:

  1. The commit message says the two sites are "already marked
    /* FIXME security guard */", but no such comment exists in
    src/unpack.c. Please drop that sentence.
  2. Please add one linux pattern to .github/workflows/gha.yml that builds
    and runs the test suite with the limits enabled, e.g. by adding
    -DMSGPACK_UNPACK_ARRAY_LIMIT=100000 -DMSGPACK_UNPACK_MAP_LIMIT=100000
    to CFLAGS for that pattern. You offered this in the description, and
    it also lets the PR run through the Windows CI, which now builds and
    runs the tests (Build and run the C tests on Windows CI #1232).

Also, the PR description still contains your drafting notes (the
"msgpack-c PR draft" heading, the commit message copy, and the
"Before you post" checklist). Please edit the description down to just
the PR description part, and remove the same FIXME sentence there.

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.
@aravindor
aravindor force-pushed the harden/unpack-array-map-limit branch from 695a371 to 8a2b51f Compare November 27, 2026 17:22
@aravindor

Copy link
Copy Markdown
Author

Thanks for checking it locally. I made the changes and force-pushed:

  1. Dropped the FIXME sentence from the commit message. (The markers I had in mind are at the MSGPACK_CS_ARRAY_32 / MSGPACK_CS_MAP_32 cases in include/msgpack/unpack_template.h, not in src/unpack.c, so the sentence pointed at the wrong file.)
  2. Added linux pattern 9 to .github/workflows/gha.yml: pattern 4 (gcc, 64-bit, shared, ASan) plus -DMSGPACK_UNPACK_ARRAY_LIMIT=100000 -DMSGPACK_UNPACK_MAP_LIMIT=100000 in CFLAGS. Locally the same configuration passes all 8 tests.
  3. Squashed into a single commit on the current c_master.

I also cut the description down to the PR text and removed the FIXME sentence there.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants