|
| 1 | +// Fuzz harness: msgpack::v1::unpacker streaming deserialisation state machine. |
| 2 | +// |
| 3 | +// Threat model (why this harness should find bugs): |
| 4 | +// - msgpack is a deserialiser: the bytes are attacker controlled in every |
| 5 | +// deployment (IPC, caches, RPC payloads). The production harness |
| 6 | +// (unpack_pack_fuzzer) only ever exercises the one-shot msgpack::unpack() |
| 7 | +// path, so the streaming unpacker state machine - the documented way to |
| 8 | +// consume bytes off a socket or file in chunks - is never fuzzed (under |
| 9 | +// the existing coverage measurement the streaming path of |
| 10 | +// include/msgpack/v1/unpack.hpp shows almost no exercised lines or |
| 11 | +// functions). |
| 12 | +// - The attacker controls both the byte stream and, in real users, the chunk |
| 13 | +// boundaries it arrives in; this harness derives both from the fuzz input. |
| 14 | +// - Expected bug classes: OOB reads/writes or use-after-free in |
| 15 | +// expand_buffer()/reserve_buffer() pointer arithmetic and in zone flush / |
| 16 | +// reference-count handling when str/bin/ext objects reference the internal |
| 17 | +// buffer (unpack_reference_func), wrong state transitions when a message |
| 18 | +// spans chunk boundaries (single-byte feeds), and hangs on adversarial |
| 19 | +// aggregate headers. |
| 20 | +// |
| 21 | +// Design notes: |
| 22 | +// - Feed protocol follows the class contract exactly: reserve_buffer(want), |
| 23 | +// memcpy into buffer() clamped to buffer_capacity(), buffer_consumed(n). |
| 24 | +// - unpack_limit fields are fuzz-chosen but bounded (depth<=64, aggregates |
| 25 | +// <=128, scalars<=4096) so limit enforcement is exercised without OOM. |
| 26 | +// - initial_buffer_size is fuzz-chosen (16..65536) to stress expand_buffer(). |
| 27 | +// - Work is bounded: total fed <= 64 KiB, objects <= 4096. |
| 28 | +// - msgpack::parse_error on invalid bytes is documented behaviour and is |
| 29 | +// caught; the swallow-all outer catch mirrors the incumbent |
| 30 | +// unpack_pack_fuzzer. |
| 31 | +#include <msgpack.hpp> |
| 32 | + |
| 33 | +#include <algorithm> |
| 34 | +#include <cstdint> |
| 35 | +#include <cstring> |
| 36 | +#include <memory> |
| 37 | +#include <vector> |
| 38 | + |
| 39 | +namespace { |
| 40 | + |
| 41 | +// Minimal C++11 replacement for FuzzedDataProvider (whose bundled header no |
| 42 | +// longer compiles under the project's -std=c++11 build convention): options |
| 43 | +// are consumed from the tail of the input, the payload is what remains. |
| 44 | +class byte_reader { |
| 45 | +public: |
| 46 | + byte_reader(const std::uint8_t* data, std::size_t size) |
| 47 | + : d_(data), s_(size) {} |
| 48 | + |
| 49 | + std::uint8_t tail_u8() { |
| 50 | + if (s_ == 0) return 0; |
| 51 | + --s_; |
| 52 | + return d_[s_]; |
| 53 | + } |
| 54 | + |
| 55 | + std::uint64_t tail_u64() { |
| 56 | + std::uint64_t v = 0; |
| 57 | + for (int i = 0; i < 8 && s_ > 0; ++i) { |
| 58 | + --s_; |
| 59 | + v = (v << 8) | static_cast<std::uint64_t>(d_[s_]); |
| 60 | + } |
| 61 | + return v; |
| 62 | + } |
| 63 | + |
| 64 | + std::size_t tail_range(std::size_t lo, std::size_t hi) { |
| 65 | + return lo + static_cast<std::size_t>( |
| 66 | + tail_u64() % static_cast<std::uint64_t>(hi - lo + 1)); |
| 67 | + } |
| 68 | + |
| 69 | + int tail_range_int(int lo, int hi) { |
| 70 | + return lo + static_cast<int>( |
| 71 | + tail_u64() % static_cast<std::uint64_t>(hi - lo + 1)); |
| 72 | + } |
| 73 | + |
| 74 | + const std::uint8_t* data() const { return d_; } |
| 75 | + std::size_t remaining() const { return s_; } |
| 76 | + |
| 77 | +private: |
| 78 | + const std::uint8_t* d_; |
| 79 | + std::size_t s_; |
| 80 | +}; |
| 81 | + |
| 82 | +struct ref_decision { |
| 83 | + int mode; |
| 84 | +}; |
| 85 | + |
| 86 | +// unpack_reference_func decides whether str/bin/ext values reference the |
| 87 | +// unpacker's internal buffer (zero-copy) or are copied into the zone. The |
| 88 | +// three modes exercise both answers and mixed per-type/per-length decisions. |
| 89 | +bool fuzz_reference_func(msgpack::type::object_type type, std::size_t len, |
| 90 | + void* user_data) { |
| 91 | + ref_decision* d = static_cast<ref_decision*>(user_data); |
| 92 | + switch (d->mode) { |
| 93 | + case 0: return true; |
| 94 | + case 1: return false; |
| 95 | + default: return (static_cast<unsigned>(type) + len) % 2 == 0; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +const std::size_t kMaxTotalFed = 64 * 1024; |
| 100 | +const std::size_t kMaxObjects = 4096; |
| 101 | + |
| 102 | +void sink_u64(std::size_t v) { |
| 103 | + static volatile std::size_t sink; |
| 104 | + sink = v; |
| 105 | +} |
| 106 | + |
| 107 | +} // namespace |
| 108 | + |
| 109 | +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 110 | + if (size < 8) return 0; |
| 111 | + byte_reader br(data, size); |
| 112 | + const int ref_mode = br.tail_range_int(0, 2); |
| 113 | + const int initial_buf_exp = br.tail_range_int(4, 16); |
| 114 | + const int depth = br.tail_range_int(1, 64); |
| 115 | + const int aggregate = br.tail_range_int(1, 128); |
| 116 | + const int scalar = br.tail_range_int(1, 4096); |
| 117 | + const std::size_t chunk = br.tail_range(1, 512); |
| 118 | + const int tail_mode = br.tail_range_int(0, 3); |
| 119 | + const bool legacy_path = (br.tail_u8() & 1u) != 0; |
| 120 | + std::vector<uint8_t> payload(br.data(), br.data() + br.remaining()); |
| 121 | + if (payload.empty()) return 0; |
| 122 | + const std::size_t total = std::min<std::size_t>(payload.size(), kMaxTotalFed); |
| 123 | + |
| 124 | + const msgpack::unpack_limit limit( |
| 125 | + depth, aggregate, aggregate, scalar, scalar, scalar); |
| 126 | + ref_decision ctx{ref_mode}; |
| 127 | + |
| 128 | + if (!legacy_path) { |
| 129 | + // Documented streaming protocol. |
| 130 | + msgpack::v1::unpacker upk(fuzz_reference_func, &ctx, |
| 131 | + static_cast<std::size_t>(1) << initial_buf_exp, |
| 132 | + limit); |
| 133 | + std::size_t pos = 0; |
| 134 | + std::size_t objects = 0; |
| 135 | + try { |
| 136 | + while (pos < total && objects < kMaxObjects) { |
| 137 | + const std::size_t want = std::min(chunk, total - pos); |
| 138 | + upk.reserve_buffer(want); |
| 139 | + const std::size_t n = std::min(want, upk.buffer_capacity()); |
| 140 | + if (n == 0) break; |
| 141 | + std::memcpy(upk.buffer(), payload.data() + pos, n); |
| 142 | + upk.buffer_consumed(n); |
| 143 | + pos += n; |
| 144 | + bool referenced = false; |
| 145 | + msgpack::object_handle oh; |
| 146 | + while (upk.next(oh, referenced)) { |
| 147 | + ++objects; |
| 148 | + sink_u64(static_cast<std::size_t>(oh.get().type)); |
| 149 | + if (objects >= kMaxObjects) break; |
| 150 | + } |
| 151 | + if (upk.nonparsed_size() > 0) { |
| 152 | + switch (tail_mode) { |
| 153 | + case 0: upk.remove_nonparsed_buffer(); break; |
| 154 | + case 1: upk.skip_nonparsed_buffer(upk.nonparsed_size() / 2); break; |
| 155 | + case 2: break; |
| 156 | + default: upk.reset(); pos = total; break; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + sink_u64(upk.message_size() + upk.parsed_size() + upk.buffer_capacity()); |
| 161 | + } catch (...) { |
| 162 | + // parse_error on invalid bytes: documented behaviour. |
| 163 | + } |
| 164 | + } else { |
| 165 | + // Legacy execute()/data()/release_zone()/reset_zone() interface |
| 166 | + // (backward-compat paths, dark in production coverage). |
| 167 | + try { |
| 168 | + msgpack::v1::unpacker upk(fuzz_reference_func, &ctx, 64, limit); |
| 169 | + std::size_t pos = 0; |
| 170 | + std::size_t objects = 0; |
| 171 | + while (pos < total && objects < kMaxObjects) { |
| 172 | + const std::size_t want = std::min<std::size_t>(256, total - pos); |
| 173 | + upk.reserve_buffer(want); |
| 174 | + const std::size_t n = std::min(want, upk.buffer_capacity()); |
| 175 | + std::memcpy(upk.buffer(), payload.data() + pos, n); |
| 176 | + upk.buffer_consumed(n); |
| 177 | + pos += n; |
| 178 | + while (upk.execute() && objects < kMaxObjects) { |
| 179 | + sink_u64(static_cast<std::size_t>(upk.data().type)); |
| 180 | + ++objects; |
| 181 | + std::unique_ptr<msgpack::zone> z(upk.release_zone()); |
| 182 | + // The execute() contract requires a full reset() before the |
| 183 | + // next call (msgpack_unpacker_reset() in the C API); |
| 184 | + // reset_zone() alone would leave stale parser state. |
| 185 | + upk.reset(); |
| 186 | + } |
| 187 | + } |
| 188 | + } catch (...) { |
| 189 | + } |
| 190 | + } |
| 191 | + return 0; |
| 192 | +} |
0 commit comments