Skip to content

fix(cli): reject --max-alloc=0 instead of disabling the guard - #7285

Merged
oferchen merged 2 commits into
masterfrom
fix/reject-max-alloc-zero
Aug 13, 2026
Merged

oferchen merged 2 commits into
masterfrom
fix/reject-max-alloc-zero

Conversation

@oferchen

Copy link
Copy Markdown
Owner

What upstream does

rsync 3.5.0 added a zero check to the --max-alloc block in
options.c:parse_arguments() (options.c:2066-2074):

if (max_alloc_arg) {
        ssize_t size = parse_size_arg(max_alloc_arg, 'B', "max-alloc", 1024*1024, -1, True);
        if (size < 0)
                goto cleanup;
        if (size == 0) {
                snprintf(err_buf, sizeof err_buf, "max-alloc must be greater than zero\n");
                goto cleanup;
        }
        max_alloc = size;
}

In 3.4.4 the size == 0 arm does not exist, so zero fell through to
if (!max_alloc) max_alloc = SIZE_MAX; and removed the per-allocation
ceiling that my_alloc() enforces - the defence behind the earlier
allocation CVEs.

Observable contract mirrored here

value
message max-alloc must be greater than zero (verbatim)
client exit 1 - goto cleanup returns 0 from parse_arguments, main.c calls option_error() then exit_cleanup(RERR_SYNTAX)
server exit non-zero - the daemon reports err_buf and exits RERR_UNSUPPORTED (clientserver.c:1254-1268)

parse_arguments() is shared between roles, so the same check covers the
operator flag, RSYNC_MAX_ALLOC, and a value a peer forwarded on the wire.
testsuite/daemon-max-alloc-zero_test.py exists precisely because the
client-side test alone does not protect a daemon: an unpatched 3.2.7 client
still forwards --max-alloc=0.

parse_size_arg's "too small" message keeps its or 0 for unlimited
clause - 3.5.0 left that string untouched even though the caller now
refuses zero. Mirrored verbatim rather than corrected, with the reason
recorded in a comment.

What oc did before (MEASURED)

Against the binary built from origin/master:

$ oc-rsync -a --max-alloc=0 s/ d/          ; echo $?
0                                          # file copied, guard disabled
$ RSYNC_MAX_ALLOC=0 oc-rsync -a s/ d/      ; echo $?
0

Three sites encoded "0 means unlimited":

  • crates/cli/src/frontend/execution/options/size.rs - MAX_ALLOC_UNLIMITED
    returned zero verbatim
  • crates/cli/src/frontend/server/run.rs - a forwarded zero called
    protocol::set_max_alloc(usize::MAX)
  • crates/core/src/client/run/mod.rs - apply_max_alloc did the same

Shared helper: yes, and it is the right place

Both entry points call one parser, parse_max_alloc_argument
(crates/cli/src/frontend/execution/options/size.rs) - the client via
drive/options.rs, the server via server/run.rs, and the environment
default via arguments/env.rs feeding the same flag slot. The check is a
single guard there.

It is not a generic numeric-option helper, though. --block-size=0
goes through the sibling parse_block_size_argument and legitimately means
"use the default" (upstream passes min_value 0 for it), and --bwlimit=0
means unlimited upstream as well. So for the sibling 3.5.0 CVEs in this
family - zstd thread count, MSG_IO_TIMEOUT, xattr expansion, equal-checksum
chain length - there is no one place to fix them all; each needs its own
bound at its own decode site. Worth knowing before those tasks start.

Also checked, not fixed

testsuite/daemon-size-arg-overflow_test.py covers a different bug in the
same function: C99 snprintf returns the length it would have written, so
parse_size_arg's trailing err_buf[len] = '\n' writes out of bounds when a
daemon client sends a multi-kilobyte size value. That is a C string-handling
defect with no analogue in oc - SizeArgError carries no buffer and the
message is a format!, so there is nothing to clamp. Not folded in; stated
here rather than silently skipped.

Changes

  • size.rs - reject zero with upstream's text; delete MAX_ALLOC_UNLIMITED
  • server/run.rs - drop the unreachable unlimited branch; render the error
    with .text() like every sibling branch, instead of emitting a doubled
    rsync error: ... envelope
  • core/client/run/mod.rs - a library-supplied 0 now leaves the standing
    ceiling rather than lifting it, matching protocol::set_max_alloc
  • help text already said "0 is rejected"; the man page now says it too

Tests

