From 8e5815fb0f6a3eac46d6d1a0e078a278019f0d84 Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Mon, 9 Nov 2026 09:40:45 +0000 Subject: [PATCH] OSS-Fuzz: Add new fuzzer/corpus/dict targets stream unpacking operations Signed-off-by: Arthur Chan --- fuzz/stream_unpack_fuzzer.cpp | 192 ++++++++++++++++++ fuzz/stream_unpack_fuzzer.dict | 45 ++++ .../Array32Short | Bin 0 -> 6 bytes fuzz/stream_unpack_fuzzer_seed_corpus/Arrays | 1 + fuzz/stream_unpack_fuzzer_seed_corpus/Bin300 | 1 + .../stream_unpack_fuzzer_seed_corpus/BinSizes | Bin 0 -> 268 bytes .../stream_unpack_fuzzer_seed_corpus/ExtTypes | 1 + .../FixintEdges | Bin 0 -> 4 bytes fuzz/stream_unpack_fuzzer_seed_corpus/Floats | Bin 0 -> 27 bytes .../Int16Edges | Bin 0 -> 9 bytes .../Int32Edges | Bin 0 -> 15 bytes .../Int64Edges | Bin 0 -> 32 bytes .../Int8Edges | Bin 0 -> 8 bytes .../MalformedMap | Bin 0 -> 6 bytes .../MapStrInt | 1 + fuzz/stream_unpack_fuzzer_seed_corpus/Maps | 1 + .../MixedTree | Bin 0 -> 25 bytes .../MultiMessage | 1 + .../stream_unpack_fuzzer_seed_corpus/Nested32 | 1 + fuzz/stream_unpack_fuzzer_seed_corpus/NilBool | 1 + fuzz/stream_unpack_fuzzer_seed_corpus/Str255 | 1 + fuzz/stream_unpack_fuzzer_seed_corpus/Str256 | Bin 0 -> 259 bytes fuzz/stream_unpack_fuzzer_seed_corpus/Str300 | 1 + fuzz/stream_unpack_fuzzer_seed_corpus/StrFix | 1 + fuzz/stream_unpack_fuzzer_seed_corpus/StrUtf8 | 1 + .../Timestamps | Bin 0 -> 31 bytes .../TruncatedStr | 1 + .../UintEdges | 1 + 28 files changed, 251 insertions(+) create mode 100644 fuzz/stream_unpack_fuzzer.cpp create mode 100644 fuzz/stream_unpack_fuzzer.dict create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Array32Short create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Arrays create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Bin300 create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/BinSizes create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/ExtTypes create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/FixintEdges create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Floats create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Int16Edges create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Int32Edges create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Int64Edges create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Int8Edges create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/MalformedMap create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/MapStrInt create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Maps create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/MixedTree create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/MultiMessage create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Nested32 create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/NilBool create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Str255 create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Str256 create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Str300 create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/StrFix create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/StrUtf8 create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/Timestamps create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/TruncatedStr create mode 100644 fuzz/stream_unpack_fuzzer_seed_corpus/UintEdges 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 0000000000000000000000000000000000000000..eec653fb298fef1fe43902065b33c1bd075fc5fc GIT binary patch literal 6 Ncmcc1z`(%B2ml2)0N?-s literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..c7a75abeb8872957b91cb91dc8abd4335faa05d2 GIT binary patch literal 268 UcmX@Y$_N5S85x*HI5@%p0I41XSpWb4 literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..fdc28a16161e109241f27ebc78c5598833a8385f GIT binary patch literal 4 LcmZRmfAAjw1V92` literal 0 HcmV?d00001 diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Floats b/fuzz/stream_unpack_fuzzer_seed_corpus/Floats new file mode 100644 index 0000000000000000000000000000000000000000..e7008f04df2e4272c2896efb2c848ad727bee328 GIT binary patch literal 27 ecmX?Q-@w3ds=ogJ|I-efioZi#bWbxt03!gSE(s9; literal 0 HcmV?d00001 diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Int16Edges b/fuzz/stream_unpack_fuzzer_seed_corpus/Int16Edges new file mode 100644 index 0000000000000000000000000000000000000000..60821b6dec6cff0a3324c439707aa58727aa2b33 GIT binary patch literal 9 QcmccEz;Lnt|Hc3R0UR#|Z~y=R literal 0 HcmV?d00001 diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Int32Edges b/fuzz/stream_unpack_fuzzer_seed_corpus/Int32Edges new file mode 100644 index 0000000000000000000000000000000000000000..5eb93a50c7588725ad7e6bd00a2c8dfce5267509 GIT binary patch literal 15 VcmccAz`($8ss8`}|CfOHKLA4m3U2@a literal 0 HcmV?d00001 diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Int64Edges b/fuzz/stream_unpack_fuzzer_seed_corpus/Int64Edges new file mode 100644 index 0000000000000000000000000000000000000000..b594731ca02ac77e548644fb13fb37202d908a88 GIT binary patch literal 32 UcmccIzyJo9>;Hqn<^QM<0660pFaQ7m literal 0 HcmV?d00001 diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/Int8Edges b/fuzz/stream_unpack_fuzzer_seed_corpus/Int8Edges new file mode 100644 index 0000000000000000000000000000000000000000..4e86e1f063bb1b3a6c9819df96bc76be836368bd GIT binary patch literal 8 Pcmcc6aH0Of{|gKN89)U; literal 0 HcmV?d00001 diff --git a/fuzz/stream_unpack_fuzzer_seed_corpus/MalformedMap b/fuzz/stream_unpack_fuzzer_seed_corpus/MalformedMap new file mode 100644 index 0000000000000000000000000000000000000000..df64c08cf1a3665a69a5e33ddb007b66137701d1 GIT binary patch literal 6 Ncmcb|z_c)t5da710ptJx literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..279793dd46ac752c170c5921a408b40a719dbb23 GIT binary patch literal 25 fcmbQ-xG?)TGdm*_^P~&^&S;(9|A7Gn1P%ZIhU5vD literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..00d3fe1f891c254b42aa015a29d0974022b9e939 GIT binary patch literal 259 Ncmcb`$dEL`0RR$#V%z`# literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..64ae12b3761b6fe4374775a49a369f6b2cac7388 GIT binary patch literal 31 Xcmccip8*K2|7V1P<2+yiCMW;^e)