diff --git a/fuzz/stream_unpack_fuzzer.cpp b/fuzz/stream_unpack_fuzzer.cpp new file mode 100644 index 000000000..1c60a8bd8 --- /dev/null +++ b/fuzz/stream_unpack_fuzzer.cpp @@ -0,0 +1,192 @@ +// Fuzz harness: msgpack::v1::unpacker streaming deserialisation state machine. +// +// Threat model (why this harness should find bugs): +// - msgpack is a deserialiser: the bytes are attacker controlled in every +// deployment (IPC, caches, RPC payloads). The production harness +// (unpack_pack_fuzzer) only ever exercises the one-shot msgpack::unpack() +// path, so the streaming unpacker state machine - the documented way to +// consume bytes off a socket or file in chunks - is never fuzzed (under +// the existing coverage measurement the streaming path of +// include/msgpack/v1/unpack.hpp shows almost no exercised lines or +// functions). +// - The attacker controls both the byte stream and, in real users, the chunk +// boundaries it arrives in; this harness derives both from the fuzz input. +// - Expected bug classes: OOB reads/writes or use-after-free in +// expand_buffer()/reserve_buffer() pointer arithmetic and in zone flush / +// reference-count handling when str/bin/ext objects reference the internal +// buffer (unpack_reference_func), wrong state transitions when a message +// spans chunk boundaries (single-byte feeds), and hangs on adversarial +// aggregate headers. +// +// Design notes: +// - Feed protocol follows the class contract exactly: reserve_buffer(want), +// memcpy into buffer() clamped to buffer_capacity(), buffer_consumed(n). +// - unpack_limit fields are fuzz-chosen but bounded (depth<=64, aggregates +// <=128, scalars<=4096) so limit enforcement is exercised without OOM. +// - initial_buffer_size is fuzz-chosen (16..65536) to stress expand_buffer(). +// - Work is bounded: total fed <= 64 KiB, objects <= 4096. +// - msgpack::parse_error on invalid bytes is documented behaviour and is +// caught; the swallow-all outer catch mirrors the incumbent +// unpack_pack_fuzzer. +#include + +#include +#include +#include +#include +#include + +namespace { + +// Minimal C++11 replacement for FuzzedDataProvider (whose bundled header no +// longer compiles under the project's -std=c++11 build convention): options +// are consumed from the tail of the input, the payload is what remains. +class byte_reader { +public: + byte_reader(const std::uint8_t* data, std::size_t size) + : d_(data), s_(size) {} + + std::uint8_t tail_u8() { + if (s_ == 0) return 0; + --s_; + return d_[s_]; + } + + std::uint64_t tail_u64() { + std::uint64_t v = 0; + for (int i = 0; i < 8 && s_ > 0; ++i) { + --s_; + v = (v << 8) | static_cast(d_[s_]); + } + return v; + } + + std::size_t tail_range(std::size_t lo, std::size_t hi) { + return lo + static_cast( + tail_u64() % static_cast(hi - lo + 1)); + } + + int tail_range_int(int lo, int hi) { + return lo + static_cast( + tail_u64() % static_cast(hi - lo + 1)); + } + + const std::uint8_t* data() const { return d_; } + std::size_t remaining() const { return s_; } + +private: + const std::uint8_t* d_; + std::size_t s_; +}; + +struct ref_decision { + int mode; +}; + +// unpack_reference_func decides whether str/bin/ext values reference the +// unpacker's internal buffer (zero-copy) or are copied into the zone. The +// three modes exercise both answers and mixed per-type/per-length decisions. +bool fuzz_reference_func(msgpack::type::object_type type, std::size_t len, + void* user_data) { + ref_decision* d = static_cast(user_data); + switch (d->mode) { + case 0: return true; + case 1: return false; + default: return (static_cast(type) + len) % 2 == 0; + } +} + +const std::size_t kMaxTotalFed = 64 * 1024; +const std::size_t kMaxObjects = 4096; + +void sink_u64(std::size_t v) { + static volatile std::size_t sink; + sink = v; +} + +} // namespace + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + if (size < 8) return 0; + byte_reader br(data, size); + const int ref_mode = br.tail_range_int(0, 2); + const int initial_buf_exp = br.tail_range_int(4, 16); + const int depth = br.tail_range_int(1, 64); + const int aggregate = br.tail_range_int(1, 128); + const int scalar = br.tail_range_int(1, 4096); + const std::size_t chunk = br.tail_range(1, 512); + const int tail_mode = br.tail_range_int(0, 3); + const bool legacy_path = (br.tail_u8() & 1u) != 0; + std::vector payload(br.data(), br.data() + br.remaining()); + if (payload.empty()) return 0; + const std::size_t total = std::min(payload.size(), kMaxTotalFed); + + const msgpack::unpack_limit limit( + depth, aggregate, aggregate, scalar, scalar, scalar); + ref_decision ctx{ref_mode}; + + if (!legacy_path) { + // Documented streaming protocol. + msgpack::v1::unpacker upk(fuzz_reference_func, &ctx, + static_cast(1) << initial_buf_exp, + limit); + std::size_t pos = 0; + std::size_t objects = 0; + try { + while (pos < total && objects < kMaxObjects) { + const std::size_t want = std::min(chunk, total - pos); + upk.reserve_buffer(want); + const std::size_t n = std::min(want, upk.buffer_capacity()); + if (n == 0) break; + std::memcpy(upk.buffer(), payload.data() + pos, n); + upk.buffer_consumed(n); + pos += n; + bool referenced = false; + msgpack::object_handle oh; + while (upk.next(oh, referenced)) { + ++objects; + sink_u64(static_cast(oh.get().type)); + if (objects >= kMaxObjects) break; + } + if (upk.nonparsed_size() > 0) { + switch (tail_mode) { + case 0: upk.remove_nonparsed_buffer(); break; + case 1: upk.skip_nonparsed_buffer(upk.nonparsed_size() / 2); break; + case 2: break; + default: upk.reset(); pos = total; break; + } + } + } + sink_u64(upk.message_size() + upk.parsed_size() + upk.buffer_capacity()); + } catch (...) { + // parse_error on invalid bytes: documented behaviour. + } + } else { + // Legacy execute()/data()/release_zone()/reset_zone() interface + // (backward-compat paths, dark in production coverage). + try { + msgpack::v1::unpacker upk(fuzz_reference_func, &ctx, 64, limit); + std::size_t pos = 0; + std::size_t objects = 0; + while (pos < total && objects < kMaxObjects) { + const std::size_t want = std::min(256, total - pos); + upk.reserve_buffer(want); + const std::size_t n = std::min(want, upk.buffer_capacity()); + std::memcpy(upk.buffer(), payload.data() + pos, n); + upk.buffer_consumed(n); + pos += n; + while (upk.execute() && objects < kMaxObjects) { + sink_u64(static_cast(upk.data().type)); + ++objects; + std::unique_ptr z(upk.release_zone()); + // The execute() contract requires a full reset() before the + // next call (msgpack_unpacker_reset() in the C API); + // reset_zone() alone would leave stale parser state. + upk.reset(); + } + } + } catch (...) { + } + } + return 0; +} diff --git a/fuzz/stream_unpack_fuzzer.dict b/fuzz/stream_unpack_fuzzer.dict new file mode 100644 index 000000000..6a936fa7e --- /dev/null +++ b/fuzz/stream_unpack_fuzzer.dict @@ -0,0 +1,45 @@ +nil="\xc0" +false="\xc2" +true="\xc3" +bin8="\xc4" +bin16="\xc5" +bin32="\xc6" +ext8="\xc7" +ext16="\xc8" +ext32="\xc9" +float32="\xca" +float64="\xcb" +uint8="\xcc" +uint16="\xcd" +uint32="\xce" +uint64="\xcf" +int8="\xd0" +int16="\xd1" +int32="\xd2" +int64="\xd3" +fixext1="\xd4" +fixext2="\xd5" +fixext4="\xd6" +fixext8="\xd7" +fixext16="\xd8" +str8="\xd9" +str16="\xda" +str32="\xdb" +array16="\xdc" +array32="\xdd" +map16="\xde" +map32="\xdf" +pos_fixint_min="\x00" +pos_fixint_max="\x7f" +neg_fixint_min="\xe0" +neg_fixint_max="\xff" +fixmap_min="\x80" +fixmap_max="\x8f" +fixarray_min="\x90" +fixarray_max="\x9f" +fixstr_min="\xa0" +fixstr_max="\xbf" +timestamp32="\xd6\xff" +timestamp64="\xd7\xff" +timestamp96="\xc7\x0c\xff" +never_used="\xc1" diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Array32Short b/fuzz/stream_unpack_fuzzer_seed_corpus/Array32Short new file mode 100644 index 000000000..eec653fb2 Binary files /dev/null and b/fuzz/stream_unpack_fuzzer_seed_corpus/Array32Short differ diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Arrays b/fuzz/stream_unpack_fuzzer_seed_corpus/Arrays new file mode 100644 index 000000000..f426c24a6 --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/Arrays @@ -0,0 +1 @@ +’“’ \ No newline at end of file diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Bin300 b/fuzz/stream_unpack_fuzzer_seed_corpus/Bin300 new file mode 100644 index 000000000..31913affd --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/Bin300 @@ -0,0 +1 @@ +Å, \ No newline at end of file diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/BinSizes b/fuzz/stream_unpack_fuzzer_seed_corpus/BinSizes new file mode 100644 index 000000000..c7a75abeb Binary files /dev/null and b/fuzz/stream_unpack_fuzzer_seed_corpus/BinSizes differ diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/ExtTypes b/fuzz/stream_unpack_fuzzer_seed_corpus/ExtTypes new file mode 100644 index 000000000..0b6b9677f --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/ExtTypes @@ -0,0 +1 @@ +ŌÕÖŲ \ No newline at end of file diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/FixintEdges b/fuzz/stream_unpack_fuzzer_seed_corpus/FixintEdges new file mode 100644 index 000000000..fdc28a161 Binary files /dev/null and b/fuzz/stream_unpack_fuzzer_seed_corpus/FixintEdges differ diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Floats b/fuzz/stream_unpack_fuzzer_seed_corpus/Floats new file mode 100644 index 000000000..e7008f04d Binary files /dev/null and b/fuzz/stream_unpack_fuzzer_seed_corpus/Floats differ diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Int16Edges b/fuzz/stream_unpack_fuzzer_seed_corpus/Int16Edges new file mode 100644 index 000000000..60821b6de Binary files /dev/null and b/fuzz/stream_unpack_fuzzer_seed_corpus/Int16Edges differ diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Int32Edges b/fuzz/stream_unpack_fuzzer_seed_corpus/Int32Edges new file mode 100644 index 000000000..5eb93a50c Binary files /dev/null and b/fuzz/stream_unpack_fuzzer_seed_corpus/Int32Edges differ diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Int64Edges b/fuzz/stream_unpack_fuzzer_seed_corpus/Int64Edges new file mode 100644 index 000000000..b594731ca Binary files /dev/null and b/fuzz/stream_unpack_fuzzer_seed_corpus/Int64Edges differ diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Int8Edges b/fuzz/stream_unpack_fuzzer_seed_corpus/Int8Edges new file mode 100644 index 000000000..4e86e1f06 Binary files /dev/null and b/fuzz/stream_unpack_fuzzer_seed_corpus/Int8Edges differ diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/MalformedMap b/fuzz/stream_unpack_fuzzer_seed_corpus/MalformedMap new file mode 100644 index 000000000..df64c08cf Binary files /dev/null and b/fuzz/stream_unpack_fuzzer_seed_corpus/MalformedMap differ diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/MapStrInt b/fuzz/stream_unpack_fuzzer_seed_corpus/MapStrInt new file mode 100644 index 000000000..c717962f3 --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/MapStrInt @@ -0,0 +1 @@ +ƒ”x”y”z \ No newline at end of file diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Maps b/fuzz/stream_unpack_fuzzer_seed_corpus/Maps new file mode 100644 index 000000000..f5557c69f --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/Maps @@ -0,0 +1 @@ +€”a‚”k”v”j¢vv \ No newline at end of file diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/MixedTree b/fuzz/stream_unpack_fuzzer_seed_corpus/MixedTree new file mode 100644 index 000000000..279793dd4 Binary files /dev/null and b/fuzz/stream_unpack_fuzzer_seed_corpus/MixedTree differ diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/MultiMessage b/fuzz/stream_unpack_fuzzer_seed_corpus/MultiMessage new file mode 100644 index 000000000..307fac2bf --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/MultiMessage @@ -0,0 +1 @@ +Ā‘”z \ No newline at end of file diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Nested32 b/fuzz/stream_unpack_fuzzer_seed_corpus/Nested32 new file mode 100644 index 000000000..d8a72681d --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/Nested32 @@ -0,0 +1 @@ +‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘ \ No newline at end of file diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/NilBool b/fuzz/stream_unpack_fuzzer_seed_corpus/NilBool new file mode 100644 index 000000000..ce2fe1dcb --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/NilBool @@ -0,0 +1 @@ +ĄĀĆ \ No newline at end of file diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Str255 b/fuzz/stream_unpack_fuzzer_seed_corpus/Str255 new file mode 100644 index 000000000..3962a854b --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/Str255 @@ -0,0 +1 @@ +Ł’aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \ No newline at end of file diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Str256 b/fuzz/stream_unpack_fuzzer_seed_corpus/Str256 new file mode 100644 index 000000000..00d3fe1f8 Binary files /dev/null and b/fuzz/stream_unpack_fuzzer_seed_corpus/Str256 differ diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Str300 b/fuzz/stream_unpack_fuzzer_seed_corpus/Str300 new file mode 100644 index 000000000..b13495532 --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/Str300 @@ -0,0 +1 @@ +Ś,cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc \ No newline at end of file diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/StrFix b/fuzz/stream_unpack_fuzzer_seed_corpus/StrFix new file mode 100644 index 000000000..fdfcc9dcb --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/StrFix @@ -0,0 +1 @@ +”x£abc \ No newline at end of file diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/StrUtf8 b/fuzz/stream_unpack_fuzzer_seed_corpus/StrUtf8 new file mode 100644 index 000000000..2c9071b26 --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/StrUtf8 @@ -0,0 +1 @@ +¦cafĆ©¤šŸ˜€ \ No newline at end of file diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Timestamps b/fuzz/stream_unpack_fuzzer_seed_corpus/Timestamps new file mode 100644 index 000000000..64ae12b37 Binary files /dev/null and b/fuzz/stream_unpack_fuzzer_seed_corpus/Timestamps differ diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/TruncatedStr b/fuzz/stream_unpack_fuzzer_seed_corpus/TruncatedStr new file mode 100644 index 000000000..e155fc020 --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/TruncatedStr @@ -0,0 +1 @@ +Łab \ No newline at end of file diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/UintEdges b/fuzz/stream_unpack_fuzzer_seed_corpus/UintEdges new file mode 100644 index 000000000..b05d03467 --- /dev/null +++ b/fuzz/stream_unpack_fuzzer_seed_corpus/UintEdges @@ -0,0 +1 @@ +Ģ’Ķ’’Ī’’’’Ļ’’’’’’’’ \ No newline at end of file