Skip to content

fix(batch): close two replay-script injections in the generated .sh - #7445

Merged
oferchen merged 2 commits into
masterfrom
fix/batch-replay-script-injection
Aug 22, 2026
Merged

oferchen merged 2 commits into
masterfrom
fix/batch-replay-script-injection

Conversation

@oferchen

Copy link
Copy Markdown
Owner

What

Two independent ways a crafted name reached the shell when an operator ran the
BATCH.sh a --write-batch run generates. Both reproduce against the rsync
3.5.0 testsuite, with the real 3.5.0 binary passing as the control.

1. write-batch-quoting - the option quoter was upstream 3.4.x

shell_quote implemented upstream's conditional quoting: wrap an argument
only when strpbrk matches a special-character set. Its doc even cited the set,
"'&;|[]()$#!*?^\ - which omits the backtick, <, > and the braces. A
destination named d`>PWNED`x/ was therefore written bare, and the command
substitution executed on replay.

Upstream 3.5.0 rewrote write_arg (batch.c:164-198) to single-quote
unconditionally, and its own comment gives the reason: every metacharacter -
backtick, newline, redirection - must stay literal. Two further rules changed
with it, and all three are ported:

rule 3.4.x 3.5.0 (now)
wrap only if strpbrk hits always
embedded ' '' (close/reopen) '\'' (close/escape/reopen)
bare -opt= prefix whenever an = exists only if every byte from the leading - to the = is [-_0-9A-Za-z]

The third rule matters on its own: without it an attacker-shaped -a`cmd`=v
gets its -acmd= half emitted outside the quotes.

Bonus defect closed. Switching to '\'' also fixes silent data loss the old
form had: 'a''b' collapses to ab, so an option value containing a quote was
corrupted by the generated replay script. The round-trip test that pinned
that loss as a known limitation now pins the opposite - the quote survives.

2. write-batch-filter-injection - the filter here-doc had no newline guard

Filter rules are replayed through a here-doc terminated by #E#. Nothing
rejected a rule whose pattern contained a newline, so a pattern could forge that
terminator on a line of its own and append arbitrary shell to the script.

Upstream refuses fail-closed, per rule (batch.c:213-240): strchr(pattern, '\n') -> rprintf(FERROR, ...) + exit_cleanup(RERR_SYNTAX). Ported verbatim,
message included.

Placement is the substance here. The refusal goes in
serialize_filter_rules, the last layer that still sees rules individually.
By the time the script emitter runs they are one flattened string, where a
rule-internal newline is indistinguishable from the separator between rules - a
check there could only guess.

Tests

