Skip to content

fix(transfer): turn buffered map_file out-of-range panic into Err - #5566

Merged
oferchen merged 3 commits into
masterfrom
fix/uts-18-buffered-bounds-fail-loud
Jun 11, 2026
Merged

oferchen merged 3 commits into
masterfrom
fix/uts-18-buffered-bounds-fail-loud

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Panic site identified. crates/transfer/src/map_file/buffered.rs:144 (self.buffer.copy_within(src_offset..src_offset + reuse_len, 0)) aborts the process with range end index 262144 out of range for slice of length 48128 when a malformed zlib delta drives the overlap branch of load_window against a resized buffer that cannot hold the prior window's claimed extent. A sibling site at map_ptr (&self.buffer[start..end]) carries the same panic surface.
  • Caller chain documented. transfer_ops/token_loop.rs:179 (and twin sites in transfer_ops/response.rs:254, receiver/transfer/sync.rs:572, delta_apply/applicator.rs:358) pass (offset, bytes_to_copy) derived from a token's block_idx * block_length. A malicious or corrupted basis stream can drive block_idx into a range where the cached window's claimed extent no longer matches the resized buffer.
  • Fail-loud guard applied. Both slice sites in BufferedMap now validate the range before indexing and return io::ErrorKind::InvalidData with the exact out-of-range pair and buffer length. copy_within is preceded by a checked_add + length check; map_ptr's final slice uses slice::get(..). The errors propagate up through map_ptr -> token_reader.see_token -> the transfer loop and map to upstream rsync's basis-corruption exit-code path.
  • Root-cause follow-up filed. Whether the upstream sender's request should be rejected (preferred, matches upstream basis-corruption semantics) or the buffered window should be extended to honour the request is left to UTS-18.g. This PR closes the panic surface so the test fails with a clean Err instead of SIGABRT + core dump.

Test plan

  • New unit test map_ptr_slice_out_of_range_returns_err_not_panic fabricates a cached window state with window_len > buffer.len() and asserts map_ptr(128, 64) returns InvalidData with 128..192 and 32 in the message (stripped-down ratio of the production crash).
  • New unit test load_window_overlap_shrink_returns_err_not_panic drives the overlap-shrink branch directly with window_start=0, window_len=1500, max_window=1024 so src_offset+reuse_len=1500 walks past the resized 1024-byte buffer; asserts Err, not panic.
  • Out-of-range path: compress-zlib-insert now surfaces a typed InvalidData error and the process exits with the standard transfer error code instead of SIGABRT.
  • cargo fmt --all -- --check (verified by CI fmt+clippy gate).
  • cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings (verified by CI).
  • cargo nextest run -p transfer --all-features covers map_file::tests (verified by CI nextest matrix).

@oferchen
oferchen force-pushed the fix/uts-18-buffered-bounds-fail-loud branch from bb7b78b to 6ce80f5 Compare June 10, 2026 16:09
@github-actions github-actions Bot added the bug Something isn't working label Jun 10, 2026
oferchen added a commit that referenced this pull request Jun 10, 2026
Locks coverage of the UTS-18.f / UTS-18.g panic class in
`transfer::map_file::BufferedMap` closed by PR #5566 (fail-loud
`copy_within` + slice guards) and PR #5569 (`window_len` clamp at the
source). Future regressions in `load_window` overlap-shrink or
`map_ptr` slice bounds surface as nightly fuzz-coverage findings instead
of waiting on a real crash report.

- New libFuzzer target `fuzz/fuzz_targets/buffered_map.rs` drives both
  panic sites through the public `MapStrategy::map_ptr` entry. Six u64
  LE words at the head of each input parametrize `(file_size,
  window_size, request_start/len, second_start/len)`; remaining bytes
  tile the synthetic basis file. Two `map_ptr` calls per input cover
  the empty-cache load path and the forward-overlap branch of
  `load_window`.
