Skip to content

fix(protocol): guard read_varint against integer overflow on malformed input - #5874

Merged
oferchen merged 1 commit into
masterfrom
fix/pull-varint-overflow-guard
Jun 17, 2026
Merged

oferchen merged 1 commit into
masterfrom
fix/pull-varint-overflow-guard

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Add explicit min_bytes validation to read_varlong so a fuzzer-found or caller-induced min_bytes == 0 (or min_bytes > 8) surfaces as InvalidData instead of panicking on usize subtract underflow at min - 1 or slice OOB at initial[..min]. The encodable range is 1..=8: upstream's u.b union is 9 bytes wide and at least one byte is the leading tag.
  • Add regression tests for min_bytes == 0, min_bytes > 8, and the full 0xFC..=0xFF overflow-tag range against read_varint (each tag in that range maps to extra=5..6 via INT_BYTE_EXTRA, all exceeding MAX_EXTRA_BYTES=4).
  • No wire-protocol behavior change for legitimate inputs. Existing read_varint decoder already errors gracefully on overflow tags; this PR makes the sibling read_varlong decoder equally panic-proof against malformed caller input.

Root cause

The slice-OOB panic in read_varlong was the highest-likelihood vector for the read_varint-family overflow reported under the daemon-pull failure (PULL-VARINT / #4345 / UTS-V4.C). Crosses with the filter-fuzzer crashes (#4350 CI-MASTER-FUZZ) which exercise the same call chain via flist decode.

Upstream reference: target/interop/upstream-src/rsync-3.4.1/io.c:1827 read_varlong() implicitly requires min_bytes >= 1 (it reads min_bytes including the leading tag and indexes b2[min_bytes-1] plus u.b[min_bytes + extra - 1]). Upstream guard at io.c:1846 is if (min_bytes + extra > (int)sizeof u.b) where sizeof u.b == 9; we already mirror this for the upper-side check.

Files touched

  • crates/protocol/src/varint/decode.rs — min_bytes range guard in read_varlong, rustdoc updated.
  • crates/protocol/src/varint/tests.rs — three new regression tests.

Test plan

  • cargo fmt --all --check
  • cargo build -p protocol --lib
  • CI: nextest (stable) on Linux/Windows/macOS, fmt+clippy, fuzz coverage

@oferchen
oferchen force-pushed the fix/pull-varint-overflow-guard branch from 3ce03de to 1baf804 Compare June 17, 2026 15:10
@github-actions github-actions Bot added the bug Something isn't working label Jun 17, 2026
…d input

Add explicit min_bytes validation to read_varlong so a fuzzer-found or
caller-induced min_bytes==0 or min_bytes>8 surfaces as InvalidData instead
of panicking on usize subtract underflow at `min - 1` or slice OOB at
`initial[..min]`. The valid encodable range is 1..=8: upstream's `u.b`
union is 9 bytes wide and at least one byte is the leading tag.

upstream: io.c:1827 read_varlong() implicitly requires min_bytes >= 1
(it reads min_bytes including the leading tag and indexes b2[min_bytes-1]
plus u.b[min_bytes + extra - 1]). Bound is upstream's
`if (min_bytes + extra > (int)sizeof u.b)`.

Add regression tests covering:
- read_varlong with min_bytes==0 (underflow panic before guard)
- read_varlong with min_bytes>8 (slice OOB before guard)
- read_varint across the full 0xFC..=0xFF tag range (extra=5,5,6,6),
  each of which must surface InvalidData (matches upstream io.c:1809
  "Overflow in read_varint()")

No wire-protocol behavior change for legitimate inputs. Crosses with
PULL-VARINT (#4345) and CI-MASTER-FUZZ filter-fuzzer crashes.
@oferchen
oferchen force-pushed the fix/pull-varint-overflow-guard branch from 1baf804 to 3151947 Compare June 17, 2026 15:20
@oferchen
oferchen merged commit 7604027 into master Jun 17, 2026
1 check passed
@oferchen
oferchen deleted the fix/pull-varint-overflow-guard branch June 17, 2026 15:29
oferchen added a commit that referenced this pull request Aug 24, 2026
…d input (#5874)

Add explicit min_bytes validation to read_varlong so a fuzzer-found or
caller-induced min_bytes==0 or min_bytes>8 surfaces as InvalidData instead
of panicking on usize subtract underflow at `min - 1` or slice OOB at
`initial[..min]`. The valid encodable range is 1..=8: upstream's `u.b`
union is 9 bytes wide and at least one byte is the leading tag.

upstream: io.c:1827 read_varlong() implicitly requires min_bytes >= 1
(it reads min_bytes including the leading tag and indexes b2[min_bytes-1]
plus u.b[min_bytes + extra - 1]). Bound is upstream's
`if (min_bytes + extra > (int)sizeof u.b)`.

Add regression tests covering:
- read_varlong with min_bytes==0 (underflow panic before guard)
- read_varlong with min_bytes>8 (slice OOB before guard)
- read_varint across the full 0xFC..=0xFF tag range (extra=5,5,6,6),
  each of which must surface InvalidData (matches upstream io.c:1809
  "Overflow in read_varint()")

No wire-protocol behavior change for legitimate inputs. Crosses with
PULL-VARINT (#4345) and CI-MASTER-FUZZ filter-fuzzer crashes.
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