Repository navigation
Description
Summary
msgpack::unpack() with the default unpack_limit accepts unlimited nesting depth (depth = 0xffffffff). Unpacking itself is iterative and works, but converting the result to msgpack::type::variant / msgpack::type::variant_ref (the Boost-based "dynamic" variant adaptor) recurses once per nesting level. A ~20 KB input made of nested one-element arrays crashes the process with a stack overflow (SIGSEGV). Both as<variant>() and convert(variant&) are affected.
Other consumers of the same object (operator<<, msgpack::pack, msgpack::clone, operator==) handle the same input fine, because they use the iterative object_parser.
Affected code
msgpack-cxx cpp_master @ 0105f53 (2026-11-20, after cpp-9.0.0). Older releases with the same adaptor are likely affected too, but I have not tested them.
include/msgpack/v1/adaptor/boost/msgpack_variant.hpp:285-313:as<type::basic_variant<STR, BIN, EXT>>. Fortype::ARRAYit callso.as<std::vector<type::basic_variant<...>>>()(line 305), and fortype::MAPit callso.as<std::multimap<...>>().include/msgpack/v1/adaptor/boost/msgpack_variant.hpp:320-:convert<type::basic_variant<...>>follows the same pattern.include/msgpack/v1/adaptor/vector.hpp:31-43:as<std::vector<T>>callsp->as<T>()for each element, which recurses back into the variant adaptor.include/msgpack/v1/unpack_decl.hpp:90-95: the defaultunpack_limithasdepth = 0xffffffff, so nothing bounds the depth before the conversion runs.
Reproduction
Input: 20,000 bytes of 0x91 (fixarray with 1 element) followed by 0xc0 (nil). The file is 20,001 bytes long. To generate it:
python3 -c "import sys; sys.stdout.buffer.write(b'\x91'*20000+b'\xc0')" > deep.mpkProgram:
#include <msgpack.hpp>
#include <msgpack/adaptor/boost/msgpack_variant.hpp>
#include <string>
int main() {
std::string buf(20000, '\x91');
buf.push_back('\xc0');
msgpack::object_handle oh = msgpack::unpack(buf.data(), buf.size()); // default limits: OK
auto v = oh.get().as<msgpack::type::variant>(); // crashes here
(void)v;
}g++ -std=c++17 -O2 -I msgpack-c/include poc.cpp -o poc && ./poc # Segmentation faultTested on Ubuntu 24.04, GCC 13.3, Boost 1.83, and the default 8 MB main-thread stack:
| Build | Result |
|---|---|
-O0 -fsanitize=address and -O1 -fsanitize=address |
AddressSanitizer: stack-overflow (frames alternate between as<basic_variant> at msgpack_variant.hpp:306 and as<std::vector<basic_variant>> at vector.hpp:35/40) |
-O0 and -O2, no sanitizer, -std=c++11 and -std=c++17 |
SIGSEGV (exit 139) |
variant_ref instead of variant |
same crash |
oh.get().convert(v) instead of as<> |
same crash |
The approximate minimum depth that crashes on an 8 MB stack is about 9,900 levels at -O0 and about 18,000 levels at -O2. Threads with smaller stacks crash at a lower depth. Close to the crash threshold the conversion also becomes slow, because each level copies the nested vectors (about 2.3 s at depth 10,000 with -O2).
Setting an explicit depth limit avoids the crash:
msgpack::unpack(buf.data(), buf.size(), nullptr, nullptr, msgpack::unpack_limit(0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 512)) throws msgpack::depth_size_overflow, as expected.
Impact
Denial of service. An application that unpacks untrusted MessagePack with default limits and converts the result to msgpack::type::variant / variant_ref (a natural choice for schema-less data) can be crashed by a single small message. I see no memory disclosure or corruption beyond the stack exhaustion.
Preconditions
- The application uses the Boost variant adaptor (
msgpack/adaptor/boost/msgpack_variant.hpp). - The input is attacker-controlled.
- No
depthis set inunpack_limit(the default).
Applications that convert into fixed, statically typed structures are not affected, because the depth is bounded by the type.
Suggested fix (any of these)
- Bound the recursion in the variant adaptor, for example by passing a depth counter through the
basic_variantconversion and throwingmsgpack::depth_size_overflowpast a limit. Alternatively, build the variant iteratively with an explicit stack, asobject_parseralready does. - Use a finite default for
unpack_limit::depth(for example a few hundred or a few thousand levels), similar to other parsers. This also protects user code that recurses overmsgpack::object. - At minimum, document that
variant/variant_refconversion is recursive and that untrusted input needs an explicitunpack_limitdepth.
I'm happy to test a patch. I have not opened a PR, because the right default depth is a project decision.
Activity