Skip to content

fix: guard zlib obuf overflow on matched-block inserts (upstream #951) - #5535

Closed
oferchen wants to merge 2 commits into
masterfrom
fix/uts-18-zlib-obuf-guard
Closed

oferchen wants to merge 2 commits into
masterfrom
fix/uts-18-zlib-obuf-guard

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Drains the entire 64 KiB matched-block insert through the deflate stream in ZlibTokenEncoder::see_token, instead of issuing a single compress() call that could drop the unconsumed tail when the output buffer fills under pending sync-flush pressure.
  • Sizes the dictionary-sync output buffer for the worst-case combined output (AVAIL_OUT_SIZE(0xFFFF) + 16) so a maximum-size stored block plus the Z_SYNC_FLUSH trailer always fits in a single pass.
  • Hard-errors if the compressor ever stalls with the worst-case obuf reserve already in place, replacing a silent dictionary-desync with a loud failure.

Rationale

Mirrors upstream rsync issue #951 against token.c:send_deflated_token(). The upstream obuf is sized for AVAIL_OUT_SIZE(CHUNK_SIZE) = 32 816 bytes, but the matched-block insert path can deflate up to 0xFFFF bytes per call while still carrying pending output from a preceding Z_SYNC_FLUSH. The corresponding flate2-based path inherited the same under-sized reserve and additionally never looped on partial consumption, so the encoder's dictionary silently diverged from the decoder's whenever a basis match landed at the obuf boundary.

The protocol < 31 data-duplicating bug is preserved so wire behaviour stays bit-compatible with legacy peers.

Test plan

  • CI fmt + clippy.
  • CI nextest (stable) - covers the new regression tests see_token_drains_64kib_insert_with_pending_flush_output and see_token_handles_exact_max_stored_block.
  • CI Windows, macOS, Linux musl stable jobs.
  • CI interop validation.

The CPRES_ZLIB dictionary-sync path can drop the tail of a matched-block
insert when the deflate output buffer fills mid-chunk. `compress()` only
consumes input until its output buffer is exhausted, but `see_token`
called it once per 64 KiB chunk and discarded the unconsumed tail. With
pending output already buffered from a preceding `Z_SYNC_FLUSH`, the
combined output for a worst-case 0xFFFF byte insert exceeds the prior
65 536 byte reserve, silently corrupting the receiver's dictionary.

- Size the output buffer for `AVAIL_OUT_SIZE(0xFFFF) + 16` so the
  worst-case stored-block plus sync trailer fits in a single pass.
- Loop the `compress()` call until the input chunk is drained; surface
  a hard error if the compressor ever stalls with worst-case obuf
  capacity already in place.
- Preserve the protocol < 31 data-duplicating bug so wire behaviour
  matches upstream for legacy peers.

Mirrors upstream rsync issue #951 against token.c:send_deflated_token().
@github-actions github-actions Bot added the bug Something isn't working label Jun 10, 2026
@oferchen

Copy link
Copy Markdown
Owner Author

Superseded by #5520 (UTS Stream-C: "compress-zlib-insert (UTS-18): ZlibTokenEncoder::see_token() called compress(..., Z_SYNC_FLUSH) exactly once per 0xFFFF chunk"). PR #5520 (merged 2026-06-10) already addresses the same upstream rsync#951 root cause in crates/protocol/src/wire/compressed_token/zlib_codec.rs by draining the Sync flush in a loop until input is consumed and the output buffer is partially filled. The fix on master is functionally equivalent to this PR's drain loop; reimplementing the obuf reserve constants over a merged fix would be a competing rewrite rather than a surgical change. Closing as superseded.

@oferchen oferchen closed this Jun 10, 2026
@oferchen
oferchen deleted the fix/uts-18-zlib-obuf-guard branch June 10, 2026 14:47
oferchen added a commit that referenced this pull request Jun 10, 2026
Records the wire-byte and runtime invariants that govern the delta-transfer
codec stack: the upstream-compatible zlib token decoder, the BufferedMap
basis-window mapper, and the streaming raw-deflate primitives in the
compress crate. Closes the documentation gap left open by the UTS-18 series
(BufferedMap fail-loud bounds, window_len clamp, never-shrink) and PR #5535
(URV-1 zlib obuf overflow on matched-block inserts).

- New design doc at docs/design/zlib-codec-invariants.md catalogues wire
  invariants (INSERT / COPY / END token contracts), runtime invariants
  per code path, the failure-mode -> regression-test table, and fuzz
  target rationale.
- New complementary fuzz target fuzz/fuzz_targets/zlib_token_decode.rs
  drives CountingZlibDecoder through arbitrary byte sequences under
  streaming read semantics. Asserts panic-freedom, monotonic byte-counter,
  and 100x expansion ceiling. Uses only existing crate APIs.
- Two new unit tests in crates/compress/src/zlib/tests.rs pin the
  malformed-INSERT and COPY-past-EOF panic-freedom guarantees at the
  streaming decoder layer (malformed stored-block NLEN check, post-EOF
  Ok(0) tail reads).
- Wires zlib_token_decode into the nightly fuzz-coverage matrix workflow
  and the FCV-20.c coverage tracking table.
oferchen added a commit that referenced this pull request Jun 10, 2026
Records the wire-byte and runtime invariants that govern the delta-transfer
codec stack: the upstream-compatible zlib token decoder, the BufferedMap
basis-window mapper, and the streaming raw-deflate primitives in the
compress crate. Closes the documentation gap left open by the UTS-18 series
(BufferedMap fail-loud bounds, window_len clamp, never-shrink) and PR #5535
(URV-1 zlib obuf overflow on matched-block inserts).

- New design doc at docs/design/zlib-codec-invariants.md catalogues wire
  invariants (INSERT / COPY / END token contracts), runtime invariants
  per code path, the failure-mode -> regression-test table, and fuzz
  target rationale.
- New complementary fuzz target fuzz/fuzz_targets/zlib_token_decode.rs
  drives CountingZlibDecoder through arbitrary byte sequences under
  streaming read semantics. Asserts panic-freedom, monotonic byte-counter,
  and 100x expansion ceiling. Uses only existing crate APIs.
- Two new unit tests in crates/compress/src/zlib/tests.rs pin the
  malformed-INSERT and COPY-past-EOF panic-freedom guarantees at the
  streaming decoder layer (malformed stored-block NLEN check, post-EOF
  Ok(0) tail reads).
- Wires zlib_token_decode into the nightly fuzz-coverage matrix workflow
  and the FCV-20.c coverage tracking table.
oferchen added a commit that referenced this pull request Aug 24, 2026
Records the wire-byte and runtime invariants that govern the delta-transfer
codec stack: the upstream-compatible zlib token decoder, the BufferedMap
basis-window mapper, and the streaming raw-deflate primitives in the
compress crate. Closes the documentation gap left open by the UTS-18 series
(BufferedMap fail-loud bounds, window_len clamp, never-shrink) and PR #5535
(URV-1 zlib obuf overflow on matched-block inserts).

- New design doc at docs/design/zlib-codec-invariants.md catalogues wire
  invariants (INSERT / COPY / END token contracts), runtime invariants
  per code path, the failure-mode -> regression-test table, and fuzz
  target rationale.
- New complementary fuzz target fuzz/fuzz_targets/zlib_token_decode.rs
  drives CountingZlibDecoder through arbitrary byte sequences under
  streaming read semantics. Asserts panic-freedom, monotonic byte-counter,
  and 100x expansion ceiling. Uses only existing crate APIs.
- Two new unit tests in crates/compress/src/zlib/tests.rs pin the
  malformed-INSERT and COPY-past-EOF panic-freedom guarantees at the
  streaming decoder layer (malformed stored-block NLEN check, post-EOF
  Ok(0) tail reads).
- Wires zlib_token_decode into the nightly fuzz-coverage matrix workflow
  and the FCV-20.c coverage tracking table.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant