Skip to content

Stack overflow (uncontrolled recursion) when converting deeply nested input to msgpack::type::variant with default unpack_limit #1234

Open

Description

@GitEffort

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>>. For type::ARRAY it calls o.as<std::vector<type::basic_variant<...>>>() (line 305), and for type::MAP it calls o.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>> calls p->as<T>() for each element, which recurses back into the variant adaptor.
  • include/msgpack/v1/unpack_decl.hpp:90-95: the default unpack_limit has depth = 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.mpk

Program:

#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 fault

Tested 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 depth is set in unpack_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)

  1. Bound the recursion in the variant adaptor, for example by passing a depth counter through the basic_variant conversion and throwing msgpack::depth_size_overflow past a limit. Alternatively, build the variant iteratively with an explicit stack, as object_parser already does.
  2. 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 over msgpack::object.
  3. At minimum, document that variant / variant_ref conversion is recursive and that untrusted input needs an explicit unpack_limit depth.

I'm happy to test a patch. I have not opened a PR, because the right default depth is a project decision.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions