61 Commits
Author SHA1 Message Date
TapTap 6968ff6734 Merge PR #282: fix(benchmark): use real FastSync flags and Release builds
CI / lint (push) Successful in 1m31s
CI / sanitizers (undefined) (push) Successful in 54s
CI / sanitizers (address) (push) Successful in 1m1s
CI / fuzz-build (push) Successful in 33s
CI / coverage (push) Successful in 52s
CI / valgrind (push) Successful in 3m14s
CI / build-and-test (push) Successful in 5m31s
2026-09-13 10:18:21 +02:00
TapTap 5aca91ab22 fix(benchmark): use real FastSync flags and Release builds
CI / lint (pull_request) Successful in 1m29s
CI / sanitizers (address) (pull_request) Skipped
CI / sanitizers (undefined) (pull_request) Skipped
CI / fuzz-build (pull_request) Skipped
CI / coverage (pull_request) Skipped
CI / valgrind (pull_request) Skipped
CI / build-and-test (pull_request) Successful in 1m44s
The benchmark tool used stale rsync-style spellings that map to

different FastSync options, so it never enabled the features it

claimed to measure:

  -c -> --checksum (not compression)

  -m -> --prune-empty-dirs (not multithreading)

  -s -> --secluded-args, a no-op (not chunk serialization)

  -f -> --filter, needs an argument (not sendfile)

Replace them with the real flags (-z, -j, --chunk-serialization,

--sendfile), force CMAKE_BUILD_TYPE=Release, route informational

output to stderr so --output json emits valid JSON, surface

client/rsync failures instead of silently dropping them, and widen

the results table for the longer config names. Update the benchmark

skill to match (correct flags, server invocation, and replace the

nonexistent test.py --full with benchmark/bench.py).
2026-09-13 10:14:37 +02:00
TapTap 9242e86772 Merge Wave 7: fix shared/server layering and explicit CMake targets
CI / lint (push) Successful in 1m30s
CI / sanitizers (undefined) (push) Successful in 56s
CI / sanitizers (address) (push) Successful in 1m3s
CI / fuzz-build (push) Successful in 34s
CI / coverage (push) Successful in 52s
CI / valgrind (push) Successful in 3m14s
CI / build-and-test (push) Successful in 5m33s
2026-09-13 07:30:33 +02:00
TapTap 0155902d95 docs: update stale PipelineContextReceiver reference 2026-09-13 07:30:28 +02:00
TapTap 3499baf80b build: explicit CMake targets; move receiver pipeline out of shared 2026-09-13 07:20:28 +02:00
TapTap 83eacf3151 Merge Wave 6: client features (--port, --threads=N, abort, keepalive) and test coverage
CI / lint (push) Successful in 1m30s
CI / sanitizers (undefined) (push) Successful in 1m4s
CI / sanitizers (address) (push) Successful in 1m9s
CI / fuzz-build (push) Successful in 36s
CI / coverage (push) Successful in 51s
CI / valgrind (push) Successful in 3m14s
CI / build-and-test (push) Successful in 5m37s
2026-09-13 06:59:08 +02:00
TapTap c2df0347ef fix(client,protocol): EINTR-safe sends, armed abort, keepalive drain grace, TLS WANT_WRITE 2026-09-13 06:59:02 +02:00
TapTap dd44537b44 Merge branch 'fix/w6-tests' into fix/w6-integration 2026-09-13 06:25:05 +02:00
TapTap 2854a9d149 test: fuzz manifest/protocol/xattr, hardlink unit, fault injection 2026-09-13 06:24:46 +02:00
TapTap 1fa2fbd266 feat(client): --port alias, --threads=N, graceful abort, keepalive 2026-09-13 06:22:28 +02:00
TapTap 10b18ab2d2 Merge Wave 5a: dead-code removal, scanner options embed, shared config invariants, bounded metadata API
CI / lint (push) Successful in 1m31s
CI / sanitizers (undefined) (push) Successful in 1m0s
CI / sanitizers (address) (push) Successful in 1m7s
CI / fuzz-build (push) Successful in 30s
CI / coverage (push) Successful in 51s
CI / valgrind (push) Successful in 3m12s
CI / build-and-test (push) Successful in 5m32s
2026-09-13 05:48:32 +02:00
TapTap dcc78c14c5 docs,fuzz: fix ownership/alloc comments; fuzz chunk metadata path 2026-09-13 05:48:27 +02:00
TapTap 5a829adb85 Merge branch 'fix/w5-scanner' into fix/w5-integration 2026-09-13 05:28:50 +02:00
TapTap 42c72030fb Merge branch 'fix/w5-config' into fix/w5-integration 2026-09-13 05:28:50 +02:00
TapTap 99f8045105 refactor(scanner,send): embed scanner options; unify stats and config ownership 2026-09-13 05:28:32 +02:00
TapTap eea66a7848 refactor(config,metadata): shared invariants; length-bounded metadata parser 2026-09-13 05:24:23 +02:00
TapTap c944787e03 refactor: remove dead file_store subsystem and unused wrappers 2026-09-13 05:19:18 +02:00
TapTap 57ce6d04f0 Merge Wave 4: performance (packed metadata 2.20.0, indexed lookups, zstd reuse, byte-bounded queues)
CI / lint (push) Successful in 1m30s
CI / sanitizers (undefined) (push) Successful in 1m1s
CI / sanitizers (address) (push) Successful in 1m7s
CI / fuzz-build (push) Successful in 29s
CI / coverage (push) Successful in 50s
CI / valgrind (push) Successful in 3m12s
CI / build-and-test (push) Successful in 5m22s
2026-09-13 05:01:21 +02:00
TapTap 3cf2e2c91f docs(version): align 2.20.0 artifacts; fix protocol-bump rationale 2026-09-13 05:01:15 +02:00
TapTap 301cb0dbaf fix(utils,file-list): bound keep/files-from indexes to O(M) memory 2026-09-13 04:52:10 +02:00
TapTap a4f4110397 Merge branch 'fix/w4-queue' into fix/w4-integration 2026-09-13 04:19:12 +02:00
TapTap 24fe8c5583 Merge branch 'fix/w4-compress' into fix/w4-integration 2026-09-13 04:19:12 +02:00
TapTap d274bdff4e Merge branch 'fix/w4-hash' into fix/w4-integration 2026-09-13 04:19:12 +02:00
TapTap 6c636a19e6 perf(compression,tcp): reuse zstd contexts; enable TCP_NODELAY 2026-09-13 04:18:55 +02:00
TapTap 1a83e284c4 perf(utils,file-list): index delete keep-set and --files-from lookups 2026-09-13 04:16:28 +02:00
TapTap 317d5d081a perf(protocol): pack metadata into one frame (PROTOCOL 2.20.0) 2026-09-13 04:08:36 +02:00
TapTap 69fe7f3c9f perf(send,scanner): byte-bound sender queues; drop redundant stat 2026-09-13 04:06:30 +02:00
TapTap ddc71a7df5 Merge Wave 3b: configurable protocol timeout and idle/session bounds
CI / lint (push) Successful in 1m31s
CI / sanitizers (undefined) (push) Successful in 1m1s
CI / sanitizers (address) (push) Successful in 1m8s
CI / fuzz-build (push) Successful in 30s
CI / coverage (push) Successful in 50s
CI / build-and-test (push) Successful in 4m37s
CI / valgrind (push) Successful in 3m12s
2026-09-13 03:46:25 +02:00
TapTap ffa1d24625 fix(receiver): harden idle-progress definition, single error frame, sendfile timeout 2026-09-13 03:46:20 +02:00
TapTap b16349b81e fix(protocol): honor --timeout for protocol I/O; bound idle/session time 2026-09-13 03:29:01 +02:00
TapTap 4bc84fe954 Merge Wave 3a: daemon host ACL, configurable max connections, peer audit, auth-failure delay
CI / lint (push) Successful in 1m31s
CI / sanitizers (undefined) (push) Successful in 57s
CI / sanitizers (address) (push) Successful in 1m5s
CI / fuzz-build (push) Successful in 29s
CI / coverage (push) Successful in 49s
CI / build-and-test (push) Successful in 4m35s
CI / valgrind (push) Successful in 3m10s
2026-09-13 02:51:11 +02:00
TapTap fc560246c1 fix(daemon): close ACL fail-opens (v4-mapped peers, invalid patterns) and cap auth delay 2026-09-13 02:51:07 +02:00
TapTap dff6609976 feat(daemon): host ACL, configurable max connections, peer audit, auth-failure delay 2026-09-13 02:36:15 +02:00
TapTap 1acb66628d Merge Wave 2: thread-safety fixes (signals, fd ownership, handler epilogue, logging, scanner leak)
CI / lint (push) Successful in 1m30s
CI / sanitizers (undefined) (push) Successful in 59s
CI / sanitizers (address) (push) Successful in 1m6s
CI / fuzz-build (push) Successful in 28s
CI / coverage (push) Successful in 49s
CI / build-and-test (push) Successful in 4m31s
CI / valgrind (push) Successful in 3m10s
2026-09-13 02:13:27 +02:00
TapTap ba1c7a369f fix(server,log): non-socket shutdown fallback, drop redundant delay cleanup, unlock logging I/O 2026-09-13 02:13:22 +02:00
TapTap d28489d83c Merge branch 'fix/w2-scan' into fix/w2-integration 2026-09-13 01:49:44 +02:00
TapTap 312ed05170 Merge branch 'fix/w2-log' into fix/w2-integration 2026-09-13 01:49:44 +02:00
TapTap fecbe2c90c fix(server): child-safe signals, single fd owner, handler cleanup epilogue 2026-09-13 01:49:27 +02:00
TapTap c8f5d80fcb fix(log): serialize message emission; clear log_fp before close; use logger 2026-09-13 01:44:59 +02:00
TapTap 8147ff7b50 fix(scanner): free chunk_data on chunk-create failure 2026-09-13 01:36:51 +02:00
TapTap b7fbb56289 test(file): silence cppcheck constVariablePointer in empty-path test
CI / lint (push) Successful in 1m32s
CI / sanitizers (undefined) (push) Successful in 57s
CI / sanitizers (address) (push) Successful in 1m4s
CI / fuzz-build (push) Successful in 30s
CI / coverage (push) Successful in 49s
CI / build-and-test (push) Successful in 4m28s
CI / valgrind (push) Successful in 3m10s
2026-09-13 01:25:29 +02:00
TapTap 08063b6d73 Merge Wave 1: critical/High fixes (UAF, DoS caps, leaks, hardening)
CI / lint (push) Failing after 1m32s
CI / build-and-test (push) Skipped
CI / sanitizers (address) (push) Skipped
CI / sanitizers (undefined) (push) Skipped
CI / fuzz-build (push) Skipped
CI / coverage (push) Skipped
CI / valgrind (push) Skipped
2026-09-13 01:18:58 +02:00
TapTap ea2f76cd7a fix(receiver): charge per-entry DirTimeList cost; cap client --skip-compress 2026-09-13 01:18:54 +02:00
TapTap 76eeba1773 Merge branch 'fix/w1d-hardening' into fix/w1-integration 2026-09-13 00:59:35 +02:00
TapTap b72ab298ab Merge branch 'fix/w1c-wire' into fix/w1-integration 2026-09-13 00:59:35 +02:00
TapTap 446a714ef8 Merge branch 'fix/w1b-receiver' into fix/w1-integration 2026-09-13 00:59:35 +02:00
TapTap a90e234eb3 harden: overflow guards, auth-user validation, TLS1.3 policy, build hardening 2026-09-13 00:59:21 +02:00
TapTap f8252cf3e7 fix(protocol): bound pre-auth config string memory 2026-09-13 00:57:32 +02:00
TapTap 4557924972 fix(receiver): cap DirTimeList growth and fix placeholder Data leaks 2026-09-13 00:57:32 +02:00
TapTap 59ce174d22 fix(client-send): UAF in basis preflight and missing_args leak 2026-09-13 00:57:09 +02:00
TapTap 2a8941ee5c Merge feat/ref-integration: SuperMode enum, dir-time gate dedup, parse_args/server_module_gate splits
CI / lint (push) Successful in 1m30s
CI / sanitizers (undefined) (push) Successful in 59s
CI / sanitizers (address) (push) Successful in 1m6s
CI / fuzz-build (push) Successful in 29s
CI / coverage (push) Successful in 49s
CI / valgrind (push) Successful in 2m9s
CI / build-and-test (push) Successful in 4m31s
2026-09-12 21:11:03 +02:00
TapTap 37037a6ee7 refactor(client-cli): drop unused CliParseCtx positional fields (cppcheck) 2026-09-12 21:07:14 +02:00
TapTap 061e9ad43f Merge feat/ref-modulegate: split server_module_gate into helpers 2026-09-12 20:56:43 +02:00
TapTap f928879755 Merge feat/ref-parseargs: split parse_args into focused helpers 2026-09-12 20:56:43 +02:00
TapTap 84b7cb0de3 refactor(server): split server_module_gate into ordered helper stages 2026-09-12 20:56:33 +02:00
TapTap 921472b8b3 refactor(client-cli): split parse_args into focused option handlers
Break the ~700-line parse_args god function into cohesive static helpers
grouped by concern: output controls, pre-negation, range/time options, the
OPTION_TABLE dispatcher, flag/meta handlers, IO/network options, filter and
logging options, checksum/socket options, remote/basis/identity options,
positional handling, and a final lowering step.

A file-local CliParseCtx carries the config, cursor, positional buffers and
the mutable parse flags, so each handler stays focused. The dispatcher calls
the handlers in the original recognition order and preserves the exact
return contract (0/1/negative), error messages, log levels and control flow.

Behavior preserved; no functional changes.
2026-09-12 20:55:04 +02:00
TapTap 082ac2645d Merge feat/ref-dirtime: dir-time capture gate dedup 2026-09-12 20:43:42 +02:00
TapTap eefbd1e849 Merge feat/ref-supermode: SuperMode enum 2026-09-12 20:43:42 +02:00
TapTap 1fd462cca8 refactor(config): replace SUPER_MODE_* macros with SuperMode enum
Type Config.super_mode as SuperMode (a proper C enum) instead of a bare
int.  The wire boundary still carries the mode as an int: send casts the
enum explicitly and receive reads a temporary int, validates the
AUTO..OFF range, then casts.  Emitted bytes and accepted values are
unchanged.  ModuleGateContext.super_mode_override keeps its -1 sentinel
as int with an explicit cast at the apply site.

Behavior preserved.
2026-09-12 20:43:24 +02:00
TapTap 4ac37c4d8a refactor(dir-times): extract dir_times_should_capture predicate
Deduplicate the repeated directory-time capture gate
(`config->use_metadata && !config->omit_dir_times`) used by the
sender-side (multiprocessing.c) and receiver-side (receiver.c) sinks
into a single predicate declared next to the DirTimeList machinery in
file_receive.h and defined in file_receive.c.

Behavior preserved: identical short-circuit condition and semantics,
no signature or protocol changes.
2026-09-12 20:42:47 +02:00
TapTap 08af945bd6 Merge main back into dev after v2.19.0 release
CI / lint (push) Successful in 1m32s
CI / sanitizers (address) (push) Successful in 55s
CI / fuzz-build (push) Successful in 30s
CI / sanitizers (undefined) (push) Successful in 53s
CI / coverage (push) Successful in 47s
CI / build-and-test (push) Successful in 4m28s
CI / valgrind (push) Successful in 2m11s
2026-09-12 20:22:54 +02:00
82 changed files with 7308 additions and 2304 deletions

No files matched your search

+6 -6
View File
@@ -12,7 +12,7 @@ jobs:
container: gitea.tap-tap.win/taptap/fastsync-ci:v10 container: gitea.tap-tap.win/taptap/fastsync-ci:v10
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
- name: clang-format check - name: clang-format check
run: find src/ tests/ -name '*.c' -o -name '*.h' | xargs clang-format --dry-run --Werror run: find src/ tests/ -name '*.c' -o -name '*.h' | xargs clang-format --dry-run --Werror
@@ -30,7 +30,7 @@ jobs:
needs: lint needs: lint
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
- name: Configure - name: Configure
run: cmake -B build -S . -DSTRICT_WARNINGS=ON run: cmake -B build -S . -DSTRICT_WARNINGS=ON
@@ -59,7 +59,7 @@ jobs:
sanitizer: [address, undefined] sanitizer: [address, undefined]
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
- name: Configure - name: Configure
run: cmake -B build-${{ matrix.sanitizer }} -S . -DSANITIZER=${{ matrix.sanitizer }} run: cmake -B build-${{ matrix.sanitizer }} -S . -DSANITIZER=${{ matrix.sanitizer }}
@@ -77,7 +77,7 @@ jobs:
if: github.event_name == 'push' if: github.event_name == 'push'
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
- name: Configure (clang + fuzz) - name: Configure (clang + fuzz)
run: CC=clang CXX=clang++ cmake -B build-fuzz -S . -DENABLE_FUZZ=ON run: CC=clang CXX=clang++ cmake -B build-fuzz -S . -DENABLE_FUZZ=ON
@@ -99,7 +99,7 @@ jobs:
if: github.event_name == 'push' if: github.event_name == 'push'
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
- name: Configure - name: Configure
run: cmake -B build -S . -DENABLE_COVERAGE=ON run: cmake -B build -S . -DENABLE_COVERAGE=ON
@@ -123,7 +123,7 @@ jobs:
if: github.event_name == 'push' if: github.event_name == 'push'
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
- name: Configure - name: Configure
run: cmake -B build -S . -DSTRICT_WARNINGS=ON run: cmake -B build -S . -DSTRICT_WARNINGS=ON
+34 -21
View File
@@ -38,50 +38,62 @@ dd if=/dev/urandom of=/tmp/fastsync_bench/src/large.bin bs=1M count=10 2>/dev/nu
Test each configuration 3 times, record median: Test each configuration 3 times, record median:
```bash ```bash
# Real FastSync flags: -z=compression, -j=multithreading,
# --chunk-serialization, --sendfile (long form only). The old rsync-style
# spellings -c/-m/-s/-f are NOT the same options (-c=--checksum,
# -m=--prune-empty-dirs, -s=--secluded-args, -f=--filter) and must not be used.
CONFIGS=( CONFIGS=(
"Standard|" "Standard|"
"Compression|-c" "Compression|-z"
"Multithreading|-m" "Multithreading|-j"
"MT+Compression|-m -c" "MT+Compression|-j -z"
"Chunk Serialization|-s" "Chunk Serialization|-j -z --chunk-serialization"
"MT+Compression+Chunk|-m -c -s" "Sendfile|--sendfile"
"Sendfile|-f"
) )
PORT=18080
for config in "${CONFIGS[@]}"; do for config in "${CONFIGS[@]}"; do
IFS='|' read -r name flags <<< "$config" IFS='|' read -r name flags <<< "$config"
echo "=== $name ===" echo "=== $name ==="
for run in 1 2 3; do for run in 1 2 3; do
rm -rf /tmp/fastsync_bench/dst rm -rf /tmp/fastsync_bench/dst
mkdir -p /tmp/fastsync_bench/dst mkdir -p /tmp/fastsync_bench/dst
./build/server & ./build/server -p "$PORT" --allow-unauthenticated &
SERVER_PID=$! SERVER_PID=$!
sleep 0.5 sleep 0.5
START=$(date +%s%N) START=$(date +%s%N)
./build/client --source-dir /tmp/fastsync_bench/src \ ./build/client --source-dir /tmp/fastsync_bench/src \
--dest-dir /tmp/fastsync_bench/dst \ --dest-dir /tmp/fastsync_bench/dst \
--server-port "$PORT" \
--save-to-disk $flags --save-to-disk $flags
END=$(date +%s%N) END=$(date +%s%N)
ELAPSED=$(( (END - START) / 1000000 )) ELAPSED=$(( (END - START) / 1000000 ))
echo " Run $run: ${ELAPSED}ms" echo " Run $run: ${ELAPSED}ms"
kill $SERVER_PID 2>/dev/null kill $SERVER_PID 2>/dev/null
wait $SERVER_PID 2>/dev/null wait $SERVER_PID 2>/dev/null
done done
done done
``` ```
### Step 4: Full Integration Benchmark (Optional) ### Step 4: Full Benchmark Tool (Preferred)
The maintained benchmark tool is `benchmark/bench.py`. It handles building,
data generation, network shaping (LAN/WAN profiles or custom `--delay`/`--jitter`/
`--throughput`/`--loss`), rsync comparison, and JSON/table reporting:
For comprehensive benchmarking with network shaping:
```bash ```bash
python3 test.py --full python3 benchmark/bench.py --help
python3 benchmark/bench.py --runs 5 --profiles unlimited
python3 benchmark/bench.py --size-mb 100 --random-ratio 0.5 --output json
python3 benchmark/bench.py --delay 50ms --jitter 10ms --throughput 100mbit
``` ```
This tests LAN/WAN profiles, SSH, TLS, and compares against rsync. Network shaping needs root (`tc`/`netem` on `lo`). SSH and TLS coverage lives in
the pytest integration suite, not the benchmark tool.
### Step 5: Report Results ### Step 5: Report Results
@@ -92,13 +104,14 @@ Platform: <OS, CPU, network>
Configuration | Run 1 | Run 2 | Run 3 | Median Configuration | Run 1 | Run 2 | Run 3 | Median
-----------------------|---------|---------|---------|-------- -----------------------|---------|---------|---------|--------
Standard | 0.12s | 0.11s | 0.12s | 0.12s Standard | 0.12s | 0.11s | 0.12s | 0.12s
Compression (-c) | 0.09s | 0.08s | 0.09s | 0.09s Compression (-z) | 0.09s | 0.08s | 0.09s | 0.09s
Multithreading (-m) | 0.07s | 0.07s | 0.08s | 0.07s Multithreading (-j) | 0.07s | 0.07s | 0.08s | 0.07s
MT+Compression (-m -c) | 0.05s | 0.05s | 0.06s | 0.05s MT+Compression (-j -z) | 0.05s | 0.05s | 0.06s | 0.05s
Sendfile (-f) | 0.04s | 0.04s | 0.04s | 0.04s Chunk Serialization (--chunk-serialization) | 0.05s | 0.04s | 0.05s | 0.05s
Sendfile (--sendfile) | 0.04s | 0.04s | 0.04s | 0.04s
Best configuration: MT+Compression (-m -c) Best configuration: Sendfile (--sendfile)
Throughput: <X> MB/s Throughput: <X> MB/s
``` ```
+29
View File
@@ -4,6 +4,35 @@ All notable changes to FastSync are documented here. Versions match
`PROTOCOL_VERSION` (printed by `fastsync --version`); the client and server must `PROTOCOL_VERSION` (printed by `fastsync --version`); the client and server must
run the same version because the handshake is strict. run the same version because the handshake is strict.
## [2.20.0] - 2026-09-13
### Security
- Cap cumulative `DirTimeList` growth and bound pre-auth config-string memory
(remote memory-exhaustion DoS).
- Daemon host access control (`hosts allow`/`hosts deny`, IPv4/IPv6/CIDR),
configurable global `max connections`, connection audit logging, and a
bounded `auth failure delay` throttle. IPv4-mapped peers are normalized and
invalid patterns are rejected at parse time (no silent fail-open).
- Honor `--timeout` for protocol I/O and bound idle/session time to defeat
keepalive slowloris; child-safe signal handling in the forked daemon.
- Compiler/linker hardening (`_FORTIFY_SOURCE`, stack protector, PIE, RELRO)
and pinned build dependencies.
### Fixed
- Use-after-free in the basis-dir oversize preflight.
- Placeholder `Data` leaks, `missing_args` leak, scanner chunk leak.
- Thread-safe logging; single fd owner and cleanup epilogue in the server
handler.
### Performance
- Metadata now crosses the wire as one packed frame (protocol 2.20.0).
- Delete keep-set and `--files-from` lookups indexed (O(n*m) → O(n)).
- Reused per-thread zstd contexts; `TCP_NODELAY` by default.
- Byte-bounded sender queues; removed a redundant scanner `stat()`.
## [2.19.0] - 2026-09-12 ## [2.19.0] - 2026-09-12
### Security ### Security
+196 -26
View File
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.22) cmake_minimum_required(VERSION 3.22)
project(FastFileTransfer VERSION 2.19.0) project(FastFileTransfer VERSION 2.20.0)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD 11)
@@ -38,11 +38,24 @@ if(ENABLE_COVERAGE)
add_link_options(--coverage) add_link_options(--coverage)
endif() endif()
# --- Build hardening option ---
# Production hardening is applied to the shipping server/client binaries only,
# and only when no sanitizer or coverage instrumentation is active: sanitizers
# carry their own instrumentation, and _FORTIFY_SOURCE requires an optimising
# build (never the -O0 used for coverage).
option(ENABLE_HARDENING "Enable compiler/linker hardening for production targets" ON)
set(HARDENING_ACTIVE OFF)
if(ENABLE_HARDENING AND SANITIZER STREQUAL "none" AND NOT ENABLE_COVERAGE)
set(HARDENING_ACTIVE ON)
endif()
include(FetchContent) include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
xxhash xxhash
GIT_REPOSITORY https://github.com/Cyan4973/xxHash GIT_REPOSITORY https://github.com/Cyan4973/xxHash
GIT_TAG v0.8.3 # v0.8.3 is a lightweight tag pointing at this exact commit (no ^{} peel
# entry); pin the commit SHA instead of the mutable tag.
GIT_TAG e626a72bc2321cd320e953a0ccf1584cad60f363 # v0.8.3
SOURCE_SUBDIR cmake_unofficial SOURCE_SUBDIR cmake_unofficial
) )
FetchContent_MakeAvailable(xxhash) FetchContent_MakeAvailable(xxhash)
@@ -57,35 +70,176 @@ endif()
find_package(OpenSSL REQUIRED) find_package(OpenSSL REQUIRED)
file(GLOB SHARED_SRCS "src/shared/*.c") # --- Explicit source lists ---
set(FILE_STORE_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/src/shared/file_store.c") # The shared library is self-contained: it must never depend on the client or
list(REMOVE_ITEM SHARED_SRCS ${FILE_STORE_SRCS}) # server modules. In particular, the receiver pipeline (receive_thread /
file(GLOB SERVER_SRCS "src/server/*.c") # write_thread) lives under src/server, not here, so the client executable can
set(SERVER_RECEIVER_SRCS src/server/receiver.c) # link the shared library without pulling in any server code.
file(GLOB CLIENT_SRCS "src/client/*.c") set(SHARED_SRCS
src/shared/array_list.c
src/shared/batch.c
src/shared/charset.c
src/shared/checksum.c
src/shared/chmod.c
src/shared/chunk.c
src/shared/compression.c
src/shared/config.c
src/shared/credentials.c
src/shared/daemon_conf.c
src/shared/data.c
src/shared/delay_updates.c
src/shared/delta.c
src/shared/file.c
src/shared/file_list.c
src/shared/file_receive.c
src/shared/file_send.c
src/shared/file_store.c
src/shared/filter.c
src/shared/hardlink.c
src/shared/identity.c
src/shared/log.c
src/shared/metadata.c
src/shared/motd.c
src/shared/multiprocessing.c
src/shared/protocol.c
src/shared/queue.c
src/shared/stop_condition.c
src/shared/transport_ssh.c
src/shared/transport_tcp.c
src/shared/transport_tls.c
src/shared/utils.c
src/shared/xattr.c
)
# Server implementation (no main): the receiver read/write pipeline plus the
# CLI parser. The server executable adds its own main (server.c).
set(SERVER_CORE_SRCS
src/server/receiver.c
src/server/receiver_pipeline.c
src/server/server_cli.c
)
set(SERVER_MAIN_SRCS src/server/server.c)
# Client implementation (no main): everything except the CLI entry point.
set(CLIENT_CORE_SRCS
src/client/change_list.c
src/client/client_send.c
src/client/client_validation.c
src/client/scanner.c
src/client/usage.c
)
set(CLIENT_MAIN_SRCS src/client/client_cli.c)
# --- Library targets ---
add_library(fastsync_shared STATIC ${SHARED_SRCS})
target_include_directories(fastsync_shared PUBLIC src/shared)
target_link_libraries(fastsync_shared PUBLIC Threads::Threads ${ZSTD_LIBRARY} OpenSSL::SSL
OpenSSL::Crypto xxhash)
add_library(fastsync_client_core STATIC ${CLIENT_CORE_SRCS})
target_include_directories(fastsync_client_core PUBLIC src/client)
target_link_libraries(fastsync_client_core PUBLIC fastsync_shared)
add_library(fastsync_server_core STATIC ${SERVER_CORE_SRCS})
target_include_directories(fastsync_server_core PUBLIC src/server)
target_link_libraries(fastsync_server_core PUBLIC fastsync_shared)
# --- Main executables --- # --- Main executables ---
add_executable(server ${SERVER_SRCS} ${SHARED_SRCS} ${FILE_STORE_SRCS}) # The client links only the shared library and its own core; it deliberately
target_include_directories(server PRIVATE src/shared src/server src/client) # does NOT get src/server on its include path nor compile receiver.c.
target_link_libraries(server PRIVATE Threads::Threads ${ZSTD_LIBRARY} OpenSSL::SSL OpenSSL::Crypto xxhash) add_executable(server ${SERVER_MAIN_SRCS})
target_link_libraries(server PRIVATE fastsync_server_core)
add_executable(client ${CLIENT_SRCS} ${SHARED_SRCS} ${FILE_STORE_SRCS} ${SERVER_RECEIVER_SRCS}) add_executable(client ${CLIENT_MAIN_SRCS})
target_include_directories(client PRIVATE src/shared src/server src/client) target_link_libraries(client PRIVATE fastsync_client_core)
target_link_libraries(client PRIVATE Threads::Threads ${ZSTD_LIBRARY} OpenSSL::SSL OpenSSL::Crypto xxhash)
# --- Production hardening ---
# Each compile flag is probed so a compiler/architecture that lacks it still
# configures cleanly. _FORTIFY_SOURCE is guarded separately because it only
# works in an optimising build. xxHash is a static archive built by
# FetchContent, so it must be position-independent for the -pie link; the same
# applies to the first-party static libraries linked into the -pie binaries.
if(HARDENING_ACTIVE)
set_target_properties(xxhash fastsync_shared fastsync_server_core fastsync_client_core
PROPERTIES POSITION_INDEPENDENT_CODE ON)
include(CheckCCompilerFlag)
foreach(flag -fstack-protector-strong -fstack-clash-protection -fPIE)
string(MAKE_C_IDENTIFIER "HARDEN_${flag}" _harden_var)
check_c_compiler_flag("${flag}" ${_harden_var})
endforeach()
check_c_compiler_flag("-D_FORTIFY_SOURCE=2" HARDEN_FORTIFY_SOURCE)
foreach(target fastsync_shared fastsync_server_core fastsync_client_core server client)
foreach(flag -fstack-protector-strong -fstack-clash-protection -fPIE)
string(MAKE_C_IDENTIFIER "HARDEN_${flag}" _harden_var)
if(${_harden_var})
target_compile_options(${target} PRIVATE ${flag})
endif()
endforeach()
if(HARDEN_FORTIFY_SOURCE)
target_compile_options(${target} PRIVATE -D_FORTIFY_SOURCE=2)
endif()
endforeach()
foreach(target server client)
target_link_options(${target} PRIVATE -pie -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack)
endforeach()
endif()
# --- Testing --- # --- Testing ---
enable_testing() enable_testing()
# Common test libraries # --- Unit tests ---
set(TEST_LIBS Threads::Threads ${ZSTD_LIBRARY} OpenSSL::SSL OpenSSL::Crypto xxhash) # The monolithic test binary exercises both client and server code, so it is
set(TEST_INCLUDES tests src/shared src/server src/client) # the one place that legitimately sees both include directories and links both
# core libraries. client_cli.c is compiled here directly (with the test build
# define) rather than linked from fastsync_client_core so its test-only shims
# and the absence of main() are preserved.
set(TEST_SRCS
tests/runner.c
tests/test_array_list.c
tests/test_batch.c
tests/test_change_list.c
tests/test_checksum.c
tests/test_chunk.c
tests/test_client_cli.c
tests/test_compression.c
tests/test_config.c
tests/test_credentials.c
tests/test_daemon_conf.c
tests/test_data.c
tests/test_delay_updates.c
tests/test_delta.c
tests/test_file.c
tests/test_file_list.c
tests/test_file_sendfile.c
tests/test_fuzz_smoke.c
tests/test_glob.c
tests/test_hardlink.c
tests/test_iconv.c
tests/test_log.c
tests/test_metadata.c
tests/test_motd.c
tests/test_multiprocessing.c
tests/test_property.c
tests/test_protocol.c
tests/test_queue.c
tests/test_receiver_timeout.c
tests/test_robustness.c
tests/test_scanner.c
tests/test_server.c
tests/test_server_cli.c
tests/test_shared_utils.c
tests/test_stop.c
tests/test_stress.c
tests/test_transport_ssh.c
tests/test_transport_tcp.c
tests/test_transport_tls.c
tests/test_xattr.c
)
# Monolithic test binary (backward compatible) add_executable(tests ${TEST_SRCS} src/client/client_cli.c)
file(GLOB TEST_SRCS "tests/test_*.c" "tests/runner.c") target_include_directories(tests PRIVATE tests)
add_executable(tests ${TEST_SRCS} ${SHARED_SRCS} ${FILE_STORE_SRCS} ${SERVER_RECEIVER_SRCS} src/client/scanner.c src/client/change_list.c src/client/client_cli.c src/client/client_validation.c src/client/usage.c src/server/server_cli.c)
target_include_directories(tests PRIVATE ${TEST_INCLUDES})
target_compile_definitions(tests PRIVATE FASTSYNC_TEST_BUILD) target_compile_definitions(tests PRIVATE FASTSYNC_TEST_BUILD)
target_link_libraries(tests PRIVATE ${TEST_LIBS}) target_link_libraries(tests PRIVATE fastsync_server_core fastsync_client_core)
add_test(NAME unit_all COMMAND tests) add_test(NAME unit_all COMMAND tests)
# --- Fuzz targets (requires clang) --- # --- Fuzz targets (requires clang) ---
@@ -94,13 +248,29 @@ if(ENABLE_FUZZ)
if(NOT CMAKE_C_COMPILER_ID MATCHES "Clang") if(NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
message(FATAL_ERROR "ENABLE_FUZZ requires Clang (compiler is ${CMAKE_C_COMPILER_ID})") message(FATAL_ERROR "ENABLE_FUZZ requires Clang (compiler is ${CMAKE_C_COMPILER_ID})")
endif() endif()
file(GLOB FUZZ_SRCS "tests/fuzz/*.c") set(FUZZ_SRCS
tests/fuzz/fuzz_chunk_deserialize.c
tests/fuzz/fuzz_compress_decompress.c
tests/fuzz/fuzz_config_receive.c
tests/fuzz/fuzz_delta_deserialize.c
tests/fuzz/fuzz_delta_signature_deserialize.c
tests/fuzz/fuzz_glob_match.c
tests/fuzz/fuzz_identity_parse.c
tests/fuzz/fuzz_manifest.c
tests/fuzz/fuzz_metadata_from_buf.c
tests/fuzz/fuzz_protocol_framing.c
tests/fuzz/fuzz_xattr_block.c
)
# Compile the sources under test directly so libFuzzer's coverage
# instrumentation sees them (static libraries would be uninstrumented).
set(FUZZ_CORE_SRCS ${SHARED_SRCS} src/server/receiver.c src/server/receiver_pipeline.c)
foreach(FUZZ_SRC ${FUZZ_SRCS}) foreach(FUZZ_SRC ${FUZZ_SRCS})
get_filename_component(FUZZ_NAME ${FUZZ_SRC} NAME_WE) get_filename_component(FUZZ_NAME ${FUZZ_SRC} NAME_WE)
add_executable(${FUZZ_NAME} ${FUZZ_SRC} ${SHARED_SRCS} ${FILE_STORE_SRCS} ${SERVER_RECEIVER_SRCS}) add_executable(${FUZZ_NAME} ${FUZZ_SRC} ${FUZZ_CORE_SRCS})
target_include_directories(${FUZZ_NAME} PRIVATE ${TEST_INCLUDES}) target_include_directories(${FUZZ_NAME} PRIVATE tests src/shared src/server)
target_compile_options(${FUZZ_NAME} PRIVATE -fsanitize=fuzzer,address,undefined -fno-omit-frame-pointer) target_compile_options(${FUZZ_NAME} PRIVATE -fsanitize=fuzzer,address,undefined -fno-omit-frame-pointer)
target_link_options(${FUZZ_NAME} PRIVATE -fsanitize=fuzzer,address,undefined) target_link_options(${FUZZ_NAME} PRIVATE -fsanitize=fuzzer,address,undefined)
target_link_libraries(${FUZZ_NAME} PRIVATE ${TEST_LIBS}) target_link_libraries(${FUZZ_NAME} PRIVATE Threads::Threads ${ZSTD_LIBRARY} OpenSSL::SSL
OpenSSL::Crypto xxhash)
endforeach() endforeach()
endif() endif()
+48 -8
View File
@@ -104,7 +104,7 @@ partial, alternate, and planned behavior.
| `-c, --checksum` | Verify content by checksum instead of size+mtime | | `-c, --checksum` | Verify content by checksum instead of size+mtime |
| `-z, --compress [level]` | Enable streaming zstd compression (level 1–22, default 5) | | `-z, --compress [level]` | Enable streaming zstd compression (level 1–22, default 5) |
| `-a, --archive` | rsync archive mode (`-rlptgoD`): links, metadata, devices and specials (not compression/multithreading) | | `-a, --archive` | rsync archive mode (`-rlptgoD`): links, metadata, devices and specials (not compression/multithreading) |
| `-j, --threads` | Multithreading mode | | `-j, --threads[=N]` | Multithreading mode; `N` (1–256) sets the parallel scanner worker count, bare `-j`/`--threads` uses the default |
| `-m` | rsync `--prune-empty-dirs` (short form now rsync-parity) | | `-m` | rsync `--prune-empty-dirs` (short form now rsync-parity) |
| `--chunk-serialization` | Chunk serialization (batch all files per chunk; long form only) | | `--chunk-serialization` | Chunk serialization (batch all files per chunk; long form only) |
| `-s` | rsync `--secluded-args` compatibility no-op (remote SSH argv is already injection-safe) | | `-s` | rsync `--secluded-args` compatibility no-op (remote SSH argv is already injection-safe) |
@@ -132,7 +132,7 @@ partial, alternate, and planned behavior.
| `--existing` | Skip files not already present at the destination; update existing files normally. | | `--existing` | Skip files not already present at the destination; update existing files normally. |
| `--bwlimit <KB/s>` | Bandwidth limit in kilobytes per second | | `--bwlimit <KB/s>` | Bandwidth limit in kilobytes per second |
| `--chunk-size <n>` | Chunk size in bytes (default: 10485760) | | `--chunk-size <n>` | Chunk size in bytes (default: 10485760) |
| `--timeout <sec>` | I/O timeout in seconds (default: 30) | | `--timeout <sec>` | Positive I/O timeout in seconds, applied to both the socket (`SO_RCVTIMEO`/`SO_SNDTIMEO`, built-in default 30 s) and the per-message protocol poll deadline (built-in default 60 s). Omit the option to keep both built-ins; `0` is rejected. The server side keeps the built-in 60 s protocol window (the value is not sent on the wire). |
| `--contimeout <sec>` | Connection timeout in seconds (default: 10) | | `--contimeout <sec>` | Connection timeout in seconds (default: 10) |
| `--backup` | Backup existing destination files before overwriting | | `--backup` | Backup existing destination files before overwriting |
| `--backup-dir <dir>` | Target directory for backups (requires `--backup`) | | `--backup-dir <dir>` | Target directory for backups (requires `--backup`) |
@@ -151,6 +151,19 @@ partial, alternate, and planned behavior.
| `--ca <path>` | TLS CA certificate file for verification (PEM) | | `--ca <path>` | TLS CA certificate file for verification (PEM) |
| `--client-cn <name>` | TLS client certificate common name; mandatory with `--tls` (a TLS connection always verifies the client CN) | | `--client-cn <name>` | TLS client certificate common name; mandatory with `--tls` (a TLS connection always verifies the client CN) |
**Per-message vs. connection timeouts.** `--timeout` bounds each individual protocol
send/receive (the `poll()` deadline), so a peer that stops mid-frame is dropped. It
does not, by itself, stop a peer that keeps sending well-formed frames forever. The
receiver therefore also enforces two wall-clock (`CLOCK_MONOTONIC`) bounds on a
connection: a **1 hour** idle limit and a **24 hour** overall session cap. Only
frames that move real work (not `STATUS_KEEPALIVE`/`STATUS_ABORT` and not an
empty `STATUS_CHECK_BATCH`/`STATUS_DIR_TIMES`) refresh the idle timestamp, so a
peer cannot hold a connection slot by emitting cheap empty frames; a peer that
fabricates minimal non-empty frames can still occupy a slot until the 24 hour
cap, since no bound can require actual payload without risking a legitimate
long operation. Both are deliberately generous so a legitimate long-running
transfer is never aborted.
### Server ### Server
| Argument | Description | | Argument | Description |
@@ -189,7 +202,7 @@ partial, alternate, and planned behavior.
3. **FileMetadata** — `mode`, `uid`, `gid`, `mtime_sec`, `mtime_nsec`; 3. **FileMetadata** — `mode`, `uid`, `gid`, `mtime_sec`, `mtime_nsec`;
uid / gid are advisory wire fields and are never applied by the receiver; uid / gid are advisory wire fields and are never applied by the receiver;
atime is unsupported atime is unsupported
4. **Config** — runtime parameters (transported over wire, TLS settings excluded). Includes `timeout`, `contimeout`, `quiet`, `backup`, `backup_dir`, `stats`, `max_depth`, `log_file`, `queue_size`. 4. **Config** — runtime parameters (transported over wire, TLS settings excluded). Includes `timeout`, `contimeout`, `quiet`, `backup`, `backup_dir`, `stats`, `max_depth`, `log_file`.
5. **Queue** — thread-safe bounded queue with condition variables 5. **Queue** — thread-safe bounded queue with condition variables
6. **DirectoryScanner** — recursive BFS traversal with exclude and include pattern support, max-depth enforcement 6. **DirectoryScanner** — recursive BFS traversal with exclude and include pattern support, max-depth enforcement
@@ -365,7 +378,7 @@ features without changing the meaning of ordinary compatibility options.
| Option | Purpose | | Option | Purpose |
|---|---| |---|---|
| `-j`, `--threads` | Enable the multithreaded scanner/loader/sender pipeline. | | `-j`, `--threads[=N]` | Enable the multithreaded scanner/loader/sender pipeline. `N` (1–256) sets the parallel scanner worker count; bare `-j`/`--threads` uses the default. |
| `-z [level]`, `--compress [level]` | Enable streaming zstd compression, levels 1-22. | | `-z [level]`, `--compress [level]` | Enable streaming zstd compression, levels 1-22. |
| `--compress-level <n>` | Set the zstd compression level. | | `--compress-level <n>` | Set the zstd compression level. |
| `--zc <alg>` | Alias for `--compress-choice`. FastSync supports `zstd` and `none`. | | `--zc <alg>` | Alias for `--compress-choice`. FastSync supports `zstd` and `none`. |
@@ -379,12 +392,12 @@ features without changing the meaning of ordinary compatibility options.
| `--delta-block <bytes>` | Set the FastSync delta block size (`--block-size` is an alias). | | `--delta-block <bytes>` | Set the FastSync delta block size (`--block-size` is an alias). |
| `--delta-max <bytes>` | Limit files eligible for FastSync delta transfer. | | `--delta-max <bytes>` | Limit files eligible for FastSync delta transfer. |
| `--server-host <host>` | Select the TCP server host. | | `--server-host <host>` | Select the TCP server host. |
| `--server-port <port>` | Select the TCP server port. | | `--server-port <port>` | Select the TCP server port (`--port <port>` and `--port=<port>` are rsync-friendly aliases). |
| `--tls` | Enable TLS for TCP transport. | | `--tls` | Enable TLS for TCP transport. |
| `--bwlimit <KB/s>` | Apply token-bucket bandwidth limiting. | | `--bwlimit <KB/s>` | Apply token-bucket bandwidth limiting. |
| `--progress` | Show transfer progress and throughput. | | `--progress` | Show transfer progress and throughput. |
| `--stats` | Print transfer statistics. | | `--stats` | Print transfer statistics. |
| `--timeout <seconds>` | Set I/O timeout. | | `--timeout <seconds>` | Set the socket **and** per-message protocol I/O timeout (positive seconds). Omit to keep the built-in 30 s socket / 60 s protocol defaults. |
| `--contimeout <seconds>` | Set connection timeout. | | `--contimeout <seconds>` | Set connection timeout. |
Short-option conflicts with rsync have been resolved for the CLI namespace Short-option conflicts with rsync have been resolved for the CLI namespace
@@ -465,7 +478,7 @@ link-target transfer remains incomplete. |
| `--dest-dir <path>` | Set the destination directory explicitly. | | `--dest-dir <path>` | Set the destination directory explicitly. |
| `--save-to-disk` | Enable server-side disk persistence. | | `--save-to-disk` | Enable server-side disk persistence. |
| `--server-host <host>` | TCP server address. | | `--server-host <host>` | TCP server address. |
| `--server-port <port>` | TCP server port. | | `--server-port <port>` | TCP server port. `--port <port>` / `--port=<port>` is an alias. |
| `--tls` | Enable TLS. Requires `--cert` and `--key`. | | `--tls` | Enable TLS. Requires `--cert` and `--key`. |
| `--cert <path>` | TLS certificate file. | | `--cert <path>` | TLS certificate file. |
| `--key <path>` | TLS private key file. | | `--key <path>` | TLS private key file. |
@@ -487,6 +500,33 @@ defaults to the current directory. |
| `-v`, `--verbose` | Enable debug logging. | | `-v`, `--verbose` | Enable debug logging. |
| `--help` | Print server usage. | | `--help` | Print server usage. |
### Daemon configuration
`fastsync-server --daemon --config FILE` reads a line-based module config (an
implicit global section, then `[module]` sections). Besides `port`, `motd file`,
and `address`, the global section accepts:
- `max connections = N` — cap on concurrent connections, default 100. The
listener enforces it; `0`, negative, and non-numeric values are parse errors.
- `auth failure delay = MS` — milliseconds to sleep after a failed
authentication, default 500. `0` disables it and the value is capped at 60000,
so online password guessing is rate-limited per connection. Successful auths
are never delayed.
- `hosts allow` / `hosts deny` — comma- and/or whitespace-separated host access
patterns.
A `[module]` may also set `max connections` (parsed and validated but not
enforced per module — the global cap applies to the whole listener) and its own
`hosts allow`/`hosts deny`.
Host patterns are `*` (match all), IPv4/IPv6 literals, or IPv4/IPv6 CIDR
(`10.0.0.0/8`, `2001:db8::/32`). Hostnames are not resolved, so hostname globs
are rejected at parse time rather than silently never matching. A matching
`hosts deny` rejects; if any `hosts allow` entries exist, a peer matching none of
them is rejected; deny takes precedence over allow. The global list is checked
before the module list, before authentication, and the connecting peer address
(IPv4 or IPv6) appears in the connection and authentication audit log lines.
## Architecture ## Architecture
### Client ### Client
@@ -511,7 +551,7 @@ defaults to the current directory. |
## Protocol and Security ## Protocol and Security
FastSync protocol version `2.19.0` is shared by the client and server. The FastSync protocol version `2.20.0` is shared by the client and server. The
current protocol is sender-driven and includes configuration negotiation, current protocol is sender-driven and includes configuration negotiation,
including the maximum allocation limit, incremental checks, checksums, including the maximum allocation limit, incremental checks, checksums,
manifests, keep-alives, abort handling, per-file remove-source results, and manifests, keep-alives, abort handling, per-file remove-source results, and
+25 -6
View File
@@ -612,7 +612,7 @@ now transmits targets (the prior behavior was broken/partial); its status moved
|------|-------------------|-----------------|-------| |------|-------------------|-----------------|-------|
| `-e`, `--rsh=COMMAND` | Remote shell to use | ✅ Implemented | `-e`/`--rsh` (and `--rsh=COMMAND`) select the remote-shell program used to build the SSH child argv, overriding the default `ssh`. The command is whitespace-split into the leading argv words so rsync's `-e "ssh -p 2222"` works; the standard `-o` family, an optional `-p` port, `user@host` and the quoted remote command (`fastsync-server --stdio`) follow. Stored in the `rsh_command` config field. **Client-only, never crosses the wire** (it is a launch concern, not a handshake property) | | `-e`, `--rsh=COMMAND` | Remote shell to use | ✅ Implemented | `-e`/`--rsh` (and `--rsh=COMMAND`) select the remote-shell program used to build the SSH child argv, overriding the default `ssh`. The command is whitespace-split into the leading argv words so rsync's `-e "ssh -p 2222"` works; the standard `-o` family, an optional `-p` port, `user@host` and the quoted remote command (`fastsync-server --stdio`) follow. Stored in the `rsh_command` config field. **Client-only, never crosses the wire** (it is a launch concern, not a handshake property) |
| `--rsync-path=PROGRAM` | rsync binary on remote | ✅ Implemented | Alias for `--fastsync-server-path`: both write the `fastsync_server_path` config field used as the remote-side server program (always quoted as one remote-shell word), which CROSSES the wire as before. Kept separate from `--rsh`, which names the local connecting program | | `--rsync-path=PROGRAM` | rsync binary on remote | ✅ Implemented | Alias for `--fastsync-server-path`: both write the `fastsync_server_path` config field used as the remote-side server program (always quoted as one remote-shell word), which CROSSES the wire as before. Kept separate from `--rsh`, which names the local connecting program |
| `--port=PORT` | Alternate daemon port | ✅ Implemented | rsync's daemon-port flag maps to the client-side `server_port` config field: a client connects to a TCP/TLS server (incl. `host::module/path` daemon destinations) with `--server-port`, and the `fastsync-server --daemon` listener's port is taken from its config's `port` key (default 873) or overridden by `--dparam port=` / `-p` | | `--port=PORT`, `--port PORT` | Alternate daemon port | ✅ Implemented | rsync's daemon-port flag is an alias for `--server-port`: both spellings (and `--server-port=PORT`) map to the client-side `server_port` config field. The client connects to a TCP/TLS server (incl. `host::module/path` daemon destinations) on that port, and the `fastsync-server --daemon` listener's port is taken from its config's `port` key (default 873) or overridden by `--dparam port=` / `-p` |
| `--sockopts=OPTIONS` | Custom TCP options | ✅ Implemented | Comma-separated allowlist of `OPT=VAL` applied via `setsockopt` after `socket()` before `connect()`/`bind()`. Only `TCP_NODELAY`, `SO_KEEPALIVE`, `SO_REUSEADDR` (0/1) and `SO_RCVBUF`/`SO_SNDBUF` (byte count) are accepted; an unknown option name or a bad value is rejected up front, never silently ignored. A value is required for every option (`OPT=VAL`; a bare name is an error). Applied to the outgoing TCP and TLS client socket; absent by default. `SockOptEntry`/`sockopts` config fields. Local socket concern: never crosses the wire | | `--sockopts=OPTIONS` | Custom TCP options | ✅ Implemented | Comma-separated allowlist of `OPT=VAL` applied via `setsockopt` after `socket()` before `connect()`/`bind()`. Only `TCP_NODELAY`, `SO_KEEPALIVE`, `SO_REUSEADDR` (0/1) and `SO_RCVBUF`/`SO_SNDBUF` (byte count) are accepted; an unknown option name or a bad value is rejected up front, never silently ignored. A value is required for every option (`OPT=VAL`; a bare name is an error). Applied to the outgoing TCP and TLS client socket; absent by default. `SockOptEntry`/`sockopts` config fields. Local socket concern: never crosses the wire |
| `--blocking-io` | Use blocking I/O for remote shell | ✅ Implemented | With `--blocking-io` the SSH-transport socketpair socket is left without `SO_RCVTIMEO`/`SO_SNDTIMEO`, so the transfer blocks naturally; by default it gets the same read/write timeout as the TCP transport (see `--timeout`). `blocking_io` config bool. **Client-only, never crosses the wire** | | `--blocking-io` | Use blocking I/O for remote shell | ✅ Implemented | With `--blocking-io` the SSH-transport socketpair socket is left without `SO_RCVTIMEO`/`SO_SNDTIMEO`, so the transfer blocks naturally; by default it gets the same read/write timeout as the TCP transport (see `--timeout`). `blocking_io` config bool. **Client-only, never crosses the wire** |
| `--outbuf=N\|L\|B` | Set output buffering | ✅ Implemented | `N` (none/unbuffered) → `_IONBF`, `L` (line) → `_IOLBF`, `B` (block, the default) → `_IOFBF` via `setvbuf` on stdout and stderr. Garbage values are rejected. `outbuf` config field (`OutbufMode`). **Client-only, never crosses the wire** | | `--outbuf=N\|L\|B` | Set output buffering | ✅ Implemented | `N` (none/unbuffered) → `_IONBF`, `L` (line) → `_IOLBF`, `B` (block, the default) → `_IOFBF` via `setvbuf` on stdout and stderr. Garbage values are rejected. `outbuf` config field (`OutbufMode`). **Client-only, never crosses the wire** |
@@ -627,7 +627,7 @@ now transmits targets (the prior behavior was broken/partial); its status moved
|------|-------------------|-----------------|-------| |------|-------------------|-----------------|-------|
| `--daemon` | Run as rsync daemon | ✅ Implemented | Wave A: a real persistent listener. `fastsync-server --daemon --config FILE` (plus `--no-detach` to stay foreground; without it the listener detaches to the background after binding) reads a FastSync-native module config file and serves each connection confined to the requested module's `path` root (never a client-chosen root; every client-chosen-ownership/super-user request (`--numeric-ids`/`--chown`/`--usermap`/`--groupmap`/`--fake-super`/`--copy-as`/explicit `--super`) is refused unless the module opts in with `client owner = yes`, and the operator `--no-super` veto is honored). TCP/TLS via the existing `--tls` stack; plaintext still requires `--allow-unauthenticated` (same secure default as the standalone server). Client destinations use rsync's `host::module/path` form. Wire/protocol: the config frame gained a trailing daemon-module string and `PROTOCOL_VERSION` was bumped **2.14.0 → 2.15.0** (see the Daemon Mode notes below). Daemon mode is built in FastSync's own protocol/config grammar, not rsync's SMB/daemon option encoding | | `--daemon` | Run as rsync daemon | ✅ Implemented | Wave A: a real persistent listener. `fastsync-server --daemon --config FILE` (plus `--no-detach` to stay foreground; without it the listener detaches to the background after binding) reads a FastSync-native module config file and serves each connection confined to the requested module's `path` root (never a client-chosen root; every client-chosen-ownership/super-user request (`--numeric-ids`/`--chown`/`--usermap`/`--groupmap`/`--fake-super`/`--copy-as`/explicit `--super`) is refused unless the module opts in with `client owner = yes`, and the operator `--no-super` veto is honored). TCP/TLS via the existing `--tls` stack; plaintext still requires `--allow-unauthenticated` (same secure default as the standalone server). Client destinations use rsync's `host::module/path` form. Wire/protocol: the config frame gained a trailing daemon-module string and `PROTOCOL_VERSION` was bumped **2.14.0 → 2.15.0** (see the Daemon Mode notes below). Daemon mode is built in FastSync's own protocol/config grammar, not rsync's SMB/daemon option encoding |
| `--config=FILE` | Alternate rsyncd.conf file | ✅ Implemented | Wave A: selects the daemon config file. Default when omitted (in `--daemon` mode): `~/.config/fastsync/fastsyncd.conf` if it exists, else `/etc/fastsyncd.conf`. The grammar is FastSync-native (documented in the Daemon Mode notes below) and strictly rejects unknown keys so a typo can never silently change what a module serves; requires `--daemon` | | `--config=FILE` | Alternate rsyncd.conf file | ✅ Implemented | Wave A: selects the daemon config file. Default when omitted (in `--daemon` mode): `~/.config/fastsync/fastsyncd.conf` if it exists, else `/etc/fastsyncd.conf`. The grammar is FastSync-native (documented in the Daemon Mode notes below) and strictly rejects unknown keys so a typo can never silently change what a module serves; requires `--daemon` |
| `--dparam=OVERRIDE` | Override global daemon config | ✅ Implemented | Wave A: overrides one global scalar from the command line (`--dparam port=8734` and `--dparam=KEY=VALUE` both work). Limited to the global scalar keys the grammar defines (`port`, `motd file`, `address`); keys are case-insensitive and unknown keys/invalid values are rejected. Requires `--daemon` | | `--dparam=OVERRIDE` | Override global daemon config | ✅ Implemented | Wave A: overrides one global scalar from the command line (`--dparam port=8734` and `--dparam=KEY=VALUE` both work). Limited to the global keys the grammar defines (`port`, `motd file`, `address`, `max connections`, `auth failure delay`, `hosts allow`, `hosts deny`); keys are case-insensitive and unknown keys/invalid values are rejected. Requires `--daemon` |
| `--no-detach` | Don't detach from parent | ✅ Implemented | Wave A: with `--daemon`, keeps the listener in the foreground (what integration tests use). Without it the daemonizes (fork/setsid, stdio redirected to /dev/null) after the listening socket is bound. Requires `--daemon` | | `--no-detach` | Don't detach from parent | ✅ Implemented | Wave A: with `--daemon`, keeps the listener in the foreground (what integration tests use). Without it the daemonizes (fork/setsid, stdio redirected to /dev/null) after the listening socket is bound. Requires `--daemon` |
| `--password-file=FILE` | Read daemon password from file | ✅ Implemented | A7 daemon auth. Client: `--password-file` supplies `user:password` for a `host::module/path` destination (the username is taken from this file, so `user@host::module` stays rejected); the literal password is held client-side only for the SCRAM handshake and wiped at teardown. Server (`fastsync-server --daemon --password-file FILE`): the salted-PBKDF2 verifier store that modules with `auth users` are verified against. **Neither the password nor any replayable bearer value crosses the wire or is stored server-side** — the store holds a per-user salt plus derived keys, and the daemon proves the secret with a per-connection nonce challenge. The file must be private to its owner: both the client and server verify the exact inode they read (open-then-`fstat`, so the check cannot be raced) and refuse a `--password-file`/`--early-input` that is not owned by the current user or grants any group/other permission bit (mode 0600), mirroring the TLS private-key check. A process-substitution pipe (`--early-input <(vault ...)`) is still accepted when it satisfies those checks. See the Daemon Mode notes below for the file formats and the plaintext/TLS caveat | | `--password-file=FILE` | Read daemon password from file | ✅ Implemented | A7 daemon auth. Client: `--password-file` supplies `user:password` for a `host::module/path` destination (the username is taken from this file, so `user@host::module` stays rejected); the literal password is held client-side only for the SCRAM handshake and wiped at teardown. Server (`fastsync-server --daemon --password-file FILE`): the salted-PBKDF2 verifier store that modules with `auth users` are verified against. **Neither the password nor any replayable bearer value crosses the wire or is stored server-side** — the store holds a per-user salt plus derived keys, and the daemon proves the secret with a per-connection nonce challenge. The file must be private to its owner: both the client and server verify the exact inode they read (open-then-`fstat`, so the check cannot be raced) and refuse a `--password-file`/`--early-input` that is not owned by the current user or grants any group/other permission bit (mode 0600), mirroring the TLS private-key check. A process-substitution pipe (`--early-input <(vault ...)`) is still accepted when it satisfies those checks. See the Daemon Mode notes below for the file formats and the plaintext/TLS caveat |
| `--early-input=FILE` | Use FILE for daemon early exec | ✅ Implemented | Server-only (requires `--daemon`): a second credential-store file, same new-format grammar as `--password-file`, read before the listener accepts connections (a secrets-manager / process-substitution source). Its entries layer over `--password-file`: byte-identical verifiers dedupe, a conflicting verifier for the same user is a startup error. A daemon whose modules declare `auth users` must be given at least one of the two, or it refuses to start (fail closed) | | `--early-input=FILE` | Use FILE for daemon early exec | ✅ Implemented | Server-only (requires `--daemon`): a second credential-store file, same new-format grammar as `--password-file`, read before the listener accepts connections (a secrets-manager / process-substitution source). Its entries layer over `--password-file`: byte-identical verifiers dedupe, a conflicting verifier for the same user is a startup error. A daemon whose modules declare `auth users` must be given at least one of the two, or it refuses to start (fail closed) |
@@ -635,7 +635,9 @@ now transmits targets (the prior behavior was broken/partial); its status moved
**Daemon Mode notes (Wave A protocol 2.15.0; A7 auth protocol 2.19.0; MOTD no bump):** FastSync daemon mode is supported in FastSync's own protocol/config grammar, not rsync's SMB/daemon option encoding. **Daemon Mode notes (Wave A protocol 2.15.0; A7 auth protocol 2.19.0; MOTD no bump):** FastSync daemon mode is supported in FastSync's own protocol/config grammar, not rsync's SMB/daemon option encoding.
- **Config grammar** (`fastsyncd.conf`): line-based; an implicit global section first, then `[module]` sections. Keys are case-insensitive, values are trimmed and may be wrapped in one layer of double quotes (`path = "/srv/my dir"`). `#` and `;` at the start of a line (after leading whitespace) are full-line comments; inline comments and `\` continuations are not supported. Lines are bounded (4096 chars). Global keys: `port` (default 873), `motd file` (the daemon sends its bounded, escaped content to a client after the module gate/auth accepts, unless the client passes `--no-motd`), `address` (optional bind address). Module keys: `path` (required; the daemon-side authorized root for that module), `read only` (yes/no/true/false/1/0, default no), `client owner` (yes/no/true/false/1/0, default no; opts the module into client-chosen ownership — see below), `auth users` (comma list). **Unknown keys and malformed lines are parse-and-reject errors** (never silently ignored), so a typo cannot change what a module serves. - **Config grammar** (`fastsyncd.conf`): line-based; an implicit global section first, then `[module]` sections. Keys are case-insensitive, values are trimmed and may be wrapped in one layer of double quotes (`path = "/srv/my dir"`). `#` and `;` at the start of a line (after leading whitespace) are full-line comments; inline comments and `\` continuations are not supported. Lines are bounded (4096 chars). Global keys: `port` (default 873), `motd file` (the daemon sends its bounded, escaped content to a client after the module gate/auth accepts, unless the client passes `--no-motd`), `address` (optional bind address), `max connections` (positive integer cap on concurrent connections, default 100; 0/negative/garbage is a parse error), `auth failure delay` (milliseconds to sleep after a failed authentication, default 500; 0 disables, capped at 5000), `hosts allow` and `hosts deny` (comma- and/or whitespace-separated host access patterns — see the host access control note below). Module keys: `path` (required; the daemon-side authorized root for that module), `read only` (yes/no/true/false/1/0, default no), `client owner` (yes/no/true/false/1/0, default no; opts the module into client-chosen ownership — see below), `auth users` (comma list), `max connections` (optional per-module cap; parsed and stored but **not enforced** — the global cap applies to the whole listener), `hosts allow`/`hosts deny` (per-module host access lists). **Unknown keys and malformed lines are parse-and-reject errors** (never silently ignored), so a typo cannot change what a module serves.
- **Host access control (`hosts allow`/`hosts deny`):** both keys accept a comma- and/or whitespace-separated list of patterns and may appear globally and/or per module (multiple config-file lines append; a `--dparam` override replaces). Supported patterns are `*` (match all), an IPv4 or IPv6 literal (`10.0.0.1`, `2001:db8::1`), and an IPv4/IPv6 CIDR (`10.0.0.0/8`, `2001:db8::/32`). Hostname patterns are **not** supported: because the peer is always a numeric address and no reverse DNS is performed, a hostname/glob pattern would silently never match, so it is rejected at load time (fail-closed) instead of being accepted as a dead rule. An IPv4 peer on a dual-stack IPv6 listener is normalized from its `::ffff:a.b.c.d` form so IPv4 patterns match it. rsync-like semantics: a matching `hosts deny` rejects; if any `hosts allow` entries exist, a peer matching none of them is rejected; deny takes precedence over allow. The daemon enforces the global list first, then the selected module's list, **before authentication** in `server_module_gate`, with an audit log line naming the peer, the module and the outcome. The numeric peer address is obtained with `getpeername`+`inet_ntop` (`utils_fd_peer_ip`, handling both address families); when it cannot be obtained a module with any ACL fails closed (refused), while an ACL-free module continues and logs at debug. A malformed pattern (e.g. an out-of-range CIDR prefix) is a parse error at load time.
- **Connection cap and auth throttle:** the global `max connections` key (default 100) is plumbed into the listener (`transport_tcp.c`), which rejects a connection once the accept-loop parent's active-child count reaches it; the IPv4/IPv6 peer is logged for every accepted connection. The optional per-module `max connections` key is parsed and validated but **not enforced** (connections are counted in the parent before the client's module is known); the daemon logs a startup warning for any module that sets it. On a failed authentication the per-connection child sleeps the global `auth failure delay` (default 500 ms, 0 disables, capped at 5000) via `nanosleep` before the connection closes, rate-limiting online guessing without delaying a success.
- **Module selection & confinement:** the client requests a module with an rsync-style `host::module[/path]` destination. The module name crosses the wire as a trailing string on the config frame (bumping `PROTOCOL_VERSION` 2.14.0 → 2.15.0; the bump is required because the config-frame layout changed and the strict same-version handshake is what prevents a peer from desynchronizing on the new trailing field). The daemon looks the module up in ITS OWN config and uses the module's `path` as the authorized root through the exact same `configure_authorization` confinement the standalone server applies to `--destination-root` (`file_open_secure_parent`, `has_path_traversal`, `path_is_within`); the client never supplies the root, every client-chosen-ownership/super-user request is refused unless the module declares `client owner = yes` (the daemon's per-module opt-in, see below), and the operator `--no-super` veto forces super-user activities off for every daemon connection. The client's `/path` part is relative inside the module and is rejected if absolute or if it contains `..`. Unknown modules are refused before any data moves (the run fails cleanly at the config handshake). An absolute destination and a module request against a non-daemon server are also refused. - **Module selection & confinement:** the client requests a module with an rsync-style `host::module[/path]` destination. The module name crosses the wire as a trailing string on the config frame (bumping `PROTOCOL_VERSION` 2.14.0 → 2.15.0; the bump is required because the config-frame layout changed and the strict same-version handshake is what prevents a peer from desynchronizing on the new trailing field). The daemon looks the module up in ITS OWN config and uses the module's `path` as the authorized root through the exact same `configure_authorization` confinement the standalone server applies to `--destination-root` (`file_open_secure_parent`, `has_path_traversal`, `path_is_within`); the client never supplies the root, every client-chosen-ownership/super-user request is refused unless the module declares `client owner = yes` (the daemon's per-module opt-in, see below), and the operator `--no-super` veto forces super-user activities off for every daemon connection. The client's `/path` part is relative inside the module and is rejected if absolute or if it contains `..`. Unknown modules are refused before any data moves (the run fails cleanly at the config handshake). An absolute destination and a module request against a non-daemon server are also refused.
- **`client owner` (client-chosen-ownership opt-in):** by default a daemon module refuses every request that would let the client pick an owner or ask for super-user activities — `--numeric-ids`, `--chown`, `--usermap`/`--groupmap`, `--fake-super`, `--copy-as`, and an explicit `--super` — at the config handshake (before `STATUS_OK`), because a daemon has no per-module opt-in for client-chosen ownership and any anonymous client could otherwise force arbitrary owner ids inside the module root. `client owner = yes` opts a single module in, allowing those requests within that module's root (the standalone listener and the SSH `--stdio` server always honor them for their single operator-authorized root). Without the opt-in the daemon also forces super-user **device** activity off for that connection — char/block device-node creation (`--devices`) and `--write-devices` — even under the default `AUTO` mode, so a non-opted module can never be made to `mknod` or write a raw device; those entries are skipped (not refused) so an ordinary `-a` push still succeeds without device nodes. The opt-in does **not** lift the privilege requirement: `--copy-as` still needs a root receiver, and the operator `--no-super` veto still forces super-user activities off for every connection. The daemon logs a prominent startup warning for each `client owner = yes` module so the operator's deliberate choice is visible. - **`client owner` (client-chosen-ownership opt-in):** by default a daemon module refuses every request that would let the client pick an owner or ask for super-user activities — `--numeric-ids`, `--chown`, `--usermap`/`--groupmap`, `--fake-super`, `--copy-as`, and an explicit `--super` — at the config handshake (before `STATUS_OK`), because a daemon has no per-module opt-in for client-chosen ownership and any anonymous client could otherwise force arbitrary owner ids inside the module root. `client owner = yes` opts a single module in, allowing those requests within that module's root (the standalone listener and the SSH `--stdio` server always honor them for their single operator-authorized root). Without the opt-in the daemon also forces super-user **device** activity off for that connection — char/block device-node creation (`--devices`) and `--write-devices` — even under the default `AUTO` mode, so a non-opted module can never be made to `mknod` or write a raw device; those entries are skipped (not refused) so an ordinary `-a` push still succeeds without device nodes. The opt-in does **not** lift the privilege requirement: `--copy-as` still needs a root receiver, and the operator `--no-super` veto still forces super-user activities off for every connection. The daemon logs a prominent startup warning for each `client owner = yes` module so the operator's deliberate choice is visible.
- **`read only` safe default:** every network transfer FastSync currently supports is a push that writes under the module root, so a `read only` module refuses the connection (clear server log "module is read only"; the client exits non-zero, nothing is transferred). A future pull/list operation can be opened up when it exists; the knob is already stored. - **`read only` safe default:** every network transfer FastSync currently supports is a push that writes under the module root, so a `read only` module refuses the connection (clear server log "module is read only"; the client exits non-zero, nothing is transferred). A future pull/list operation can be opened up when it exists; the knob is already stored.
@@ -677,7 +679,7 @@ now transmits targets (the prior behavior was broken/partial); its status moved
| `--stop-after=MINS` | Stop after N minutes | ✅ Implemented | Client-only sender stop deadline (Phase 6): computing `--stop-after=MINS` (a positive minute count; 0/negative/garbage rejected) and `--stop-at=TIME` (`HH:MM`, `HH:MM:SS`, or `now+N[smhd]`; a past time stops immediately). The transfer stops ELEGANTLY at the next chunk boundary: everything already fully sent is kept and applied, the run returns 0, and --delete (late/delete-after timing) does NOT wipe the destination — when the scan is cut short the partial keep-set manifest is suppressed with a warning (the delete walk is skipped rather than acting on an incomplete keep-set, so unscanned source mirrors survive). `--delete-before`/`--delete-during` still run their complete pre-scan (which ignores the deadline). Local client-only fields: never serialized into the wire config frame, so no PROTOCOL_VERSION bump. `--stop-after` uses CLOCK_MONOTONIC; `--stop-at` uses the wall clock. Works single-threaded and under `-j`/`--threads` (multithreaded). Divergence: rsync computes `--stop-after` from the run start; FastSync likewise. When both are given, the earlier of the two deadlines wins (checked per iteration). See the Phase-6 stop notes below | | `--stop-after=MINS` | Stop after N minutes | ✅ Implemented | Client-only sender stop deadline (Phase 6): computing `--stop-after=MINS` (a positive minute count; 0/negative/garbage rejected) and `--stop-at=TIME` (`HH:MM`, `HH:MM:SS`, or `now+N[smhd]`; a past time stops immediately). The transfer stops ELEGANTLY at the next chunk boundary: everything already fully sent is kept and applied, the run returns 0, and --delete (late/delete-after timing) does NOT wipe the destination — when the scan is cut short the partial keep-set manifest is suppressed with a warning (the delete walk is skipped rather than acting on an incomplete keep-set, so unscanned source mirrors survive). `--delete-before`/`--delete-during` still run their complete pre-scan (which ignores the deadline). Local client-only fields: never serialized into the wire config frame, so no PROTOCOL_VERSION bump. `--stop-after` uses CLOCK_MONOTONIC; `--stop-at` uses the wall clock. Works single-threaded and under `-j`/`--threads` (multithreaded). Divergence: rsync computes `--stop-after` from the run start; FastSync likewise. When both are given, the earlier of the two deadlines wins (checked per iteration). See the Phase-6 stop notes below |
| `--stop-at=TIME` | Stop at specified time | ✅ Implemented | Same feature as `--stop-after` (deadline transfer stop), absolute wall-clock form (`HH:MM[:SS]` or `now+N[smhd]`). See the row above and the Phase-6 stop notes | | `--stop-at=TIME` | Stop at specified time | ✅ Implemented | Same feature as `--stop-after` (deadline transfer stop), absolute wall-clock form (`HH:MM[:SS]` or `now+N[smhd]`). See the row above and the Phase-6 stop notes |
| `--fsync` | Fsync every written file before publication | ✅ Implemented | | | `--fsync` | Fsync every written file before publication | ✅ Implemented | |
| `--protocol=NUM` | Force older protocol version | ✅ Implemented | Forces the wire protocol version for this transfer. FastSync has exactly ONE wire format (`PROTOCOL_VERSION`, currently 2.19.0) with no downgrade/backward-compat code paths, so `--protocol=2.19.0` is accepted (it sets the version claim the client sends, which the server already requires to match exactly) and **every other value is rejected up front** with a clear error before any connection — it does not and cannot speak an older or virtual wire format. Divergence from rsync (which negotiates a range and downgrades to an integer 0..31): FastSync's honest contract is force-to-the-one-supported-value; a genuine downgrade would require a per-version compatibility layer that does not exist. Client-only; the server-side exact-match check is unchanged. `--protocol=2.18.0`/`2.18`/`2.17.0`/`2.16.0`/`2.15.0`/`216`/`31`/garbage are all rejected. See the Phase-6 protocol note below | | `--protocol=NUM` | Force older protocol version | ✅ Implemented | Forces the wire protocol version for this transfer. FastSync has exactly ONE wire format (`PROTOCOL_VERSION`, currently 2.20.0) with no downgrade/backward-compat code paths, so `--protocol=2.20.0` is accepted (it sets the version claim the client sends, which the server already requires to match exactly) and **every other value is rejected up front** with a clear error before any connection — it does not and cannot speak an older or virtual wire format. Divergence from rsync (which negotiates a range and downgrades to an integer 0..31): FastSync's honest contract is force-to-the-one-supported-value; a genuine downgrade would require a per-version compatibility layer that does not exist. Client-only; the server-side exact-match check is unchanged. `--protocol=2.19.0`/`2.18.0`/`2.18`/`2.17.0`/`2.16.0`/`2.15.0`/`216`/`31`/garbage are all rejected. See the Phase-6 protocol note below |
| `--iconv=CONVERT_SPEC` | Charset conversion | ✅ Implemented | Charset conversion of FILE NAMES (not content) at the protocol boundary via iconv(3): `--iconv=LOCAL[,REMOTE]` — the sender converts each local filename LOCAL→REMOTE before transmitting, and the receiver converts each wire filename REMOTE→LOCAL before creating/writing. The full CONVERT_SPEC is serialized into the config frame as a new trailing string field so the peer knows the wire charset; **PROTOCOL_VERSION bumped 2.15.0 → 2.16.0**. `LOCAL[,REMOTE]` parse: single charset ⇒ LOCAL==REMOTE (identity both ways); garbage rejected up front. Validation probes BOTH directions (a spec that only opens one way is refused, as is a NUL-emitting target charset like utf-16/utf-32/ucs-2, since filenames cannot contain NUL). An unrepresentable name (EILSEQ/EINVAL) fails that path cleanly with a logged `--iconv: cannot convert file name ...` and is never written mangled/truncated. Conversion is applied at EVERY wire-path site (regular/MKDIR/hardlink path+target/symlink path+target/SPECIAL, the delete manifest, the incremental-check path, and the `-s`/`chunk_serialize` embedded blob path), on both client and server (`--iconv` is also a server/daemon option). Zero overhead when unset. See the Phase-6 iconv notes below | | `--iconv=CONVERT_SPEC` | Charset conversion | ✅ Implemented | Charset conversion of FILE NAMES (not content) at the protocol boundary via iconv(3): `--iconv=LOCAL[,REMOTE]` — the sender converts each local filename LOCAL→REMOTE before transmitting, and the receiver converts each wire filename REMOTE→LOCAL before creating/writing. The full CONVERT_SPEC is serialized into the config frame as a new trailing string field so the peer knows the wire charset; **PROTOCOL_VERSION bumped 2.15.0 → 2.16.0**. `LOCAL[,REMOTE]` parse: single charset ⇒ LOCAL==REMOTE (identity both ways); garbage rejected up front. Validation probes BOTH directions (a spec that only opens one way is refused, as is a NUL-emitting target charset like utf-16/utf-32/ucs-2, since filenames cannot contain NUL). An unrepresentable name (EILSEQ/EINVAL) fails that path cleanly with a logged `--iconv: cannot convert file name ...` and is never written mangled/truncated. Conversion is applied at EVERY wire-path site (regular/MKDIR/hardlink path+target/symlink path+target/SPECIAL, the delete manifest, the incremental-check path, and the `-s`/`chunk_serialize` embedded blob path), on both client and server (`--iconv` is also a server/daemon option). Zero overhead when unset. See the Phase-6 iconv notes below |
| `--checksum-seed=NUM` | Set checksum seed | ✅ Implemented | Sets the seed for FastSync's whole-file xxHash64 digest (full 64-bit seed) and for the delta path's per-block xxHash32 strong checksum (low 32 bits of the seed). An explicit seed deterministically changes every computed digest on BOTH endpoints (sender and receiver share the seed via the config frame, protocol 2.10.0), so identical runs with the same seed skip the same files and a changed seed changes the digests — the explicit-seed path that makes xxHash comparisons deterministic. `--checksum-choice=md5` has no seed and ignores it (documented). The value is a strict decimal 0..2⁶⁴-1 (blank, signed, or non-numeric values are rejected). Like rsync, a seed only matters where a digest is actually computed (`--checksum` or a basis-dir run, or a delta transfer); it does not by itself enable `--checksum`/`--delta`. Divergence from rsync: the default is seed 0, and FastSync never randomizes the seed (rsync uses a random per-transfer seed when `--checksum-seed` is unset); FastSync's unset default therefore reproduces its historical byte-for-byte behavior | | `--checksum-seed=NUM` | Set checksum seed | ✅ Implemented | Sets the seed for FastSync's whole-file xxHash64 digest (full 64-bit seed) and for the delta path's per-block xxHash32 strong checksum (low 32 bits of the seed). An explicit seed deterministically changes every computed digest on BOTH endpoints (sender and receiver share the seed via the config frame, protocol 2.10.0), so identical runs with the same seed skip the same files and a changed seed changes the digests — the explicit-seed path that makes xxHash comparisons deterministic. `--checksum-choice=md5` has no seed and ignores it (documented). The value is a strict decimal 0..2⁶⁴-1 (blank, signed, or non-numeric values are rejected). Like rsync, a seed only matters where a digest is actually computed (`--checksum` or a basis-dir run, or a delta transfer); it does not by itself enable `--checksum`/`--delta`. Divergence from rsync: the default is seed 0, and FastSync never randomizes the seed (rsync uses a random per-transfer seed when `--checksum-seed` is unset); FastSync's unset default therefore reproduces its historical byte-for-byte behavior |
| `--secluded-args`, `-s` | Use protocol to send args | ⛔ Impossible/Divergence | Accepted for CLI compatibility (including the rsync short `-s`, Phase 7 Wave A) but a documented **no-op / divergence**. rsync's `-s` protects arguments from shell expansion by shipping them over the protocol; FastSync never passes remote arguments through a shell expansion boundary in the first place — its SSH transport builds the remote argv as **single-quote-escaped shell words** (`ssh_build_remote_command`), so the injection/leak that `-s` guards against does not exist and there is nothing to "seclude". Implementing a true arg-send protocol would mean replacing the argv-based SSH launch with an in-band argument channel, a large redesign of the transport that buys no security here. Chunk serialization remains the long-only `--chunk-serialization`. | | `--secluded-args`, `-s` | Use protocol to send args | ⛔ Impossible/Divergence | Accepted for CLI compatibility (including the rsync short `-s`, Phase 7 Wave A) but a documented **no-op / divergence**. rsync's `-s` protects arguments from shell expansion by shipping them over the protocol; FastSync never passes remote arguments through a shell expansion boundary in the first place — its SSH transport builds the remote argv as **single-quote-escaped shell words** (`ssh_build_remote_command`), so the injection/leak that `-s` guards against does not exist and there is nothing to "seclude". Implementing a true arg-send protocol would mean replacing the argv-based SSH launch with an in-band argument channel, a large redesign of the transport that buys no security here. Chunk serialization remains the long-only `--chunk-serialization`. |
@@ -793,7 +795,7 @@ These are the hardest compatibility items because they require durable formats o
**Phase 6, Wave B (iconv) shipping note (PROTOCOL 2.15.0 → 2.16.0):** `--iconv=LOCAL[,REMOTE]` converts file NAMES at the wire boundary (never content). The full CONVERT_SPEC is serialized into the config frame as a new trailing string field (empty→NULL canonicalized), so both ends share the same wire charset interpretation; this required the PROTOCOL bump because the frame is a strict ordered sequence and a peer that does not parse the new trailing field would desynchronize. Each end derives LOCAL (its own charset) and REMOTE (the wire charset): the sender opens LOCAL→REMOTE and converts every transmitted filename; the receiver opens REMOTE→LOCAL and converts every received filename before creating/writing. Conversion is applied at every wire-path site (regular/MKDIR/hardlink path+target/symlink path+target/SPECIAL, the delete manifest keep/protected/missing entries, the incremental-check path, and the embedded `-s`/chunk-blob path). A name it cannot convert (EILSEQ/EINVAL) is failed cleanly with a logged `--iconv: cannot convert file name ...` and is never written truncated/mangled. Validation probes both directions up front (both the sender local→remote and the receiver remote→local, and, for a server/daemon with its own `--iconv`, the client-REMOTE→server-LOCAL pair) so an unusable spec is rejected before the connection rather than mid-transfer, and NUL-emitting target charsets (utf-16/utf-32/ucs-2) are refused because filenames cannot contain NUL. Divergence documented upstream: the receiver does NOT half-swap; the wire charset always comes from the sender's REMOTE half, so a server whose local charset differs from the client's LOCAL must declare it with its own `--iconv`. Conversion is process-global and runs on a single thread per process (sender thread / receiver-loop thread), initialized before worker threads start and freed after they join. **Phase 6, Wave B (iconv) shipping note (PROTOCOL 2.15.0 → 2.16.0):** `--iconv=LOCAL[,REMOTE]` converts file NAMES at the wire boundary (never content). The full CONVERT_SPEC is serialized into the config frame as a new trailing string field (empty→NULL canonicalized), so both ends share the same wire charset interpretation; this required the PROTOCOL bump because the frame is a strict ordered sequence and a peer that does not parse the new trailing field would desynchronize. Each end derives LOCAL (its own charset) and REMOTE (the wire charset): the sender opens LOCAL→REMOTE and converts every transmitted filename; the receiver opens REMOTE→LOCAL and converts every received filename before creating/writing. Conversion is applied at every wire-path site (regular/MKDIR/hardlink path+target/symlink path+target/SPECIAL, the delete manifest keep/protected/missing entries, the incremental-check path, and the embedded `-s`/chunk-blob path). A name it cannot convert (EILSEQ/EINVAL) is failed cleanly with a logged `--iconv: cannot convert file name ...` and is never written truncated/mangled. Validation probes both directions up front (both the sender local→remote and the receiver remote→local, and, for a server/daemon with its own `--iconv`, the client-REMOTE→server-LOCAL pair) so an unusable spec is rejected before the connection rather than mid-transfer, and NUL-emitting target charsets (utf-16/utf-32/ucs-2) are refused because filenames cannot contain NUL. Divergence documented upstream: the receiver does NOT half-swap; the wire charset always comes from the sender's REMOTE half, so a server whose local charset differs from the client's LOCAL must declare it with its own `--iconv`. Conversion is process-global and runs on a single thread per process (sender thread / receiver-loop thread), initialized before worker threads start and freed after they join.
**Phase 6, Wave C (protocol-version) shipping note (no PROTOCOL_VERSION change):** `--protocol=NUM` lets the client force the wire protocol version for a transfer. FastSync's protocol is a single lockstep format: the config frame is a strict ordered sequence and the server requires the client's version string to equal `PROTOCOL_VERSION` exactly (`config_receive_with_validate`, src/shared/config.c) — there are no older-format code paths and no downgrade/negotiation machinery, so a lower/higher/virtual version can never be spoken. The honest contract is therefore: `--protocol=2.19.0` (the current `PROTOCOL_VERSION`, as of the A7 auth redesign) is accepted and stored into the client's `version` claim (which `config_send` already transmits), and every other value — `2.18.0`, `2.18`, `2.17.0`, `2.16.0`, `2.15.0`, `3.0.0`, rsync-integer spellings like `216`/`31`, garbage, empty — is rejected up front in `validate_config()` before any connection, with a clear error that FastSync supports only its current wire protocol and cannot speak an older or virtual one. Implementation is client-only: a server-side `--protocol` is intentionally not added because the server has no negotiation (it only enforces exact match), and it could only ever be the current version. This preserves (and slightly tightens) existing validation: the client now also refuses to launch with a version it cannot actually speak, rather than only the server rejecting it later. A genuine downgrade would require a per-version compatibility layer for every frame/feature added since (append 2.10, preallocate 2.11, hardlinks 2.12, devices/specials/symlink-trust/xattr 2.13, remote-option 2.14, daemon module/auth 2.15, iconv 2.16, dir/symlink times 2.17, privilege flags --super/--copy-as 2.18, SCRAM daemon auth 2.19) and is intentionally out of scope — documented divergences from rsync's integer-negotiated downgrade remain. **Phase 6, Wave C (protocol-version) shipping note (no PROTOCOL_VERSION change):** `--protocol=NUM` lets the client force the wire protocol version for a transfer. FastSync's protocol is a single lockstep format: the config frame is a strict ordered sequence and the server requires the client's version string to equal `PROTOCOL_VERSION` exactly (`config_receive_with_validate`, src/shared/config.c) — there are no older-format code paths and no downgrade/negotiation machinery, so a lower/higher/virtual version can never be spoken. The honest contract is therefore: `--protocol=2.20.0` (the current `PROTOCOL_VERSION`, as of the packed-metadata wave) is accepted and stored into the client's `version` claim (which `config_send` already transmits), and every other value — `2.19.0`, `2.18.0`, `2.18`, `2.17.0`, `2.16.0`, `2.15.0`, `3.0.0`, rsync-integer spellings like `216`/`31`, garbage, empty — is rejected up front in `validate_config()` before any connection, with a clear error that FastSync supports only its current wire protocol and cannot speak an older or virtual one. Implementation is client-only: a server-side `--protocol` is intentionally not added because the server has no negotiation (it only enforces exact match), and it could only ever be the current version. This preserves (and slightly tightens) existing validation: the client now also refuses to launch with a version it cannot actually speak, rather than only the server rejecting it later. A genuine downgrade would require a per-version compatibility layer for every frame/feature added since (append 2.10, preallocate 2.11, hardlinks 2.12, devices/specials/symlink-trust/xattr 2.13, remote-option 2.14, daemon module/auth 2.15, iconv 2.16, dir/symlink times 2.17, privilege flags --super/--copy-as 2.18, SCRAM daemon auth 2.19, packed metadata 2.20) and is intentionally out of scope — documented divergences from rsync's integer-negotiated downgrade remain.
**Phase-1/2 selection-and-update status correction (docs):** `-I/--ignore-times`, `--size-only`, `-@/--modify-window`, `--existing`, `--ignore-existing`, `-u/--update`, `-W/--whole-file`, and `--compress-threads` were previously listed as not-implemented in this document but are in fact fully implemented and tested on `dev`. This pass corrects the matrix to match the code. The realistic model of these is that FastSync is a *sender-driven* whole-tree copy, so the size+mtime quick-check and all three receiver-policy skips (`--existing`, `--ignore-existing`, `-u`) are evaluated against the **destination** on the receiver side, and their booleans cross the wire in the config frame. `-I`/`--size-only`/`--modify-window` modify the `--incremental` per-file `STATUS_CHECK` handshake's match predicate (`-I` disables the mtime leg and forces transfer; `--size-only` drops only the mtime leg; `--modify-window` adds tolerance to `metadata_mtime_matches`); they require `--incremental` (or a basis dir) to have a handshake to affect, mirroring how they only matter where a quick-check exists in rsync. `--existing`/`--ignore-existing`/`-u` are receiver write-time policies (skipping the write / newer-destination guard) applied across the regular-file, `--delay-updates`-staged, hardlink-sibling, and special/device paths; `-u` implies `-M` metadata and uses a second-then-nanosecond strict `>` newer check; both correctly influence `--remove-source-files` (a skipped source is not removed). `-W/--whole-file` disables block-level delta (opt-in via `--delta`), folded into the wire `use_delta` so no protocol bump was needed, and makes `--fuzzy` inert; `--append`/`--append-verify` are rejected with `-W`. `--compress-threads=NUM` (1..64, client-only, never crosses the wire) sizes the zstd compression worker pool. No code was changed by this correction; the implementation had landed in earlier merge waves (feat/ignore-times, feat/ignore-existing via the newer `file_to_disk_secure_no_replace`/`linkat EEXIST` path, feat/size-only, feat/modify-window, feat/whole-file, feat/update, compression-threads). **Phase-1/2 selection-and-update status correction (docs):** `-I/--ignore-times`, `--size-only`, `-@/--modify-window`, `--existing`, `--ignore-existing`, `-u/--update`, `-W/--whole-file`, and `--compress-threads` were previously listed as not-implemented in this document but are in fact fully implemented and tested on `dev`. This pass corrects the matrix to match the code. The realistic model of these is that FastSync is a *sender-driven* whole-tree copy, so the size+mtime quick-check and all three receiver-policy skips (`--existing`, `--ignore-existing`, `-u`) are evaluated against the **destination** on the receiver side, and their booleans cross the wire in the config frame. `-I`/`--size-only`/`--modify-window` modify the `--incremental` per-file `STATUS_CHECK` handshake's match predicate (`-I` disables the mtime leg and forces transfer; `--size-only` drops only the mtime leg; `--modify-window` adds tolerance to `metadata_mtime_matches`); they require `--incremental` (or a basis dir) to have a handshake to affect, mirroring how they only matter where a quick-check exists in rsync. `--existing`/`--ignore-existing`/`-u` are receiver write-time policies (skipping the write / newer-destination guard) applied across the regular-file, `--delay-updates`-staged, hardlink-sibling, and special/device paths; `-u` implies `-M` metadata and uses a second-then-nanosecond strict `>` newer check; both correctly influence `--remove-source-files` (a skipped source is not removed). `-W/--whole-file` disables block-level delta (opt-in via `--delta`), folded into the wire `use_delta` so no protocol bump was needed, and makes `--fuzzy` inert; `--append`/`--append-verify` are rejected with `-W`. `--compress-threads=NUM` (1..64, client-only, never crosses the wire) sizes the zstd compression worker pool. No code was changed by this correction; the implementation had landed in earlier merge waves (feat/ignore-times, feat/ignore-existing via the newer `file_to_disk_secure_no_replace`/`linkat EEXIST` path, feat/size-only, feat/modify-window, feat/whole-file, feat/update, compression-threads).
@@ -838,6 +840,23 @@ These are the last compatibility items and the closing phase toward rsync flag p
**Post-Phase-7 Summary (after Waves A–E).** ✅143 / 🔀0 / ⛔4 / ⚠️0 / 🔄0 / ❌0 = 147. The 3 `🔀 Alt Arg` rows (`-a`, `-p`, `-z`) are ✅ (Wave A). All 10 prior `⚠️ Partial` rows are resolved to ✅ (`-S`, `-P`, `--block-size`, `--fake-super`, `--devices`, `--copy-devices`, `--write-devices`) or ⛔ (`--stderr=client`, `-N/--crtimes`, `--specials` for the impossible socket case). The 3 `🔄 Compatibility No-op` rows are resolved: `-O`/`-J` are now real ✅ (Wave D), `--secluded-args` is ⛔. The **Impossible/Divergence** bucket holds the 4 physically-impossible/divergent flags: `--stderr=client`, `-N/--crtimes`, `--specials` (sockets), `--secluded-args`. The last two `❌ Not Implemented` rows — `--super` and `--copy-as=USER[:GROUP]` — are now ✅ (Wave E). **No `❌ Not Implemented` rows remain.** **Post-Phase-7 Summary (after Waves A–E).** ✅143 / 🔀0 / ⛔4 / ⚠️0 / 🔄0 / ❌0 = 147. The 3 `🔀 Alt Arg` rows (`-a`, `-p`, `-z`) are ✅ (Wave A). All 10 prior `⚠️ Partial` rows are resolved to ✅ (`-S`, `-P`, `--block-size`, `--fake-super`, `--devices`, `--copy-devices`, `--write-devices`) or ⛔ (`--stderr=client`, `-N/--crtimes`, `--specials` for the impossible socket case). The 3 `🔄 Compatibility No-op` rows are resolved: `-O`/`-J` are now real ✅ (Wave D), `--secluded-args` is ⛔. The **Impossible/Divergence** bucket holds the 4 physically-impossible/divergent flags: `--stderr=client`, `-N/--crtimes`, `--specials` (sockets), `--secluded-args`. The last two `❌ Not Implemented` rows — `--super` and `--copy-as=USER[:GROUP]` — are now ✅ (Wave E). **No `❌ Not Implemented` rows remain.**
## Packed Metadata Frame (protocol 2.20.0)
A file's metadata used to cross the wire as up to 12 separate per-field framed
messages (a present flag followed by mode/uid/gid/mtime/atime/crtime writes),
which cost ~11 extra protocol frames per file on many-small-file trees. FastSync
now sends the metadata as ONE packed frame: a single `int32` present flag
(`0` = absent) followed, when present, by the fixed
`FILE_METADATA_WIRE_SIZE`-byte (68-byte) field record already emitted by the
shared `metadata_to_buf()`/`metadata_from_buf()` chunk codec. Absent metadata is
a lone `int32` zero. The encoded field layout is unchanged (only the framing
collapses), so chunk-serialized blobs remain byte-identical. Protocol data is an
unframed byte stream, so the packed encoding is byte-for-byte identical to the
old field-by-field writes; `PROTOCOL_VERSION` was bumped `2.19.0 → 2.20.0` as a
deliberate lockstep-release marker rather than because of a
desynchronization. The strict same-version handshake rejects any mismatch before
a byte of the frame is parsed.
### Recommended Delivery Order ### Recommended Delivery Order
1. Resolve short-option conflicts (`-m`, `-M`, `-T`, `-f`, `-s`) and define the compatibility contract. 1. Resolve short-option conflicts (`-m`, `-M`, `-T`, `-f`, `-s`) and define the compatibility contract.
@@ -874,7 +893,7 @@ Ranked by user demand, implementation complexity, and interoperability impact (_
| Feature | Description | | Feature | Description |
|---------|-------------| |---------|-------------|
| `-j` / `--threads` | Multithreaded pipeline (scanner/loader/sender) (renamed from `-m` in Phase 7 Wave A; `-m` is now rsync `--prune-empty-dirs`) | | `-j` / `--threads[=N]` | Multithreaded pipeline (scanner/loader/sender); `N` (1–256) sizes the parallel scanner worker pool, bare `-j`/`--threads` uses the built-in default (renamed from `-m` in Phase 7 Wave A; `-m` is now rsync `--prune-empty-dirs`) |
| `--chunk-serialization` | Chunk serialization mode (long form only; `-s` is now rsync `--secluded-args`) | | `--chunk-serialization` | Chunk serialization mode (long form only; `-s` is now rsync `--secluded-args`) |
| `--sendfile` | Zero-copy sendfile() syscall (TCP only) (long form only; `-f` is now rsync `--filter`) | | `--sendfile` | Zero-copy sendfile() syscall (TCP only) (long form only; `-f` is now rsync `--filter`) |
| `-z [level]` / `--compress` | zstd compression level (1-22) (`-c` is now rsync `--checksum`) | | `-z [level]` / `--compress` | zstd compression level (1-22) (`-c` is now rsync `--checksum`) |
+29 -21
View File
@@ -26,7 +26,7 @@ import time
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
BUILD_DIR = os.path.join(PROJECT_ROOT, "build") BUILD_DIR = os.path.join(PROJECT_ROOT, "build")
SERVER_CMD = [os.path.join(BUILD_DIR, "server")] SERVER_CMD = [os.path.join(BUILD_DIR, "server"), "--allow-unauthenticated"]
CLIENT_CMD = [os.path.join(BUILD_DIR, "client")] CLIENT_CMD = [os.path.join(BUILD_DIR, "client")]
BENCH_DIR = os.path.join(PROJECT_ROOT, "bench_data") BENCH_DIR = os.path.join(PROJECT_ROOT, "bench_data")
@@ -43,11 +43,12 @@ NETWORK_PROFILES = {
} }
FASTSYNC_CONFIGS = [ FASTSYNC_CONFIGS = [
{"name": "fastsync", "flags": [], "tool": "fastsync"}, {"name": "fastsync", "flags": [], "tool": "fastsync"},
{"name": "fastsync -c", "flags": ["-c"], "tool": "fastsync"}, {"name": "fastsync -z", "flags": ["-z"], "tool": "fastsync"},
{"name": "fastsync -m", "flags": ["-m"], "tool": "fastsync"}, {"name": "fastsync -j", "flags": ["-j"], "tool": "fastsync"},
{"name": "fastsync -m -c", "flags": ["-m", "-c"], "tool": "fastsync"}, {"name": "fastsync -j -z", "flags": ["-j", "-z"], "tool": "fastsync"},
{"name": "fastsync -m -c -s", "flags": ["-m", "-c", "-s"], "tool": "fastsync"}, {"name": "fastsync -j -z --chunk-serialization", "flags": ["-j", "-z", "--chunk-serialization"], "tool": "fastsync"},
{"name": "fastsync --sendfile", "flags": ["--sendfile"], "tool": "fastsync"},
] ]
RSYNC_CONFIGS = [ RSYNC_CONFIGS = [
@@ -252,8 +253,10 @@ def run_fastsync(source_dir, dest_dir, flags, port):
duration = time.monotonic() - start duration = time.monotonic() - start
if result.returncode == 0: if result.returncode == 0:
return duration return duration
sys.stderr.write(f" fastsync failed (exit {result.returncode}): "
f"{result.stderr.strip()[:500]}\n")
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
pass sys.stderr.write(" fastsync timed out after 120s\n")
return None return None
@@ -269,8 +272,10 @@ def run_rsync(source_dir, dest_dir, flags, rsync_daemon=None):
duration = time.monotonic() - start duration = time.monotonic() - start
if result.returncode == 0: if result.returncode == 0:
return duration return duration
sys.stderr.write(f" rsync failed (exit {result.returncode}): "
f"{result.stderr.strip()[:500]}\n")
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
pass sys.stderr.write(" rsync timed out after 120s\n")
return None return None
@@ -367,15 +372,15 @@ def print_table(results, total_bytes, random_ratio):
if fs_entries: if fs_entries:
print(f"\n FastSync:") print(f"\n FastSync:")
print(f" {'Config':<25} {'p50':>8} {'p95':>8} {'min':>8} {'max':>8} {'stdev':>8} {'runs':>5}") print(f" {'Config':<38} {'p50':>8} {'p95':>8} {'min':>8} {'max':>8} {'stdev':>8} {'runs':>5}")
print(f" {'-' * 25} {'-' * 8} {'-' * 8} {'-' * 8} {'-' * 8} {'-' * 8} {'-' * 5}") print(f" {'-' * 38} {'-' * 8} {'-' * 8} {'-' * 8} {'-' * 8} {'-' * 8} {'-' * 5}")
for e in sorted(fs_entries, key=lambda x: x.get("p50", 999)): for e in sorted(fs_entries, key=lambda x: x.get("p50", 999)):
_print_entry(e) _print_entry(e)
if rsync_entries: if rsync_entries:
print(f"\n rsync:") print(f"\n rsync:")
print(f" {'Config':<25} {'p50':>8} {'p95':>8} {'min':>8} {'max':>8} {'stdev':>8} {'runs':>5}") print(f" {'Config':<38} {'p50':>8} {'p95':>8} {'min':>8} {'max':>8} {'stdev':>8} {'runs':>5}")
print(f" {'-' * 25} {'-' * 8} {'-' * 8} {'-' * 8} {'-' * 8} {'-' * 8} {'-' * 5}") print(f" {'-' * 38} {'-' * 8} {'-' * 8} {'-' * 8} {'-' * 8} {'-' * 8} {'-' * 5}")
for e in sorted(rsync_entries, key=lambda x: x.get("p50", 999)): for e in sorted(rsync_entries, key=lambda x: x.get("p50", 999)):
_print_entry(e) _print_entry(e)
@@ -392,10 +397,10 @@ def print_table(results, total_bytes, random_ratio):
def _print_entry(e): def _print_entry(e):
if "p50" in e: if "p50" in e:
print(f" {e['config']:<25} {e['p50']:>7.4f}s {e['p95']:>7.4f}s " print(f" {e['config']:<38} {e['p50']:>7.4f}s {e['p95']:>7.4f}s "
f"{e['min']:>7.4f}s {e['max']:>7.4f}s {e['stdev']:>7.4f} {e['runs']:>5}") f"{e['min']:>7.4f}s {e['max']:>7.4f}s {e['stdev']:>7.4f} {e['runs']:>5}")
else: else:
print(f" {e['config']:<25} {'N/A':>8} {'N/A':>8} {'N/A':>8} {'N/A':>8} {'N/A':>8} {e['runs']:>5}") print(f" {e['config']:<38} {'N/A':>8} {'N/A':>8} {'N/A':>8} {'N/A':>8} {'N/A':>8} {e['runs']:>5}")
def main(): def main():
@@ -448,12 +453,14 @@ Examples:
help="Don't clean up test data") help="Don't clean up test data")
args = parser.parse_args() args = parser.parse_args()
# Build # Build (Release: benchmarking a debug build is meaningless)
print("Building...") print("Building (Release)...", file=sys.stderr)
if os.system(f"cmake -B {BUILD_DIR} -S {PROJECT_ROOT} > /dev/null 2>&1") != 0: configure = (f"cmake -B {BUILD_DIR} -S {PROJECT_ROOT} "
print("CMake configure failed"); sys.exit(1) f"-DCMAKE_BUILD_TYPE=Release > /dev/null 2>&1")
if os.system(configure) != 0:
print("CMake configure failed", file=sys.stderr); sys.exit(1)
if os.system(f"cmake --build {BUILD_DIR} -j$(nproc) > /dev/null 2>&1") != 0: if os.system(f"cmake --build {BUILD_DIR} -j$(nproc) > /dev/null 2>&1") != 0:
print("Build failed"); sys.exit(1) print("Build failed", file=sys.stderr); sys.exit(1)
# Determine active profile for display # Determine active profile for display
has_custom_net = args.delay or args.jitter or args.throughput or args.loss has_custom_net = args.delay or args.jitter or args.throughput or args.loss
@@ -480,7 +487,8 @@ Examples:
compressible_pct = (1 - args.random_ratio) * 100 compressible_pct = (1 - args.random_ratio) * 100
random_pct = args.random_ratio * 100 random_pct = args.random_ratio * 100
print(f"Generated {total_bytes / (1024*1024):.1f} MB " print(f"Generated {total_bytes / (1024*1024):.1f} MB "
f"({random_pct:.0f}% random, {compressible_pct:.0f}% compressible)") f"({random_pct:.0f}% random, {compressible_pct:.0f}% compressible)",
file=sys.stderr)
# Build config list # Build config list
if args.configs: if args.configs:
@@ -496,7 +504,7 @@ Examples:
total_runs = len(configs) * args.runs * len(profiles_to_run) total_runs = len(configs) * args.runs * len(profiles_to_run)
progress = Progress(total_runs, "Benchmarking") if args.progress else None progress = Progress(total_runs, "Benchmarking") if args.progress else None
if progress: if progress:
print(f"Running {total_runs} transfers...") print(f"Running {total_runs} transfers...", file=sys.stderr)
all_results = [] all_results = []
try: try:
+1090 -630
View File
File diff suppressed because it is too large. Load diff
+128 -41
View File
@@ -37,6 +37,17 @@
#define STREAM_THRESHOLD (64ULL * 1024 * 1024) #define STREAM_THRESHOLD (64ULL * 1024 * 1024)
/* Aggregate loaded payload bytes the sender may buffer across the loader queue
and the chunk in flight. Sending one chunk adds up to ~2 * MAX_CHUNK_SIZE of
transient serialize/compress buffers on top of the queued payloads, so this
ceiling keeps total pipeline memory within MAX_CONNECTION_MEMORY (mirrors the
receiver's RECEIVER_QUEUE_MAX_BYTES). */
#define SENDER_QUEUE_MAX_BYTES (MAX_CONNECTION_MEMORY - 2 * MAX_CHUNK_SIZE)
/* One mebibyte in bytes; the unit used by the --stats/--progress lines.
Always cast to double when dividing so the output stays fractional. */
#define BYTES_PER_MIB (1024ULL * 1024ULL)
/* Forward declaration for progress-reporting thread used in multithreaded send. */ /* Forward declaration for progress-reporting thread used in multithreaded send. */
static int progress_thread_fn(void* arg); static int progress_thread_fn(void* arg);
@@ -44,10 +55,32 @@ static const char* display_bytes(unsigned long long bytes, bool human_readable,
size_t buffer_size) { size_t buffer_size) {
if (human_readable && format_human_bytes(bytes, buffer, buffer_size)) if (human_readable && format_human_bytes(bytes, buffer, buffer_size))
return buffer; return buffer;
snprintf(buffer, buffer_size, "%.1f MB", bytes / 1048576.0); snprintf(buffer, buffer_size, "%.1f MB", (double)bytes / (double)BYTES_PER_MIB);
return buffer; return buffer;
} }
/* Print the canonical `--stats` line. Shared by the single-threaded and
multithreaded send paths so both honor --stats, --human-readable and --quiet
identically; `start` marks the beginning of the transfer for the rate. */
static void report_transfer_stats(const Config* config, int total_files,
unsigned long long total_bytes, time_t start) {
if (!config->stats || config->quiet)
return;
double elapsed = difftime(time(NULL), start);
double rate = elapsed > 0.0 ? (double)total_bytes / ((double)BYTES_PER_MIB * elapsed) : 0.0;
if (config->human_readable) {
char total_buffer[32];
char rate_buffer[32];
fprintf(stderr, "Stats: %d files, %s, %s/s\n", total_files,
display_bytes(total_bytes, true, total_buffer, sizeof(total_buffer)),
display_bytes((unsigned long long)(rate * (double)BYTES_PER_MIB), true, rate_buffer,
sizeof(rate_buffer)));
} else {
fprintf(stderr, "Stats: %d files, %.1f MB, %.1f MB/s\n", total_files,
(double)total_bytes / (double)BYTES_PER_MIB, rate);
}
}
/* Compiled scanner inputs that are shared read-only across scanner instances /* Compiled scanner inputs that are shared read-only across scanner instances
* and, in -m mode, across worker threads. `base_filters` owns the compiled * and, in -m mode, across worker threads. `base_filters` owns the compiled
* command-line + -C rules; the FileListSet allow-set lives in the Config. * command-line + -C rules; the FileListSet allow-set lives in the Config.
@@ -338,9 +371,10 @@ static bool basis_oversize_preflight(const Config* config) {
return false; return false;
DirectoryScanner* scanner = DirectoryScanner* scanner =
directory_scanner_create_with_options(config->send_directory, &prepared.options); directory_scanner_create_with_options(config->send_directory, &prepared.options);
prepared_scanner_destroy(&prepared); if (!scanner) {
if (!scanner) prepared_scanner_destroy(&prepared);
return false; return false;
}
bool ok = true; bool ok = true;
Chunk* chunk; Chunk* chunk;
while ((chunk = directory_scanner_next(scanner)) != NULL) { while ((chunk = directory_scanner_next(scanner)) != NULL) {
@@ -364,7 +398,10 @@ static bool basis_oversize_preflight(const Config* config) {
} }
if (directory_scanner_failed(scanner) || directory_scanner_had_io_error(scanner)) if (directory_scanner_failed(scanner) || directory_scanner_had_io_error(scanner))
ok = false; ok = false;
/* The scanner borrows prepared.options' base_filters/hardlinks pointers, so
prepared must outlive the scanner. */
directory_scanner_destroy(scanner); directory_scanner_destroy(scanner);
prepared_scanner_destroy(&prepared);
return ok; return ok;
} }
@@ -677,7 +714,7 @@ static int send_dry_run_manifest(const Config* config) {
printf("Total: %d files, %s\n", file_count, printf("Total: %d files, %s\n", file_count,
display_bytes(total_bytes, true, size_buffer, sizeof(size_buffer))); display_bytes(total_bytes, true, size_buffer, sizeof(size_buffer)));
else else
printf("Total: %d files, %.1f MB\n", file_count, total_bytes / 1048576.0); printf("Total: %d files, %.1f MB\n", file_count, (double)total_bytes / (double)BYTES_PER_MIB);
} }
return 0; return 0;
} }
@@ -848,6 +885,10 @@ static int send_delete_manifest(int fd, ArrayList* manifest, ArrayList* protecte
unlinks) before replying, so the wait uses a generous explicit deadline unlinks) before replying, so the wait uses a generous explicit deadline
instead of the default 60 s receive window. */ instead of the default 60 s receive window. */
#define DELETE_ACK_TIMEOUT_SEC 3600 #define DELETE_ACK_TIMEOUT_SEC 3600
/* While waiting for the (potentially slow) receiver-side deletion, send a
* STATUS_KEEPALIVE at most this often so the connection is demonstrably alive
* and neither side's per-message timeout trips. */
#define DELETE_ACK_KEEPALIVE_SEC 10
static bool send_delete_manifest_early(Client* client, ArrayList* manifest, static bool send_delete_manifest_early(Client* client, ArrayList* manifest,
ArrayList* protected_prefixes, ArrayList* missing_args) { ArrayList* protected_prefixes, ArrayList* missing_args) {
@@ -857,8 +898,21 @@ static bool send_delete_manifest_early(Client* client, ArrayList* manifest,
0) 0)
return false; return false;
Status ack; Status ack;
if (!receive_status_timed(client->file_descriptor, &ack, DELETE_ACK_TIMEOUT_SEC)) /* The wait is long (up to an hour) and runs inline on this thread: a helper
* thread would race the non-thread-safe protocol send path, so keepalives are
* emitted from this wait loop itself. A Ctrl-C/SIGTERM abort flag also ends
* the wait; the caller then best-effort sends STATUS_ABORT. */
if (!receive_status_keepalive(client->file_descriptor, &ack, DELETE_ACK_TIMEOUT_SEC,
DELETE_ACK_KEEPALIVE_SEC, client_abort_pending)) {
/* A Ctrl-C/SIGTERM abort ends the wait above; tell the receiver before the
caller tears the connection down (best-effort). */
if (client_abort_pending()) {
log_info_message(LOG_INFO_MISC,
"Abort requested while awaiting delete ack; sending STATUS_ABORT");
send_status(client->file_descriptor, STATUS_ABORT);
}
return false; return false;
}
if (ack != STATUS_OK) { if (ack != STATUS_OK) {
log_message(LOG_LEVEL_ERROR, "Server failed to delete files before the transfer"); log_message(LOG_LEVEL_ERROR, "Server failed to delete files before the transfer");
return false; return false;
@@ -1414,12 +1468,9 @@ static int send_chunk_with_removal(Client* client, Chunk* chunk, Config* config,
return 0; return 0;
} }
int send_chunk(Client* client, Chunk* chunk, Config* config) {
return send_chunk_with_removal(client, chunk, config, NULL);
}
static int send_chunks_multithreaded(void* pipeline_context) { static int send_chunks_multithreaded(void* pipeline_context) {
PipelineContextSender* context = (PipelineContextSender*)pipeline_context; PipelineContextSender* context = (PipelineContextSender*)pipeline_context;
time_t start = time(NULL);
Client* client = connect_transfer_client(context->config); Client* client = connect_transfer_client(context->config);
if (!client) { if (!client) {
if (context->config->transport == TRANSPORT_TCP) if (context->config->transport == TRANSPORT_TCP)
@@ -1431,6 +1482,7 @@ static int send_chunks_multithreaded(void* pipeline_context) {
} }
ProtocolSession session; ProtocolSession session;
protocol_session_init(&session, client->file_descriptor, client->file_descriptor); protocol_session_init(&session, client->file_descriptor, client->file_descriptor);
protocol_session_set_io_timeout(&session, context->config->timeout);
protocol_session_set_ssl(&session, (SSL*)client->ssl); protocol_session_set_ssl(&session, (SSL*)client->ssl);
protocol_session_bind(&session); protocol_session_bind(&session);
if (!config_send(client->file_descriptor, context->config)) { if (!config_send(client->file_descriptor, context->config)) {
@@ -1455,6 +1507,19 @@ static int send_chunks_multithreaded(void* pipeline_context) {
} }
while (true) { while (true) {
/* Graceful abort (Ctrl-C/SIGTERM): tell the receiver to clean up instead of
dying abruptly. Best-effort: a failed send just means the peer is gone.
Only reached while the session is active (config_send already succeeded). */
if (client_abort_pending()) {
log_info_message(LOG_INFO_MISC,
"Abort requested; sending STATUS_ABORT to server and disconnecting");
send_status(client->file_descriptor, STATUS_ABORT);
pipeline_cancel(context);
disconnect_transfer_client(client);
mark_sender_done(context);
protocol_session_unbind();
return thrd_error;
}
/* Phase 6: stop-elegantly at the next chunk boundary once the --stop-after /* Phase 6: stop-elegantly at the next chunk boundary once the --stop-after
/ --stop-at deadline has passed. Everything already sent is finalized by / --stop-at deadline has passed. Everything already sent is finalized by
the completion tail below; the run still returns success. */ the completion tail below; the run still returns success. */
@@ -1488,6 +1553,10 @@ static int send_chunks_multithreaded(void* pipeline_context) {
protocol_session_unbind(); protocol_session_unbind();
return thrd_error; return thrd_error;
} }
/* Payload bytes this chunk was charged for on the loader's byte budget.
Computed before destruction and released after the memory is actually
freed, so a loader blocked on the budget wakes only once room exists. */
size_t queued_payload = pipeline_context_sender_chunk_bytes(current_chunk);
unsigned long long chunk_bytes = 0; unsigned long long chunk_bytes = 0;
int chunk_files = 0; int chunk_files = 0;
for (int i = 0; i < current_chunk->element_count; i++) { for (int i = 0; i < current_chunk->element_count; i++) {
@@ -1502,6 +1571,7 @@ static int send_chunks_multithreaded(void* pipeline_context) {
context->progress_bytes = context->total_bytes; context->progress_bytes = context->total_bytes;
mtx_unlock(&context->mutex_progress); mtx_unlock(&context->mutex_progress);
chunk_destroy(current_chunk); chunk_destroy(current_chunk);
pipeline_context_sender_note_bytes_released(context, queued_payload);
} }
/* Completion tail: reached on natural exhaustion or an early stop deadline. /* Completion tail: reached on natural exhaustion or an early stop deadline.
@@ -1561,10 +1631,9 @@ static int send_chunks_multithreaded(void* pipeline_context) {
int total_files = context->total_files; int total_files = context->total_files;
unsigned long long total_bytes = context->total_bytes; unsigned long long total_bytes = context->total_bytes;
mtx_unlock(&context->mutex_progress); mtx_unlock(&context->mutex_progress);
if (context->config->stats) report_transfer_stats(context->config, total_files, total_bytes, start);
fprintf(stderr, "Stats: %d files, %.1f MB\n", total_files, total_bytes / 1048576.0);
log_info_message(LOG_INFO_STATS, "Transfer summary: %d files, %.1f MB", total_files, log_info_message(LOG_INFO_STATS, "Transfer summary: %d files, %.1f MB", total_files,
total_bytes / 1048576.0); (double)total_bytes / (double)BYTES_PER_MIB);
disconnect_transfer_client(client); disconnect_transfer_client(client);
mark_sender_done(context); mark_sender_done(context);
protocol_session_unbind(); protocol_session_unbind();
@@ -1585,7 +1654,9 @@ static int scan_directory_multithreaded(void* pipeline_context) {
PipelineContextSender* context = (PipelineContextSender*)pipeline_context; PipelineContextSender* context = (PipelineContextSender*)pipeline_context;
protocol_session_bind(&context->allocation_session); protocol_session_bind(&context->allocation_session);
PreparedScanner prepared; PreparedScanner prepared;
if (!prepare_scanner(context->config, 4, &prepared)) { /* -j/--threads=N sizes the parallel scanner's worker pool; 0 (bare -j) lets
* the scanner apply its built-in default. */
if (!prepare_scanner(context->config, context->config->scanner_threads, &prepared)) {
pipeline_cancel(context); pipeline_cancel(context);
protocol_session_unbind(); protocol_session_unbind();
return thrd_error; return thrd_error;
@@ -1723,11 +1794,7 @@ static int load_files_multithreaded(void* pipeline_context) {
} }
} }
} }
if (!queue_enqueue_multithreaded_cancel(context->queue_loader, chunk, &context->mutex_loader, if (!pipeline_context_sender_enqueue_chunk(context, chunk)) {
&context->condition_not_empty_loader,
&context->condition_not_full_loader,
&context->cancelled)) {
chunk_destroy(chunk);
pipeline_cancel(context); pipeline_cancel(context);
protocol_session_unbind(); protocol_session_unbind();
return thrd_error; return thrd_error;
@@ -1741,17 +1808,18 @@ static int load_files_multithreaded(void* pipeline_context) {
static void print_transfer_progress(unsigned long long total_bytes, time_t start, static void print_transfer_progress(unsigned long long total_bytes, time_t start,
const char* suffix, bool human_readable) { const char* suffix, bool human_readable) {
double elapsed = difftime(time(NULL), start); double elapsed = difftime(time(NULL), start);
double rate = elapsed > 0.0 ? total_bytes / (1048576.0 * elapsed) : 0.0; double rate = elapsed > 0.0 ? (double)total_bytes / ((double)BYTES_PER_MIB * elapsed) : 0.0;
if (human_readable) { if (human_readable) {
char total_buffer[32]; char total_buffer[32];
char rate_buffer[32]; char rate_buffer[32];
fprintf(stderr, "\rSent %s (%s/s) %s", fprintf(stderr, "\rSent %s (%s/s) %s",
display_bytes(total_bytes, true, total_buffer, sizeof(total_buffer)), display_bytes(total_bytes, true, total_buffer, sizeof(total_buffer)),
display_bytes((unsigned long long)(rate * 1048576.0), true, rate_buffer, display_bytes((unsigned long long)(rate * (double)BYTES_PER_MIB), true, rate_buffer,
sizeof(rate_buffer)), sizeof(rate_buffer)),
suffix); suffix);
} else { } else {
fprintf(stderr, "\rSent %.1f MB (%.1f MB/s) %s", total_bytes / 1048576.0, rate, suffix); fprintf(stderr, "\rSent %.1f MB (%.1f MB/s) %s", (double)total_bytes / (double)BYTES_PER_MIB,
rate, suffix);
} }
fflush(stderr); fflush(stderr);
} }
@@ -1881,15 +1949,21 @@ int send_files(Config* config) {
return 1; return 1;
} }
/* From here on a server session may be live, so Ctrl-C/SIGTERM should set the
abort flag (and be forwarded as STATUS_ABORT) instead of terminating. */
client_set_abort_armed(true);
Client* client = connect_transfer_client(config); Client* client = connect_transfer_client(config);
if (!client) { if (!client) {
if (config->transport == TRANSPORT_TCP) if (config->transport == TRANSPORT_TCP)
log_message(LOG_LEVEL_ERROR, "could not connect to server%s", log_message(LOG_LEVEL_ERROR, "could not connect to server%s",
config->use_tls ? " via TLS" : ""); config->use_tls ? " via TLS" : "");
if (missing_args)
array_list_delete(missing_args);
return 1; return 1;
} }
ProtocolSession session; ProtocolSession session;
protocol_session_init(&session, client->file_descriptor, client->file_descriptor); protocol_session_init(&session, client->file_descriptor, client->file_descriptor);
protocol_session_set_io_timeout(&session, config->timeout);
protocol_session_set_ssl(&session, (SSL*)client->ssl); protocol_session_set_ssl(&session, (SSL*)client->ssl);
protocol_session_bind(&session); protocol_session_bind(&session);
int ret = 1; int ret = 1;
@@ -1997,6 +2071,15 @@ int send_files(Config* config) {
only a prefix of the source. */ only a prefix of the source. */
bool scan_stopped_early = false; bool scan_stopped_early = false;
while ((current_chunk = directory_scanner_next(scanner)) != NULL) { while ((current_chunk = directory_scanner_next(scanner)) != NULL) {
/* Graceful abort (Ctrl-C/SIGTERM): notify the receiver and clean up. The
session is active (config_send already succeeded); a send failure here is
fine because the client is exiting anyway. */
if (client_abort_pending()) {
log_info_message(LOG_INFO_MISC, "Abort requested; sending STATUS_ABORT to server");
chunk_destroy(current_chunk);
send_status(client->file_descriptor, STATUS_ABORT);
goto send_fail;
}
/* Phase 6: stop-elegantly at the next chunk boundary once the deadline has /* Phase 6: stop-elegantly at the next chunk boundary once the deadline has
passed. The scanner may also have stopped early itself; either way the passed. The scanner may also have stopped early itself; either way the
completion tail below keeps everything already sent. */ completion tail below keeps everything already sent. */
@@ -2060,6 +2143,13 @@ int send_files(Config* config) {
goto send_fail; goto send_fail;
if (directory_scanner_had_io_error(scanner)) if (directory_scanner_had_io_error(scanner))
had_scan_io = true; had_scan_io = true;
/* An abort that arrived after the last chunk must still stop the completion
tail (manifest/finalize) rather than let it run to success. */
if (client_abort_pending()) {
log_info_message(LOG_INFO_MISC, "Abort requested; sending STATUS_ABORT to server");
send_status(client->file_descriptor, STATUS_ABORT);
goto send_fail;
}
/* Phase 6: the scanner may have stopped early (returning NULL without a /* Phase 6: the scanner may have stopped early (returning NULL without a
failure) as soon as the deadline passed, so reflect that here too. A failure) as soon as the deadline passed, so reflect that here too. A
deadline that cut the scan short leaves an incomplete keep-set; transmitting deadline that cut the scan short leaves an incomplete keep-set; transmitting
@@ -2116,23 +2206,9 @@ int send_files(Config* config) {
remove_transferred_sources(config, remove_sources); remove_transferred_sources(config, remove_sources);
if (config->show_progress && !config->quiet) if (config->show_progress && !config->quiet)
print_transfer_progress(total_bytes, start, "Done.\n", config->human_readable); print_transfer_progress(total_bytes, start, "Done.\n", config->human_readable);
if (config->stats && !config->quiet) { report_transfer_stats(config, total_files, total_bytes, start);
double elapsed_total = difftime(time(NULL), start);
double rate = elapsed_total > 0 ? total_bytes / (1048576.0 * elapsed_total) : 0;
if (config->human_readable) {
char total_buffer[32];
char rate_buffer[32];
fprintf(stderr, "Stats: %d files, %s, %s/s\n", total_files,
display_bytes(total_bytes, true, total_buffer, sizeof(total_buffer)),
display_bytes((unsigned long long)(rate * 1048576.0), true, rate_buffer,
sizeof(rate_buffer)));
} else {
fprintf(stderr, "Stats: %d files, %.1f MB, %.1f MB/s\n", total_files, total_bytes / 1048576.0,
rate);
}
}
log_info_message(LOG_INFO_STATS, "Transfer summary: %d files, %.1f MB", total_files, log_info_message(LOG_INFO_STATS, "Transfer summary: %d files, %.1f MB", total_files,
total_bytes / 1048576.0); (double)total_bytes / (double)BYTES_PER_MIB);
/* --ignore-errors: an unreadable source directory was skipped but the run /* --ignore-errors: an unreadable source directory was skipped but the run
still completed (and deleted); report the run as errored like rsync does. */ still completed (and deleted); report the run as errored like rsync does. */
ret = (ok && !had_scan_io) ? 0 : 1; ret = (ok && !had_scan_io) ? 0 : 1;
@@ -2155,6 +2231,7 @@ send_fail:
prepared_scanner_destroy(&prepared); prepared_scanner_destroy(&prepared);
disconnect_transfer_client(client); disconnect_transfer_client(client);
protocol_session_unbind(); protocol_session_unbind();
client_set_abort_armed(false);
return ret; return ret;
} }
@@ -2184,13 +2261,21 @@ int send_files_multithreaded(Config** config_ptr) {
return 1; return 1;
} }
/* Armed only once a session may go live (see send_files). */
client_set_abort_armed(true);
long pages = sysconf(_SC_AVPHYS_PAGES); long pages = sysconf(_SC_AVPHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE); long page_size = sysconf(_SC_PAGE_SIZE);
unsigned long long available_memory = unsigned long long available_memory =
pages > 0 && page_size > 0 ? (unsigned long long)pages * (unsigned long long)page_size pages > 0 && page_size > 0 ? (unsigned long long)pages * (unsigned long long)page_size
: 512ULL * 1024 * 1024; : 512ULL * 1024 * 1024;
unsigned long long avg_file_size = 1024 * 1024; /* Size the chunk queues from the actual chunk size rather than a fixed 1 MiB
int qsize = (int)(available_memory / avg_file_size); average: a chunk holds roughly `chunk_size` bytes of file data, so counting
chunks at 1 MiB over-estimated the queue capacity by up to 10x. The byte
budget below is the authoritative bound; this count keeps the unloaded
chunks waiting in the scanner queue bounded too. */
unsigned long long chunk_size = config->chunk_size > 0 ? config->chunk_size : DEFAULT_CHUNK_SIZE;
int qsize = (int)(available_memory / chunk_size);
if (qsize < 10) if (qsize < 10)
qsize = 10; qsize = 10;
if (qsize > 1000) if (qsize > 1000)
@@ -2215,7 +2300,8 @@ int send_files_multithreaded(Config** config_ptr) {
} }
context->missing_args = missing_args; context->missing_args = missing_args;
missing_args = NULL; /* owned by the context from here on */ missing_args = NULL; /* owned by the context from here on */
*config_ptr = NULL; /* context now owns config through all remaining paths */ pipeline_context_sender_set_queue_byte_limit(context, SENDER_QUEUE_MAX_BYTES);
/* The context borrows `config`; the caller (main) still owns and frees it. */
struct timespec now_mono; struct timespec now_mono;
if (clock_gettime(CLOCK_MONOTONIC, &now_mono) != 0) { if (clock_gettime(CLOCK_MONOTONIC, &now_mono) != 0) {
now_mono.tv_sec = 0; now_mono.tv_sec = 0;
@@ -2245,7 +2331,7 @@ int send_files_multithreaded(Config** config_ptr) {
fills the protected excluded prefixes. */ fills the protected excluded prefixes. */
PreparedScanner prepared; PreparedScanner prepared;
memset(&prepared, 0, sizeof(prepared)); memset(&prepared, 0, sizeof(prepared));
bool prepared_ok = prepare_scanner(config, 4, &prepared); bool prepared_ok = prepare_scanner(config, config->scanner_threads, &prepared);
if (prepared_ok && context->excluded_paths) if (prepared_ok && context->excluded_paths)
prepared.options.excluded_paths = context->excluded_paths; prepared.options.excluded_paths = context->excluded_paths;
bool prebuilt = prepared_ok && scan_paths_only(config, &prepared.options, context->manifest, bool prebuilt = prepared_ok && scan_paths_only(config, &prepared.options, context->manifest,
@@ -2332,5 +2418,6 @@ int send_files_multithreaded(Config** config_ptr) {
/* --ignore-errors: the run completed (and deleted) past an unreadable source /* --ignore-errors: the run completed (and deleted) past an unreadable source
directory; report it as errored like rsync does. */ directory; report it as errored like rsync does. */
pipeline_context_sender_destroy(context); pipeline_context_sender_destroy(context);
client_set_abort_armed(false);
return sender_ok && !scan_io ? 0 : 1; return sender_ok && !scan_io ? 0 : 1;
} }
+16 -2
View File
@@ -4,10 +4,24 @@
#include "chunk.h" #include "chunk.h"
#include "config.h" #include "config.h"
#include "transport_tcp.h" #include "transport_tcp.h"
#include <signal.h>
#include <stdbool.h>
int send_chunk(Client* client, Chunk* chunk, Config* config); /* Set ONLY by the client's SIGINT/SIGTERM handler (async-signal-safe: the
* handler stores 1 and does nothing else). The send loops poll it via
* client_abort_pending() and, when set, best-effort send STATUS_ABORT so the
* receiver can clean up before the client exits. */
extern volatile sig_atomic_t client_abort_requested;
bool client_abort_pending(void);
/* Arm/disarm abort handling around the network phase. While disarmed, a
* SIGINT/SIGTERM takes the default action (immediate termination) so local-only
* modes are not left unresponsive. Defined in client_cli.c. */
void client_set_abort_armed(bool armed);
/* Both sender entry points BORROW `config` for the duration of the call; they
* never free it, and the caller retains ownership (freeing it with
* config_delete() once the call returns). */
int send_files(Config* config); int send_files(Config* config);
/* Takes ownership only when *config is set to NULL on return. */
int send_files_multithreaded(Config** config); int send_files_multithreaded(Config** config);
/* Phase 6 residual-batch (client-only). See client_send.c. */ /* Phase 6 residual-batch (client-only). See client_send.c. */
int write_batch_from_source(const Config* config, const char* batch_path); int write_batch_from_source(const Config* config, const char* batch_path);
+6 -107
View File
@@ -1,6 +1,4 @@
#include "client_validation.h" #include "client_validation.h"
#include "charset.h"
#include "delay_updates.h"
#include "log.h" #include "log.h"
#include "usage.h" #include "usage.h"
#include "utils.h" #include "utils.h"
@@ -41,17 +39,6 @@ bool validate_config(const Config* config) {
print_usage(); print_usage();
return false; return false;
} }
if (config_has_basis(config) && config->use_chunk_serialization) {
log_message(LOG_LEVEL_ERROR,
"--compare-dest/--copy-dest/--link-dest require per-file incremental checks and "
"cannot be combined with -s (chunk serialization)");
return false;
}
if (config->use_sendfile && (config->use_chunk_serialization || config->use_compression)) {
log_message(LOG_LEVEL_ERROR, "-f/--sendfile cannot be combined with -c (compression) or -s "
"(chunk serialization)");
return false;
}
if (config->compression_threads > 0 && !config->use_compression) { if (config->compression_threads > 0 && !config->use_compression) {
log_message(LOG_LEVEL_ERROR, "--compress-threads requires compression (-c or -z)"); log_message(LOG_LEVEL_ERROR, "--compress-threads requires compression (-c or -z)");
return false; return false;
@@ -60,73 +47,11 @@ bool validate_config(const Config* config) {
log_message(LOG_LEVEL_ERROR, "-f/--sendfile is not supported with SSH transport"); log_message(LOG_LEVEL_ERROR, "-f/--sendfile is not supported with SSH transport");
return false; return false;
} }
if (config->use_incremental && config->use_chunk_serialization) {
log_message(LOG_LEVEL_ERROR, "--incremental is not supported with -s (chunk serialization)");
return false;
}
/* -4 and -6 are mutually exclusive: a socket address family cannot be both. */ /* -4 and -6 are mutually exclusive: a socket address family cannot be both. */
if (config->ipv4 && config->ipv6) { if (config->ipv4 && config->ipv6) {
log_message(LOG_LEVEL_ERROR, "-4/--ipv4 and -6/--ipv6 are mutually exclusive"); log_message(LOG_LEVEL_ERROR, "-4/--ipv4 and -6/--ipv6 are mutually exclusive");
return false; return false;
} }
if (config->skip_compress_set && config->use_chunk_serialization) {
log_message(LOG_LEVEL_ERROR,
"--skip-compress cannot be combined with -s (chunk serialization)");
return false;
}
if (config->use_delta && !config->whole_file && !config->use_incremental) {
log_message(LOG_LEVEL_ERROR, "--delta requires --incremental");
return false;
}
if (config->use_delta && !config->whole_file && config->use_chunk_serialization) {
log_message(LOG_LEVEL_ERROR, "--delta cannot be combined with -s (chunk serialization)");
return false;
}
if (config->use_delta && !config->whole_file && config->use_sendfile) {
log_message(LOG_LEVEL_ERROR, "--delta cannot be combined with -f (sendfile)");
return false;
}
/* --append / --append-verify resume a shorter existing destination by
transmitting only the tail. The resume needs the per-file STATUS_CHECK
handshake (so the dest length is learned), which chunk serialization -s
disables; and whole-file is the opposite intent (send everything), so the
two would silently make the resume pointless. Both are rejected up front
rather than silently degrading to a full transfer. */
if ((config->append || config->append_verify) && config->use_chunk_serialization) {
log_message(LOG_LEVEL_ERROR,
"--append/--append-verify require the per-file incremental check and cannot be "
"combined with -s (chunk serialization)");
return false;
}
if ((config->append || config->append_verify) && config->whole_file) {
log_message(LOG_LEVEL_ERROR,
"--append/--append-verify are incompatible with --whole-file (which forces a "
"full transfer)");
return false;
}
/* --hard-links/-H transmits each later group member as a dedicated per-file
STATUS_HARDLINK frame, which chunk serialization -s does not support; and a
hard-links sibling carries no payload, so the tail-resume of --append is
meaningless for it. Both combinations are rejected up front rather than
silently degrading. */
if (config->preserve_hard_links && config->use_chunk_serialization) {
log_message(LOG_LEVEL_ERROR,
"--hard-links/-H cannot be combined with -s (chunk serialization)");
return false;
}
/* -X/-A ride the per-file metadata frame; the buffer-based chunk-serialization
wire format does not carry the xattr block, so the pair is rejected up front
(mirroring -H + -s) rather than silently dropping attributes. */
if ((config->preserve_xattrs || config->preserve_acls) && config->use_chunk_serialization) {
log_message(LOG_LEVEL_ERROR,
"--xattrs/-X and --acls/-A cannot be combined with -s (chunk serialization)");
return false;
}
if (config->preserve_hard_links && (config->append || config->append_verify)) {
log_message(LOG_LEVEL_ERROR,
"--hard-links/-H cannot be combined with --append/--append-verify");
return false;
}
if (config->log_file_format && !config->log_file) { if (config->log_file_format && !config->log_file) {
log_message(LOG_LEVEL_ERROR, "--log-file-format requires --log-file"); log_message(LOG_LEVEL_ERROR, "--log-file-format requires --log-file");
return false; return false;
@@ -146,28 +71,12 @@ bool validate_config(const Config* config) {
log_message(LOG_LEVEL_ERROR, "sending daemon credentials to a non-local server requires --tls"); log_message(LOG_LEVEL_ERROR, "sending daemon credentials to a non-local server requires --tls");
return false; return false;
} }
if (config->delay_updates && config->inplace) { /* Every cross-field invariant the receiver enforces lives in one shared
log_message(LOG_LEVEL_ERROR, "--delay-updates does not work with --inplace"); predicate so the client and the server can never disagree. The client
return false; reports the specific reason here, before any network I/O. */
} const char* invariants_error = config_invariants_error(config);
if (config->delay_updates && delay_updates_staging_name_conflict(config->backup_dir)) { if (invariants_error) {
log_message(LOG_LEVEL_ERROR, log_message(LOG_LEVEL_ERROR, "%s", invariants_error);
"--backup-dir is reserved when --delay-updates is active (used for the internal "
"staging directory)");
return false;
}
if (!config_has_valid_delete_timing(config)) {
log_message(LOG_LEVEL_ERROR,
"--delete-before/--delete-during/--delete-delay/--delete-after select the delete "
"timing; at most one may be given and each implies --delete");
return false;
}
/* --iconv: reject a malformed CONVERT_SPEC or an unsupported charset name at
startup (a probe iconv_open is attempted), so a typo'd charset never fails
the run mid-transfer with per-file errors. */
if (!charset_spec_valid(config->iconv_spec)) {
log_message(LOG_LEVEL_ERROR,
"--iconv requires LOCAL[,REMOTE] charset names supported by iconv");
return false; return false;
} }
/* --protocol: FastSync has exactly one wire format, so the forced version /* --protocol: FastSync has exactly one wire format, so the forced version
@@ -180,15 +89,5 @@ bool validate_config(const Config* config) {
PROTOCOL_VERSION); PROTOCOL_VERSION);
return false; return false;
} }
/* --copy-as pushes the source ids through the metadata path (it implies
--preserve). A later --no-preserve would clear use_metadata, leaving the
transfer with nothing to chown while the receiver gate would still pass.
Refuse the combination up front rather than silently chowning nothing. */
if (config->copy_as_set && !config->use_metadata) {
log_message(LOG_LEVEL_ERROR,
"--copy-as requires metadata preservation and cannot be combined with "
"--no-preserve");
return false;
}
return true; return true;
} }
+70 -113
View File
@@ -177,7 +177,7 @@ static bool entry_passes_selection(const FileListSet* file_list, const FilterRul
/* Best-effort capture of the file's whitelisted xattrs (-X/-A). A failure to /* Best-effort capture of the file's whitelisted xattrs (-X/-A). A failure to
* read xattrs is non-fatal: the file is transferred without them. */ * read xattrs is non-fatal: the file is transferred without them. */
static void scanner_capture_xattrs(const DirectoryScanner* scanner, File* file) { static void scanner_capture_xattrs(const DirectoryScanner* scanner, File* file) {
if (!scanner || !file || !(scanner->preserve_xattrs || scanner->preserve_acls)) if (!scanner || !file || !(scanner->options.preserve_xattrs || scanner->options.preserve_acls))
return; return;
file->xattrs = xattr_capture_path(file->path); file->xattrs = xattr_capture_path(file->path);
} }
@@ -263,10 +263,10 @@ static bool excluded_sink_append(ArrayList* list, mtx_t* mtx, const char* rel) {
how manifest keep entries are stored), so the receiver's walker prefixes how manifest keep entries are stored), so the receiver's walker prefixes
match the destination layout. An allocation failure is a fatal scan error. */ match the destination layout. An allocation failure is a fatal scan error. */
static void scanner_record_excluded(DirectoryScanner* scanner, const char* fs_path) { static void scanner_record_excluded(DirectoryScanner* scanner, const char* fs_path) {
if (!scanner->excluded_paths || !fs_path) if (!scanner->options.excluded_paths || !fs_path)
return; return;
const char* rel = *fs_path == '/' ? fs_path + 1 : fs_path; const char* rel = *fs_path == '/' ? fs_path + 1 : fs_path;
if (!excluded_sink_append(scanner->excluded_paths, scanner->excluded_mutex, rel)) if (!excluded_sink_append(scanner->options.excluded_paths, scanner->options.excluded_mutex, rel))
scanner->failed = true; scanner->failed = true;
} }
@@ -274,7 +274,7 @@ static void scanner_record_excluded(DirectoryScanner* scanner, const char* fs_pa
* context, returning the context used for this directory's entries. On a parse * context, returning the context used for this directory's entries. On a parse
* error the scanner is marked failed. Returns 0 on success, -1 on failure. */ * error the scanner is marked failed. Returns 0 on success, -1 on failure. */
static int open_directory_filter_context(DirectoryScanner* scanner, const FilterNode* inherited) { static int open_directory_filter_context(DirectoryScanner* scanner, const FilterNode* inherited) {
if (!scanner->per_dir_filters) { if (!scanner->options.per_dir_filters) {
scanner->current_node = (FilterNode*)inherited; scanner->current_node = (FilterNode*)inherited;
return 0; return 0;
} }
@@ -388,9 +388,13 @@ static int scanner_inspect_entry(const ScannerOptions* options, const char* sour
goto apply_filters; goto apply_filters;
regular: regular:
if (stat(entry->path, &entry->stats) != 0) /* Not a symlink: the lstat() above already described this entry, and lstat
goto skip; and stat are identical for every non-symlink, so reuse that result instead
entry->is_directory = S_ISDIR(entry->stats.st_mode); of issuing a redundant stat() on the scanner hot path. stat() is still
used on the dereference paths above/below for actual symlinks (copy-links,
safe/copy-unsafe links, and -k symlinks-to-directories). */
entry->stats = link_stats;
entry->is_directory = S_ISDIR(link_stats.st_mode);
if (entry->is_directory) if (entry->is_directory)
return 1; return 1;
@@ -432,6 +436,11 @@ DirectoryScanner* directory_scanner_create_with_options(const char* root_directo
DirectoryScanner* scanner = calloc(1, sizeof(DirectoryScanner)); DirectoryScanner* scanner = calloc(1, sizeof(DirectoryScanner));
if (scanner == NULL) if (scanner == NULL)
return NULL; return NULL;
/* One copy of the scan inputs; normalize chunk_size as the old field-by-field
copy did. */
scanner->options = *options;
if (scanner->options.chunk_size == 0)
scanner->options.chunk_size = DESIRED_CHUNK_SIZE;
scanner->directories = queue_create(100, dir_entry_destroy); scanner->directories = queue_create(100, dir_entry_destroy);
if (!scanner->directories) { if (!scanner->directories) {
free(scanner); free(scanner);
@@ -439,31 +448,7 @@ DirectoryScanner* directory_scanner_create_with_options(const char* root_directo
} }
scanner->current_dir = NULL; scanner->current_dir = NULL;
scanner->current_path = NULL; scanner->current_path = NULL;
scanner->use_metadata = options->use_metadata;
scanner->preserve_atimes = options->preserve_atimes;
scanner->preserve_crtimes = options->preserve_crtimes;
scanner->preserve_xattrs = options->preserve_xattrs;
scanner->preserve_acls = options->preserve_acls;
scanner->chunk_size = options->chunk_size > 0 ? options->chunk_size : DESIRED_CHUNK_SIZE;
scanner->exclude_patterns = options->exclude_patterns;
scanner->exclude_count = options->exclude_count;
scanner->include_patterns = options->include_patterns;
scanner->include_count = options->include_count;
scanner->max_size = options->max_size;
scanner->min_size = options->min_size;
scanner->max_depth = options->max_depth;
scanner->current_depth = 0; scanner->current_depth = 0;
scanner->follow_symlinks = options->follow_symlinks;
scanner->copy_links = options->copy_links;
scanner->safe_links = options->safe_links;
scanner->copy_unsafe_links = options->copy_unsafe_links;
scanner->copy_dirlinks = options->copy_dirlinks;
scanner->munge_links = options->munge_links;
scanner->checksum = options->checksum;
scanner->one_file_system = options->one_file_system;
scanner->preserve_devices = options->preserve_devices;
scanner->preserve_specials = options->preserve_specials;
scanner->copy_devices = options->copy_devices;
scanner->failed = false; scanner->failed = false;
scanner->root_path = str_dup(root_directory); scanner->root_path = str_dup(root_directory);
if (!scanner->root_path) { if (!scanner->root_path) {
@@ -475,28 +460,14 @@ DirectoryScanner* directory_scanner_create_with_options(const char* root_directo
scanner->at_seed_dir = true; scanner->at_seed_dir = true;
scanner->seed_node = NULL; scanner->seed_node = NULL;
scanner->current_node = NULL; scanner->current_node = NULL;
scanner->file_list = options->file_list;
scanner->base_filters = options->base_filters;
scanner->per_dir_filters = options->per_dir_filters;
scanner->excluded_paths = options->excluded_paths;
scanner->excluded_mutex = options->excluded_mutex;
scanner->ignore_io_errors = options->ignore_io_errors;
scanner->ignore_missing_args = options->ignore_missing_args;
scanner->io_error = false; scanner->io_error = false;
scanner->dirs_mode = options->dirs;
scanner->relative_mode = options->relative && options->file_list != NULL; scanner->relative_mode = options->relative && options->file_list != NULL;
scanner->hardlinks = options->hardlinks;
scanner->prune_empty_dirs = options->prune_empty_dirs;
scanner->stop_condition = options->stop_condition;
scanner->capture_dir_times = options->capture_dir_times;
scanner->dir_entries = options->dir_entries;
scanner->dir_entries_mutex = options->dir_entries_mutex;
scanner->dirs_root_emitted = false; scanner->dirs_root_emitted = false;
scanner->list_index = 0; scanner->list_index = 0;
scanner->dirs_batch = NULL; scanner->dirs_batch = NULL;
scanner->dirs_batch_size = 0; scanner->dirs_batch_size = 0;
scanner->filter_nodes = NULL; scanner->filter_nodes = NULL;
if (scanner->base_filters || scanner->per_dir_filters) { if (scanner->options.base_filters || scanner->options.per_dir_filters) {
scanner->filter_nodes = array_list_create(filter_node_destroy); scanner->filter_nodes = array_list_create(filter_node_destroy);
if (!scanner->filter_nodes) { if (!scanner->filter_nodes) {
free(scanner->root_path); free(scanner->root_path);
@@ -505,7 +476,7 @@ DirectoryScanner* directory_scanner_create_with_options(const char* root_directo
return NULL; return NULL;
} }
} }
if (scanner->one_file_system) { if (scanner->options.one_file_system) {
struct stat root_stats; struct stat root_stats;
if (stat(root_directory, &root_stats) != 0) { if (stat(root_directory, &root_stats) != 0) {
log_perror("Could not stat source directory"); log_perror("Could not stat source directory");
@@ -587,12 +558,16 @@ void directory_scanner_destroy(DirectoryScanner* scanner) {
static Chunk* chunk_data_to_chunk(ArrayList* chunk_data) { static Chunk* chunk_data_to_chunk(ArrayList* chunk_data) {
void** chunk_items = array_list_to_array(chunk_data); void** chunk_items = array_list_to_array(chunk_data);
if (!chunk_items) if (!chunk_items) {
array_list_delete(chunk_data);
return NULL; return NULL;
}
Chunk* chunk = chunk_create((File**)chunk_items, chunk_data->size); Chunk* chunk = chunk_create((File**)chunk_items, chunk_data->size);
free(chunk_items); free(chunk_items);
if (!chunk) if (!chunk) {
array_list_delete(chunk_data);
return NULL; return NULL;
}
chunk_data->item_destroyer = NULL; chunk_data->item_destroyer = NULL;
array_list_delete(chunk_data); array_list_delete(chunk_data);
return chunk; return chunk;
@@ -707,7 +682,7 @@ static int open_next_directory(DirectoryScanner* scanner) {
scanner->current_rel = NULL; scanner->current_rel = NULL;
free(scanner->current_path); free(scanner->current_path);
scanner->current_path = NULL; scanner->current_path = NULL;
if (!scanner->ignore_io_errors || is_root_seed) { if (!scanner->options.ignore_io_errors || is_root_seed) {
scanner->failed = true; scanner->failed = true;
return -1; return -1;
} }
@@ -721,10 +696,11 @@ static int open_next_directory(DirectoryScanner* scanner) {
scanner->current_path = NULL; scanner->current_path = NULL;
return -1; return -1;
} }
if (scanner->capture_dir_times && if (scanner->options.capture_dir_times &&
!scanner_capture_dir_time(scanner->dir_entries, scanner->dir_entries_mutex, !scanner_capture_dir_time(scanner->options.dir_entries, scanner->options.dir_entries_mutex,
scanner->root_path, scanner->current_path, scanner->relative_mode, scanner->root_path, scanner->current_path, scanner->relative_mode,
scanner->preserve_atimes, scanner->preserve_crtimes)) { scanner->options.preserve_atimes,
scanner->options.preserve_crtimes)) {
closedir(scanner->current_dir); closedir(scanner->current_dir);
scanner->current_dir = NULL; scanner->current_dir = NULL;
free(scanner->current_path); free(scanner->current_path);
@@ -765,9 +741,9 @@ static File* dirs_root_dir_file(DirectoryScanner* scanner) {
return NULL; return NULL;
} }
file->is_dir = true; file->is_dir = true;
if (scanner->use_metadata) { if (scanner->options.use_metadata) {
file->metadata = file_metadata_create(scanner->root_path, &st, scanner->preserve_atimes, file->metadata = file_metadata_create(scanner->root_path, &st, scanner->options.preserve_atimes,
scanner->preserve_crtimes); scanner->options.preserve_crtimes);
if (!file->metadata) { if (!file->metadata) {
file_destroy(file); file_destroy(file);
scanner->failed = true; scanner->failed = true;
@@ -800,7 +776,7 @@ static File* dirs_file_for_entry(DirectoryScanner* scanner, const char* entry) {
missing argument and is skipped here, exactly as the recursive scan skips missing argument and is skipped here, exactly as the recursive scan skips
nothing (missing entries never appear there). Without the flags it stays nothing (missing entries never appear there). Without the flags it stays
a hard pre-transfer error. */ a hard pre-transfer error. */
if (scanner->ignore_missing_args) { if (scanner->options.ignore_missing_args) {
log_info_message(LOG_INFO_MISC, "skipping missing --files-from entry '%s'", entry); log_info_message(LOG_INFO_MISC, "skipping missing --files-from entry '%s'", entry);
free(abs_path); free(abs_path);
return NULL; return NULL;
@@ -814,8 +790,8 @@ static File* dirs_file_for_entry(DirectoryScanner* scanner, const char* entry) {
if (S_ISLNK(link_stats.st_mode)) { if (S_ISLNK(link_stats.st_mode)) {
/* A symlink is transferred (following its referent) only when a link /* A symlink is transferred (following its referent) only when a link
resolution option is active, mirroring the regular scanner. */ resolution option is active, mirroring the regular scanner. */
bool resolve = scanner->follow_symlinks || scanner->copy_links || scanner->safe_links || bool resolve = scanner->options.follow_symlinks || scanner->options.copy_links ||
scanner->copy_unsafe_links; scanner->options.safe_links || scanner->options.copy_unsafe_links;
if (!resolve || stat(abs_path, &effective) != 0) { if (!resolve || stat(abs_path, &effective) != 0) {
free(abs_path); free(abs_path);
return NULL; return NULL;
@@ -843,9 +819,9 @@ static File* dirs_file_for_entry(DirectoryScanner* scanner, const char* entry) {
return NULL; return NULL;
} }
} }
if (scanner->use_metadata) { if (scanner->options.use_metadata) {
file->metadata = file_metadata_create(file->path, &effective, scanner->preserve_atimes, file->metadata = file_metadata_create(file->path, &effective, scanner->options.preserve_atimes,
scanner->preserve_crtimes); scanner->options.preserve_crtimes);
if (!file->metadata) { if (!file->metadata) {
file_destroy(file); file_destroy(file);
scanner->failed = true; scanner->failed = true;
@@ -876,18 +852,18 @@ static bool dirs_source_dir_is_empty(const char* path) {
/* The next File from the --dirs generator, or NULL when exhausted. */ /* The next File from the --dirs generator, or NULL when exhausted. */
static File* dirs_next_file(DirectoryScanner* scanner) { static File* dirs_next_file(DirectoryScanner* scanner) {
if (!scanner->file_list) { if (!scanner->options.file_list) {
if (scanner->dirs_root_emitted) if (scanner->dirs_root_emitted)
return NULL; return NULL;
scanner->dirs_root_emitted = true; scanner->dirs_root_emitted = true;
/* --prune-empty-dirs: a physically empty source directory's explicit entry /* --prune-empty-dirs: a physically empty source directory's explicit entry
would only create an empty destination directory, so it is omitted. */ would only create an empty destination directory, so it is omitted. */
if (scanner->prune_empty_dirs && dirs_source_dir_is_empty(scanner->root_path)) if (scanner->options.prune_empty_dirs && dirs_source_dir_is_empty(scanner->root_path))
return NULL; return NULL;
return dirs_root_dir_file(scanner); return dirs_root_dir_file(scanner);
} }
while (scanner->list_index < scanner->file_list->count) { while (scanner->list_index < scanner->options.file_list->count) {
const char* entry = scanner->file_list->entries[scanner->list_index++]; const char* entry = scanner->options.file_list->entries[scanner->list_index++];
File* file = dirs_file_for_entry(scanner, entry); File* file = dirs_file_for_entry(scanner, entry);
if (scanner->failed) if (scanner->failed)
return NULL; return NULL;
@@ -914,8 +890,9 @@ static Chunk* dirs_flush_batch(DirectoryScanner* scanner) {
} }
static Chunk* directory_scanner_next_dirs(DirectoryScanner* scanner) { static Chunk* directory_scanner_next_dirs(DirectoryScanner* scanner) {
while (scanner->dirs_batch == NULL || scanner->dirs_batch_size <= scanner->chunk_size) { while (scanner->dirs_batch == NULL || scanner->dirs_batch_size <= scanner->options.chunk_size) {
if (scanner->stop_condition && stop_condition_reached(scanner->stop_condition)) { if (scanner->options.stop_condition &&
stop_condition_reached(scanner->options.stop_condition)) {
Chunk* leftover = dirs_flush_batch(scanner); Chunk* leftover = dirs_flush_batch(scanner);
if (leftover) if (leftover)
chunk_destroy(leftover); chunk_destroy(leftover);
@@ -954,7 +931,7 @@ static Chunk* directory_scanner_next_dirs(DirectoryScanner* scanner) {
} }
Chunk* directory_scanner_next(DirectoryScanner* scanner) { Chunk* directory_scanner_next(DirectoryScanner* scanner) {
if (scanner && scanner->dirs_mode) if (scanner && scanner->options.dirs)
return directory_scanner_next_dirs(scanner); return directory_scanner_next_dirs(scanner);
ArrayList* chunk_data = array_list_create(file_destroy); ArrayList* chunk_data = array_list_create(file_destroy);
if (!chunk_data) { if (!chunk_data) {
@@ -964,7 +941,8 @@ Chunk* directory_scanner_next(DirectoryScanner* scanner) {
unsigned long long chunk_data_size = 0; unsigned long long chunk_data_size = 0;
while (1) { while (1) {
if (scanner->stop_condition && stop_condition_reached(scanner->stop_condition)) { if (scanner->options.stop_condition &&
stop_condition_reached(scanner->options.stop_condition)) {
array_list_delete(chunk_data); array_list_delete(chunk_data);
return NULL; return NULL;
} }
@@ -988,34 +966,9 @@ Chunk* directory_scanner_next(DirectoryScanner* scanner) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue; continue;
ScannerOptions options = {
.use_metadata = scanner->use_metadata,
.chunk_size = scanner->chunk_size,
.exclude_patterns = scanner->exclude_patterns,
.exclude_count = scanner->exclude_count,
.include_patterns = scanner->include_patterns,
.include_count = scanner->include_count,
.max_size = scanner->max_size,
.min_size = scanner->min_size,
.max_depth = scanner->max_depth,
.num_threads = 0,
.follow_symlinks = scanner->follow_symlinks,
.copy_links = scanner->copy_links,
.safe_links = scanner->safe_links,
.copy_unsafe_links = scanner->copy_unsafe_links,
.copy_dirlinks = scanner->copy_dirlinks,
.munge_links = scanner->munge_links,
.checksum = scanner->checksum,
.one_file_system = scanner->one_file_system,
.file_list = scanner->file_list,
.base_filters = scanner->base_filters,
.per_dir_filters = scanner->per_dir_filters,
.dirs = false,
.relative = false,
};
ScannerEntry inspected; ScannerEntry inspected;
int inspection = scanner_inspect_entry(&options, scanner->current_path, scanner->current_path, int inspection = scanner_inspect_entry(&scanner->options, scanner->current_path,
entry->d_name, &inspected); scanner->current_path, entry->d_name, &inspected);
if (inspection < 0) { if (inspection < 0) {
scanner->failed = true; scanner->failed = true;
break; break;
@@ -1047,16 +1000,17 @@ Chunk* directory_scanner_next(DirectoryScanner* scanner) {
scanner->failed = true; scanner->failed = true;
break; break;
} }
bool passes_selection = bool passes_selection = entry_passes_selection(
entry_passes_selection(scanner->file_list, scanner->base_filters, scanner->current_node, scanner->options.file_list, scanner->options.base_filters, scanner->current_node, rel,
rel, entry->d_name, is_dir, scanner->per_dir_filters); entry->d_name, is_dir, scanner->options.per_dir_filters);
if (!passes_selection) { if (!passes_selection) {
/* --files-from subset pruning is not a filter exclusion: its delete /* --files-from subset pruning is not a filter exclusion: its delete
semantics stay keep-set-only (an unlisted source path is treated as semantics stay keep-set-only (an unlisted source path is treated as
absent, so its destination mirror is a deletable extra). A rule-based absent, so its destination mirror is a deletable extra). A rule-based
exclusion is recorded as a protected prefix. -R + --files-from bare exclusion is recorded as a protected prefix. -R + --files-from bare
wire paths are never recorded (see ScannerOptions.excluded_paths). */ wire paths are never recorded (see ScannerOptions.excluded_paths). */
bool files_from_prune = scanner->file_list && !file_list_affects(scanner->file_list, rel); bool files_from_prune =
scanner->options.file_list && !file_list_affects(scanner->options.file_list, rel);
if (!files_from_prune && !scanner->relative_mode) if (!files_from_prune && !scanner->relative_mode)
scanner_record_excluded(scanner, cur_path); scanner_record_excluded(scanner, cur_path);
} }
@@ -1077,12 +1031,13 @@ Chunk* directory_scanner_next(DirectoryScanner* scanner) {
if (is_dir) { if (is_dir) {
free(rel_copy); free(rel_copy);
if (!scanner_same_filesystem(scanner->one_file_system, scanner->root_dev, stats.st_dev)) { if (!scanner_same_filesystem(scanner->options.one_file_system, scanner->root_dev,
stats.st_dev)) {
free(cur_path); free(cur_path);
continue; continue;
} }
int next_depth = scanner->current_depth + 1; int next_depth = scanner->current_depth + 1;
if (scanner->max_depth <= 0 || next_depth < scanner->max_depth) { if (scanner->options.max_depth <= 0 || next_depth < scanner->options.max_depth) {
DirEntry* de = dir_entry_create(cur_path, next_depth, scanner->current_node); DirEntry* de = dir_entry_create(cur_path, next_depth, scanner->current_node);
if (!de || !queue_enqueue(scanner->directories, de)) { if (!de || !queue_enqueue(scanner->directories, de)) {
dir_entry_destroy(de); dir_entry_destroy(de);
@@ -1091,7 +1046,8 @@ Chunk* directory_scanner_next(DirectoryScanner* scanner) {
} }
free(cur_path); free(cur_path);
} else { } else {
if (scanner->max_depth > 0 && scanner->current_depth + 1 > scanner->max_depth) { if (scanner->options.max_depth > 0 &&
scanner->current_depth + 1 > scanner->options.max_depth) {
free(rel_copy); free(rel_copy);
free(cur_path); free(cur_path);
continue; continue;
@@ -1118,13 +1074,14 @@ Chunk* directory_scanner_next(DirectoryScanner* scanner) {
} }
/* --devices/--specials: a device/FIFO/socket entry marked for preservation /* --devices/--specials: a device/FIFO/socket entry marked for preservation
becomes a node to recreate (is_special, no data, rdev captured). */ becomes a node to recreate (is_special, no data, rdev captured). */
scanner_prepare_special(scanner->preserve_devices, scanner->preserve_specials, file, &stats); scanner_prepare_special(scanner->options.preserve_devices, scanner->options.preserve_specials,
if (scanner->hardlinks && S_ISREG(stats.st_mode)) file, &stats);
scanner_assign_hardlink(scanner, scanner->hardlinks, file, &stats); if (scanner->options.hardlinks && S_ISREG(stats.st_mode))
if (scanner->use_metadata) scanner_assign_hardlink(scanner, scanner->options.hardlinks, file, &stats);
file->metadata = file_metadata_create(file->path, &stats, scanner->preserve_atimes, if (scanner->options.use_metadata)
scanner->preserve_crtimes); file->metadata = file_metadata_create(file->path, &stats, scanner->options.preserve_atimes,
if (scanner->use_metadata && !file->metadata) { scanner->options.preserve_crtimes);
if (scanner->options.use_metadata && !file->metadata) {
free(rel_copy); free(rel_copy);
file_destroy(file); file_destroy(file);
scanner->failed = true; scanner->failed = true;
@@ -1139,7 +1096,7 @@ Chunk* directory_scanner_next(DirectoryScanner* scanner) {
break; break;
} }
chunk_data_size += file->data->size; chunk_data_size += file->data->size;
if (chunk_data_size > scanner->chunk_size) { if (chunk_data_size > scanner->options.chunk_size) {
free(rel_copy); free(rel_copy);
Chunk* result = chunk_data_to_chunk(chunk_data); Chunk* result = chunk_data_to_chunk(chunk_data);
if (!result) if (!result)
@@ -1203,7 +1160,7 @@ static int parallel_worker_thread(void* arg) {
free(ds->root_path); free(ds->root_path);
ds->root_path = str_dup(wa->root_dir); ds->root_path = str_dup(wa->root_dir);
ds->seed_node = wa->ps->root_filter_node; ds->seed_node = wa->ps->root_filter_node;
ds->excluded_mutex = &wa->ps->result_mutex; ds->options.excluded_mutex = &wa->ps->result_mutex;
Chunk* chunk; Chunk* chunk;
while ((chunk = directory_scanner_next(ds)) != NULL) { while ((chunk = directory_scanner_next(ds)) != NULL) {
if (!queue_enqueue_multithreaded_cancel(wa->ps->result_queue, chunk, &wa->ps->result_mutex, if (!queue_enqueue_multithreaded_cancel(wa->ps->result_queue, chunk, &wa->ps->result_mutex,
+9 -49
View File
@@ -14,6 +14,10 @@
#include <sys/types.h> #include <sys/types.h>
#include <threads.h> #include <threads.h>
/* Upper bound on the configurable parallel scanner worker count (--threads=N):
* keeps one transfer from spawning an unbounded pool on a very large machine. */
#define MAX_SCANNER_THREADS 256
typedef struct { typedef struct {
bool use_metadata; bool use_metadata;
/* Phase 4 metadata capture: -U/--atimes and -N/--crtimes tell the scanner to /* Phase 4 metadata capture: -U/--atimes and -N/--crtimes tell the scanner to
@@ -117,37 +121,16 @@ typedef struct {
typedef struct FilterNode FilterNode; typedef struct FilterNode FilterNode;
typedef struct { typedef struct {
/* Scan inputs, copied once at create time. Everything that is also a
ScannerOptions field lives here (with the normalized chunk_size); only
scanner-owned bookkeeping stays as direct members below. */
ScannerOptions options;
Queue* directories; Queue* directories;
DIR* current_dir; DIR* current_dir;
char* current_path; char* current_path;
bool use_metadata;
bool preserve_atimes;
bool preserve_crtimes;
bool preserve_xattrs;
bool preserve_acls;
unsigned long long chunk_size;
char** exclude_patterns;
int exclude_count;
char** include_patterns;
int include_count;
unsigned long long max_size;
unsigned long long min_size;
int max_depth;
int current_depth; int current_depth;
bool follow_symlinks;
bool copy_links;
bool safe_links;
bool copy_unsafe_links;
bool copy_dirlinks;
bool munge_links;
bool checksum;
bool one_file_system;
dev_t root_dev; dev_t root_dev;
bool failed; bool failed;
/* Phase 4 special/devices (see ScannerOptions). */
bool preserve_devices;
bool preserve_specials;
bool copy_devices;
/* Phase 2 (files-from / filter layer). */ /* Phase 2 (files-from / filter layer). */
char* root_path; /* transfer root (fs path) for rel computation */ char* root_path; /* transfer root (fs path) for rel computation */
char* current_rel; /* rel path of the open directory ("" == root) */ char* current_rel; /* rel path of the open directory ("" == root) */
@@ -155,40 +138,17 @@ typedef struct {
FilterNode* seed_node; /* inherited context of the seed dir, or NULL */ FilterNode* seed_node; /* inherited context of the seed dir, or NULL */
FilterNode* current_node; /* filter context of the open directory */ FilterNode* current_node; /* filter context of the open directory */
ArrayList* filter_nodes; /* owned FilterNode arena (may be NULL) */ ArrayList* filter_nodes; /* owned FilterNode arena (may be NULL) */
const FileListSet* file_list; /* --dirs / -R state for the directory-entry generator (options.dirs replaces
const FilterRuleList* base_filters;
bool per_dir_filters;
/* --dirs / -R state for the directory-entry generator (dirs_mode replaces
the recursive scan). */ the recursive scan). */
bool dirs_mode;
bool relative_mode; /* file_list && relative: send bare relative wire paths */ bool relative_mode; /* file_list && relative: send bare relative wire paths */
bool prune_empty_dirs;
bool dirs_root_emitted; bool dirs_root_emitted;
int list_index; int list_index;
ArrayList* dirs_batch; /* owned when non-NULL */ ArrayList* dirs_batch; /* owned when non-NULL */
unsigned long long dirs_batch_size; unsigned long long dirs_batch_size;
/* Excluded-path sink (see ScannerOptions). `excluded_mutex` is shared across
parallel worker threads. */
ArrayList* excluded_paths;
mtx_t* excluded_mutex;
/* --ignore-errors: continue past unreadable directories (records io_error). */
bool ignore_io_errors;
/* --ignore-missing-args: --dirs listed-but-missing entries are skipped, not
fatal (see ScannerOptions.ignore_missing_args). */
bool ignore_missing_args;
/* A directory could not be opened (I/O error, e.g. EACCES). With /* A directory could not be opened (I/O error, e.g. EACCES). With
--ignore-errors the scan continues past it and the caller decides what to --ignore-errors the scan continues past it and the caller decides what to
do; `failed` is reserved for fatal errors that always abort the scan. */ do; `failed` is reserved for fatal errors that always abort the scan. */
bool io_error; bool io_error;
/* --hard-links (-H): shared link-group detection table (see ScannerOptions).
NULL when -H is off. */
HardLinkTable* hardlinks;
/* Phase 6: sender stop deadline (from ScannerOptions). */
const StopCondition* stop_condition;
/* P7 Wave D directory-time capture (see ScannerOptions). */
bool capture_dir_times;
ArrayList* dir_entries;
mtx_t* dir_entries_mutex;
} DirectoryScanner; } DirectoryScanner;
typedef struct { typedef struct {
+6 -1
View File
@@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <delta.h> #include <delta.h>
#include <chunk.h> #include <chunk.h>
#include "scanner.h"
void print_usage(void) { void print_usage(void) {
printf("Usage:\n"); printf("Usage:\n");
@@ -144,7 +145,10 @@ void print_usage(void) {
printf(" Delta block size in bytes (default: %d)\n", DELTA_BLOCK_SIZE_DEFAULT); printf(" Delta block size in bytes (default: %d)\n", DELTA_BLOCK_SIZE_DEFAULT);
printf(" --delta-max <n> Max file size for delta transfer (default: %llu)\n", printf(" --delta-max <n> Max file size for delta transfer (default: %llu)\n",
DELTA_MAX_FILE_SIZE); DELTA_MAX_FILE_SIZE);
printf(" -j, --threads Enable multithreading\n"); printf(" -j, --threads[=N] Enable the multithreaded scanner/loader/sender\n");
printf(" pipeline; N (1-%d) sets the parallel scanner worker\n",
MAX_SCANNER_THREADS);
printf(" count (bare -j/--threads uses the default)\n");
printf(" --chunk-serialization Enable chunk serialization (long form only)\n"); printf(" --chunk-serialization Enable chunk serialization (long form only)\n");
printf(" -s, --secluded-args Protect-args compatibility option (no effect; remote\n"); printf(" -s, --secluded-args Protect-args compatibility option (no effect; remote\n");
printf(" SSH argv is already built injection-safe)\n"); printf(" SSH argv is already built injection-safe)\n");
@@ -203,6 +207,7 @@ void print_usage(void) {
printf(" --save-to-disk Write received files to disk\n"); printf(" --save-to-disk Write received files to disk\n");
printf(" --server-host <ip> Server IP address (default: 127.0.0.1)\n"); printf(" --server-host <ip> Server IP address (default: 127.0.0.1)\n");
printf(" --server-port <n> Server port (default: 8080)\n"); printf(" --server-port <n> Server port (default: 8080)\n");
printf(" --port <n> Alias for --server-port\n");
printf(" --password-file <f> Authenticate a host::module/path daemon destination.\n"); printf(" --password-file <f> Authenticate a host::module/path daemon destination.\n");
printf(" The file's first user:password line supplies the\n"); printf(" The file's first user:password line supplies the\n");
printf(" username and password (only a SHA-256 digest of the\n"); printf(" username and password (only a SHA-256 digest of the\n");
+100 -1
View File
@@ -12,6 +12,7 @@
#include "utils.h" #include "utils.h"
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <time.h>
bool receiver_outcomes_append(ReceiverOutcomes* outcomes, unsigned char code) { bool receiver_outcomes_append(ReceiverOutcomes* outcomes, unsigned char code) {
if (!outcomes) if (!outcomes)
@@ -153,6 +154,93 @@ static bool receiver_process_batch(Config* config, int file_descriptor) {
return true; return true;
} }
/* ---- Anti-slowloris connection bounds ----
* A legitimate transfer either streams data frames continuously or, when it
* must pause, sends STATUS_KEEPALIVE so the peer sees the connection is alive.
* An attacker can therefore squat on a connection slot indefinitely by sending
* only keepalives under the per-message timeout. Two CLOCK_MONOTONIC bounds
* defeat that without ever punishing a real transfer:
*
* MAX_SESSION_IDLE_SEC (1 h): the longest a stream may make no forward
* progress. Data/status frames count as progress and refresh the timer;
* keepalives do not. One hour is far longer than any real pause between
* data frames, yet small enough to reap a slowloris well before the 24 h
* session cap.
*
* MAX_SESSION_WALL_SEC (24 h): an absolute ceiling on one connection's
* lifetime as defense-in-depth against a trickle of progress frames that
* resets the idle timer just below its limit. Larger than any plausible
* single transfer while still bounding resource occupancy.
*
* Both are wall-clock deltas, so the per-message poll timeout (60 s by default,
* or --timeout) can never fool them, and both the single-threaded and the -m
* receiver paths (receiver_process_pending) share the same logic. */
#define MAX_SESSION_IDLE_SEC 3600u
#define MAX_SESSION_WALL_SEC 86400u
static unsigned int g_max_session_idle_sec = MAX_SESSION_IDLE_SEC;
static unsigned int g_max_session_wall_sec = MAX_SESSION_WALL_SEC;
void receiver_set_time_limits(unsigned int idle_sec, unsigned int wall_sec) {
g_max_session_idle_sec = idle_sec;
g_max_session_wall_sec = wall_sec;
}
void receiver_reset_time_limits(void) {
g_max_session_idle_sec = MAX_SESSION_IDLE_SEC;
g_max_session_wall_sec = MAX_SESSION_WALL_SEC;
}
bool receiver_time_limit_exceeded(const struct timespec* session_start,
const struct timespec* last_progress,
const struct timespec* now) {
if (!session_start || !last_progress || !now)
return false;
if (now->tv_sec - session_start->tv_sec >= (time_t)g_max_session_wall_sec)
return true;
if (now->tv_sec - last_progress->tv_sec >= (time_t)g_max_session_idle_sec)
return true;
return false;
}
/* A frame proves forward progress only when it cannot be fabricated for free.
* KEEPALIVE/ABORT are pure liveness, and CHECK_BATCH/DIR_TIMES may carry zero
* entries, so a peer must not be able to hold a connection slot forever by
* merely emitting empty frames. */
static bool status_counts_as_progress(Status status) {
switch (status) {
case STATUS_KEEPALIVE:
case STATUS_ABORT:
case STATUS_CHECK_BATCH:
case STATUS_DIR_TIMES:
return false;
default:
return true;
}
}
/* Refresh the progress timestamp for a forward-moving frame and enforce the
* bounds above. Returns false when the connection must be dropped; the
* terminal STATUS_ERROR is sent only when the sink owns error reporting (the
* -m sink sets send_error=false so the main thread emits exactly one). */
static bool receiver_note_status(const struct timespec* session_start,
struct timespec* last_progress, Status status, int file_descriptor,
const ReceiverSink* sink) {
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now) != 0)
now = *last_progress;
if (status_counts_as_progress(status))
*last_progress = now;
if (!receiver_time_limit_exceeded(session_start, last_progress, &now))
return true;
log_message(LOG_LEVEL_ERROR,
"Receive session exceeded its time bound (idle %us / total %us); aborting connection",
g_max_session_idle_sec, g_max_session_wall_sec);
if (!sink || sink->send_error)
send_status(file_descriptor, STATUS_ERROR);
return false;
}
int receiver_process(Config* config, int file_descriptor, const ReceiverSink* sink) { int receiver_process(Config* config, int file_descriptor, const ReceiverSink* sink) {
return receiver_process_pending(config, file_descriptor, sink, NULL); return receiver_process_pending(config, file_descriptor, sink, NULL);
} }
@@ -172,6 +260,15 @@ int receiver_process_pending(Config* config, int file_descriptor, const Receiver
Status status; Status status;
if (!receive_status(file_descriptor, &status)) if (!receive_status(file_descriptor, &status))
return -1; return -1;
/* Wall-clock (=CLOCK_MONOTONIC) anti-slowloris bookkeeping. session_start is
* fixed for the whole connection; last_progress is refreshed by every frame
* that is not a keepalive/abort. */
struct timespec session_start;
struct timespec last_progress;
clock_gettime(CLOCK_MONOTONIC, &session_start);
last_progress = session_start;
if (!receiver_note_status(&session_start, &last_progress, status, file_descriptor, sink))
return -1;
bool early_delete = config_delete_timing_early(config); bool early_delete = config_delete_timing_early(config);
/* Parked keep-set for the late/commit timing. Every exit path below frees it /* Parked keep-set for the late/commit timing. Every exit path below frees it
exactly once; the only exception is the successful FINISHED handoff, which exactly once; the only exception is the successful FINISHED handoff, which
@@ -271,6 +368,8 @@ int receiver_process_pending(Config* config, int file_descriptor, const Receiver
next_status: next_status:
if (!receive_status(file_descriptor, &status)) if (!receive_status(file_descriptor, &status))
goto receive_error; goto receive_error;
if (!receiver_note_status(&session_start, &last_progress, status, file_descriptor, sink))
goto fail;
} }
if (status != STATUS_FINISHED) { if (status != STATUS_FINISHED) {
log_message(LOG_LEVEL_ERROR, "Did not receive FINISHED Status"); log_message(LOG_LEVEL_ERROR, "Did not receive FINISHED Status");
@@ -353,7 +452,7 @@ static bool receiver_save_file(File* file, void* context_pointer) {
metadata now and apply it at the end. -O/--omit-dir-times is honored by metadata now and apply it at the end. -O/--omit-dir-times is honored by
dir_time_list_apply's caller (see receiver_send_success_frame). */ dir_time_list_apply's caller (see receiver_send_success_frame). */
if (result != FILE_SAVE_ERROR && file->is_dir && file->metadata && if (result != FILE_SAVE_ERROR && file->is_dir && file->metadata &&
context->config->use_metadata && !context->config->omit_dir_times && dir_times_should_capture(context->config) &&
!dir_time_list_add(&context->dir_times, file->path, file->metadata)) { !dir_time_list_add(&context->dir_times, file->path, file->metadata)) {
file_destroy(file); file_destroy(file);
return false; return false;
+20
View File
@@ -4,6 +4,8 @@
#include "config.h" #include "config.h"
#include "file.h" #include "file.h"
#include "file_receive.h" #include "file_receive.h"
#include <stdbool.h>
#include <time.h>
typedef bool (*ReceiverFileSink)(File* file, void* context); typedef bool (*ReceiverFileSink)(File* file, void* context);
@@ -45,4 +47,22 @@ int receiver_process_pending(Config* config, int file_descriptor, const Receiver
DeleteManifest** pending_manifest); DeleteManifest** pending_manifest);
int receiver_receive_files(Config* config, int file_descriptor); int receiver_receive_files(Config* config, int file_descriptor);
/* ---- Connection time bounds (anti-slowloris) ----
* receiver_process_pending() aborts a connection that makes no forward progress
* (only STATUS_KEEPALIVE/STATUS_ABORT frames) beyond a wall-clock idle limit,
* and enforces a hard cap on the whole session. Both are CLOCK_MONOTONIC
* deltas, independent of the per-message poll deadline, so a 60 s (or
* --timeout) receive window can never reset them. Defaults are deliberately
* generous (see MAX_SESSION_IDLE_SEC / MAX_SESSION_WALL_SEC in receiver.c). */
/* Test seam: override the idle/session wall-clock limits (0 = abort on the
* next status). Always restore with receiver_reset_time_limits(). */
void receiver_set_time_limits(unsigned int idle_sec, unsigned int wall_sec);
void receiver_reset_time_limits(void);
/* Pure predicate over explicit monotonic timestamps, exposed so the bound is
* unit-testable without sleeping. True when either the idle or the overall
* session limit has elapsed. */
bool receiver_time_limit_exceeded(const struct timespec* session_start,
const struct timespec* last_progress, const struct timespec* now);
#endif #endif
+254
View File
@@ -0,0 +1,254 @@
#include "receiver_pipeline.h"
#include "log.h"
#include "protocol.h"
#include "queue.h"
#include "utils.h"
#include <stdlib.h>
#include <string.h>
#include <threads.h>
PipelineContextReceiver* pipeline_context_receiver_create(Config* config, Queue* queue,
int file_descriptor, SSL* ssl) {
PipelineContextReceiver* context = malloc(sizeof(PipelineContextReceiver));
if (context == NULL)
return NULL;
context->config = config;
context->queue = queue;
context->file_descriptor = file_descriptor;
context->ssl = ssl;
context->outcomes.entries = NULL;
context->outcomes.count = 0;
context->outcomes.capacity = 0;
dir_time_list_init(&context->dir_times);
protocol_session_init(&context->session, file_descriptor, file_descriptor);
protocol_session_set_ssl(&context->session, ssl);
context->receiver_done = false;
context->queued_bytes = 0;
context->max_queue_bytes = 0;
context->deferred_manifest = NULL;
atomic_init(&context->cancelled, false);
int init = 0;
if (mtx_init(&context->mutex, mtx_plain) != thrd_success)
goto fail;
init++;
if (cnd_init(&context->condition_not_full) != thrd_success)
goto fail;
init++;
if (cnd_init(&context->condition_not_empty) != thrd_success)
goto fail;
// cppcheck-suppress unreadVariable
init++;
return context;
fail:
log_perror("Error initializing synchronization objects");
if (init >= 3)
cnd_destroy(&context->condition_not_empty);
if (init >= 2)
cnd_destroy(&context->condition_not_full);
if (init >= 1)
mtx_destroy(&context->mutex);
free(context);
return NULL;
}
void pipeline_context_receiver_destroy(PipelineContextReceiver* context) {
config_delete(context->config);
if (context->deferred_manifest)
delete_manifest_free(context->deferred_manifest);
queue_destroy(context->queue);
receiver_outcomes_destroy(&context->outcomes);
dir_time_list_free(&context->dir_times);
mtx_destroy(&context->mutex);
cnd_destroy(&context->condition_not_full);
cnd_destroy(&context->condition_not_empty);
free(context);
}
void pipeline_context_receiver_set_queue_byte_limit(PipelineContextReceiver* context,
size_t max_bytes) {
if (context == NULL)
return;
mtx_lock(&context->mutex);
context->max_queue_bytes = max_bytes;
context->queued_bytes = 0;
cnd_broadcast(&context->condition_not_full);
mtx_unlock(&context->mutex);
}
void pipeline_context_receiver_note_bytes_released(PipelineContextReceiver* context,
size_t released_bytes) {
if (context == NULL || context->max_queue_bytes == 0 || released_bytes == 0)
return;
mtx_lock(&context->mutex);
if (released_bytes >= context->queued_bytes)
context->queued_bytes = 0;
else
context->queued_bytes -= released_bytes;
cnd_signal(&context->condition_not_full);
mtx_unlock(&context->mutex);
}
bool pipeline_context_receiver_enqueue_file(PipelineContextReceiver* context, File* file) {
if (context == NULL || file == NULL)
return false;
size_t file_bytes = file->data ? file->data->size : 0;
mtx_lock(&context->mutex);
while (!atomic_load(&context->cancelled)) {
bool blocked_by_count = queue_is_full(context->queue);
bool blocked_by_budget = false;
if (context->max_queue_bytes > 0) {
size_t budget = context->max_queue_bytes;
size_t used = context->queued_bytes;
if (used >= budget) {
blocked_by_budget = true;
} else if (file_bytes > budget - used) {
/* A single payload larger than the whole budget (not possible with
the per-file receive cap) is only admitted to an empty pipeline so
the wait can never deadlock. */
blocked_by_budget = used != 0;
}
}
if (!blocked_by_count && !blocked_by_budget)
break;
cnd_wait(&context->condition_not_full, &context->mutex);
}
if (atomic_load(&context->cancelled)) {
mtx_unlock(&context->mutex);
file_destroy(file);
return false;
}
if (!queue_enqueue(context->queue, file)) {
mtx_unlock(&context->mutex);
file_destroy(file);
return false;
}
context->queued_bytes += file_bytes;
cnd_signal(&context->condition_not_empty);
mtx_unlock(&context->mutex);
return true;
}
static bool receiver_enqueue_file(File* file, void* context_pointer) {
PipelineContextReceiver* context = (PipelineContextReceiver*)context_pointer;
return pipeline_context_receiver_enqueue_file(context, file);
}
static void receiver_thread_fail(PipelineContextReceiver* context) {
mtx_lock(&context->mutex);
atomic_store(&context->cancelled, true);
context->receiver_done = true;
cnd_broadcast(&context->condition_not_empty);
cnd_broadcast(&context->condition_not_full);
mtx_unlock(&context->mutex);
}
int receive_thread(void* pipeline_context) {
PipelineContextReceiver* context = (PipelineContextReceiver*)pipeline_context;
protocol_session_bind(&context->session);
mtx_lock(&context->mutex);
int file_descriptor = context->file_descriptor;
const Config* config = context->config;
mtx_unlock(&context->mutex);
ReceiverSink sink = {receiver_enqueue_file, context, false, false, NULL};
if (receiver_process_pending((Config*)config, file_descriptor, &sink,
&context->deferred_manifest) != 0) {
receiver_thread_fail(context);
protocol_session_unbind();
return thrd_error;
}
mtx_lock(&context->mutex);
context->receiver_done = true;
cnd_signal(&context->condition_not_empty);
mtx_unlock(&context->mutex);
protocol_session_unbind();
return thrd_success;
}
int write_thread(void* pipeline_context) {
PipelineContextReceiver* context = (PipelineContextReceiver*)pipeline_context;
protocol_session_bind(&context->session);
mtx_lock(&context->mutex);
bool save_to_disk = context->config->save_to_disk;
char* root_directory = str_dup(context->config->receive_root_directory);
mtx_unlock(&context->mutex);
if (save_to_disk && !root_directory) {
mtx_lock(&context->mutex);
atomic_store(&context->cancelled, true);
context->receiver_done = true;
cnd_broadcast(&context->condition_not_full);
cnd_broadcast(&context->condition_not_empty);
mtx_unlock(&context->mutex);
protocol_session_unbind();
return thrd_error;
}
while (true) {
File* file =
queue_dequeue_multithreaded(context->queue, &context->mutex, &context->condition_not_empty,
&context->condition_not_full, &context->receiver_done);
if (file == NULL) {
free(root_directory);
protocol_session_unbind();
return thrd_success;
}
size_t file_bytes = file->data ? file->data->size : 0;
FileSaveResult result = FILE_SAVE_SKIPPED;
if (save_to_disk) {
result = file_save_to_disk_full(root_directory, file, context->config);
if (result == FILE_SAVE_ERROR) {
file_destroy(file);
pipeline_context_receiver_note_bytes_released(context, file_bytes);
mtx_lock(&context->mutex);
atomic_store(&context->cancelled, true);
context->receiver_done = true;
cnd_broadcast(&context->condition_not_full);
cnd_broadcast(&context->condition_not_empty);
mtx_unlock(&context->mutex);
free(root_directory);
protocol_session_unbind();
return thrd_error;
}
}
/* P7 Wave D: a directory's times are never applied inline (a later child
write would clobber them); accumulate the metadata here and let the
caller apply it once every writer has drained. */
if (result != FILE_SAVE_ERROR && file->is_dir && file->metadata &&
dir_times_should_capture(context->config) &&
!dir_time_list_add(&context->dir_times, file->path, file->metadata)) {
file_destroy(file);
pipeline_context_receiver_note_bytes_released(context, file_bytes);
mtx_lock(&context->mutex);
atomic_store(&context->cancelled, true);
context->receiver_done = true;
cnd_broadcast(&context->condition_not_full);
cnd_broadcast(&context->condition_not_empty);
mtx_unlock(&context->mutex);
free(root_directory);
protocol_session_unbind();
return thrd_error;
}
/* Record the per-file outcome so a --remove-source-files sender learns
which sources were actually written versus skipped on the receiver.
Explicit directory entries and recreated device/special nodes have no
source and are never acknowledged (mirrors receiver.c). */
if (context->config->remove_source_files && !file->is_dir && !file->is_special && !file->skip &&
!receiver_outcomes_append(&context->outcomes, (unsigned char)result)) {
file_destroy(file);
pipeline_context_receiver_note_bytes_released(context, file_bytes);
mtx_lock(&context->mutex);
atomic_store(&context->cancelled, true);
context->receiver_done = true;
cnd_broadcast(&context->condition_not_full);
cnd_broadcast(&context->condition_not_empty);
mtx_unlock(&context->mutex);
free(root_directory);
protocol_session_unbind();
return thrd_error;
}
file_destroy(file);
pipeline_context_receiver_note_bytes_released(context, file_bytes);
}
}
+68
View File
@@ -0,0 +1,68 @@
#ifndef RECEIVER_PIPELINE_H
#define RECEIVER_PIPELINE_H
#include <stdatomic.h>
#include <stdbool.h>
#include <threads.h>
#include "config.h"
#include "file.h"
#include "file_receive.h"
#include "protocol.h"
#include "queue.h"
#include "receiver.h"
#include <openssl/ssl.h>
typedef struct PipelineContextReceiver {
Queue* queue;
Config* config;
int file_descriptor;
SSL* ssl;
ProtocolSession session;
ReceiverOutcomes outcomes;
mtx_t mutex;
cnd_t condition_not_full;
cnd_t condition_not_empty;
bool receiver_done;
atomic_bool cancelled;
/* Aggregate payload bytes that have been received but not yet released by
the disk writer (queued or in the writer's hand). Guarded by `mutex`.
When `max_queue_bytes` is non-zero the receiver blocks before enqueuing
once this total would exceed it, so decompressed/copied file payloads
buffered ahead of a slow disk writer respect the per-connection memory
budget instead of growing without bound. */
size_t queued_bytes;
size_t max_queue_bytes;
/* Keep-set manifest for the commit-style (late) deletion
(--delete/--delete-after/--delete-delay). receive_thread parses the whole
protocol stream but hands the manifest here instead of deleting while the
disk writer may still be draining; the caller (server.c) commits the
deletion after both threads have joined, so no extra is removed unless the
transfer truly succeeded. NULL in the early delete modes (which delete at
the manifest). */
DeleteManifest* deferred_manifest;
/* P7 Wave D: directory metadata collected by write_thread from received
directory entries. Only write_thread mutates it (before it joins); the
caller (server.c) applies it after the delete/delay-updates phase. */
DirTimeList dir_times;
} PipelineContextReceiver;
PipelineContextReceiver* pipeline_context_receiver_create(Config* config, Queue* queue_receiver,
int file_descriptor, SSL* ssl);
void pipeline_context_receiver_destroy(PipelineContextReceiver* context);
/* Bound the bytes buffered ahead of the disk writer (see max_queue_bytes). */
void pipeline_context_receiver_set_queue_byte_limit(PipelineContextReceiver* context,
size_t max_bytes);
/* Blocking enqueue used by the receive pipeline sink. Blocks while the queue
is full by element count or when adding `file` would push queued_bytes over
the configured byte limit; waits until the disk writer releases bytes.
Takes ownership of `file` on success and destroys it on failure/cancel. */
bool pipeline_context_receiver_enqueue_file(PipelineContextReceiver* context, File* file);
/* Account for `released_bytes` of payload memory that has been freed by the
disk writer, unblocking a receiver that is waiting on the byte limit. */
void pipeline_context_receiver_note_bytes_released(PipelineContextReceiver* context,
size_t released_bytes);
int receive_thread(void* pipeline_context);
int write_thread(void* pipeline_context);
#endif
+340 -193
View File
@@ -7,10 +7,10 @@
#include "identity.h" #include "identity.h"
#include "log.h" #include "log.h"
#include "motd.h" #include "motd.h"
#include "multiprocessing.h"
#include "protocol.h" #include "protocol.h"
#include "queue.h" #include "queue.h"
#include "receiver.h" #include "receiver.h"
#include "receiver_pipeline.h"
#include "server_cli.h" #include "server_cli.h"
#include "transport_tcp.h" #include "transport_tcp.h"
#include "transport_tls.h" #include "transport_tls.h"
@@ -23,7 +23,10 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <time.h>
#include <openssl/x509.h> #include <openssl/x509.h>
static char* authorized_root; static char* authorized_root;
@@ -67,6 +70,11 @@ typedef struct ModuleGateContext {
activity (operator --no-super, or a daemon module without the activity (operator --no-super, or a daemon module without the
`client owner = yes` opt-in); -1 when the config's own mode stands. */ `client owner = yes` opt-in); -1 when the config's own mode stands. */
int super_mode_override; int super_mode_override;
/* Numeric peer address (INET6_ADDRSTRLEN is always enough), filled once by
* server_module_gate. has_peer_ip is false when getpeername/inet_ntop could
* not classify the peer; an ACL-configured module then fails closed. */
bool has_peer_ip;
char peer_ip[INET6_ADDRSTRLEN];
} ModuleGateContext; } ModuleGateContext;
/* Server half of the SCRAM challenge/response (A7 remediation, protocol /* Server half of the SCRAM challenge/response (A7 remediation, protocol
@@ -251,6 +259,220 @@ static bool configure_authorization(const char* root) {
return true; return true;
} }
/* Discriminates the outcome of the A7 auth gate so the dispatcher can map it
* back to the config_receive_with_validate contract: accepted (including
* "module needs no auth"), a config-level refusal carrying an error string, or
* a handshake that already wrote its own terminal status frame. */
typedef enum {
MODULE_AUTH_ACCEPTED = 0,
MODULE_AUTH_REFUSED,
MODULE_AUTH_TERMINATED,
} ModuleAuthResult;
/* Looks up the daemon module selected by the client's config frame and rejects
* a `read only` one (every FastSync network transfer writes; there is no
* read-only wire operation yet). Returns the module, or NULL with *error set
* to the caller-facing rejection message. */
static const DaemonModule* module_gate_lookup_module(const Config* config, const char** error) {
const DaemonModule* module = daemon_conf_find_module(g_daemon_conf, config->module);
if (module == NULL) {
char* escaped_module = output_escape(config->module, config->eight_bit_output);
log_message(LOG_LEVEL_ERROR, "unknown daemon module '%s' requested",
escaped_module ? escaped_module : "<allocation failed>");
free(escaped_module);
*error = "requested daemon module does not exist";
return NULL;
}
if (module->read_only) {
log_message(LOG_LEVEL_ERROR, "daemon module '%s' is read only; refusing write transfer",
config->module);
*error = "requested daemon module is read only";
return NULL;
}
return module;
}
/* Per-module client-chosen ownership / super-user policy (P7 Wave E hardening):
* a daemon module refuses EVERY ownership-affecting request (--numeric-ids,
* --chown, --usermap/--groupmap, --fake-super, --copy-as, explicit --super)
* unless the operator opted THIS module in with `client owner = yes`.
* Otherwise any client could force arbitrary ownership inside the module root.
* The ownership check is evaluated against the ORIGINAL config so an explicit
* --super is refused even when an operator --no-super veto already forced the
* effective copy to OFF (the veto must not silently convert a refusal into an
* accept); when no ownership flag is present, super-user DEVICE activities are
* forced off for this connection instead. Returns an error string on refusal,
* NULL on acceptance. */
static const char* module_gate_check_ownership(const Config* config, const DaemonModule* module,
ModuleGateContext* gate_ctx) {
if (module->client_owner)
return NULL;
/* Ownership: refuse the whole transfer up front (a clear failure). */
if (identity_ownership_requested(config)) {
log_message(LOG_LEVEL_ERROR,
"daemon module '%s' refuses client-chosen ownership/super-user activities "
"(no `client owner = yes` opt-in); refusing",
config->module);
return "client-chosen ownership is not permitted by this daemon module";
}
/* Super-user DEVICE activities (char/block mknod and --write-devices) are
permitted under the default AUTO mode, so without this override a root
daemon would still let a non-opted module create arbitrary device nodes
and write raw devices. Force them off for this connection: those entries
are skipped (never mknod'ed) while an ordinary `-a` push still succeeds
without device nodes, matching the operator's least-privilege choice.
The operator-level --no-super veto is already folded into this. */
if (gate_ctx)
gate_ctx->super_mode_override = SUPER_MODE_OFF;
return NULL;
}
/* Online-guessing throttle: sleep the configured `auth failure delay`
* milliseconds after a failed authentication. Runs in the per-connection
* forked child, so it never blocks the accept loop or another connection. 0
* disables it; the parser already caps it at DAEMON_CONF_MAX_AUTH_FAILURE_DELAY_MS.
* Resumes after EINTR so a signal cannot cut the delay short. */
static void daemon_auth_failure_delay(void) {
if (!g_daemon_conf || g_daemon_conf->global.auth_failure_delay_ms <= 0)
return;
int ms = g_daemon_conf->global.auth_failure_delay_ms;
struct timespec delay;
delay.tv_sec = ms / 1000;
delay.tv_nsec = (long)(ms % 1000) * 1000000L;
while (nanosleep(&delay, &delay) != 0 && errno == EINTR)
;
}
/* Host access control (global then per-module). A configured list makes an
* unprovable peer fail closed. Deny always takes precedence over allow, and a
* non-empty allow list rejects a peer that matches none of its entries. The
* audit line names the peer, the module and the outcome. Returns an
* error string on refusal, NULL on acceptance. */
static const char* module_gate_check_hosts(const Config* config, const DaemonModule* module,
ModuleGateContext* gate_ctx) {
bool global_restricted = daemon_hosts_restricted(
g_daemon_conf->global.hosts_allow, g_daemon_conf->global.hosts_allow_count,
g_daemon_conf->global.hosts_deny, g_daemon_conf->global.hosts_deny_count);
bool module_restricted = daemon_hosts_restricted(module->hosts_allow, module->hosts_allow_count,
module->hosts_deny, module->hosts_deny_count);
if (!global_restricted && !module_restricted)
return NULL;
if (!gate_ctx || !gate_ctx->has_peer_ip) {
log_message(LOG_LEVEL_WARNING,
"daemon module '%s': cannot determine peer address with host ACLs configured; "
"refusing (fail closed)",
config->module);
return "cannot verify the client host against host access controls";
}
const char* peer = gate_ctx->peer_ip;
if (global_restricted && !daemon_hosts_allowed(peer, g_daemon_conf->global.hosts_allow,
g_daemon_conf->global.hosts_allow_count,
g_daemon_conf->global.hosts_deny,
g_daemon_conf->global.hosts_deny_count)) {
log_message(LOG_LEVEL_WARNING,
"daemon module '%s': peer %s denied by global 'hosts allow'/'hosts deny'; "
"refusing",
config->module, peer);
return "client host is not permitted by this daemon";
}
if (module_restricted &&
!daemon_hosts_allowed(peer, module->hosts_allow, module->hosts_allow_count,
module->hosts_deny, module->hosts_deny_count)) {
log_message(LOG_LEVEL_WARNING,
"daemon module '%s': peer %s denied by module 'hosts allow'/'hosts deny'; "
"refusing",
config->module, peer);
return "client host is not permitted by this daemon module";
}
return NULL;
}
/* A7 auth gate: runs the SCRAM challenge/response for an auth-required module
* BEFORE the module root is installed and before any data moves. Returns
* MODULE_AUTH_ACCEPTED when the module needs no auth or the handshake succeeds,
* MODULE_AUTH_REFUSED with *error set on a config-level rejection, or
* MODULE_AUTH_TERMINATED when the handshake already wrote a terminal status. */
static ModuleAuthResult module_gate_authenticate(const Config* config, const DaemonModule* module,
ModuleGateContext* gate_ctx, const char** error) {
if (module->auth_user_count == 0)
return MODULE_AUTH_ACCEPTED;
/* Fail closed: no store -> refuse (server misconfiguration, STATUS_ERROR). */
if (g_credentials == NULL) {
log_message(LOG_LEVEL_ERROR,
"daemon module '%s' requires authentication but no credential store is "
"configured (--password-file/--early-input); refusing",
config->module);
*error = "requested daemon module requires authentication and no credential "
"store is configured";
return MODULE_AUTH_REFUSED;
}
/* Transport policy (A7-3/S1): an auth-required module only accepts
* credentials over (a) an encrypted, verified TLS connection whose client
* certificate matches --client-cn, or (b) an actual PLAINTEXT connection
* from a loopback peer that the operator explicitly opted into with
* --allow-unauthenticated. A remote plaintext peer, an un-flagged loopback
* plaintext peer, and a loopback TLS peer whose certificate does not match
* --client-cn are all refused HERE, before the challenge is sent, so an
* unverified client never receives a nonce: the loopback allowance requires
* !gate_ctx->ssl, so --tls + --allow-unauthenticated can never be used to
* bypass the client-CN check. The operator flag never permits REMOTE
* plaintext auth: remote peers still require verified TLS regardless. */
bool tls_ok = gate_ctx && gate_ctx->ssl && SSL_get_verify_result(gate_ctx->ssl) == X509_V_OK &&
tls_client_identity_allowed(gate_ctx->ssl);
bool local_ok = allow_unauthenticated && gate_ctx && !gate_ctx->ssl && gate_ctx->fd >= 0 &&
utils_fd_peer_is_local(gate_ctx->fd);
if (!tls_ok && !local_ok) {
log_message(LOG_LEVEL_ERROR,
"daemon module '%s' requires authentication over an encrypted, verified TLS "
"connection (or an opted-in loopback plaintext transport); refusing",
config->module);
*error = "daemon module requires authentication over an encrypted, verified TLS "
"connection";
return MODULE_AUTH_REFUSED;
}
/* Belt-and-braces: the transport policy above already guarantees a context
* with a usable socket (verified TLS implies a live SSL object and loopback
* allowance requires gate_ctx->fd >= 0), so this is unreachable today; keep
* the guard so the handshake can never be driven over an invalid fd. */
if (!gate_ctx || gate_ctx->fd < 0) {
log_message(LOG_LEVEL_ERROR, "daemon module '%s': no auth transport available", config->module);
*error = "authentication failed for the requested daemon module";
return MODULE_AUTH_REFUSED;
}
/* The handshake writes exactly one terminal status on failure and signals so
* via MODULE_AUTH_TERMINATED; the username may be logged (never the password
* or any derived proof). */
if (!server_auth_handshake(gate_ctx->fd, config, module)) {
const char* peer = gate_ctx->has_peer_ip ? gate_ctx->peer_ip : "unknown";
char* escaped_user =
config->auth_user ? output_escape(config->auth_user, config->eight_bit_output) : NULL;
log_message(LOG_LEVEL_WARNING,
"daemon module '%s': authentication failed for user '%s' from %s; refusing",
config->module, escaped_user ? escaped_user : "(none)", peer);
free(escaped_user);
/* Rate-limit online guessing per connection (no delay on success). */
daemon_auth_failure_delay();
return MODULE_AUTH_TERMINATED;
}
char* escaped_user = output_escape(config->auth_user, config->eight_bit_output);
log_message(LOG_LEVEL_INFO, "daemon module '%s': user '%s' from %s authenticated", config->module,
escaped_user ? escaped_user : "<allocation failed>",
gate_ctx->has_peer_ip ? gate_ctx->peer_ip : "unknown");
free(escaped_user);
return MODULE_AUTH_ACCEPTED;
}
/* Installs the module's configured path as the connection's authorized root.
* Returns an error string when the root is unusable, NULL on success. */
static const char* module_gate_install_root(const Config* config, const DaemonModule* module) {
if (!configure_authorization(module->path)) {
log_message(LOG_LEVEL_ERROR, "daemon module '%s' path '%s' is not usable", config->module,
module->path ? module->path : "(null)");
return "requested daemon module root is not usable";
}
return NULL;
}
/* Config-frame gate (runs inside config_receive_with_validate, BEFORE the /* Config-frame gate (runs inside config_receive_with_validate, BEFORE the
* STATUS_OK ack, so a rejected connection is refused at the config handshake * STATUS_OK ack, so a rejected connection is refused at the config handshake
* and no file data is ever exchanged). * and no file data is ever exchanged).
@@ -324,115 +546,36 @@ static const char* server_module_gate(const Config* config, void* context) {
return "daemon connection did not select a module (expected a " return "daemon connection did not select a module (expected a "
"host::module/path destination)"; "host::module/path destination)";
const DaemonModule* module = daemon_conf_find_module(g_daemon_conf, config->module); const char* error = NULL;
if (module == NULL) { const DaemonModule* module = module_gate_lookup_module(config, &error);
char* escaped_module = output_escape(config->module, config->eight_bit_output); if (!module)
log_message(LOG_LEVEL_ERROR, "unknown daemon module '%s' requested", return error;
escaped_module ? escaped_module : "<allocation failed>"); /* Resolve the peer once, before any auth or ownership work, so the host ACL
free(escaped_module); * and the audit lines all use the same address. A module with ACLs fails
return "requested daemon module does not exist"; * closed when the peer cannot be classified; an ACL-free module continues
* (the accept loop still logged the address). */
if (gate_ctx) {
gate_ctx->has_peer_ip =
utils_fd_peer_ip(gate_ctx->fd, gate_ctx->peer_ip, sizeof(gate_ctx->peer_ip));
if (!gate_ctx->has_peer_ip)
log_message(LOG_LEVEL_DEBUG, "daemon module '%s': peer address unavailable", config->module);
} }
if (module->read_only) { error = module_gate_check_hosts(config, module, gate_ctx);
log_message(LOG_LEVEL_ERROR, "daemon module '%s' is read only; refusing write transfer", if (error)
config->module); return error;
return "requested daemon module is read only"; error = module_gate_check_ownership(config, module, gate_ctx);
if (error)
return error;
switch (module_gate_authenticate(config, module, gate_ctx, &error)) {
case MODULE_AUTH_REFUSED:
return error;
case MODULE_AUTH_TERMINATED:
return CONFIG_VALIDATE_ALREADY_TERMINATED;
case MODULE_AUTH_ACCEPTED:
break;
} }
/* Client-chosen ownership / super-user policy (P7 Wave E hardening): a daemon /* accepted; the authorized root is now the module's path */
module refuses EVERY ownership-affecting request (--numeric-ids, --chown, return module_gate_install_root(config, module);
--usermap/--groupmap, --fake-super, --copy-as, explicit --super) unless the
operator opted THIS module in with `client owner = yes`. Otherwise any
client could force arbitrary ownership inside the module root. The
standalone/SSH server has a single operator-authorized root and keeps
honoring these. */
if (!module->client_owner) {
/* Ownership: refuse the whole transfer up front (a clear failure).
Evaluated against the ORIGINAL config so an explicit --super is refused
even when an operator --no-super veto already forced the effective copy
to OFF (the veto must not silently convert a refusal into an accept). */
if (identity_ownership_requested(config)) {
log_message(LOG_LEVEL_ERROR,
"daemon module '%s' refuses client-chosen ownership/super-user activities "
"(no `client owner = yes` opt-in); refusing",
config->module);
return "client-chosen ownership is not permitted by this daemon module";
}
/* Super-user DEVICE activities (char/block mknod and --write-devices) are
permitted under the default AUTO mode, so without this override a root
daemon would still let a non-opted module create arbitrary device nodes
and write raw devices. Force them off for this connection: those entries
are skipped (never mknod'ed) while an ordinary `-a` push still succeeds
without device nodes, matching the operator's least-privilege choice.
The operator-level --no-super veto is already folded into this. */
if (gate_ctx)
gate_ctx->super_mode_override = SUPER_MODE_OFF;
}
if (module->auth_user_count > 0) {
/* Auth-required module (A7, protocol 2.19.0): run the SCRAM challenge/
* response BEFORE the module root is installed and before any data moves.
* Fail closed: no store -> refuse (server misconfiguration, STATUS_ERROR);
* a handshake that fails before the success response writes exactly one
* STATUS_AUTH_FAILED before signalling ALREADY_TERMINATED (a failure while
* writing the success signature instead just drops the broken connection).
* The username may be logged (never the password or any derived proof). */
if (g_credentials == NULL) {
log_message(LOG_LEVEL_ERROR,
"daemon module '%s' requires authentication but no credential store is "
"configured (--password-file/--early-input); refusing",
config->module);
return "requested daemon module requires authentication and no credential "
"store is configured";
}
/* Transport policy (A7-3/S1): an auth-required module only accepts
* credentials over (a) an encrypted, verified TLS connection whose client
* certificate matches --client-cn, or (b) an actual PLAINTEXT connection
* from a loopback peer that the operator explicitly opted into with
* --allow-unauthenticated. A remote plaintext peer, an un-flagged loopback
* plaintext peer, and a loopback TLS peer whose certificate does not match
* --client-cn are all refused HERE, before the challenge is sent, so an
* unverified client never receives a nonce: the loopback allowance requires
* !gate_ctx->ssl, so --tls + --allow-unauthenticated can never be used to
* bypass the client-CN check. The operator flag never permits REMOTE
* plaintext auth: remote peers still require verified TLS regardless. */
bool tls_ok = gate_ctx && gate_ctx->ssl && SSL_get_verify_result(gate_ctx->ssl) == X509_V_OK &&
tls_client_identity_allowed(gate_ctx->ssl);
bool local_ok = allow_unauthenticated && gate_ctx && !gate_ctx->ssl && gate_ctx->fd >= 0 &&
utils_fd_peer_is_local(gate_ctx->fd);
if (!tls_ok && !local_ok) {
log_message(LOG_LEVEL_ERROR,
"daemon module '%s' requires authentication over an encrypted, verified TLS "
"connection (or an opted-in loopback plaintext transport); refusing",
config->module);
return "daemon module requires authentication over an encrypted, verified TLS "
"connection";
}
/* Belt-and-braces: the transport policy above already guarantees a context
* with a usable socket (verified TLS implies a live SSL object and loopback
* allowance requires gate_ctx->fd >= 0), so this is unreachable today; keep
* the guard so the handshake can never be driven over an invalid fd. */
if (!gate_ctx || gate_ctx->fd < 0) {
log_message(LOG_LEVEL_ERROR, "daemon module '%s': no auth transport available",
config->module);
return "authentication failed for the requested daemon module";
}
if (!server_auth_handshake(gate_ctx->fd, config, module)) {
char* escaped_user =
config->auth_user ? output_escape(config->auth_user, config->eight_bit_output) : NULL;
log_message(LOG_LEVEL_ERROR, "daemon module '%s': authentication failed for user '%s'",
config->module, escaped_user ? escaped_user : "(none)");
free(escaped_user);
return CONFIG_VALIDATE_ALREADY_TERMINATED;
}
char* escaped_user = output_escape(config->auth_user, config->eight_bit_output);
log_message(LOG_LEVEL_INFO, "daemon module '%s': user '%s' authenticated", config->module,
escaped_user ? escaped_user : "<allocation failed>");
free(escaped_user);
}
if (!configure_authorization(module->path)) {
log_message(LOG_LEVEL_ERROR, "daemon module '%s' path '%s' is not usable", config->module,
module->path ? module->path : "(null)");
return "requested daemon module root is not usable";
}
return NULL; /* accepted; authorized root is now the module's path */
} }
void handler(int file_descriptor) { void handler(int file_descriptor) {
@@ -445,12 +588,18 @@ void handler(int file_descriptor) {
gate_ctx.ssl = ssl; gate_ctx.ssl = ssl;
gate_ctx.fd = file_descriptor; gate_ctx.fd = file_descriptor;
gate_ctx.super_mode_override = -1; gate_ctx.super_mode_override = -1;
Config* config = config_receive_with_validate(file_descriptor, server_module_gate, &gate_ctx); gate_ctx.has_peer_ip = false;
gate_ctx.peer_ip[0] = '\0';
/* All teardown state starts empty so the single `done` epilogue is safe to
* reach from any error path (including before the config frame arrives). */
Config* config = NULL;
PipelineContextReceiver* context = NULL;
char* joined_destination = NULL;
bool charset_ready = false;
config = config_receive_with_validate(file_descriptor, server_module_gate, &gate_ctx);
if (config == NULL) { if (config == NULL) {
log_message(LOG_LEVEL_ERROR, "Failed to receive config"); log_message(LOG_LEVEL_ERROR, "Failed to receive config");
close(file_descriptor); goto done;
protocol_session_unbind();
return;
} }
/* Apply the super-mode veto the gate decided on (operator --no-super, or a /* Apply the super-mode veto the gate decided on (operator --no-super, or a
* daemon module without the `client owner = yes` opt-in) exactly once, so * daemon module without the `client owner = yes` opt-in) exactly once, so
@@ -458,27 +607,25 @@ void handler(int file_descriptor) {
* device-node creation) sees SUPER_MODE_OFF. The gate never mutated the * device-node creation) sees SUPER_MODE_OFF. The gate never mutated the
* received config. */ * received config. */
if (gate_ctx.super_mode_override != -1) if (gate_ctx.super_mode_override != -1)
config->super_mode = gate_ctx.super_mode_override; config->super_mode = (SuperMode)gate_ctx.super_mode_override;
protocol_set_8_bit_output(config->eight_bit_output); protocol_set_8_bit_output(config->eight_bit_output);
/* Server-side per-message protocol deadline for every frame from here on.
* `timeout` is not serialized, so this is the server's own config (the server
* has no --timeout CLI and defaults it to 0): the built-in 60 s window stays
* in effect. A client's --timeout tightens only that client's own protocol
* I/O and the server's socket read/write timeout is the transport default. */
protocol_session_set_io_timeout(&session, config->timeout);
if (!authorized_root) { if (!authorized_root) {
log_message(LOG_LEVEL_ERROR, "No server-side destination root configured"); log_message(LOG_LEVEL_ERROR, "No server-side destination root configured");
config_delete(config); goto done;
close(file_descriptor);
protocol_session_unbind();
return;
} }
if (!allow_unauthenticated && ssl == NULL) { if (!allow_unauthenticated && ssl == NULL) {
log_message(LOG_LEVEL_ERROR, "Rejected unauthenticated plaintext connection"); log_message(LOG_LEVEL_ERROR, "Rejected unauthenticated plaintext connection");
config_delete(config); goto done;
close(file_descriptor);
protocol_session_unbind();
return;
} }
if (ssl && required_client_cn && !tls_client_identity_allowed(ssl)) { if (ssl && required_client_cn && !tls_client_identity_allowed(ssl)) {
log_message(LOG_LEVEL_ERROR, "Rejected TLS client with unauthorized identity"); log_message(LOG_LEVEL_ERROR, "Rejected TLS client with unauthorized identity");
config_delete(config); goto done;
close(file_descriptor);
return;
} }
/* Daemon mode: the module's root is the authorized root (installed by /* Daemon mode: the module's root is the authorized root (installed by
server_module_gate), and the client's destination is a MODULE-RELATIVE server_module_gate), and the client's destination is a MODULE-RELATIVE
@@ -488,13 +635,9 @@ void handler(int file_descriptor) {
if (g_daemon_conf && config->receive_root_directory && config->receive_root_directory[0] == '/') { if (g_daemon_conf && config->receive_root_directory && config->receive_root_directory[0] == '/') {
log_message(LOG_LEVEL_ERROR, "Rejected absolute daemon destination (must be relative to the " log_message(LOG_LEVEL_ERROR, "Rejected absolute daemon destination (must be relative to the "
"selected module root)"); "selected module root)");
config_delete(config); goto done;
close(file_descriptor);
protocol_session_unbind();
return;
} }
char* destination = config->receive_root_directory; char* destination = config->receive_root_directory;
char* joined_destination = NULL;
if (destination && destination[0] != '/') if (destination && destination[0] != '/')
joined_destination = path_cat(authorized_root, destination); joined_destination = path_cat(authorized_root, destination);
if (joined_destination) if (joined_destination)
@@ -503,19 +646,16 @@ void handler(int file_descriptor) {
!path_is_within(authorized_root, destination)) { !path_is_within(authorized_root, destination)) {
log_message(LOG_LEVEL_ERROR, "Rejected destination outside authorized root"); log_message(LOG_LEVEL_ERROR, "Rejected destination outside authorized root");
free(joined_destination); free(joined_destination);
config_delete(config); joined_destination = NULL;
close(file_descriptor); goto done;
return;
} }
if (joined_destination) { if (joined_destination) {
free(config->receive_root_directory); free(config->receive_root_directory);
config->receive_root_directory = joined_destination; config->receive_root_directory = joined_destination;
joined_destination = NULL;
} }
if (!config->receive_root_directory) { if (!config->receive_root_directory) {
config_delete(config); goto done;
close(file_descriptor);
protocol_session_unbind();
return;
} }
config->use_delete = config->use_delete && allow_delete; config->use_delete = config->use_delete && allow_delete;
/* --iconv (protocol 2.16.0): install the receiver-side wire->local conversion /* --iconv (protocol 2.16.0): install the receiver-side wire->local conversion
@@ -524,13 +664,13 @@ void handler(int file_descriptor) {
any) may override the local charset; a spec the client is known to have any) may override the local charset; a spec the client is known to have
validated cannot fail here unless the server's override names an validated cannot fail here unless the server's override names an
unsupported charset. */ unsupported charset. */
if (config->iconv_spec && !charset_wire_init_receiver(config->iconv_spec, server_iconv_spec)) { if (config->iconv_spec) {
log_message(LOG_LEVEL_ERROR, if (!charset_wire_init_receiver(config->iconv_spec, server_iconv_spec)) {
"--iconv: unsupported charset conversion requested (LOCAL[,REMOTE])"); log_message(LOG_LEVEL_ERROR,
config_delete(config); "--iconv: unsupported charset conversion requested (LOCAL[,REMOTE])");
close(file_descriptor); goto done;
protocol_session_unbind(); }
return; charset_ready = true;
} }
/* --delete-missing-args deletes destination mirrors receiver-side, so it is /* --delete-missing-args deletes destination mirrors receiver-side, so it is
deletion and stays gated by the same --allow-delete server policy. When deletion and stays gated by the same --allow-delete server policy. When
@@ -545,10 +685,7 @@ void handler(int file_descriptor) {
log_message(LOG_LEVEL_ERROR, "destination root is not available: %s", log_message(LOG_LEVEL_ERROR, "destination root is not available: %s",
escaped_root ? escaped_root : "<allocation failed>"); escaped_root ? escaped_root : "<allocation failed>");
free(escaped_root); free(escaped_root);
config_delete(config); goto done;
close(file_descriptor);
protocol_session_unbind();
return;
} }
/* A --delay-updates transfer stages under a private 0700 directory inside /* A --delay-updates transfer stages under a private 0700 directory inside
the receive root. Create it up front (wiping leftovers of any previously the receive root. Create it up front (wiping leftovers of any previously
@@ -557,11 +694,7 @@ void handler(int file_descriptor) {
config->delay_context = delay_updates_context_create(config->receive_root_directory); config->delay_context = delay_updates_context_create(config->receive_root_directory);
if (!config->delay_context || !delay_updates_prepare(config->delay_context)) { if (!config->delay_context || !delay_updates_prepare(config->delay_context)) {
log_message(LOG_LEVEL_ERROR, "Failed to initialize --delay-updates staging area"); log_message(LOG_LEVEL_ERROR, "Failed to initialize --delay-updates staging area");
delay_updates_cleanup(config->delay_context); goto done;
config_delete(config);
close(file_descriptor);
protocol_session_unbind();
return;
} }
} }
/* Preserve the negotiated identity policy for the fd-relative ownership /* Preserve the negotiated identity policy for the fd-relative ownership
@@ -571,10 +704,7 @@ void handler(int file_descriptor) {
rather than silently applying the wrong ownership policy. */ rather than silently applying the wrong ownership policy. */
if (!identity_set_active(config)) { if (!identity_set_active(config)) {
log_message(LOG_LEVEL_ERROR, "Failed to activate identity policy"); log_message(LOG_LEVEL_ERROR, "Failed to activate identity policy");
config_delete(config); goto done;
close(file_descriptor);
protocol_session_unbind();
return;
} }
/* Persist the negotiated --keep-dirlinks policy once, here at config-accept, /* Persist the negotiated --keep-dirlinks policy once, here at config-accept,
before any multithreaded receiver/writer threads are spawned, so the before any multithreaded receiver/writer threads are spawned, so the
@@ -605,38 +735,26 @@ void handler(int file_descriptor) {
if (!motd_send(file_descriptor, motd ? motd : "")) { if (!motd_send(file_descriptor, motd ? motd : "")) {
free(motd); free(motd);
log_message(LOG_LEVEL_ERROR, "Failed to send daemon MOTD"); log_message(LOG_LEVEL_ERROR, "Failed to send daemon MOTD");
config_delete(config); goto done;
close(file_descriptor);
protocol_session_unbind();
identity_clear_active();
return;
} }
free(motd); free(motd);
} }
if (config->use_multithreading) { if (config->use_multithreading) {
Queue* q = queue_create(100, file_destroy); Queue* q = queue_create(100, file_destroy);
if (q == NULL) { if (q == NULL)
config_delete(config); goto done;
close(file_descriptor); context = pipeline_context_receiver_create(config, q, file_descriptor, ssl);
protocol_session_unbind();
identity_clear_active();
return;
}
PipelineContextReceiver* context =
pipeline_context_receiver_create(config, q, file_descriptor, ssl);
if (context == NULL) { if (context == NULL) {
queue_destroy(q); queue_destroy(q);
config_delete(config); goto done;
close(file_descriptor);
protocol_session_unbind();
identity_clear_active();
return;
} }
protocol_session_set_max_alloc(&context->session, config->max_alloc); protocol_session_set_max_alloc(&context->session, config->max_alloc);
protocol_session_set_io_timeout(&context->session, config->timeout);
atomic_store(&context->session.total_allocated_bytes, atomic_store(&context->session.total_allocated_bytes,
atomic_load(&session.total_allocated_bytes)); atomic_load(&session.total_allocated_bytes));
pipeline_context_receiver_set_queue_byte_limit(context, RECEIVER_QUEUE_MAX_BYTES); pipeline_context_receiver_set_queue_byte_limit(context, RECEIVER_QUEUE_MAX_BYTES);
thrd_t receiver, writer; thrd_t receiver = {0};
thrd_t writer = {0};
bool receiver_created = thrd_create(&receiver, receive_thread, context) == thrd_success; bool receiver_created = thrd_create(&receiver, receive_thread, context) == thrd_success;
bool writer_created = false; bool writer_created = false;
if (receiver_created) if (receiver_created)
@@ -649,17 +767,19 @@ void handler(int file_descriptor) {
cnd_broadcast(&context->condition_not_full); cnd_broadcast(&context->condition_not_full);
cnd_broadcast(&context->condition_not_empty); cnd_broadcast(&context->condition_not_empty);
mtx_unlock(&context->mutex); mtx_unlock(&context->mutex);
close(file_descriptor); /* Unblock a worker parked in socket I/O without closing the fd (the
* child owns the single close). shutdown() only affects sockets; for
* the --stdio pipe the receiver's per-message poll timeout still
* bounds the join, so do nothing there rather than close a descriptor
* another thread may still be using. */
struct stat fd_stat;
if (fstat(file_descriptor, &fd_stat) == 0 && S_ISSOCK(fd_stat.st_mode))
shutdown(file_descriptor, SHUT_RDWR);
thrd_join(receiver, NULL); thrd_join(receiver, NULL);
} else {
close(file_descriptor);
} }
if (writer_created) if (writer_created)
thrd_join(writer, NULL); thrd_join(writer, NULL);
pipeline_context_receiver_destroy(context); goto done;
protocol_session_unbind();
identity_clear_active();
return;
} }
int receiver_result; int receiver_result;
int writer_result; int writer_result;
@@ -703,21 +823,36 @@ void handler(int file_descriptor) {
} else { } else {
send_status(file_descriptor, STATUS_ERROR); send_status(file_descriptor, STATUS_ERROR);
} }
if (!transfer_ok) { if (!transfer_ok)
log_message(LOG_LEVEL_ERROR, "Transfer failed"); log_message(LOG_LEVEL_ERROR, "Transfer failed");
if (config->delay_updates && config->delay_context)
delay_updates_cleanup(config->delay_context);
}
pipeline_context_receiver_destroy(context);
} else { } else {
if (receiver_receive_files(config, file_descriptor) != 0) if (receiver_receive_files(config, file_descriptor) != 0)
log_message(LOG_LEVEL_ERROR, "Transfer failed"); log_message(LOG_LEVEL_ERROR, "Transfer failed");
config_delete(config);
} }
protocol_session_unbind();
done:
/* Single cleanup epilogue: every error path jumps here, so the iconv
* receiver conversion is released, the identity snapshot cleared, the
* protocol session unbound and the config freed exactly once. The
* connection fd is deliberately NOT closed here -- the child functions own
* its single close (plain_child_fn / tls_child_fn), and the --stdio call
* site must leave stdin/stdout open. */
if (charset_ready)
charset_wire_free();
/* The delay-updates staging tree is released by config_delete (which the
branch below always reaches), so it is cleaned exactly once. */
identity_clear_active(); identity_clear_active();
charset_wire_free(); protocol_session_unbind();
close(file_descriptor); if (context != NULL) {
/* context owns both the config and the queue it was created with. */
pipeline_context_receiver_destroy(context);
context = NULL;
config = NULL;
} else {
config_delete(config);
config = NULL;
}
free(joined_destination);
} }
#ifndef FASTSYNC_SERVER_AS_LIB #ifndef FASTSYNC_SERVER_AS_LIB
@@ -744,7 +879,8 @@ static void print_server_usage(void) {
printf(" --config=FILE Daemon config file (default: ~/.config/fastsync/\n"); printf(" --config=FILE Daemon config file (default: ~/.config/fastsync/\n");
printf(" fastsyncd.conf, else /etc/fastsyncd.conf)\n"); printf(" fastsyncd.conf, else /etc/fastsyncd.conf)\n");
printf(" --dparam=KEY=VALUE Override one global config key on the command line\n"); printf(" --dparam=KEY=VALUE Override one global config key on the command line\n");
printf(" (port, motd file, address)\n"); printf(" (port, motd file, address, max connections,\n");
printf(" auth failure delay, hosts allow, hosts deny)\n");
printf(" --no-detach Stay in the foreground (default detaches to\n"); printf(" --no-detach Stay in the foreground (default detaches to\n");
printf(" background when running --daemon)\n"); printf(" background when running --daemon)\n");
printf(" --password-file=FILE Credential store for modules that declare\n"); printf(" --password-file=FILE Credential store for modules that declare\n");
@@ -912,6 +1048,9 @@ int main(int argc, char* argv[]) {
return 1; return 1;
} }
io_set_fds(STDIN_FILENO, STDOUT_FILENO); io_set_fds(STDIN_FILENO, STDOUT_FILENO);
/* handler() does not own the stdio fds: it never closes its descriptor
* argument, so STDIN/STDOUT stay open for this (single-shot) SSH session
* and are released by process exit. */
handler(STDIN_FILENO); handler(STDIN_FILENO);
release_authorization(); release_authorization();
server_cli_options_free(&opts); server_cli_options_free(&opts);
@@ -956,6 +1095,12 @@ int main(int argc, char* argv[]) {
"and device nodes within that module root -- pair it with `auth users` " "and device nodes within that module root -- pair it with `auth users` "
"unless the module is intentionally open to the network", "unless the module is intentionally open to the network",
g_daemon_conf->modules[i].name); g_daemon_conf->modules[i].name);
if (g_daemon_conf->modules[i].max_connections > 0)
log_message(LOG_LEVEL_WARNING,
"daemon module '%s': per-module 'max connections' is stored but not enforced "
"per module; the global 'max connections' cap (%d) applies to the whole "
"listener",
g_daemon_conf->modules[i].name, g_daemon_conf->global.max_connections);
} }
/* Daemon credential store (Wave B). --password-file and --early-input /* Daemon credential store (Wave B). --password-file and --early-input
* feed the same store, loaded BEFORE the listener forks so every * feed the same store, loaded BEFORE the listener forks so every
@@ -1020,6 +1165,8 @@ int main(int argc, char* argv[]) {
exit_code = 1; exit_code = 1;
goto out; goto out;
} }
if (g_daemon_conf)
server_set_max_connections(g_server, (unsigned int)g_daemon_conf->global.max_connections);
if (opts.use_tls) { if (opts.use_tls) {
if (!opts.tls_cert || !opts.tls_key || !opts.tls_ca || !opts.client_cn) { if (!opts.tls_cert || !opts.tls_key || !opts.tls_ca || !opts.client_cn) {
fprintf(stderr, "Error: --tls requires --cert, --key, --ca, and --client-cn\n"); fprintf(stderr, "Error: --tls requires --cert, --key, --ca, and --client-cn\n");
+3
View File
@@ -1,6 +1,7 @@
#include "log.h" #include "log.h"
#include "array_list.h" #include "array_list.h"
#include "protocol.h" #include "protocol.h"
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -39,6 +40,8 @@ void array_list_delete(ArrayList* array_list) {
static bool array_list_extend(ArrayList* array_list) { static bool array_list_extend(ArrayList* array_list) {
if (array_list == NULL) if (array_list == NULL)
return false; return false;
if (array_list->capacity > INT_MAX / 2)
return false;
int new_capacity = array_list->capacity * 2; int new_capacity = array_list->capacity * 2;
if (new_capacity == 0) if (new_capacity == 0)
new_capacity = INITIAL_ARRAY_SIZE; new_capacity = INITIAL_ARRAY_SIZE;
+55 -97
View File
@@ -207,17 +207,20 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
return NULL; return NULL;
char* data_pointer = data->data; char* data_pointer = data->data;
size_t remaining_size = data->size; size_t remaining_size = data->size;
/* The element currently being parsed is owned by `files` only after the
* array_list_add() at the end of the iteration; until then the error
* epilogue destroys it directly. Keeping this one pointer nulled after the
* hand-off makes the single cleanup path correct for every failure. */
File* file = NULL;
while (remaining_size > 0) { while (remaining_size > 0) {
if ((unsigned int)files->size >= MAX_FILES_PER_CHUNK) { if ((unsigned int)files->size >= MAX_FILES_PER_CHUNK) {
log_message(LOG_LEVEL_ERROR, "Chunk contains too many files"); log_message(LOG_LEVEL_ERROR, "Chunk contains too many files");
array_list_delete(files); goto error;
return NULL;
} }
if (remaining_size < sizeof(size_t)) { if (remaining_size < sizeof(size_t)) {
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for path length"); log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for path length");
array_list_delete(files); goto error;
return NULL;
} }
size_t path_len; size_t path_len;
@@ -227,26 +230,19 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
if (path_len > SIZE_MAX - 1 || remaining_size < path_len) { if (path_len > SIZE_MAX - 1 || remaining_size < path_len) {
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for path"); log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for path");
array_list_delete(files); goto error;
return NULL;
} }
if (path_len == SIZE_MAX) {
array_list_delete(files);
return NULL;
}
char* path = protocol_alloc(path_len + 1); char* path = protocol_alloc(path_len + 1);
if (path == NULL) { if (path == NULL) {
log_perror("Could not allocate memory for file path"); log_perror("Could not allocate memory for file path");
array_list_delete(files); goto error;
return NULL;
} }
memcpy(path, data_pointer, path_len); memcpy(path, data_pointer, path_len);
path[path_len] = '\0'; path[path_len] = '\0';
if (memchr(path, '\0', path_len) != NULL) { if (memchr(path, '\0', path_len) != NULL) {
free(path); free(path);
array_list_delete(files); goto error;
return NULL;
} }
data_pointer += path_len; data_pointer += path_len;
remaining_size -= path_len; remaining_size -= path_len;
@@ -260,8 +256,7 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
if (local_path == NULL) { if (local_path == NULL) {
log_message(LOG_LEVEL_ERROR, log_message(LOG_LEVEL_ERROR,
"--iconv: received chunk file name cannot be converted to the local charset"); "--iconv: received chunk file name cannot be converted to the local charset");
array_list_delete(files); goto error;
return NULL;
} }
path = local_path; path = local_path;
path_len = strlen(path); path_len = strlen(path);
@@ -269,30 +264,23 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
if (path_len == 0 || has_path_traversal(path)) { if (path_len == 0 || has_path_traversal(path)) {
free(path); free(path);
array_list_delete(files); goto error;
return NULL;
} }
File* file = file_create(path); file = file_create(path);
free(path); free(path);
if (file == NULL) { if (file == NULL)
array_list_delete(files); goto error;
return NULL;
}
if (remaining_size < sizeof(int)) { if (remaining_size < sizeof(int)) {
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for entry type"); log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for entry type");
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
int entry_type; int entry_type;
memcpy(&entry_type, data_pointer, sizeof(int)); memcpy(&entry_type, data_pointer, sizeof(int));
if (entry_type != 0 && entry_type != 1 && entry_type != 2 && entry_type != 3) { if (entry_type != 0 && entry_type != 1 && entry_type != 2 && entry_type != 3) {
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: bad entry type"); log_message(LOG_LEVEL_ERROR, "Invalid chunk format: bad entry type");
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
file->is_dir = entry_type == 1; file->is_dir = entry_type == 1;
file->is_symlink = entry_type == 2; file->is_symlink = entry_type == 2;
@@ -303,9 +291,7 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
if (file->is_special) { if (file->is_special) {
if (remaining_size < 2 * (int32_t)sizeof(int32_t)) { if (remaining_size < 2 * (int32_t)sizeof(int32_t)) {
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for special rdev"); log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for special rdev");
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
int32_t special_major, special_minor; int32_t special_major, special_minor;
memcpy(&special_major, data_pointer, sizeof(special_major)); memcpy(&special_major, data_pointer, sizeof(special_major));
@@ -320,9 +306,7 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
if (special_major < 0 || special_minor < 0 || special_major > 0xffff || if (special_major < 0 || special_minor < 0 || special_major > 0xffff ||
special_minor > 0x00ffffff) { special_minor > 0x00ffffff) {
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: out-of-range special rdev"); log_message(LOG_LEVEL_ERROR, "Invalid chunk format: out-of-range special rdev");
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
file->rdev_major = special_major; file->rdev_major = special_major;
file->rdev_minor = special_minor; file->rdev_minor = special_minor;
@@ -331,37 +315,32 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
if (use_metadata) { if (use_metadata) {
if (remaining_size < sizeof(int)) { if (remaining_size < sizeof(int)) {
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for metadata"); log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for metadata");
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
// Peek at present flag to determine total size needed before reading /* Peek at the present flag to determine the total record size before
decoding. metadata_from_buf() independently bounds-checks every read
against remaining_size, so a short body can never over-read. */
int present_flag; int present_flag;
memcpy(&present_flag, data_pointer, sizeof(int)); memcpy(&present_flag, data_pointer, sizeof(int));
if ((present_flag != 0 && present_flag != 1) || if ((present_flag != 0 && present_flag != 1) ||
(present_flag == 1 && remaining_size < sizeof(int) + FILE_METADATA_WIRE_SIZE)) { (present_flag == 1 && remaining_size < sizeof(int) + FILE_METADATA_WIRE_SIZE)) {
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for metadata body"); log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for metadata body");
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
file->metadata = metadata_from_buf(&data_pointer); file->metadata = metadata_from_buf((const uint8_t*)data_pointer, remaining_size);
remaining_size -= sizeof(int); size_t metadata_consumed = sizeof(int);
if (present_flag == 1) { if (present_flag == 1) {
if (file->metadata == NULL) { if (file->metadata == NULL)
file_destroy(file); goto error;
array_list_delete(files); metadata_consumed += FILE_METADATA_WIRE_SIZE;
return NULL;
}
remaining_size -= FILE_METADATA_WIRE_SIZE;
} }
data_pointer += metadata_consumed;
remaining_size -= metadata_consumed;
} }
if (remaining_size < sizeof(size_t)) { if (remaining_size < sizeof(size_t)) {
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for data size"); log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for data size");
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
size_t file_data_size; size_t file_data_size;
@@ -371,35 +350,26 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
if (remaining_size < file_data_size) { if (remaining_size < file_data_size) {
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for file content"); log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for file content");
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
// Reject individual file data larger than the maximum allowed size. // Reject individual file data larger than the maximum allowed size.
if (file_data_size > MAX_FILE_DATA_SIZE) { if (file_data_size > MAX_FILE_DATA_SIZE) {
log_message(LOG_LEVEL_ERROR, "File data size %zu exceeds maximum %llu", file_data_size, log_message(LOG_LEVEL_ERROR, "File data size %zu exceeds maximum %llu", file_data_size,
(unsigned long long)MAX_FILE_DATA_SIZE); (unsigned long long)MAX_FILE_DATA_SIZE);
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
size_t allocation_size = file_data_size > 0 ? file_data_size : 1; size_t allocation_size = file_data_size > 0 ? file_data_size : 1;
void* file_data = protocol_alloc(allocation_size); void* file_data = protocol_alloc(allocation_size);
if (file_data == NULL) { if (file_data == NULL) {
log_perror("Could not allocate memory for file data"); log_perror("Could not allocate memory for file data");
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
memcpy(file_data, data_pointer, file_data_size); memcpy(file_data, data_pointer, file_data_size);
Data* replacement = data_create(file_data, file_data_size); Data* replacement = data_create(file_data, file_data_size);
if (replacement == NULL) { if (replacement == NULL)
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
}
data_destroy(file->data); data_destroy(file->data);
file->data = replacement; file->data = replacement;
data_pointer += file_data_size; data_pointer += file_data_size;
@@ -408,9 +378,7 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
if (file->is_symlink) { if (file->is_symlink) {
if (remaining_size < sizeof(size_t)) { if (remaining_size < sizeof(size_t)) {
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for symlink target"); log_message(LOG_LEVEL_ERROR, "Invalid chunk format: not enough data for symlink target");
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
size_t target_len; size_t target_len;
memcpy(&target_len, data_pointer, sizeof(size_t)); memcpy(&target_len, data_pointer, sizeof(size_t));
@@ -418,24 +386,18 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
remaining_size -= sizeof(size_t); remaining_size -= sizeof(size_t);
if (target_len == 0 || remaining_size < target_len) { if (target_len == 0 || remaining_size < target_len) {
log_message(LOG_LEVEL_ERROR, "Invalid chunk format: bad symlink target"); log_message(LOG_LEVEL_ERROR, "Invalid chunk format: bad symlink target");
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
char* target = protocol_alloc(target_len + 1); char* target = protocol_alloc(target_len + 1);
if (!target) { if (!target) {
log_perror("Could not allocate memory for symlink target"); log_perror("Could not allocate memory for symlink target");
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
memcpy(target, data_pointer, target_len); memcpy(target, data_pointer, target_len);
target[target_len] = '\0'; target[target_len] = '\0';
if (memchr(target, '\0', target_len) != NULL) { if (memchr(target, '\0', target_len) != NULL) {
free(target); free(target);
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
/* The symlink target also rides the wire charset; decode it to the local /* The symlink target also rides the wire charset; decode it to the local
charset like the path (a target is a path). */ charset like the path (a target is a path). */
@@ -446,9 +408,7 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
log_message(LOG_LEVEL_ERROR, log_message(LOG_LEVEL_ERROR,
"--iconv: received chunk symlink target cannot be converted to the local " "--iconv: received chunk symlink target cannot be converted to the local "
"charset"); "charset");
file_destroy(file); goto error;
array_list_delete(files);
return NULL;
} }
target = local_target; target = local_target;
} }
@@ -457,29 +417,27 @@ Chunk* chunk_deserialize(Data* data, bool use_metadata) {
remaining_size -= target_len; remaining_size -= target_len;
} }
if (!array_list_add(files, file)) { if (!array_list_add(files, file))
file_destroy(file); goto error;
array_list_delete(files); file = NULL;
return NULL;
}
} }
File** file_array = (File**)array_list_to_array(files); File** file_array = (File**)array_list_to_array(files);
if (files->size > 0 && file_array == NULL) { if (files->size > 0 && file_array == NULL)
array_list_delete(files); goto error;
return NULL;
}
Chunk* chunk = chunk_create(file_array, files->size); Chunk* chunk = chunk_create(file_array, files->size);
free(file_array); free(file_array);
if (chunk == NULL) { if (chunk == NULL)
array_list_delete(files); goto error;
return NULL;
}
files->item_destroyer = NULL; files->item_destroyer = NULL;
array_list_delete(files); array_list_delete(files);
return chunk; return chunk;
error:
if (file)
file_destroy(file);
array_list_delete(files);
return NULL;
} }
Data* chunk_compress(Chunk* chunk, int compression_level, bool use_metadata) { Data* chunk_compress(Chunk* chunk, int compression_level, bool use_metadata) {
+182 -49
View File
@@ -2,11 +2,12 @@
#include "data.h" #include "data.h"
#include "log.h" #include "log.h"
#include "protocol.h" #include "protocol.h"
#include <stdlib.h>
#include <limits.h> #include <limits.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#include <threads.h>
#include <unistd.h> #include <unistd.h>
#include <zstd.h> #include <zstd.h>
@@ -16,10 +17,6 @@
static char* SKIP_COMPRESSION_EXTENSIONS[] = {".jpg", ".jpeg", ".png", ".gif", ".mp4", ".mkv", static char* SKIP_COMPRESSION_EXTENSIONS[] = {".jpg", ".jpeg", ".png", ".gif", ".mp4", ".mkv",
".zip", ".gz", ".xz", ".zst", NULL}; ".zip", ".gz", ".xz", ".zst", NULL};
bool compression_should_skip(const char* path) {
return compression_should_skip_with_suffixes(path, NULL, -1);
}
bool compression_should_skip_with_suffixes(const char* path, char* const* suffixes, int count) { bool compression_should_skip_with_suffixes(const char* path, char* const* suffixes, int count) {
if (!path) if (!path)
return false; return false;
@@ -39,6 +36,96 @@ bool compression_should_skip_with_suffixes(const char* path, char* const* suffix
return false; return false;
} }
/* Per-thread cache of zstd contexts plus the grow-only compression scratch
* buffer. zstd contexts are stateful and not safe to share between threads,
* so each thread keeps its own (see compression_get_thread_ctx). The cache is
* stored in a C11 thread-specific storage slot whose destructor releases the
* contexts when the thread exits; this keeps LeakSanitizer clean for the
* short-lived sender/receiver/scanner worker threads without every worker
* entry point having to remember to call compression_free_thread_contexts().
* The main thread's slot is not torn down by tss at process exit, so an atexit
* hook releases it (and compression_free_thread_contexts allows eager
* release). */
typedef struct {
ZSTD_CCtx* cctx;
ZSTD_DCtx* dctx;
void* out_buf; /* reusable ZSTD_compressBound-sized output scratch */
size_t out_cap; /* bytes currently allocated for out_buf */
int level; /* compression level currently applied to cctx */
int workers; /* nbWorkers currently applied to cctx */
bool params_set;
bool cached; /* false when the TSS slot could not be used: caller owns */
} CompressionThreadCtx;
static once_flag compression_tls_once = ONCE_FLAG_INIT;
static tss_t compression_tls_key;
static bool compression_tls_ready;
static void compression_tls_make_key(void);
static void compression_ctx_free(CompressionThreadCtx* ctx) {
if (!ctx)
return;
if (ctx->cctx)
ZSTD_freeCCtx(ctx->cctx);
if (ctx->dctx)
ZSTD_freeDCtx(ctx->dctx);
free(ctx->out_buf);
free(ctx);
}
static void compression_tls_destructor(void* value) {
compression_ctx_free((CompressionThreadCtx*)value);
}
void compression_free_thread_contexts(void) {
call_once(&compression_tls_once, compression_tls_make_key);
if (!compression_tls_ready)
return;
CompressionThreadCtx* ctx = (CompressionThreadCtx*)tss_get(compression_tls_key);
if (!ctx)
return;
/* Clear the slot first so the thread-exit destructor cannot free it twice. */
tss_set(compression_tls_key, NULL);
compression_ctx_free(ctx);
}
static void compression_atexit_cleanup(void) {
compression_free_thread_contexts();
}
static void compression_tls_make_key(void) {
if (tss_create(&compression_tls_key, compression_tls_destructor) == thrd_success) {
compression_tls_ready = true;
atexit(compression_atexit_cleanup);
}
}
static CompressionThreadCtx* compression_get_thread_ctx(void) {
call_once(&compression_tls_once, compression_tls_make_key);
if (!compression_tls_ready) {
/* Extremely unlikely: fall back to an uncached context the caller frees. */
return (CompressionThreadCtx*)calloc(1, sizeof(CompressionThreadCtx));
}
CompressionThreadCtx* ctx = (CompressionThreadCtx*)tss_get(compression_tls_key);
if (ctx)
return ctx;
ctx = (CompressionThreadCtx*)calloc(1, sizeof(CompressionThreadCtx));
if (!ctx)
return NULL;
ctx->cached = true;
if (tss_set(compression_tls_key, ctx) != thrd_success)
ctx->cached = false;
return ctx;
}
/* Release an uncached context immediately; cached contexts are owned by the
* thread's TSS slot and freed on thread exit / compression_free_thread_contexts. */
static void compression_ctx_put(CompressionThreadCtx* ctx) {
if (ctx && !ctx->cached)
compression_ctx_free(ctx);
}
Data* data_compress(Data* data_to_compress, int compression_level) { Data* data_compress(Data* data_to_compress, int compression_level) {
return data_compress_with_threads(data_to_compress, compression_level, 0); return data_compress_with_threads(data_to_compress, compression_level, 0);
} }
@@ -50,68 +137,102 @@ Data* data_compress_with_threads(Data* data_to_compress, int compression_level,
return NULL; return NULL;
log_message(LOG_LEVEL_DEBUG, "Starting to compress data"); log_message(LOG_LEVEL_DEBUG, "Starting to compress data");
size_t dst_size = ZSTD_compressBound(data_to_compress->size); size_t dst_size = ZSTD_compressBound(data_to_compress->size);
Data* compressed_data = data_create_empty(dst_size);
if (compressed_data == NULL)
return NULL;
ZSTD_CCtx* cctx = ZSTD_createCCtx(); CompressionThreadCtx* ctx = compression_get_thread_ctx();
if (!cctx) { if (ctx == NULL) {
log_message(LOG_LEVEL_ERROR, "Failed to create ZSTD compression context"); log_message(LOG_LEVEL_ERROR, "Failed to allocate ZSTD compression context");
data_destroy(compressed_data);
return NULL; return NULL;
} }
Data* compressed_data = NULL;
size_t zret = ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compression_level); if (!ctx->cctx) {
if (ZSTD_isError(zret)) { ctx->cctx = ZSTD_createCCtx();
log_message(LOG_LEVEL_ERROR, "Failed to set compression level: %s", ZSTD_getErrorName(zret)); if (!ctx->cctx) {
ZSTD_freeCCtx(cctx); log_message(LOG_LEVEL_ERROR, "Failed to create ZSTD compression context");
data_destroy(compressed_data); goto cleanup;
return NULL; }
ctx->params_set = false;
} }
/* Reset only the session: parameters (and any already-allocated zstd worker
* pool) stay attached to the context, so compressing the next file does not
* rebuild the pool. */
ZSTD_CCtx_reset(ctx->cctx, ZSTD_reset_session_only);
if (!ctx->params_set || ctx->level != compression_level) {
size_t zret = ZSTD_CCtx_setParameter(ctx->cctx, ZSTD_c_compressionLevel, compression_level);
if (ZSTD_isError(zret)) {
log_message(LOG_LEVEL_ERROR, "Failed to set compression level: %s", ZSTD_getErrorName(zret));
goto cleanup;
}
ctx->level = compression_level;
}
int available_threads = 0;
if (compression_threads > 0) { if (compression_threads > 0) {
long online_cpus = sysconf(_SC_NPROCESSORS_ONLN); long online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
int available_threads = online_cpus > 0 && online_cpus < compression_threads available_threads = online_cpus > 0 && online_cpus < compression_threads ? (int)online_cpus
? (int)online_cpus : compression_threads;
: compression_threads; }
zret = ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, available_threads); if (!ctx->params_set || ctx->workers != available_threads) {
size_t zret = ZSTD_CCtx_setParameter(ctx->cctx, ZSTD_c_nbWorkers, available_threads);
if (ZSTD_isError(zret)) { if (ZSTD_isError(zret)) {
log_message(LOG_LEVEL_ERROR, "Failed to set compression threads: %s", log_message(LOG_LEVEL_ERROR, "Failed to set compression threads: %s",
ZSTD_getErrorName(zret)); ZSTD_getErrorName(zret));
ZSTD_freeCCtx(cctx); goto cleanup;
data_destroy(compressed_data);
return NULL;
} }
ctx->workers = available_threads;
}
ctx->params_set = true;
if (available_threads > 0) {
/* Streaming compression needs the source size before threaded mode can end a frame. */ /* Streaming compression needs the source size before threaded mode can end a frame. */
zret = ZSTD_CCtx_setPledgedSrcSize(cctx, data_to_compress->size); size_t zret = ZSTD_CCtx_setPledgedSrcSize(ctx->cctx, data_to_compress->size);
if (ZSTD_isError(zret)) { if (ZSTD_isError(zret)) {
log_message(LOG_LEVEL_ERROR, "Failed to set compression source size: %s", log_message(LOG_LEVEL_ERROR, "Failed to set compression source size: %s",
ZSTD_getErrorName(zret)); ZSTD_getErrorName(zret));
ZSTD_freeCCtx(cctx); goto cleanup;
data_destroy(compressed_data);
return NULL;
} }
} }
if (ctx->out_cap < dst_size) {
void* grown = protocol_realloc(ctx->out_buf, dst_size);
if (grown == NULL) {
log_message(LOG_LEVEL_ERROR, "Failed to allocate compression buffer");
goto cleanup;
}
ctx->out_buf = grown;
ctx->out_cap = dst_size;
}
ZSTD_inBuffer input = {data_to_compress->data, data_to_compress->size, 0}; ZSTD_inBuffer input = {data_to_compress->data, data_to_compress->size, 0};
ZSTD_outBuffer output = {compressed_data->data, dst_size, 0}; ZSTD_outBuffer output = {ctx->out_buf, dst_size, 0};
size_t ret; size_t ret;
do { do {
ret = ZSTD_compressStream2(cctx, &output, &input, ZSTD_e_end); ret = ZSTD_compressStream2(ctx->cctx, &output, &input, ZSTD_e_end);
if (ZSTD_isError(ret)) { if (ZSTD_isError(ret)) {
log_message(LOG_LEVEL_ERROR, "Compression failed: %s", ZSTD_getErrorName(ret)); log_message(LOG_LEVEL_ERROR, "Compression failed: %s", ZSTD_getErrorName(ret));
ZSTD_freeCCtx(cctx); goto cleanup;
data_destroy(compressed_data);
return NULL;
} }
} while (ret > 0); } while (ret > 0);
/* Hand off an exactly-sized copy; the scratch buffer stays cached so the next
* call does not reallocate a ZSTD_compressBound-sized block. */
compressed_data = data_create_empty(output.pos);
if (compressed_data == NULL) {
log_message(LOG_LEVEL_ERROR, "Failed to allocate compressed data");
goto cleanup;
}
if (output.pos > 0)
memcpy(compressed_data->data, ctx->out_buf, output.pos);
compressed_data->size = output.pos; compressed_data->size = output.pos;
ZSTD_freeCCtx(cctx);
log_debug_message(LOG_DEBUG_UTIL, "Data succesfully compressed from %zu to %zu", log_debug_message(LOG_DEBUG_UTIL, "Data succesfully compressed from %zu to %zu",
data_to_compress->size, compressed_data->size); data_to_compress->size, compressed_data->size);
cleanup:
compression_ctx_put(ctx);
return compressed_data; return compressed_data;
} }
@@ -144,20 +265,30 @@ Data* data_decompress_limited(Data* compressed_data, size_t maximum_size) {
return NULL; return NULL;
} }
ZSTD_DCtx* dctx = ZSTD_createDCtx(); CompressionThreadCtx* ctx = compression_get_thread_ctx();
if (!dctx) { if (ctx == NULL) {
log_message(LOG_LEVEL_ERROR, "Failed to create ZSTD decompression context"); log_message(LOG_LEVEL_ERROR, "Failed to allocate ZSTD decompression context");
return NULL; return NULL;
} }
Data* uncompressed_data = NULL;
if (!ctx->dctx) {
ctx->dctx = ZSTD_createDCtx();
if (!ctx->dctx) {
log_message(LOG_LEVEL_ERROR, "Failed to create ZSTD decompression context");
goto cleanup;
}
}
/* Reset only the session; decompression parameters are sticky. */
ZSTD_DCtx_reset(ctx->dctx, ZSTD_reset_session_only);
size_t buf_size = (dst_size > 0) ? (size_t)dst_size : INITIAL_DECOMPRESS_BUF_SIZE; size_t buf_size = (dst_size > 0) ? (size_t)dst_size : INITIAL_DECOMPRESS_BUF_SIZE;
if (buf_size > maximum_size) if (buf_size > maximum_size)
buf_size = maximum_size; buf_size = maximum_size;
Data* uncompressed_data = data_create_empty(buf_size); uncompressed_data = data_create_empty(buf_size);
if (!uncompressed_data) { if (!uncompressed_data) {
log_message(LOG_LEVEL_ERROR, "Failed to allocate decompression buffer"); log_message(LOG_LEVEL_ERROR, "Failed to allocate decompression buffer");
ZSTD_freeDCtx(dctx); goto cleanup;
return NULL;
} }
ZSTD_inBuffer input = {compressed_data->data, compressed_data->size, 0}; ZSTD_inBuffer input = {compressed_data->data, compressed_data->size, 0};
@@ -165,20 +296,20 @@ Data* data_decompress_limited(Data* compressed_data, size_t maximum_size) {
size_t ret; size_t ret;
do { do {
ret = ZSTD_decompressStream(dctx, &output, &input); ret = ZSTD_decompressStream(ctx->dctx, &output, &input);
if (ZSTD_isError(ret)) { if (ZSTD_isError(ret)) {
log_message(LOG_LEVEL_ERROR, "Decompression failed: %s", ZSTD_getErrorName(ret)); log_message(LOG_LEVEL_ERROR, "Decompression failed: %s", ZSTD_getErrorName(ret));
ZSTD_freeDCtx(dctx);
data_destroy(uncompressed_data); data_destroy(uncompressed_data);
return NULL; uncompressed_data = NULL;
goto cleanup;
} }
if (ret > 0 && output.pos == output.size) { if (ret > 0 && output.pos == output.size) {
if (buf_size >= hard_limit || buf_size > SIZE_MAX / 2) { if (buf_size >= hard_limit || buf_size > SIZE_MAX / 2) {
log_message(LOG_LEVEL_ERROR, "Decompressed data exceeds %llu bytes", log_message(LOG_LEVEL_ERROR, "Decompressed data exceeds %llu bytes",
(unsigned long long)MAX_DECOMPRESSED_SIZE); (unsigned long long)MAX_DECOMPRESSED_SIZE);
ZSTD_freeDCtx(dctx);
data_destroy(uncompressed_data); data_destroy(uncompressed_data);
return NULL; uncompressed_data = NULL;
goto cleanup;
} }
buf_size *= 2; buf_size *= 2;
if (buf_size > hard_limit) if (buf_size > hard_limit)
@@ -186,9 +317,9 @@ Data* data_decompress_limited(Data* compressed_data, size_t maximum_size) {
void* new_data = protocol_realloc(uncompressed_data->data, buf_size); void* new_data = protocol_realloc(uncompressed_data->data, buf_size);
if (!new_data) { if (!new_data) {
log_message(LOG_LEVEL_ERROR, "Failed to grow decompression buffer"); log_message(LOG_LEVEL_ERROR, "Failed to grow decompression buffer");
ZSTD_freeDCtx(dctx);
data_destroy(uncompressed_data); data_destroy(uncompressed_data);
return NULL; uncompressed_data = NULL;
goto cleanup;
} }
uncompressed_data->data = new_data; uncompressed_data->data = new_data;
output.dst = new_data; output.dst = new_data;
@@ -197,9 +328,11 @@ Data* data_decompress_limited(Data* compressed_data, size_t maximum_size) {
} while (ret > 0); } while (ret > 0);
uncompressed_data->size = output.pos; uncompressed_data->size = output.pos;
ZSTD_freeDCtx(dctx);
log_debug_message(LOG_DEBUG_UTIL, "Decompressed data successfully"); log_debug_message(LOG_DEBUG_UTIL, "Decompressed data successfully");
cleanup:
compression_ctx_put(ctx);
return uncompressed_data; return uncompressed_data;
} }
+8 -1
View File
@@ -11,7 +11,14 @@ Data* data_compress_with_threads(Data* data_to_compress, int compression_level,
int compression_threads); int compression_threads);
Data* data_decompress(Data* compressed_data); Data* data_decompress(Data* compressed_data);
Data* data_decompress_limited(Data* compressed_data, size_t maximum_size); Data* data_decompress_limited(Data* compressed_data, size_t maximum_size);
bool compression_should_skip(const char* path);
bool compression_should_skip_with_suffixes(const char* path, char* const* suffixes, int count); bool compression_should_skip_with_suffixes(const char* path, char* const* suffixes, int count);
/* Release the calling thread's cached zstd contexts (compressor, decompressor
* and scratch buffer). The cache is thread-local and is also released
* automatically when a worker thread exits (via a C11 tss destructor) and for
* the main thread at process exit; this explicit entry point exists so tests
* and long-lived callers can drop the cache deterministically. Safe to call
* when no context has been created, and idempotent. */
void compression_free_thread_contexts(void);
#endif #endif
+158 -70
View File
@@ -23,6 +23,7 @@ static void config_set_defaults(Config* config) {
config->receive_root_directory = NULL; config->receive_root_directory = NULL;
config->save_to_disk = false; config->save_to_disk = false;
config->use_multithreading = false; config->use_multithreading = false;
config->scanner_threads = 0;
config->use_chunk_serialization = false; config->use_chunk_serialization = false;
config->use_compression = false; config->use_compression = false;
config->use_metadata = false; config->use_metadata = false;
@@ -67,7 +68,11 @@ static void config_set_defaults(Config* config) {
config->tls_ca = NULL; config->tls_ca = NULL;
config->server_host = str_dup("127.0.0.1"); config->server_host = str_dup("127.0.0.1");
config->server_port = 8080; config->server_port = 8080;
config->timeout = 30; /* 0 means "--timeout not given": the transport keeps its own built-in 30 s
* socket timeout (tcp_set_timeouts ignores non-positive values) and the
* protocol layer keeps its built-in 60 s per-message deadline. A positive
* value overrides BOTH (see protocol_session_set_io_timeout). */
config->timeout = 0;
config->contimeout = 10; config->contimeout = 10;
config->quiet = false; config->quiet = false;
config->backup = false; config->backup = false;
@@ -75,7 +80,6 @@ static void config_set_defaults(Config* config) {
config->stats = false; config->stats = false;
config->max_depth = 0; config->max_depth = 0;
config->log_file = NULL; config->log_file = NULL;
config->queue_size = 100;
config->follow_symlinks = false; config->follow_symlinks = false;
config->partial = false; config->partial = false;
config->copy_links = false; config->copy_links = false;
@@ -143,14 +147,11 @@ static void config_set_defaults(Config* config) {
config->delete_during = false; config->delete_during = false;
config->delete_delay = false; config->delete_delay = false;
config->address = NULL; config->address = NULL;
config->bind_address = NULL;
config->ipv6 = false; config->ipv6 = false;
config->ipv4 = false; config->ipv4 = false;
config->sockopts = NULL; config->sockopts = NULL;
config->sockopt_count = 0; config->sockopt_count = 0;
config->daemon = false; config->daemon = false;
config->daemon_config = NULL;
config->server_mode = false;
config->no_motd = false; config->no_motd = false;
config->checksum = false; config->checksum = false;
config->checksum_algo = CHECKSUM_ALGO_XXH64; config->checksum_algo = CHECKSUM_ALGO_XXH64;
@@ -202,7 +203,57 @@ static bool receive_wire_bool(int fd, bool* value) {
return true; return true;
} }
/* Cumulative budget for the strings retained by one received Config (see
* MAX_CONFIG_STRING_BYTES). Config strings are received once per connection
* before authentication and live for its whole lifetime, so the charge is never
* released. */
typedef struct {
unsigned long long used;
} ConfigStringBudget;
/* Charge `bytes` (the retained allocation: string body plus NUL) against the
* aggregate config-string budget. Returns false when the ceiling would be
* exceeded, letting the caller reject the frame with a clear error instead of
* retaining unbounded pre-auth memory. */
static bool config_string_budget_charge(ConfigStringBudget* budget, size_t bytes) {
if ((unsigned long long)bytes > MAX_CONFIG_STRING_BYTES ||
budget->used > MAX_CONFIG_STRING_BYTES - (unsigned long long)bytes) {
log_message(LOG_LEVEL_ERROR, "Config string budget exceeded (%llu + %zu > %llu bytes)",
budget->used, bytes, (unsigned long long)MAX_CONFIG_STRING_BYTES);
return false;
}
budget->used += (unsigned long long)bytes;
return true;
}
static char* config_receive_str(int fd, ConfigStringBudget* budget) {
char* value = receive_str(fd);
if (!value)
return NULL;
if (!config_string_budget_charge(budget, strlen(value) + 1)) {
free(value);
return NULL;
}
return value;
}
static char* config_receive_str_redacted(int fd, ConfigStringBudget* budget) {
char* value = receive_str_redacted(fd);
if (!value)
return NULL;
if (!config_string_budget_charge(budget, strlen(value) + 1)) {
free(value);
return NULL;
}
return value;
}
static bool validate_received_config(const Config* config) { static bool validate_received_config(const Config* config) {
/* Cross-field invariants live in one place (config_invariants_error) so the
receiver enforces every combination the client relies on; a hostile peer
can forge a frame that violates any clause of the shared predicate. */
if (config_invariants_error(config) != NULL)
return false;
return valid_wire_bool(config->save_to_disk) && valid_wire_bool(config->use_multithreading) && return valid_wire_bool(config->save_to_disk) && valid_wire_bool(config->use_multithreading) &&
valid_wire_bool(config->use_chunk_serialization) && valid_wire_bool(config->use_chunk_serialization) &&
valid_wire_bool(config->use_compression) && valid_wire_bool(config->use_metadata) && valid_wire_bool(config->use_compression) && valid_wire_bool(config->use_metadata) &&
@@ -226,30 +277,14 @@ static bool validate_received_config(const Config* config) {
valid_wire_bool(config->delete_delay) && valid_wire_bool(config->delete_during) && valid_wire_bool(config->delete_delay) && valid_wire_bool(config->delete_during) &&
valid_wire_bool(config->relative) && valid_wire_bool(config->prune_empty_dirs) && valid_wire_bool(config->relative) && valid_wire_bool(config->prune_empty_dirs) &&
valid_wire_bool(config->delay_updates) && valid_wire_bool(config->mkpath) && valid_wire_bool(config->delay_updates) && valid_wire_bool(config->mkpath) &&
!(config->delay_updates && config->inplace) &&
!(config->delay_updates && delay_updates_staging_name_conflict(config->backup_dir)) &&
valid_wire_bool(config->partial) && valid_wire_bool(config->delete_before) && valid_wire_bool(config->partial) && valid_wire_bool(config->delete_before) &&
valid_wire_bool(config->checksum) && valid_wire_bool(config->eight_bit_output) && valid_wire_bool(config->checksum) && valid_wire_bool(config->eight_bit_output) &&
checksum_algo_valid(config->checksum_algo) && config_has_valid_delete_timing(config) && checksum_algo_valid(config->checksum_algo) && identity_wire_valid(config) &&
identity_wire_valid(config) &&
!(config->skip_compress_set && config->use_chunk_serialization) &&
/* --append / --append-verify tail resume needs the per-file check,
which chunk serialization -s disables: reject on the receiver too
so a -s sender cannot negotiate an inert append mode. */
!((config->append || config->append_verify) && config->use_chunk_serialization) &&
!(config->preserve_hard_links && config->use_chunk_serialization) &&
!(config->preserve_hard_links && (config->append || config->append_verify)) &&
/* The xattr block rides the per-file streaming frame, which -s drops. */
!((config->preserve_xattrs || config->preserve_acls) && config->use_chunk_serialization) &&
valid_wire_bool(config->preserve_atimes) && valid_wire_bool(config->preserve_crtimes) && valid_wire_bool(config->preserve_atimes) && valid_wire_bool(config->preserve_crtimes) &&
valid_wire_bool(config->omit_dir_times) && valid_wire_bool(config->omit_link_times) && valid_wire_bool(config->omit_dir_times) && valid_wire_bool(config->omit_link_times) &&
valid_wire_bool(config->munge_links) && valid_wire_bool(config->keep_dirlinks) && valid_wire_bool(config->munge_links) && valid_wire_bool(config->keep_dirlinks) &&
valid_wire_bool(config->fake_super) && valid_wire_bool(config->fake_super) &&
(!config->copy_as_set || (config->copy_as_uid >= 0 && config->copy_as_gid >= 0)) && (!config->copy_as_set || (config->copy_as_uid >= 0 && config->copy_as_gid >= 0)) &&
/* --copy-as forces ownership through the metadata path; without
metadata it would pass the privilege gate but silently chown
nothing. Refuse the frame instead. */
(!config->copy_as_set || config->use_metadata) &&
(!config->use_compression || (!config->use_compression ||
(config->compression_level >= 1 && config->compression_level <= 22)) && (config->compression_level >= 1 && config->compression_level <= 22)) &&
config->chunk_size > 0 && config->chunk_size <= MAX_CHUNK_SIZE && config->chunk_size > 0 && config->chunk_size <= MAX_CHUNK_SIZE &&
@@ -257,17 +292,9 @@ static bool validate_received_config(const Config* config) {
config->delta_block_size <= DELTA_BLOCK_SIZE_MAX && config->delta_block_size <= DELTA_BLOCK_SIZE_MAX &&
config->delta_max_file_size <= DELTA_MAX_FILE_SIZE && config->modify_window >= 0 && config->delta_max_file_size <= DELTA_MAX_FILE_SIZE && config->modify_window >= 0 &&
config->max_delete >= -1 && config->skip_compress_count >= 0 && config->max_delete >= -1 && config->skip_compress_count >= 0 &&
config->skip_compress_count <= 10000 && config->max_alloc > 0 && config->skip_compress_count <= MAX_SKIP_COMPRESS_SUFFIXES && config->max_alloc > 0 &&
(!config->chmod_spec || !*config->chmod_spec || (!config->chmod_spec || !*config->chmod_spec ||
chmod_apply(0, config->chmod_spec, &(mode_t){0})) && chmod_apply(0, config->chmod_spec, &(mode_t){0})) &&
/* The received --iconv CONVERT_SPEC is untrusted input that drives
the receiver's path decoding: reject a malformed spec or an
unsupported charset name so the run is refused up front instead of
every received file name failing mid-transfer. A NULL spec (iconv
disabled) is always accepted. */
(!config->iconv_spec || charset_spec_valid(config->iconv_spec)) &&
/* --super / --no-super: the received tri-state must be one of the
defined values (AUTO/ON/OFF); anything else is a malformed frame. */
config->super_mode >= SUPER_MODE_AUTO && config->super_mode <= SUPER_MODE_OFF; config->super_mode >= SUPER_MODE_AUTO && config->super_mode <= SUPER_MODE_OFF;
} }
@@ -299,6 +326,65 @@ bool config_has_valid_delete_timing(const Config* config) {
return timing_count <= 1; return timing_count <= 1;
} }
/* The cross-field invariants FastSync relies on, in one place. Every message
* here was previously duplicated (verbatim) in client_validation.c and/or
* config.c; the client reports the returned string for UX and the server
* enforces the same rules at its trust boundary. Pure: no I/O, no logging.
* The order is deliberate (most specific structural conflicts first). */
const char* config_invariants_error(const Config* config) {
if (!config)
return "Invalid configuration";
if (config_has_basis(config) && config->use_chunk_serialization)
return "--compare-dest/--copy-dest/--link-dest require per-file incremental checks and cannot "
"be combined with -s (chunk serialization)";
if (config->use_sendfile && (config->use_chunk_serialization || config->use_compression))
return "-f/--sendfile cannot be combined with -c (compression) or -s (chunk serialization)";
if (config->use_incremental && config->use_chunk_serialization)
return "--incremental is not supported with -s (chunk serialization)";
if (config->skip_compress_set && config->use_chunk_serialization)
return "--skip-compress cannot be combined with -s (chunk serialization)";
if (config->use_delta && !config->whole_file && !config->use_incremental)
return "--delta requires --incremental";
if (config->use_delta && !config->whole_file && config->use_chunk_serialization)
return "--delta cannot be combined with -s (chunk serialization)";
if (config->use_delta && !config->whole_file && config->use_sendfile)
return "--delta cannot be combined with -f (sendfile)";
/* --append / --append-verify resume a shorter existing destination by
transmitting only the tail. The resume needs the per-file STATUS_CHECK
handshake (so the dest length is learned), which chunk serialization -s
disables; whole-file is the opposite intent (send everything). */
if ((config->append || config->append_verify) && config->use_chunk_serialization)
return "--append/--append-verify require the per-file incremental check and cannot be "
"combined with -s (chunk serialization)";
if ((config->append || config->append_verify) && config->whole_file)
return "--append/--append-verify are incompatible with --whole-file (which forces a full "
"transfer)";
/* -H transmits each later hard-link group member as a dedicated per-file
STATUS_HARDLINK frame, which -s does not support; and a hard-links sibling
carries no payload, so the tail-resume of --append is meaningless. */
if (config->preserve_hard_links && config->use_chunk_serialization)
return "--hard-links/-H cannot be combined with -s (chunk serialization)";
/* -X/-A ride the per-file metadata frame; the chunk-serialization wire format
does not carry the xattr block. */
if ((config->preserve_xattrs || config->preserve_acls) && config->use_chunk_serialization)
return "--xattrs/-X and --acls/-A cannot be combined with -s (chunk serialization)";
if (config->preserve_hard_links && (config->append || config->append_verify))
return "--hard-links/-H cannot be combined with --append/--append-verify";
if (config->delay_updates && config->inplace)
return "--delay-updates does not work with --inplace";
if (config->delay_updates && delay_updates_staging_name_conflict(config->backup_dir))
return "--backup-dir is reserved when --delay-updates is active (used for the internal "
"staging directory)";
if (!config_has_valid_delete_timing(config))
return "--delete-before/--delete-during/--delete-delay/--delete-after select the delete "
"timing; at most one may be given and each implies --delete";
if (config->iconv_spec && !charset_spec_valid(config->iconv_spec))
return "--iconv requires LOCAL[,REMOTE] charset names supported by iconv";
if (config->copy_as_set && !config->use_metadata)
return "--copy-as requires metadata preservation and cannot be combined with --no-preserve";
return NULL;
}
bool config_has_basis(const Config* config) { bool config_has_basis(const Config* config) {
return config && config->basis_count > 0; return config && config->basis_count > 0;
} }
@@ -648,6 +734,9 @@ void config_delete(Config* config) {
if (config == NULL) if (config == NULL)
return; return;
if (config->log_file) { if (config->log_file) {
/* The logging subsystem borrows this FILE*; detach it before closing so a
* concurrent log call can never touch the freed handle. */
log_set_file(NULL);
fclose(config->log_file); fclose(config->log_file);
config->log_file = NULL; config->log_file = NULL;
} }
@@ -697,9 +786,7 @@ void config_delete(Config* config) {
free(config->partial_dir); free(config->partial_dir);
free(config->suffix); free(config->suffix);
free(config->address); free(config->address);
free(config->bind_address);
free(config->sockopts); free(config->sockopts);
free(config->daemon_config);
free(config->compress_choice); free(config->compress_choice);
free(config->chmod_spec); free(config->chmod_spec);
if (config->skip_compress_suffixes) { if (config->skip_compress_suffixes) {
@@ -819,7 +906,7 @@ static bool send_checksum_options(int fd, const Config* c) {
send_n_data(fd, &c->checksum_seed, sizeof(c->checksum_seed)); send_n_data(fd, &c->checksum_seed, sizeof(c->checksum_seed));
} }
static bool receive_core_fields(int fd, Config* c) { static bool receive_core_fields(int fd, Config* c, ConfigStringBudget* budget) {
int value; int value;
if (!receive_wire_bool(fd, &c->eight_bit_output)) if (!receive_wire_bool(fd, &c->eight_bit_output))
return false; return false;
@@ -829,8 +916,8 @@ static bool receive_core_fields(int fd, Config* c) {
if (c->max_alloc > MAX_SERVER_ALLOC) if (c->max_alloc > MAX_SERVER_ALLOC)
c->max_alloc = MAX_SERVER_ALLOC; c->max_alloc = MAX_SERVER_ALLOC;
protocol_session_set_max_alloc(NULL, c->max_alloc); protocol_session_set_max_alloc(NULL, c->max_alloc);
c->send_directory = receive_str(fd); c->send_directory = config_receive_str(fd, budget);
c->receive_root_directory = receive_str(fd); c->receive_root_directory = config_receive_str(fd, budget);
if (!c->send_directory || !c->receive_root_directory) if (!c->send_directory || !c->receive_root_directory)
return false; return false;
if (!receive_wire_bool(fd, &c->save_to_disk) || !receive_wire_bool(fd, &c->use_multithreading) || if (!receive_wire_bool(fd, &c->save_to_disk) || !receive_wire_bool(fd, &c->use_multithreading) ||
@@ -863,10 +950,10 @@ static bool receive_delta_fields(int fd, Config* c) {
receive_n_data(fd, &c->delta_max_file_size, sizeof(unsigned long long)); receive_n_data(fd, &c->delta_max_file_size, sizeof(unsigned long long));
} }
static bool receive_file_options(int fd, Config* c) { static bool receive_file_options(int fd, Config* c, ConfigStringBudget* budget) {
if (!receive_wire_bool(fd, &c->backup)) if (!receive_wire_bool(fd, &c->backup))
return false; return false;
char* backup_dir = receive_str(fd); char* backup_dir = config_receive_str(fd, budget);
if (!backup_dir) if (!backup_dir)
return false; return false;
if (*backup_dir != '\0') { if (*backup_dir != '\0') {
@@ -920,8 +1007,8 @@ static bool receive_selection_options(int fd, Config* c) {
return receive_wire_bool(fd, &c->delete_delay); return receive_wire_bool(fd, &c->delete_delay);
} }
static bool receive_resume_options(int fd, Config* c) { static bool receive_resume_options(int fd, Config* c, ConfigStringBudget* budget) {
char* temp_dir = receive_str(fd); char* temp_dir = config_receive_str(fd, budget);
if (!temp_dir) if (!temp_dir)
return false; return false;
if (*temp_dir != '\0') { if (*temp_dir != '\0') {
@@ -935,7 +1022,7 @@ static bool receive_resume_options(int fd, Config* c) {
string for "unset". Canonicalize the empty wire value back to NULL so string for "unset". Canonicalize the empty wire value back to NULL so
receivers observe exactly what the client configured (plain --backup, for receivers observe exactly what the client configured (plain --backup, for
example, must not look like --backup-dir ""). */ example, must not look like --backup-dir ""). */
char* partial_dir = receive_str(fd); char* partial_dir = config_receive_str(fd, budget);
if (!partial_dir) if (!partial_dir)
return false; return false;
if (*partial_dir != '\0') { if (*partial_dir != '\0') {
@@ -943,7 +1030,7 @@ static bool receive_resume_options(int fd, Config* c) {
} else { } else {
free(partial_dir); free(partial_dir);
} }
char* suffix = receive_str(fd); char* suffix = config_receive_str(fd, budget);
if (!suffix) if (!suffix)
return false; return false;
if (*suffix != '\0') { if (*suffix != '\0') {
@@ -957,20 +1044,20 @@ static bool receive_resume_options(int fd, Config* c) {
return false; return false;
if (!receive_n_data(fd, &c->modify_window, sizeof(c->modify_window))) if (!receive_n_data(fd, &c->modify_window, sizeof(c->modify_window)))
return false; return false;
c->compress_choice = receive_str(fd); c->compress_choice = config_receive_str(fd, budget);
if (!c->compress_choice) if (!c->compress_choice)
return false; return false;
c->chmod_spec = receive_str(fd); c->chmod_spec = config_receive_str(fd, budget);
if (!c->chmod_spec || !receive_wire_bool(fd, &c->skip_compress_set) || if (!c->chmod_spec || !receive_wire_bool(fd, &c->skip_compress_set) ||
!receive_int(fd, &c->skip_compress_count) || c->skip_compress_count < 0 || !receive_int(fd, &c->skip_compress_count) || c->skip_compress_count < 0 ||
c->skip_compress_count > 10000) c->skip_compress_count > MAX_SKIP_COMPRESS_SUFFIXES)
return false; return false;
if (c->skip_compress_count > 0) { if (c->skip_compress_count > 0) {
c->skip_compress_suffixes = calloc((size_t)c->skip_compress_count, sizeof(char*)); c->skip_compress_suffixes = calloc((size_t)c->skip_compress_count, sizeof(char*));
if (!c->skip_compress_suffixes) if (!c->skip_compress_suffixes)
return false; return false;
for (int i = 0; i < c->skip_compress_count; i++) { for (int i = 0; i < c->skip_compress_count; i++) {
c->skip_compress_suffixes[i] = receive_str(fd); c->skip_compress_suffixes[i] = config_receive_str(fd, budget);
if (!c->skip_compress_suffixes[i]) if (!c->skip_compress_suffixes[i])
return false; return false;
} }
@@ -978,7 +1065,7 @@ static bool receive_resume_options(int fd, Config* c) {
return true; return true;
} }
static bool receive_basis_options(int fd, Config* c) { static bool receive_basis_options(int fd, Config* c, ConfigStringBudget* budget) {
int count; int count;
if (!receive_int(fd, &count)) if (!receive_int(fd, &count))
return false; return false;
@@ -988,7 +1075,7 @@ static bool receive_basis_options(int fd, Config* c) {
int type; int type;
if (!receive_int(fd, &type) || type <= BASIS_DEST_NONE || type > BASIS_DEST_LINK) if (!receive_int(fd, &type) || type <= BASIS_DEST_NONE || type > BASIS_DEST_LINK)
return false; return false;
char* path = receive_str(fd); char* path = config_receive_str(fd, budget);
if (!path) if (!path)
return false; return false;
/* config_basis_append validates and canonicalizes the path; a rejected /* config_basis_append validates and canonicalizes the path; a rejected
@@ -1119,8 +1206,8 @@ static bool send_daemon_module(int fd, const Config* c) {
return send_str(fd, c->module ? c->module : ""); return send_str(fd, c->module ? c->module : "");
} }
static bool receive_daemon_module(int fd, Config* c) { static bool receive_daemon_module(int fd, Config* c, ConfigStringBudget* budget) {
char* module = receive_str(fd); char* module = config_receive_str(fd, budget);
if (!module) if (!module)
return false; return false;
/* Guard against a hostile client flooding the log with an over-long module /* Guard against a hostile client flooding the log with an over-long module
@@ -1155,14 +1242,14 @@ static bool send_daemon_auth(int fd, const Config* c) {
return send_str_redacted(fd, c->auth_user); return send_str_redacted(fd, c->auth_user);
} }
static bool receive_daemon_auth(int fd, Config* c) { static bool receive_daemon_auth(int fd, Config* c, ConfigStringBudget* budget) {
int present; int present;
if (!receive_int(fd, &present) || !valid_wire_bool(present)) if (!receive_int(fd, &present) || !valid_wire_bool(present))
return false; return false;
if (!present) if (!present)
return true; return true;
/* Redacted receive: never log the incoming username body. */ /* Redacted receive: never log the incoming username body. */
char* user = receive_str_redacted(fd); char* user = config_receive_str_redacted(fd, budget);
if (!user) if (!user)
return false; return false;
if (!credentials_username_valid(user)) { if (!credentials_username_valid(user)) {
@@ -1272,8 +1359,8 @@ static bool send_iconv_spec(int fd, const Config* c) {
return send_str(fd, c->iconv_spec ? c->iconv_spec : ""); return send_str(fd, c->iconv_spec ? c->iconv_spec : "");
} }
static bool receive_iconv_spec(int fd, Config* c) { static bool receive_iconv_spec(int fd, Config* c, ConfigStringBudget* budget) {
char* spec = receive_str(fd); char* spec = config_receive_str(fd, budget);
if (!spec) if (!spec)
return false; return false;
if (*spec == '\0') { if (*spec == '\0') {
@@ -1293,14 +1380,14 @@ static bool receive_iconv_spec(int fd, Config* c) {
* validated to the SUPER_MODE_AUTO..SUPER_MODE_OFF range (also re-checked by * validated to the SUPER_MODE_AUTO..SUPER_MODE_OFF range (also re-checked by
* validate_received_config). */ * validate_received_config). */
static bool send_privilege_options(int fd, const Config* c) { static bool send_privilege_options(int fd, const Config* c) {
return send_int(fd, c->super_mode); return send_int(fd, (int)c->super_mode);
} }
static bool receive_privilege_options(int fd, Config* c) { static bool receive_privilege_options(int fd, Config* c) {
int mode; int mode;
if (!receive_int(fd, &mode) || mode < SUPER_MODE_AUTO || mode > SUPER_MODE_OFF) if (!receive_int(fd, &mode) || mode < SUPER_MODE_AUTO || mode > SUPER_MODE_OFF)
return false; return false;
c->super_mode = mode; c->super_mode = (SuperMode)mode;
return true; return true;
} }
@@ -1376,47 +1463,48 @@ Config* config_receive_with_validate(int file_descriptor, ConfigValidateFunc val
Config* config = config_create(); Config* config = config_create();
if (!config) if (!config)
return NULL; return NULL;
ConfigStringBudget budget = {0};
free(config->version); free(config->version);
config->version = receive_str(file_descriptor); config->version = config_receive_str(file_descriptor, &budget);
if (!config->version) if (!config->version)
goto error; goto error;
if (strcmp(config->version, PROTOCOL_VERSION) != 0) { if (strcmp(config->version, PROTOCOL_VERSION) != 0) {
char* escaped_version = output_escape(config->version, false); char* escaped_version = output_escape(config->version, false);
fprintf(stderr, "Protocol version mismatch: client=%s, server=%s\n", log_message(LOG_LEVEL_ERROR, "Protocol version mismatch: client=%s, server=%s",
escaped_version ? escaped_version : "<allocation failed>", PROTOCOL_VERSION); escaped_version ? escaped_version : "<allocation failed>", PROTOCOL_VERSION);
free(escaped_version); free(escaped_version);
send_status(file_descriptor, STATUS_ERROR); send_status(file_descriptor, STATUS_ERROR);
goto error; goto error;
} }
if (!receive_core_fields(file_descriptor, config) || if (!receive_core_fields(file_descriptor, config, &budget) ||
!receive_delta_fields(file_descriptor, config) || !receive_delta_fields(file_descriptor, config) ||
!receive_file_options(file_descriptor, config) || !receive_file_options(file_descriptor, config, &budget) ||
!receive_selection_options(file_descriptor, config) || !receive_selection_options(file_descriptor, config) ||
!receive_resume_options(file_descriptor, config) || !receive_resume_options(file_descriptor, config, &budget) ||
!receive_basis_options(file_descriptor, config) || !receive_basis_options(file_descriptor, config, &budget) ||
!receive_fuzzy_option(file_descriptor, config) || !receive_fuzzy_option(file_descriptor, config) ||
!receive_checksum_options(file_descriptor, config) || !receive_checksum_options(file_descriptor, config) ||
!receive_identity_options(file_descriptor, config) || !receive_identity_options(file_descriptor, config) ||
!receive_metadata_times_options(file_descriptor, config) || !receive_metadata_times_options(file_descriptor, config) ||
!receive_symlink_trust_options(file_descriptor, config) || !receive_symlink_trust_options(file_descriptor, config) ||
!receive_phase4_xattr_options(file_descriptor, config) || !receive_phase4_xattr_options(file_descriptor, config) ||
!receive_daemon_module(file_descriptor, config) || !receive_daemon_module(file_descriptor, config, &budget) ||
!receive_daemon_auth(file_descriptor, config) || !receive_daemon_auth(file_descriptor, config, &budget) ||
!receive_iconv_spec(file_descriptor, config) || !receive_iconv_spec(file_descriptor, config, &budget) ||
!receive_privilege_options(file_descriptor, config) || !receive_privilege_options(file_descriptor, config) ||
!receive_copy_as_options(file_descriptor, config)) !receive_copy_as_options(file_descriptor, config))
goto error; goto error;
if (config->compress_choice[0] != '\0' && strcmp(config->compress_choice, "zstd") != 0 && if (config->compress_choice[0] != '\0' && strcmp(config->compress_choice, "zstd") != 0 &&
strcmp(config->compress_choice, "none") != 0) { strcmp(config->compress_choice, "none") != 0) {
char* escaped_choice = output_escape(config->compress_choice, config->eight_bit_output); char* escaped_choice = output_escape(config->compress_choice, config->eight_bit_output);
fprintf(stderr, "Unsupported compression choice: %s\n", log_message(LOG_LEVEL_ERROR, "Unsupported compression choice: %s",
escaped_choice ? escaped_choice : "<allocation failed>"); escaped_choice ? escaped_choice : "<allocation failed>");
free(escaped_choice); free(escaped_choice);
send_status(file_descriptor, STATUS_ERROR); send_status(file_descriptor, STATUS_ERROR);
goto error; goto error;
} }
if (!validate_received_config(config)) { if (!validate_received_config(config)) {
fprintf(stderr, "Invalid configuration received from client\n"); log_message(LOG_LEVEL_ERROR, "Invalid configuration received from client");
send_status(file_descriptor, STATUS_ERROR); send_status(file_descriptor, STATUS_ERROR);
goto error; goto error;
} }
@@ -1430,7 +1518,7 @@ Config* config_receive_with_validate(int file_descriptor, ConfigValidateFunc val
* the CONFIG_VALIDATE_ALREADY_TERMINATED sentinel, so no second status is * the CONFIG_VALIDATE_ALREADY_TERMINATED sentinel, so no second status is
* written. */ * written. */
if (rejection != CONFIG_VALIDATE_ALREADY_TERMINATED) { if (rejection != CONFIG_VALIDATE_ALREADY_TERMINATED) {
fprintf(stderr, "%s\n", rejection); log_message(LOG_LEVEL_ERROR, "%s", rejection);
send_status(file_descriptor, STATUS_ERROR); send_status(file_descriptor, STATUS_ERROR);
} }
goto error; goto error;
+65 -18
View File
@@ -68,12 +68,24 @@ typedef struct {
int value; /* 0/1 for booleans, byte count for SO_RCVBUF/SO_SNDBUF */ int value; /* 0/1 for booleans, byte count for SO_RCVBUF/SO_SNDBUF */
} SockOptEntry; } SockOptEntry;
/* --super / --no-super tri-state (Config->super_mode). AUTO (default) and ON
* both permit a confined super-user attempt (AUTO preserves FastSync's
* historical best-effort behavior; an unprivileged attempt is refused by the
* kernel and skipped per entry); OFF forbids the attempt even for root. See
* privilege_super_mode_permitted() in identity.h. */
typedef enum SuperMode { SUPER_MODE_AUTO = 0, SUPER_MODE_ON = 1, SUPER_MODE_OFF = 2 } SuperMode;
typedef struct Config { typedef struct Config {
char* version; char* version;
char* send_directory; char* send_directory;
char* receive_root_directory; char* receive_root_directory;
bool save_to_disk; bool save_to_disk;
bool use_multithreading; bool use_multithreading;
/* -j/--threads=N: number of parallel scanner worker threads for the -m
* pipeline. 0 (the default, also set by bare -j/--threads) means "use the
* scanner's built-in default" (4). CLIENT-ONLY: it is a local scheduling
* concern and is NEVER serialized into the wire config frame. */
int scanner_threads;
bool use_chunk_serialization; bool use_chunk_serialization;
bool use_compression; bool use_compression;
bool use_sendfile; bool use_sendfile;
@@ -150,7 +162,12 @@ typedef struct Config {
char* tls_cert; char* tls_cert;
char* tls_key; char* tls_key;
char* tls_ca; char* tls_ca;
/* --timeout: per-message I/O deadline in seconds. 0 (the default/unset
* sentinel) leaves the transport's built-in 30 s socket timeout and the
* protocol's built-in 60 s per-message deadline in place; a positive value
* overrides both. See protocol_session_set_io_timeout. */
int timeout; int timeout;
/* --contimeout: connect()/accept timeout, transport layer only. */
int contimeout; int contimeout;
bool quiet; bool quiet;
bool backup; bool backup;
@@ -158,7 +175,6 @@ typedef struct Config {
bool stats; bool stats;
int max_depth; int max_depth;
FILE* log_file; FILE* log_file;
int queue_size;
bool follow_symlinks; bool follow_symlinks;
bool partial; bool partial;
@@ -336,21 +352,17 @@ typedef struct Config {
// PR #181: IPv6 and bind address // PR #181: IPv6 and bind address
char* address; char* address;
char* bind_address;
bool ipv6; bool ipv6;
bool ipv4; bool ipv4;
/* --sockopts=OPTIONS (Phase 5, Wave B): strict allowlist of TCP/socket /* --sockopts=OPTIONS (Phase 5, Wave B): strict allowlist of TCP/socket
* options applied via setsockopt after socket() and before connect()/bind(). * options applied via setsockopt after socket() and before connect()/bind().
* These are LOCAL socket concerns: they never cross the wire config frame. * These are LOCAL socket concerns: they never cross the wire config frame.
* .address is the outgoing/source bind address (--address); .bind_address is * .address is the outgoing/source bind address (--address). */
* reserved for daemon-side binding and is not wired yet. */
SockOptEntry* sockopts; SockOptEntry* sockopts;
int sockopt_count; int sockopt_count;
// PR #182: Daemon/server mode // PR #182: Daemon/server mode
bool daemon; bool daemon;
char* daemon_config;
bool server_mode;
/* --no-motd (Wave C): CLIENT-ONLY, never crosses the wire. Suppresses /* --no-motd (Wave C): CLIENT-ONLY, never crosses the wire. Suppresses
* DISPLAY of the daemon's MOTD; the daemon still sends the MOTD frame, so * DISPLAY of the daemon's MOTD; the daemon still sends the MOTD frame, so
* the client reads and discards it to keep the stream in sync. rsync's * the client reads and discards it to keep the stream in sync. rsync's
@@ -416,7 +428,7 @@ typedef struct Config {
* as a trailing int so the receiver can enforce the policy. See * as a trailing int so the receiver can enforce the policy. See
* privilege_super_permitted() and identity_ownership_requested() in * privilege_super_permitted() and identity_ownership_requested() in
* identity.h. */ * identity.h. */
int super_mode; SuperMode super_mode;
// Receiver-side runtime staging registry for --delay-updates. Never sent // Receiver-side runtime staging registry for --delay-updates. Never sent
// over the wire and never set on the sender side. // over the wire and never set on the sender side.
@@ -630,12 +642,45 @@ typedef struct Config {
* anything else) is what keeps a 2.19 client and a 2.18 server from ever * anything else) is what keeps a 2.19 client and a 2.18 server from ever
* reaching that state. SECURITY: a 2.19 store holds a salted PBKDF2 verifier * reaching that state. SECURITY: a 2.19 store holds a salted PBKDF2 verifier
* and cannot verify (and refuses to load) a legacy unsalted-SHA-256 store line, * and cannot verify (and refuses to load) a legacy unsalted-SHA-256 store line,
* so an old bearer digest can never be replayed against a 2.19 daemon. */ * so an old bearer digest can never be replayed against a 2.19 daemon.
#define PROTOCOL_VERSION "2.19.0" *
* Packed Metadata Wave: 2.19.0 -> 2.20.0.
*
* WHY the bump: metadata_send()/metadata_receive() no longer emit/consume the
* metadata as up to 12 separate per-field writes. A file's metadata now
* crosses the wire as ONE packed frame: a single int32 present flag (0 =
* absent, 1 = present) followed, when present, by the fixed
* FILE_METADATA_WIRE_SIZE-byte (68-byte) field record produced by
* metadata_to_buf(). Protocol data is an unframed byte stream, so the packed
* encoding is byte-for-byte identical to the old field-by-field writes (same
* fields, same order, same widths); the change only removes per-field syscalls.
* The bump is therefore a deliberate lockstep-release marker, not a
* desynchronization fix — the strict same-version handshake still rejects a
* mixed 2.19/2.20 deployment. The chunk codec, which already used the packed
* metadata_to_buf()/metadata_from_buf() form, is unchanged. */
#define PROTOCOL_VERSION "2.20.0"
#define DEFAULT_CHUNK_SIZE (10 * 1024 * 1024) #define DEFAULT_CHUNK_SIZE (10 * 1024 * 1024)
/* Upper bound on total basis-dir entries (rsync caps --link-dest at 20). */ /* Upper bound on total basis-dir entries (rsync caps --link-dest at 20). */
#define MAX_BASIS_DIRS 64 #define MAX_BASIS_DIRS 64
/* Upper bound on the number of --skip-compress suffixes accepted from the wire.
* Each suffix is an independent wire string (up to MAX_STRING_SIZE = 64 KiB), so
* without this a hostile pre-auth client could otherwise retain
* skip_count * MAX_STRING_SIZE bytes on the server before authentication; 256
* covers any realistic suffix list while keeping the worst case small. */
#define MAX_SKIP_COMPRESS_SUFFIXES 256
/* Aggregate ceiling on the bytes retained by ALL strings in one received config
* frame (version, send/receive roots, backup/temp/partial/suffix, compression
* choice, chmod spec, skip-compress suffixes, basis paths, module, auth user,
* iconv spec, ...). The config frame is parsed BEFORE authentication and every
* one of these strings lives for the whole connection, so this cumulative
* (never released) budget bounds the pre-auth memory a single connection can
* pin. MAX_SKIP_COMPRESS_SUFFIXES / MAX_BASIS_DIRS bound the individual
* repeatable counts; this budget bounds their product and any single oversized
* field. */
#define MAX_CONFIG_STRING_BYTES (1ULL * 1024 * 1024)
/* Identity-mapping sentinels and bounds (see identity.h for semantics). /* Identity-mapping sentinels and bounds (see identity.h for semantics).
* IDENTITY_MATCH_ANY is a usermap/groupmap FROM '*' (matches any id); * IDENTITY_MATCH_ANY is a usermap/groupmap FROM '*' (matches any id);
* IDENTITY_CURRENT is a chown / map TO '*' (resolve to the receiver's current * IDENTITY_CURRENT is a chown / map TO '*' (resolve to the receiver's current
@@ -644,15 +689,6 @@ typedef struct Config {
#define IDENTITY_CURRENT (-1) #define IDENTITY_CURRENT (-1)
#define MAX_IDENTITY_MAP 128 #define MAX_IDENTITY_MAP 128
/* --super / --no-super tri-state (Config->super_mode). AUTO (default) and ON
* both permit a confined super-user attempt (AUTO preserves FastSync's
* historical best-effort behavior; an unprivileged attempt is refused by the
* kernel and skipped per entry); OFF forbids the attempt even for root. See
* privilege_super_mode_permitted() in identity.h. */
#define SUPER_MODE_AUTO 0
#define SUPER_MODE_ON 1
#define SUPER_MODE_OFF 2
Config* config_create(void); Config* config_create(void);
void config_delete(Config* config); void config_delete(Config* config);
@@ -718,6 +754,17 @@ bool config_delete_timing_early(const Config* config);
* set (none = the default delete-after commit timing); without deletion no * set (none = the default delete-after commit timing); without deletion no
* timing flag may be set (each timing flag implies --delete). */ * timing flag may be set (each timing flag implies --delete). */
bool config_has_valid_delete_timing(const Config* config); bool config_has_valid_delete_timing(const Config* config);
/* Single source of truth for the cross-field ("combination") invariants a
* Config must satisfy. Returns NULL when `config` is consistent, or a static,
* human-readable error string (no trailing period) describing the FIRST
* violation found. No I/O, no logging and no printing, so it is safe to call
* from every trust boundary; the iconv rule does invoke charset_spec_valid
* (which parses via str_dup/iconv_open), so it is not allocation-free. The client calls
* it from validate_config() for up-front UX and the server calls it from
* validate_received_config() so the receiver enforces exactly the same
* invariants it relies on (the server is the trust boundary). */
const char* config_invariants_error(const Config* config);
/* True when at least one --compare-dest/--copy-dest/--link-dest was set. */ /* True when at least one --compare-dest/--copy-dest/--link-dest was set. */
bool config_has_basis(const Config* config); bool config_has_basis(const Config* config);
/* Append one basis-dir entry. Returns 0 on success, -1 on allocation failure. */ /* Append one basis-dir entry. Returns 0 on success, -1 on allocation failure. */
+281 -4
View File
@@ -1,8 +1,13 @@
#include "daemon_conf.h" #include "daemon_conf.h"
#include "credentials.h"
#include "utils.h" #include "utils.h"
#include <arpa/inet.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <netinet/in.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -48,6 +53,158 @@ static bool parse_bool_value(const char* value, bool* out) {
return false; return false;
} }
/* Parse an IPv4/IPv6 CIDR "addr/prefix" into `bytes`/`*family`. Returns false
* for a malformed address, a missing/oversized prefix, or a prefix that does
* not fit the address family. */
static bool parse_cidr(const char* cidr, int* prefix_out, uint8_t* bytes, int* family_out) {
const char* slash = strchr(cidr, '/');
if (!slash)
return false;
size_t addr_len = (size_t)(slash - cidr);
if (addr_len == 0 || addr_len >= INET6_ADDRSTRLEN)
return false;
char addr[INET6_ADDRSTRLEN];
memcpy(addr, cidr, addr_len);
addr[addr_len] = '\0';
char* end = NULL;
long prefix = strtol(slash + 1, &end, 10);
if (end == slash + 1 || *end != '\0')
return false;
struct in_addr v4;
struct in6_addr v6;
if (inet_pton(AF_INET, addr, &v4) == 1) {
if (prefix < 0 || prefix > 32)
return false;
memcpy(bytes, &v4, sizeof(v4));
*prefix_out = (int)prefix;
*family_out = AF_INET;
return true;
}
if (inet_pton(AF_INET6, addr, &v6) == 1) {
if (prefix < 0 || prefix > 128)
return false;
memcpy(bytes, &v6, sizeof(v6));
*prefix_out = (int)prefix;
*family_out = AF_INET6;
return true;
}
return false;
}
/* A host pattern is valid when it is `*`, a valid IPv4/IPv6 literal, or a valid
* CIDR. Peer addresses reaching the matcher are always numeric, so hostname
* globs are rejected at parse time: accepting one would create a deny rule that
* silently never matches (fail-open). */
static bool host_pattern_valid(const char* pattern) {
if (!pattern || *pattern == '\0')
return false;
if (strcmp(pattern, "*") == 0)
return true;
if (strchr(pattern, '/')) {
uint8_t bytes[16];
int prefix;
int family;
return parse_cidr(pattern, &prefix, bytes, &family);
}
struct in_addr v4;
struct in6_addr v6;
return inet_pton(AF_INET, pattern, &v4) == 1 || inet_pton(AF_INET6, pattern, &v6) == 1;
}
/* Append every comma- and/or whitespace-separated host pattern in `value` to
* the heap-owned list (or replace the list when `replace` is set, which --dparam
* uses so an override can narrow access rather than only widen it). Returns
* false (err filled) on an invalid pattern or an allocation failure. */
static bool store_host_list(char*** list, int* count, const char* value, const char* key,
const char* module_name, bool replace, char* err, size_t err_size) {
if (replace) {
for (int i = 0; i < *count; i++)
free((*list)[i]);
free(*list);
*list = NULL;
*count = 0;
}
char* copy = str_dup(value);
if (!copy) {
if (module_name)
set_error(err, err_size, "out of memory parsing '%s' for module '%s'", key, module_name);
else
set_error(err, err_size, "out of memory parsing '%s'", key);
return false;
}
char* save = NULL;
for (char* token = strtok_r(copy, ", \t", &save); token; token = strtok_r(NULL, ", \t", &save)) {
if (!host_pattern_valid(token)) {
if (module_name)
set_error(err, err_size, "module '%s': invalid host pattern '%s' in '%s'", module_name,
token, key);
else
set_error(err, err_size, "invalid host pattern '%s' in '%s'", token, key);
free(copy);
return false;
}
char** grown = realloc(*list, (size_t)(*count + 1) * sizeof(char*));
if (!grown) {
if (module_name)
set_error(err, err_size, "out of memory parsing '%s' for module '%s'", key, module_name);
else
set_error(err, err_size, "out of memory parsing '%s'", key);
free(copy);
return false;
}
*list = grown;
char* dup = str_dup(token);
if (!dup) {
if (module_name)
set_error(err, err_size, "out of memory parsing '%s' for module '%s'", key, module_name);
else
set_error(err, err_size, "out of memory parsing '%s'", key);
free(copy);
return false;
}
(*list)[(*count)++] = dup;
}
free(copy);
return true;
}
/* Parse a `max connections` value: a positive integer (0/negative/garbage are
* rejected because they would silently disable the cap or admit nothing). */
static bool store_max_connections(int* slot, const char* value, const char* module_name, char* err,
size_t err_size) {
char* end = NULL;
errno = 0;
long n = strtol(value, &end, 10);
if (*value == '\0' || errno != 0 || *end != '\0' || n <= 0 || n > INT_MAX) {
if (module_name)
set_error(err, err_size,
"module '%s': invalid 'max connections' '%s' (must be a positive "
"integer)",
module_name, value);
else
set_error(err, err_size, "invalid 'max connections' '%s' (must be a positive integer)",
value);
return false;
}
*slot = (int)n;
return true;
}
/* Parse an `auth failure delay` value: 0 (disabled) through the configured cap. */
static bool store_auth_failure_delay(int* slot, const char* value, char* err, size_t err_size) {
char* end = NULL;
errno = 0;
long n = strtol(value, &end, 10);
if (*value == '\0' || errno != 0 || *end != '\0' || n < 0 ||
n > DAEMON_CONF_MAX_AUTH_FAILURE_DELAY_MS) {
set_error(err, err_size, "invalid 'auth failure delay' '%s' (must be 0-%d milliseconds)", value,
DAEMON_CONF_MAX_AUTH_FAILURE_DELAY_MS);
return false;
}
*slot = (int)n;
return true;
}
bool daemon_module_name_valid(const char* name) { bool daemon_module_name_valid(const char* name) {
if (!name || *name == '\0') if (!name || *name == '\0')
return false; return false;
@@ -68,14 +225,25 @@ DaemonConf* daemon_conf_create(void) {
if (!conf) if (!conf)
return NULL; return NULL;
conf->global.port = DAEMON_CONF_DEFAULT_PORT; conf->global.port = DAEMON_CONF_DEFAULT_PORT;
conf->global.max_connections = DAEMON_CONF_DEFAULT_MAX_CONNECTIONS;
conf->global.auth_failure_delay_ms = DAEMON_CONF_DEFAULT_AUTH_FAILURE_DELAY_MS;
return conf; return conf;
} }
/* Free a heap-owned pattern list of `count` entries. */
static void free_string_list(char** list, int count) {
for (int i = 0; i < count; i++)
free(list[i]);
free(list);
}
void daemon_conf_free(DaemonConf* conf) { void daemon_conf_free(DaemonConf* conf) {
if (!conf) if (!conf)
return; return;
free(conf->global.motd_file); free(conf->global.motd_file);
free(conf->global.address); free(conf->global.address);
free_string_list(conf->global.hosts_allow, conf->global.hosts_allow_count);
free_string_list(conf->global.hosts_deny, conf->global.hosts_deny_count);
for (int i = 0; i < conf->module_count; i++) { for (int i = 0; i < conf->module_count; i++) {
DaemonModule* m = &conf->modules[i]; DaemonModule* m = &conf->modules[i];
free(m->name); free(m->name);
@@ -83,6 +251,8 @@ void daemon_conf_free(DaemonConf* conf) {
for (int j = 0; j < m->auth_user_count; j++) for (int j = 0; j < m->auth_user_count; j++)
free(m->auth_users[j]); free(m->auth_users[j]);
free(m->auth_users); free(m->auth_users);
free_string_list(m->hosts_allow, m->hosts_allow_count);
free_string_list(m->hosts_deny, m->hosts_deny_count);
} }
free(conf->modules); free(conf->modules);
free(conf); free(conf);
@@ -122,8 +292,8 @@ static bool store_port(int* slot, const char* value, char* err, size_t err_size)
/* Apply a global scalar key/value. Keys are case-insensitive. Returns false /* Apply a global scalar key/value. Keys are case-insensitive. Returns false
* (err filled) on an unknown key or an invalid value. */ * (err filled) on an unknown key or an invalid value. */
static bool apply_global_key(DaemonConf* conf, char* key, const char* value, char* err, static bool apply_global_key(DaemonConf* conf, char* key, const char* value, bool replace_hosts,
size_t err_size) { char* err, size_t err_size) {
if (key_equals(key, "port")) if (key_equals(key, "port"))
return store_port(&conf->global.port, value, err, err_size); return store_port(&conf->global.port, value, err, err_size);
if (key_equals(key, "motd file")) { if (key_equals(key, "motd file")) {
@@ -140,6 +310,16 @@ static bool apply_global_key(DaemonConf* conf, char* key, const char* value, cha
} }
return true; return true;
} }
if (key_equals(key, "max connections"))
return store_max_connections(&conf->global.max_connections, value, NULL, err, err_size);
if (key_equals(key, "auth failure delay"))
return store_auth_failure_delay(&conf->global.auth_failure_delay_ms, value, err, err_size);
if (key_equals(key, "hosts allow"))
return store_host_list(&conf->global.hosts_allow, &conf->global.hosts_allow_count, value,
"hosts allow", NULL, replace_hosts, err, err_size);
if (key_equals(key, "hosts deny"))
return store_host_list(&conf->global.hosts_deny, &conf->global.hosts_deny_count, value,
"hosts deny", NULL, replace_hosts, err, err_size);
set_error(err, err_size, "unknown global key '%s'", key); set_error(err, err_size, "unknown global key '%s'", key);
return false; return false;
} }
@@ -192,6 +372,12 @@ static bool apply_module_key(DaemonModule* module, char* key, char* value, char*
const char* user = trim_ws(token); const char* user = trim_ws(token);
if (*user == '\0') if (*user == '\0')
continue; continue;
if (!credentials_username_valid(user)) {
set_error(err, err_size, "module '%s': invalid 'auth users' entry '%s'", module->name,
user);
free(list);
return false;
}
char** grown = char** grown =
realloc(module->auth_users, (size_t)(module->auth_user_count + 1) * sizeof(char*)); realloc(module->auth_users, (size_t)(module->auth_user_count + 1) * sizeof(char*));
if (!grown) { if (!grown) {
@@ -213,6 +399,14 @@ static bool apply_module_key(DaemonModule* module, char* key, char* value, char*
free(list); free(list);
return true; return true;
} }
if (key_equals(key, "max connections"))
return store_max_connections(&module->max_connections, value, module->name, err, err_size);
if (key_equals(key, "hosts allow"))
return store_host_list(&module->hosts_allow, &module->hosts_allow_count, value, "hosts allow",
false, module->name, err, err_size);
if (key_equals(key, "hosts deny"))
return store_host_list(&module->hosts_deny, &module->hosts_deny_count, value, "hosts deny",
false, module->name, err, err_size);
set_error(err, err_size, "unknown key '%s' in module '%s'", key, module->name); set_error(err, err_size, "unknown key '%s' in module '%s'", key, module->name);
return false; return false;
} }
@@ -393,7 +587,7 @@ DaemonConf* daemon_conf_load(const char* path, char* err, size_t err_size) {
break; break;
} }
} else { } else {
if (!apply_global_key(conf, key, value, err, err_size)) { if (!apply_global_key(conf, key, value, false, err, err_size)) {
ok = false; ok = false;
break; break;
} }
@@ -448,7 +642,90 @@ int daemon_conf_apply_dparam(DaemonConf* conf, const char* assignment, char* err
set_error(err, err_size, "--dparam '%s' has an empty value", assignment); set_error(err, err_size, "--dparam '%s' has an empty value", assignment);
return -1; return -1;
} }
bool ok = apply_global_key(conf, key, value, err, err_size); bool ok = apply_global_key(conf, key, value, true, err, err_size);
free(copy); free(copy);
return ok ? 0 : -1; return ok ? 0 : -1;
} }
/* Compare the first `prefix` bits of two 16-byte address buffers. */
static bool bit_prefix_match(const uint8_t* a, const uint8_t* b, int prefix) {
int whole = prefix / 8;
if (whole > 0 && memcmp(a, b, (size_t)whole) != 0)
return false;
int remainder = prefix % 8;
if (remainder == 0)
return true;
uint8_t mask = (uint8_t)(0xffu << (8 - remainder));
return (a[whole] & mask) == (b[whole] & mask);
}
/* Case-insensitive glob match used for hostname patterns. Falls back to the
* shared case-sensitive matcher when an operand is too long for the stack
* buffers. */
static bool host_glob_match(const char* pattern, const char* str) {
char pbuf[256];
char sbuf[256];
size_t plen = strlen(pattern);
size_t slen = strlen(str);
if (plen >= sizeof(pbuf) || slen >= sizeof(sbuf))
return glob_match(pattern, str);
for (size_t i = 0; i <= plen; i++)
pbuf[i] = (char)tolower((unsigned char)pattern[i]);
for (size_t i = 0; i <= slen; i++)
sbuf[i] = (char)tolower((unsigned char)str[i]);
return glob_match(pbuf, sbuf);
}
bool daemon_host_pattern_match(const char* pattern, const char* peer_ip) {
if (!pattern || *pattern == '\0' || !peer_ip || *peer_ip == '\0')
return false;
if (strcmp(pattern, "*") == 0)
return true;
if (strchr(pattern, '/')) {
uint8_t pattern_bytes[16];
uint8_t peer_bytes[16];
int prefix = 0;
int family = AF_UNSPEC;
if (!parse_cidr(pattern, &prefix, pattern_bytes, &family))
return false;
if (inet_pton(family, peer_ip, peer_bytes) != 1)
return false;
return bit_prefix_match(pattern_bytes, peer_bytes, prefix);
}
struct in_addr pattern_v4;
struct in_addr peer_v4;
if (inet_pton(AF_INET, pattern, &pattern_v4) == 1)
return inet_pton(AF_INET, peer_ip, &peer_v4) == 1 && pattern_v4.s_addr == peer_v4.s_addr;
struct in6_addr pattern_v6;
struct in6_addr peer_v6;
if (inet_pton(AF_INET6, pattern, &pattern_v6) == 1)
return inet_pton(AF_INET6, peer_ip, &peer_v6) == 1 &&
memcmp(&pattern_v6, &peer_v6, sizeof(pattern_v6)) == 0;
/* Not a literal: a hostname/glob pattern. */
return host_glob_match(pattern, peer_ip);
}
bool daemon_hosts_allowed(const char* peer_ip, char* const* allow, int allow_count,
char* const* deny, int deny_count) {
if (!peer_ip)
return false;
for (int i = 0; i < deny_count; i++) {
if (daemon_host_pattern_match(deny[i], peer_ip))
return false;
}
if (allow_count > 0) {
for (int i = 0; i < allow_count; i++) {
if (daemon_host_pattern_match(allow[i], peer_ip))
return true;
}
return false;
}
return true;
}
bool daemon_hosts_restricted(char* const* allow, int allow_count, char* const* deny,
int deny_count) {
(void)allow;
(void)deny;
return allow_count > 0 || deny_count > 0;
}
+55 -6
View File
@@ -52,14 +52,32 @@ typedef struct DaemonModule {
activities. Without it the daemon refuses all of them. */ activities. Without it the daemon refuses all of them. */
char** auth_users; /* `auth users = a,b`; Wave B credential list */ char** auth_users; /* `auth users = a,b`; Wave B credential list */
int auth_user_count; int auth_user_count;
/* `max connections = N` (optional per-module cap). 0 means "not set"
* (inherit the global cap). Parsed, stored, and validated, but NOT enforced
* per-module: connections are counted in the accept-loop parent before the
* client's module is known, so only the global cap is enforced (see
* transport_tcp.c and the Daemon Mode notes in RSYNC_COMPAT.md). */
int max_connections;
char** hosts_allow; /* `hosts allow = a,b`; host access allow patterns */
int hosts_allow_count;
char** hosts_deny; /* `hosts deny = a,b`; host access deny patterns */
int hosts_deny_count;
} DaemonModule; } DaemonModule;
/* Global (pre-module) scalar keys. `motd file` is parsed and stored but has /* Global (pre-module) scalar keys. `motd file` is parsed and stored but has
* no wire effect yet (MOTD display is Wave C). */ * no wire effect yet (MOTD display is Wave C). */
typedef struct DaemonConfGlobals { typedef struct DaemonConfGlobals {
int port; /* `port`, default DAEMON_CONF_DEFAULT_PORT (873) */ int port; /* `port`, default DAEMON_CONF_DEFAULT_PORT (873) */
char* motd_file; /* `motd file`, may be NULL */ char* motd_file; /* `motd file`, may be NULL */
char* address; /* `address` (optional bind address), may be NULL */ char* address; /* `address` (optional bind address), may be NULL */
int max_connections; /* `max connections`, default
DAEMON_CONF_DEFAULT_MAX_CONNECTIONS (100) */
int auth_failure_delay_ms; /* `auth failure delay`, milliseconds; default
DAEMON_CONF_DEFAULT_AUTH_FAILURE_DELAY_MS */
char** hosts_allow; /* `hosts allow`; global host access allow patterns */
int hosts_allow_count;
char** hosts_deny; /* `hosts deny`; global host access deny patterns */
int hosts_deny_count;
} DaemonConfGlobals; } DaemonConfGlobals;
typedef struct DaemonConf { typedef struct DaemonConf {
@@ -69,6 +87,16 @@ typedef struct DaemonConf {
} DaemonConf; } DaemonConf;
#define DAEMON_CONF_DEFAULT_PORT 873 #define DAEMON_CONF_DEFAULT_PORT 873
/* Default global connection cap when `max connections` is absent. Matches the
* historical hardcoded listener value. */
#define DAEMON_CONF_DEFAULT_MAX_CONNECTIONS 100
/* Default `auth failure delay` in milliseconds (0 disables the throttle). */
#define DAEMON_CONF_DEFAULT_AUTH_FAILURE_DELAY_MS 500
/* Largest accepted `auth failure delay`, so a typo cannot pin a connection
* child in nanosleep for an absurd time. */
/* Bounded well below the socket I/O timeout so a failed-auth child cannot hold
* a connection slot for long enough to amplify connection-cap exhaustion. */
#define DAEMON_CONF_MAX_AUTH_FAILURE_DELAY_MS 5000
/* Longest accepted config line (excluding the trailing newline). Longer lines /* Longest accepted config line (excluding the trailing newline). Longer lines
* are rejected rather than buffered unboundedly. */ * are rejected rather than buffered unboundedly. */
#define DAEMON_CONF_MAX_LINE 4096 #define DAEMON_CONF_MAX_LINE 4096
@@ -99,9 +127,30 @@ const DaemonModule* daemon_conf_find_module(const DaemonConf* conf, const char*
bool daemon_module_name_valid(const char* name); bool daemon_module_name_valid(const char* name);
/* Parse one --dparam=KEY=VALUE (or "--dparam KEY=VALUE") override string and /* Parse one --dparam=KEY=VALUE (or "--dparam KEY=VALUE") override string and
* apply it to the global scalars only. Keys are case-insensitive and limited * apply it to the global keys only. Keys are case-insensitive and limited to
* to the global scalar keys defined by the grammar (port, motd file, address). * the global keys defined by the grammar (port, motd file, address,
* Returns 0 on success, -1 on error (err filled). */ * max connections, auth failure delay, hosts allow, hosts deny). Returns 0 on
* success, -1 on error (err filled). */
int daemon_conf_apply_dparam(DaemonConf* conf, const char* assignment, char* err, size_t err_size); int daemon_conf_apply_dparam(DaemonConf* conf, const char* assignment, char* err, size_t err_size);
/* Host access-control matching (pure; no I/O). `daemon_host_pattern_match`
* matches one configured pattern against a numeric peer IP string. Supported
* patterns: `*` (match anything), an IPv4/IPv6 literal, an IPv4/IPv6 CIDR
* (`10.0.0.0/8`, `2001:db8::/32`), or a glob (`*.example.com`) evaluated with
* the same matcher as file globs; a glob only matches a peer string of the
* same shape, so a numeric peer never matches a hostname glob. */
bool daemon_host_pattern_match(const char* pattern, const char* peer_ip);
/* rsync-like combined decision over a deny list and an allow list: a matching
* deny rejects (deny takes precedence); otherwise, when any allow entries
* exist, a peer that matches none is rejected; with no allow entries every
* peer not denied is accepted. An empty/unset pair returns true. */
bool daemon_hosts_allowed(const char* peer_ip, char* const* allow, int allow_count,
char* const* deny, int deny_count);
/* True when at least one allow or deny pattern is configured (i.e. an
* unprovable peer must fail closed rather than being treated as unrestricted). */
bool daemon_hosts_restricted(char* const* allow, int allow_count, char* const* deny,
int deny_count);
#endif #endif
+3 -2
View File
@@ -18,8 +18,9 @@ typedef struct {
/* Receiver-side --delay-updates staging registry. All successfully written /* Receiver-side --delay-updates staging registry. All successfully written
files land under a private staging directory inside the receive root and are files land under a private staging directory inside the receive root and are
atomically renamed into their final destination only at the very end of the atomically renamed into their final destination only at the very end of the
transfer. A single PipelineContextReceiver has exactly one writer thread, transfer. A single receiver pipeline (see src/server/receiver_pipeline.h)
but the registry is still mutex-protected so the same object can be safely has exactly one writer thread, but the registry is still mutex-protected so
the same object can be safely
shared with the publish/cleanup phase that runs after the threads join. */ shared with the publish/cleanup phase that runs after the threads join. */
typedef struct DelayUpdatesContext { typedef struct DelayUpdatesContext {
char* root_directory; /* receive root the staging dir lives under */ char* root_directory; /* receive root the staging dir lives under */
-9
View File
@@ -287,11 +287,6 @@ size_t file_content_to_buffer(File* file) {
static int authorized_root_fd = -1; static int authorized_root_fd = -1;
static char* authorized_root_path; static char* authorized_root_path;
static bool path_is_within_root(const char* root, const char* path) {
size_t root_len = strlen(root);
return strncmp(root, path, root_len) == 0 && (path[root_len] == '\0' || path[root_len] == '/');
}
bool file_set_authorized_root(int fd, const char* canonical_path) { bool file_set_authorized_root(int fd, const char* canonical_path) {
char* path_copy = canonical_path ? str_dup(canonical_path) : NULL; char* path_copy = canonical_path ? str_dup(canonical_path) : NULL;
if (canonical_path && !path_copy) { if (canonical_path && !path_copy) {
@@ -362,10 +357,6 @@ void file_set_keep_dirlinks(bool enable) {
file_keep_dirlinks = enable; file_keep_dirlinks = enable;
} }
bool file_get_keep_dirlinks(void) {
return file_keep_dirlinks;
}
/* --trust-sender (Phase 5) receiver process-wide policy: when set, the receiver /* --trust-sender (Phase 5) receiver process-wide policy: when set, the receiver
* trusts the sender's file list and skips its own redundant up-front re- * trusts the sender's file list and skips its own redundant up-front re-
* validation (empty/".." path rejection, escaping-symlink-target containment). * validation (empty/".." path rejection, escaping-symlink-target containment).
-1
View File
@@ -52,7 +52,6 @@ bool file_symlink_at_secure(const char* path, const char* target);
/* --keep-dirlinks (-K) receiver process-wide policy: allow an in-root existing /* --keep-dirlinks (-K) receiver process-wide policy: allow an in-root existing
* symlink-to-directory to be followed as a directory. */ * symlink-to-directory to be followed as a directory. */
void file_set_keep_dirlinks(bool enable); void file_set_keep_dirlinks(bool enable);
bool file_get_keep_dirlinks(void);
/* --trust-sender receiver process-wide policy (Phase 5). When set, the /* --trust-sender receiver process-wide policy (Phase 5). When set, the
* receiver trusts that the sender already produced a clean file list and skips * receiver trusts that the sender already produced a clean file list and skips
+53 -20
View File
@@ -2,6 +2,7 @@
#include "log.h" #include "log.h"
#include "utils.h" #include "utils.h"
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -51,7 +52,8 @@ static int normalize_entry(const char* raw, size_t len, bool strip_line_endings,
if (len == 0) if (len == 0)
return 0; return 0;
if (raw[0] == '/') { if (raw[0] == '/') {
snprintf(err, err_size, "absolute path entries are not allowed: '%.*s'", (int)len, raw); int print_len = len > (size_t)INT_MAX ? INT_MAX : (int)len;
snprintf(err, err_size, "absolute path entries are not allowed: '%.*s'", print_len, raw);
return -1; return -1;
} }
/* Reject NUL bytes inside a token defensively (NUL-delimited mode splits on /* Reject NUL bytes inside a token defensively (NUL-delimited mode splits on
@@ -102,8 +104,27 @@ static int normalize_entry(const char* raw, size_t len, bool strip_line_endings,
return result; return result;
} }
/* Build the membership index over the exact entries only. `file_list_affects`
combines the exact/descendant lookups with a walk of the query's own ancestor
prefixes, so no ancestor prefix is ever materialized as a copy and the index
stays O(entry count) memory regardless of path depth. An empty entry (the
source root) sets whole_tree and short-circuits every query. */
static bool file_list_index_build(FileListSet* set, char* err, size_t err_size) {
if (!path_index_build(&set->index, (const char* const*)set->entries, (size_t)set->count)) {
snprintf(err, err_size, "memory allocation failed");
return false;
}
for (int i = 0; i < set->count; i++) {
if (set->entries[i][0] == '\0') {
set->whole_tree = true;
break;
}
}
return true;
}
static FileListSet* string_list_to_set(StringList* raw, char* err, size_t err_size) { static FileListSet* string_list_to_set(StringList* raw, char* err, size_t err_size) {
FileListSet* set = malloc(sizeof(FileListSet)); FileListSet* set = calloc(1, sizeof(FileListSet));
if (!set) { if (!set) {
snprintf(err, err_size, "memory allocation failed"); snprintf(err, err_size, "memory allocation failed");
return NULL; return NULL;
@@ -112,6 +133,10 @@ static FileListSet* string_list_to_set(StringList* raw, char* err, size_t err_si
set->entries = raw->items; set->entries = raw->items;
raw->items = NULL; raw->items = NULL;
raw->count = 0; raw->count = 0;
if (!file_list_index_build(set, err, err_size)) {
file_list_destroy(set);
return NULL;
}
return set; return set;
} }
@@ -158,34 +183,42 @@ FileListSet* file_list_load(const char* path, bool null_separated, char* err, si
void file_list_destroy(FileListSet* set) { void file_list_destroy(FileListSet* set) {
if (!set) if (!set)
return; return;
path_index_free(&set->index);
for (int i = 0; i < set->count; i++) for (int i = 0; i < set->count; i++)
free(set->entries[i]); free(set->entries[i]);
free(set->entries); free(set->entries);
free(set); free(set);
} }
static bool path_has_prefix(const char* path, const char* prefix) {
size_t plen = strlen(prefix);
if (strncmp(path, prefix, plen) != 0)
return false;
return path[plen] == '/' || path[plen] == '\0';
}
bool file_list_affects(const FileListSet* set, const char* rel) { bool file_list_affects(const FileListSet* set, const char* rel) {
if (!set) if (!set)
return true; return true;
if (!rel) if (!rel)
return false; return false;
for (int i = 0; i < set->count; i++) { if (set->whole_tree)
const char* entry = set->entries[i]; return true; /* whole tree listed */
if (entry[0] == '\0') /* An exact entry match means `rel` itself is listed. */
return true; /* whole tree listed */ if (path_index_contains(&set->index, rel))
if (strcmp(rel, entry) == 0) return true;
return true; /* the entry itself is listed */ /* Otherwise `rel` is affected when a listed entry is an ancestor directory of
if (path_has_prefix(rel, entry)) it; walk rel's own directory prefixes (which preserve path-boundary
return true; /* rel lives under a listed directory */ semantics) and test each for an exact entry. No prefixes are stored. */
if (path_has_prefix(entry, rel)) size_t len = strlen(rel);
return true; /* rel is an ancestor directory of a listed entry */ while (len > 0) {
const char* slash = NULL;
for (size_t i = len; i-- > 0;) {
if (rel[i] == '/') {
slash = rel + i;
break;
}
}
if (!slash)
break;
len = (size_t)(slash - rel);
if (path_index_contains_n(&set->index, rel, len))
return true;
} }
return false; /* Finally `rel` is affected when it is an ancestor directory of a listed
entry (binary search for the first entry at or after `rel` + '/'). */
return path_index_has_descendant(&set->index, rel);
} }
+10 -2
View File
@@ -1,6 +1,7 @@
#ifndef FILE_LIST_H #ifndef FILE_LIST_H
#define FILE_LIST_H #define FILE_LIST_H
#include "utils.h"
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
@@ -12,11 +13,18 @@
* of "." means the whole tree, absolute entries and ".." traversal are * of "." means the whole tree, absolute entries and ".." traversal are
* rejected at parse time. The set is immutable and shared read-only across * rejected at parse time. The set is immutable and shared read-only across
* scanner worker threads. * scanner worker threads.
*/ *
* Membership is answered from `index`, built once at load time over the exact
* entries only: `index.exact` matches a listed path, the sorted view detects an
* ancestor directory of a listed entry, and `rel`'s own directory prefixes are
* matched against the exact set while descending. No ancestor prefix is stored
* as a separate string, so the index is O(entry count) memory however deep the
* paths are, and each query is O(path length) comparisons. */
typedef struct { typedef struct {
char** entries; /* normalized rel paths; "" means the whole tree */ char** entries; /* normalized rel paths; "" means the whole tree */
int count; int count;
PathIndex index;
bool whole_tree; /* an entry of "" lists the source root */
} FileListSet; } FileListSet;
/* Load and validate a --files-from file. When `null_separated` (-0/--from0) /* Load and validate a --files-from file. When `null_separated` (-0/--from0)
+22 -2
View File
@@ -1696,9 +1696,9 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
return NULL; return NULL;
} }
if (has_path_traversal(check_path)) { if (check_path[0] == '\0' || has_path_traversal(check_path)) {
char* escaped_path = output_escape(check_path, log_get_8_bit_output()); char* escaped_path = output_escape(check_path, log_get_8_bit_output());
log_message(LOG_LEVEL_ERROR, "Path traversal detected: %s", log_message(LOG_LEVEL_ERROR, "Invalid received check path: %s",
escaped_path ? escaped_path : "<allocation failed>"); escaped_path ? escaped_path : "<allocation failed>");
free(escaped_path); free(escaped_path);
free(check_path); free(check_path);
@@ -1832,6 +1832,7 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
existing/ignore-existing/update/backup/delay-updates policy. */ existing/ignore-existing/update/backup/delay-updates policy. */
File* materialized = file_create(check_path); File* materialized = file_create(check_path);
if (materialized && basis.content) { if (materialized && basis.content) {
data_destroy(materialized->data);
materialized->data = basis.content; materialized->data = basis.content;
basis.content = NULL; basis.content = NULL;
materialized->metadata = file_metadata_create(NULL, &basis.st, false, false); materialized->metadata = file_metadata_create(NULL, &basis.st, false, false);
@@ -2095,6 +2096,7 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
file->metadata = meta; file->metadata = meta;
file->xattrs = append_xattrs; file->xattrs = append_xattrs;
append_xattrs = NULL; append_xattrs = NULL;
data_destroy(file->data);
file->data = data_create(full, full_size); file->data = data_create(full, full_size);
if (!file->data) { /* data_create already freed full on failure */ if (!file->data) { /* data_create already freed full on failure */
file_destroy(file); file_destroy(file);
@@ -2236,6 +2238,10 @@ File* file_receive(const Config* config, int file_descriptor) {
/* ---- P7 Wave D: deferred directory times ---- */ /* ---- P7 Wave D: deferred directory times ---- */
bool dir_times_should_capture(const Config* config) {
return config->use_metadata && !config->omit_dir_times;
}
void dir_time_list_init(DirTimeList* list) { void dir_time_list_init(DirTimeList* list) {
if (!list) if (!list)
return; return;
@@ -2243,6 +2249,7 @@ void dir_time_list_init(DirTimeList* list) {
list->entries = NULL; list->entries = NULL;
list->count = 0; list->count = 0;
list->capacity = 0; list->capacity = 0;
list->bytes = 0;
} }
void dir_time_list_free(DirTimeList* list) { void dir_time_list_free(DirTimeList* list) {
@@ -2256,11 +2263,23 @@ void dir_time_list_free(DirTimeList* list) {
list->entries = NULL; list->entries = NULL;
list->count = 0; list->count = 0;
list->capacity = 0; list->capacity = 0;
list->bytes = 0;
} }
bool dir_time_list_add(DirTimeList* list, const char* wire_path, const FileMetadata* metadata) { bool dir_time_list_add(DirTimeList* list, const char* wire_path, const FileMetadata* metadata) {
if (!list || !wire_path || !metadata) if (!list || !wire_path || !metadata)
return true; /* nothing to remember; never a hard error */ return true; /* nothing to remember; never a hard error */
/* Cumulative, not per-frame: the sender may stream a tree across unbounded
STATUS_DIR_TIMES frames, so bound the TOTAL retained here. Reject before
touching the list, leaving it exactly as it was (the caller fails the
transfer, which becomes a clean protocol error). */
size_t path_len = strlen(wire_path);
/* Charge the whole per-entry cost (path copy + pointer slot + metadata
struct), not just the path, so the array growth is bounded by the same
cumulative budget. */
size_t entry_cost = path_len + sizeof(FileMetadata) + sizeof(char*);
if (list->count >= MAX_DIR_TIME_ENTRIES || entry_cost > MAX_DIR_TIME_BYTES - list->bytes)
return false;
if (list->count == list->capacity) { if (list->count == list->capacity) {
size_t new_capacity = list->capacity == 0 ? 16 : list->capacity * 2; size_t new_capacity = list->capacity == 0 ? 16 : list->capacity * 2;
if (new_capacity < list->capacity) if (new_capacity < list->capacity)
@@ -2287,6 +2306,7 @@ bool dir_time_list_add(DirTimeList* list, const char* wire_path, const FileMetad
list->paths[list->count] = copy; list->paths[list->count] = copy;
list->entries[list->count] = *metadata; list->entries[list->count] = *metadata;
list->count++; list->count++;
list->bytes += entry_cost;
return true; return true;
} }
+18 -1
View File
@@ -7,6 +7,15 @@
/* Server-side file receive/save path. */ /* Server-side file receive/save path. */
/* Cumulative caps for the deferred directory-time accumulator. The sender may
* legitimately split a large tree across repeated STATUS_DIR_TIMES frames, so a
* per-frame bound is not enough: the receiver must bound the TOTAL it retains
* against a hostile sender. Mirror the delete-manifest limits
* (MAX_MANIFEST_ENTRIES / MAX_MANIFEST_BYTES): the entry count bounds the
* metadata array and the byte budget bounds the concatenated path strings. */
#define MAX_DIR_TIME_ENTRIES (1024 * 1024)
#define MAX_DIR_TIME_BYTES (16ULL * 1024 * 1024)
File* file_receive(const Config* config, int file_descriptor); File* file_receive(const Config* config, int file_descriptor);
File* file_receive_directory(int file_descriptor, const Config* config); File* file_receive_directory(int file_descriptor, const Config* config);
File* file_receive_dir_time(int file_descriptor, const Config* config); File* file_receive_dir_time(int file_descriptor, const Config* config);
@@ -28,12 +37,20 @@ typedef struct {
FileMetadata* entries; /* owned, parallel to paths */ FileMetadata* entries; /* owned, parallel to paths */
size_t count; size_t count;
size_t capacity; size_t capacity;
size_t bytes; /* cumulative strlen of every retained path */
} DirTimeList; } DirTimeList;
/* Capture gate shared by the sender-side and receiver-side sinks: directory
* metadata is accumulated only when --times/--metadata is in effect and
* -O/--omit-dir-times does not suppress it. Kept here, next to the accumulator
* it guards, so both call sites express the same condition. */
bool dir_times_should_capture(const Config* config);
void dir_time_list_init(DirTimeList* list); void dir_time_list_init(DirTimeList* list);
void dir_time_list_free(DirTimeList* list); void dir_time_list_free(DirTimeList* list);
/* Deep-copy one directory's path + metadata into the list. Returns false on /* Deep-copy one directory's path + metadata into the list. Returns false on
* allocation failure (the caller fails the transfer). */ * allocation failure OR when the cumulative entry/byte caps would be exceeded
* (the caller fails the transfer). */
bool dir_time_list_add(DirTimeList* list, const char* wire_path, const FileMetadata* metadata); bool dir_time_list_add(DirTimeList* list, const char* wire_path, const FileMetadata* metadata);
/* Apply every accumulated directory's mtime (and atime when captured) beneath /* Apply every accumulated directory's mtime (and atime when captured) beneath
* `root_directory`, confined fd-relative. Best-effort per entry: an absent * `root_directory`, confined fd-relative. Best-effort per entry: an absent
+1 -1
View File
@@ -145,7 +145,7 @@ bool file_send_sendfile_with_skip(File* file, int file_descriptor, bool use_meta
off_t offset = 0; off_t offset = 0;
struct timespec deadline; struct timespec deadline;
clock_gettime(CLOCK_MONOTONIC, &deadline); clock_gettime(CLOCK_MONOTONIC, &deadline);
deadline.tv_sec += 60; deadline.tv_sec += protocol_get_io_timeout_sec();
while ((unsigned long long)offset < file_size) { while ((unsigned long long)offset < file_size) {
struct timespec now; struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
-186
View File
@@ -1,129 +1,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include "file_store.h" #include "file_store.h"
#include "metadata.h"
#include "utils.h"
static int authorized_root_fd = -1;
static char* authorized_root_path;
static bool path_is_within_root(const char* root, const char* path) {
size_t root_length = strlen(root);
return strncmp(root, path, root_length) == 0 &&
(path[root_length] == '\0' || path[root_length] == '/');
}
bool file_store_set_authorized_root(int fd, const char* canonical_path) {
char* new_path = canonical_path ? str_dup(canonical_path) : NULL;
if (canonical_path && !new_path) {
authorized_root_fd = -1;
free(authorized_root_path);
authorized_root_path = NULL;
return false;
}
free(authorized_root_path);
authorized_root_path = new_path;
authorized_root_fd = fd;
return true;
}
int file_store_open_secure_parent(const char* path, char** leaf_out) {
char* copy = str_dup(path);
if (!copy)
return -1;
char* parent = dirname(copy);
const char* slash = strrchr(path, '/');
char* leaf = str_dup(slash ? slash + 1 : path);
if (!leaf) {
free(copy);
return -1;
}
int fd;
if (authorized_root_fd >= 0) {
if (!authorized_root_path || path[0] != '/' ||
!path_is_within_root(authorized_root_path, path)) {
free(copy);
free(leaf);
return -1;
}
fd = dup(authorized_root_fd);
if (fd < 0) {
free(copy);
free(leaf);
return -1;
}
size_t root_length = strlen(authorized_root_path);
char* relative = str_dup(path + root_length);
if (!relative) {
free(copy);
free(leaf);
close(fd);
return -1;
}
free(copy);
copy = relative;
parent = dirname(copy);
} else {
fd = (parent[0] == '/') ? open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC)
: open(".", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
}
if (fd < 0) {
free(copy);
free(leaf);
return -1;
}
char* save = NULL;
char* component = strtok_r(parent, "/", &save);
while (component) {
if (strcmp(component, "..") == 0) {
close(fd);
free(copy);
free(leaf);
return -1;
}
if (strcmp(component, ".") != 0) {
int next = openat(fd, component, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
if (next < 0 && errno == ENOENT) {
if (mkdirat(fd, component, 0755) == 0 || errno == EEXIST)
next = openat(fd, component, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
}
if (next < 0) {
close(fd);
free(copy);
free(leaf);
return -1;
}
close(fd);
fd = next;
}
component = strtok_r(NULL, "/", &save);
}
free(copy);
*leaf_out = leaf;
return fd;
}
bool file_store_rename_secure(const char* old_path, const char* new_path) {
char *old_leaf = NULL, *new_leaf = NULL;
int old_parent = file_store_open_secure_parent(old_path, &old_leaf);
int new_parent = file_store_open_secure_parent(new_path, &new_leaf);
bool ok = old_parent >= 0 && new_parent >= 0 &&
renameat(old_parent, old_leaf, new_parent, new_leaf) == 0;
if (old_parent >= 0)
close(old_parent);
if (new_parent >= 0)
close(new_parent);
free(old_leaf);
free(new_leaf);
return ok;
}
static bool write_all(int fd, const void* data, unsigned long long size) { static bool write_all(int fd, const void* data, unsigned long long size) {
const unsigned char* p = data; const unsigned char* p = data;
@@ -176,67 +54,3 @@ bool file_store_write_sparse(int fd, const unsigned char* data, unsigned long lo
} }
return ftruncate(fd, (off_t)size) == 0; return ftruncate(fd, (off_t)size) == 0;
} }
bool file_store_write_secure(const char* path, const void* data, unsigned long long data_size,
bool inplace, bool sparse, const FileMetadata* metadata,
bool preserve_executability) {
char* leaf = NULL;
int dirfd = file_store_open_secure_parent(path, &leaf);
if (dirfd < 0)
return false;
int fd = -1;
bool ok = false;
if (inplace) {
fd = openat(dirfd, leaf, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0644);
if (fd >= 0) {
if (sparse && data_size > 0) {
if (ftruncate(fd, (off_t)data_size) == 0)
ok = file_store_write_sparse(fd, data, data_size);
} else {
ok = write_all(fd, data, data_size);
}
if (ok && metadata)
ok = file_restore_metadata_fd(fd, metadata, preserve_executability);
}
} else {
int tmp_size = snprintf(NULL, 0, ".%s.tmp.%ld.%u", leaf, (long)getpid(), 99U);
if (tmp_size < 0) {
close(dirfd);
free(leaf);
return false;
}
char* tmp = malloc((size_t)tmp_size + 1);
if (!tmp) {
close(dirfd);
free(leaf);
return false;
}
for (unsigned int i = 0; i < 100 && !ok; ++i) {
snprintf(tmp, (size_t)tmp_size + 1, ".%s.tmp.%ld.%u", leaf, (long)getpid(), i);
fd = openat(dirfd, tmp, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_NOFOLLOW, 0600);
if (fd < 0)
continue;
if (sparse && data_size > 0)
ok = ftruncate(fd, (off_t)data_size) == 0;
if (ok || (!sparse || data_size == 0))
ok = (sparse && data_size > 0)
? file_store_write_sparse(fd, (const unsigned char*)data, data_size)
: write_all(fd, data, data_size);
if (ok && metadata)
ok = file_restore_metadata_fd(fd, metadata, preserve_executability);
if (close(fd) != 0)
ok = false;
fd = -1;
if (ok && renameat(dirfd, tmp, dirfd, leaf) != 0)
ok = false;
if (!ok)
unlinkat(dirfd, tmp, 0);
}
free(tmp);
}
if (fd >= 0)
close(fd);
close(dirfd);
free(leaf);
return ok;
}
-7
View File
@@ -1,15 +1,8 @@
#ifndef FILE_STORE_H #ifndef FILE_STORE_H
#define FILE_STORE_H #define FILE_STORE_H
#include "file.h"
#include <stdbool.h> #include <stdbool.h>
bool file_store_set_authorized_root(int fd, const char* canonical_path);
int file_store_open_secure_parent(const char* path, char** leaf_out);
bool file_store_rename_secure(const char* old_path, const char* new_path);
bool file_store_write_secure(const char* path, const void* data, unsigned long long data_size,
bool inplace, bool sparse, const FileMetadata* metadata,
bool preserve_executability);
/* Sparse-aware write (--sparse/-S): every all-zero run of at least /* Sparse-aware write (--sparse/-S): every all-zero run of at least
* SPARSE_HOLE_MIN bytes is skipped with lseek(SEEK_CUR) so it becomes a real * SPARSE_HOLE_MIN bytes is skipped with lseek(SEEK_CUR) so it becomes a real
* hole; every other byte is written. The caller pre-sizes the file with * hole; every other byte is written. The caller pre-sizes the file with
+2 -2
View File
@@ -30,7 +30,7 @@ typedef struct {
/* --super / --no-super tri-state (SUPER_MODE_AUTO when unset). Snapshotted /* --super / --no-super tri-state (SUPER_MODE_AUTO when unset). Snapshotted
* per connection so privilege_super_permitted() can gate super-user * per connection so privilege_super_permitted() can gate super-user
* activities without a Config argument. */ * activities without a Config argument. */
int super_mode; SuperMode super_mode;
/* --copy-as=USER[:GROUP]: snapshotted so the ownership resolver can force the /* --copy-as=USER[:GROUP]: snapshotted so the ownership resolver can force the
* target ids without a Config argument. */ * target ids without a Config argument. */
bool copy_as_set; bool copy_as_set;
@@ -128,7 +128,7 @@ bool privilege_super_permitted(void) {
return privilege_super_mode_permitted(g_identity.super_mode); return privilege_super_mode_permitted(g_identity.super_mode);
} }
bool privilege_super_mode_permitted(int mode) { bool privilege_super_mode_permitted(SuperMode mode) {
/* AUTO and ON both attempt the confined operation; OFF forbids it even for a /* AUTO and ON both attempt the confined operation; OFF forbids it even for a
* root receiver. AUTO is the historical FastSync behavior (always attempt * root receiver. AUTO is the historical FastSync behavior (always attempt
* and let the kernel refuse an unprivileged call, which the caller skips), so * and let the kernel refuse an unprivileged call, which the caller skips), so
+1 -1
View File
@@ -128,6 +128,6 @@ bool identity_wire_valid(const Config* config);
* best-effort behavior where an unprivileged attempt is refused by the kernel * best-effort behavior where an unprivileged attempt is refused by the kernel
* and skipped. Neither EVER elevates privileges. */ * and skipped. Neither EVER elevates privileges. */
bool privilege_super_permitted(void); bool privilege_super_permitted(void);
bool privilege_super_mode_permitted(int mode); bool privilege_super_mode_permitted(SuperMode mode);
#endif #endif
+73 -27
View File
@@ -3,7 +3,9 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <threads.h>
#include <time.h> #include <time.h>
static const char* log_level_strings[] = {"DEBUG", "INFO", "WARN", "ERROR"}; static const char* log_level_strings[] = {"DEBUG", "INFO", "WARN", "ERROR"};
@@ -15,6 +17,18 @@ static FILE* log_fp = NULL;
static _Thread_local bool eight_bit_output; static _Thread_local bool eight_bit_output;
static LogStderrMode stderr_mode = LOG_STDERR_ERRORS; static LogStderrMode stderr_mode = LOG_STDERR_ERRORS;
/* Serializes access to log_fp and makes each emitted line atomic: the
* timestamp prefix, formatted body, and trailing newline are written as one
* critical section so concurrent threads cannot interleave partial lines.
* Initialized lazily (matching the protocol.c bw_mutex idiom) because logging
* can happen before main() installs any synchronization. */
static mtx_t log_mutex;
static once_flag log_mutex_once = ONCE_FLAG_INIT;
static void log_mutex_init(void) {
mtx_init(&log_mutex, mtx_plain);
}
void set_log_level(LogLevel level) { void set_log_level(LogLevel level) {
current_log_level = level; current_log_level = level;
} }
@@ -41,7 +55,10 @@ uint32_t get_log_info_flags(void) {
} }
void log_set_file(FILE* fp) { void log_set_file(FILE* fp) {
call_once(&log_mutex_once, log_mutex_init);
mtx_lock(&log_mutex);
log_fp = fp; log_fp = fp;
mtx_unlock(&log_mutex);
} }
void log_set_8_bit_output(bool enabled) { void log_set_8_bit_output(bool enabled) {
@@ -60,13 +77,48 @@ LogStderrMode log_get_stderr_mode(void) {
return stderr_mode; return stderr_mode;
} }
static inline void write_message(FILE* dest_io, LogLevel log_level, struct tm t, const char* format, /* Format one complete log line (timestamp prefix + body + newline) into a
va_list args) { * freshly allocated buffer. This is pure CPU/malloc work and must happen
fprintf(dest_io, "%04d-%02d-%02d %02d:%02d:%02d [%s]: ", t.tm_year + 1900, t.tm_mon + 1, * OUTSIDE the log mutex: the mutex only guards the log_fp pointer, so a
t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, log_level_strings[log_level]); * stalled stderr/stdout pipe cannot block every logging thread. Returns NULL
* on allocation/formatting failure. */
static char* format_log_line(LogLevel log_level, const struct tm* t, const char* format,
va_list args) {
char prefix[64];
int prefix_len = snprintf(
prefix, sizeof(prefix), "%04d-%02d-%02d %02d:%02d:%02d [%s]: ", t->tm_year + 1900,
t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, log_level_strings[log_level]);
if (prefix_len < 0 || prefix_len >= (int)sizeof(prefix))
return NULL;
va_list copy;
va_copy(copy, args);
int body_len = vsnprintf(NULL, 0, format, copy);
va_end(copy);
if (body_len < 0)
return NULL;
size_t total = (size_t)prefix_len + (size_t)body_len;
char* line = malloc(total + 2); /* body bytes + '\n' + NUL */
if (!line)
return NULL;
memcpy(line, prefix, (size_t)prefix_len);
vsnprintf(line + prefix_len, (size_t)body_len + 1, format, args);
line[total] = '\n';
line[total + 1] = '\0';
return line;
}
vfprintf(dest_io, format, args); /* Write an already-formatted line to the console and, if configured, the log
fprintf(dest_io, "\n"); * file. Only the log_fp pointer is read under the mutex (so log_set_file /
* config_delete cannot free it while it is in use); the single console fputs
* runs unlocked but is internally atomic per stdio stream. */
static void emit_log_line(FILE* console, const char* line) {
fputs(line, console);
call_once(&log_mutex_once, log_mutex_init);
mtx_lock(&log_mutex);
FILE* file = log_fp;
if (file)
fputs(line, file);
mtx_unlock(&log_mutex);
} }
void log_message(LogLevel log_level, const char* format, ...) { void log_message(LogLevel log_level, const char* format, ...) {
@@ -86,14 +138,12 @@ void log_message(LogLevel log_level, const char* format, ...) {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
write_message(dest_io, log_level, t, format, args); char* line = format_log_line(log_level, &t, format, args);
va_end(args); va_end(args);
if (!line)
if (log_fp) { return;
va_start(args, format); emit_log_line(dest_io, line);
write_message(log_fp, log_level, t, format, args); free(line);
va_end(args);
}
} }
void log_debug_message(LogDebugFlag flag, const char* format, ...) { void log_debug_message(LogDebugFlag flag, const char* format, ...) {
@@ -107,14 +157,12 @@ void log_debug_message(LogDebugFlag flag, const char* format, ...) {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
write_message(stdout, LOG_LEVEL_DEBUG, t, format, args); char* line = format_log_line(LOG_LEVEL_DEBUG, &t, format, args);
va_end(args); va_end(args);
if (!line)
if (log_fp) { return;
va_start(args, format); emit_log_line(stdout, line);
write_message(log_fp, LOG_LEVEL_DEBUG, t, format, args); free(line);
va_end(args);
}
} }
void log_info_message(LogInfoFlag flag, const char* format, ...) { void log_info_message(LogInfoFlag flag, const char* format, ...) {
@@ -129,14 +177,12 @@ void log_info_message(LogInfoFlag flag, const char* format, ...) {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
write_message(stdout, LOG_LEVEL_INFO, t, format, args); char* line = format_log_line(LOG_LEVEL_INFO, &t, format, args);
va_end(args); va_end(args);
if (!line)
if (log_fp) { return;
va_start(args, format); emit_log_line(stdout, line);
write_message(log_fp, LOG_LEVEL_INFO, t, format, args); free(line);
va_end(args);
}
} }
void log_perror(const char* context) { void log_perror(const char* context) {
+51 -153
View File
@@ -90,63 +90,65 @@ void metadata_to_buf(char** buf, const FileMetadata* m) {
*buf += sizeof(crtime_nsec); *buf += sizeof(crtime_nsec);
} }
FileMetadata* metadata_from_buf(char** buf) { FileMetadata* metadata_from_buf(const uint8_t* buf, size_t len) {
if (buf == NULL || len < sizeof(int32_t))
return NULL;
int32_t present; int32_t present;
memcpy(&present, *buf, sizeof(present)); memcpy(&present, buf, sizeof(present));
*buf += sizeof(present); if (present != 1)
if (present != 0 && present != 1)
return NULL; return NULL;
if (!present) if (len < sizeof(int32_t) + FILE_METADATA_WIRE_SIZE)
return NULL; return NULL;
const uint8_t* cursor = buf + sizeof(int32_t);
FileMetadata* m = protocol_alloc(sizeof(FileMetadata)); FileMetadata* m = protocol_alloc(sizeof(FileMetadata));
if (m == NULL) if (m == NULL)
return NULL; return NULL;
int32_t mode; int32_t mode;
memcpy(&mode, *buf, sizeof(mode)); memcpy(&mode, cursor, sizeof(mode));
*buf += sizeof(mode); cursor += sizeof(mode);
m->mode = (mode_t)mode; m->mode = (mode_t)mode;
int32_t uid; int32_t uid;
memcpy(&uid, *buf, sizeof(uid)); memcpy(&uid, cursor, sizeof(uid));
*buf += sizeof(uid); cursor += sizeof(uid);
m->uid = (uid_t)uid; m->uid = (uid_t)uid;
int32_t gid; int32_t gid;
memcpy(&gid, *buf, sizeof(gid)); memcpy(&gid, cursor, sizeof(gid));
*buf += sizeof(gid); cursor += sizeof(gid);
m->gid = (gid_t)gid; m->gid = (gid_t)gid;
int64_t mtime_sec; int64_t mtime_sec;
memcpy(&mtime_sec, *buf, sizeof(mtime_sec)); memcpy(&mtime_sec, cursor, sizeof(mtime_sec));
*buf += sizeof(mtime_sec); cursor += sizeof(mtime_sec);
m->mtime_sec = (time_t)mtime_sec; m->mtime_sec = (time_t)mtime_sec;
int64_t mtime_nsec; int64_t mtime_nsec;
memcpy(&mtime_nsec, *buf, sizeof(mtime_nsec)); memcpy(&mtime_nsec, cursor, sizeof(mtime_nsec));
*buf += sizeof(mtime_nsec); cursor += sizeof(mtime_nsec);
m->mtime_nsec = (long)mtime_nsec; m->mtime_nsec = (long)mtime_nsec;
int32_t atime_valid; int32_t atime_valid;
memcpy(&atime_valid, *buf, sizeof(atime_valid)); memcpy(&atime_valid, cursor, sizeof(atime_valid));
*buf += sizeof(atime_valid); cursor += sizeof(atime_valid);
int64_t atime_sec; int64_t atime_sec;
memcpy(&atime_sec, *buf, sizeof(atime_sec)); memcpy(&atime_sec, cursor, sizeof(atime_sec));
*buf += sizeof(atime_sec); cursor += sizeof(atime_sec);
int64_t atime_nsec; int64_t atime_nsec;
memcpy(&atime_nsec, *buf, sizeof(atime_nsec)); memcpy(&atime_nsec, cursor, sizeof(atime_nsec));
*buf += sizeof(atime_nsec); cursor += sizeof(atime_nsec);
int32_t crtime_valid; int32_t crtime_valid;
memcpy(&crtime_valid, *buf, sizeof(crtime_valid)); memcpy(&crtime_valid, cursor, sizeof(crtime_valid));
*buf += sizeof(crtime_valid); cursor += sizeof(crtime_valid);
int64_t crtime_sec; int64_t crtime_sec;
memcpy(&crtime_sec, *buf, sizeof(crtime_sec)); memcpy(&crtime_sec, cursor, sizeof(crtime_sec));
*buf += sizeof(crtime_sec); cursor += sizeof(crtime_sec);
int64_t crtime_nsec; int64_t crtime_nsec;
memcpy(&crtime_nsec, *buf, sizeof(crtime_nsec)); memcpy(&crtime_nsec, cursor, sizeof(crtime_nsec));
*buf += sizeof(crtime_nsec); cursor += sizeof(crtime_nsec);
m->atime_valid = atime_valid != 0; m->atime_valid = atime_valid != 0;
m->atime_sec = (time_t)atime_sec; m->atime_sec = (time_t)atime_sec;
m->atime_nsec = (long)atime_nsec; m->atime_nsec = (long)atime_nsec;
m->crtime_valid = crtime_valid != 0; m->crtime_valid = crtime_valid != 0;
m->crtime_sec = (time_t)crtime_sec; m->crtime_sec = (time_t)crtime_sec;
m->crtime_nsec = (long)crtime_nsec; m->crtime_nsec = (long)crtime_nsec;
if (present != 1 || mtime_nsec < 0 || mtime_nsec >= 1000000000LL || mode < 0 || uid < 0 || if (mtime_nsec < 0 || mtime_nsec >= 1000000000LL || mode < 0 || uid < 0 || gid < 0 ||
gid < 0 || atime_valid < 0 || atime_valid > 1 || crtime_valid < 0 || crtime_valid > 1 || atime_valid < 0 || atime_valid > 1 || crtime_valid < 0 || crtime_valid > 1 ||
(atime_valid && (atime_nsec < 0 || atime_nsec >= 1000000000LL)) || (atime_valid && (atime_nsec < 0 || atime_nsec >= 1000000000LL)) ||
(crtime_valid && (crtime_nsec < 0 || crtime_nsec >= 1000000000LL))) { (crtime_valid && (crtime_nsec < 0 || crtime_nsec >= 1000000000LL))) {
free(m); free(m);
@@ -157,33 +159,17 @@ FileMetadata* metadata_from_buf(char** buf) {
bool metadata_send(int file_descriptor, const FileMetadata* m) { bool metadata_send(int file_descriptor, const FileMetadata* m) {
if (m == NULL) { if (m == NULL) {
int32_t zero = 0; int32_t absent = 0;
return send_n_data(file_descriptor, &zero, sizeof(zero)); return send_n_data(file_descriptor, &absent, sizeof(absent));
} }
int32_t present = 1; /* One packed frame (protocol 2.20.0): the int32 present flag followed by the
int32_t mode = (int32_t)m->mode; fixed FILE_METADATA_WIRE_SIZE-byte field record. metadata_to_buf() emits
int32_t uid = (int32_t)m->uid; exactly that layout (present + fields), so build it once and write the
int32_t gid = (int32_t)m->gid; whole record in a single call instead of one frame per field. */
int64_t mtime_sec = (int64_t)m->mtime_sec; char packed[sizeof(int32_t) + FILE_METADATA_WIRE_SIZE];
int64_t mtime_nsec = (int64_t)m->mtime_nsec; char* cursor = packed;
int32_t atime_valid = m->atime_valid ? 1 : 0; metadata_to_buf(&cursor, m);
int64_t atime_sec = (int64_t)m->atime_sec; return send_n_data(file_descriptor, packed, sizeof(packed));
int64_t atime_nsec = (int64_t)m->atime_nsec;
int32_t crtime_valid = m->crtime_valid ? 1 : 0;
int64_t crtime_sec = (int64_t)m->crtime_sec;
int64_t crtime_nsec = (int64_t)m->crtime_nsec;
return send_n_data(file_descriptor, &present, sizeof(present)) &&
send_n_data(file_descriptor, &mode, sizeof(mode)) &&
send_n_data(file_descriptor, &uid, sizeof(uid)) &&
send_n_data(file_descriptor, &gid, sizeof(gid)) &&
send_n_data(file_descriptor, &mtime_sec, sizeof(mtime_sec)) &&
send_n_data(file_descriptor, &mtime_nsec, sizeof(mtime_nsec)) &&
send_n_data(file_descriptor, &atime_valid, sizeof(atime_valid)) &&
send_n_data(file_descriptor, &atime_sec, sizeof(atime_sec)) &&
send_n_data(file_descriptor, &atime_nsec, sizeof(atime_nsec)) &&
send_n_data(file_descriptor, &crtime_valid, sizeof(crtime_valid)) &&
send_n_data(file_descriptor, &crtime_sec, sizeof(crtime_sec)) &&
send_n_data(file_descriptor, &crtime_nsec, sizeof(crtime_nsec));
} }
FileMetadata* metadata_receive(int file_descriptor, int* ok) { FileMetadata* metadata_receive(int file_descriptor, int* ok) {
@@ -203,109 +189,21 @@ FileMetadata* metadata_receive(int file_descriptor, int* ok) {
*ok = 0; *ok = 0;
return NULL; return NULL;
} }
FileMetadata* m = protocol_alloc(sizeof(FileMetadata)); /* Rebuild the packed record metadata_from_buf() expects: the present flag we
just read, followed by exactly FILE_METADATA_WIRE_SIZE field bytes. */
char packed[sizeof(int32_t) + FILE_METADATA_WIRE_SIZE];
memcpy(packed, &present, sizeof(present));
if (!receive_n_data(file_descriptor, packed + sizeof(present), FILE_METADATA_WIRE_SIZE)) {
if (ok)
*ok = 0;
return NULL;
}
FileMetadata* m = metadata_from_buf((const uint8_t*)packed, sizeof(packed));
if (m == NULL) { if (m == NULL) {
if (ok) if (ok)
*ok = 0; *ok = 0;
return NULL; return NULL;
} }
int32_t mode;
if (!receive_n_data(file_descriptor, &mode, sizeof(mode))) {
free(m);
if (ok)
*ok = 0;
return NULL;
}
m->mode = (mode_t)mode;
int32_t uid;
if (!receive_n_data(file_descriptor, &uid, sizeof(uid))) {
free(m);
if (ok)
*ok = 0;
return NULL;
}
m->uid = (uid_t)uid;
int32_t gid;
if (!receive_n_data(file_descriptor, &gid, sizeof(gid))) {
free(m);
if (ok)
*ok = 0;
return NULL;
}
m->gid = (gid_t)gid;
int64_t mtime_sec;
if (!receive_n_data(file_descriptor, &mtime_sec, sizeof(mtime_sec))) {
free(m);
if (ok)
*ok = 0;
return NULL;
}
m->mtime_sec = (time_t)mtime_sec;
int64_t mtime_nsec;
if (!receive_n_data(file_descriptor, &mtime_nsec, sizeof(mtime_nsec))) {
free(m);
if (ok)
*ok = 0;
return NULL;
}
m->mtime_nsec = (long)mtime_nsec;
int32_t atime_valid;
if (!receive_n_data(file_descriptor, &atime_valid, sizeof(atime_valid))) {
free(m);
if (ok)
*ok = 0;
return NULL;
}
int64_t atime_sec;
if (!receive_n_data(file_descriptor, &atime_sec, sizeof(atime_sec))) {
free(m);
if (ok)
*ok = 0;
return NULL;
}
int64_t atime_nsec;
if (!receive_n_data(file_descriptor, &atime_nsec, sizeof(atime_nsec))) {
free(m);
if (ok)
*ok = 0;
return NULL;
}
int32_t crtime_valid;
if (!receive_n_data(file_descriptor, &crtime_valid, sizeof(crtime_valid))) {
free(m);
if (ok)
*ok = 0;
return NULL;
}
int64_t crtime_sec;
if (!receive_n_data(file_descriptor, &crtime_sec, sizeof(crtime_sec))) {
free(m);
if (ok)
*ok = 0;
return NULL;
}
int64_t crtime_nsec;
if (!receive_n_data(file_descriptor, &crtime_nsec, sizeof(crtime_nsec))) {
free(m);
if (ok)
*ok = 0;
return NULL;
}
m->atime_valid = atime_valid != 0;
m->atime_sec = (time_t)atime_sec;
m->atime_nsec = (long)atime_nsec;
m->crtime_valid = crtime_valid != 0;
m->crtime_sec = (time_t)crtime_sec;
m->crtime_nsec = (long)crtime_nsec;
if (mtime_nsec < 0 || mtime_nsec >= 1000000000LL || mode < 0 || uid < 0 || gid < 0 ||
atime_valid < 0 || atime_valid > 1 || crtime_valid < 0 || crtime_valid > 1 ||
(atime_valid && (atime_nsec < 0 || atime_nsec >= 1000000000LL)) ||
(crtime_valid && (crtime_nsec < 0 || crtime_nsec >= 1000000000LL))) {
free(m);
if (ok)
*ok = 0;
return NULL;
}
if (ok) if (ok)
*ok = 1; *ok = 1;
return m; return m;
+15 -2
View File
@@ -3,6 +3,7 @@
#include "file.h" #include "file.h"
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <time.h> #include <time.h>
@@ -29,11 +30,23 @@
/* Size of metadata fields on wire, excluding the int32_t `present` field that /* Size of metadata fields on wire, excluding the int32_t `present` field that
* is always sent first. The total wire size for present metadata is * is always sent first. The total wire size for present metadata is
* sizeof(int32_t) + FILE_METADATA_WIRE_SIZE (68 bytes on most platforms). */ * sizeof(int32_t) + FILE_METADATA_WIRE_SIZE (68 bytes on most platforms).
*
* metadata_send()/metadata_receive() (protocol 2.20.0) frame the metadata as a
* single packed record: one int32 present flag (0 = absent) followed, when
* present, by exactly FILE_METADATA_WIRE_SIZE bytes of field data. This is the
* same present+fields byte layout metadata_to_buf()/metadata_from_buf() use, so
* the wire metadata is now one frame instead of one frame per field. */
#define FILE_METADATA_WIRE_SIZE (sizeof(int32_t) * 5 + sizeof(int64_t) * 6) #define FILE_METADATA_WIRE_SIZE (sizeof(int32_t) * 5 + sizeof(int64_t) * 6)
void metadata_to_buf(char** buf, const FileMetadata* m); void metadata_to_buf(char** buf, const FileMetadata* m);
FileMetadata* metadata_from_buf(char** buf); /* Decode one packed metadata record (an int32 present flag followed, when
* present, by FILE_METADATA_WIRE_SIZE field bytes) from `buf`, which has `len`
* readable bytes. Every read is bounds-checked against `len`, so the function
* can never over-read the caller's buffer: a too-short record, an absent
* (present == 0) record and a malformed record all return NULL. A successful
* decode returns a heap-allocated FileMetadata owned by the caller. */
FileMetadata* metadata_from_buf(const uint8_t* buf, size_t len);
bool metadata_send(int file_descriptor, const FileMetadata* m); bool metadata_send(int file_descriptor, const FileMetadata* m);
FileMetadata* metadata_receive(int file_descriptor, int* ok); FileMetadata* metadata_receive(int file_descriptor, int* ok);
void file_restore_metadata(const char* path, const FileMetadata* metadata, void file_restore_metadata(const char* path, const FileMetadata* metadata,
+85 -247
View File
@@ -1,15 +1,16 @@
#include "multiprocessing.h" #include "multiprocessing.h"
#include "receiver.h"
#include "array_list.h" #include "array_list.h"
#include "chunk.h" #include "chunk.h"
#include "config.h" #include "config.h"
#include "data.h" #include "data.h"
#include "file.h" #include "file.h"
#include "file_receive.h"
#include "log.h" #include "log.h"
#include "protocol.h" #include "protocol.h"
#include "queue.h" #include "queue.h"
#include "utils.h" #include "utils.h"
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -25,6 +26,8 @@ PipelineContextSender* pipeline_context_sender_create(Config* config, Queue* que
context->queue_loader = queue_loader; context->queue_loader = queue_loader;
context->scanner_done = false; context->scanner_done = false;
context->loader_done = false; context->loader_done = false;
context->queued_bytes = 0;
context->max_queue_bytes = 0;
context->manifest = NULL; context->manifest = NULL;
context->excluded_paths = NULL; context->excluded_paths = NULL;
context->missing_args = NULL; context->missing_args = NULL;
@@ -96,7 +99,88 @@ fail:
return NULL; return NULL;
} }
void pipeline_context_sender_set_queue_byte_limit(PipelineContextSender* context,
size_t max_bytes) {
if (context == NULL)
return;
mtx_lock(&context->mutex_loader);
context->max_queue_bytes = max_bytes;
context->queued_bytes = 0;
cnd_broadcast(&context->condition_not_full_loader);
mtx_unlock(&context->mutex_loader);
}
size_t pipeline_context_sender_chunk_bytes(const Chunk* chunk) {
if (chunk == NULL || chunk->items == NULL)
return 0;
size_t total = 0;
for (int i = 0; i < chunk->element_count; i++) {
const File* file = chunk->items[i];
if (file == NULL || file->data == NULL || file->data->data == NULL)
continue;
if (file->data->size > SIZE_MAX - total)
return SIZE_MAX;
total += file->data->size;
}
return total;
}
void pipeline_context_sender_note_bytes_released(PipelineContextSender* context,
size_t released_bytes) {
if (context == NULL || context->max_queue_bytes == 0 || released_bytes == 0)
return;
mtx_lock(&context->mutex_loader);
if (released_bytes >= context->queued_bytes)
context->queued_bytes = 0;
else
context->queued_bytes -= released_bytes;
cnd_signal(&context->condition_not_full_loader);
mtx_unlock(&context->mutex_loader);
}
bool pipeline_context_sender_enqueue_chunk(PipelineContextSender* context, Chunk* chunk) {
if (context == NULL || chunk == NULL)
return false;
size_t chunk_bytes = pipeline_context_sender_chunk_bytes(chunk);
mtx_lock(&context->mutex_loader);
while (!atomic_load(&context->cancelled)) {
bool blocked_by_count = queue_is_full(context->queue_loader);
bool blocked_by_budget = false;
if (context->max_queue_bytes > 0) {
size_t budget = context->max_queue_bytes;
size_t used = context->queued_bytes;
if (used >= budget) {
blocked_by_budget = true;
} else if (chunk_bytes > budget - used) {
/* A single payload larger than the whole budget is only admitted to an
empty pipeline so the wait can never deadlock. */
blocked_by_budget = used != 0;
}
}
if (!blocked_by_count && !blocked_by_budget)
break;
cnd_wait(&context->condition_not_full_loader, &context->mutex_loader);
}
if (atomic_load(&context->cancelled)) {
mtx_unlock(&context->mutex_loader);
chunk_destroy(chunk);
return false;
}
if (!queue_enqueue(context->queue_loader, chunk)) {
mtx_unlock(&context->mutex_loader);
chunk_destroy(chunk);
return false;
}
context->queued_bytes += chunk_bytes;
cnd_signal(&context->condition_not_empty_loader);
mtx_unlock(&context->mutex_loader);
return true;
}
void pipeline_context_sender_destroy(PipelineContextSender* context) { void pipeline_context_sender_destroy(PipelineContextSender* context) {
/* `config` is borrowed: the caller retains ownership and frees it after the
pipeline has been destroyed (the worker threads are already joined, so no
config access can outlive this call). */
if (context->manifest) { if (context->manifest) {
array_list_delete(context->manifest); array_list_delete(context->manifest);
} }
@@ -110,7 +194,6 @@ void pipeline_context_sender_destroy(PipelineContextSender* context) {
array_list_delete(context->dir_entries); array_list_delete(context->dir_entries);
if (context->dir_entries_mutex_init) if (context->dir_entries_mutex_init)
mtx_destroy(&context->dir_entries_mutex); mtx_destroy(&context->dir_entries_mutex);
config_delete(context->config);
queue_destroy(context->queue_scanner); queue_destroy(context->queue_scanner);
queue_destroy(context->queue_loader); queue_destroy(context->queue_loader);
mtx_destroy(&context->mutex_scanner); mtx_destroy(&context->mutex_scanner);
@@ -122,248 +205,3 @@ void pipeline_context_sender_destroy(PipelineContextSender* context) {
mtx_destroy(&context->mutex_progress); mtx_destroy(&context->mutex_progress);
free(context); free(context);
} }
PipelineContextReceiver* pipeline_context_receiver_create(Config* config, Queue* queue,
int file_descriptor, SSL* ssl) {
PipelineContextReceiver* context = malloc(sizeof(PipelineContextReceiver));
if (context == NULL)
return NULL;
context->config = config;
context->queue = queue;
context->file_descriptor = file_descriptor;
context->ssl = ssl;
context->outcomes.entries = NULL;
context->outcomes.count = 0;
context->outcomes.capacity = 0;
dir_time_list_init(&context->dir_times);
protocol_session_init(&context->session, file_descriptor, file_descriptor);
protocol_session_set_ssl(&context->session, ssl);
context->receiver_done = false;
context->queued_bytes = 0;
context->max_queue_bytes = 0;
context->deferred_manifest = NULL;
atomic_init(&context->cancelled, false);
int init = 0;
if (mtx_init(&context->mutex, mtx_plain) != thrd_success)
goto fail;
init++;
if (cnd_init(&context->condition_not_full) != thrd_success)
goto fail;
init++;
if (cnd_init(&context->condition_not_empty) != thrd_success)
goto fail;
// cppcheck-suppress unreadVariable
init++;
return context;
fail:
log_perror("Error initializing synchronization objects");
if (init >= 3)
cnd_destroy(&context->condition_not_empty);
if (init >= 2)
cnd_destroy(&context->condition_not_full);
if (init >= 1)
mtx_destroy(&context->mutex);
free(context);
return NULL;
}
void pipeline_context_receiver_destroy(PipelineContextReceiver* context) {
config_delete(context->config);
if (context->deferred_manifest)
delete_manifest_free(context->deferred_manifest);
queue_destroy(context->queue);
receiver_outcomes_destroy(&context->outcomes);
dir_time_list_free(&context->dir_times);
mtx_destroy(&context->mutex);
cnd_destroy(&context->condition_not_full);
cnd_destroy(&context->condition_not_empty);
free(context);
}
void pipeline_context_receiver_set_queue_byte_limit(PipelineContextReceiver* context,
size_t max_bytes) {
if (context == NULL)
return;
mtx_lock(&context->mutex);
context->max_queue_bytes = max_bytes;
context->queued_bytes = 0;
cnd_broadcast(&context->condition_not_full);
mtx_unlock(&context->mutex);
}
void pipeline_context_receiver_note_bytes_released(PipelineContextReceiver* context,
size_t released_bytes) {
if (context == NULL || context->max_queue_bytes == 0 || released_bytes == 0)
return;
mtx_lock(&context->mutex);
if (released_bytes >= context->queued_bytes)
context->queued_bytes = 0;
else
context->queued_bytes -= released_bytes;
cnd_signal(&context->condition_not_full);
mtx_unlock(&context->mutex);
}
bool pipeline_context_receiver_enqueue_file(PipelineContextReceiver* context, File* file) {
if (context == NULL || file == NULL)
return false;
size_t file_bytes = file->data ? file->data->size : 0;
mtx_lock(&context->mutex);
while (!atomic_load(&context->cancelled)) {
bool blocked_by_count = queue_is_full(context->queue);
bool blocked_by_budget = false;
if (context->max_queue_bytes > 0) {
size_t budget = context->max_queue_bytes;
size_t used = context->queued_bytes;
if (used >= budget) {
blocked_by_budget = true;
} else if (file_bytes > budget - used) {
/* A single payload larger than the whole budget (not possible with
the per-file receive cap) is only admitted to an empty pipeline so
the wait can never deadlock. */
blocked_by_budget = used != 0;
}
}
if (!blocked_by_count && !blocked_by_budget)
break;
cnd_wait(&context->condition_not_full, &context->mutex);
}
if (atomic_load(&context->cancelled)) {
mtx_unlock(&context->mutex);
file_destroy(file);
return false;
}
if (!queue_enqueue(context->queue, file)) {
mtx_unlock(&context->mutex);
file_destroy(file);
return false;
}
context->queued_bytes += file_bytes;
cnd_signal(&context->condition_not_empty);
mtx_unlock(&context->mutex);
return true;
}
static bool receiver_enqueue_file(File* file, void* context_pointer) {
PipelineContextReceiver* context = (PipelineContextReceiver*)context_pointer;
return pipeline_context_receiver_enqueue_file(context, file);
}
static void receiver_thread_fail(PipelineContextReceiver* context) {
mtx_lock(&context->mutex);
atomic_store(&context->cancelled, true);
context->receiver_done = true;
cnd_broadcast(&context->condition_not_empty);
cnd_broadcast(&context->condition_not_full);
mtx_unlock(&context->mutex);
}
int receive_thread(void* pipeline_context) {
PipelineContextReceiver* context = (PipelineContextReceiver*)pipeline_context;
protocol_session_bind(&context->session);
mtx_lock(&context->mutex);
int file_descriptor = context->file_descriptor;
const Config* config = context->config;
mtx_unlock(&context->mutex);
ReceiverSink sink = {receiver_enqueue_file, context, false, false, NULL};
if (receiver_process_pending((Config*)config, file_descriptor, &sink,
&context->deferred_manifest) != 0) {
receiver_thread_fail(context);
protocol_session_unbind();
return thrd_error;
}
mtx_lock(&context->mutex);
context->receiver_done = true;
cnd_signal(&context->condition_not_empty);
mtx_unlock(&context->mutex);
protocol_session_unbind();
return thrd_success;
}
int write_thread(void* pipeline_context) {
PipelineContextReceiver* context = (PipelineContextReceiver*)pipeline_context;
protocol_session_bind(&context->session);
mtx_lock(&context->mutex);
bool save_to_disk = context->config->save_to_disk;
char* root_directory = str_dup(context->config->receive_root_directory);
mtx_unlock(&context->mutex);
if (save_to_disk && !root_directory) {
mtx_lock(&context->mutex);
atomic_store(&context->cancelled, true);
context->receiver_done = true;
cnd_broadcast(&context->condition_not_full);
cnd_broadcast(&context->condition_not_empty);
mtx_unlock(&context->mutex);
protocol_session_unbind();
return thrd_error;
}
while (true) {
File* file =
queue_dequeue_multithreaded(context->queue, &context->mutex, &context->condition_not_empty,
&context->condition_not_full, &context->receiver_done);
if (file == NULL) {
free(root_directory);
protocol_session_unbind();
return thrd_success;
}
size_t file_bytes = file->data ? file->data->size : 0;
FileSaveResult result = FILE_SAVE_SKIPPED;
if (save_to_disk) {
result = file_save_to_disk_full(root_directory, file, context->config);
if (result == FILE_SAVE_ERROR) {
file_destroy(file);
pipeline_context_receiver_note_bytes_released(context, file_bytes);
mtx_lock(&context->mutex);
atomic_store(&context->cancelled, true);
context->receiver_done = true;
cnd_broadcast(&context->condition_not_full);
cnd_broadcast(&context->condition_not_empty);
mtx_unlock(&context->mutex);
free(root_directory);
protocol_session_unbind();
return thrd_error;
}
}
/* P7 Wave D: a directory's times are never applied inline (a later child
write would clobber them); accumulate the metadata here and let the
caller apply it once every writer has drained. */
if (result != FILE_SAVE_ERROR && file->is_dir && file->metadata &&
context->config->use_metadata && !context->config->omit_dir_times &&
!dir_time_list_add(&context->dir_times, file->path, file->metadata)) {
file_destroy(file);
pipeline_context_receiver_note_bytes_released(context, file_bytes);
mtx_lock(&context->mutex);
atomic_store(&context->cancelled, true);
context->receiver_done = true;
cnd_broadcast(&context->condition_not_full);
cnd_broadcast(&context->condition_not_empty);
mtx_unlock(&context->mutex);
free(root_directory);
protocol_session_unbind();
return thrd_error;
}
/* Record the per-file outcome so a --remove-source-files sender learns
which sources were actually written versus skipped on the receiver.
Explicit directory entries and recreated device/special nodes have no
source and are never acknowledged (mirrors receiver.c). */
if (context->config->remove_source_files && !file->is_dir && !file->is_special && !file->skip &&
!receiver_outcomes_append(&context->outcomes, (unsigned char)result)) {
file_destroy(file);
pipeline_context_receiver_note_bytes_released(context, file_bytes);
mtx_lock(&context->mutex);
atomic_store(&context->cancelled, true);
context->receiver_done = true;
cnd_broadcast(&context->condition_not_full);
cnd_broadcast(&context->condition_not_empty);
mtx_unlock(&context->mutex);
free(root_directory);
protocol_session_unbind();
return thrd_error;
}
file_destroy(file);
pipeline_context_receiver_note_bytes_released(context, file_bytes);
}
}
+28 -52
View File
@@ -5,11 +5,11 @@
#include <stdatomic.h> #include <stdatomic.h>
#include "array_list.h" #include "array_list.h"
#include "chunk.h"
#include "config.h" #include "config.h"
#include "file.h" #include "file.h"
#include "protocol.h" #include "protocol.h"
#include "queue.h" #include "queue.h"
#include "receiver.h"
#include "stop_condition.h" #include "stop_condition.h"
#include <openssl/ssl.h> #include <openssl/ssl.h>
@@ -25,6 +25,15 @@ typedef struct {
cnd_t condition_not_full_loader; cnd_t condition_not_full_loader;
cnd_t condition_not_empty_loader; cnd_t condition_not_empty_loader;
bool loader_done; bool loader_done;
/* Aggregate loaded payload bytes queued on queue_loader but not yet released
by the sender. Guarded by `mutex_loader`. When `max_queue_bytes` is
non-zero the loader blocks before enqueueing a chunk that would push this
total over it, so the sender buffers a bounded number of bytes rather than
an unbounded count of chunks that may each be up to chunk_size (or a single
file) in size. Files streamed straight from disk by sendfile hold no
payload, so only in-memory (`data->data`) payloads are counted. */
size_t queued_bytes;
size_t max_queue_bytes;
ArrayList* manifest; ArrayList* manifest;
/* Protected prefixes (paths the source scan excluded by user rules) sent /* Protected prefixes (paths the source scan excluded by user rules) sent
with the keep-set manifest so --delete leaves them alone unless with the keep-set manifest so --delete leaves them alone unless
@@ -76,58 +85,25 @@ typedef struct {
bool dir_entries_mutex_init; bool dir_entries_mutex_init;
} PipelineContextSender; } PipelineContextSender;
typedef struct PipelineContextReceiver { /* `config` is borrowed and must outlive the context: destroy does NOT free it,
Queue* queue; so the caller owns it and frees it with config_delete() afterwards. */
Config* config;
int file_descriptor;
SSL* ssl;
ProtocolSession session;
ReceiverOutcomes outcomes;
mtx_t mutex;
cnd_t condition_not_full;
cnd_t condition_not_empty;
bool receiver_done;
atomic_bool cancelled;
/* Aggregate payload bytes that have been received but not yet released by
the disk writer (queued or in the writer's hand). Guarded by `mutex`.
When `max_queue_bytes` is non-zero the receiver blocks before enqueuing
once this total would exceed it, so decompressed/copied file payloads
buffered ahead of a slow disk writer respect the per-connection memory
budget instead of growing without bound. */
size_t queued_bytes;
size_t max_queue_bytes;
/* Keep-set manifest for the commit-style (late) deletion
(--delete/--delete-after/--delete-delay). receive_thread parses the whole
protocol stream but hands the manifest here instead of deleting while the
disk writer may still be draining; the caller (server.c) commits the
deletion after both threads have joined, so no extra is removed unless the
transfer truly succeeded. NULL in the early delete modes (which delete at
the manifest). */
DeleteManifest* deferred_manifest;
/* P7 Wave D: directory metadata collected by write_thread from received
directory entries. Only write_thread mutates it (before it joins); the
caller (server.c) applies it after the delete/delay-updates phase. */
DirTimeList dir_times;
} PipelineContextReceiver;
PipelineContextSender* pipeline_context_sender_create(Config* config, Queue* queue_scanner, PipelineContextSender* pipeline_context_sender_create(Config* config, Queue* queue_scanner,
Queue* queue_loader); Queue* queue_loader);
void pipeline_context_sender_destroy(PipelineContextSender* context); void pipeline_context_sender_destroy(PipelineContextSender* context);
PipelineContextReceiver* pipeline_context_receiver_create(Config* config, Queue* queue_receiver, /* Bound the loaded payload bytes the sender may buffer ahead of the network
int file_descriptor, SSL* ssl); writer (see max_queue_bytes). */
void pipeline_context_receiver_destroy(PipelineContextReceiver* context); void pipeline_context_sender_set_queue_byte_limit(PipelineContextSender* context, size_t max_bytes);
/* Bound the bytes buffered ahead of the disk writer (see max_queue_bytes). */ /* Total payload bytes a chunk currently holds in memory (loaded file data
void pipeline_context_receiver_set_queue_byte_limit(PipelineContextReceiver* context, only; zero for entries with no payload or data streamed from disk). */
size_t max_bytes); size_t pipeline_context_sender_chunk_bytes(const Chunk* chunk);
/* Blocking enqueue used by the receive pipeline sink. Blocks while the queue /* Blocking enqueue used by the sender's loader stage. Blocks while
is full by element count or when adding `file` would push queued_bytes over queue_loader is full by element count or when adding `chunk` would push the
the configured byte limit; waits until the disk writer releases bytes. queued payload bytes over the configured byte limit; waits until the sender
Takes ownership of `file` on success and destroys it on failure/cancel. */ releases bytes. Takes ownership of `chunk` on success and destroys it on
bool pipeline_context_receiver_enqueue_file(PipelineContextReceiver* context, File* file); failure/cancel. */
/* Account for `released_bytes` of payload memory that has been freed by the bool pipeline_context_sender_enqueue_chunk(PipelineContextSender* context, Chunk* chunk);
disk writer, unblocking a receiver that is waiting on the byte limit. */ /* Account for `released_bytes` of payload memory that the sender freed after
void pipeline_context_receiver_note_bytes_released(PipelineContextReceiver* context, destroying a chunk, unblocking a loader waiting on the byte limit. */
size_t released_bytes); void pipeline_context_sender_note_bytes_released(PipelineContextSender* context,
int receive_thread(void* pipeline_context); size_t released_bytes);
int write_thread(void* pipeline_context);
#endif #endif
+175 -6
View File
@@ -76,10 +76,23 @@ void protocol_session_init(ProtocolSession* session, int read_fd, int write_fd)
session->read_fd = read_fd; session->read_fd = read_fd;
session->write_fd = write_fd; session->write_fd = write_fd;
session->max_alloc = DEFAULT_MAX_ALLOC; session->max_alloc = DEFAULT_MAX_ALLOC;
session->io_timeout_sec = RECEIVE_TIMEOUT_SEC;
atomic_init(&session->total_allocated_bytes, 0); atomic_init(&session->total_allocated_bytes, 0);
protocol_session_set_bwlimit(session, global_bwlimit()); protocol_session_set_bwlimit(session, global_bwlimit());
} }
void protocol_session_set_io_timeout(ProtocolSession* session, int sec) {
if (!session)
return;
session->io_timeout_sec = sec;
}
int protocol_get_io_timeout_sec(void) {
const ProtocolSession* session = bound_session ? bound_session : &legacy_io_session;
int sec = session->io_timeout_sec;
return sec > 0 ? sec : RECEIVE_TIMEOUT_SEC;
}
void protocol_session_set_max_alloc(ProtocolSession* session, unsigned long long max_alloc) { void protocol_session_set_max_alloc(ProtocolSession* session, unsigned long long max_alloc) {
if (!session) if (!session)
session = bound_session ? bound_session : &legacy_io_session; session = bound_session ? bound_session : &legacy_io_session;
@@ -257,10 +270,11 @@ bool protocol_send_n_data(ProtocolSession* session, const void* data, size_t dat
log_debug_message(LOG_DEBUG_IO, " Sending n Data: %zu", data_size); log_debug_message(LOG_DEBUG_IO, " Sending n Data: %zu", data_size);
if (!session) if (!session)
return false; return false;
int timeout_sec = session->io_timeout_sec > 0 ? session->io_timeout_sec : SEND_TIMEOUT_SEC;
int fd = session->write_fd; int fd = session->write_fd;
struct timespec deadline; struct timespec deadline;
clock_gettime(CLOCK_MONOTONIC, &deadline); clock_gettime(CLOCK_MONOTONIC, &deadline);
deadline.tv_sec += SEND_TIMEOUT_SEC; deadline.tv_sec += timeout_sec;
short wait_events = POLLOUT; short wait_events = POLLOUT;
ssize_t total_bytes_send = 0; ssize_t total_bytes_send = 0;
while ((size_t)total_bytes_send < data_size) { while ((size_t)total_bytes_send < data_size) {
@@ -289,6 +303,12 @@ bool protocol_send_n_data(ProtocolSession* session, const void* data, size_t dat
wait_events = ssl_err == SSL_ERROR_WANT_WRITE ? POLLOUT : POLLIN; wait_events = ssl_err == SSL_ERROR_WANT_WRITE ? POLLOUT : POLLIN;
continue; continue;
} }
/* A signal (e.g. Ctrl-C) interrupts the blocking TLS write: retry so
the send loop can observe the abort flag at the next checkpoint. */
if (ssl_err == SSL_ERROR_SYSCALL && errno == EINTR)
continue;
} else if (errno == EINTR) {
continue;
} }
log_message(LOG_LEVEL_ERROR, "Could not send data"); log_message(LOG_LEVEL_ERROR, "Could not send data");
return false; return false;
@@ -306,7 +326,10 @@ bool protocol_receive_n_data_timed(ProtocolSession* session, void* data, size_t
int timeout_sec); int timeout_sec);
bool protocol_receive_n_data(ProtocolSession* session, void* data, size_t data_size) { bool protocol_receive_n_data(ProtocolSession* session, void* data, size_t data_size) {
return protocol_receive_n_data_timed(session, data, data_size, RECEIVE_TIMEOUT_SEC); /* Honor the session's configured deadline; protocol_receive_n_data_timed
* re-applies the built-in 60 s default when the value is <= 0. */
int timeout_sec = session ? session->io_timeout_sec : 0;
return protocol_receive_n_data_timed(session, data, data_size, timeout_sec);
} }
bool protocol_receive_n_data_timed(ProtocolSession* session, void* data, size_t data_size, bool protocol_receive_n_data_timed(ProtocolSession* session, void* data, size_t data_size,
@@ -553,10 +576,6 @@ Data* protocol_receive_data_limited(ProtocolSession* session, unsigned long long
return result; return result;
} }
Data* protocol_receive_data(ProtocolSession* session) {
return protocol_receive_data_limited(session, MAX_DATA_PAYLOAD_SIZE);
}
bool protocol_send_int(ProtocolSession* session, int data) { bool protocol_send_int(ProtocolSession* session, int data) {
if (!protocol_send_n_data(session, &data, sizeof(int))) if (!protocol_send_n_data(session, &data, sizeof(int)))
return false; return false;
@@ -596,6 +615,151 @@ bool protocol_receive_status_timed(ProtocolSession* session, Status* status, int
return true; return true;
} }
/* Read exactly one Status frame within `deadline` (CLOCK_MONOTONIC). Unlike
* protocol_receive_status_keepalive this never emits a keepalive: it is used
* to consume the first byte(s) of an already-signalled frame and to drain the
* peer's outstanding keepalive replies, where injecting a write could split a
* reply across a frame boundary. Returns false on timeout/EOF/error. */
static bool protocol_read_status_until(ProtocolSession* session, Status* status,
const struct timespec* deadline) {
Status received = STATUS_ERROR;
size_t got = 0;
short wait_events = POLLIN;
while (got < sizeof(Status)) {
if (!session->ssl || SSL_pending(session->ssl) == 0) {
int remaining_ms = deadline_remaining_ms(deadline);
if (remaining_ms <= 0) {
log_message(LOG_LEVEL_ERROR, "Receive timeout while reading status");
return false;
}
struct pollfd pfd = {.fd = session->read_fd, .events = wait_events};
int poll_result = poll(&pfd, 1, remaining_ms);
if (poll_result == 0) {
log_message(LOG_LEVEL_ERROR, "Receive timeout while reading status");
return false;
}
if (poll_result < 0) {
if (errno == EINTR)
continue;
return false;
}
if (pfd.revents & (POLLERR | POLLNVAL))
return false;
}
ssize_t bytes_received;
if (session->ssl)
bytes_received = SSL_read(session->ssl, (char*)&received + got, sizeof(Status) - got);
else
bytes_received = read(session->read_fd, (char*)&received + got, sizeof(Status) - got);
if (bytes_received <= 0) {
if (session->ssl) {
int ssl_err = SSL_get_error(session->ssl, (int)bytes_received);
if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
wait_events = ssl_err == SSL_ERROR_WANT_WRITE ? POLLOUT : POLLIN;
continue;
}
}
if (bytes_received < 0 && errno == EINTR)
continue;
log_message(LOG_LEVEL_ERROR, "Connection closed while receiving status");
return false;
}
got += (size_t)bytes_received;
}
*status = received;
return true;
}
bool protocol_receive_status_keepalive(ProtocolSession* session, Status* status, int timeout_sec,
int keepalive_interval_sec, ProtocolWaitAbort abort_check) {
if (!session || !status)
return false;
if (timeout_sec <= 0)
timeout_sec = RECEIVE_TIMEOUT_SEC;
if (keepalive_interval_sec <= 0)
keepalive_interval_sec = timeout_sec;
struct timespec deadline;
clock_gettime(CLOCK_MONOTONIC, &deadline);
deadline.tv_sec += timeout_sec;
unsigned long keepalives_sent = 0;
unsigned long replies_seen = 0;
Status final = STATUS_ERROR;
while (true) {
if (abort_check && abort_check())
return false;
if (!session->ssl || SSL_pending(session->ssl) == 0) {
int remaining_ms = deadline_remaining_ms(&deadline);
if (remaining_ms <= 0) {
log_message(LOG_LEVEL_ERROR, "Receive timeout after %ds", timeout_sec);
return false;
}
/* Only interleave a keepalive while waiting for the FIRST byte of a
* frame; once part of a frame is buffered a write could race the peer's
* reply into the middle of it. */
long long interval_ms_ll = (long long)keepalive_interval_sec * 1000LL;
int interval_ms = interval_ms_ll > INT_MAX ? INT_MAX : (int)interval_ms_ll;
int wait_ms = interval_ms < remaining_ms ? interval_ms : remaining_ms;
struct pollfd pfd = {.fd = session->read_fd, .events = POLLIN};
int poll_result = poll(&pfd, 1, wait_ms);
if (poll_result == 0) {
if (abort_check && abort_check())
return false;
if (!protocol_send_status(session, STATUS_KEEPALIVE))
return false;
keepalives_sent++;
continue;
}
if (poll_result < 0) {
if (errno == EINTR)
continue;
return false;
}
if (pfd.revents & (POLLERR | POLLNVAL))
return false;
}
Status received;
if (!protocol_read_status_until(session, &received, &deadline))
return false;
if (received == STATUS_KEEPALIVE) {
/* The receiver's answer to one of our keepalives. */
replies_seen++;
continue;
}
final = received;
break;
}
/* Drain the replies the receiver still owes for keepalives we sent while it
* was busy. It answers them only after the real status, so leaving them
* unread would put stale KEEPALIVE frames ahead of the next exchange and
* desynchronize the protocol. */
if (replies_seen < keepalives_sent) {
/* A short separate grace, not the (possibly exhausted) main deadline: the
terminal status already arrived, so a peer that never answers its owed
keepalives must not turn a successful ack into a reported failure. */
struct timespec drain_deadline;
clock_gettime(CLOCK_MONOTONIC, &drain_deadline);
drain_deadline.tv_sec += 1;
while (replies_seen < keepalives_sent) {
Status drained;
if (!protocol_read_status_until(session, &drained, &drain_deadline)) {
log_message(LOG_LEVEL_WARNING, "peer did not answer %lu keepalive(s); continuing",
keepalives_sent - replies_seen);
break;
}
if (drained != STATUS_KEEPALIVE) {
log_message(LOG_LEVEL_ERROR, "Unexpected status while draining keepalive replies");
return false;
}
replies_seen++;
}
}
*status = final;
log_debug_message(LOG_DEBUG_PROTO, "Received Status: %s", status_to_string(*status));
return true;
}
bool send_str(int fd, const char* data) { bool send_str(int fd, const char* data) {
return protocol_send_str(legacy_session(-1, fd), data); return protocol_send_str(legacy_session(-1, fd), data);
} }
@@ -635,3 +799,8 @@ bool receive_status(int fd, Status* status) {
bool receive_status_timed(int fd, Status* status, int timeout_sec) { bool receive_status_timed(int fd, Status* status, int timeout_sec) {
return protocol_receive_status_timed(legacy_session(fd, -1), status, timeout_sec); return protocol_receive_status_timed(legacy_session(fd, -1), status, timeout_sec);
} }
bool receive_status_keepalive(int fd, Status* status, int timeout_sec, int keepalive_interval_sec,
ProtocolWaitAbort abort_check) {
return protocol_receive_status_keepalive(legacy_session(fd, -1), status, timeout_sec,
keepalive_interval_sec, abort_check);
}
+34 -1
View File
@@ -52,6 +52,12 @@ typedef struct ProtocolSession {
atomic_ullong total_allocated_bytes; atomic_ullong total_allocated_bytes;
bool eight_bit_output; bool eight_bit_output;
unsigned long long max_alloc; unsigned long long max_alloc;
/* Per-session deadline (seconds) applied to every protocol send/receive by
* protocol_send_n_data / protocol_receive_n_data. Defaults to the built-in
* 60 s window; a value <= 0 falls back to that default. Set from the
* negotiated Config->timeout so --timeout is honored by the poll()-driven
* protocol I/O, not just the socket SO_RCVTIMEO/SO_SNDTIMEO. */
int io_timeout_sec;
} ProtocolSession; } ProtocolSession;
typedef int Status; typedef int Status;
@@ -141,6 +147,15 @@ void protocol_session_unbind(void);
void protocol_session_set_ssl(ProtocolSession* session, SSL* ssl); void protocol_session_set_ssl(ProtocolSession* session, SSL* ssl);
void protocol_session_set_bwlimit(ProtocolSession* session, unsigned long long bytes_per_sec); void protocol_session_set_bwlimit(ProtocolSession* session, unsigned long long bytes_per_sec);
void protocol_session_set_max_alloc(ProtocolSession* session, unsigned long long max_alloc); void protocol_session_set_max_alloc(ProtocolSession* session, unsigned long long max_alloc);
/* Override the per-message send/receive deadline for this session.
* `sec` <= 0 restores the built-in 60 s default (used for --timeout=0/unset).
* An explicit long deadline (e.g. the delete-ack wait) is applied per-call by
* protocol_receive_status_timed and is unaffected by this setter. */
void protocol_session_set_io_timeout(ProtocolSession* session, int sec);
/* Effective per-message I/O deadline (seconds) for the currently-bound session,
* falling back to the built-in default. Used by the plaintext sendfile path
* which bypasses the protocol send primitive. */
int protocol_get_io_timeout_sec(void);
void* protocol_alloc(size_t size); void* protocol_alloc(size_t size);
void* protocol_realloc(void* ptr, size_t size); void* protocol_realloc(void* ptr, size_t size);
void protocol_session_set_8_bit_output(ProtocolSession* session, bool enabled); void protocol_session_set_8_bit_output(ProtocolSession* session, bool enabled);
@@ -157,7 +172,6 @@ char* protocol_receive_str(ProtocolSession* session);
bool protocol_send_str_redacted(ProtocolSession* session, const char* data); bool protocol_send_str_redacted(ProtocolSession* session, const char* data);
char* protocol_receive_str_redacted(ProtocolSession* session); char* protocol_receive_str_redacted(ProtocolSession* session);
bool protocol_send_data(ProtocolSession* session, const Data* data); bool protocol_send_data(ProtocolSession* session, const Data* data);
Data* protocol_receive_data(ProtocolSession* session);
Data* protocol_receive_data_limited(ProtocolSession* session, unsigned long long maximum_size); Data* protocol_receive_data_limited(ProtocolSession* session, unsigned long long maximum_size);
bool protocol_send_int(ProtocolSession* session, int data); bool protocol_send_int(ProtocolSession* session, int data);
bool protocol_receive_int(ProtocolSession* session, int* data); bool protocol_receive_int(ProtocolSession* session, int* data);
@@ -184,4 +198,23 @@ bool receive_status(int file_descriptor, Status* status);
this so the sender does not abort after the deletion already committed. */ this so the sender does not abort after the deletion already committed. */
bool receive_status_timed(int file_descriptor, Status* status, int timeout_sec); bool receive_status_timed(int file_descriptor, Status* status, int timeout_sec);
/* Callback polled by protocol_receive_status_keepalive once per keepalive
interval. Return true to stop waiting (e.g. a SIGINT/SIGTERM abort flag was
set). Kept as a function pointer so the protocol layer does not depend on
client signal state. */
typedef bool (*ProtocolWaitAbort)(void);
/* Like receive_status_timed, but while the peer is silent it emits
STATUS_KEEPALIVE every keepalive_interval_sec (the receiver answers each with
STATUS_KEEPALIVE, which this function consumes and skips) so a long
server-side operation does not look like a dead connection. The total wait
is still bounded by timeout_sec; abort_check (may be NULL) is polled every
interval and, when it returns true, ends the wait immediately with false.
Runs entirely on the calling thread: the protocol send path is NOT safe for
concurrent writers, so this must not be paired with a helper thread. */
bool receive_status_keepalive(int file_descriptor, Status* status, int timeout_sec,
int keepalive_interval_sec, ProtocolWaitAbort abort_check);
bool protocol_receive_status_keepalive(ProtocolSession* session, Status* status, int timeout_sec,
int keepalive_interval_sec, ProtocolWaitAbort abort_check);
#endif #endif
+1 -1
View File
@@ -128,7 +128,7 @@ char* ssh_build_remote_command(const char* server_path, bool old_args, char* con
q++; q++;
len++; len++;
} }
if (len > SIZE_MAX - q * 3 || len + q * 3 + 3 > SIZE_MAX - command_len) if (q > (SIZE_MAX - len) / 3 || len + q * 3 + 3 > SIZE_MAX - command_len)
return NULL; return NULL;
command_len += len + q * 3 + 3; command_len += len + q * 3 + 3;
} }
+43 -9
View File
@@ -19,6 +19,7 @@
static volatile sig_atomic_t g_active_connections = 0; static volatile sig_atomic_t g_active_connections = 0;
static void tcp_apply_socket_timeout(int fd); static void tcp_apply_socket_timeout(int fd);
static void tcp_enable_nodelay_default(int fd, int family);
static void sigchld_handler(int sig) { static void sigchld_handler(int sig) {
(void)sig; (void)sig;
@@ -115,6 +116,11 @@ Server* server_create(int port) {
return server_create_ex(port, NULL); return server_create_ex(port, NULL);
} }
void server_set_max_connections(Server* server, unsigned int max_connections) {
if (server && max_connections > 0)
server->max_connections = max_connections;
}
void server_delete(Server** server) { void server_delete(Server** server) {
if (server == NULL || *server == NULL) if (server == NULL || *server == NULL)
return; return;
@@ -135,7 +141,7 @@ static void accept_loop(Server* server, void (*child_fn)(int, void*), void* chil
} }
signal(SIGCHLD, sigchld_handler); signal(SIGCHLD, sigchld_handler);
while (1) { while (1) {
struct sockaddr_in client_addr; struct sockaddr_storage client_addr;
socklen_t client_len = sizeof(client_addr); socklen_t client_len = sizeof(client_addr);
int fd = accept(server->file_descriptor, (struct sockaddr*)&client_addr, &client_len); int fd = accept(server->file_descriptor, (struct sockaddr*)&client_addr, &client_len);
if (fd < 0) { if (fd < 0) {
@@ -143,18 +149,31 @@ static void accept_loop(Server* server, void (*child_fn)(int, void*), void* chil
continue; continue;
} }
tcp_apply_socket_timeout(fd); tcp_apply_socket_timeout(fd);
tcp_enable_nodelay_default(fd, client_addr.ss_family);
char peer[128];
if (!utils_sockaddr_to_string((const struct sockaddr*)&client_addr, peer, sizeof(peer)))
snprintf(peer, sizeof(peer), "unknown");
if ((unsigned int)g_active_connections >= server->max_connections) { if ((unsigned int)g_active_connections >= server->max_connections) {
log_message(LOG_LEVEL_WARNING, "Max connections (%u) reached, rejecting", log_message(LOG_LEVEL_WARNING, "Max connections (%u) reached, rejecting %s",
server->max_connections); server->max_connections, peer);
close(fd); close(fd);
continue; continue;
} }
log_message(LOG_LEVEL_INFO, "%s", log_fmt); log_message(LOG_LEVEL_INFO, "%s from %s", log_fmt, peer);
pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) { if (pid == 0) {
/* Connection children must not run the parent's global cleanup(): it
* frees state (credentials / daemon conf) that the child's worker
* threads may still be reading and closes fd numbers the child could
* already have reused. Reset the inherited handlers so a signal
* terminates the child directly; SIGCHLD is reset too since a child
* must never reap the parent's children. This runs before the child
* spawns any thread, so it cannot race one. */
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGCHLD, SIG_DFL);
close(server->file_descriptor); close(server->file_descriptor);
child_fn(fd, child_ctx); child_fn(fd, child_ctx);
close(fd);
_exit(0); _exit(0);
} else if (pid > 0) { } else if (pid > 0) {
g_active_connections++; g_active_connections++;
@@ -169,6 +188,9 @@ struct plain_ctx {
static void plain_child_fn(int fd, void* ctx) { static void plain_child_fn(int fd, void* ctx) {
((struct plain_ctx*)ctx)->handler(fd); ((struct plain_ctx*)ctx)->handler(fd);
/* handler() never closes the connection fd; the child owns its single
* close here after the handler has fully torn down. */
close(fd);
} }
bool server_listen(Server* server, void (*handler)(int file_descriptor)) { bool server_listen(Server* server, void (*handler)(int file_descriptor)) {
@@ -211,6 +233,19 @@ static void tcp_apply_socket_timeout(int fd) {
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
} }
/* Enable TCP_NODELAY by default on a transfer socket: the protocol emits many
* small messages and Nagle's algorithm would otherwise coalesce/delay them.
* Best-effort only: the family guard keeps this to IP/TCP sockets, and a
* setsockopt failure is ignored. A caller-provided --sockopts TCP_NODELAY=0
* is applied afterwards on the connect path, so an explicit user choice still
* wins. */
static void tcp_enable_nodelay_default(int fd, int family) {
if (family != AF_INET && family != AF_INET6)
return;
int value = 1;
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof(value));
}
Client* client_create() { Client* client_create() {
Client* client = (Client*)malloc(sizeof(Client)); Client* client = (Client*)malloc(sizeof(Client));
if (client == NULL) { if (client == NULL) {
@@ -356,6 +391,9 @@ bool tcp_connect_socket_ex(Client* client, const char* host, int port,
if (client->file_descriptor < 0) if (client->file_descriptor < 0)
continue; continue;
/* Default first; a user --sockopts TCP_NODELAY=0 applied below overrides. */
tcp_enable_nodelay_default(client->file_descriptor, rp->ai_family);
if (opts && opts->sockopt_count > 0 && if (opts && opts->sockopt_count > 0 &&
!tcp_apply_sockopts(client->file_descriptor, opts->sockopts, opts->sockopt_count)) { !tcp_apply_sockopts(client->file_descriptor, opts->sockopts, opts->sockopt_count)) {
close(client->file_descriptor); close(client->file_descriptor);
@@ -402,10 +440,6 @@ bool tcp_connect_socket_ex(Client* client, const char* host, int port,
return true; return true;
} }
bool tcp_connect_socket(Client* client, const char* host, int port) {
return tcp_connect_socket_ex(client, host, port, NULL);
}
bool client_connect_ex(Client* client, const char* host, int port, const TcpConnectOptions* opts) { bool client_connect_ex(Client* client, const char* host, int port, const TcpConnectOptions* opts) {
if (!tcp_connect_socket_ex(client, host, port, opts)) if (!tcp_connect_socket_ex(client, host, port, opts))
return false; return false;
+3 -1
View File
@@ -45,6 +45,9 @@ typedef struct {
Server* server_create_ex(int port, const ServerBindOptions* bind_opts); Server* server_create_ex(int port, const ServerBindOptions* bind_opts);
Server* server_create(int port); Server* server_create(int port);
/* Override the listener's connection cap (the global daemon `max connections`
* value). A non-positive value is ignored so the default cap stands. */
void server_set_max_connections(Server* server, unsigned int max_connections);
bool server_listen(Server* server, void (*handler)(int file_descriptor)); bool server_listen(Server* server, void (*handler)(int file_descriptor));
void server_accept_loop(Server* server, void (*child_fn)(int, void*), void* child_ctx, void server_accept_loop(Server* server, void (*child_fn)(int, void*), void* child_ctx,
const char* log_fmt); const char* log_fmt);
@@ -54,7 +57,6 @@ bool client_connect_ex(Client* client, const char* host, int port, const TcpConn
bool client_connect(Client* client, const char* host, int port); bool client_connect(Client* client, const char* host, int port);
bool tcp_connect_socket_ex(Client* client, const char* host, int port, bool tcp_connect_socket_ex(Client* client, const char* host, int port,
const TcpConnectOptions* opts); const TcpConnectOptions* opts);
bool tcp_connect_socket(Client* client, const char* host, int port);
void client_disconnect(Client* client); void client_disconnect(Client* client);
void client_delete(Client* client); void client_delete(Client* client);
void tcp_set_timeouts(int timeout_sec, int contimeout_sec); void tcp_set_timeouts(int timeout_sec, int contimeout_sec);
+17
View File
@@ -65,6 +65,18 @@ static SSL_CTX* create_ssl_ctx(bool is_server, const char* cert, const char* key
SSL_CTX_free(ctx); SSL_CTX_free(ctx);
return NULL; return NULL;
} }
/* TLS 1.3 ciphersuites are configured separately from the TLS 1.2 and below
* cipher list above. Pin the three AEAD suites OpenSSL offers, dropping
* TLS_AES_128_CCM_SHA256 and the CCM_8 variant, and fail closed if the
* library rejects the policy. SSL_CTX_set_ciphersuites needs OpenSSL 1.1.1;
* earlier versions have no TLS 1.3, so the call is compile-guarded. */
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
if (SSL_CTX_set_ciphersuites(
ctx, "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256") != 1) {
SSL_CTX_free(ctx);
return NULL;
}
#endif
if (cert && key) { if (cert && key) {
struct stat key_stat; struct stat key_stat;
@@ -180,13 +192,18 @@ static void tls_child_fn(int fd, void* arg) {
SSL* ssl = wrap_fd_with_ssl(fd, ctx->ssl_ctx, true, NULL); SSL* ssl = wrap_fd_with_ssl(fd, ctx->ssl_ctx, true, NULL);
if (!ssl) { if (!ssl) {
io_set_ssl(NULL); io_set_ssl(NULL);
close(fd);
return; return;
} }
io_set_ssl(ssl); io_set_ssl(ssl);
ctx->handler(fd); ctx->handler(fd);
/* Shut the TLS layer down before releasing the fd: handler() no longer
* closes it, so SSL_shutdown still has a valid socket. The child owns the
* single fd close, performed last. */
SSL_shutdown(ssl); SSL_shutdown(ssl);
SSL_free(ssl); SSL_free(ssl);
io_set_ssl(NULL); io_set_ssl(NULL);
close(fd);
} }
bool server_listen_tls(Server* server, void (*handler)(int file_descriptor)) { bool server_listen_tls(Server* server, void (*handler)(int file_descriptor)) {
+352 -37
View File
@@ -13,6 +13,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <xxhash.h>
static int authorized_root_fd = -1; static int authorized_root_fd = -1;
static char* authorized_root_path; static char* authorized_root_path;
@@ -35,11 +36,19 @@ void utils_set_authorized_root_fd(int fd) {
(void)utils_set_authorized_root(fd, NULL); (void)utils_set_authorized_root(fd, NULL);
} }
static bool path_is_within_root(const char* root, const char* path) { bool path_is_within_root(const char* root, const char* path) {
size_t root_len = strlen(root); size_t root_len = strlen(root);
return strncmp(root, path, root_len) == 0 && (path[root_len] == '\0' || path[root_len] == '/'); return strncmp(root, path, root_len) == 0 && (path[root_len] == '\0' || path[root_len] == '/');
} }
/* Open the destination root directory itself, confined to the authorized root.
* NOTE (do not merge with file_open_secure_parent): this walk opens dest_root
* (a directory that must already exist) and returns its fd, whereas
* file_open_secure_parent resolves the PARENT of a file path, optionally
* creating missing components and honouring --keep-dirlinks / --copy-as. The
* two differ in create-vs-no-create, in what path component they stop at, and
* in the extra receiver policies they apply, so they are intentionally kept
* separate. Both rely on the shared lexical path_is_within_root check. */
static int open_authorized_destination(const char* dest_root) { static int open_authorized_destination(const char* dest_root) {
if (authorized_root_fd < 0 || !authorized_root_path || !dest_root || if (authorized_root_fd < 0 || !authorized_root_path || !dest_root ||
!path_is_within_root(authorized_root_path, dest_root)) !path_is_within_root(authorized_root_path, dest_root))
@@ -96,6 +105,241 @@ char* str_dup(const char* string) {
return new_string; return new_string;
} }
#define STR_HASH_SET_MIN_CAPACITY 16
static size_t str_hash_set_hash(const char* key, size_t len) {
return (size_t)XXH64(key, len, 0);
}
/* Store a borrowed key. Returns 1 when a new slot was filled and 0 for a
* duplicate. */
static int str_hash_set_put(StrHashSet* set, const char* key, size_t len) {
size_t mask = set->capacity - 1;
size_t index = str_hash_set_hash(key, len) & mask;
while (true) {
StrHashSetSlot* slot = &set->slots[index];
if (!slot->key) {
slot->key = key;
set->size++;
return 1;
}
if (strlen(slot->key) == len && memcmp(slot->key, key, len) == 0)
return 0;
index = (index + 1) & mask;
}
}
static bool str_hash_set_resize(StrHashSet* set, size_t new_capacity) {
StrHashSetSlot* old_slots = set->slots;
size_t old_capacity = set->capacity;
StrHashSetSlot* slots = calloc(new_capacity, sizeof(StrHashSetSlot));
if (!slots)
return false;
set->slots = slots;
set->capacity = new_capacity;
set->size = 0;
for (size_t i = 0; i < old_capacity; i++) {
if (old_slots[i].key)
(void)str_hash_set_put(set, old_slots[i].key, strlen(old_slots[i].key));
}
free(old_slots);
return true;
}
static bool str_hash_set_grow(StrHashSet* set) {
if (set->capacity != 0 && (set->size + 1) * 4 <= set->capacity * 3)
return true;
size_t new_capacity = set->capacity ? set->capacity * 2 : STR_HASH_SET_MIN_CAPACITY;
return str_hash_set_resize(set, new_capacity);
}
bool str_hash_set_init(StrHashSet* set, size_t hint) {
if (!set)
return false;
set->slots = NULL;
set->capacity = 0;
set->size = 0;
size_t capacity = STR_HASH_SET_MIN_CAPACITY;
while (capacity < (hint + 1) * 2 && capacity <= SIZE_MAX / 2)
capacity *= 2;
set->slots = calloc(capacity, sizeof(StrHashSetSlot));
if (!set->slots)
return false;
set->capacity = capacity;
return true;
}
void str_hash_set_free(StrHashSet* set) {
if (!set)
return;
free(set->slots);
set->slots = NULL;
set->capacity = 0;
set->size = 0;
}
bool str_hash_set_insert_ref(StrHashSet* set, const char* key) {
if (!set || !key)
return false;
if (!str_hash_set_grow(set))
return false;
/* put() returns 1 for a new slot and 0 for a duplicate; both are success. */
(void)str_hash_set_put(set, key, strlen(key));
return true;
}
static const StrHashSetSlot* str_hash_set_find_n(const StrHashSet* set, const char* key,
size_t len) {
if (!set || set->capacity == 0 || !key)
return NULL;
size_t mask = set->capacity - 1;
size_t index = str_hash_set_hash(key, len) & mask;
while (true) {
const StrHashSetSlot* slot = &set->slots[index];
if (!slot->key)
return NULL;
if (strlen(slot->key) == len && memcmp(slot->key, key, len) == 0)
return slot;
index = (index + 1) & mask;
}
}
bool str_hash_set_lookup_n(const StrHashSet* set, const char* key, size_t len) {
return str_hash_set_find_n(set, key, len) != NULL;
}
bool str_hash_set_lookup(const StrHashSet* set, const char* key) {
if (!key)
return false;
return str_hash_set_lookup_n(set, key, strlen(key));
}
static int str_sorted_array_compare(const void* left, const void* right) {
const char* const* left_key = left;
const char* const* right_key = right;
return strcmp(*left_key, *right_key);
}
bool str_sorted_array_build(StrSortedArray* array, const char* const* items, size_t count) {
if (!array)
return false;
array->items = NULL;
array->count = 0;
if (count == 0)
return true;
if (!items || count > SIZE_MAX / sizeof(const char*))
return false;
const char** sorted = malloc(count * sizeof(*sorted));
if (!sorted)
return false;
for (size_t i = 0; i < count; i++)
sorted[i] = items[i];
qsort(sorted, count, sizeof(*sorted), str_sorted_array_compare);
array->items = sorted;
array->count = count;
return true;
}
void str_sorted_array_free(StrSortedArray* array) {
if (!array)
return;
free(array->items);
array->items = NULL;
array->count = 0;
}
bool str_sorted_array_contains(const StrSortedArray* array, const char* key) {
if (!array || !key || array->count == 0)
return false;
size_t lo = 0;
size_t hi = array->count;
while (lo < hi) {
size_t mid = lo + (hi - lo) / 2;
int cmp = strcmp(array->items[mid], key);
if (cmp < 0)
lo = mid + 1;
else if (cmp > 0)
hi = mid;
else
return true;
}
return false;
}
/* Compare `entry` against the virtual key `key` + '/' without allocating the
* concatenation. Returns <0, 0 or >0 as `entry` sorts before, equal to, or
* after that virtual key. */
static int str_sorted_array_compare_prefix(const char* entry, const char* key, size_t key_len) {
int cmp = strncmp(entry, key, key_len);
if (cmp != 0)
return cmp;
unsigned char next = (unsigned char)entry[key_len];
if (next == '\0')
return -1; /* entry == key sorts before key + '/' */
return (int)next - (int)'/';
}
bool str_sorted_array_has_child_prefix(const StrSortedArray* array, const char* key) {
if (!array || !key || array->count == 0 || key[0] == '\0')
return false;
size_t key_len = strlen(key);
size_t lo = 0;
size_t hi = array->count;
while (lo < hi) {
size_t mid = lo + (hi - lo) / 2;
if (str_sorted_array_compare_prefix(array->items[mid], key, key_len) < 0)
lo = mid + 1;
else
hi = mid;
}
if (lo >= array->count)
return false;
const char* entry = array->items[lo];
return strncmp(entry, key, key_len) == 0 && entry[key_len] == '/';
}
bool path_index_build(PathIndex* index, const char* const* entries, size_t count) {
if (!index)
return false;
index->exact.slots = NULL;
index->exact.capacity = 0;
index->exact.size = 0;
index->sorted.items = NULL;
index->sorted.count = 0;
if (!str_hash_set_init(&index->exact, count))
return false;
if (!str_sorted_array_build(&index->sorted, entries, count)) {
str_hash_set_free(&index->exact);
return false;
}
for (size_t i = 0; i < count; i++) {
if (!str_hash_set_insert_ref(&index->exact, entries[i])) {
path_index_free(index);
return false;
}
}
return true;
}
void path_index_free(PathIndex* index) {
if (!index)
return;
str_hash_set_free(&index->exact);
str_sorted_array_free(&index->sorted);
}
bool path_index_contains(const PathIndex* index, const char* path) {
return index && str_hash_set_lookup(&index->exact, path);
}
bool path_index_contains_n(const PathIndex* index, const char* path, size_t len) {
return index && str_hash_set_lookup_n(&index->exact, path, len);
}
bool path_index_has_descendant(const PathIndex* index, const char* path) {
return index && str_sorted_array_has_child_prefix(&index->sorted, path);
}
char* output_escape(const char* string, bool eight_bit_output) { char* output_escape(const char* string, bool eight_bit_output) {
if (!string) if (!string)
return NULL; return NULL;
@@ -196,15 +440,23 @@ bool format_human_bytes(unsigned long long bytes, char* buffer, size_t buffer_si
return written >= 0 && (size_t)written < buffer_size; return written >= 0 && (size_t)written < buffer_size;
} }
static bool is_dir_in_manifest(const char* rel_path, ArrayList* manifest) { /* Build the keep-set index from the exact manifest entries only. A lookup of
size_t len = strlen(rel_path); `rel` succeeds iff `rel` is a kept entry, a kept directory, or an ancestor
for (int i = 0; i < manifest->size; i++) { directory of kept content (the old is_dir_in_manifest predicate); the sorted
const char* entry = (const char*)manifest->items[i]; view answers "is an ancestor of kept content" without materializing any
// Check if entry starts with rel_path + '/' or matches exactly per-component prefix copy, so the index is O(manifest size) memory. */
if (strncmp(entry, rel_path, len) == 0 && (entry[len] == '/' || entry[len] == '\0')) static bool build_keep_index(const ArrayList* manifest, PathIndex* index) {
return true; if (!manifest || manifest->size <= 0)
} return path_index_build(index, NULL, 0);
return false; return path_index_build(index, (const char* const*)manifest->items, (size_t)manifest->size);
}
static bool keep_is_dir(const PathIndex* index, const char* rel_path) {
return path_index_contains(index, rel_path) || path_index_has_descendant(index, rel_path);
}
static bool keep_is_file(const PathIndex* index, const char* rel_path) {
return path_index_contains(index, rel_path);
} }
/* True when child_rel is, or lies below, a protected entry. A prefix "a" /* True when child_rel is, or lies below, a protected entry. A prefix "a"
@@ -234,7 +486,7 @@ bool path_under_skip_prefix(const char* child_rel, bool at_root, const DeleteSki
prefixes) mark the enclosing directory as surviving, exactly as they would prefixes) mark the enclosing directory as surviving, exactly as they would
make a real rmdir fail with ENOTEMPTY. Stops early once *count reaches the make a real rmdir fail with ENOTEMPTY. Stops early once *count reaches the
cap (sets *exceeds). Returns false on a traversal error. */ cap (sets *exceeds). Returns false on a traversal error. */
static bool count_extras_fd(int dirfd, const char* rel_path, ArrayList* manifest, size_t cap, static bool count_extras_fd(int dirfd, const char* rel_path, const PathIndex* keep, size_t cap,
size_t* count, bool* exceeds, const DeleteSkipEntry* skips, size_t* count, bool* exceeds, const DeleteSkipEntry* skips,
int skip_count, bool* survives) { int skip_count, bool* survives) {
/* openat(dirfd, ".") opens an independent file description: a dup() would /* openat(dirfd, ".") opens an independent file description: a dup() would
@@ -284,15 +536,15 @@ static bool count_extras_fd(int dirfd, const char* rel_path, ArrayList* manifest
bool child_ok = true; bool child_ok = true;
bool child_survives = true; bool child_survives = true;
if (childfd >= 0) { if (childfd >= 0) {
child_ok = count_extras_fd(childfd, child_rel, manifest, cap, count, exceeds, skips, child_ok = count_extras_fd(childfd, child_rel, keep, cap, count, exceeds, skips, skip_count,
skip_count, &child_survives); &child_survives);
close(childfd); close(childfd);
} else if (errno != ENOENT) { } else if (errno != ENOENT) {
operation_ok = false; operation_ok = false;
} }
if (!child_ok) if (!child_ok)
operation_ok = false; operation_ok = false;
if (is_dir_in_manifest(child_rel, manifest)) { if (keep_is_dir(keep, child_rel)) {
/* A directory with kept content below it is never removed. */ /* A directory with kept content below it is never removed. */
local_survives = true; local_survives = true;
} else if (child_survives) { } else if (child_survives) {
@@ -308,13 +560,7 @@ static bool count_extras_fd(int dirfd, const char* rel_path, ArrayList* manifest
} }
} }
} else { } else {
bool found = false; bool found = keep_is_file(keep, child_rel);
for (int i = 0; i < manifest->size; i++) {
if (strcmp((char*)manifest->items[i], child_rel) == 0) {
found = true;
break;
}
}
if (!found) { if (!found) {
if (*count >= cap) { if (*count >= cap) {
*exceeds = true; *exceeds = true;
@@ -330,7 +576,7 @@ static bool count_extras_fd(int dirfd, const char* rel_path, ArrayList* manifest
return operation_ok; return operation_ok;
} }
static bool delete_extras_fd(int dirfd, const char* rel_path, ArrayList* manifest, static bool delete_extras_fd(int dirfd, const char* rel_path, const PathIndex* keep,
size_t max_delete, size_t* deleted_count, const DeleteSkipEntry* skips, size_t max_delete, size_t* deleted_count, const DeleteSkipEntry* skips,
int skip_count) { int skip_count) {
/* Independent file description (see count_extras_fd). */ /* Independent file description (see count_extras_fd). */
@@ -379,15 +625,15 @@ static bool delete_extras_fd(int dirfd, const char* rel_path, ArrayList* manifes
int childfd = openat(dirfd, entry->d_name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); int childfd = openat(dirfd, entry->d_name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
bool child_removed = false; bool child_removed = false;
if (childfd >= 0) { if (childfd >= 0) {
child_removed = delete_extras_fd(childfd, child_rel, manifest, max_delete, deleted_count, child_removed = delete_extras_fd(childfd, child_rel, keep, max_delete, deleted_count, skips,
skips, skip_count); skip_count);
if (!child_removed) if (!child_removed)
operation_ok = false; operation_ok = false;
close(childfd); close(childfd);
} else if (errno != ENOENT) { } else if (errno != ENOENT) {
operation_ok = false; operation_ok = false;
} }
if (child_removed && !is_dir_in_manifest(child_rel, manifest)) { if (child_removed && !keep_is_dir(keep, child_rel)) {
if (*deleted_count >= max_delete) { if (*deleted_count >= max_delete) {
operation_ok = false; operation_ok = false;
} else { } else {
@@ -406,13 +652,7 @@ static bool delete_extras_fd(int dirfd, const char* rel_path, ArrayList* manifes
} }
} else { } else {
// Check if relative path is in manifest // Check if relative path is in manifest
bool found = false; bool found = keep_is_file(keep, child_rel);
for (int i = 0; i < manifest->size; i++) {
if (strcmp((char*)manifest->items[i], child_rel) == 0) {
found = true;
break;
}
}
if (!found) { if (!found) {
if (*deleted_count >= max_delete) { if (*deleted_count >= max_delete) {
operation_ok = false; operation_ok = false;
@@ -436,13 +676,18 @@ static bool delete_extras_fd(int dirfd, const char* rel_path, ArrayList* manifes
return operation_ok; return operation_ok;
} }
DeleteWalkResult delete_extras_limited(const char* dest_root, ArrayList* manifest, DeleteWalkResult delete_extras_limited(const char* dest_root, const ArrayList* manifest,
size_t max_delete, const DeleteSkipEntry* skips, size_t max_delete, const DeleteSkipEntry* skips,
int skip_count, size_t* deleted_out) { int skip_count, size_t* deleted_out) {
if (deleted_out) if (deleted_out)
*deleted_out = 0; *deleted_out = 0;
if (!manifest) if (!manifest)
return DELETE_WALK_ERROR; return DELETE_WALK_ERROR;
/* Index the keep-set once so both passes answer membership in O(path length)
instead of scanning every manifest entry for every destination entry. */
PathIndex keep;
if (!build_keep_index(manifest, &keep))
return DELETE_WALK_ERROR;
int rootfd; int rootfd;
if (authorized_root_fd >= 0) { if (authorized_root_fd >= 0) {
if (authorized_root_path) if (authorized_root_path)
@@ -454,35 +699,40 @@ DeleteWalkResult delete_extras_limited(const char* dest_root, ArrayList* manifes
} else { } else {
rootfd = open(dest_root, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); rootfd = open(dest_root, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
} }
if (rootfd < 0) if (rootfd < 0) {
path_index_free(&keep);
return DELETE_WALK_ERROR; return DELETE_WALK_ERROR;
}
if (max_delete != SIZE_MAX) { if (max_delete != SIZE_MAX) {
/* Rehearse the deletion first so a run that would exceed the cap removes /* Rehearse the deletion first so a run that would exceed the cap removes
nothing (rsync's all-or-nothing --max-delete contract). */ nothing (rsync's all-or-nothing --max-delete contract). */
size_t count = 0; size_t count = 0;
bool exceeds = false; bool exceeds = false;
bool survives = false; bool survives = false;
bool counted_ok = count_extras_fd(rootfd, "", manifest, max_delete, &count, &exceeds, skips, bool counted_ok = count_extras_fd(rootfd, "", &keep, max_delete, &count, &exceeds, skips,
skip_count, &survives); skip_count, &survives);
if (!counted_ok) { if (!counted_ok) {
close(rootfd); close(rootfd);
path_index_free(&keep);
return DELETE_WALK_ERROR; return DELETE_WALK_ERROR;
} }
if (exceeds) { if (exceeds) {
close(rootfd); close(rootfd);
path_index_free(&keep);
return DELETE_WALK_LIMIT_EXCEEDED; return DELETE_WALK_LIMIT_EXCEEDED;
} }
} }
size_t deleted_count = 0; size_t deleted_count = 0;
bool ok = delete_extras_fd(rootfd, "", manifest, max_delete, &deleted_count, skips, skip_count); bool ok = delete_extras_fd(rootfd, "", &keep, max_delete, &deleted_count, skips, skip_count);
if (close(rootfd) != 0) if (close(rootfd) != 0)
ok = false; ok = false;
path_index_free(&keep);
if (deleted_out) if (deleted_out)
*deleted_out = deleted_count; *deleted_out = deleted_count;
return ok ? DELETE_WALK_OK : DELETE_WALK_ERROR; return ok ? DELETE_WALK_OK : DELETE_WALK_ERROR;
} }
bool delete_extras(const char* dest_root, ArrayList* manifest) { bool delete_extras(const char* dest_root, const ArrayList* manifest) {
return delete_extras_limited(dest_root, manifest, SIZE_MAX, NULL, 0, NULL) == DELETE_WALK_OK; return delete_extras_limited(dest_root, manifest, SIZE_MAX, NULL, 0, NULL) == DELETE_WALK_OK;
} }
@@ -587,6 +837,71 @@ bool utils_fd_peer_is_local(int fd) {
return utils_sockaddr_is_loopback((const struct sockaddr*)&peer); return utils_sockaddr_is_loopback((const struct sockaddr*)&peer);
} }
/* Numeric peer address of a connected fd. Only AF_INET/AF_INET6 peers are
formatted; every other descriptor/family (pipe, AF_UNIX socketpair, ...) or a
getpeername failure returns false with buf emptied. The caller must treat
that as "cannot tell". */
bool utils_fd_peer_ip(int fd, char* buf, size_t len) {
if (!buf || len == 0)
return false;
buf[0] = '\0';
if (fd < 0)
return false;
struct sockaddr_storage peer;
socklen_t peer_len = sizeof(peer);
if (getpeername(fd, (struct sockaddr*)&peer, &peer_len) != 0)
return false;
const void* src = NULL;
int family = peer.ss_family;
if (family == AF_INET) {
src = &((const struct sockaddr_in*)&peer)->sin_addr;
} else if (family == AF_INET6) {
const struct sockaddr_in6* peer6 = (const struct sockaddr_in6*)&peer;
/* A dual-stack IPv6 listener reports IPv4 peers as ::ffff:a.b.c.d. Emit
* the IPv4 form so IPv4 ACL patterns (and logs) see the real address. */
if (IN6_IS_ADDR_V4MAPPED(&peer6->sin6_addr)) {
struct in_addr v4;
memcpy(&v4, &peer6->sin6_addr.s6_addr[12], sizeof(v4));
return inet_ntop(AF_INET, &v4, buf, (socklen_t)len) != NULL;
}
src = &peer6->sin6_addr;
} else {
return false;
}
return inet_ntop(family, src, buf, (socklen_t)len) != NULL;
}
/* "ip:port" / "[ip]:port" for a connected peer, used to log the connecting
address in the accept loop. Returns false for a non-INET family. */
bool utils_sockaddr_to_string(const struct sockaddr* addr, char* buf, size_t len) {
if (!addr || !buf || len == 0)
return false;
buf[0] = '\0';
char ip[INET6_ADDRSTRLEN];
unsigned short port;
int written;
if (addr->sa_family == AF_INET) {
const struct sockaddr_in* v4 = (const struct sockaddr_in*)addr;
if (!inet_ntop(AF_INET, &v4->sin_addr, ip, sizeof(ip)))
return false;
port = ntohs(v4->sin_port);
written = snprintf(buf, len, "%s:%u", ip, port);
} else if (addr->sa_family == AF_INET6) {
const struct sockaddr_in6* v6 = (const struct sockaddr_in6*)addr;
if (!inet_ntop(AF_INET6, &v6->sin6_addr, ip, sizeof(ip)))
return false;
port = ntohs(v6->sin6_port);
written = snprintf(buf, len, "[%s]:%u", ip, port);
} else {
return false;
}
if (written < 0 || (size_t)written >= len) {
buf[0] = '\0';
return false;
}
return true;
}
/* True when a client-supplied host string names a loopback destination: /* True when a client-supplied host string names a loopback destination:
"localhost", any 127.0.0.0/8 literal, "::1", or "[::1]". */ "localhost", any 127.0.0.0/8 literal, "::1", or "[::1]". */
bool utils_host_is_loopback(const char* host) { bool utils_host_is_loopback(const char* host) {
+87 -2
View File
@@ -6,6 +6,77 @@
#include <stdbool.h> #include <stdbool.h>
#include <sys/socket.h> #include <sys/socket.h>
/* Small open-addressing string hash set used to turn quadratic membership
* scans into O(path length) exact-match lookups (the --delete keep-set and the
* --files-from allow-set). Keys are hashed with xxHash64 (seed 0); collisions
* are resolved by linear probing over a power-of-two table that grows at 75%
* load. Keys are always borrowed from the caller and must outlive the set; the
* set never copies or owns keys, so indexing M entries costs O(M) memory. The
* set is not thread-safe for mutation, but a fully built set supports
* concurrent read-only lookups. */
typedef struct {
const char* key; /* NULL marks an empty slot */
} StrHashSetSlot;
typedef struct {
StrHashSetSlot* slots;
size_t capacity; /* power of two, zero before init */
size_t size;
} StrHashSet;
/* Initialize an empty set sized for roughly `hint` entries. Returns false on
* allocation failure. */
bool str_hash_set_init(StrHashSet* set, size_t hint);
void str_hash_set_free(StrHashSet* set);
/* Insert a borrowed key (must outlive the set). A duplicate is ignored.
* Returns false on allocation failure. */
bool str_hash_set_insert_ref(StrHashSet* set, const char* key);
/* Look up a NUL-terminated key / a key of `len` bytes. */
bool str_hash_set_lookup(const StrHashSet* set, const char* key);
bool str_hash_set_lookup_n(const StrHashSet* set, const char* key, size_t len);
/* Sorted, non-owning view of NUL-terminated strings. Built from borrowed
* pointers (qsort), so indexing M entries costs O(M) memory and O(M log M)
* time; exact membership and ancestor-prefix existence are binary searches
* that never materialize a prefix copy. */
typedef struct {
const char** items; /* sorted with strcmp; borrowed, never freed */
size_t count;
} StrSortedArray;
/* Build `array` over the borrowed `items`. Only the pointer array is copied,
* never the strings. Returns false on allocation failure. */
bool str_sorted_array_build(StrSortedArray* array, const char* const* items, size_t count);
void str_sorted_array_free(StrSortedArray* array);
/* True when some item equals `key`. */
bool str_sorted_array_contains(const StrSortedArray* array, const char* key);
/* True when some item starts with `key` followed by '/' (i.e. `key` is a proper
* ancestor directory of an item). Allocates nothing. */
bool str_sorted_array_has_child_prefix(const StrSortedArray* array, const char* key);
/* Read-only membership index over exact relative paths. `exact` answers
* O(path length) equality; `sorted` answers whether any indexed path lies
* strictly below a query directory. Both borrow their keys from the caller and
* no ancestor prefix is stored as a separate string, so an index over M entries
* is O(M) memory regardless of path depth. Not thread-safe to build, but safe
* for concurrent read-only queries once built. */
typedef struct {
StrHashSet exact;
StrSortedArray sorted;
} PathIndex;
/* Build an index borrowing `entries` (which must outlive the index). Returns
* false on allocation failure, freeing any partial state. */
bool path_index_build(PathIndex* index, const char* const* entries, size_t count);
void path_index_free(PathIndex* index);
/* True when `path` is an indexed entry. */
bool path_index_contains(const PathIndex* index, const char* path);
/* Length-bounded form of path_index_contains (`path` need not be terminated). */
bool path_index_contains_n(const PathIndex* index, const char* path, size_t len);
/* True when some indexed entry lies strictly below `path` (starts with
* `path` + '/'). */
bool path_index_has_descendant(const PathIndex* index, const char* path);
char* str_dup(const char* string); char* str_dup(const char* string);
char* output_escape(const char* string, bool eight_bit_output); char* output_escape(const char* string, bool eight_bit_output);
char* path_cat(const char* path1, const char* path2); char* path_cat(const char* path1, const char* path2);
@@ -48,14 +119,20 @@ bool path_under_skip_prefix(const char* child_rel, bool at_root, const DeleteSki
and the delete pass are two separate walks, so a concurrent change between and the delete pass are two separate walks, so a concurrent change between
them (another process adding/removing entries) can make the second pass them (another process adding/removing entries) can make the second pass
delete a different set than the first one counted. */ delete a different set than the first one counted. */
DeleteWalkResult delete_extras_limited(const char* dest_root, ArrayList* manifest, DeleteWalkResult delete_extras_limited(const char* dest_root, const ArrayList* manifest,
size_t max_delete, const DeleteSkipEntry* skips, size_t max_delete, const DeleteSkipEntry* skips,
int skip_count, size_t* deleted_out); int skip_count, size_t* deleted_out);
bool delete_extras(const char* dest_root, ArrayList* manifest); bool delete_extras(const char* dest_root, const ArrayList* manifest);
bool utils_set_authorized_root(int fd, const char* canonical_path); bool utils_set_authorized_root(int fd, const char* canonical_path);
/* The fd-only compatibility form is fail-closed for path-based operations; /* The fd-only compatibility form is fail-closed for path-based operations;
* callers should use utils_set_authorized_root with the canonical identity. */ * callers should use utils_set_authorized_root with the canonical identity. */
void utils_set_authorized_root_fd(int fd); void utils_set_authorized_root_fd(int fd);
/* True when `path` is `root` itself or lies directly beneath it: a lexical
* prefix test requiring the byte after `root` to be '\0' or '/'. Both `root`
* and `path` must be absolute canonical paths free of "."/".." components (the
* callers guarantee this); this is containment by string, not by resolved
* symlinks. Shared by the utils and file secure-walk root confinement. */
bool path_is_within_root(const char* root, const char* path);
bool has_path_traversal(const char* path); bool has_path_traversal(const char* path);
bool utils_valid_batch_path(const char* path); bool utils_valid_batch_path(const char* path);
bool format_human_bytes(unsigned long long bytes, char* buffer, size_t buffer_size); bool format_human_bytes(unsigned long long bytes, char* buffer, size_t buffer_size);
@@ -77,5 +154,13 @@ bool append_tail_length(unsigned long long old_size, unsigned long long check_si
bool utils_sockaddr_is_loopback(const struct sockaddr* addr); bool utils_sockaddr_is_loopback(const struct sockaddr* addr);
bool utils_fd_peer_is_local(int fd); bool utils_fd_peer_is_local(int fd);
bool utils_host_is_loopback(const char* host); bool utils_host_is_loopback(const char* host);
/* Numeric peer address of a connected fd (INET6_ADDRSTRLEN is always enough).
* Returns false and leaves buf empty when the fd is not a connected INET socket
* or getpeername/inet_ntop fails. Used by the daemon host-access gate; a false
* return is "cannot tell" and must be treated as fail-closed when ACLs apply. */
bool utils_fd_peer_ip(int fd, char* buf, size_t len);
/* Format a sockaddr as "ip:port" (IPv4) or "[ip]:port" (IPv6) for logging.
* Returns false (buf emptied) for a non-INET family or a formatting failure. */
bool utils_sockaddr_to_string(const struct sockaddr* addr, char* buf, size_t len);
#endif #endif
+5 -1
View File
@@ -17,7 +17,11 @@ int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (!d) if (!d)
return 0; return 0;
Chunk* chunk = chunk_deserialize(d, false); /* Exercise both the metadata and non-metadata chunk layouts: the
metadata branch (present flag + 4-vs-72 advance) is only reachable with
use_metadata=true, so base the choice on the input rather than hardcoding
false. */
Chunk* chunk = chunk_deserialize(d, (data[0] & 1) != 0);
if (chunk) if (chunk)
chunk_destroy(chunk); chunk_destroy(chunk);
+126
View File
@@ -0,0 +1,126 @@
/*
* Fuzz the delete-manifest parser: receive_manifest_entries(int fd).
*
* The parser reads three length-delimited sections (keeps, protected prefixes,
* missing-args paths) from the connection. Feeding raw bytes alone exercises
* the "reject the first malformed count/string" fast paths, but because each
* section is self-delimiting a single bad value hides every later section.
*
* To reach the protected-prefix and missing-args parsers (the paths that drive
* actual destination deletion) we build one canonical, fully-valid manifest
* with hand-written wire framing and then feed the receiver several shapes:
*
* 1. raw : the raw fuzz bytes as the whole manifest.
* 2. keeps : the valid keep count only + the fuzz bytes, so the fuzzer
* drives the keep count and entries directly.
* 3. prot : the valid keeps section + the fuzz bytes, so the fuzzer drives
* the protected count and prefixes.
* 4. missing: the valid keeps+protected sections + the fuzz bytes, so the
* fuzzer drives the trailing missing-args section, including the
* aggregate MAX_MANIFEST_BYTES budget.
*
* The wire encoding matches receive_int (native int) and receive_wire_str
* (native size_t length prefix + body); no charset conversion is configured in
* the fuzz process, so receive_wire_str is receive_str.
*/
#include "file_receive.h"
#include "protocol.h"
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
static unsigned char g_manifest[512];
static size_t g_len_after_count; /* offset of the first keep entry */
static size_t g_len_after_keeps; /* offset of the protected count */
static size_t g_len_after_protected; /* offset of the missing count */
static int g_manifest_ready;
static void append_int32(unsigned char* buf, size_t* off, int32_t value) {
memcpy(buf + *off, &value, sizeof(value));
*off += sizeof(value);
}
static void append_wire_str(unsigned char* buf, size_t* off, const char* s) {
size_t n = strlen(s);
memcpy(buf + *off, &n, sizeof(n));
*off += sizeof(n);
memcpy(buf + *off, s, n);
*off += n;
}
static void build_canonical_manifest(void) {
g_manifest_ready = 1;
size_t off = 0;
append_int32(g_manifest, &off, 2);
g_len_after_count = off;
append_wire_str(g_manifest, &off, "keep/a");
append_wire_str(g_manifest, &off, "keep/b");
g_len_after_keeps = off;
append_int32(g_manifest, &off, 1);
append_wire_str(g_manifest, &off, "excluded/prefix");
g_len_after_protected = off;
append_int32(g_manifest, &off, 1);
append_wire_str(g_manifest, &off, "missing/path");
}
/* Best-effort non-blocking write: an oversized fuzz input is truncated rather
* than stalling the harness. */
static void write_best_effort(int fd, const void* data, size_t size) {
const unsigned char* p = data;
size_t off = 0;
while (off < size) {
ssize_t n = write(fd, p + off, size - off);
if (n > 0) {
off += (size_t)n;
continue;
}
if (n < 0 && errno == EINTR)
continue;
break;
}
}
/* Build prefix ++ data as a stream and drive receive_manifest_entries over it.
* The write half is shut down first so the parser always sees EOF instead of
* blocking on a missing frame tail. */
static void receive_stream(const unsigned char* prefix, size_t prefix_len, const uint8_t* data,
size_t size) {
int sv[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0)
return;
int flags = fcntl(sv[0], F_GETFL, 0);
if (flags != -1)
(void)fcntl(sv[0], F_SETFL, flags | O_NONBLOCK);
if (prefix_len > 0)
write_best_effort(sv[0], prefix, prefix_len);
if (size > 0)
write_best_effort(sv[0], data, size);
shutdown(sv[0], SHUT_WR);
DeleteManifest* manifest = receive_manifest_entries(sv[1]);
delete_manifest_free(manifest);
close(sv[0]);
close(sv[1]);
}
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (!g_manifest_ready)
build_canonical_manifest();
/* Raw bytes as the whole manifest. */
receive_stream(NULL, 0, data, size);
/* Keep the valid framing so the fuzzer reaches each later section. */
receive_stream(g_manifest, g_len_after_protected, data, size);
receive_stream(g_manifest, g_len_after_keeps, data, size);
receive_stream(g_manifest, g_len_after_count, data, size);
return 0;
}
+8 -8
View File
@@ -5,19 +5,19 @@
#include <string.h> #include <string.h>
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size < sizeof(int) + FILE_METADATA_WIRE_SIZE) /* Exercise the bounds-checked decoder on EVERY input length, including
return 0; * records shorter than a full metadata body; the decoder must reject those
* without reading past `size`. */
char* buf = malloc(size); char* buf = malloc(size > 0 ? size : 1);
if (!buf) if (!buf)
return 0; return 0;
memcpy(buf, data, size); if (size > 0)
memcpy(buf, data, size);
char* original_buf = buf; FileMetadata* m = metadata_from_buf((const uint8_t*)buf, size);
FileMetadata* m = metadata_from_buf(&buf);
if (m) if (m)
free(m); free(m);
free(original_buf); free(buf);
return 0; return 0;
} }
+130
View File
@@ -0,0 +1,130 @@
/*
* Fuzz the base protocol framing: receive_str / receive_data / receive_status
* (plus the redacted string, the size-limited data and the timed-status
* variants) fed arbitrary bytes over an in-memory socketpair.
*
* Every receive primitive reads a fixed-width header (a size_t string length,
* an unsigned long long data length, an int status/int value) and then a body.
* The fuzzer attacks:
* - oversized length headers (the MAX_STRING_SIZE / MAX_DATA_PAYLOAD_SIZE
* gates must reject before allocating),
* - truncated bodies (a declared body larger than the stream must fail
* cleanly at EOF, never read uninitialised memory or leak),
* - embedded NUL bytes in strings (must be refused),
* - out-of-range status enum values (status_to_string must stay in bounds).
*
* Each entry point gets its own socketpair because a single receive consumes a
* variable number of bytes from the stream; reusing one would make the later
* calls meaningless. The write half is shut down first so a truncated frame
* always terminates at EOF instead of blocking.
*/
#include "data.h"
#include "protocol.h"
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
static void write_best_effort(int fd, const void* data, size_t size) {
const unsigned char* p = data;
size_t off = 0;
while (off < size) {
ssize_t n = write(fd, p + off, size - off);
if (n > 0) {
off += (size_t)n;
continue;
}
if (n < 0 && errno == EINTR)
continue;
break;
}
}
/* Create a socketpair pre-loaded with `data`, shut down the write half and
* return the read end (which the receiver reads from). `*write_end` is also
* returned so the caller can close it. */
static int make_stream(const uint8_t* data, size_t size, int* write_end) {
int sv[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0) {
*write_end = -1;
return -1;
}
int flags = fcntl(sv[0], F_GETFL, 0);
if (flags != -1)
(void)fcntl(sv[0], F_SETFL, flags | O_NONBLOCK);
if (size > 0)
write_best_effort(sv[0], data, size);
shutdown(sv[0], SHUT_WR);
*write_end = sv[0];
return sv[1];
}
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
int w;
int rd = make_stream(data, size, &w);
if (rd >= 0) {
char* s = receive_str(rd);
free(s);
close(rd);
close(w);
}
rd = make_stream(data, size, &w);
if (rd >= 0) {
char* s = receive_str_redacted(rd);
free(s);
close(rd);
close(w);
}
/* Bounded so a crafted 256 MiB length header cannot make each iteration
allocate the full MAX_DATA_PAYLOAD_SIZE under ASan; the framing logic is
identical to receive_data(), which delegates to the limited variant. */
rd = make_stream(data, size, &w);
if (rd >= 0) {
Data* d = receive_data_limited(rd, 1u << 20);
data_destroy(d);
close(rd);
close(w);
}
/* The size-limited variant must reject anything beyond its explicit bound
* before allocating the body buffer. */
rd = make_stream(data, size, &w);
if (rd >= 0) {
Data* d = receive_data_limited(rd, 256);
data_destroy(d);
close(rd);
close(w);
}
rd = make_stream(data, size, &w);
if (rd >= 0) {
Status status = STATUS_OK;
(void)receive_status(rd, &status);
close(rd);
close(w);
}
rd = make_stream(data, size, &w);
if (rd >= 0) {
Status status = STATUS_OK;
(void)receive_status_timed(rd, &status, 1);
close(rd);
close(w);
}
rd = make_stream(data, size, &w);
if (rd >= 0) {
int value = 0;
(void)receive_int(rd, &value);
close(rd);
close(w);
}
return 0;
}
+115
View File
@@ -0,0 +1,115 @@
/*
* Fuzz the xattr wire block parser: xattr_receive(int fd, int* ok).
*
* The block is a count followed by that many (name_len, name, value_len, value)
* records. The receiver must reject an invalid count, an out-of-range or
* negative name/value length, an embedded NUL or non-whitelisted namespace in
* the name, an oversized value, and an aggregate payload beyond
* XATTR_TOTAL_MAX -- all without over-allocating or leaking.
*
* Raw bytes mostly stop at the first invalid count/length, so we also build a
* canonical, fully-valid two-entry block by hand and feed the receiver valid
* prefixes of it followed by the fuzz bytes. That drives the deep value-
* parsing and per-entry namespace/budget checks with attacker-controlled input.
*/
#include "protocol.h"
#include "xattr.h"
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
static unsigned char g_block[512];
static size_t g_off_after_count; /* start of entry 0 */
static size_t g_off_after_entry0; /* start of entry 1 */
static size_t g_off_value0; /* start of the first value length */
static int g_block_ready;
static void append_int32(unsigned char* buf, size_t* off, int32_t value) {
memcpy(buf + *off, &value, sizeof(value));
*off += sizeof(value);
}
static void append_bytes(unsigned char* buf, size_t* off, const void* p, size_t n) {
if (n > 0)
memcpy(buf + *off, p, n);
*off += n;
}
static void build_canonical_block(void) {
g_block_ready = 1;
size_t off = 0;
append_int32(g_block, &off, 2);
g_off_after_count = off;
int32_t name0_len = (int32_t)strlen("user.foo");
append_int32(g_block, &off, name0_len);
append_bytes(g_block, &off, "user.foo", (size_t)name0_len);
g_off_value0 = off;
append_int32(g_block, &off, 3);
append_bytes(g_block, &off, "bar", 3);
g_off_after_entry0 = off;
int32_t name1_len = (int32_t)strlen("user.empty");
append_int32(g_block, &off, name1_len);
append_bytes(g_block, &off, "user.empty", (size_t)name1_len);
append_int32(g_block, &off, 0);
}
static void write_best_effort(int fd, const void* data, size_t size) {
const unsigned char* p = data;
size_t off = 0;
while (off < size) {
ssize_t n = write(fd, p + off, size - off);
if (n > 0) {
off += (size_t)n;
continue;
}
if (n < 0 && errno == EINTR)
continue;
break;
}
}
static void receive_stream(const unsigned char* prefix, size_t prefix_len, const uint8_t* data,
size_t size) {
int sv[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0)
return;
int flags = fcntl(sv[0], F_GETFL, 0);
if (flags != -1)
(void)fcntl(sv[0], F_SETFL, flags | O_NONBLOCK);
if (prefix_len > 0)
write_best_effort(sv[0], prefix, prefix_len);
if (size > 0)
write_best_effort(sv[0], data, size);
shutdown(sv[0], SHUT_WR);
int ok = 0;
FileXattrList* list = xattr_receive(sv[1], &ok);
xattr_list_free(list);
close(sv[0]);
close(sv[1]);
}
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (!g_block_ready)
build_canonical_block();
/* Raw bytes as the whole block. */
receive_stream(NULL, 0, data, size);
/* Valid framing so the fuzzer mutates the entry list, the first value and
* the second entry respectively instead of stopping at the count. */
receive_stream(g_block, g_off_after_entry0, data, size);
receive_stream(g_block, g_off_value0, data, size);
receive_stream(g_block, g_off_after_count, data, size);
return 0;
}
+17 -2
View File
@@ -51,6 +51,7 @@ READONLY_MODULE = os.path.join(MODULE_ROOT, "readonly")
AUTH_MODULE = os.path.join(MODULE_ROOT, "auth") AUTH_MODULE = os.path.join(MODULE_ROOT, "auth")
TEAM_MODULE = os.path.join(MODULE_ROOT, "team") TEAM_MODULE = os.path.join(MODULE_ROOT, "team")
OWNER_MODULE = os.path.join(MODULE_ROOT, "owner") OWNER_MODULE = os.path.join(MODULE_ROOT, "owner")
DENIED_MODULE = os.path.join(MODULE_ROOT, "denied")
CONF_FILE = os.path.join(TEST_DATA_DIR, "fastsyncd.conf") CONF_FILE = os.path.join(TEST_DATA_DIR, "fastsyncd.conf")
CRED_FILE = os.path.join(TEST_DATA_DIR, "fastsyncd.passwd") CRED_FILE = os.path.join(TEST_DATA_DIR, "fastsyncd.passwd")
STARTFAIL_CONF = os.path.join(TEST_DATA_DIR, "fastsyncd_startfail.conf") STARTFAIL_CONF = os.path.join(TEST_DATA_DIR, "fastsyncd_startfail.conf")
@@ -191,7 +192,7 @@ def _config_port(config_path):
@pytest.fixture(scope="module", autouse=True) @pytest.fixture(scope="module", autouse=True)
def daemon_env(): def daemon_env():
for d in (MODULE_ROOT, FILES_MODULE, READONLY_MODULE, AUTH_MODULE, TEAM_MODULE, OWNER_MODULE, for d in (MODULE_ROOT, FILES_MODULE, READONLY_MODULE, AUTH_MODULE, TEAM_MODULE, OWNER_MODULE,
DETACH_MODULE): DENIED_MODULE, DETACH_MODULE):
shutil.rmtree(d, ignore_errors=True) shutil.rmtree(d, ignore_errors=True)
os.makedirs(d, exist_ok=True) os.makedirs(d, exist_ok=True)
generate_test_files(SOURCE_DIR, full=False) generate_test_files(SOURCE_DIR, full=False)
@@ -231,7 +232,12 @@ def daemon_env():
"[owner]\n" "[owner]\n"
"path = %s\n" "path = %s\n"
"client owner = yes\n" "client owner = yes\n"
% (config_port, FILES_MODULE, READONLY_MODULE, AUTH_MODULE, TEAM_MODULE, OWNER_MODULE)) "\n"
"[denied]\n"
"path = %s\n"
"hosts deny = 127.0.0.1\n"
% (config_port, FILES_MODULE, READONLY_MODULE, AUTH_MODULE, TEAM_MODULE, OWNER_MODULE,
DENIED_MODULE))
# A dedicated config for the fail-closed startup check: an auth-required # A dedicated config for the fail-closed startup check: an auth-required
# module with no credential store must refuse to start. Its own free port # module with no credential store must refuse to start. Its own free port
@@ -368,6 +374,15 @@ class TestDaemonRejection:
result = _push("127.0.0.1::/sub", daemon.port) result = _push("127.0.0.1::/sub", daemon.port)
assert result.returncode != 0 assert result.returncode != 0
@pytest.mark.ci
def test_hosts_deny_rejects_loopback(self, daemon):
"""Host access control: a module with `hosts deny = 127.0.0.1` refuses a
loopback client at the config gate, before any data is exchanged."""
before = self._tree_files()
result = _push("127.0.0.1::denied", daemon.port)
assert result.returncode != 0
assert self._tree_files() == before, "host-denied connection wrote under the module root"
def test_dotdot_destination_rejected(self, daemon): def test_dotdot_destination_rejected(self, daemon):
"""A '..' path expansion in the module-relative path is refused at parse """A '..' path expansion in the module-relative path is refused at parse
time so a client cannot escape the module root while it is still on the time so a client cannot escape the module root while it is still on the
+367
View File
@@ -0,0 +1,367 @@
"""Fault injection: the server must survive truncated / corrupted protocol
frames and abrupt mid-frame disconnects, and keep serving later connections.
These tests deliberately speak raw bytes to a real server process:
* malformed frames before/inside the config handshake (oversized length
headers, truncated string bodies, outright garbage),
* a captured *valid* config frame replayed so the connection reaches the
operation loop, followed by a partial ``STATUS_MANIFEST`` frame that is cut
mid-body and dropped, and
* a real client run relayed through a proxy that truncates the stream at a
range of byte offsets and resets both ends.
After every fault the server process is asserted alive and a subsequent
ordinary transfer must complete and verify, proving the accept loop and
per-connection children recovered cleanly. All interactions are bounded by
short socket timeouts (no sleeps).
"""
import os
import select
import shutil
import socket
import struct
import subprocess
import sys
import threading
import pytest
sys.path.insert(0, os.path.dirname(__file__))
from common import ( # noqa: E402
ServerManager,
TEST_DATA_DIR,
get_dest_received_dir,
run_client,
verify_transfer,
)
PROTOCOL_VERSION = b"2.20.0"
STATUS_MANIFEST = 5
STATUS_OK = 0
SOURCE_DIR = os.path.join(TEST_DATA_DIR, "fault_src")
DEST_DIR = os.path.join(TEST_DATA_DIR, "fault_dst")
@pytest.fixture(scope="module")
def fault_server():
"""A dedicated server so the aliveness assertions observe exactly the
process these faults were sent to."""
server = ServerManager()
server.start()
yield server
server.stop()
@pytest.fixture(scope="module", autouse=True)
def _seed_source():
if os.path.exists(SOURCE_DIR):
shutil.rmtree(SOURCE_DIR)
os.makedirs(os.path.join(SOURCE_DIR, "nested"))
with open(os.path.join(SOURCE_DIR, "hello.txt"), "wb") as fh:
fh.write(b"fault injection payload\n" * 64)
with open(os.path.join(SOURCE_DIR, "nested", "deep.bin"), "wb") as fh:
fh.write(bytes(range(256)) * 16)
yield
shutil.rmtree(SOURCE_DIR, ignore_errors=True)
shutil.rmtree(DEST_DIR, ignore_errors=True)
def _assert_alive(server):
assert server._proc is not None, "server process missing"
assert server._proc.poll() is None, (
f"server exited with {server._proc.returncode} after fault injection"
)
def _recover(server, label):
"""Run one ordinary transfer and verify it end-to-end."""
shutil.rmtree(DEST_DIR, ignore_errors=True)
os.makedirs(DEST_DIR)
result, _ = run_client(SOURCE_DIR, DEST_DIR, flags=["--preserve"], port=server.port)
assert result.returncode == 0, (
f"{label}: recovery transfer failed rc={result.returncode}: "
f"{(result.stderr or result.stdout)[:200]}"
)
received = get_dest_received_dir(DEST_DIR, SOURCE_DIR)
mismatches, missing = verify_transfer(SOURCE_DIR, received)
assert not missing, f"{label}: recovery missing {missing}"
assert not mismatches, f"{label}: recovery mismatch {mismatches}"
def _abrupt_close(sock):
"""Force an RST instead of a graceful FIN, the nastier mid-frame drop."""
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))
except OSError:
pass
try:
sock.close()
except OSError:
pass
def _raw_connect(server):
sock = socket.create_connection(("127.0.0.1", server.port), timeout=5)
sock.settimeout(5)
return sock
def _recv_exact(sock, n):
buf = b""
while len(buf) < n:
chunk = sock.recv(n - len(buf))
if not chunk:
return None
buf += chunk
return buf
# --- faults before/inside the config handshake -----------------------------
CONFIG_HANDSHAKE_FAULTS = {
"empty": b"",
# Length header claims a 1 EiB string body that never arrives.
"oversized_length": struct.pack("<Q", 1 << 60),
# A truncated 8-byte length header (only 3 bytes of it are sent).
"truncated_length_header": b"\x10\x00\x00",
# A valid version string followed by a string-length header whose body is
# deliberately truncated (mid-config-frame disconnect).
"truncated_config_body": struct.pack("<Q", len(PROTOCOL_VERSION)) + PROTOCOL_VERSION
+ struct.pack("<Q", 4096)
+ b"partial",
# Pure garbage that is not a valid frame at any offset.
"garbage": b"\xff" * 32,
}
class TestConfigHandshakeFaults:
def test_truncated_and_corrupt_config_frames(self, fault_server):
for name, payload in CONFIG_HANDSHAKE_FAULTS.items():
sock = _raw_connect(fault_server)
if payload:
sock.sendall(payload)
_abrupt_close(sock)
_assert_alive(fault_server)
_recover(fault_server, "config handshake faults")
# --- capture a valid config frame, then truncate a STATUS_MANIFEST ----------
class _CaptureProxy:
"""Relay one client<->server connection and record the client's config
frame (all client bytes forwarded before the server's first reply)."""
def __init__(self, target_port):
self.target = ("127.0.0.1", target_port)
self.listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.listener.bind(("127.0.0.1", 0))
self.listener.listen(1)
self.listener.settimeout(20)
self.port = self.listener.getsockname()[1]
self.config_frame = None
def run(self, cmd):
def serve():
try:
client, _ = self.listener.accept()
except OSError:
return
try:
backend = socket.create_connection(self.target, timeout=10)
except OSError:
client.close()
return
client.settimeout(20)
backend.settimeout(20)
buf_c = bytearray()
seen_server = False
try:
while True:
ready, _, _ = select.select([client, backend], [], [], 20)
if not ready:
break
done = False
for sock in ready:
data = sock.recv(65536)
if not data:
done = True
continue
if sock is client:
buf_c += data
backend.sendall(data)
else:
if not seen_server:
seen_server = True
self.config_frame = bytes(buf_c)
client.sendall(data)
if done:
break
except OSError:
pass
finally:
client.close()
backend.close()
thread = threading.Thread(target=serve)
thread.start()
result = subprocess.run(cmd, capture_output=True, text=True, timeout=60)
thread.join(20)
return result
def close(self):
try:
self.listener.close()
except OSError:
pass
@pytest.fixture(scope="module")
def captured_config(fault_server):
"""Capture the config frame of one real client run through a relay."""
proxy = _CaptureProxy(fault_server.port)
cmd = [
os.path.join(os.path.dirname(__file__), "..", "..", "build", "client"),
"--source-dir",
SOURCE_DIR,
"--dest-dir",
DEST_DIR,
"--save-to-disk",
"--server-port",
str(proxy.port),
]
try:
result = proxy.run(cmd)
assert result.returncode == 0, (
f"capture run failed rc={result.returncode}: "
f"{(result.stderr or result.stdout)[:200]}"
)
assert proxy.config_frame, "failed to capture the client config frame"
yield proxy.config_frame
finally:
proxy.close()
class TestTruncatedStatusFrame:
def test_partial_manifest_frame_then_drop(self, fault_server, captured_config):
sock = _raw_connect(fault_server)
sock.sendall(captured_config)
ack = _recv_exact(sock, 4)
assert ack is not None, "server closed before the config ack"
(status,) = struct.unpack("<i", ack)
assert status == STATUS_OK, f"expected STATUS_OK, got {status}"
# STATUS_MANIFEST, then only half of the keep-count int, then an RST.
sock.sendall(struct.pack("<i", STATUS_MANIFEST) + b"\x02\x00")
_abrupt_close(sock)
_assert_alive(fault_server)
_recover(fault_server, "truncated manifest frame")
def test_manifest_count_without_sections(self, fault_server, captured_config):
"""A syntactically valid STATUS_MANIFEST whose bodies never arrive."""
sock = _raw_connect(fault_server)
sock.sendall(captured_config)
assert _recv_exact(sock, 4) is not None
sock.sendall(struct.pack("<i", STATUS_MANIFEST) + struct.pack("<i", 3))
# Announce three keeps but send none; then drop.
_abrupt_close(sock)
_assert_alive(fault_server)
_recover(fault_server, "manifest body truncation")
# --- abrupt truncation of a real transfer ----------------------------------
class _TruncatingProxy:
"""Forward at most ``max_client_bytes`` from client to server, then reset
both ends mid-stream. Runs one client command (which is expected to fail)."""
def __init__(self, target_port, max_client_bytes):
self.target = ("127.0.0.1", target_port)
self.max_client_bytes = max_client_bytes
self.listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.listener.bind(("127.0.0.1", 0))
self.listener.listen(1)
self.listener.settimeout(20)
self.port = self.listener.getsockname()[1]
def run(self, cmd):
def serve():
try:
client, _ = self.listener.accept()
except OSError:
return
try:
backend = socket.create_connection(self.target, timeout=10)
except OSError:
client.close()
return
# A short receive timeout bounds the case where the client has
# nothing left to send and is waiting on the server: the proxy then
# cuts the stream anyway instead of stalling the test.
client.settimeout(2)
backend.settimeout(20)
forwarded = 0
try:
while forwarded < self.max_client_bytes:
data = client.recv(65536)
if not data:
break
room = self.max_client_bytes - forwarded
take = data[:room]
backend.sendall(take)
forwarded += len(take)
if forwarded >= self.max_client_bytes:
break
except (OSError, socket.timeout):
pass
for sock in (client, backend):
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))
except OSError:
pass
try:
sock.close()
except OSError:
pass
thread = threading.Thread(target=serve)
thread.start()
try:
subprocess.run(cmd, capture_output=True, text=True, timeout=30)
finally:
thread.join(20)
self.listener.close()
class TestAbruptMidTransferDisconnect:
def test_client_stream_cut_at_offsets(self, fault_server, captured_config):
"""Cut the real client stream at offsets anchored to the config frame's
actual size: mid-config, right after the config, and into the operation
stream -- each followed by an RST of both ends."""
config_len = len(captured_config)
cuts = sorted({max(1, config_len // 2), max(1, config_len - 1), config_len + 8,
config_len + 256})
for cut in cuts:
proxy = _TruncatingProxy(fault_server.port, cut)
cmd = [
os.path.join(os.path.dirname(__file__), "..", "..", "build", "client"),
"--source-dir",
SOURCE_DIR,
"--dest-dir",
DEST_DIR,
"--save-to-disk",
"--server-port",
str(proxy.port),
]
# The client is expected to fail; what matters is the server survives.
proxy.run(cmd)
_assert_alive(fault_server)
_recover(fault_server, "abrupt mid-transfer disconnects")
+14
View File
@@ -1239,6 +1239,20 @@ class TestProgress:
assert "Stats:" in result.stderr assert "Stats:" in result.stderr
assert "KB" in result.stderr assert "KB" in result.stderr
def test_human_readable_stats_multithreaded(self, shared_server):
# The multithreaded sender shares the single-threaded --stats format,
# including --human-readable and the rate suffix.
clean_dir(DEST_DIR)
result, dur = run_client(
SOURCE_DIR, DEST_DIR,
flags=["--threads", "-h", "--stats"],
port=shared_server.port,
)
assert result.returncode == 0, f"Exit {result.returncode}: {result.stderr[:100]}"
assert "Stats:" in result.stderr
assert "KB" in result.stderr
assert "/s" in result.stderr
def test_human_readable_progress_multithreaded(self, shared_server): def test_human_readable_progress_multithreaded(self, shared_server):
clean_dir(DEST_DIR) clean_dir(DEST_DIR)
result, dur = run_client( result, dur = run_client(
+3 -3
View File
@@ -94,14 +94,14 @@ def _seed_protocol_source(source):
class TestProtocol: class TestProtocol:
@pytest.mark.ci @pytest.mark.ci
def test_protocol_current_version_accepted(self, shared_server): def test_protocol_current_version_accepted(self, shared_server):
"""--protocol=2.19.0 (the current PROTOCOL_VERSION) is accepted and the """--protocol=2.20.0 (the current PROTOCOL_VERSION) is accepted and the
transfer completes normally.""" transfer completes normally."""
source = os.path.join(TEST_DATA_DIR, "proto_ok_src") source = os.path.join(TEST_DATA_DIR, "proto_ok_src")
dest = os.path.join(TEST_DATA_DIR, "proto_ok_dst") dest = os.path.join(TEST_DATA_DIR, "proto_ok_dst")
shutil.rmtree(dest, ignore_errors=True) shutil.rmtree(dest, ignore_errors=True)
os.makedirs(dest) os.makedirs(dest)
_seed_protocol_source(source) _seed_protocol_source(source)
result, _ = run_client(source, dest, flags=["--protocol=2.19.0"], result, _ = run_client(source, dest, flags=["--protocol=2.20.0"],
port=shared_server.port) port=shared_server.port)
assert result.returncode == 0, \ assert result.returncode == 0, \
f"--protocol current run failed: {(result.stderr or result.stdout)[:400]}" f"--protocol current run failed: {(result.stderr or result.stdout)[:400]}"
@@ -118,7 +118,7 @@ class TestProtocol:
shutil.rmtree(dest, ignore_errors=True) shutil.rmtree(dest, ignore_errors=True)
os.makedirs(dest) os.makedirs(dest)
_seed_protocol_source(source) _seed_protocol_source(source)
for bad in ("2.18.0", "2.17.0", "2.15.0", "2.16.0", "216", "31"): for bad in ("2.19.0", "2.18.0", "2.17.0", "2.15.0", "2.16.0", "216", "31"):
result, _ = run_client(source, dest, flags=[f"--protocol={bad}"], result, _ = run_client(source, dest, flags=[f"--protocol={bad}"],
port=shared_server.port) port=shared_server.port)
assert result.returncode != 0, f"--protocol={bad} should be rejected" assert result.returncode != 0, f"--protocol={bad} should be rejected"
+6
View File
@@ -12,9 +12,11 @@
#include "test_delay_updates.h" #include "test_delay_updates.h"
#include "test_delta.h" #include "test_delta.h"
#include "test_file.h" #include "test_file.h"
#include "test_file_list.h"
#include "test_file_sendfile.h" #include "test_file_sendfile.h"
#include "test_fuzz_smoke.h" #include "test_fuzz_smoke.h"
#include "test_glob.h" #include "test_glob.h"
#include "test_hardlink.h"
#include "test_iconv.h" #include "test_iconv.h"
#include "test_log.h" #include "test_log.h"
#include "test_metadata.h" #include "test_metadata.h"
@@ -23,6 +25,7 @@
#include "test_property.h" #include "test_property.h"
#include "test_protocol.h" #include "test_protocol.h"
#include "test_queue.h" #include "test_queue.h"
#include "test_receiver_timeout.h"
#include "test_robustness.h" #include "test_robustness.h"
#include "test_scanner.h" #include "test_scanner.h"
#include "test_server.h" #include "test_server.h"
@@ -61,10 +64,12 @@ int main() {
RUN_TEST(test_delta); RUN_TEST(test_delta);
RUN_TEST(test_data); RUN_TEST(test_data);
RUN_TEST(test_protocol); RUN_TEST(test_protocol);
RUN_TEST(test_receiver_timeout);
RUN_TEST(test_metadata); RUN_TEST(test_metadata);
RUN_TEST(test_glob); RUN_TEST(test_glob);
RUN_TEST(test_iconv); RUN_TEST(test_iconv);
RUN_TEST(test_file); RUN_TEST(test_file);
RUN_TEST(test_file_list);
RUN_TEST(test_trust_sender); RUN_TEST(test_trust_sender);
RUN_TEST(test_delay_updates); RUN_TEST(test_delay_updates);
RUN_TEST(test_file_sendfile); RUN_TEST(test_file_sendfile);
@@ -84,6 +89,7 @@ int main() {
RUN_TEST(test_server_cli); RUN_TEST(test_server_cli);
RUN_TEST(test_fuzz_smoke); RUN_TEST(test_fuzz_smoke);
RUN_TEST(test_xattr); RUN_TEST(test_xattr);
RUN_TEST(test_hardlink);
printf("\n\033[1;36m=== TEST SUMMARY ===\033[0m\n"); printf("\n\033[1;36m=== TEST SUMMARY ===\033[0m\n");
printf("Total Tests Run: %d\n", tests_run); printf("Total Tests Run: %d\n", tests_run);
+19 -1
View File
@@ -1,6 +1,7 @@
#include "test_array_list.h" #include "test_array_list.h"
#include "array_list.h" #include "array_list.h"
#include "test_utils.h" #include "test_utils.h"
#include <limits.h>
#include <stdlib.h> #include <stdlib.h>
static int destroyer_calls = 0; static int destroyer_calls = 0;
@@ -9,7 +10,7 @@ static void test_destroyer(void* item) {
free(item); free(item);
} }
void test_array_list() { static void test_array_list_basic() {
ArrayList* list = array_list_create(free); ArrayList* list = array_list_create(free);
EXPECT_NOT_NULL(list); EXPECT_NOT_NULL(list);
EXPECT_EQ_INT(list->size, 0); EXPECT_EQ_INT(list->size, 0);
@@ -54,3 +55,20 @@ void test_array_list() {
array_list_delete(list); array_list_delete(list);
EXPECT_EQ_INT(destroyer_calls, 106); EXPECT_EQ_INT(destroyer_calls, 106);
} }
/* A capacity that would overflow `capacity * 2` must be refused instead of
* wrapping into signed-overflow UB; array_list_add surfaces the failure. */
static void test_array_list_extend_overflow_guard() {
ArrayList* list = array_list_create(NULL);
EXPECT_NOT_NULL(list);
list->capacity = INT_MAX / 2 + 1;
list->size = list->capacity;
EXPECT_FALSE(array_list_add(list, NULL));
list->size = 0;
array_list_delete(list);
}
void test_array_list() {
test_array_list_basic();
test_array_list_extend_overflow_guard();
}
+129 -4
View File
@@ -1,5 +1,6 @@
#include "test_client_cli.h" #include "test_client_cli.h"
#include "checksum.h" #include "checksum.h"
#include "client_send.h"
#include "client_validation.h" #include "client_validation.h"
#include "chmod.h" #include "chmod.h"
#include "config.h" #include "config.h"
@@ -122,6 +123,52 @@ static void test_validate_config_delta_sendfile_constraints() {
config_delete(cfg); config_delete(cfg);
} }
/* The client must still reject every combination now enforced by the shared
config_invariants_error() predicate (the server trusts the same rules). */
static void test_validate_config_unified_invariants() {
Config* cfg = valid_client_config();
cfg->use_incremental = true;
cfg->use_delta = true;
cfg->use_chunk_serialization = true;
EXPECT_FALSE(validate_config(cfg)); /* delta + chunk */
config_delete(cfg);
cfg = valid_client_config();
cfg->use_delta = true; /* whole_file false */
EXPECT_FALSE(validate_config(cfg)); /* delta without incremental */
config_delete(cfg);
cfg = valid_client_config();
cfg->use_sendfile = true;
cfg->use_chunk_serialization = true;
EXPECT_FALSE(validate_config(cfg)); /* sendfile + chunk */
config_delete(cfg);
cfg = valid_client_config();
cfg->preserve_hard_links = true;
cfg->use_chunk_serialization = true;
EXPECT_FALSE(validate_config(cfg)); /* hard-links + chunk */
config_delete(cfg);
cfg = valid_client_config();
cfg->preserve_hard_links = true;
cfg->append = true;
EXPECT_FALSE(validate_config(cfg)); /* hard-links + append */
config_delete(cfg);
cfg = valid_client_config();
cfg->append = true;
cfg->whole_file = true;
EXPECT_FALSE(validate_config(cfg)); /* append + whole-file */
config_delete(cfg);
cfg = valid_client_config();
cfg->preserve_xattrs = true;
cfg->use_chunk_serialization = true;
EXPECT_FALSE(validate_config(cfg)); /* xattrs + chunk */
config_delete(cfg);
}
/* Test main() with --help flag (early return path, no server connection needed) */ /* Test main() with --help flag (early return path, no server connection needed) */
static void test_cli_help() { static void test_cli_help() {
/* We can't easily call main() because it calls send_files which needs a server. /* We can't easily call main() because it calls send_files which needs a server.
@@ -259,7 +306,7 @@ static void test_parse_args_protocol_accept_current() {
Config* cfg = valid_client_config(); Config* cfg = valid_client_config();
EXPECT_NOT_NULL(cfg); EXPECT_NOT_NULL(cfg);
char* argv_equals[] = {"fastsync", "--source-dir", "/src", char* argv_equals[] = {"fastsync", "--source-dir", "/src",
"--dest-dir", "/dst", "--protocol=2.19.0"}; "--dest-dir", "/dst", "--protocol=2.20.0"};
int positional_args[2]; int positional_args[2];
int positional_count = 0; int positional_count = 0;
EXPECT_EQ_INT(parse_args(cfg, 6, argv_equals, positional_args, &positional_count), 0); EXPECT_EQ_INT(parse_args(cfg, 6, argv_equals, positional_args, &positional_count), 0);
@@ -269,7 +316,7 @@ static void test_parse_args_protocol_accept_current() {
cfg = valid_client_config(); cfg = valid_client_config();
EXPECT_NOT_NULL(cfg); EXPECT_NOT_NULL(cfg);
char* argv_space[] = {"fastsync", "--source-dir", "/src", "--dest-dir", char* argv_space[] = {"fastsync", "--source-dir", "/src", "--dest-dir",
"/dst", "--protocol", "2.19.0"}; "/dst", "--protocol", "2.20.0"};
positional_count = 0; positional_count = 0;
EXPECT_EQ_INT(parse_args(cfg, 7, argv_space, positional_args, &positional_count), 0); EXPECT_EQ_INT(parse_args(cfg, 7, argv_space, positional_args, &positional_count), 0);
EXPECT_EQ_STR(cfg->version, PROTOCOL_VERSION); EXPECT_EQ_STR(cfg->version, PROTOCOL_VERSION);
@@ -279,8 +326,8 @@ static void test_parse_args_protocol_accept_current() {
/* Any --protocol value other than the current PROTOCOL_VERSION must end in /* Any --protocol value other than the current PROTOCOL_VERSION must end in
* failure (parse_args simply stores it; validate_config rejects it up front). */ * failure (parse_args simply stores it; validate_config rejects it up front). */
static void test_parse_args_protocol_rejects_other_versions() { static void test_parse_args_protocol_rejects_other_versions() {
static const char* const bad_versions[] = {"2.17", "2.16", "2.15.0", "2.16.0", "2.17.0", static const char* const bad_versions[] = {
"2.18.0", "216", "31", "abc", ""}; "2.17", "2.16", "2.15.0", "2.16.0", "2.17.0", "2.18.0", "2.19.0", "216", "31", "abc", ""};
for (size_t i = 0; i < sizeof(bad_versions) / sizeof(bad_versions[0]); i++) { for (size_t i = 0; i < sizeof(bad_versions) / sizeof(bad_versions[0]); i++) {
Config* cfg = valid_client_config(); Config* cfg = valid_client_config();
EXPECT_NOT_NULL(cfg); EXPECT_NOT_NULL(cfg);
@@ -533,6 +580,80 @@ static void test_parse_args_invalid_server_port() {
config_delete(cfg); config_delete(cfg);
} }
/* --port is a documented rsync-style alias for --server-port; both the
* two-argument and the inline "=" spellings must work. */
static void test_parse_args_port_alias() {
Config* cfg = config_create();
char* argv_space[] = {"fastsync", "--port", "9000", "/src", "/dst"};
int positional_args[2];
int positional_count = 0;
EXPECT_EQ_INT(parse_args(cfg, 5, argv_space, positional_args, &positional_count), 0);
EXPECT_EQ_INT(cfg->server_port, 9000);
config_delete(cfg);
cfg = config_create();
char* argv_inline[] = {"fastsync", "--port=9001", "/src", "/dst"};
positional_count = 0;
EXPECT_EQ_INT(parse_args(cfg, 4, argv_inline, positional_args, &positional_count), 0);
EXPECT_EQ_INT(cfg->server_port, 9001);
config_delete(cfg);
cfg = config_create();
char* argv_long[] = {"fastsync", "--server-port=9002", "/src", "/dst"};
positional_count = 0;
EXPECT_EQ_INT(parse_args(cfg, 4, argv_long, positional_args, &positional_count), 0);
EXPECT_EQ_INT(cfg->server_port, 9002);
config_delete(cfg);
}
/* --threads=N sizes the pipeline scanner; bare -j/--threads keeps the default
* (scanner_threads == 0), and invalid values are rejected. */
static void test_parse_args_threads() {
Config* cfg = config_create();
char* argv_eq[] = {"fastsync", "--threads=8", "/src", "/dst"};
int positional_args[2];
int positional_count = 0;
EXPECT_EQ_INT(parse_args(cfg, 4, argv_eq, positional_args, &positional_count), 0);
EXPECT_TRUE(cfg->use_multithreading);
EXPECT_EQ_INT(cfg->scanner_threads, 8);
config_delete(cfg);
cfg = config_create();
char* argv_short[] = {"fastsync", "-j", "/src", "/dst"};
positional_count = 0;
EXPECT_EQ_INT(parse_args(cfg, 4, argv_short, positional_args, &positional_count), 0);
EXPECT_TRUE(cfg->use_multithreading);
EXPECT_EQ_INT(cfg->scanner_threads, 0);
config_delete(cfg);
cfg = config_create();
char* argv_long[] = {"fastsync", "--threads", "/src", "/dst"};
positional_count = 0;
EXPECT_EQ_INT(parse_args(cfg, 4, argv_long, positional_args, &positional_count), 0);
EXPECT_TRUE(cfg->use_multithreading);
EXPECT_EQ_INT(cfg->scanner_threads, 0);
config_delete(cfg);
const char* bad[] = {"--threads=0", "--threads=-3", "--threads=abc", "--threads=257"};
for (size_t i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) {
cfg = config_create();
char* argv_bad[] = {"fastsync", (char*)bad[i], "/src", "/dst"};
positional_count = 0;
EXPECT_EQ_INT(parse_args(cfg, 4, argv_bad, positional_args, &positional_count), -1);
config_delete(cfg);
}
}
/* The graceful-abort flag is a plain sig_atomic_t toggled by the handler. */
static void test_client_abort_flag() {
client_abort_requested = 0;
EXPECT_FALSE(client_abort_pending());
client_abort_requested = 1;
EXPECT_TRUE(client_abort_pending());
client_abort_requested = 0;
EXPECT_FALSE(client_abort_pending());
}
/* Test parse_args rejects invalid compression level (-z/--compress) */ /* Test parse_args rejects invalid compression level (-z/--compress) */
static void test_parse_args_invalid_compression_level() { static void test_parse_args_invalid_compression_level() {
Config* cfg = config_create(); Config* cfg = config_create();
@@ -3155,6 +3276,7 @@ void test_client_cli() {
test_validate_config_tls_requirements(); test_validate_config_tls_requirements();
test_validate_config_credentials_require_tls_or_loopback(); test_validate_config_credentials_require_tls_or_loopback();
test_validate_config_delta_sendfile_constraints(); test_validate_config_delta_sendfile_constraints();
test_validate_config_unified_invariants();
test_cli_help(); test_cli_help();
test_cli_archive_flags(); test_cli_archive_flags();
test_cli_dry_run(); test_cli_dry_run();
@@ -3177,6 +3299,9 @@ void test_client_cli() {
test_parse_args_invalid_port(); test_parse_args_invalid_port();
test_parse_args_non_numeric_port(); test_parse_args_non_numeric_port();
test_parse_args_invalid_server_port(); test_parse_args_invalid_server_port();
test_parse_args_port_alias();
test_parse_args_threads();
test_client_abort_flag();
test_parse_args_invalid_compression_level(); test_parse_args_invalid_compression_level();
test_parse_args_valid_compression_level(); test_parse_args_valid_compression_level();
test_parse_args_debug_flags(); test_parse_args_debug_flags();
+75
View File
@@ -6,6 +6,7 @@
#include "utils.h" #include "utils.h"
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <threads.h>
#include <unistd.h> #include <unistd.h>
static void test_data_compress_decompress_roundtrip() { static void test_data_compress_decompress_roundtrip() {
@@ -137,10 +138,84 @@ static void test_chunk_compress_decompress_roundtrip() {
unlink(path2); unlink(path2);
} }
typedef struct {
int id;
int iterations;
bool ok;
} CompressionThreadArg;
/* Each worker exercises the per-thread cached zstd contexts: several
* compress/decompress round-trips with varying payload sizes, levels and
* worker counts so the context is reused (and its parameters re-applied)
* across calls, concurrently with other workers. */
static int compression_reuse_worker(void* arg) {
CompressionThreadArg* a = (CompressionThreadArg*)arg;
a->ok = true;
for (int it = 0; it < a->iterations; it++) {
size_t size = 512 + (size_t)((a->id * 7919 + it * 104729) % (48 * 1024));
char* original = malloc(size);
if (!original) {
a->ok = false;
break;
}
for (size_t i = 0; i < size; i++)
original[i] = (char)((i * 31 + (size_t)a->id + (size_t)it * 7) % 251);
Data* input = data_create(original, size);
if (!input) { /* data_create takes ownership of original, even on failure */
a->ok = false;
break;
}
int level = 1 + ((it / 2) % 5);
int threads = ((it / 2) % 2 == 0) ? 2 : 0;
Data* compressed = data_compress_with_threads(input, level, threads);
if (!compressed) {
data_destroy(input);
a->ok = false;
break;
}
Data* decompressed = data_decompress(compressed);
bool roundtrip_ok = decompressed != NULL && decompressed->size == size &&
memcmp(decompressed->data, original, size) == 0;
data_destroy(decompressed);
data_destroy(compressed);
data_destroy(input);
if (!roundtrip_ok) {
a->ok = false;
break;
}
}
/* Deliberately do NOT free the thread context here: the C11 tss destructor
* must release it when this thread exits (validated by LeakSanitizer). */
return thrd_success;
}
static void test_data_compress_reused_contexts_multithreaded() {
enum { NTHREADS = 8, ITERATIONS = 6 };
thrd_t threads[NTHREADS];
CompressionThreadArg args[NTHREADS];
bool all_created = true;
for (int i = 0; i < NTHREADS; i++) {
args[i].id = i;
args[i].iterations = ITERATIONS;
args[i].ok = false;
if (thrd_create(&threads[i], compression_reuse_worker, &args[i]) != thrd_success) {
all_created = false;
break;
}
}
EXPECT_TRUE(all_created);
for (int i = 0; i < NTHREADS; i++)
EXPECT_EQ_INT(thrd_join(threads[i], NULL), thrd_success);
for (int i = 0; i < NTHREADS; i++)
EXPECT_TRUE(args[i].ok);
compression_free_thread_contexts();
}
void test_compression() { void test_compression() {
test_data_compress_decompress_roundtrip(); test_data_compress_decompress_roundtrip();
test_data_compress_decompress_large(); test_data_compress_decompress_large();
test_skip_compress_suffix_matching(); test_skip_compress_suffix_matching();
test_data_compress_with_threads_roundtrip(); test_data_compress_with_threads_roundtrip();
test_data_compress_reused_contexts_multithreaded();
test_chunk_compress_decompress_roundtrip(); test_chunk_compress_decompress_roundtrip();
} }
+240 -1
View File
@@ -4,8 +4,10 @@
#include "multiprocessing.h" #include "multiprocessing.h"
#include "protocol.h" #include "protocol.h"
#include "queue.h" #include "queue.h"
#include "receiver_pipeline.h"
#include "test_utils.h" #include "test_utils.h"
#include "utils.h" #include "utils.h"
#include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <string.h> #include <string.h>
@@ -382,6 +384,7 @@ static void test_pipeline_sender_lifecycle() {
EXPECT_EQ_INT((int)pcs->allocation_session.max_alloc, (int)cfg->max_alloc); EXPECT_EQ_INT((int)pcs->allocation_session.max_alloc, (int)cfg->max_alloc);
pipeline_context_sender_destroy(pcs); pipeline_context_sender_destroy(pcs);
config_delete(cfg); /* the context borrows cfg; the caller owns it */
} }
static void test_pipeline_receiver_lifecycle() { static void test_pipeline_receiver_lifecycle() {
@@ -1672,7 +1675,7 @@ static void test_config_receive_rejects_invalid_iconv_spec() {
static void test_config_super_mode_wire_roundtrip() { static void test_config_super_mode_wire_roundtrip() {
if (is_running_under_valgrind()) if (is_running_under_valgrind())
return; return;
int modes[] = {SUPER_MODE_AUTO, SUPER_MODE_ON, SUPER_MODE_OFF}; SuperMode modes[] = {SUPER_MODE_AUTO, SUPER_MODE_ON, SUPER_MODE_OFF};
for (size_t i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) { for (size_t i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
int p[2]; int p[2];
EXPECT_EQ_INT(socketpair(AF_UNIX, SOCK_STREAM, 0, p), 0); EXPECT_EQ_INT(socketpair(AF_UNIX, SOCK_STREAM, 0, p), 0);
@@ -1846,6 +1849,89 @@ static void test_config_receive_rejects_copy_as_without_metadata() {
config_delete(c); config_delete(c);
} }
/* Like roundtrip_config_ok, but the parent is the RECEIVER so the frame can be
rejected MID-way, before the sender finishes writing it. The sender child
ignores SIGPIPE so the receiver closing early cannot kill it; the parent
waits for the child to exit after observing the rejection. */
static bool roundtrip_config_rejected(const Config* send_cfg) {
int p[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, p) != 0)
return false;
pid_t pid = fork();
if (pid == 0) {
(void)signal(SIGPIPE, SIG_IGN);
close(p[0]);
io_set_fds(p[1], p[1]);
config_send(p[1], send_cfg);
close(p[1]);
_exit(0);
}
close(p[1]);
io_set_fds(p[0], p[0]);
Config* recv = config_receive(p[0]);
bool rejected = recv == NULL;
config_delete(recv);
close(p[0]);
int status;
waitpid(pid, &status, 0);
return rejected;
}
/* Build a Config with `count` --skip-compress suffixes, each `suffix_len` bytes
long, for the pre-auth config-string budget tests. */
static Config* make_skip_compress_config(int count, size_t suffix_len) {
Config* c = config_create();
if (!c)
return NULL;
c->send_directory = str_dup("/src");
c->receive_root_directory = str_dup("/dst");
c->skip_compress_set = true;
c->skip_compress_count = count;
c->skip_compress_suffixes = calloc((size_t)count, sizeof(char*));
if (!c->skip_compress_suffixes) {
config_delete(c);
return NULL;
}
char* suffix = malloc(suffix_len + 1);
if (!suffix) {
config_delete(c);
return NULL;
}
memset(suffix, 'x', suffix_len);
suffix[suffix_len] = '\0';
for (int i = 0; i < count; i++)
c->skip_compress_suffixes[i] = str_dup(suffix);
free(suffix);
return c;
}
/* Pre-auth memory bound: one connection must not retain unbounded config
strings. An over-limit --skip-compress count is refused, and even an
in-range count cannot exceed the aggregate per-connection string budget. */
static void test_config_receive_rejects_oversized_string_budget() {
if (is_running_under_valgrind())
return;
/* Exactly MAX_SKIP_COMPRESS_SUFFIXES tiny suffixes are accepted. */
Config* ok = make_skip_compress_config(MAX_SKIP_COMPRESS_SUFFIXES, 1);
EXPECT_NOT_NULL(ok);
EXPECT_TRUE(roundtrip_config_ok(ok));
config_delete(ok);
/* One suffix over the count cap is rejected before any suffix is read. */
Config* over_count = make_skip_compress_config(MAX_SKIP_COMPRESS_SUFFIXES + 1, 1);
EXPECT_NOT_NULL(over_count);
EXPECT_TRUE(roundtrip_config_rejected(over_count));
config_delete(over_count);
/* In-range count, but the strings together exceed MAX_CONFIG_STRING_BYTES
(64 suffixes * ~64 KiB > 1 MiB), so the aggregate budget rejects it. */
Config* over_bytes = make_skip_compress_config(64, MAX_STRING_SIZE - 1);
EXPECT_NOT_NULL(over_bytes);
EXPECT_TRUE(roundtrip_config_rejected(over_bytes));
config_delete(over_bytes);
}
/* identity_copy_as_refused() is the pure, pre-snapshot refusal predicate: a /* identity_copy_as_refused() is the pure, pre-snapshot refusal predicate: a
--copy-as is refused when the receiver is not root OR the effective super --copy-as is refused when the receiver is not root OR the effective super
mode is OFF (an operator veto), and never when --copy-as is unset. */ mode is OFF (an operator veto), and never when --copy-as is unset. */
@@ -1958,6 +2044,156 @@ static void test_super_does_not_imply_numeric() {
config_delete(c); config_delete(c);
} }
/* The single shared predicate must reject every cross-field combination the
client/server enforce and accept a plain valid config. Because both
validate_config() (client) and validate_received_config() (server) call it,
this table documents the whole invariant set in one place. */
static void test_config_invariants_error_all_combinations() {
Config* c = config_create();
EXPECT_NOT_NULL(c);
c->send_directory = str_dup("/src");
c->receive_root_directory = str_dup("/dst");
EXPECT_NULL(config_invariants_error(c));
config_delete(c);
c = config_create();
EXPECT_EQ_INT(config_basis_append(c, BASIS_DEST_COMPARE, "sub"), 0);
c->use_chunk_serialization = true;
EXPECT_NOT_NULL(config_invariants_error(c)); /* basis + chunk */
config_delete(c);
c = config_create();
c->use_sendfile = true;
c->use_compression = true;
EXPECT_NOT_NULL(config_invariants_error(c)); /* sendfile + compression */
config_delete(c);
c = config_create();
c->use_sendfile = true;
c->use_chunk_serialization = true;
EXPECT_NOT_NULL(config_invariants_error(c)); /* sendfile + chunk */
config_delete(c);
c = config_create();
c->use_incremental = true;
c->use_chunk_serialization = true;
EXPECT_NOT_NULL(config_invariants_error(c)); /* incremental + chunk */
config_delete(c);
c = config_create();
c->skip_compress_set = true;
c->use_chunk_serialization = true;
EXPECT_NOT_NULL(config_invariants_error(c)); /* skip-compress + chunk */
config_delete(c);
c = config_create();
c->use_delta = true; /* whole_file false -> active */
EXPECT_NOT_NULL(config_invariants_error(c)); /* delta without incremental */
config_delete(c);
c = config_create();
c->use_delta = true;
c->use_incremental = true;
c->use_chunk_serialization = true;
EXPECT_NOT_NULL(config_invariants_error(c)); /* delta + chunk */
config_delete(c);
c = config_create();
c->use_delta = true;
c->use_incremental = true;
c->use_sendfile = true;
EXPECT_NOT_NULL(config_invariants_error(c)); /* delta + sendfile */
config_delete(c);
c = config_create();
c->append = true;
c->use_chunk_serialization = true;
EXPECT_NOT_NULL(config_invariants_error(c)); /* append + chunk */
config_delete(c);
c = config_create();
c->append = true;
c->whole_file = true;
EXPECT_NOT_NULL(config_invariants_error(c)); /* append + whole-file */
config_delete(c);
c = config_create();
c->preserve_hard_links = true;
c->use_chunk_serialization = true;
EXPECT_NOT_NULL(config_invariants_error(c)); /* hard-links + chunk */
config_delete(c);
c = config_create();
c->preserve_xattrs = true;
c->use_chunk_serialization = true;
EXPECT_NOT_NULL(config_invariants_error(c)); /* xattrs + chunk */
config_delete(c);
c = config_create();
c->preserve_hard_links = true;
c->append = true;
EXPECT_NOT_NULL(config_invariants_error(c)); /* hard-links + append */
config_delete(c);
c = config_create();
c->delay_updates = true;
c->inplace = true;
EXPECT_NOT_NULL(config_invariants_error(c)); /* delay-updates + inplace */
config_delete(c);
c = config_create();
c->delay_updates = true;
c->backup_dir = str_dup(".fastsync-stage");
EXPECT_NOT_NULL(config_invariants_error(c)); /* delay-updates staging conflict */
config_delete(c);
c = config_create();
c->delete_delay = true; /* a timing flag without --delete */
EXPECT_NOT_NULL(config_invariants_error(c)); /* invalid delete timing */
config_delete(c);
c = config_create();
c->iconv_spec = str_dup("no-such-charset,utf-8");
EXPECT_NOT_NULL(config_invariants_error(c)); /* malformed iconv spec */
config_delete(c);
c = config_create();
c->copy_as_set = true;
c->use_metadata = false;
EXPECT_NOT_NULL(config_invariants_error(c)); /* copy-as without metadata */
config_delete(c);
}
/* The receiver previously missed several of these; a forged frame that sets
the offending serialized fields must now be refused at the config
handshake. (whole_file is client-only, so its rules cannot appear here.) */
static void test_config_receive_rejects_unified_invariants() {
if (is_running_under_valgrind())
return;
struct {
bool incremental, delta, chunk, sendfile, compression;
} cases[] = {
{true, false, true, false, false}, /* --incremental + -s */
{false, true, true, false, false}, /* --delta + -s */
{false, true, false, false, false}, /* --delta without --incremental */
{false, false, false, true, true}, /* --sendfile + compression */
{false, false, true, true, false}, /* --sendfile + -s */
};
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
Config* c = config_create();
EXPECT_NOT_NULL(c);
c->send_directory = str_dup("/src");
c->receive_root_directory = str_dup("/dst");
c->use_incremental = cases[i].incremental;
c->use_delta = cases[i].delta;
c->use_chunk_serialization = cases[i].chunk;
c->use_sendfile = cases[i].sendfile;
c->use_compression = cases[i].compression;
EXPECT_FALSE(roundtrip_config_ok(c));
config_delete(c);
}
}
void test_config() { void test_config() {
test_config_lifecycle(); test_config_lifecycle();
test_config_ssh_dest(); test_config_ssh_dest();
@@ -2009,7 +2245,10 @@ void test_config() {
test_config_copy_as_wire_roundtrip(); test_config_copy_as_wire_roundtrip();
test_config_receive_rejects_negative_copy_as(); test_config_receive_rejects_negative_copy_as();
test_config_receive_rejects_copy_as_without_metadata(); test_config_receive_rejects_copy_as_without_metadata();
test_config_receive_rejects_oversized_string_budget();
test_config_receive_with_validate_rejects(); test_config_receive_with_validate_rejects();
test_config_invariants_error_all_combinations();
test_config_receive_rejects_unified_invariants();
} }
test_identity_copy_as_refused(); test_identity_copy_as_refused();
test_identity_ownership_requested(); test_identity_ownership_requested();
+175
View File
@@ -1,4 +1,5 @@
#include "test_daemon_conf.h" #include "test_daemon_conf.h"
#include "credentials.h"
#include "daemon_conf.h" #include "daemon_conf.h"
#include "test_utils.h" #include "test_utils.h"
#include <stdio.h> #include <stdio.h>
@@ -30,6 +31,10 @@ static void test_daemon_conf_create_defaults() {
EXPECT_EQ_INT(conf->global.port, DAEMON_CONF_DEFAULT_PORT); EXPECT_EQ_INT(conf->global.port, DAEMON_CONF_DEFAULT_PORT);
EXPECT_NULL(conf->global.motd_file); EXPECT_NULL(conf->global.motd_file);
EXPECT_NULL(conf->global.address); EXPECT_NULL(conf->global.address);
EXPECT_EQ_INT(conf->global.max_connections, DAEMON_CONF_DEFAULT_MAX_CONNECTIONS);
EXPECT_EQ_INT(conf->global.auth_failure_delay_ms, DAEMON_CONF_DEFAULT_AUTH_FAILURE_DELAY_MS);
EXPECT_EQ_INT(conf->global.hosts_allow_count, 0);
EXPECT_EQ_INT(conf->global.hosts_deny_count, 0);
EXPECT_EQ_INT(conf->module_count, 0); EXPECT_EQ_INT(conf->module_count, 0);
daemon_conf_free(conf); daemon_conf_free(conf);
} }
@@ -309,6 +314,18 @@ static void test_daemon_conf_dparam_override() {
EXPECT_EQ_INT(daemon_conf_apply_dparam(conf, "port = 9000", err, sizeof(err)), 0); EXPECT_EQ_INT(daemon_conf_apply_dparam(conf, "port = 9000", err, sizeof(err)), 0);
EXPECT_EQ_INT(conf->global.port, 9000); EXPECT_EQ_INT(conf->global.port, 9000);
EXPECT_EQ_INT(daemon_conf_apply_dparam(conf, "max connections=7", err, sizeof(err)), 0);
EXPECT_EQ_INT(conf->global.max_connections, 7);
EXPECT_EQ_INT(daemon_conf_apply_dparam(conf, "AUTH FAILURE DELAY=1500", err, sizeof(err)), 0);
EXPECT_EQ_INT(conf->global.auth_failure_delay_ms, 1500);
EXPECT_EQ_INT(
daemon_conf_apply_dparam(conf, "hosts allow=127.0.0.1,10.0.0.0/8", err, sizeof(err)), 0);
EXPECT_EQ_INT(conf->global.hosts_allow_count, 2);
/* A later --dparam replaces the list (an override must be able to narrow). */
EXPECT_EQ_INT(daemon_conf_apply_dparam(conf, "hosts allow=127.0.0.1", err, sizeof(err)), 0);
EXPECT_EQ_INT(conf->global.hosts_allow_count, 1);
EXPECT_EQ_STR(conf->global.hosts_allow[0], "127.0.0.1");
EXPECT_EQ_INT(daemon_conf_apply_dparam(conf, "port=notaport", err, sizeof(err)), -1); EXPECT_EQ_INT(daemon_conf_apply_dparam(conf, "port=notaport", err, sizeof(err)), -1);
EXPECT_EQ_INT(daemon_conf_apply_dparam(conf, "bogus=1", err, sizeof(err)), -1); EXPECT_EQ_INT(daemon_conf_apply_dparam(conf, "bogus=1", err, sizeof(err)), -1);
EXPECT_TRUE(strstr(err, "unknown global key") != NULL); EXPECT_TRUE(strstr(err, "unknown global key") != NULL);
@@ -320,6 +337,161 @@ static void test_daemon_conf_dparam_override() {
daemon_conf_free(conf); daemon_conf_free(conf);
} }
/* Each `auth users` entry is validated with the same username rule as the
* credential store, so invisible whitespace/control characters can never make
* an exact strcmp match ambiguous. */
static void test_daemon_conf_auth_users_validated() {
char* path;
char err[256];
const DaemonConf* conf;
EXPECT_EQ_INT(write_conf("[m]\npath = /x\nauth users = alice, bad user\n", &path), 0);
conf = daemon_conf_load(path, err, sizeof(err));
free(path);
EXPECT_NULL(conf);
EXPECT_TRUE(strstr(err, "invalid 'auth users' entry") != NULL);
EXPECT_EQ_INT(write_conf("[m]\npath = /x\nauth users = good\tbad\n", &path), 0);
conf = daemon_conf_load(path, err, sizeof(err));
free(path);
EXPECT_NULL(conf);
EXPECT_TRUE(strstr(err, "invalid 'auth users' entry") != NULL);
/* An over-long name exceeds CREDENTIAL_MAX_USER_LEN and is rejected. */
{
char body[CREDENTIAL_MAX_USER_LEN + 128];
int n = snprintf(body, sizeof(body), "[m]\npath = /x\nauth users = ");
memset(body + n, 'a', CREDENTIAL_MAX_USER_LEN + 1);
body[n + CREDENTIAL_MAX_USER_LEN + 1] = '\n';
body[n + CREDENTIAL_MAX_USER_LEN + 2] = '\0';
EXPECT_EQ_INT(write_conf(body, &path), 0);
conf = daemon_conf_load(path, err, sizeof(err));
free(path);
EXPECT_NULL(conf);
EXPECT_TRUE(strstr(err, "invalid 'auth users' entry") != NULL);
}
/* Empty entries between commas are skipped, not treated as invalid. */
EXPECT_EQ_INT(write_conf("[m]\npath = /x\nauth users = alice,, bob\n", &path), 0);
DaemonConf* ok_conf = daemon_conf_load(path, err, sizeof(err));
free(path);
EXPECT_NOT_NULL(ok_conf);
EXPECT_EQ_INT(ok_conf->modules[0].auth_user_count, 2);
EXPECT_EQ_STR(ok_conf->modules[0].auth_users[0], "alice");
EXPECT_EQ_STR(ok_conf->modules[0].auth_users[1], "bob");
daemon_conf_free(ok_conf);
}
/* Wave 3 daemon hardening: configurable global/per-module connection caps,
* auth-failure throttle and host access lists parse strictly (valid values are
* stored, malformed values fail the whole load). */
static void test_daemon_conf_limits_and_hosts_parse() {
char* path;
char err[256];
EXPECT_EQ_INT(write_conf("max connections = 25\n"
"auth failure delay = 0\n"
"hosts allow = 10.0.0.0/8, 192.168.1.0/24\n"
"hosts deny = 192.168.0.1 2001:db8::/32\n"
"\n"
"[m]\n"
"path = /x\n"
"max connections = 3\n"
"hosts allow = 127.0.0.1\n"
"hosts deny = *\n",
&path),
0);
DaemonConf* conf = daemon_conf_load(path, err, sizeof(err));
free(path);
EXPECT_NOT_NULL(conf);
EXPECT_EQ_INT(conf->global.max_connections, 25);
EXPECT_EQ_INT(conf->global.auth_failure_delay_ms, 0);
EXPECT_EQ_INT(conf->global.hosts_allow_count, 2);
EXPECT_EQ_STR(conf->global.hosts_allow[0], "10.0.0.0/8");
EXPECT_EQ_STR(conf->global.hosts_allow[1], "192.168.1.0/24");
EXPECT_EQ_INT(conf->global.hosts_deny_count, 2);
EXPECT_EQ_STR(conf->global.hosts_deny[0], "192.168.0.1");
EXPECT_EQ_STR(conf->global.hosts_deny[1], "2001:db8::/32");
EXPECT_EQ_INT(conf->modules[0].max_connections, 3);
EXPECT_EQ_INT(conf->modules[0].hosts_allow_count, 1);
EXPECT_EQ_STR(conf->modules[0].hosts_allow[0], "127.0.0.1");
EXPECT_EQ_INT(conf->modules[0].hosts_deny_count, 1);
EXPECT_EQ_STR(conf->modules[0].hosts_deny[0], "*");
daemon_conf_free(conf);
const char* bad_values[] = {
"max connections = 0\n", "max connections = -1\n",
"max connections = abc\n", "auth failure delay = -1\n",
"auth failure delay = 70000\n", "auth failure delay = soon\n",
"hosts allow = 10.0.0.0/99\n", "hosts deny = 2001:db8::/129\n",
"hosts allow = *.example.com\n", "hosts deny = not-an-ip\n",
};
for (size_t i = 0; i < sizeof(bad_values) / sizeof(bad_values[0]); i++) {
EXPECT_EQ_INT(write_conf(bad_values[i], &path), 0);
const DaemonConf* rejected = daemon_conf_load(path, err, sizeof(err));
free(path);
EXPECT_NULL(rejected);
}
/* The same strictness applies inside a module section. */
const char* bad_module[] = {
"[m]\npath = /x\nmax connections = 0\n",
"[m]\npath = /x\nhosts allow = 10.0.0.0/40\n",
"[m]\npath = /x\nhosts deny = 999.1.1.1/8\n",
};
for (size_t i = 0; i < sizeof(bad_module) / sizeof(bad_module[0]); i++) {
EXPECT_EQ_INT(write_conf(bad_module[i], &path), 0);
const DaemonConf* rejected = daemon_conf_load(path, err, sizeof(err));
free(path);
EXPECT_NULL(rejected);
EXPECT_TRUE(strstr(err, "invalid") != NULL);
}
/* An empty hosts list is not an error (no patterns are added). */
EXPECT_EQ_INT(write_conf("hosts allow = \n[m]\npath = /x\n", &path), 0);
conf = daemon_conf_load(path, err, sizeof(err));
free(path);
EXPECT_NOT_NULL(conf);
EXPECT_EQ_INT(conf->global.hosts_allow_count, 0);
daemon_conf_free(conf);
}
static void test_daemon_hosts_allowed() {
/* Pattern forms. */
EXPECT_TRUE(daemon_host_pattern_match("*", "203.0.113.9"));
EXPECT_TRUE(daemon_host_pattern_match("10.0.0.1", "10.0.0.1"));
EXPECT_FALSE(daemon_host_pattern_match("10.0.0.1", "10.0.0.2"));
EXPECT_TRUE(daemon_host_pattern_match("10.0.0.0/8", "10.255.1.2"));
EXPECT_FALSE(daemon_host_pattern_match("10.0.0.0/8", "11.0.0.1"));
EXPECT_TRUE(daemon_host_pattern_match("2001:db8::/32", "2001:db8:1234::5"));
EXPECT_FALSE(daemon_host_pattern_match("2001:db8::/32", "2001:db9::1"));
EXPECT_TRUE(daemon_host_pattern_match("::1", "::1"));
EXPECT_FALSE(daemon_host_pattern_match("::1", "::2"));
EXPECT_TRUE(daemon_host_pattern_match("*.example.com", "host.example.com"));
EXPECT_FALSE(daemon_host_pattern_match("*.example.com", "example.org"));
EXPECT_FALSE(daemon_host_pattern_match(NULL, "10.0.0.1"));
EXPECT_FALSE(daemon_host_pattern_match("10.0.0.1", NULL));
EXPECT_FALSE(daemon_host_pattern_match("", "10.0.0.1"));
char* allow[] = {"10.0.0.0/8"};
char* deny[] = {"10.0.0.1"};
/* Deny takes precedence over a matching allow. */
EXPECT_FALSE(daemon_hosts_allowed("10.0.0.1", allow, 1, deny, 1));
EXPECT_TRUE(daemon_hosts_allowed("10.0.0.2", allow, 1, deny, 1));
/* A non-empty allow list rejects a peer that matches none of its entries. */
EXPECT_FALSE(daemon_hosts_allowed("192.168.1.1", allow, 1, NULL, 0));
/* With only a deny list, everything not denied is accepted. */
EXPECT_TRUE(daemon_hosts_allowed("192.168.1.1", NULL, 0, deny, 1));
EXPECT_FALSE(daemon_hosts_allowed("10.0.0.1", NULL, 0, deny, 1));
/* No lists at all accepts everyone. */
EXPECT_TRUE(daemon_hosts_allowed("192.168.1.1", NULL, 0, NULL, 0));
/* An unprovable peer (NULL) never matches an allow list. */
EXPECT_FALSE(daemon_hosts_allowed(NULL, allow, 1, NULL, 0));
EXPECT_FALSE(daemon_hosts_restricted(NULL, 0, NULL, 0));
EXPECT_TRUE(daemon_hosts_restricted(allow, 1, NULL, 0));
EXPECT_TRUE(daemon_hosts_restricted(NULL, 0, deny, 1));
}
static void test_daemon_module_name_valid() { static void test_daemon_module_name_valid() {
EXPECT_TRUE(daemon_module_name_valid("backup")); EXPECT_TRUE(daemon_module_name_valid("backup"));
EXPECT_TRUE(daemon_module_name_valid("Backup_2")); EXPECT_TRUE(daemon_module_name_valid("Backup_2"));
@@ -351,5 +523,8 @@ void test_daemon_conf() {
test_daemon_conf_missing_file_rejected(); test_daemon_conf_missing_file_rejected();
test_daemon_conf_find_module(); test_daemon_conf_find_module();
test_daemon_conf_dparam_override(); test_daemon_conf_dparam_override();
test_daemon_conf_auth_users_validated();
test_daemon_conf_limits_and_hosts_parse();
test_daemon_hosts_allowed();
test_daemon_module_name_valid(); test_daemon_module_name_valid();
} }
+74 -3
View File
@@ -3,7 +3,6 @@
#endif #endif
#include "test_file.h" #include "test_file.h"
#include "file.h" #include "file.h"
#include "file_store.h"
#include "file_receive.h" #include "file_receive.h"
#include "data.h" #include "data.h"
#include "config.h" #include "config.h"
@@ -1225,7 +1224,7 @@ void test_trust_sender() {
} }
/* --sparse/-S hole preservation: a buffer with a long zero run written via /* --sparse/-S hole preservation: a buffer with a long zero run written via
* file_store_write_secure(sparse=true) must round-trip its content exactly and * file_to_disk_secure(sparse=true) must round-trip its content exactly and
* have the right logical size, and should additionally be genuinely sparse on * have the right logical size, and should additionally be genuinely sparse on
* filesystems that support holes. The sparseness assertion is tolerant: if the * filesystems that support holes. The sparseness assertion is tolerant: if the
* filesystem reports no holes (SEEK_HOLE/SEEK_DATA -> ENXIO) we skip the strict * filesystem reports no holes (SEEK_HOLE/SEEK_DATA -> ENXIO) we skip the strict
@@ -1247,7 +1246,7 @@ static void test_file_write_to_disk_sparse_preserves_holes() {
buf[size - 1 - i] = (unsigned char)((i * 7) % 253); buf[size - 1 - i] = (unsigned char)((i * 7) % 253);
} }
EXPECT_TRUE(file_store_write_secure(path, buf, size, false, true, NULL, false)); EXPECT_TRUE(file_to_disk_secure(path, buf, size, false, true, false, NULL, false, NULL));
/* Logical size must equal data_size exactly. */ /* Logical size must equal data_size exactly. */
struct stat st; struct stat st;
@@ -1370,6 +1369,76 @@ static void test_dir_time_list() {
rmdir(root); rmdir(root);
} }
/* A hostile sender can stream unbounded STATUS_DIR_TIMES frames; the
* accumulator must bound the CUMULATIVE path bytes (not just one frame) and
* reject the add that would cross the cap, leaving the list untouched. */
static void test_dir_time_list_cap() {
DirTimeList list;
dir_time_list_init(&list);
EXPECT_EQ_INT((int)list.bytes, 0);
FileMetadata metadata = {.mtime_sec = 1, .mtime_nsec = 0};
size_t path_len = MAX_STRING_SIZE - 1;
char* path = malloc(path_len + 1);
EXPECT_NOT_NULL(path);
memset(path, 'a', path_len);
path[path_len] = '\0';
bool rejected = false;
for (size_t i = 0; i < MAX_DIR_TIME_ENTRIES + 1 && !rejected; i++) {
size_t before_count = list.count;
size_t before_bytes = list.bytes;
if (!dir_time_list_add(&list, path, &metadata)) {
rejected = true;
/* The rejected add must not have partially mutated the list. */
EXPECT_TRUE(list.count == before_count);
EXPECT_TRUE(list.bytes == before_bytes);
} else {
EXPECT_TRUE(list.count == before_count + 1);
EXPECT_TRUE(list.bytes == before_bytes + path_len + sizeof(FileMetadata) + sizeof(char*));
}
}
EXPECT_TRUE(rejected);
EXPECT_TRUE(list.count <= MAX_DIR_TIME_ENTRIES);
EXPECT_TRUE(list.bytes <= MAX_DIR_TIME_BYTES);
/* The retained entries are still intact and freeable after the rejection. */
EXPECT_TRUE(list.count > 0);
EXPECT_TRUE(strcmp(list.paths[0], path) == 0);
dir_time_list_free(&list);
EXPECT_EQ_INT((int)list.bytes, 0);
free(path);
}
/* receive_incremental_check must reject an empty check_path; every other
* receive path rejects path[0]=='\0'. Feed the check header (empty wire path
* + size/mtime/nsec) and assert the check is refused without being skipped. */
static void test_receive_incremental_check_empty_path() {
Config* cfg = config_create();
EXPECT_NOT_NULL(cfg);
cfg->checksum = false;
int p[2];
EXPECT_EQ_INT(pipe(p), 0);
size_t wire_len = 0;
unsigned long long check_size = 0;
long long check_mtime = 0;
long long check_mtime_nsec = 0;
EXPECT_TRUE(send_n_data(p[1], &wire_len, sizeof(wire_len)));
EXPECT_TRUE(send_n_data(p[1], &check_size, sizeof(check_size)));
EXPECT_TRUE(send_n_data(p[1], &check_mtime, sizeof(check_mtime)));
EXPECT_TRUE(send_n_data(p[1], &check_mtime_nsec, sizeof(check_mtime_nsec)));
bool skipped = true;
const File* file = receive_incremental_check(p[0], cfg, &skipped);
EXPECT_NULL(file);
EXPECT_FALSE(skipped);
close(p[0]);
close(p[1]);
config_delete(cfg);
}
/* -K/--keep-dirlinks secure open: with an authorized root, a destination path /* -K/--keep-dirlinks secure open: with an authorized root, a destination path
* component that is a symlink to an IN-ROOT directory is used as that directory * component that is a symlink to an IN-ROOT directory is used as that directory
* (its referent is opened through a relative O_NOFOLLOW walk from the root fd, * (its referent is opened through a relative O_NOFOLLOW walk from the root fd,
@@ -1531,6 +1600,8 @@ void test_file() {
} }
test_file_metadata_create(); test_file_metadata_create();
test_dir_time_list(); test_dir_time_list();
test_dir_time_list_cap();
test_receive_incremental_check_empty_path();
test_keep_dirlinks_secure_open(); test_keep_dirlinks_secure_open();
test_inplace_overwrite_clears_special_mode_bits(); test_inplace_overwrite_clears_special_mode_bits();
test_inplace_overwrite_metadata_strips_special_bits(); test_inplace_overwrite_metadata_strips_special_bits();
+196
View File
@@ -0,0 +1,196 @@
#include "test_file_list.h"
#include "file_list.h"
#include "test_utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Reference implementation of the ORIGINAL file_list_affects linear scan. The
indexed implementation must agree with it on every query; this pins the
subtle semantics: empty entry == whole tree, exact match, rel under a listed
directory, and rel an ancestor directory of a listed entry. */
static bool reference_affects(const FileListSet* set, const char* rel) {
if (!set)
return true;
if (!rel)
return false;
for (int i = 0; i < set->count; i++) {
const char* entry = set->entries[i];
if (entry[0] == '\0')
return true;
if (strcmp(rel, entry) == 0)
return true;
size_t entry_len = strlen(entry);
if (strncmp(rel, entry, entry_len) == 0 && (rel[entry_len] == '/' || rel[entry_len] == '\0'))
return true;
size_t rel_len = strlen(rel);
if (strncmp(entry, rel, rel_len) == 0 && (entry[rel_len] == '/' || entry[rel_len] == '\0'))
return true;
}
return false;
}
static void write_list(const char* path, const char* bytes) {
FILE* fp = fopen(path, "wb");
EXPECT_NOT_NULL(fp);
size_t len = strlen(bytes);
EXPECT_EQ_INT((int)fwrite(bytes, 1, len, fp), (int)len);
fclose(fp);
}
static void check_queries(const FileListSet* set, const char* const* queries, int query_count) {
for (int i = 0; i < query_count; i++) {
bool expected = reference_affects(set, queries[i]);
bool actual = file_list_affects(set, queries[i]);
if (expected != actual) {
printf(" [FAIL] affects(\"%s\"): expected %d, got %d\n", queries[i], expected, actual);
current_test_failed = true;
return;
}
}
}
static void test_membership_matches_reference() {
const char* path = "test_file_list_case.txt";
char err[160];
/* Nested directories, an ancestor of a listed entry, an exact file, a
non-matching neighbor with the same prefix, and a literal '*'. */
write_list(path, "a\na/b\na/b/c\nab\nc.txt\nsub/b.bin\n*\n");
FileListSet* set = file_list_load(path, false, err, sizeof(err));
EXPECT_NOT_NULL(set);
const char* queries[] = {
"a", "a/b", "a/b/c", "a/b/c/d", "a/bx", "a/x", "ab",
"abc", "c.txt", "c.txt/x", "c", "sub", "sub/b.bin", "sub/b.bin/z",
"sub2", "*", "x", "", "a/b/cd", "/", "a/b/",
};
check_queries(set, queries, (int)(sizeof(queries) / sizeof(queries[0])));
EXPECT_TRUE(file_list_affects(set, "a/b/c/d"));
EXPECT_TRUE(file_list_affects(set, "a/bx")); /* under listed directory "a" */
EXPECT_TRUE(file_list_affects(set, "a/b/c"));
EXPECT_TRUE(file_list_affects(set, "a/x")); /* under listed directory "a" */
EXPECT_FALSE(file_list_affects(set, "abc")); /* component boundary: not "a" */
EXPECT_TRUE(file_list_affects(set, "sub"));
EXPECT_FALSE(file_list_affects(set, "sub2"));
file_list_destroy(set);
remove(path);
/* A single "." entry means the whole tree: every non-NULL query is true. */
write_list(path, ".\n");
set = file_list_load(path, false, err, sizeof(err));
EXPECT_NOT_NULL(set);
const char* root_queries[] = {"", "a", "a/b/c", "unrelated", "*", "/"};
for (int i = 0; i < (int)(sizeof(root_queries) / sizeof(root_queries[0])); i++)
EXPECT_TRUE(file_list_affects(set, root_queries[i]));
EXPECT_FALSE(file_list_affects(set, NULL));
check_queries(set, root_queries, (int)(sizeof(root_queries) / sizeof(root_queries[0])));
file_list_destroy(set);
remove(path);
/* Trailing slashes and "./" prefixes normalize away, so the query matches the
clean path (and not the raw spelling). */
write_list(path, "./dir/\ndir2/./x\n");
set = file_list_load(path, false, err, sizeof(err));
EXPECT_NOT_NULL(set);
EXPECT_TRUE(file_list_affects(set, "dir"));
EXPECT_TRUE(file_list_affects(set, "dir/x"));
EXPECT_TRUE(file_list_affects(set, "dir2/x"));
EXPECT_TRUE(file_list_affects(set, "dir2"));
EXPECT_TRUE(file_list_affects(set, "dir/")); /* boundary prefix of listed "dir" */
check_queries(set, (const char*[]){"dir", "dir/", "dir/x", "dir2", "dir2/x", "dir3"}, 6);
file_list_destroy(set);
remove(path);
/* An empty file yields an empty set: nothing is affected, and NULL set still
means "everything". */
write_list(path, "");
set = file_list_load(path, false, err, sizeof(err));
EXPECT_NOT_NULL(set);
EXPECT_EQ_INT(set->count, 0);
EXPECT_FALSE(file_list_affects(set, "a"));
EXPECT_FALSE(file_list_affects(set, ""));
check_queries(set, (const char*[]){"a", "a/b", ""}, 3);
file_list_destroy(set);
remove(path);
/* NULL set is the unrestricted case. */
EXPECT_TRUE(file_list_affects(NULL, "anything"));
EXPECT_TRUE(file_list_affects(NULL, NULL));
}
/* Explicit ancestor/descendant coverage: a query that is a proper ancestor of
a listed entry is affected, and a query below a listed entry is affected,
while a component-boundary neighbor is not. */
static void test_ancestor_and_descendant_queries() {
const char* path = "test_file_list_ancestor.txt";
char err[160];
write_list(path, "top/mid/leaf.txt\nsingle.txt\n");
FileListSet* set = file_list_load(path, false, err, sizeof(err));
EXPECT_NOT_NULL(set);
/* q is an ancestor of a listed entry. */
EXPECT_TRUE(file_list_affects(set, "top"));
EXPECT_TRUE(file_list_affects(set, "top/mid"));
EXPECT_FALSE(file_list_affects(set, "top/other")); /* neither direction */
EXPECT_FALSE(file_list_affects(set, "to")); /* component boundary */
/* A listed entry is an ancestor of q. */
EXPECT_TRUE(file_list_affects(set, "single.txt"));
EXPECT_TRUE(file_list_affects(set, "single.txt/deeper"));
EXPECT_FALSE(file_list_affects(set, "single.txtx")); /* boundary */
check_queries(set,
(const char*[]){"top", "top/mid", "top/mid/leaf.txt", "top/other", "single.txt",
"single.txt/deeper", "single.txtx", "to"},
8);
file_list_destroy(set);
remove(path);
}
/* Regression for the remote OOM: an adversarial --files-from entry made of a
very deep chain of repeated components must be indexed with memory
proportional to the entry count. The old implementation stored one copied
ancestor prefix per component (O(L^2) bytes for a single entry); the sorted
index stores the exact entries only. */
static void test_deep_paths_are_bounded() {
const char* path = "test_file_list_deep.txt";
enum { COMPONENTS = 20000 };
size_t entry_len = (size_t)COMPONENTS * 2; /* "a/" per component */
char* entry = malloc(entry_len + 1);
EXPECT_NOT_NULL(entry);
for (size_t i = 0; i < entry_len; i += 2) {
entry[i] = 'a';
entry[i + 1] = '/';
}
entry[entry_len - 1] = 'z'; /* .../a/z: a deep leaf name */
entry[entry_len] = '\0';
FILE* fp = fopen(path, "wb");
EXPECT_NOT_NULL(fp);
EXPECT_EQ_INT((int)fwrite(entry, 1, entry_len, fp), (int)entry_len);
EXPECT_EQ_INT(fputc('\n', fp), '\n');
fclose(fp);
char err[160];
FileListSet* set = file_list_load(path, false, err, sizeof(err));
EXPECT_NOT_NULL(set);
EXPECT_EQ_INT(set->count, 1);
/* One exact entry stored, not one node per path component. */
EXPECT_EQ_INT((int)set->index.sorted.count, 1);
EXPECT_EQ_INT((int)set->index.exact.size, 1);
EXPECT_TRUE(file_list_affects(set, entry)); /* exact */
EXPECT_TRUE(file_list_affects(set, "a")); /* ancestor of the entry */
EXPECT_TRUE(file_list_affects(set, "a/a")); /* deeper ancestor */
EXPECT_FALSE(file_list_affects(set, "b")); /* unrelated */
EXPECT_FALSE(file_list_affects(set, "aa")); /* component boundary */
file_list_destroy(set);
remove(path);
free(entry);
}
void test_file_list() {
test_membership_matches_reference();
test_ancestor_and_descendant_queries();
test_deep_paths_are_bounded();
}
+6
View File
@@ -0,0 +1,6 @@
#ifndef TEST_FILE_LIST_H
#define TEST_FILE_LIST_H
void test_file_list();
#endif
+1 -2
View File
@@ -128,8 +128,7 @@ static void test_fuzz_metadata_from_buf() {
EXPECT_EQ_INT((int)(meta_ptr - meta_buf), (int)meta_buf_size); EXPECT_EQ_INT((int)(meta_ptr - meta_buf), (int)meta_buf_size);
/* Deserialize from buffer (simulates fuzz_metadata_from_buf) */ /* Deserialize from buffer (simulates fuzz_metadata_from_buf) */
char* buf_copy = meta_buf; FileMetadata* deserialized = metadata_from_buf((const uint8_t*)meta_buf, (size_t)meta_buf_size);
FileMetadata* deserialized = metadata_from_buf(&buf_copy);
EXPECT_NOT_NULL(deserialized); EXPECT_NOT_NULL(deserialized);
EXPECT_EQ_INT((int)deserialized->mode, (int)meta->mode); EXPECT_EQ_INT((int)deserialized->mode, (int)meta->mode);
EXPECT_EQ_INT((int)deserialized->mtime_sec, (int)meta->mtime_sec); EXPECT_EQ_INT((int)deserialized->mtime_sec, (int)meta->mtime_sec);
+203
View File
@@ -0,0 +1,203 @@
#include "test_hardlink.h"
#include "hardlink.h"
#include "test_utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
/* A fresh table starts empty and destroy accepts NULL / a fresh table. */
static void test_hardlink_create_destroy() {
hardlink_table_destroy(NULL);
HardLinkTable* table = hardlink_table_create();
EXPECT_NOT_NULL(table);
EXPECT_EQ_INT((int)table->count, 0);
EXPECT_EQ_INT((int)table->capacity, 0);
EXPECT_EQ_INT(table->next_gid, 1);
hardlink_table_destroy(table);
}
/* The first member of an (dev, ino) group is data-carrying and owns the group;
* every later member gets the SAME gid, is not first, and points back at the
* first member's wire path. */
static void test_hardlink_grouping() {
HardLinkTable* table = hardlink_table_create();
EXPECT_NOT_NULL(table);
int gid_a = -1, gid_b = -1;
bool first_a = false, first_b = false;
char* first_path_a = NULL;
char* first_path_b = NULL;
EXPECT_TRUE(
hardlink_table_assign(table, "dir/first.txt", 7, 42, &gid_a, &first_a, &first_path_a));
EXPECT_TRUE(first_a);
EXPECT_EQ_INT(gid_a, 1);
EXPECT_NOT_NULL(first_path_a);
EXPECT_EQ_STR(first_path_a, "dir/first.txt");
EXPECT_TRUE(
hardlink_table_assign(table, "dir/second.txt", 7, 42, &gid_b, &first_b, &first_path_b));
EXPECT_FALSE(first_b);
EXPECT_EQ_INT(gid_b, gid_a);
EXPECT_NOT_NULL(first_path_b);
EXPECT_EQ_STR(first_path_b, "dir/first.txt");
/* Two members map onto a single stored group. */
EXPECT_EQ_INT((int)table->count, 1);
EXPECT_EQ_INT(table->next_gid, 2);
free(first_path_a);
free(first_path_b);
hardlink_table_destroy(table);
}
/* A different inode on the same device is a distinct group with a fresh gid. */
static void test_hardlink_distinct_inode() {
HardLinkTable* table = hardlink_table_create();
EXPECT_NOT_NULL(table);
int gid1 = -1, gid2 = -1;
bool first1 = false, first2 = false;
char* path1 = NULL;
char* path2 = NULL;
EXPECT_TRUE(hardlink_table_assign(table, "a", 7, 100, &gid1, &first1, &path1));
EXPECT_TRUE(first1);
EXPECT_TRUE(hardlink_table_assign(table, "b", 7, 101, &gid2, &first2, &path2));
EXPECT_TRUE(first2);
EXPECT_TRUE(gid1 != gid2);
EXPECT_EQ_INT(gid1, 1);
EXPECT_EQ_INT(gid2, 2);
EXPECT_EQ_STR(path1, "a");
EXPECT_EQ_STR(path2, "b");
free(path1);
free(path2);
hardlink_table_destroy(table);
}
/* Identical (dev, ino) on a DIFFERENT device must never be conflated: inode
* numbers are only unique per filesystem, so grouping is scoped by st_dev. */
static void test_hardlink_distinct_device() {
HardLinkTable* table = hardlink_table_create();
EXPECT_NOT_NULL(table);
int gid1 = -1, gid2 = -1;
bool first1 = false, first2 = false;
char* path1 = NULL;
char* path2 = NULL;
EXPECT_TRUE(hardlink_table_assign(table, "dev_a/one", 1, 55, &gid1, &first1, &path1));
EXPECT_TRUE(hardlink_table_assign(table, "dev_b/one", 2, 55, &gid2, &first2, &path2));
EXPECT_TRUE(first1);
EXPECT_TRUE(first2);
EXPECT_TRUE(gid1 != gid2);
EXPECT_EQ_INT((int)table->count, 2);
free(path1);
free(path2);
hardlink_table_destroy(table);
}
/* The table owns deep copies of every path: mutating (or freeing) the caller's
* buffer after assign must not affect the stored / returned paths. */
static void test_hardlink_path_ownership() {
HardLinkTable* table = hardlink_table_create();
EXPECT_NOT_NULL(table);
char caller[] = "owned/path";
int gid = -1;
bool is_first = false;
char* out = NULL;
EXPECT_TRUE(hardlink_table_assign(table, caller, 3, 9, &gid, &is_first, &out));
EXPECT_TRUE(is_first);
/* The returned pointer is a distinct allocation, not the caller's buffer. */
EXPECT_TRUE(out != caller);
EXPECT_TRUE(table->items[0].first_path != caller);
memset(caller, 'X', sizeof(caller) - 1);
EXPECT_EQ_STR(out, "owned/path");
EXPECT_EQ_STR(table->items[0].first_path, "owned/path");
/* Later members get their own independent copy of the first path. */
char second_caller[] = "owned/second";
int gid2 = -1;
bool first2 = true;
char* out2 = NULL;
EXPECT_TRUE(hardlink_table_assign(table, second_caller, 3, 9, &gid2, &first2, &out2));
EXPECT_FALSE(first2);
EXPECT_EQ_STR(out2, "owned/path");
EXPECT_TRUE(out2 != table->items[0].first_path);
EXPECT_TRUE(out2 != out);
free(out);
free(out2);
hardlink_table_destroy(table);
}
/* Bad arguments must be rejected without touching the table. */
static void test_hardlink_reject_bad_args() {
HardLinkTable* table = hardlink_table_create();
EXPECT_NOT_NULL(table);
int gid = 0;
bool is_first = false;
char* out = NULL;
EXPECT_FALSE(hardlink_table_assign(NULL, "x", 1, 1, &gid, &is_first, &out));
EXPECT_FALSE(hardlink_table_assign(table, NULL, 1, 1, &gid, &is_first, &out));
EXPECT_FALSE(hardlink_table_assign(table, "x", 1, 1, NULL, &is_first, &out));
EXPECT_FALSE(hardlink_table_assign(table, "x", 1, 1, &gid, NULL, &out));
EXPECT_FALSE(hardlink_table_assign(table, "x", 1, 1, &gid, &is_first, NULL));
EXPECT_EQ_INT((int)table->count, 0);
hardlink_table_destroy(table);
}
/* Many distinct groups grow the item array through its realloc path and keep
* gid assignment stable and monotonic. */
static void test_hardlink_many_groups() {
HardLinkTable* table = hardlink_table_create();
EXPECT_NOT_NULL(table);
const int n = 200;
for (int i = 0; i < n; i++) {
int gid = -1;
bool is_first = false;
char* out = NULL;
char path[32];
snprintf(path, sizeof(path), "file_%d", i);
EXPECT_TRUE(hardlink_table_assign(table, path, 1, (ino_t)(1000 + i), &gid, &is_first, &out));
EXPECT_TRUE(is_first);
EXPECT_EQ_INT(gid, i + 1);
EXPECT_EQ_STR(out, path);
free(out);
}
EXPECT_EQ_INT((int)table->count, n);
EXPECT_EQ_INT(table->next_gid, n + 1);
/* Re-querying an existing inode still reports the original gid. */
int gid = -1;
bool is_first = true;
char* out = NULL;
EXPECT_TRUE(hardlink_table_assign(table, "file_7_again", 1, 1007, &gid, &is_first, &out));
EXPECT_FALSE(is_first);
EXPECT_EQ_INT(gid, 8);
EXPECT_EQ_STR(out, "file_7");
free(out);
hardlink_table_destroy(table);
}
void test_hardlink() {
test_hardlink_create_destroy();
test_hardlink_grouping();
test_hardlink_distinct_inode();
test_hardlink_distinct_device();
test_hardlink_path_ownership();
test_hardlink_reject_bad_args();
test_hardlink_many_groups();
}
+6
View File
@@ -0,0 +1,6 @@
#ifndef TEST_HARDLINK_H
#define TEST_HARDLINK_H
void test_hardlink(void);
#endif
+116
View File
@@ -1,7 +1,9 @@
#include "test_log.h" #include "test_log.h"
#include "log.h" #include "log.h"
#include "test_utils.h" #include "test_utils.h"
#include <fcntl.h>
#include <string.h> #include <string.h>
#include <threads.h>
#include <unistd.h> #include <unistd.h>
/* Test default log level: WARNING and ERROR should print, DEBUG and INFO should not. /* Test default log level: WARNING and ERROR should print, DEBUG and INFO should not.
@@ -147,6 +149,118 @@ static void test_log_debug_enabled_matches_gate() {
set_log_debug_flags(LOG_DEBUG_ALL); set_log_debug_flags(LOG_DEBUG_ALL);
} }
#define LOG_CONCURRENCY_THREADS 8
#define LOG_CONCURRENCY_LINES 250
typedef struct {
int id;
} LogConcurrencyArg;
static int log_concurrency_worker(void* context) {
LogConcurrencyArg* arg = context;
for (int i = 0; i < LOG_CONCURRENCY_LINES; i++) {
log_message(LOG_LEVEL_WARNING, "worker %d line %d", arg->id, i);
}
return 0;
}
static int count_substring(const char* haystack, const char* needle) {
int count = 0;
size_t needle_length = strlen(needle);
const char* cursor = haystack;
while ((cursor = strstr(cursor, needle)) != NULL) {
count++;
cursor += needle_length;
}
return count;
}
/* Concurrent log_message() calls from many threads must never interleave a
* single line: every emitted line has exactly one timestamp prefix and one
* body. Before write_message() was serialized, the three separate fprintf
* calls (prefix, body, newline) let lines tear. */
static void test_log_concurrent_no_torn_lines(void) {
FILE* fp = tmpfile();
EXPECT_NOT_NULL(fp);
/* Mute the console mirror so the workers don't flood the test output. */
fflush(stdout);
fflush(stderr);
int saved_stdout = dup(STDOUT_FILENO);
int saved_stderr = dup(STDERR_FILENO);
int null_fd = open("/dev/null", O_WRONLY);
EXPECT_TRUE(saved_stdout >= 0);
EXPECT_TRUE(saved_stderr >= 0);
EXPECT_TRUE(null_fd >= 0);
EXPECT_TRUE(dup2(null_fd, STDOUT_FILENO) >= 0);
EXPECT_TRUE(dup2(null_fd, STDERR_FILENO) >= 0);
close(null_fd);
set_log_level(LOG_LEVEL_WARNING);
log_set_stderr_mode(LOG_STDERR_ERRORS);
log_set_file(fp);
thrd_t threads[LOG_CONCURRENCY_THREADS];
LogConcurrencyArg args[LOG_CONCURRENCY_THREADS];
int created = 0;
for (int i = 0; i < LOG_CONCURRENCY_THREADS; i++) {
args[i].id = i;
if (thrd_create(&threads[i], log_concurrency_worker, &args[i]) != thrd_success)
break;
created++;
}
for (int i = 0; i < created; i++) {
thrd_join(threads[i], NULL);
}
log_set_file(NULL);
fflush(fp);
fflush(stdout);
fflush(stderr);
dup2(saved_stdout, STDOUT_FILENO);
dup2(saved_stderr, STDERR_FILENO);
close(saved_stdout);
close(saved_stderr);
rewind(fp);
char line[512];
int total_lines = 0;
int malformed_lines = 0;
bool saw_missing_newline = false;
while (fgets(line, sizeof(line), fp) != NULL) {
size_t length = strlen(line);
if (length == 0 || line[length - 1] != '\n')
saw_missing_newline = true;
if (strncmp(line, "20", 2) != 0 || count_substring(line, "[WARN]: worker ") != 1)
malformed_lines++;
total_lines++;
}
fclose(fp);
EXPECT_EQ_INT(created, LOG_CONCURRENCY_THREADS);
EXPECT_FALSE(saw_missing_newline);
EXPECT_EQ_INT(malformed_lines, 0);
EXPECT_EQ_INT(total_lines, LOG_CONCURRENCY_THREADS * LOG_CONCURRENCY_LINES);
log_set_stderr_mode(LOG_STDERR_ERRORS);
}
/* Detaching the logger from a FILE* before it is closed must leave the logging
* subsystem safe: later calls must not touch the freed handle. */
static void test_log_set_file_null_before_fclose(void) {
FILE* fp = tmpfile();
EXPECT_NOT_NULL(fp);
set_log_level(LOG_LEVEL_ERROR);
log_set_file(fp);
log_message(LOG_LEVEL_ERROR, "line before detach");
log_set_file(NULL);
fclose(fp);
log_message(LOG_LEVEL_ERROR, "line after close");
EXPECT_TRUE(true);
}
void test_log() { void test_log() {
test_log_message_debug(); test_log_message_debug();
test_log_message_info(); test_log_message_info();
@@ -159,4 +273,6 @@ void test_log() {
test_log_stderr_mode_all(); test_log_stderr_mode_all();
test_log_message_formats(); test_log_message_formats();
test_log_debug_enabled_matches_gate(); test_log_debug_enabled_matches_gate();
test_log_concurrent_no_torn_lines();
test_log_set_file_null_before_fclose();
} }
+174 -37
View File
@@ -5,6 +5,8 @@
#include "test_utils.h" #include "test_utils.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
@@ -27,8 +29,8 @@ static void test_metadata_to_from_buf_roundtrip() {
char* write_ptr = buf; char* write_ptr = buf;
metadata_to_buf(&write_ptr, &original); metadata_to_buf(&write_ptr, &original);
char* read_ptr = buf; FileMetadata* result =
FileMetadata* result = metadata_from_buf(&read_ptr); metadata_from_buf((const uint8_t*)buf, FILE_METADATA_WIRE_SIZE + sizeof(int));
EXPECT_NOT_NULL(result); EXPECT_NOT_NULL(result);
EXPECT_EQ_INT(result->mode, 0755); EXPECT_EQ_INT(result->mode, 0755);
@@ -43,8 +45,6 @@ static void test_metadata_to_from_buf_roundtrip() {
EXPECT_EQ_INT(result->crtime_sec, 1200000000); EXPECT_EQ_INT(result->crtime_sec, 1200000000);
EXPECT_EQ_INT(result->crtime_nsec, 750000000); EXPECT_EQ_INT(result->crtime_nsec, 750000000);
EXPECT_EQ_INT((int)(read_ptr - buf), (int)FILE_METADATA_WIRE_SIZE + (int)sizeof(int));
free(result); free(result);
free(buf); free(buf);
} }
@@ -69,14 +69,46 @@ static void test_metadata_from_buf_null() {
int present = 0; int present = 0;
memcpy(buf, &present, sizeof(int)); memcpy(buf, &present, sizeof(int));
char* read_ptr = buf; const FileMetadata* result =
const FileMetadata* result = metadata_from_buf(&read_ptr); metadata_from_buf((const uint8_t*)buf, FILE_METADATA_WIRE_SIZE + sizeof(int));
EXPECT_NULL(result); EXPECT_NULL(result);
free(buf); free(buf);
} }
/* The decoder must reject (never over-read) a present record that is even one
* byte shorter than the full int32 flag + FILE_METADATA_WIRE_SIZE body, and
* must reject a buffer too short to even hold the present flag. */
static void test_metadata_from_buf_bounds() {
char* buf = malloc(FILE_METADATA_WIRE_SIZE + sizeof(int));
EXPECT_NOT_NULL(buf);
FileMetadata original = {.mode = 0644,
.uid = 1,
.gid = 2,
.mtime_sec = 3,
.mtime_nsec = 4,
.atime_valid = true,
.atime_sec = 5,
.atime_nsec = 6,
.crtime_valid = false};
char* write_ptr = buf;
metadata_to_buf(&write_ptr, &original);
EXPECT_NULL(metadata_from_buf((const uint8_t*)buf, 0));
EXPECT_NULL(metadata_from_buf((const uint8_t*)buf, sizeof(int)));
EXPECT_NULL(metadata_from_buf((const uint8_t*)buf, FILE_METADATA_WIRE_SIZE + sizeof(int) - 1));
/* A buffer larger than the record decodes using only the record prefix. */
FileMetadata* decoded =
metadata_from_buf((const uint8_t*)buf, FILE_METADATA_WIRE_SIZE + sizeof(int) + 16);
EXPECT_NOT_NULL(decoded);
EXPECT_EQ_INT(decoded->mode, 0644);
free(decoded);
EXPECT_NULL(metadata_from_buf(NULL, FILE_METADATA_WIRE_SIZE + sizeof(int)));
free(buf);
}
static void test_metadata_send_receive_roundtrip() { static void test_metadata_send_receive_roundtrip() {
io_set_bwlimit(0); io_set_bwlimit(0);
int p[2]; int p[2];
@@ -134,6 +166,113 @@ static void test_metadata_send_null() {
close(p[1]); close(p[1]);
} }
/* protocol 2.20.0: metadata is one packed frame. With metadata present the
* wire record is exactly sizeof(int32_t) + FILE_METADATA_WIRE_SIZE bytes (the
* present flag followed by the fixed field record); absent metadata is a lone
* int32 zero. */
static void test_metadata_wire_is_one_packed_frame() {
io_set_bwlimit(0);
int p[2];
EXPECT_EQ_INT(pipe(p), 0);
io_set_fds(p[0], p[1]);
FileMetadata original = {.mode = 0640,
.uid = 42,
.gid = 43,
.mtime_sec = 111,
.mtime_nsec = 222,
.atime_valid = true,
.atime_sec = 333,
.atime_nsec = 444,
.crtime_valid = false,
.crtime_sec = 0,
.crtime_nsec = 0};
EXPECT_TRUE(metadata_send(p[1], &original));
unsigned char wire[sizeof(int32_t) + FILE_METADATA_WIRE_SIZE];
EXPECT_EQ_INT((int)read(p[0], wire, sizeof(wire)), (int)sizeof(wire));
int32_t flag;
memcpy(&flag, wire, sizeof(flag));
EXPECT_EQ_INT(flag, 1);
int avail = -1;
EXPECT_EQ_INT(ioctl(p[0], FIONREAD, &avail), 0);
EXPECT_EQ_INT(avail, 0);
/* The present frame decodes in one shot with the shared codec. */
FileMetadata* decoded = metadata_from_buf((const uint8_t*)wire, sizeof(wire));
EXPECT_NOT_NULL(decoded);
EXPECT_EQ_INT(decoded->mode, 0640);
EXPECT_EQ_INT(decoded->uid, 42);
EXPECT_EQ_INT(decoded->gid, 43);
EXPECT_EQ_INT(decoded->mtime_sec, 111);
EXPECT_EQ_INT(decoded->mtime_nsec, 222);
EXPECT_TRUE(decoded->atime_valid);
EXPECT_EQ_INT(decoded->atime_sec, 333);
EXPECT_EQ_INT(decoded->atime_nsec, 444);
EXPECT_FALSE(decoded->crtime_valid);
free(decoded);
/* Absent metadata is a lone int32 zero (4 bytes). */
EXPECT_TRUE(metadata_send(p[1], NULL));
EXPECT_EQ_INT((int)read(p[0], wire, sizeof(int32_t)), (int)sizeof(int32_t));
memcpy(&flag, wire, sizeof(flag));
EXPECT_EQ_INT(flag, 0);
avail = -1;
EXPECT_EQ_INT(ioctl(p[0], FIONREAD, &avail), 0);
EXPECT_EQ_INT(avail, 0);
close(p[0]);
close(p[1]);
}
/* Round-trip over a socketpair (not just a pipe): present metadata compares
* equal field-by-field and absent metadata yields NULL with ok == 1. */
static void test_metadata_send_receive_socketpair() {
io_set_bwlimit(0);
int sv[2];
EXPECT_EQ_INT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0);
io_set_fds(sv[0], sv[1]);
FileMetadata original = {.mode = 0600,
.uid = 7,
.gid = 8,
.mtime_sec = 1000,
.mtime_nsec = 1,
.atime_valid = true,
.atime_sec = 2000,
.atime_nsec = 2,
.crtime_valid = true,
.crtime_sec = 3000,
.crtime_nsec = 3};
EXPECT_TRUE(metadata_send(sv[1], &original));
int ok = 0;
FileMetadata* received = metadata_receive(sv[0], &ok);
EXPECT_NOT_NULL(received);
EXPECT_EQ_INT(ok, 1);
EXPECT_EQ_INT(received->mode, 0600);
EXPECT_EQ_INT(received->uid, 7);
EXPECT_EQ_INT(received->gid, 8);
EXPECT_EQ_INT(received->mtime_sec, 1000);
EXPECT_EQ_INT(received->mtime_nsec, 1);
EXPECT_TRUE(received->atime_valid);
EXPECT_EQ_INT(received->atime_sec, 2000);
EXPECT_EQ_INT(received->atime_nsec, 2);
EXPECT_TRUE(received->crtime_valid);
EXPECT_EQ_INT(received->crtime_sec, 3000);
EXPECT_EQ_INT(received->crtime_nsec, 3);
free(received);
EXPECT_TRUE(metadata_send(sv[1], NULL));
ok = 0;
received = metadata_receive(sv[0], &ok);
EXPECT_NULL(received);
EXPECT_EQ_INT(ok, 1);
close(sv[0]);
close(sv[1]);
}
static void test_metadata_rejects_invalid_values() { static void test_metadata_rejects_invalid_values() {
int p[2]; int p[2];
EXPECT_EQ_INT(pipe(p), 0); EXPECT_EQ_INT(pipe(p), 0);
@@ -148,36 +287,30 @@ static void test_metadata_rejects_invalid_values() {
} }
/* metadata_receive must reject an out-of-range atime/crtime nsec even when the /* metadata_receive must reject an out-of-range atime/crtime nsec even when the
* flag would otherwise be valid (defense-in-depth on the -U/-N wire fields). */ * flag would otherwise be valid (defense-in-depth on the -U/-N wire fields).
* The packed record is built by the shared codec so the out-of-range value
* actually reaches the wire. */
static void test_metadata_receive_rejects_bad_optional_times() { static void test_metadata_receive_rejects_bad_optional_times() {
int p[2]; int p[2];
EXPECT_EQ_INT(pipe(p), 0); EXPECT_EQ_INT(pipe(p), 0);
io_set_fds(p[0], p[1]); io_set_fds(p[0], p[1]);
int32_t present = 1; FileMetadata bad = {.mode = 0644,
int32_t mode = 0644; .uid = 1000,
int32_t uid = 1000; .gid = 1000,
int32_t gid = 1000; .mtime_sec = 1,
int64_t mtime_sec = 1; .mtime_nsec = 0,
int64_t mtime_nsec = 0; .atime_valid = true,
int32_t atime_valid = 1; .atime_sec = 1,
int64_t atime_sec = 1; .atime_nsec = 2000000000, /* invalid: >= 1e9 */
int64_t atime_nsec = 2000000000; /* invalid: >= 1e9 */ .crtime_valid = false,
EXPECT_TRUE(send_n_data(p[1], &present, sizeof(present))); .crtime_sec = 0,
EXPECT_TRUE(send_n_data(p[1], &mode, sizeof(mode))); .crtime_nsec = 0};
EXPECT_TRUE(send_n_data(p[1], &uid, sizeof(uid))); char packed[sizeof(int32_t) + FILE_METADATA_WIRE_SIZE];
EXPECT_TRUE(send_n_data(p[1], &gid, sizeof(gid))); char* write_ptr = packed;
EXPECT_TRUE(send_n_data(p[1], &mtime_sec, sizeof(mtime_sec))); metadata_to_buf(&write_ptr, &bad);
EXPECT_TRUE(send_n_data(p[1], &mtime_nsec, sizeof(mtime_nsec))); EXPECT_TRUE(send_n_data(p[1], packed, sizeof(packed)));
EXPECT_TRUE(send_n_data(p[1], &atime_valid, sizeof(atime_valid)));
EXPECT_TRUE(send_n_data(p[1], &atime_sec, sizeof(atime_sec)));
EXPECT_TRUE(send_n_data(p[1], &atime_nsec, sizeof(atime_nsec)));
int32_t crtime_valid = 0;
int64_t crtime_sec = 0;
int64_t crtime_nsec = 0;
EXPECT_TRUE(send_n_data(p[1], &crtime_valid, sizeof(crtime_valid)));
EXPECT_TRUE(send_n_data(p[1], &crtime_sec, sizeof(crtime_sec)));
EXPECT_TRUE(send_n_data(p[1], &crtime_nsec, sizeof(crtime_nsec)));
int ok = 1; int ok = 1;
EXPECT_NULL(metadata_receive(p[0], &ok)); EXPECT_NULL(metadata_receive(p[0], &ok));
EXPECT_EQ_INT(ok, 0); EXPECT_EQ_INT(ok, 0);
@@ -235,12 +368,13 @@ static void test_file_restore_metadata() {
const char* content = "test content"; const char* content = "test content";
EXPECT_TRUE(file_write_to_disk(path, content, strlen(content), false, false)); EXPECT_TRUE(file_write_to_disk(path, content, strlen(content), false, false));
FileMetadata m; FileMetadata m = {.mode = 0644,
m.mode = 0644; .uid = getuid(),
m.uid = getuid(); .gid = getgid(),
m.gid = getgid(); .mtime_sec = 1234567890,
m.mtime_sec = 1234567890; .mtime_nsec = 0,
m.mtime_nsec = 0; .atime_valid = false,
.crtime_valid = false};
file_restore_metadata(path, &m, false); file_restore_metadata(path, &m, false);
@@ -345,8 +479,11 @@ void test_metadata() {
test_metadata_to_from_buf_roundtrip(); test_metadata_to_from_buf_roundtrip();
test_metadata_to_buf_null(); test_metadata_to_buf_null();
test_metadata_from_buf_null(); test_metadata_from_buf_null();
test_metadata_from_buf_bounds();
test_metadata_send_receive_roundtrip(); test_metadata_send_receive_roundtrip();
test_metadata_send_null(); test_metadata_send_null();
test_metadata_wire_is_one_packed_frame();
test_metadata_send_receive_socketpair();
test_metadata_rejects_invalid_values(); test_metadata_rejects_invalid_values();
test_metadata_receive_rejects_bad_optional_times(); test_metadata_receive_rejects_bad_optional_times();
test_metadata_mtime_window(); test_metadata_mtime_window();
+123
View File
@@ -3,6 +3,7 @@
#include "config.h" #include "config.h"
#include "protocol.h" #include "protocol.h"
#include "queue.h" #include "queue.h"
#include "receiver_pipeline.h"
#include "utils.h" #include "utils.h"
#include "test_utils.h" #include "test_utils.h"
#include <stdio.h> #include <stdio.h>
@@ -37,6 +38,7 @@ static void test_sender_create_destroy() {
EXPECT_NULL(ctx->manifest); EXPECT_NULL(ctx->manifest);
pipeline_context_sender_destroy(ctx); pipeline_context_sender_destroy(ctx);
config_delete(cfg); /* the context borrows cfg; the caller owns it */
} }
/* Test pipeline_context_receiver_create/destroy with valid arguments */ /* Test pipeline_context_receiver_create/destroy with valid arguments */
@@ -80,6 +82,7 @@ static void test_sender_queue_capacities() {
EXPECT_EQ_INT(ctx->queue_scanner->capacity, 1); EXPECT_EQ_INT(ctx->queue_scanner->capacity, 1);
EXPECT_EQ_INT(ctx->queue_loader->capacity, 1); EXPECT_EQ_INT(ctx->queue_loader->capacity, 1);
pipeline_context_sender_destroy(ctx); pipeline_context_sender_destroy(ctx);
config_delete(cfg); /* the context borrows cfg; the caller owns it */
} }
/* Invalid queue capacities must not create unusable pipeline queues. */ /* Invalid queue capacities must not create unusable pipeline queues. */
@@ -332,6 +335,124 @@ static void test_receiver_enqueue_byte_budget() {
config_delete(cfg); config_delete(cfg);
} }
/* Build a one-file chunk that appears to hold `bytes` of loaded payload by
handing it a real buffer of that size (the byte accounting counts only
in-memory `data->data`, mirroring sendfile's streamed chunks). */
static Chunk* make_loaded_chunk(const char* name, size_t bytes) {
File* file = file_create(name);
if (!file)
return NULL;
void* buffer = malloc(bytes > 0 ? bytes : 1);
if (!buffer) {
file_destroy(file);
return NULL;
}
file->data->data = buffer;
file->data->size = bytes;
File* items[1] = {file};
Chunk* chunk = chunk_create(items, 1);
if (!chunk)
file_destroy(file);
return chunk;
}
/* Only loaded (in-memory) payload is charged: a chunk whose files have no
buffer (e.g. sendfile streams the bytes from disk) accounts for zero. */
static void test_sender_chunk_bytes_accounting() {
EXPECT_EQ_INT((int)pipeline_context_sender_chunk_bytes(NULL), 0);
File* streamed = file_create("sender_account_streamed");
EXPECT_NOT_NULL(streamed);
streamed->data->size = 4096; /* declared size, but no in-memory buffer */
File* streamed_items[1] = {streamed};
Chunk* streamed_chunk = chunk_create(streamed_items, 1);
EXPECT_NOT_NULL(streamed_chunk);
EXPECT_EQ_INT((int)pipeline_context_sender_chunk_bytes(streamed_chunk), 0);
chunk_destroy(streamed_chunk);
Chunk* loaded = make_loaded_chunk("sender_account_loaded", 2000);
EXPECT_NOT_NULL(loaded);
EXPECT_EQ_INT((int)pipeline_context_sender_chunk_bytes(loaded), 2000);
chunk_destroy(loaded);
}
typedef struct {
PipelineContextSender* context;
Chunk* chunk;
atomic_bool* done;
atomic_bool* result;
} SenderByteBudgetArg;
static int sender_byte_budget_worker(void* arg) {
SenderByteBudgetArg* worker = arg;
bool ok = pipeline_context_sender_enqueue_chunk(worker->context, worker->chunk);
atomic_store(worker->result, ok);
atomic_store(worker->done, true);
return thrd_success;
}
/* The sender's loader stage must not buffer more loaded payload bytes ahead of
the network writer than the configured byte budget: an enqueue that would
exceed the budget blocks until the sender releases bytes. */
static void test_sender_enqueue_byte_budget() {
Config* cfg = config_create();
EXPECT_NOT_NULL(cfg);
free(cfg->version);
cfg->version = str_dup(PROTOCOL_VERSION);
cfg->send_directory = str_dup("/src");
cfg->receive_root_directory = str_dup("/dst");
Queue* q_scanner = queue_create(16, chunk_destroy);
Queue* q_loader = queue_create(16, chunk_destroy);
EXPECT_NOT_NULL(q_scanner);
EXPECT_NOT_NULL(q_loader);
PipelineContextSender* ctx = pipeline_context_sender_create(cfg, q_scanner, q_loader);
EXPECT_NOT_NULL(ctx);
pipeline_context_sender_set_queue_byte_limit(ctx, 3000);
Chunk* first = make_loaded_chunk("sender_budget_1", 2000);
EXPECT_NOT_NULL(first);
EXPECT_TRUE(pipeline_context_sender_enqueue_chunk(ctx, first));
EXPECT_EQ_INT((int)ctx->queued_bytes, 2000);
/* A second 2000-byte chunk would push the pipeline to 4000 > 3000 budget, so
its enqueue must block until the first chunk's bytes are released. */
Chunk* second = make_loaded_chunk("sender_budget_2", 2000);
EXPECT_NOT_NULL(second);
atomic_bool done;
atomic_bool result;
atomic_init(&done, false);
atomic_init(&result, false);
SenderByteBudgetArg arg = {ctx, second, &done, &result};
thrd_t enqueuer;
EXPECT_EQ_INT(thrd_create(&enqueuer, sender_byte_budget_worker, &arg), thrd_success);
/* Give a broken (unbounded) implementation every chance to enqueue. */
struct timespec wait = {0, 200 * 1000000L};
thrd_sleep(&wait, NULL);
EXPECT_FALSE(atomic_load(&done));
EXPECT_EQ_INT((int)ctx->queued_bytes, 2000); /* budget still honored */
/* Simulate the sender: dequeue + destroy the first chunk, then release its
bytes. Only the post-join state (below) is deterministic. */
Chunk* drained =
queue_dequeue_multithreaded(q_loader, &ctx->mutex_loader, &ctx->condition_not_empty_loader,
&ctx->condition_not_full_loader, &ctx->loader_done);
EXPECT_NOT_NULL(drained);
chunk_destroy(drained);
pipeline_context_sender_note_bytes_released(ctx, 2000);
EXPECT_EQ_INT(thrd_join(enqueuer, NULL), thrd_success);
EXPECT_TRUE(atomic_load(&done));
EXPECT_TRUE(atomic_load(&result));
EXPECT_EQ_INT((int)ctx->queued_bytes, 2000); /* second payload now in flight */
/* pipeline_context_sender_destroy frees the still-queued second chunk and
owns q_scanner/q_loader; cfg stays borrowed and is freed by the caller. */
pipeline_context_sender_destroy(ctx);
config_delete(cfg);
}
void test_multiprocessing() { void test_multiprocessing() {
test_sender_create_destroy(); test_sender_create_destroy();
test_receiver_create_destroy(); test_receiver_create_destroy();
@@ -344,4 +465,6 @@ void test_multiprocessing() {
} }
test_write_thread_done(); test_write_thread_done();
test_receiver_enqueue_byte_budget(); test_receiver_enqueue_byte_budget();
test_sender_chunk_bytes_accounting();
test_sender_enqueue_byte_budget();
} }
+123
View File
@@ -412,6 +412,35 @@ static void test_protocol_accounting_release_does_not_underflow() {
protocol_session_unbind(); protocol_session_unbind();
} }
static void test_protocol_session_io_timeout() {
/* Default is the built-in 60 s window; the setter stores exactly what it is
* given (<= 0 means "fall back to the default") so callers can propagate
* --timeout without special-casing 0. */
ProtocolSession session;
protocol_session_init(&session, -1, -1);
EXPECT_EQ_INT(session.io_timeout_sec, 60);
protocol_session_set_io_timeout(&session, 120);
EXPECT_EQ_INT(session.io_timeout_sec, 120);
protocol_session_set_io_timeout(&session, 0);
EXPECT_EQ_INT(session.io_timeout_sec, 0);
/* A NULL session is a no-op, not a crash. */
protocol_session_set_io_timeout(NULL, 5);
/* A short per-session deadline must actually bound a non-responsive read:
* with no writer the poll waits for the configured 1 s and then fails,
* rather than the built-in 60 s. */
int p[2];
EXPECT_EQ_INT(pipe(p), 0);
ProtocolSession timed;
protocol_session_init(&timed, p[0], p[1]);
protocol_session_set_io_timeout(&timed, 1);
char buf[4];
EXPECT_FALSE(protocol_receive_n_data(&timed, buf, sizeof(buf)));
close(p[0]);
close(p[1]);
}
static void test_send_receive_status_timed() { static void test_send_receive_status_timed() {
int p[2]; int p[2];
EXPECT_EQ_INT(pipe(p), 0); EXPECT_EQ_INT(pipe(p), 0);
@@ -431,6 +460,96 @@ static void test_send_receive_status_timed() {
close(p[0]); close(p[0]);
} }
static bool keepalive_always_abort(void) {
return true;
}
/* A pre-buffered KEEPALIVE reply from the peer must be consumed transparently,
leaving the first real status visible to the caller. */
static void test_receive_status_keepalive_skips_reply() {
int p[2];
EXPECT_EQ_INT(pipe(p), 0);
ProtocolSession session;
protocol_session_init(&session, p[0], p[1]);
EXPECT_TRUE(protocol_send_status(&session, STATUS_KEEPALIVE));
EXPECT_TRUE(protocol_send_status(&session, STATUS_OK));
Status received = STATUS_ERROR;
EXPECT_TRUE(protocol_receive_status_keepalive(&session, &received, 5, 1, NULL));
EXPECT_EQ_INT((int)received, (int)STATUS_OK);
close(p[0]);
close(p[1]);
}
/* The abort callback ends the wait immediately, before any keepalive traffic. */
static void test_receive_status_keepalive_aborts() {
int p[2];
EXPECT_EQ_INT(pipe(p), 0);
ProtocolSession session;
protocol_session_init(&session, p[0], p[1]);
Status received = STATUS_ERROR;
EXPECT_FALSE(
protocol_receive_status_keepalive(&session, &received, 5, 1, keepalive_always_abort));
close(p[0]);
close(p[1]);
}
typedef struct {
int peer_read_fd;
int peer_write_fd;
bool replied;
} KeepalivePeerArg;
static int keepalive_peer(void* arg) {
KeepalivePeerArg* peer = arg;
ProtocolSession session;
protocol_session_init(&session, peer->peer_read_fd, peer->peer_write_fd);
Status status = STATUS_ERROR;
if (protocol_receive_status(&session, &status) && status == STATUS_KEEPALIVE) {
/* Model the busy receiver: it sends the real ack first, then the keepalive
reply it owes for the queued keepalive (which the client must drain so it
does not desynchronize the stream). */
peer->replied = protocol_send_status(&session, STATUS_OK) &&
protocol_send_status(&session, STATUS_KEEPALIVE);
}
return thrd_success;
}
/* While the peer is silent the helper must emit STATUS_KEEPALIVE, then consume
the peer's ack and drain the keepalive reply that follows it -- proving the
inline keepalive loop works without a second writer racing the send path. */
static void test_receive_status_keepalive_emits() {
int to_client[2];
int to_peer[2];
EXPECT_EQ_INT(pipe(to_client), 0);
EXPECT_EQ_INT(pipe(to_peer), 0);
ProtocolSession session;
protocol_session_init(&session, to_client[0], to_peer[1]);
KeepalivePeerArg peer = {.peer_read_fd = to_peer[0], .peer_write_fd = to_client[1]};
thrd_t thread;
EXPECT_EQ_INT(thrd_create(&thread, keepalive_peer, &peer), thrd_success);
Status received = STATUS_ERROR;
EXPECT_TRUE(protocol_receive_status_keepalive(&session, &received, 10, 1, NULL));
EXPECT_EQ_INT((int)received, (int)STATUS_OK);
int result = 0;
EXPECT_EQ_INT(thrd_join(thread, &result), thrd_success);
EXPECT_EQ_INT(result, thrd_success);
EXPECT_TRUE(peer.replied);
close(to_client[0]);
close(to_client[1]);
close(to_peer[0]);
close(to_peer[1]);
}
void test_protocol() { void test_protocol() {
test_send_receive_n_data(); test_send_receive_n_data();
test_send_receive_n_data_zero(); test_send_receive_n_data_zero();
@@ -440,7 +559,11 @@ void test_protocol() {
test_send_receive_data(); test_send_receive_data();
test_send_receive_int(); test_send_receive_int();
test_send_receive_status(); test_send_receive_status();
test_protocol_session_io_timeout();
test_send_receive_status_timed(); test_send_receive_status_timed();
test_receive_status_keepalive_skips_reply();
test_receive_status_keepalive_aborts();
test_receive_status_keepalive_emits();
test_receive_n_data_truncated(); test_receive_n_data_truncated();
test_receive_str_truncated(); test_receive_str_truncated();
test_max_alloc_rejects_single_buffer(); test_max_alloc_rejects_single_buffer();
+97
View File
@@ -0,0 +1,97 @@
#include "test_receiver_timeout.h"
#include "protocol.h"
#include "receiver.h"
#include "test_utils.h"
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
static bool sink_discard(File* file, void* context) {
(void)context;
file_destroy(file);
return true;
}
/* The idle/session bound is a pure function of three monotonic timestamps, so
* it can be exercised deterministically without sleeping an hour. A tiny
* overridden limit covers the same arithmetic the loop uses. */
static void test_receiver_time_limit_predicate() {
receiver_set_time_limits(10, 100);
struct timespec start = {.tv_sec = 1000, .tv_nsec = 0};
struct timespec fresh = {.tv_sec = 1000, .tv_nsec = 0};
struct timespec just_idle = {.tv_sec = 1009, .tv_nsec = 0}; /* 9 s no progress */
struct timespec idle = {.tv_sec = 1010, .tv_nsec = 0}; /* 10 s no progress */
struct timespec just_wall = {.tv_sec = 1099, .tv_nsec = 0};
struct timespec wall = {.tv_sec = 1100, .tv_nsec = 0}; /* 100 s session */
struct timespec wp_just = {.tv_sec = 1098, .tv_nsec = 0}; /* idle 1 s */
struct timespec wp_wall = {.tv_sec = 1099, .tv_nsec = 0}; /* idle 1 s */
EXPECT_FALSE(receiver_time_limit_exceeded(&start, &fresh, &fresh));
EXPECT_FALSE(receiver_time_limit_exceeded(&start, &fresh, &just_idle));
EXPECT_TRUE(receiver_time_limit_exceeded(&start, &fresh, &idle));
EXPECT_FALSE(receiver_time_limit_exceeded(&start, &wp_just, &just_wall));
EXPECT_TRUE(receiver_time_limit_exceeded(&start, &wp_wall, &wall));
/* Reset restores the generous production defaults (1 h idle / 24 h total). */
receiver_reset_time_limits();
struct timespec under_hour = {.tv_sec = 1000 + 3599, .tv_nsec = 0};
EXPECT_FALSE(receiver_time_limit_exceeded(&start, &start, &under_hour));
receiver_reset_time_limits();
}
/* Drive the actual receive loop with a test-only idle limit of 0 so the very
* first keepalive is rejected: this exercises the loop's abort path (log +
* STATUS_ERROR + return -1) with no timing dependence. */
static void test_receiver_aborts_idle_keepalive() {
receiver_set_time_limits(0, 3600);
int sv[2];
EXPECT_EQ_INT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0);
Config* config = config_create();
EXPECT_NOT_NULL(config);
ReceiverSink sink = {.store_file = sink_discard,
.context = NULL,
.send_error = true,
.send_success = false,
.send_success_frame = NULL};
/* Bind an explicit session so the fd-based receive helpers use the
* socketpair rather than any transport left over from an earlier test. */
ProtocolSession session;
protocol_session_init(&session, sv[1], sv[1]);
protocol_session_bind(&session);
Status keepalive = STATUS_KEEPALIVE;
ssize_t wrote = write(sv[0], &keepalive, sizeof(keepalive));
int result = -2;
if (wrote == (ssize_t)sizeof(keepalive))
result = receiver_process_pending(config, sv[1], &sink, NULL);
Status reply = STATUS_OK;
ssize_t got = -1;
if (result == -1)
got = read(sv[0], &reply, sizeof(reply));
/* Tear down the binding/descriptors BEFORE asserting: an EXPECT_* failure
* returns immediately, and a dangling bound_session would poison later
* fd-level protocol I/O tests. */
protocol_session_unbind();
config_delete(config);
close(sv[0]);
close(sv[1]);
receiver_reset_time_limits();
EXPECT_EQ_INT((int)wrote, (int)sizeof(keepalive));
EXPECT_EQ_INT(result, -1);
EXPECT_EQ_INT((int)got, (int)sizeof(reply));
EXPECT_EQ_INT((int)reply, (int)STATUS_ERROR);
}
void test_receiver_timeout(void) {
/* EXPECT_* returns from the current function on failure, so reset the
* process-global limits around the subtests (and again after) to guarantee a
* failed assertion cannot leave the receiver aborted for later tests. */
receiver_reset_time_limits();
test_receiver_time_limit_predicate();
test_receiver_aborts_idle_keepalive();
receiver_reset_time_limits();
}
+6
View File
@@ -0,0 +1,6 @@
#ifndef TEST_RECEIVER_TIMEOUT_H
#define TEST_RECEIVER_TIMEOUT_H
void test_receiver_timeout(void);
#endif
+187
View File
@@ -1317,6 +1317,191 @@ static void test_scanner_captures_directory_times() {
rmdir(root); rmdir(root);
} }
/* Ownership guard for chunk_data_to_chunk(): a returned Chunk owns its File
* objects, so destroying the chunk must free them exactly once and the scanner
* must never free them again. chunk_size = 1 forces the mid-directory
* conversion branch (chunk_data_size > chunk_size) for every file, and the
* chunk is destroyed immediately, catching a double free / use-after-free under
* ASan if ownership transfer regressed.
*
* The failure path (array_list_to_array() or chunk_create() returning NULL) is
* not reachable from a unit test: both allocate through protocol_alloc(), and
* each allocation they perform is no larger than the array_list allocations
* that already succeeded while building the list (array_list_to_array() copies
* exactly `size` pointers, which never exceeds the capacity just grown, and
* sizeof(Chunk) is far below the initial 100-entry item array). Binding a
* small --max-alloc session therefore always fails *before* this function, not
* inside it, so fault injection cannot isolate these paths. */
static void test_scanner_chunk_ownership() {
const char* dir = "test_scan_ownership";
const char* file1 = "test_scan_ownership/a.txt";
const char* file2 = "test_scan_ownership/b.txt";
const char* file3 = "test_scan_ownership/c.txt";
EXPECT_EQ_INT(mkdir(dir, 0755), 0);
create_test_file(file1, "aaaa");
create_test_file(file2, "bbbb");
create_test_file(file3, "cccc");
ScannerOptions options = {0};
options.chunk_size = 1;
DirectoryScanner* scanner = directory_scanner_create_with_options(dir, &options);
EXPECT_NOT_NULL(scanner);
int chunks = 0;
int files = 0;
Chunk* chunk;
while ((chunk = directory_scanner_next(scanner)) != NULL) {
chunks++;
files += chunk->element_count;
EXPECT_EQ_INT(chunk->element_count, 1);
chunk_destroy(chunk);
EXPECT_FALSE(directory_scanner_failed(scanner));
}
EXPECT_EQ_INT(files, 3);
EXPECT_EQ_INT(chunks, 3);
EXPECT_FALSE(directory_scanner_failed(scanner));
directory_scanner_destroy(scanner);
unlink(file1);
unlink(file2);
unlink(file3);
rmdir(dir);
}
/* The scanner derives entry type from a single lstat() for non-symlinks
* (regular files and directories) and only calls stat() to dereference real
* symlinks. Guard the regular-file/directory/symlink distinction across the
* default (symlinks skipped), --copy-links (dereferenced) and -l (carried)
* modes so the lstat/stat reuse cannot misclassify entries. */
static void test_scanner_entry_classification() {
const char* root = "test_scan_classify";
const char* sub = "test_scan_classify/sub";
const char* file = "test_scan_classify/file.txt";
const char* nested = "test_scan_classify/sub/nested.txt";
const char* link_file = "test_scan_classify/link_file";
const char* link_dir = "test_scan_classify/link_dir";
EXPECT_EQ_INT(mkdir(root, 0755), 0);
EXPECT_EQ_INT(mkdir(sub, 0755), 0);
create_test_file(file, "hello"); /* 5 bytes */
create_test_file(nested, "nested"); /* 6 bytes */
EXPECT_EQ_INT(symlink("file.txt", link_file), 0);
EXPECT_EQ_INT(symlink("sub", link_dir), 0);
/* Default: no link option -> symlinks are skipped entirely; regular files and
directories (descended, not emitted) are classified as before. */
{
ScannerOptions options = {0};
DirectoryScanner* scanner = directory_scanner_create_with_options(root, &options);
EXPECT_NOT_NULL(scanner);
size_t root_len = strlen(root);
bool file_ok = false, nested_ok = false, link_seen = false;
Chunk* chunk;
while ((chunk = directory_scanner_next(scanner)) != NULL) {
for (int i = 0; i < chunk->element_count; i++) {
const File* f = chunk->items[i];
const char* rel = f->path + root_len;
if (*rel == '/')
rel++;
if (strcmp(rel, "file.txt") == 0) {
file_ok = !f->is_dir && !f->is_symlink && f->data->size == 5;
} else if (strcmp(rel, "sub/nested.txt") == 0) {
nested_ok = !f->is_dir && !f->is_symlink && f->data->size == 6;
} else {
link_seen = true;
}
}
chunk_destroy(chunk);
}
EXPECT_FALSE(directory_scanner_failed(scanner));
EXPECT_TRUE(file_ok);
EXPECT_TRUE(nested_ok);
EXPECT_FALSE(link_seen);
directory_scanner_destroy(scanner);
}
/* --copy-links: symlinks are dereferenced. A link to a file becomes a
regular file with the referent's size; a link to a directory is traversed. */
{
ScannerOptions options = {0};
options.copy_links = true;
DirectoryScanner* scanner = directory_scanner_create_with_options(root, &options);
EXPECT_NOT_NULL(scanner);
size_t root_len = strlen(root);
bool file_ok = false, nested_ok = false, link_file_ok = false;
bool link_dir_nested_ok = false, symlink_leaked = false;
Chunk* chunk;
while ((chunk = directory_scanner_next(scanner)) != NULL) {
for (int i = 0; i < chunk->element_count; i++) {
const File* f = chunk->items[i];
const char* rel = f->path + root_len;
if (*rel == '/')
rel++;
if (f->is_symlink)
symlink_leaked = true;
if (strcmp(rel, "file.txt") == 0)
file_ok = !f->is_dir && f->data->size == 5;
else if (strcmp(rel, "sub/nested.txt") == 0)
nested_ok = !f->is_dir && f->data->size == 6;
else if (strcmp(rel, "link_file") == 0)
link_file_ok = !f->is_dir && f->data->size == 5;
else if (strcmp(rel, "link_dir/nested.txt") == 0)
link_dir_nested_ok = !f->is_dir && f->data->size == 6;
}
chunk_destroy(chunk);
}
EXPECT_FALSE(directory_scanner_failed(scanner));
EXPECT_TRUE(file_ok);
EXPECT_TRUE(nested_ok);
EXPECT_TRUE(link_file_ok);
EXPECT_TRUE(link_dir_nested_ok);
EXPECT_FALSE(symlink_leaked);
directory_scanner_destroy(scanner);
}
/* -l (--links): symlinks are carried through as symlinks, not dereferenced. */
{
ScannerOptions options = {0};
options.follow_symlinks = true;
DirectoryScanner* scanner = directory_scanner_create_with_options(root, &options);
EXPECT_NOT_NULL(scanner);
size_t root_len = strlen(root);
bool file_ok = false, link_file_ok = false, link_dir_ok = false, leaked_dir = false;
Chunk* chunk;
while ((chunk = directory_scanner_next(scanner)) != NULL) {
for (int i = 0; i < chunk->element_count; i++) {
const File* f = chunk->items[i];
const char* rel = f->path + root_len;
if (*rel == '/')
rel++;
if (strcmp(rel, "file.txt") == 0)
file_ok = !f->is_dir && !f->is_symlink && f->data->size == 5;
else if (strcmp(rel, "link_file") == 0)
link_file_ok = f->is_symlink && !f->is_dir;
else if (strcmp(rel, "link_dir") == 0)
link_dir_ok = f->is_symlink && !f->is_dir;
else if (strcmp(rel, "link_dir/nested.txt") == 0)
leaked_dir = true;
}
chunk_destroy(chunk);
}
EXPECT_FALSE(directory_scanner_failed(scanner));
EXPECT_TRUE(file_ok);
EXPECT_TRUE(link_file_ok);
EXPECT_TRUE(link_dir_ok);
EXPECT_FALSE(leaked_dir);
directory_scanner_destroy(scanner);
}
unlink(link_file);
unlink(link_dir);
unlink(nested);
unlink(file);
rmdir(sub);
rmdir(root);
}
void test_scanner() { void test_scanner() {
test_scanner_single_file(); test_scanner_single_file();
test_scanner_multiple_files(); test_scanner_multiple_files();
@@ -1353,4 +1538,6 @@ void test_scanner() {
test_dirs_files_from(); test_dirs_files_from();
test_files_from_relative_send_path(); test_files_from_relative_send_path();
test_scanner_captures_directory_times(); test_scanner_captures_directory_times();
test_scanner_chunk_ownership();
test_scanner_entry_classification();
} }
+167
View File
@@ -150,6 +150,43 @@ static void test_walker_removes_extras_keeps_manifest_and_protected() {
free(root); free(root);
} }
static void test_walker_keeps_nested_manifest_dirs() {
/* The keep-set index must preserve deep content: a directory is protected
when its own name is a keep entry OR when kept content lives below it, and
an exact kept file survives while its siblings are removed. */
char* root = make_walk_root("nestedkeep");
EXPECT_NOT_NULL(root);
EXPECT_TRUE(write_file_at(root, "extra.txt", "extra"));
EXPECT_EQ_INT(make_subdir(root, "keepdir"), 0);
EXPECT_EQ_INT(make_subdir(root, "keepdir/deep"), 0);
EXPECT_TRUE(write_file_at(root, "keepdir/deep/keep.txt", "kept"));
EXPECT_TRUE(write_file_at(root, "keepdir/extra2.txt", "extra"));
EXPECT_EQ_INT(make_subdir(root, "dropdir"), 0);
EXPECT_EQ_INT(make_subdir(root, "keep2"), 0);
EXPECT_TRUE(write_file_at(root, "keep2/inner.txt", "kept"));
EXPECT_EQ_INT(make_subdir(root, "keep3"), 0);
const char* keeps[] = {"keepdir/deep/keep.txt", "keep2/inner.txt", "keep3"};
ArrayList* manifest = make_manifest_strings(keeps, 3);
EXPECT_NOT_NULL(manifest);
size_t deleted = 0;
DeleteWalkResult result = delete_extras_limited(root, manifest, 100000, NULL, 0, &deleted);
EXPECT_EQ_INT((int)result, (int)DELETE_WALK_OK);
EXPECT_FALSE(file_exists(root, "extra.txt"));
EXPECT_TRUE(file_exists(root, "keepdir/deep/keep.txt"));
EXPECT_FALSE(file_exists(root, "keepdir/extra2.txt"));
EXPECT_TRUE(dir_exists(root, "keepdir"));
EXPECT_TRUE(dir_exists(root, "keepdir/deep"));
EXPECT_FALSE(dir_exists(root, "dropdir"));
EXPECT_TRUE(dir_exists(root, "keep2"));
EXPECT_TRUE(file_exists(root, "keep2/inner.txt"));
EXPECT_TRUE(dir_exists(root, "keep3")); /* an exact directory keep entry survives */
EXPECT_EQ_INT((int)deleted, 3);
array_list_delete(manifest);
remove_walk_tree(root);
free(root);
}
static void test_walker_max_delete_exceeded_deletes_nothing() { static void test_walker_max_delete_exceeded_deletes_nothing() {
char* root = make_walk_root("maxdel"); char* root = make_walk_root("maxdel");
EXPECT_NOT_NULL(root); EXPECT_NOT_NULL(root);
@@ -356,13 +393,143 @@ static void test_loopback_helpers() {
close(listener); close(listener);
} }
/* The daemon host ACL reads the numeric peer address through
* utils_fd_peer_ip. A real loopback TCP peer reports "127.0.0.1"; a pipe or an
* AF_UNIX socketpair has no INET peer and must return false with an empty
* buffer (the fail-closed "cannot tell" result). */
static void test_fd_peer_ip() {
char ip[INET6_ADDRSTRLEN];
EXPECT_FALSE(utils_fd_peer_ip(-1, ip, sizeof(ip)));
EXPECT_EQ_STR(ip, "");
EXPECT_FALSE(utils_fd_peer_ip(-1, NULL, 0));
int pipe_fds[2];
EXPECT_EQ_INT(pipe(pipe_fds), 0);
EXPECT_FALSE(utils_fd_peer_ip(pipe_fds[0], ip, sizeof(ip)));
EXPECT_EQ_STR(ip, "");
close(pipe_fds[0]);
close(pipe_fds[1]);
int pair_fds[2];
EXPECT_EQ_INT(socketpair(AF_UNIX, SOCK_STREAM, 0, pair_fds), 0);
EXPECT_FALSE(utils_fd_peer_ip(pair_fds[0], ip, sizeof(ip)));
EXPECT_EQ_STR(ip, "");
close(pair_fds[0]);
close(pair_fds[1]);
int listener = socket(AF_INET, SOCK_STREAM, 0);
EXPECT_TRUE(listener >= 0);
struct sockaddr_in bind_addr;
memset(&bind_addr, 0, sizeof(bind_addr));
bind_addr.sin_family = AF_INET;
bind_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
bind_addr.sin_port = 0;
EXPECT_EQ_INT(bind(listener, (const struct sockaddr*)&bind_addr, sizeof(bind_addr)), 0);
EXPECT_EQ_INT(listen(listener, 1), 0);
socklen_t addr_len = sizeof(bind_addr);
EXPECT_EQ_INT(getsockname(listener, (struct sockaddr*)&bind_addr, &addr_len), 0);
int dialer = socket(AF_INET, SOCK_STREAM, 0);
EXPECT_TRUE(dialer >= 0);
EXPECT_EQ_INT(connect(dialer, (const struct sockaddr*)&bind_addr, sizeof(bind_addr)), 0);
int accepted = accept(listener, NULL, NULL);
EXPECT_TRUE(accepted >= 0);
EXPECT_TRUE(utils_fd_peer_ip(accepted, ip, sizeof(ip)));
EXPECT_EQ_STR(ip, "127.0.0.1");
/* utils_sockaddr_to_string includes the port for a real peer. */
struct sockaddr_storage peer;
socklen_t peer_len = sizeof(peer);
EXPECT_EQ_INT(getpeername(accepted, (struct sockaddr*)&peer, &peer_len), 0);
char peer_string[128];
EXPECT_TRUE(
utils_sockaddr_to_string((const struct sockaddr*)&peer, peer_string, sizeof(peer_string)));
EXPECT_TRUE(strncmp(peer_string, "127.0.0.1:", strlen("127.0.0.1:")) == 0);
close(accepted);
close(dialer);
close(listener);
/* A non-INET family formats to "unknown" at the call site, not a bogus IP. */
struct sockaddr sa_unix;
memset(&sa_unix, 0, sizeof(sa_unix));
sa_unix.sa_family = AF_UNIX;
EXPECT_FALSE(utils_sockaddr_to_string(&sa_unix, peer_string, sizeof(peer_string)));
EXPECT_EQ_STR(peer_string, "");
}
/* The keep/files-from indexes must store exactly the input entries (one node
each), never a copied ancestor prefix per component. This builds a PathIndex
over paths thousands of components deep and checks the structural bound plus
the exact / descendant query semantics. */
static void test_path_index_bounded() {
enum { COUNT = 8, COMPONENTS = 5000 };
size_t entry_len = (size_t)COMPONENTS * 2 + 2; /* trailing "xN" */
char* storage = malloc((size_t)COUNT * (entry_len + 1));
EXPECT_NOT_NULL(storage);
const char** entries = calloc(COUNT, sizeof(char*));
EXPECT_NOT_NULL(entries);
for (int i = 0; i < COUNT; i++) {
char* entry = storage + (size_t)i * (entry_len + 1);
size_t pos = 0;
for (int c = 0; c < COMPONENTS; c++) {
entry[pos++] = 'a';
entry[pos++] = '/';
}
entry[pos++] = 'x';
entry[pos++] = (char)('0' + i);
entry[pos] = '\0';
entries[i] = entry;
}
PathIndex index;
EXPECT_TRUE(path_index_build(&index, entries, COUNT));
EXPECT_EQ_INT((int)index.sorted.count, COUNT);
EXPECT_EQ_INT((int)index.exact.size, COUNT);
EXPECT_TRUE(path_index_contains(&index, entries[0]));
EXPECT_FALSE(path_index_contains(&index, "a"));
EXPECT_TRUE(path_index_has_descendant(&index, "a"));
EXPECT_TRUE(path_index_has_descendant(&index, "a/a"));
EXPECT_FALSE(path_index_has_descendant(&index, "aa"));
path_index_free(&index);
free((void*)entries);
free(storage);
}
static void test_path_index_semantics() {
const char* entries[] = {"a/b/c.txt", "a/b/d.txt", "x.txt", "deep/deeper/deepest"};
PathIndex index;
EXPECT_TRUE(path_index_build(&index, entries, 4));
EXPECT_TRUE(path_index_contains(&index, "a/b/c.txt"));
EXPECT_FALSE(path_index_contains(&index, "a/b"));
EXPECT_TRUE(path_index_contains_n(&index, "a/b/c.txt/ignored", 9));
EXPECT_FALSE(path_index_contains_n(&index, "a/b/c.txt/ignored", 10));
EXPECT_TRUE(path_index_has_descendant(&index, "a"));
EXPECT_TRUE(path_index_has_descendant(&index, "a/b"));
EXPECT_FALSE(path_index_has_descendant(&index, "a/b/c.txt"));
EXPECT_FALSE(path_index_has_descendant(&index, "ab"));
EXPECT_FALSE(path_index_has_descendant(&index, ""));
path_index_free(&index);
/* A zero-entry index answers no queries. */
PathIndex empty;
EXPECT_TRUE(path_index_build(&empty, NULL, 0));
EXPECT_EQ_INT((int)empty.sorted.count, 0);
EXPECT_FALSE(path_index_contains(&empty, "a"));
EXPECT_FALSE(path_index_has_descendant(&empty, "a"));
path_index_free(&empty);
}
void test_shared_utils() { void test_shared_utils() {
test_path_index_bounded();
test_path_index_semantics();
test_walker_removes_extras_keeps_manifest_and_protected(); test_walker_removes_extras_keeps_manifest_and_protected();
test_walker_keeps_nested_manifest_dirs();
test_walker_max_delete_exceeded_deletes_nothing(); test_walker_max_delete_exceeded_deletes_nothing();
test_walker_max_delete_exact_bound_deletes(); test_walker_max_delete_exact_bound_deletes();
test_walker_unlimited_deletes_all(); test_walker_unlimited_deletes_all();
test_walker_hard_bound_all_or_nothing(); test_walker_hard_bound_all_or_nothing();
test_loopback_helpers(); test_loopback_helpers();
test_fd_peer_ip();
/* --append / --append-verify tail-resume math: a resume is eligible only for /* --append / --append-verify tail-resume math: a resume is eligible only for
a shorter existing destination, and the tail length is then the difference. */ a shorter existing destination, and the tail length is then the difference. */
+45
View File
@@ -2,6 +2,7 @@
#include "protocol.h" #include "protocol.h"
#include "test_utils.h" #include "test_utils.h"
#include "transport_tcp.h" #include "transport_tcp.h"
#include <arpa/inet.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <string.h> #include <string.h>
@@ -195,6 +196,49 @@ static void test_client_disconnect_delete() {
client_delete(c); client_delete(c);
} }
/* TCP_NODELAY is enabled by default on a connected transfer socket, and an
* explicit --sockopts TCP_NODELAY=0 still overrides it. */
static void test_tcp_nodelay_default_and_override() {
Server* s = server_create(0);
EXPECT_NOT_NULL(s);
EXPECT_EQ_INT(listen(s->file_descriptor, 1), 0);
struct sockaddr_in bound;
socklen_t bound_len = sizeof(bound);
EXPECT_EQ_INT(getsockname(s->file_descriptor, (struct sockaddr*)&bound, &bound_len), 0);
int port = (int)ntohs(bound.sin_port);
EXPECT_TRUE(port > 0);
Client* c = client_create();
EXPECT_NOT_NULL(c);
EXPECT_TRUE(client_connect(c, "127.0.0.1", port));
int got = 0;
socklen_t len = sizeof(got);
EXPECT_EQ_INT(getsockopt(c->file_descriptor, IPPROTO_TCP, TCP_NODELAY, &got, &len), 0);
EXPECT_EQ_INT(got, 1);
client_disconnect(c);
client_delete(c);
SockOptEntry* entries = NULL;
int count = 0;
EXPECT_EQ_INT(config_sockopts_parse("TCP_NODELAY=0", &entries, &count), 0);
TcpConnectOptions opts;
memset(&opts, 0, sizeof(opts));
opts.sockopts = entries;
opts.sockopt_count = count;
Client* c2 = client_create();
EXPECT_NOT_NULL(c2);
EXPECT_TRUE(client_connect_ex(c2, "127.0.0.1", port, &opts));
got = 0;
len = sizeof(got);
EXPECT_EQ_INT(getsockopt(c2->file_descriptor, IPPROTO_TCP, TCP_NODELAY, &got, &len), 0);
EXPECT_EQ_INT(got, 0);
client_disconnect(c2);
client_delete(c2);
free(entries);
server_delete(&s);
}
void test_transport_tcp() { void test_transport_tcp() {
test_server_create_ephemeral(); test_server_create_ephemeral();
test_server_delete_null(); test_server_delete_null();
@@ -211,4 +255,5 @@ void test_transport_tcp() {
test_sockopts_apply_sets_option(); test_sockopts_apply_sets_option();
test_server_create_bind_address(); test_server_create_bind_address();
test_server_create_bind_ipv6(); test_server_create_bind_ipv6();
test_tcp_nodelay_default_and_override();
} }