Skip to content

inffast: reject distances past window size in inflateBack() - #1428

Open
professor-moody wants to merge 1 commit into
madler:developfrom
professor-moody:fix-inflateback-small-window-distance-check
Open

professor-moody wants to merge 1 commit into
madler:developfrom
professor-moody:fix-inflateback-small-window-distance-check

Conversation

@professor-moody

Copy link
Copy Markdown

Description

Fix a fast-path distance validation bug in inflate_fast() (inffast.c) when called from inflateBack() (infback.c) with windowBits < 15 after the first window flush.

Root cause

When inflateBack() calls inflate_fast(strm, state->wsize) (infback.c:424), the output buffer is the caller's circular window of size wsize (1U << windowBits), so beg equals window and out - beg is 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 sets state->whave to state->wsize.

At that point, only wsize bytes of history exist in the circular window: window[0 .. produced - 1] from the current cycle and window[produced .. wsize - 1] surviving from the previous cycle. However, the byte-wise loop in inffast.c (inffast.c:684-687) only checked:

  • op = dist - (out - beg)
  • if (op > whave)

Because whave == wsize after the first flush, that predicate accepted any match with wsize < dist <= produced + wsize, computing from = window + (wsize - op) inside the current cycle and silently returning Z_STREAM_END with aliased output bytes. Meanwhile, the slow path in infback.c (lines 514-519) enforces the window-size bound directly and rejects the same stream with Z_DATA_ERROR ("invalid distance too far back"), so inflateBack() produced different outcomes depending only on input chunking.

Changes

  • inffast.c:
    • Remove the #if Z_INFLATE_FETCH != 8 guards around int back; (inffast.c:229) and back = window != Z_NULL && out + strm->avail_out == window + wsize; (inffast.c:269) so inflateBack() detection is available across all Z_INFLATE_FETCH configurations (64, 32, and 8).
    • Tighten the byte-wise window distance guard (inffast.c:683) to if (op > whave || (back && dist > wsize)), matching the slow-path bound in infback.c while preserving standard inflate() behavior (back == 0) where the output buffer is separate from state->window.
  • test/infcover.c:
    • Add test_inflateback_small_window_valid_control verifying that inflateBack() decodes valid cross-cycle matches up to and including dist == wsize (windowBits = 9 at distances 400, 511, 512; windowBits = 10 at distance 1024) identically on both all-at-once (fast path) and 1-byte chunked (slow path) input, that inflateBack() with windowBits = 15 decodes the dist = 513 stream cleanly, and that standard inflate() with windowBits = -9 and a separate output buffer continues to decode dist = 513 through the byte-wise loop when op <= whave.
    • Add test_inflateback_small_window_rejects_too_far_back verifying that inflateBack() rejects distances past wsize (windowBits = 9 at distances 513, 585, 600; windowBits = 10 at distance 1025) with Z_DATA_ERROR and "invalid distance too far back" on both all-at-once and 1-byte chunked input, and verifying whave reset on z_stream reuse after a flush.

Testing

  • Verified with Clang 22.1.8 (ASan+UBSan and Release -O2 -DNDEBUG) and GCC 16.2.1 (ASan+UBSan) on Linux x86_64:
    • Before the inffast.c fix, test_inflateback_small_window_rejects_too_far_back fails on dist = 513 (windowBits = 9) because inflateBack() returns Z_STREAM_END with corrupted output instead of Z_DATA_ERROR (17/18 CTest tests pass, zlib_coverage fails).
    • With the fix, all 18/18 CTest targets pass across all three build configurations, and ./configure --cover && make test test64 cover passes (zlib test OK, zlib shared test OK, zlib 64-bit test OK).
  • Verified inffast.c compiles 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 ran infcover in each configuration.
  • Verified with Clang llvm-cov that all branches of back (inffast.c:269) and if (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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant