// Event-store block decoding over untrusted bytes. decode_block must never // crash or allocate beyond kMaxBlockRaw, and a block it accepts must // re-encode and decode to the same content. The first byte is a control byte: // bit 0 offer a (fixed) dictionary to the decoder // bit 1 recompute both CRCs first, so mutations reach header validation // and the zstd decompressor instead of stopping at the checksum #include "strawberry/store/codec.hpp" #include #include #include #include using namespace strawberry::store; namespace { std::shared_ptr make_dictionary() { std::vector samples; for (int i = 0; i < 64; ++i) { samples.push_back("/usr/bin/python3 -c import os; exec=" + std::to_string(i * 7919) + " /home/user/.local/bin/tool --flag " + std::to_string(i)); } return ZstdDictionary::train(samples, 1024, 3); } const ZstdDictionary* lookup(uint32_t id, const void* ctx) noexcept { const auto* d = static_cast(ctx); return d != nullptr && d->id() == id ? d : nullptr; } } // namespace extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { static const auto dict = make_dictionary(); if (size == 0) return 0; const bool offer_dict = (data[0] & 1) != 0; std::string bytes(reinterpret_cast(data) + 1, size - 1); if ((data[0] & 2) != 0 && bytes.size() >= kBlockHeaderBytes) { auto put32 = [&](std::size_t at, uint32_t v) { for (int i = 0; i < 4; ++i) bytes[at + i] = static_cast(v >> (8 * i)); }; uint32_t stored = 0; for (int i = 0; i < 4; ++i) stored |= static_cast(static_cast(bytes[12 + i])) << (8 * i); const std::size_t avail = bytes.size() - kBlockHeaderBytes; const std::size_t n = stored < avail ? stored : avail; put32(20, crc32c(bytes.data() + kBlockHeaderBytes, n)); put32(24, crc32c(bytes.data(), 24)); } const std::string_view in(bytes); std::string raw; std::size_t consumed = 0; BlockInfo info; const BlockError e = decode_block(in, raw, consumed, lookup, offer_dict ? dict.get() : nullptr, &info); if (e != BlockError::ok) return 0; if (consumed > in.size() || raw.size() != info.raw_bytes || raw.size() > kMaxBlockRaw) __builtin_trap(); std::string again; encode_block(again, raw, 1, info.dictionary_id != 0 ? dict.get() : nullptr); std::string back; std::size_t c2 = 0; if (decode_block(again, back, c2, lookup, dict.get()) != BlockError::ok || back != raw) __builtin_trap(); // Also walk the decoded records (as a reader would). RecordReader records(raw); RecordView rec; while (records.next(rec)) { } return 0; }