- Three deterministic corpus seeds under `fuzz/corpus/buffered_map/`:
  `uts_18_overlap_shrink.bin` mirrors the production crash math
  (file=48128, MAX_MAP_SIZE=262144); `uts_18_truncated.bin` covers the
  48-byte header boundary; `uts_18_zero_len.bin` covers the `len == 0`
  short-circuit.
- `buffered_map` wired into `.github/workflows/fuzz-coverage-report.yml`
  matrix so the nightly run uploads lcov for the new target.
- Audit note at `docs/audits/edg-panic-4-buffered-fuzz.md`.
oferchen added 2 commits June 11, 2026 06:05
A malformed zlib delta stream could request a basis window whose
`reuse_len` (derived from the prior window extent) exceeds the resized
buffer length when the file tails off near EOF, or a slice whose `end`
walks past the cached window's backing buffer. Either case hit a bare
slice/copy_within and aborted the process with `SIGABRT`:

    range end index 262144 out of range for slice of length 48128

`copy_within` and `&buf[..end]` panic on out-of-range; on untrusted
input the receiver should reject the basis with a typed I/O error
instead of taking the whole process down.

Adds a fail-loud guard at both slice sites in `BufferedMap`:

- `load_window` overlap branch: validate `src_offset.checked_add(reuse_len)`
  against the resized buffer length before `copy_within`.
- `map_ptr` final slice: replace `&self.buffer[start..end]` with
  `self.buffer.get(start..end).ok_or_else(...)`.

Both surface `io::ErrorKind::InvalidData` with the exact range and
buffer length, mirroring upstream rsync's behaviour of treating
basis-corruption as a recoverable transfer-level error.

Two regression tests fabricate inconsistent window state and assert
`map_ptr` returns Err rather than panicking - the first targets the
map_ptr slice site, the second drives the load_window overlap-shrink
branch with the same ratio as the production crash.

The root-cause fix - extending the buffered window vs rejecting the
upstream request - is a follow-up. This PR closes the panic surface so
the test fails with a clean Err instead of aborting.

# Conflicts:
#	crates/transfer/src/map_file/buffered.rs
UTS-18.g root-cause clamp: record window_len against file_size minus
window_start so any state-fabrication path cannot leave window_len
claiming more bytes than the file actually has. The clamp guarantees
the UTS-18.f bounds checks never trip on legitimate input where the
underlying file is smaller than MAX_MAP_SIZE.

Adds the UTS-18.g positive regression: a file of 48128 bytes (the
production crash size) with MAX_MAP_SIZE=262144 must complete
successfully via map_ptr, with window_len clamped to 48128 rather
than inheriting the 262144 max_window value.

Also folds in the UTS-18.g.2 cargo fmt collapse of the map_ptr
range-overflow format! call.
@oferchen
oferchen force-pushed the fix/uts-18-buffered-bounds-fail-loud branch from c2e178b to 579072c Compare June 11, 2026 03:08
@oferchen
oferchen merged commit eb05719 into master Jun 11, 2026
2 of 11 checks passed
@oferchen
oferchen deleted the fix/uts-18-buffered-bounds-fail-loud branch June 11, 2026 03:11
oferchen added a commit that referenced this pull request Aug 24, 2026
…loss

PR #5569 review flagged a data-loss path in `BufferedMap::load_window`:
`Vec::resize(window_size, 0)` truncates the buffer when a smaller window
is requested. Under the overlap-shrink branch (new window near EOF after
a full-size load), the bytes the overlap branch is about to relocate via
`copy_within` are dropped before the copy runs, leading to either an
out-of-bounds panic on the source range or silent corruption of the
reused suffix once PR #5566's guard converts the panic to InvalidData.

Upstream `fileio.c:236` `realloc_array` only grows the backing buffer.
Mirror that invariant by replacing `buffer.resize(window_size, 0)` with
`buffer.resize(window_size.max(buffer.len()), 0)`. The buffer becomes
monotonically non-decreasing (capped at MAX_MAP_SIZE peak); `window_len`
continues to bound the valid region so callers never see stale tail
bytes.