New crates/cli/tests/max_alloc_zero_rejected.rs drives the real binary:

  • client_rejects_max_alloc_zero - exit 1 + upstream text
  • client_accepts_a_non_zero_max_alloc - positive control; --max-alloc=2G
    transfers and the destination bytes match, so the rejection test cannot
    pass because everything is refused
  • server_rejects_a_peer_forwarded_max_alloc_zero - --server ... --max-alloc=0
    with closed stdin, the wire-supplied case

Unit cells in size.rs and frontend/tests/parse_max_alloc.rs cover every
spelling of zero (0, 0B, 0K, 0.0M), the RSYNC_MAX_ALLOC=0 path, and
keep the below-1-MiB and above-ceiling rejections pinned. Four pre-existing
tests asserted the old accept-zero behaviour and were inverted.

Verification

cargo fmt --all -- --check                                                     clean
cargo clippy --locked --workspace --all-targets --all-features --no-deps -D warnings
                                                                               clean (2m07s)
cargo nextest run -p cli --all-features -E 'test(max_alloc)'                   71 passed
cargo nextest run -p core -p cli --all-features \
  -E 'test(memory_limit) + test(max_alloc) + test(size) + test(help)'          341 passed

Behaviour after the fix, measured:

$ oc-rsync -a --max-alloc=0 s/ d/
oc-rsync error: max-alloc must be greater than zero (code 1) ...      exit 1
$ RSYNC_MAX_ALLOC=0 oc-rsync -a s/ d/
oc-rsync error: max-alloc must be greater than zero (code 1) ...      exit 1
$ oc-rsync --server --sender -e.LsfxCIvu --max-alloc=0 . s/ < /dev/null
oc-rsync error: max-alloc must be greater than zero (code 1) ...      exit 1
$ oc-rsync -a --max-alloc=2G s/ d/                                    exit 0
$ oc-rsync -a --max-alloc=512K s/ d/
oc-rsync error: --max-alloc=512K is too small (min: 1.00M or 0 for unlimited)  exit 1

upstream rsync 3.5.0 refuses a max-alloc of 0 in parse_arguments()
(options.c:2069-2072) with "max-alloc must be greater than zero".
Previously 0 fell through to `if (!max_alloc) max_alloc = SIZE_MAX;`,
which removed the per-allocation ceiling entirely - the defence behind
the earlier allocation CVEs. The value also arrives from a peer, so a
modified or older client could disable a daemon's cap over the wire.

oc had the same defect: --max-alloc=0 and RSYNC_MAX_ALLOC=0 both
published usize::MAX, on the client path and on the server decode path.

parse_max_alloc_argument is the one parser both entry points share, so
the check lands there once and covers the flag, the environment default
and a peer-forwarded value. The now-unreachable "0 means unlimited"
branches in the server decoder and in apply_max_alloc are removed; a
library caller that still supplies 0 leaves the standing ceiling in
place, matching protocol::set_max_alloc's contract for a zero argument.

The 1 MiB minimum message keeps its "or 0 for unlimited" clause because
3.5.0 left parse_size_arg's text unchanged.

Also fixes the server-side error rendering for this option, which passed
a fully rendered Message where its sibling branches pass bare text and so
emitted a doubled "rsync error: ..." envelope.
@github-actions github-actions Bot added the bug Something isn't working label Aug 13, 2026
@oferchen
oferchen merged commit c1305f7 into master Aug 13, 2026
75 checks passed
@oferchen
oferchen deleted the fix/reject-max-alloc-zero branch August 13, 2026 17:37
oferchen added a commit that referenced this pull request Aug 24, 2026
upstream rsync 3.5.0 refuses a max-alloc of 0 in parse_arguments()
(options.c:2069-2072) with "max-alloc must be greater than zero".
Previously 0 fell through to `if (!max_alloc) max_alloc = SIZE_MAX;`,
which removed the per-allocation ceiling entirely - the defence behind
the earlier allocation CVEs. The value also arrives from a peer, so a
modified or older client could disable a daemon's cap over the wire.

oc had the same defect: --max-alloc=0 and RSYNC_MAX_ALLOC=0 both
published usize::MAX, on the client path and on the server decode path.

parse_max_alloc_argument is the one parser both entry points share, so
the check lands there once and covers the flag, the environment default
and a peer-forwarded value. The now-unreachable "0 means unlimited"
branches in the server decoder and in apply_max_alloc are removed; a
library caller that still supplies 0 leaves the standing ceiling in
place, matching protocol::set_max_alloc's contract for a zero argument.

The 1 MiB minimum message keeps its "or 0 for unlimited" clause because
3.5.0 left parse_size_arg's text unchanged.

Also fixes the server-side error rendering for this option, which passed
a fully rendered Message where its sibling branches pass bare text and so
emitted a doubled "rsync error: ..." envelope.
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