Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 192 additions & 0 deletions fuzz/stream_unpack_fuzzer.cpp
Original file line number Diff line number Diff line change
@@ -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 <msgpack.hpp>

#include <algorithm>
#include <cstdint>
#include <cstring>
#include <memory>
#include <vector>

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<std::uint64_t>(d_[s_]);
}
return v;
}

std::size_t tail_range(std::size_t lo, std::size_t hi) {
return lo + static_cast<std::size_t>(
tail_u64() % static_cast<std::uint64_t>(hi - lo + 1));
}

int tail_range_int(int lo, int hi) {
return lo + static_cast<int>(
tail_u64() % static_cast<std::uint64_t>(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<ref_decision*>(user_data);
switch (d->mode) {
case 0: return true;
case 1: return false;
default: return (static_cast<unsigned>(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<uint8_t> payload(br.data(), br.data() + br.remaining());
if (payload.empty()) return 0;
const std::size_t total = std::min<std::size_t>(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<std::size_t>(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<std::size_t>(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<std::size_t>(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<std::size_t>(upk.data().type));
++objects;
std::unique_ptr<msgpack::zone> 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;
}
45 changes: 45 additions & 0 deletions fuzz/stream_unpack_fuzzer.dict
Original file line number Diff line number Diff line change
@@ -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"
Binary file not shown.
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/Arrays
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
’“’
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/Bin300
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Å,
Binary file added fuzz/stream_unpack_fuzzer_seed_corpus/BinSizes
Binary file not shown.
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/ExtTypes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ÔÕÖØ
Binary file added fuzz/stream_unpack_fuzzer_seed_corpus/FixintEdges
Binary file not shown.
Binary file added fuzz/stream_unpack_fuzzer_seed_corpus/Floats
Binary file not shown.
Binary file added fuzz/stream_unpack_fuzzer_seed_corpus/Int16Edges
Binary file not shown.
Binary file added fuzz/stream_unpack_fuzzer_seed_corpus/Int32Edges
Binary file not shown.
Binary file added fuzz/stream_unpack_fuzzer_seed_corpus/Int64Edges
Binary file not shown.
Binary file added fuzz/stream_unpack_fuzzer_seed_corpus/Int8Edges
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/MapStrInt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ƒ¡x¡y¡z
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/Maps
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
€¡a‚¡k¡v¡j¢vv
Binary file added fuzz/stream_unpack_fuzzer_seed_corpus/MixedTree
Binary file not shown.
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/MultiMessage
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
‘¡z
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/Nested32
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/NilBool
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ÀÂÃ
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/Str255
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ùÿaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Binary file added fuzz/stream_unpack_fuzzer_seed_corpus/Str256
Binary file not shown.
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/Str300
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ú,cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/StrFix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
¡x£abc
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/StrUtf8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
¦café¤ðŸ˜€
Binary file added fuzz/stream_unpack_fuzzer_seed_corpus/Timestamps
Binary file not shown.
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/TruncatedStr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ùab
1 change: 1 addition & 0 deletions fuzz/stream_unpack_fuzzer_seed_corpus/UintEdges
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ÌÿÍÿÿÎÿÿÿÿÏÿÿÿÿÿÿÿÿ
Loading