Download tools/domain_knowledge/zstd.md from AProver/aprover: direct link, hf CLI and curl.
- Browser
- Download file 5.31 kB
-
https://huggingface.co/spaces/AProver/aprover/resolve/main/tools/domain_knowledge/zstd.md
- Command line
-
hf download hf://spaces/AProver/aprover/tools/domain_knowledge/zstd.md
-
curl -L -o zstd.md https://huggingface.co/spaces/AProver/aprover/resolve/main/tools/domain_knowledge/zstd.md
zstd domain knowledge for bmc-agent
Format
zstd compressed stream:
Frame = Magic(4B) + FrameHeader(2-14B) + Block[]+ + (optional) Content_Checksum(4B)
Block = BlockHeader(3B) + Block_Content(N bytes)
BlockType in {Raw, RLE, Compressed, Reserved}
Compressed_Block = Literals + Sequences
Literals = LiteralsHeader + Huffman_Tree(optional) + Huffman_Stream(1 or 4 streams)
Sequences = SequencesHeader + LL_Code + OF_Code + ML_Code + Bitstream
The decompressor is a bit-stream-driven state machine using FSE (Finite State Entropy) and Huffman tables. Each table has bounded alphabet size and bounded code lengths.
Public API entry points (attacker-reachable)
Untrusted compressed input flows in via:
ZSTD_decompress(dst, dstCapacity, src, srcSize)β one-shotZSTD_decompressDCtx(dctx, ...)β with contextZSTD_decompressStream(zds, output, input)β streamingZSTD_decompressContinue(dctx, dst, ...)β low-level continuationZSTD_decompressBegin(dctx)+ZSTD_decompressContinueβ block-by-block
Dictionary loading is also an attacker vector:
ZSTD_decompress_usingDict(dctx, dst, ..., dict, dictSize)β dictionary from untrusted source
Key type definitions
typedef enum { ZSTDds_getFrameHeaderSize, ... } ZSTD_dStage;
typedef struct ZSTD_DCtx_s ZSTD_DCtx;
typedef struct ZSTD_DStream_s ZSTD_DStream; /* alias for ZSTD_DCtx */
typedef ZSTD_DCtx HUF_DTable; /* Huffman decode table */
typedef U32 FSE_DTable; /* FSE decode table */
size_t return values use ZSTD_isError(ret) to test for error (a magic-range
value above (size_t)-128); otherwise ret is the decompressed size or
bytes-needed.
Historical bug classes (from CVE + OSS-Fuzz history)
Bit-stream decoder OOB reads (parallel to libarchive RAR5 patterns)
BIT_readBits,BIT_lookBits,BIT_reloadDStreamβ read N bits from an in-memory bit-buffer; if N exceeds the buffer, OOB- Same
+N tailstructural-invariant as libarchive'sread_bits_16/32 - Check: is the buffer over-allocated to support max peek-ahead?
FSE/Huffman table corruption (less common but high impact)
- Malformed literal-tree spec β FSE state machine reads past table bounds
HUF_decompress*_usingDTable_internalfamily- Bound:
HUF_TABLELOG_MAX(typically 12) β alphabet β€ 4096
Sequence decoding bugs (offset/match-length encoding)
- LL/OF/ML codes have bounded alphabets (35/31/52)
- Offsets above
window_sizeare spec violations but parser may not check ZSTD_decodeSequence/ZSTD_execSequencefamily
Dictionary loading bugs (CVE-2021-24032 was the headline)
- Malformed dictionary header β uninit values used downstream
ZSTD_decompressBegin_usingDictand friends
Window-size overflow (multiple, depends on input frame header)
- Frame header declares window_size which is upper-bounded; attacker
can request very large values; pre-check is
ZSTD_decompressBound
- Frame header declares window_size which is upper-bounded; attacker
can request very large values; pre-check is
Threat model for realism judgment
A bug is REALISTIC if:
- Reachable from
ZSTD_decompress*with attacker-controlledsrcbytes - The function-under-test is called from
ZSTD_decompressFrame/ZSTD_decompressBlock/ZSTD_decompressSequencesLong
A bug is UNREALISTIC if:
- Requires
ZSTD_DCtx*to be uninitialized βZSTD_createDCtxallocates and initializes (G2 pattern, same as libarchive) - Requires
dctx->bTypeor similar enum field to be outside its valid range β decoded values are always within their spec - Requires bit-stream peek-ahead to land OOB β the input buffer in
ZSTD_decompressContinueis sized by caller'ssrcSize; peek-ahead is bounded byBIT_*API contract (32 bits at a time, never beyondbitContainer + sizeof(BIT_DStream_t))
Bug-density priority
High priority (decompression core):
huf_decompress.cβ Huffman table parsing + decodezstd_decompress_block.cβ block-level decompressionzstd_decompress.cβ frame-level entry pointszstd_ddict.cβ dictionary loading
Lower priority:
fse_decompress.cβ FSE decompression (called by huf_decompress, well-tested)huf_compress.cetc. β compression side (not attacker-reachable from untrusted input)zstd_v01.c-v07.cβ legacy format compatibility (out of standard rotation)
Specific spec hints
- Most decompression functions return
size_twhereZSTD_isError(ret)means "magic error sentinel value." Postconditions should account for both normal and error returns. dctx->litPtranddctx->litEndbound the literal buffer; reads outside this range are bugs.- Bit-reader
BIT_DStream_thas invariants:bitContainerholds up to 64 bitsbitsConsumed <= 64ptr >= startandptr <= limitPtr
- Huffman alphabet size is bounded by
HUF_TABLELOG_MAX = 12β max symbol index< (1 << 12) = 4096.
Known FPs to suppress
dctx->Xfield NULL when called from harness: G2 βZSTD_createDCtx/ZSTD_initDStreaminitializes everything.BIT_DStream_tfields outside invariant: G4 βBIT_initDStreamestablishes the invariant from the caller.- Bit-stream peek N bits when
bitsConsumed + N > 64: G3 β the bit-reader refills viaBIT_reloadDStreambetween use cycles.