Repository navigation
seekable: reject seek tables exceeding ZSTD_SEEKABLE_MAXFRAMES - #4685
evilgensec wants to merge 1 commit into
Conversation
ZSTD_seekable_loadSeekTable() read Number_Of_Frames from the seek table footer and used it to size the entry array and the table-size arithmetic without bounding it by ZSTD_SEEKABLE_MAXFRAMES, the limit the writer enforces in ZSTD_seekable_logFrame(). A crafted seek table could set Number_Of_Frames close to 2^32. On platforms with a 32-bit size_t this overflows the malloc(sizeof(seekEntry_t) * (numFrames + 1)) size computation, under-allocating the array while the parse loop still writes numFrames entries, a heap out-of-bounds write. 64-bit platforms are unaffected: the oversized allocation does not overflow and fails cleanly. Reject the value before these computations, matching the limit applied on write.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds defensive validation when loading a seek table to prevent oversized numFrames values from causing unsafe allocations or arithmetic issues during decompression.
Changes:
- Introduces an early rejection when
numFramesexceedsZSTD_SEEKABLE_MAXFRAMES. - Refactors
tableSize/frameSizecomputation into separate variables after validation.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Closing this. I'll route the change through the project's preferred reporting process instead. Apologies for the noise. |
|
Reopening to keep the fix available for review. |
|
Bump, and thanks for your patience. This rejects a seek table whose frame count exceeds |
|
Follow-up ping. This rejects seekable seek tables exceeding ZSTD_SEEKABLE_MAXFRAMES, closing an oversized-allocation path from attacker-controlled frame counts. Happy to adjust per review. |
What
ZSTD_seekable_loadSeekTable()readsNumber_Of_Framesfrom the seek table footer and uses it, unbounded, to size:tableSize = sizePerEntry * numFrames(U32), andmalloc(sizeof(seekEntry_t) * (numFrames + 1)).It never checks the value against
ZSTD_SEEKABLE_MAXFRAMES, the limit the writer enforces inZSTD_seekable_logFrame()(zstdseek_compress.c). The reader therefore accepts a frame count the writer would never emit.Impact
A crafted seek table can set
Number_Of_Framesclose to2^32.size_tis 32-bit,sizeof(seekEntry_t) * (numFrames + 1)overflows. The entry array is under-allocated, but the cumulative-position loop still writesnumFramesentries — a heap out-of-bounds write.size_t, so it does not overflow; the allocation simply becomes huge and fails cleanly, so there is no out-of-bounds write. TheU32tableSize/frameSizearithmetic still wraps on these platforms.The footer field is part of the trailer of an otherwise valid seekable archive, so a reader handed an attacker-influenced file reaches this path.
Fix
Reject
Number_Of_Frames > ZSTD_SEEKABLE_MAXFRAMESbefore the size computations, mirroring the bound applied on write. With the limit in place neithersizePerEntry * numFrames(≤12 * 0x8000000) norsizeof(seekEntry_t) * (numFrames + 1)can overflow a 32-bitsize_t, and an over-large value is rejected withcorruption_detected.Testing
contrib/seekable_formatcompiles clean under-Wall -Wextra -Werror.make -C contrib/seekable_format/tests testpasses (round-trip, the two no-hang cases, empty-string magic, multi-decompress) — valid archives are unaffected, since the bound (0x8000000) is far above any frame count a writer produces.