Skip to content

x3 parser: heap out-of-bounds read / information disclosure when unpacking str/bin/ext from a non-contiguous iterator #1224

Open

Description

@AmirWilayah

Summary

When built with MSGPACK_USE_X3_PARSE, msgpack::unpack(begin, end) reads out of bounds for str/bin/ext values if begin/end are iterators into a non-contiguous container(std::deque, std::list, …). The x3 grammar hands the visitor &range.front() plus the attacker-declared size, and create_object_visitor::visit_str/bin/ext
(include/msgpack/v2/create_object_visitor.hpp:124) memcpys that many bytes assuming the range is contiguous. The out-of-bounds bytes end up in the decoded object.

Contiguous sources (std::string, std::vector) are fine. boost::spirit::multi_pass (used by
example/x3/stream_unpack.cpp) is also fine, so the documented example is not affected — the
problem is specifically node/block iterators.

Steps to reproduce

Build with x3 + ASan and unpack a str larger than one std::deque block from a std::deque:

#include <msgpack.hpp>
#include <deque>
#include <string>
#include <sstream>
int main() {
    std::stringstream ss;
    msgpack::pack(ss, std::string(600, 'A'));   // a str longer than a deque block (512B)
    std::string s = ss.str();
    std::deque<char> d(s.begin(), s.end());
    auto oh = msgpack::unpack(d.begin(), d.end());   // OOB read here
    (void)oh;
}
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-buffer-overflow ... READ of size 600 ...
  in create_object_visitor::visit_str  (create_object_visitor.hpp:124)
0 bytes after a 512-byte region

A follow-up test shows the over-read bytes are adjacent heap, not the source: a decoded 5000-byte string differs from the input in ~3978 of its bytes (information disclosure into the decoded value).

Expected result

Either a correct decode, or a clean error — no read past the input.

Impact

Out-of-bounds heap read with attacker-controlled length (str/bin/ext size); the decoded object can contain adjacent heap memory (information disclosure). No out-of-bounds write and no reliable crash were observed. Requires MSGPACK_USE_X3_PARSE (non-default) and a non-contiguous input iterator.

Suggested fix

In the x3 str/bin/ext actions, don't assume &range.front() is contiguous for size
bytes: copy the matched range through the iterators into a contiguous buffer before calling the visitor, or static_assert that the iterator is contiguous/random-access so a node container fails to compile.

