Skip to content

x3 parser: use-after-free in reference mode (zero-copy) when unpacking from a buffering iterator (boost::spirit::multi_pass) #1225

Open

Description

@AmirWilayah

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, so multi_pass fails to compile rather than producing dangling pointers at runtime

Activity

  1. AmirWilayah commented on Nov 17, 2026

    @AmirWilayah
    Author

    ASan stack trace

    ==56505==ERROR: AddressSanitizer: heap-use-after-free on address 0x516000001b80 at pc 0x5cfd0c4083f4 bp 0x7ffc29c79e30 sp 0x7ffc29c79e28
    READ of size 1 at 0x516000001b80 thread T0
        #0 0x5cfd0c4083f3 in main /tmp/x3ref_poc.cpp:20:23
        #1 0x77f20022a1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: 8cebda42d140fc2b09639f8b13a5d5f2e617ef09)
        #2 0x77f20022a28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: 8cebda42d140fc2b09639f8b13a5d5f2e617ef09)
        #3 0x5cfd0c32c144 in _start (/tmp/x3ref_poc+0x30144) (BuildId: 1c3bb2a671c9d3f1ecb8cf25f69f70a6b44ca7c8)
    
    0x516000001b80 is located 0 bytes inside of 600-byte region [0x516000001b80,0x516000001dd8)
    freed by thread T0 here:
        #0 0x5cfd0c405e31 in operator delete(void*) (/tmp/x3ref_poc+0x109e31) (BuildId: 1c3bb2a671c9d3f1ecb8cf25f69f70a6b44ca7c8)
        #1 0x5cfd0c433ae4 in std::__new_allocator<char>::deallocate(char*, unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/new_allocator.h:172:2
        #2 0x5cfd0c433ae4 in std::allocator_traits<std::allocator<char>>::deallocate(std::allocator<char>&, char*, unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/alloc_traits.h:517:13
        #3 0x5cfd0c433ae4 in std::_Vector_base<char, std::allocator<char>>::_M_deallocate(char*, unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_vector.h:390:4
        #4 0x5cfd0c433ae4 in std::_Vector_base<char, std::allocator<char>>::~_Vector_base() /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_vector.h:369:2
        #5 0x5cfd0c433ae4 in std::vector<char, std::allocator<char>>::~vector() /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_vector.h:738:7
        #6 0x5cfd0c433ae4 in void std::_Destroy<std::vector<char, std::allocator<char>>>(std::vector<char, std::allocator<char>>*) /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_construct.h:151:19
        #7 0x5cfd0c433ae4 in void std::_Destroy_aux<false>::__destroy<std::vector<char, std::allocator<char>>*>(std::vector<char, std::allocator<char>>*, std::vector<char, std::allocator<char>>*) /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_construct.h:163:6
        #8 0x5cfd0c433ae4 in void std::_Destroy<std::vector<char, std::allocator<char>>*>(std::vector<char, std::allocator<char>>*, std::vector<char, std::allocator<char>>*) /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_construct.h:195:7
        #9 0x5cfd0c433ae4 in void std::_Destroy<std::vector<char, std::allocator<char>>*, std::vector<char, std::allocator<char>>>(std::vector<char, std::allocator<char>>*, std::vector<char, std::allocator<char>>*, std::allocator<std::vector<char, std::allocator<char>>>&) /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/alloc_traits.h:948:7
        #10 0x5cfd0c433ae4 in std::deque<std::vector<char, std::allocator<char>>, std::allocator<std::vector<char, std::allocator<char>>>>::_M_destroy_data_aux(std::_Deque_iterator<std::vector<char, std::allocator<char>>, std::vector<char, std::allocator<char>>&, std::vector<char, std::allocator<char>>*>, std::_Deque_iterator<std::vector<char, std::allocator<char>>, std::vector<char, std::allocator<char>>&, std::vector<char, std::allocator<char>>*>) /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/deque.tcc:875:3
    
    previously allocated by thread T0 here:
        #0 0x5cfd0c4055b1 in operator new(unsigned long) (/tmp/x3ref_poc+0x1095b1) (BuildId: 1c3bb2a671c9d3f1ecb8cf25f69f70a6b44ca7c8)
        #1 0x5cfd0c42db4b in std::__new_allocator<char>::allocate(unsigned long, void const*) /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/new_allocator.h:151:27
        #2 0x5cfd0c42db4b in std::allocator_traits<std::allocator<char>>::allocate(std::allocator<char>&, unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/alloc_traits.h:482:20
        #3 0x5cfd0c42db4b in std::_Vector_base<char, std::allocator<char>>::_M_allocate(unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_vector.h:381:20
        #4 0x5cfd0c42db4b in void std::vector<char, std::allocator<char>>::_M_range_initialize<boost::spirit::multi_pass<std::istreambuf_iterator<char, std::char_traits<char>>, boost::spirit::iterator_policies::default_policy<boost::spirit::iterator_policies::ref_counted, boost::spirit::iterator_policies::no_check, boost::spirit::iterator_policies::buffering_input_iterator, boost::spirit::iterator_policies::split_std_deque>>>(boost::spirit::multi_pass<std::istreambuf_iterator<char, std::char_traits<char>>, boost::spirit::iterator_policies::default_policy<boost::spirit::iterator_policies::ref_counted, boost::spirit::iterator_policies::no_check, boost::spirit::iterator_policies::buffering_input_iterator, boost::spirit::iterator_policies::split_std_deque>>, boost::spirit::multi_pass<std::istreambuf_iterator<char, std::char_traits<char>>, boost::spirit::iterator_policies::default_policy<boost::spirit::iterator_policies::ref_counted, boost::spirit::iterator_policies::no_check, boost::spirit::iterator_policies::buffering_input_iterator, boost::spirit::iterator_policies::split_std_deque>>, std::forward_iterator_tag) /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_vector.h:1692:14
        #5 0x5cfd0c42d600 in std::vector<char, std::allocator<char>>::vector<boost::spirit::multi_pass<std::istreambuf_iterator<char, std::char_traits<char>>, boost::spirit::iterator_policies::default_policy<boost::spirit::iterator_policies::ref_counted, boost::spirit::iterator_policies::no_check, boost::spirit::iterator_policies::buffering_input_iterator, boost::spirit::iterator_policies::split_std_deque>>, void>(boost::spirit::multi_pass<std::istreambuf_iterator<char, std::char_traits<char>>, boost::spirit::iterator_policies::default_policy<boost::spirit::iterator_policies::ref_counted, boost::spirit::iterator_policies::no_check, boost::spirit::iterator_policies::buffering_input_iterator, boost::spirit::iterator_policies::split_std_deque>>, boost::spirit::multi_pass<std::istreambuf_iterator<char, std::char_traits<char>>, boost::spirit::iterator_policies::default_policy<boost::spirit::iterator_policies::ref_counted, boost::spirit::iterator_policies::no_check, boost::spirit::iterator_policies::buffering_input_iterator, boost::spirit::iterator_policies::split_std_deque>>, std::allocator<char> const&) /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_vector.h:711:4
    
    SUMMARY: AddressSanitizer: heap-use-after-free /tmp/x3ref_poc.cpp:20:23 in main
    Shadow bytes around the buggy address:
      0x516000001900: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
      0x516000001980: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
      0x516000001a00: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
      0x516000001a80: fd fd fd fd fd fd fd fd fd fd fd fd fa fa fa fa
      0x516000001b00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    =>0x516000001b80:[fd]fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
      0x516000001c00: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
      0x516000001c80: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
      0x516000001d00: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
      0x516000001d80: fd fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa
      0x516000001e00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    Shadow byte legend (one shadow byte represents 8 application bytes):
      Addressable:           00
      Partially addressable: 01 02 03 04 05 06 07
      Heap left redzone:       fa
      Freed heap region:       fd
      Stack left redzone:      f1
      Stack mid redzone:       f2
      Stack right redzone:     f3
      Stack after return:      f5
      Stack use after scope:   f8
      Global redzone:          f9
      Global init order:       f6
      Poisoned by user:        f7
      Container overflow:      fc
      Array cookie:            ac
      Intra object redzone:    bb
      ASan internal:           fe
      Left alloca redzone:     ca
      Right alloca redzone:    cb
    ==56505==ABORTING
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