The existing shell_quote goldens encoded the 3.4.x rules, so they had to be
re-based - against the 3.5.0 C source, not against current oc output, which
would have made them tautological. Added:

  • a case per metacharacter the old set missed, with the live backtick exploit
    first;
  • the conditional-prefix rule in both directions (--no_iconv='x' takes the
    prefix, '-a`b=c' does not);
  • ''\''' and 'a'\''b' for the new escape idiom.

Verification

  • write-batch-quoting and write-batch-filter-injection: PASS. Real rsync
    3.5.0 passes both as the control.
  • RED proven by mutation, one fix at a time. Reverting shell_quote to the
    conditional form reds write-batch-quoting and leaves
    write-batch-filter-injection green; disabling the newline guard reds
    write-batch-filter-injection and leaves write-batch-quoting green. Each
    cell is the other's non-vacuity control, so neither row can be passing for the
    other fix's reason.
  • cargo fmt --all -- --check clean; cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings clean.
  • cargo nextest run -p core -p batch --all-features: 3035 passed. The one
    failure, run_client_sparse_copy_creates_holes, passes in isolation and is a
    recorded APFS parallel-run flake unrelated to this change.

Manifest

Both rows flip fail -> pass in upstream-3.5.0-expect.{nonroot,root}.txt
in the same PR. Row counts unchanged at 349 per leg. The TCP manifests do not
carry these cells.

Two independent ways a crafted name reached the shell when an operator ran
BATCH.sh, both reproduced against the rsync 3.5.0 testsuite with the real
3.5.0 binary passing as the control.

1. write-batch-quoting - shell_quote implemented upstream 3.4.x's CONDITIONAL
   quoting: it wrapped an argument only when `strpbrk` matched a
   special-character set. That set omits the backtick, `<`, `>` and the
   braces, so a destination named `d`>PWNED`x/` was written bare and the
   command substitution EXECUTED on replay. Upstream 3.5.0 rewrote write_arg
   (batch.c:164-198) to single-quote unconditionally, and its own comment
   gives the reason: every metacharacter - backtick, newline, redirection -
   must stay literal. Two further rules changed with it: an embedded `'` now
   uses the POSIX close/escape/reopen idiom `'\''` rather than `''`, and the
   bare `-opt=` prefix is emitted only when the option name is `[-_0-9A-Za-z]`,
   so an attacker-shaped `-a`cmd`=v` is quoted whole.

   Switching to `'\''` also fixes a silent data-loss bug the old form had:
   `'a''b'` collapses to `ab`, so an option value containing a quote was
   corrupted by the generated script. The round-trip test that pinned that
   loss as a known limitation now pins the opposite - the quote survives.

2. write-batch-filter-injection - the filter here-doc had no newline guard, so
   a rule whose pattern contained a newline could forge the `#E#` terminator
   on a line of its own and append arbitrary shell to the script. Upstream
   refuses fail-closed per rule (batch.c:213-240, RERR_SYNTAX); the refusal
   goes in serialize_filter_rules, the last layer that still sees rules
   individually - by the time the script emitter runs they are one flattened
   string where a rule-internal newline is indistinguishable from the
   separator between rules.

Tests: the existing shell_quote goldens encoded the 3.4.x rules and are
re-based against the 3.5.0 C source, not against current oc output. Added a
case per metacharacter the old set missed, with the live backtick exploit
first, plus the conditional-prefix rows in both directions.

Verified: write-batch-quoting and write-batch-filter-injection both PASS
(upstream 3.5.0 passes both as control); core+batch 3035/3035; fmt and
workspace clippy clean.
The manifest is the ledger: with the injections closed, both cells pass, and
leaving the rows at `fail` would report an unexpected PASS and redden the
required legs. Measured green against a freshly built binary, with each fix
mutated in isolation to prove the row is earned - reverting shell_quote to the
conditional form reds write-batch-quoting alone, and disabling the newline
guard reds write-batch-filter-injection alone, each leaving the other cell
green as its non-vacuity control.

Row counts unchanged at 349 on both legs.
@github-actions github-actions Bot added the bug Something isn't working label Aug 22, 2026
@oferchen
oferchen merged commit 451dccc into master Aug 22, 2026
70 checks passed
@oferchen
oferchen deleted the fix/batch-replay-script-injection branch August 22, 2026 18:37
oferchen added a commit that referenced this pull request Aug 24, 2026
…7477)

The docs were last refreshed at #7423; 55 PRs have merged since, and the
testsuite figures they quote had drifted far enough to understate the
current state by roughly half.

CHANGELOG: fill in Unreleased for everything merged since #7423. The
dominant theme is routing every operator-named auxiliary file open onto
the ownership walk - upstream's open_no_attacker_symlinks() applied at the
call sites upstream applies it - plus a daemon hardening batch and an
alt-dest correctness batch.

README and SECURITY: the per-leg outcome tables were re-derived from the
committed expect manifests rather than carried forward. Failures fell and
the corpus grew, so every figure moved:

  non-root pipe  43 -> 21 fail   (345 -> 349 corpus)
  root pipe      62 -> 29 fail   (345 -> 349 corpus)
  non-root tcp   42 -> 29 fail   (155 -> 158 corpus)
  root tcp       55 -> 38 fail   (155 -> 158 corpus)

Distinct divergences: 64 -> 31 across the two full-corpus legs, 89 -> 55
across all four.

SECURITY, two CVE rows that had gone stale:
- CVE-2026-53790 said "under audit". The refusal has since been applied at
  the live sink - oc-rsync's own single-character config expander, which
  upstream does not have, so an upstream-shaped port alone would have been
  inert (#7465, #7430, #7445).
- CVE-2026-53795 listed only the staging family. The auxiliary-file family
  followed it onto the walk (#7421-#7426, #7439, #7441-#7443, #7459,
  #7463); what remains is the per-option-family confinement.
oferchen added a commit that referenced this pull request Aug 24, 2026
…7477)

The docs were last refreshed at #7423; 55 PRs have merged since, and the
testsuite figures they quote had drifted far enough to understate the
current state by roughly half.

CHANGELOG: fill in Unreleased for everything merged since #7423. The
dominant theme is routing every operator-named auxiliary file open onto
the ownership walk - upstream's open_no_attacker_symlinks() applied at the
call sites upstream applies it - plus a daemon hardening batch and an
alt-dest correctness batch.

README and SECURITY: the per-leg outcome tables were re-derived from the
committed expect manifests rather than carried forward. Failures fell and
the corpus grew, so every figure moved:

  non-root pipe  43 -> 21 fail   (345 -> 349 corpus)
  root pipe      62 -> 29 fail   (345 -> 349 corpus)
  non-root tcp   42 -> 29 fail   (155 -> 158 corpus)
  root tcp       55 -> 38 fail   (155 -> 158 corpus)

Distinct divergences: 64 -> 31 across the two full-corpus legs, 89 -> 55
across all four.

SECURITY, two CVE rows that had gone stale:
- CVE-2026-53790 said "under audit". The refusal has since been applied at
  the live sink - oc-rsync's own single-character config expander, which
  upstream does not have, so an upstream-shaped port alone would have been
  inert (#7465, #7430, #7445).
- CVE-2026-53795 listed only the staging family. The auxiliary-file family
  followed it onto the walk (#7421-#7426, #7439, #7441-#7443, #7459,
  #7463); what remains is the per-option-family confinement.
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