Skip to content

fix(sender): reject stray negative NDX instead of skipping it - #7365

Merged
oferchen merged 1 commit into
masterfrom
fix/sender-ndx-protocol-violation
Aug 15, 2026
Merged

oferchen merged 1 commit into
masterfrom
fix/sender-ndx-protocol-violation

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Upstream

read_ndx_and_attrs's read_loop (rsync.c:329-381) handles negative indices in a fixed order:

if (ndx == NDX_DONE) return ndx;                       /* :335 */
if (ndx == NDX_DEL_STATS) { read_del_stats(f_in); ... } /* :337 */
if (!inc_recurse || am_sender) {                        /* :343  <-- the gate */
        rprintf(FERROR, "Invalid file index: %d (%d - %d) [%s]\n", ...);
        exit_cleanup(RERR_PROTOCOL);
}
if (ndx == NDX_FLIST_EOF) { ... }                       /* :354 */
/* sub-list branch */                                   /* :360 */

The gate is above the NDX_FLIST_EOF and sub-list branches. For a sender (am_sender) it is unconditional — those are not markers to skip, they are protocol violations. Only a receiver inside the INC_RECURSE window may grow its file list from the stream.

The defect

run_transfer_loop is oc's send_files analogue (its own comments cite sender.c:256-261/:267-272, and upstream reads its NDX via the same read_ndx_and_attrs at sender.c:236). It open-coded the classifier and failed open on three arms:

oc arm oc did upstream does
NDX_FLIST_EOF debug_log! + continue exit 2
_ if ndx <= NDX_FLIST_OFFSET debug_log! + continue exit 2
_ (unknown negative) debug_log! + continue exit 2

A peer could desync the send loop with no diagnostic and no exit code.

Change

The three arms collapse into one, because upstream rejects them at one site. The error comes from ndx_stream::invalid_file_index, which already renders upstream's exact wording and is tagged as a protocol violation → RERR_PROTOCOL (2), so the message keeps a single owner rather than gaining a second copy. last mirrors GoodbyeNdxSink::last_file_ndx (rsync.c:345-348).

NDX_DEL_STATS deliberately keeps its own arm — upstream drains it at :337, above the gate — as does NDX_DONE, which carries oc's phase-transition logic.

This is not a behaviour-neutral refactor: it converts three silent-continue arms into hard exits. That is the point.

Why it survived

The full suite passed before and after the fix — nothing exercised these arms. That is the signature of a silent fail-open, and it is why the tests below assert the new behaviour directly rather than relying on the suite.

Verification

  • 3 rejection tests, one per arm, asserting upstream's message text.
  • RED proven: restoring the fail-open continue ahead of the rejection makes all three fail.
  • Non-vacuity companion sender_still_accepts_del_stats_then_done: feeds a real NDX_DEL_STATS frame plus its five varints and asserts the loop still completes. It passes both before and after — it exists to catch a fix that turns a legitimate frame fatal.
  • cargo nextest run -p transfer --all-features: 2753 run, 2753 passed
  • cargo fmt --all -- --check: clean
  • cargo clippy --locked --workspace --all-targets --all-features --no-deps -- -D warnings: clean

Context

This is the fourth and last open copy of the NDX classifier. The other three (read_next_frame, read_ndx_skipping_del_stats, the goodbye loop) were already collapsed onto the shared ndx_stream primitive by #7228 — this one was missed, and it is the only one that ever had a contract divergence rather than being a pure duplicate.

upstream: rsync.c:343-353. Inside `read_ndx_and_attrs`'s read_loop the gate
`if (!inc_recurse || am_sender)` sits ABOVE both the NDX_FLIST_EOF branch
(:354) and the sub-list branch (:360), so for a sender every remaining
negative index is rejected with

    Invalid file index: %d (%d - %d) [%s]

followed by exit_cleanup(RERR_PROTOCOL). Only a receiver inside the
INC_RECURSE window may grow its file list from the stream.

oc's send loop open-coded the classifier and skipped three of those cases:
NDX_FLIST_EOF, any `ndx <= NDX_FLIST_OFFSET`, and any other negative value
each logged at debug level and continued. A peer could desync the loop with
no diagnostic and no exit code. The three collapse into one arm because
upstream rejects them at one site.

The rejection reuses `ndx_stream::invalid_file_index`, which already renders
upstream's wording and is tagged as a protocol violation, so the message has
a single owner. NDX_DEL_STATS keeps its own arm: upstream drains it at :337,
above the gate.

The whole suite passed before and after, which is how the divergence
survived - nothing exercised it. The three new tests fail when the
fail-open `continue` is restored; the fourth is the non-vacuity companion
and passes either way, guarding against the fix turning a legitimate
NDX_DEL_STATS frame into a fatal.

