Summary
MessagePack for C# contains a flaw in its timestamp extension parsing where the reader allocates stack memory directly from an attacker-controlled extension length.
A crafted MessagePack payload can trigger a StackOverflowException that cannot be caught, abruptly terminating the host process whenever MessagePackReader.ReadDateTime() is invoked on malicious input.
Affected context
This vulnerability appears when applications use MessagePack for C# to parse untrusted or semi-trusted payloads and call MessagePackReader.ReadDateTime() on data that may include extension values.
In the "slow path" for timestamp extension parsing, the implementation uses the extension body length from the incoming data to compute a token size and immediately performs a stackalloc based on that value.
If the environment allows arbitrary or poorly validated MessagePack blobs to reach this parsing routine, a small malicious payload can trigger disproportionately large stack allocation before validation occurs.
Exploit preconditions
- Attack surface: Any API or protocol that deserializes MessagePack data using MessagePack for C# and reaches
ReadDateTime()on attacker-supplied input. - Required access: Ability to send or supply crafted MessagePack payloads to the target service (e.g., network client, message bus publisher, file uploader).
- Trigger point: Parsing a timestamp extension where the payload claims an excessively large extension body length, invoking the slow-path timestamp parsing logic.
- Why exploitation is possible: The implementation uses the extension body length from the wire to size a stack allocation (
stackalloc) before verifying it against the expected timestamp sizes, allowing an attacker-controlled size to overrun stack limits and cause an uncatchableStackOverflowException.
Why this happens
At its core, the bug stems from trusting size metadata embedded in untrusted input and using it directly in stack-based memory allocation, without prior bounds checking.
This maps to CWE-674: Uncontrolled Resource Consumption and CWE-789: Uncontrolled Memory Allocation, where input-driven sizes can exhaust process resources.
Additional contributing aspects include CWE-190: Integer Overflow or Wraparound risk when computing token sizes, and CWE-125: Out-of-bounds Read potential if validation is bypassed.
The insecure pattern is: reading a length field from the MessagePack timestamp extension, computing tokenSize using that value, and calling stackalloc tokenSize before verifying that the claimed length matches one of the tightly constrained valid timestamp formats.
Potential impact
- An attacker can cause immediate termination of the host process via an uncatchable
StackOverflowException. - Critical services using MessagePack for C# may experience repeated crashes, resulting in denial of service.
- Systems relying on high availability (APIs, microservices, background workers) can be forced into failure or continuous restart loops.
- Chained failures in dependent services may propagate, leading to broader application or system instability.
General mitigation guidance
Effective mitigation focuses on reducing trust in input-driven size fields and avoiding stack-based allocations that can grow without strict bounds.
Pattern-based defensive measures include:
- Validating extension lengths and timestamp sizes before any allocation, ensuring the value falls within a small, fixed set of accepted sizes.
- Preferring fixed-size or capped allocations on the stack, and moving variable-length parsing to heap-backed buffers with explicit upper limits.
- Introducing global limits on MessagePack payload size and per-field length constraints in deserialization routines.
- Designing parsing code with defensive checks around length arithmetic to prevent overflow or exaggerated sizes.
- Running services with health monitoring and graceful restart mechanisms, limiting the blast radius of unexpected process termination.
How teams detect issues like this
Detection typically involves a combination of code-level inspection and runtime observability:
- Static analysis rules that flag
stackallocor other stack-based allocations driven directly by input-derived lengths or extension values. - Targeted code review of MessagePack parsing routines, focusing on any path where extension lengths, timestamps, or variable-length fields are used in memory allocation before validation.
- Runtime monitoring and logging for recurring
StackOverflowExceptionevents, crashes, or abnormal termination patterns when deserializing MessagePack data. - Policy-based checks in CI/CD that require explicit bounds checking around all computed sizes in serialization/deserialization libraries.
- Optional use of automated analyzers that recognize common resource exhaustion patterns and unsafe size handling in C# code.