Picked the never-shrink fix over file-boundary-only shrink or
shrink-and-reload because it is the smallest delta, matches upstream
exactly, and has negligible memory cost in practice.
oferchen added a commit that referenced this pull request Aug 24, 2026
Locks coverage of the UTS-18.f / UTS-18.g panic class in
`transfer::map_file::BufferedMap` closed by PR #5566 (fail-loud
`copy_within` + slice guards) and PR #5569 (`window_len` clamp at the
source). Future regressions in `load_window` overlap-shrink or
`map_ptr` slice bounds surface as nightly fuzz-coverage findings instead
of waiting on a real crash report.

- New libFuzzer target `fuzz/fuzz_targets/buffered_map.rs` drives both
  panic sites through the public `MapStrategy::map_ptr` entry. Six u64
  LE words at the head of each input parametrize `(file_size,
  window_size, request_start/len, second_start/len)`; remaining bytes
  tile the synthetic basis file. Two `map_ptr` calls per input cover
  the empty-cache load path and the forward-overlap branch of
  `load_window`.
- Three deterministic corpus seeds under `fuzz/corpus/buffered_map/`:
  `uts_18_overlap_shrink.bin` mirrors the production crash math
  (file=48128, MAX_MAP_SIZE=262144); `uts_18_truncated.bin` covers the
  48-byte header boundary; `uts_18_zero_len.bin` covers the `len == 0`
  short-circuit.
- `buffered_map` wired into `.github/workflows/fuzz-coverage-report.yml`
  matrix so the nightly run uploads lcov for the new target.
- Audit note at `docs/audits/edg-panic-4-buffered-fuzz.md`.
oferchen added a commit that referenced this pull request Aug 24, 2026
EDG-PANIC.1: workspace audit of bare slice indexing patterns on
attacker-controlled buffers (protocol/transfer/checksums/compress).
~314 indexing sites reviewed; only the two known UTS-18 sites in
BufferedMap surfaced as suspect, both with a fix in flight via
PR #5566. All other indexing is bounds-checked on the prior line,
backed by fixed-size [u8; N] arrays, min-clamped, or covered by a
length-prefix or library contract. Findings written to
docs/audits/edg-panic-1-bare-slice-indexing.md with follow-up items
enumerated as future EDG-PANIC tasks.

EDG-PANIC.3: pin the BufferedMap basis-slice bounds contract with
five negative-bounds unit tests against map_ptr (the BufferedMap
"slice" accessor):

  - offset past EOF returns Err
  - length overflow (usize::MAX) returns Err
  - u64::MAX offset returns Err
  - offset == file_size with non-zero len returns Err
    (offset == file_size with zero len is a legitimate empty slice)
  - range straddling EOF returns Err

Each test asserts io::ErrorKind::UnexpectedEof so a future change
that drops or weakens the saturating_add guard is caught at the
test boundary. Mirrors the malformed delta stream attack surface
UTS-18 root-caused (range end index N out of range for slice of
length M aborting the process).

Refs: #3891, #3893
oferchen added a commit that referenced this pull request Aug 24, 2026
…l-loud

fix(transfer): turn buffered map_file out-of-range panic into Err
oferchen added a commit that referenced this pull request Aug 24, 2026
UTS-18.j: extend URV-1 attestation with the basis-window panic chain
discovered after the original codec-only audit cleared the file.
Document the three follow-on regressions (window_len overstatement,
buffer.resize overlap-shrink, bare slice index on misaligned chunk
requests) and the remediation PRs (#5566, #5569, #5588, #5590, #5627).

UTS-18.i: keep `compress-zlib-insert` out of the upstream-testsuite
known-failures list so the workflow's wildcard run surfaces any
regression as an UNEXPECTED FAIL. The cell already iterates
`$suitedir/*.test`, so explicit wiring is the explanatory comment in
`tools/ci/upstream_testsuite_known_failures.conf` plus the
attestation-doc cross-reference.
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