transfer: 2753 run, 2753 passed.
@github-actions github-actions Bot added the bug Something isn't working label Aug 15, 2026
@oferchen
oferchen merged commit 3c13430 into master Aug 15, 2026
68 checks passed
@oferchen
oferchen deleted the fix/sender-ndx-protocol-violation branch August 15, 2026 21:59
oferchen added a commit that referenced this pull request Aug 24, 2026
upstream: rsync.c:343-353. Inside `read_ndx_and_attrs`'s read_loop the gate
`if (!inc_recurse || am_sender)` sits ABOVE both the NDX_FLIST_EOF branch
(:354) and the sub-list branch (:360), so for a sender every remaining
negative index is rejected with

    Invalid file index: %d (%d - %d) [%s]

followed by exit_cleanup(RERR_PROTOCOL). Only a receiver inside the
INC_RECURSE window may grow its file list from the stream.

oc's send loop open-coded the classifier and skipped three of those cases:
NDX_FLIST_EOF, any `ndx <= NDX_FLIST_OFFSET`, and any other negative value
each logged at debug level and continued. A peer could desync the loop with
no diagnostic and no exit code. The three collapse into one arm because
upstream rejects them at one site.

The rejection reuses `ndx_stream::invalid_file_index`, which already renders
upstream's wording and is tagged as a protocol violation, so the message has
a single owner. NDX_DEL_STATS keeps its own arm: upstream drains it at :337,
above the gate.

The whole suite passed before and after, which is how the divergence
survived - nothing exercised it. The three new tests fail when the
fail-open `continue` is restored; the fourth is the non-vacuity companion
and passes either way, guarding against the fix turning a legitimate
NDX_DEL_STATS frame into a fatal.

transfer: 2753 run, 2753 passed.
oferchen added a commit that referenced this pull request Sep 8, 2026
… per rsync.c:436-444 (#7744)

upstream: rsync.c:436-444 - inside read_ndx_and_attrs(), once the attribute
tail has decoded ITEM_TRANSFER, `i = ndx - cur_flist->ndx_start; if (i < 0 ||
!S_ISREG(cur_flist->files[i]->mode))` prints

    received request to transfer non-regular file: %d [%s]

and aborts with exit_cleanup(RERR_PROTOCOL). The guard runs on BOTH ends that
read a request/echo. oc had no equivalent on either read path: the sender's
request loop fell through to a silent `!file_entry.is_file()` skip (no
diagnostic, no exit code), and the receiver's echo read serviced the frame.

Placement: the receiver's half lives in the shared primitive
(ndx_stream::read_ndx_and_attrs) behind a new non-defaulted
FlistMarkerSink::ndx_is_regular_file, so every consumer of the primitive is
covered by one owner; the sender's request read is open-coded in
transfer_loop.rs and applies the same predicate at its attrs read, sharing
ndx_stream::non_regular_transfer_request so the diagnostic has one owner.
No third NDX->entry resolver is introduced: the sender resolves through
NdxMap (resolve_itemize_ndx), the receiver through wire_to_flat_ndx.

What existing gates already covered, and what is new:
- #7365 refuses every stray NEGATIVE wire NDX; upstream rejects those at
  rsync.c:344-354, before this guard, so there is no overlap - a negative
  NDX never reaches the check on either side.
- #7630 refuses a modern NDX that overflows a signed file index at the
  codec, also before the guard.
- upstream's `i < 0` residual (a non-negative NDX below the current list's
  ndx_start, i.e. the INC_RECURSE gap NDX) is refused by the S_ISREG half
  here: resolve_itemize_ndx maps the gap to the owning *directory* entry, so
  an ITEM_TRANSFER gap frame fails the regular-file test exactly where
  upstream fails i < 0. An index past the end of the list stays with its
  existing owners (validate_file_index on the sender, the echo-ordering
  check on the receiver, mirroring upstream's earlier flist_for_ndx abort),
  so this guard never claims a range fault with the non-regular diagnostic.
- The NEW substance is the S_ISREG check itself, on both read sites.

Ordering consequence, pinned by tests: an ITEM_TRANSFER frame naming a
CLEARED slot is now refused by this guard (a cleared entry's zeroed mode is
not S_ISREG), exactly as upstream, where rsync.c:436-444 runs before
recv_files()'s F_IS_ACTIVE test (receiver.c:871-881). The cleared-entry
diagnostic keeps owning non-transfer frames.

The module doc's ":420-428" citation had drifted against the pinned 3.5.0
tree and claimed the guard "stays with their current owners"; both
corrected. The requesting-side polite skip in the generator is untouched.

Mutation proof: disabling the primitive guard fails exactly
item_transfer_frame_for_non_regular_entry_is_refused_with_upstream_wording,
non_regular_transfer_echo_is_refused_with_upstream_wording and
cleared_slot_under_item_transfer_reports_the_non_regular_guard (16 sibling
tests stay green); disabling the sender-site guard fails exactly
sender_refuses_transfer_request_for_a_symlink_entry and
sender_refuses_transfer_request_for_a_directory_entry (17 stay green).
Both mutations reverted.

transfer: 2494 run, 2494 passed.
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