Repository navigation
inffast: reject distances past window size in inflateBack() - #1428
Open
professor-moody wants to merge 1 commit into
Open
professor-moody wants to merge 1 commit into
professor-moody wants to merge 1 commit into
Conversation
When inflateBack() calls inflate_fast(), the output buffer is the
caller's circular window of size wsize, so beg equals window and
out - beg is the number of bytes produced in the current window cycle.
Once ROOM() flushes the window the first time, state->whave is set to
state->wsize.
In the byte-wise loop of inflate_fast(), the distance check only
rejected op = dist - (out - beg) > whave. After the first flush, that
accepted any distance up to (out - beg) + wsize instead of capping
distances at wsize, causing inflateBack() with windowBits < 15 to
silently copy aliased bytes from the circular window and return
Z_STREAM_END, whereas the slow path in infback.c rejects the same
stream with Z_DATA_ERROR ("invalid distance too far back").
Compute back unconditionally (for all Z_INFLATE_FETCH modes, including
8) and reject dist > wsize in the byte-wise window check when back is
set. Keep standard inflate() behavior unchanged when back is zero, and
add regression tests in test/infcover.c covering valid distances up to
dist == wsize, standard inflate(-9) with a separate output buffer,
rejection of distances past wsize on both fast and slow paths, and
z_stream reuse across flushes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fix a fast-path distance validation bug in
inflate_fast()(inffast.c) when called frominflateBack()(infback.c) withwindowBits < 15after the first window flush.Root cause
When
inflateBack()callsinflate_fast(strm, state->wsize)(infback.c:424), the output buffer is the caller's circular window of sizewsize(1U << windowBits), sobegequalswindowandout - begis the number of bytes produced in the current window cycle. Once the ROOM macro in infback.c (lines 151-162) flushes the window the first time, it setsstate->whavetostate->wsize.At that point, only
wsizebytes of history exist in the circular window:window[0 .. produced - 1]from the current cycle andwindow[produced .. wsize - 1]surviving from the previous cycle. However, the byte-wise loop ininffast.c(inffast.c:684-687) only checked:op = dist - (out - beg)if (op > whave)Because
whave == wsizeafter the first flush, that predicate accepted any match withwsize < dist <= produced + wsize, computingfrom = window + (wsize - op)inside the current cycle and silently returningZ_STREAM_ENDwith aliased output bytes. Meanwhile, the slow path in infback.c (lines 514-519) enforces the window-size bound directly and rejects the same stream withZ_DATA_ERROR("invalid distance too far back"), soinflateBack()produced different outcomes depending only on input chunking.Changes
inffast.c:#if Z_INFLATE_FETCH != 8guards aroundint back;(inffast.c:229) andback = window != Z_NULL && out + strm->avail_out == window + wsize;(inffast.c:269) soinflateBack()detection is available across allZ_INFLATE_FETCHconfigurations (64, 32, and 8).inffast.c:683) toif (op > whave || (back && dist > wsize)), matching the slow-path bound in infback.c while preserving standardinflate()behavior (back == 0) where the output buffer is separate fromstate->window.test/infcover.c:test_inflateback_small_window_valid_controlverifying thatinflateBack()decodes valid cross-cycle matches up to and includingdist == wsize(windowBits = 9at distances 400, 511, 512;windowBits = 10at distance 1024) identically on both all-at-once (fast path) and 1-byte chunked (slow path) input, thatinflateBack()withwindowBits = 15decodes thedist = 513stream cleanly, and that standardinflate()withwindowBits = -9and a separate output buffer continues to decodedist = 513through the byte-wise loop whenop <= whave.test_inflateback_small_window_rejects_too_far_backverifying thatinflateBack()rejects distances pastwsize(windowBits = 9at distances 513, 585, 600;windowBits = 10at distance 1025) withZ_DATA_ERRORand"invalid distance too far back"on both all-at-once and 1-byte chunked input, and verifyingwhavereset onz_streamreuse after a flush.Testing
inffast.cfix,test_inflateback_small_window_rejects_too_far_backfails ondist = 513(windowBits = 9) becauseinflateBack()returnsZ_STREAM_ENDwith corrupted output instead ofZ_DATA_ERROR(17/18 CTest tests pass, zlib_coverage fails)../configure --cover && make test test64 coverpasses (zlib test OK, zlib shared test OK, zlib 64-bit test OK).inffast.ccompiles cleanly under strict C89 pedantic warnings-as-errors (-std=c89 -pedantic -Wall -Wextra -Werror) with both Clang and GCC across-DZ_INFLATE_FETCH=8,-DZ_INFLATE_FETCH=32,-DZ_INFLATE_FETCH=64 -DZ_INFLATE_BYTES,-DINFLATE_STRICT, and-DZLIB_CONST, and raninfcoverin each configuration.back(inffast.c:269) andif (op > whave || (back && dist > wsize))(inffast.c:683) execute both true and false outcomes.By submitting this pull request, I certify that I agree to the Developer Certificate of Origin (DCO) v1.1, have the right to submit this code under the zlib license, and have personally reviewed and verified all submitted lines in accordance with CONTRIBUTING.md.
AI assistance was used in drafting and validating this change; the patch and tests were reviewed and verified by the author.