Repository navigation
Description
Related to #1224 — same x3 visitor pointer-contract surface
(include/msgpack/v2/create_object_visitor.hpp), but a distinct bug: a use-after-free
(CWE-416) in reference mode, rather than the out-of-bounds read (CWE-125) of #1224. Different mechanism, different trigger; filing separately so it is tracked and fixed on its own.
Summary
When built with MSGPACK_USE_X3_PARSE and unpacked in reference mode (passing a reference function that returns true, i.e. zero-copy), msgpack::unpack(begin, end, f, ...) over a buffering iterator — boost::spirit::multi_pass, which uses the default split_std_deque storage — returns an object_handle whose str/bin/ext pointers are already dangling.
The object's pointers reference multi_pass's internal buffer, not the caller's input. That
buffer is a std::vector<char> that reallocates as more bytes are read (push_back growth); each reallocation frees the previous block. By the time parsing finishes, pointers captured for earlier values point into freed memory. The use-after-free happens while both the iterators and the original byte source are still alive — the application does nothing wrong and cannot prevent it by keeping anything alive.
Why this is distinct from #1224
| #1224 | this issue | |
|---|---|---|
| Weakness | CWE-125 out-of-bounds read | CWE-416 use-after-free |
| Mechanism | reads size bytes past &range.front() (contiguity assumption) |
pointer valid at capture, its storage is freed/reallocated later |
| Trigger | non-contiguous container, copy mode | reference mode + a buffering/reallocating iterator |
Both stem from the x3 str/bin/ext actions handing the visitor a raw (ptr, size) and
create_object_visitor storing/reading it, but the failure modes and fixes differ.
Steps to reproduce
Decode an array of several str values in reference mode from a multi_pass over a stream, then
read an early element after parsing completes — all iterators and the source kept alive:
#include <msgpack.hpp>
#include <boost/spirit/include/support_multi_pass.hpp>
#include <sstream>
#include <string>
#include <cstdio>
static bool ref_true(msgpack::type::object_type, std::size_t, void*) { return true; }
int main() {
// pack an array of 8 distinct 600-byte strings
std::stringstream ss; msgpack::packer<std::stringstream> pk(ss);
const int N = 8;
pk.pack_array(N);
for (int i = 0; i < N; i++) { std::string e(600, char('A' + i)); pk.pack_str(e.size()); pk.pack_str_body(e.data(), e.size()); }
std::string src = ss.str();
std::istringstream iss(src); iss.unsetf(std::ios::skipws);
typedef std::istreambuf_iterator<char> base_it;
auto first = boost::spirit::make_default_multi_pass(base_it(iss));
auto last = boost::spirit::make_default_multi_pass(base_it());
// reference mode (zero-copy); iterators + src kept alive for the whole function
auto oh = msgpack::unpack(first, last, ref_true, nullptr, msgpack::unpack_limit());
auto arr = oh.get();
// read element 0 last: its buffer was reallocated away while parsing later elements
auto& e0 = arr.via.array.ptr[0];
volatile char c = e0.via.str.ptr[0]; // <-- heap-use-after-free here
(void)c;
printf("size=%u\n", e0.via.str.size);
}clang++ -std=c++17 -fsanitize=address -DMSGPACK_USE_X3_PARSE -DMSGPACK_USE_BOOST \
-I <msgpack include> -isystem <boost> poc.cpp && ./a.out
Actual result
AddressSanitizer: heap-use-after-free READ of size 1 ...
freed by thread T0 here:
... std::vector<char>::~vector
... std::deque<std::vector<char>>::_M_destroy_data_aux (operator delete)
previously allocated by thread T0 here:
... std::vector<char>::_M_range_initialize< boost::spirit::multi_pass< ... split_std_deque > >
The data lives in multi_pass's split_std_deque storage (a std::deque<std::vector> of buffered blocks). As iteration advances, that storage frees the block the reference-mode pointer points into — surfacing either as a block being destroyed (~vector via deque::_M_destroy_data_aux, as in the full trace below) or as a block vector reallocating on growth (vector::_M_realloc_insert). Either way the referenced region is freed mid-parse and fully poisoned (fd); the decoded object holds dangling pointers the moment unpack() returns.
Expected result
A successful unpack() should not return an object containing dangling pointers. Either decode correctly (copy into the zone), or reject/static_assert the unsafe iterator category.
Impact
Use-after-free (CWE-416): the decoded str/bin/ext values alias freed heap, so reading them yields whatever has since been allocated there (silent data corruption / information disclosure) or crashes. Caller-unpreventable — keeping the source and iterators alive does not help, because the data lives in multi_pass's transient buffer, which the library reallocates internally.
Gating / scope (kept explicit)
Requires all of: MSGPACK_USE_X3_PARSE (non-default CMake option); reference mode (an explicit reference function returning true); and a buffering/reallocating iterator (multi_pass, or any iterator whose dereferenced storage can move). Contiguous, stable sources (std::string, std::vector, raw const char*) in reference mode point into the live input and are safe as documented. The shipped example/x3/stream_unpack.cpp uses copy mode
(msgpack::unpack(b, e) with no reference function) and is not affected.
Suggested fix
x3 reference mode is unsound for any iterator whose referenced storage is not stable for the lifetime of the decoded object. Options:
- In reference mode, only hand the visitor a zero-copy pointer when the iterator is
contiguous/random-access over stable storage; otherwise fall back to copying into the zone (as copy mode already does). - Or
static_assert/ document that reference mode requires a contiguous, stable iterator, somulti_passfails to compile rather than producing dangling pointers at runtime
Activity
AmirWilayah commentedon Nov 17, 2026
ASan stack trace