Activity

  1. AmirWilayah commented on Nov 17, 2026

    @AmirWilayah
    Author

    ASan Stack trace

    ==ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 600
        #0 ... in __asan_memcpy
        #1 ... in msgpack::v2::detail::create_object_visitor::visit_str(char const*, unsigned int)
             .../msgpack/v2/create_object_visitor.hpp:124
        [~35 Boost.Spirit X3 template frames omitted]
        #33 ... in main .../X3NC_x3_parse_noncontiguous_iterator_overread.cpp:14
    
    0x515000000280 is located 0 bytes after 512-byte region [0x515000000080,0x515000000280)
    allocated by thread T0 here:
        #0 ... in operator new(unsigned long)
        #3 ... in std::_Deque_base<char, std::allocator<char>>::_M_allocate_node()  stl_deque.h:583
        #4 ... in std::_Deque_base<char, std::allocator<char>>::_M_create_nodes(...)  stl_deque.h:684
    
    SUMMARY: AddressSanitizer: heap-buffer-overflow in __asan_memcpy
  2. AmirWilayah commented on Nov 17, 2026

    @AmirWilayah
    Author

    Suggest fix
    msgpack-cxx-X3NC.patch

    --- a/include/msgpack/v2/x3_parse.hpp   2026-11-16 20:07:44.000000000 +0800
    +++ b/include/msgpack/v2/x3_parse.hpp   2026-11-18 01:07:42.890907207 +0800
    @@ -21,6 +21,9 @@
     #include "msgpack/unpack_exception.hpp"
    
     #include <limits>
    +#include <deque>
    +#include <vector>
    +#include <algorithm>
    
     #if __GNUC__ >= 4
     #pragma GCC diagnostic push
    @@ -127,6 +130,17 @@
         app_specific(Vis&& vis):vis(vis) {}
         std::vector<index_size> index_sizes;
         Visitor vis;
    +
    +    // Fix (OOB read): the matched str/bin/ext range may be stored non-contiguously
    +    // (std::deque/std::list iterators, multi_pass, ...). Copy it into a contiguous,
    +    // pointer-stable buffer before handing (ptr, size) to the visitor, instead of
    +    // taking &range.front() and reading `size` contiguous bytes past one block.
    +    std::deque<std::vector<char> > m_contig;
    +    template <typename Range>
    +    const char* contiguous(Range const& r) {
    +        m_contig.emplace_back(r.begin(), r.end());
    +        return m_contig.back().data();
    +    }
     };
    
     template <typename Visitor>
    @@ -344,7 +358,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& str = _attr(ctx);
                     auto size = static_cast<uint32_t>(std::distance(str.begin(), str.end()));
    -                app_specific.vis.visit_str(size ? &str.front() : nullptr, size);
    +                app_specific.vis.visit_str(size ? app_specific.contiguous(str) : nullptr, size);
                 }
             )
         ]
    @@ -368,7 +382,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& str = _attr(ctx);
                     auto size = static_cast<uint32_t>(std::distance(str.begin(), str.end()));
    -                app_specific.vis.visit_str(size ? &str.front() : nullptr, size);
    +                app_specific.vis.visit_str(size ? app_specific.contiguous(str) : nullptr, size);
                 }
             )
         ]
    @@ -392,7 +406,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& str = _attr(ctx);
                     auto size = static_cast<uint32_t>(std::distance(str.begin(), str.end()));
    -                app_specific.vis.visit_str(size ? &str.front() : nullptr, size);
    +                app_specific.vis.visit_str(size ? app_specific.contiguous(str) : nullptr, size);
                 }
             )
         ]
    @@ -416,7 +430,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& str = _attr(ctx);
                     auto size = static_cast<uint32_t>(std::distance(str.begin(), str.end()));
    -                app_specific.vis.visit_str(size ? &str.front() : nullptr, size);
    +                app_specific.vis.visit_str(size ? app_specific.contiguous(str) : nullptr, size);
                 }
             )
         ]
    @@ -440,7 +454,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& bin = _attr(ctx);
                     auto size = static_cast<uint32_t>(std::distance(bin.begin(), bin.end()));
    -                app_specific.vis.visit_bin(size ? &bin.front() : nullptr, size);
    +                app_specific.vis.visit_bin(size ? app_specific.contiguous(bin) : nullptr, size);
                 }
             )
         ]
    @@ -464,7 +478,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& bin = _attr(ctx);
                     auto size = static_cast<uint32_t>(std::distance(bin.begin(), bin.end()));
    -                app_specific.vis.visit_bin(size ? &bin.front() : nullptr, size);
    +                app_specific.vis.visit_bin(size ? app_specific.contiguous(bin) : nullptr, size);
                 }
             )
         ]
    @@ -488,7 +502,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& bin = _attr(ctx);
                     auto size = static_cast<uint32_t>(std::distance(bin.begin(), bin.end()));
    -                app_specific.vis.visit_bin(size ? &bin.front() : nullptr, size);
    +                app_specific.vis.visit_bin(size ? app_specific.contiguous(bin) : nullptr, size);
                 }
             )
         ]
    @@ -596,7 +610,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& ext = _attr(ctx);
                     auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
    -                app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
    +                app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
                 }
             )
         ]
    @@ -620,7 +634,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& ext = _attr(ctx);
                     auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
    -                app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
    +                app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
                 }
             )
         ]
    @@ -644,7 +658,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& ext = _attr(ctx);
                     auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
    -                app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
    +                app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
                 }
             )
         ]
    @@ -668,7 +682,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& ext = _attr(ctx);
                     auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
    -                app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
    +                app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
                 }
             )
         ]
    @@ -692,7 +706,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& ext = _attr(ctx);
                     auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
    -                app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
    +                app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
                 }
             )
         ]
    @@ -716,7 +730,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& ext = _attr(ctx);
                     auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
    -                app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
    +                app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
                 }
             )
         ]
    @@ -740,7 +754,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& ext = _attr(ctx);
                     auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
    -                app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
    +                app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
                 }
             )
         ]
    @@ -768,7 +782,7 @@
                     auto& app_specific = x3::get<tag_app_specific>(ctx).get();
                     auto const& ext = _attr(ctx);
                     auto size = static_cast<std::size_t>(std::distance(ext.begin(), ext.end()));
    -                app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size);
    +                app_specific.vis.visit_ext(size ? app_specific.contiguous(ext) : nullptr, size);
                 }
             )
         ];
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