Skip to content

fix(logging): escape log-file lines in the sink, not the CLI renderer - #7357

Merged
oferchen merged 1 commit into
masterfrom
fix/log-file-escaping-in-sink
Aug 15, 2026
Merged

oferchen merged 1 commit into
masterfrom
fix/log-file-escaping-in-sink

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Problem

Upstream rsync has exactly one writer to the log file: logit() (log.c:126), which hands every line to filtered_fwrite(logfile_fp, buf, len, 0, 1, trailing) at log.c:132. Escaping in the sink is what makes it unbypassable - a producer cannot forget it, because there is no other way to reach the file.

oc-rsync escaped in the CLI out-format renderer instead. That covered --log-file on the client and nothing else. The daemon writes its log through an entirely different path, so a peer-supplied operand reached the log with a raw ESC where rsync 3.5.0 writes \#033:

module 'm' from localhost (127.0.0.1): client args: --server --sender ... m/no<ESC>pe

Every line the sink emits carries a genuine timestamp and pid, so a name carrying a newline could open a second, entirely authentic-looking record (CWE-117).

Change

Move the escaper from crates/cli/src/frontend/escape.rs down to crates/logging-sink/src/escape.rs, and apply it inside LogFileWriter - the one type both the CLI (summary.rs) and the daemon (daemon.rs, privilege.rs, hardening.rs, module_access) construct for a log file. logging-sink was already a dependency of both, so nothing new enters the crate graph.

The renderer switches to a new EscapeStyle::passthrough. Applying both would double-escape: the sink's \#ddd guard (log.c:252-254) fires on the renderer's own output and turns \#033 into \#134#033 - which is exactly what the existing oracle test reported when only half the change was in place.

Two consequences fall out of putting it in the sink:

  • EscapeStyle gains an escape_controls switch. Upstream has no such switch because every filtered_fwrite call site escapes C0 controls. oc-rsync needs one because it now has a sink upstream does not - the passthrough style, whose whole job is to leave the bytes alone.
  • The Write adapter treats a newline as a record terminator only when it ends the write. A newline inside the message is escaped as \#012, matching log.c:128-129, which strips exactly one trailing newline. The previous byte-stream splitting turned an embedded newline into a second prefixed line - the injection this defends against. Producers already deliver one message per call (MessageSink, and writeln! on the CLI side), so this is the boundary upstream works on.

Verification

  • New tests/daemon_log_file_escaping.rs pins the half a renderer-side escaper cannot reach: a peer-supplied operand carrying ESC and LF, asserted byte-exactly as A\#033B\#012C, with a printable control operand in the same run so the assertions cannot pass vacuously.
  • RED proven: reverting only the sink escaping (write_escaped -> raw write_all) fails the new test with a raw ESC reached the daemon log file.
  • The existing 3.5.0 oracle test crates/cli/tests/log_file_control_char_escape.rs passes unchanged - that is what proves the move did not perturb the client half.
  • cargo fmt --all -- --check clean; cargo clippy --locked --workspace --all-targets --all-features --no-deps -- -D warnings clean.
  • cargo nextest run -p logging-sink -p cli --all-features: 3974 passed. -p daemon --all-features: 1797 passed.

Closes the residual filed when the log-file escaping port was reviewed.

Upstream has exactly one writer to the log file: `logit()` (log.c:126),
which hands every line to `filtered_fwrite(logfile_fp, buf, len, 0, 1,
trailing)` at log.c:132. Escaping in the sink is what makes it
unbypassable - a producer cannot forget it, because there is no other
way to reach the file.

oc-rsync escaped in the CLI out-format renderer instead. That covered
`--log-file` on the client and nothing else: the daemon writes its log
through an entirely different path, so a peer-supplied operand reached
the log with a raw ESC where rsync 3.5.0 writes `\#33`.

Move the escaper down to `logging_sink::logfile::LogFileWriter`, the one
type both the CLI (summary.rs) and the daemon (daemon.rs, privilege.rs,
hardening.rs, module_access) construct for a log file, and switch the
renderer to the new `EscapeStyle::passthrough`. Applying both would
double-escape: the sink's `\#ddd` guard (log.c:252-254) fires on the
renderer's own output and turns `\#33` into `\#134#033`.

Two consequences fall out of putting it in the sink:

- `EscapeStyle` gains an `escape_controls` switch. Upstream has no such
  switch because every `filtered_fwrite` call site escapes C0 controls;
  oc-rsync needs one because it now has a sink upstream does not - the
  passthrough style, whose whole job is to leave the bytes alone.

- The `Write` adapter now treats a newline as a record terminator only
  when it ends the write. A newline *inside* the message is escaped as
  `\#12`, matching log.c:128-129, which strips exactly one trailing
  newline. This is the injection itself: every line the sink emits gets
  a real timestamp and pid, so a name carrying `\n` would otherwise
  produce a second, entirely authentic-looking record.

Tests: a daemon integration test pins the half a renderer-side escaper
cannot reach (peer-supplied operand in the `client args:` line), with a
printable control operand so the assertions cannot pass vacuously. It
fails with a raw ESC in the log when the sink escaping is reverted. The
existing 3.5.0 oracle test for `--log-file` still passes unchanged,
which is what proves the move did not perturb the client half.
@github-actions github-actions Bot added the bug Something isn't working label Aug 15, 2026
@oferchen
oferchen merged commit 231b024 into master Aug 15, 2026
68 checks passed
@oferchen
oferchen deleted the fix/log-file-escaping-in-sink branch August 15, 2026 17:20
oferchen added a commit that referenced this pull request Aug 24, 2026
…#7357)

Upstream has exactly one writer to the log file: `logit()` (log.c:126),
which hands every line to `filtered_fwrite(logfile_fp, buf, len, 0, 1,
trailing)` at log.c:132. Escaping in the sink is what makes it
unbypassable - a producer cannot forget it, because there is no other
way to reach the file.

oc-rsync escaped in the CLI out-format renderer instead. That covered
`--log-file` on the client and nothing else: the daemon writes its log
through an entirely different path, so a peer-supplied operand reached
the log with a raw ESC where rsync 3.5.0 writes `\#33`.

Move the escaper down to `logging_sink::logfile::LogFileWriter`, the one
type both the CLI (summary.rs) and the daemon (daemon.rs, privilege.rs,
hardening.rs, module_access) construct for a log file, and switch the
renderer to the new `EscapeStyle::passthrough`. Applying both would
double-escape: the sink's `\#ddd` guard (log.c:252-254) fires on the
renderer's own output and turns `\#33` into `\#134#033`.

Two consequences fall out of putting it in the sink:

- `EscapeStyle` gains an `escape_controls` switch. Upstream has no such
  switch because every `filtered_fwrite` call site escapes C0 controls;
  oc-rsync needs one because it now has a sink upstream does not - the
  passthrough style, whose whole job is to leave the bytes alone.

- The `Write` adapter now treats a newline as a record terminator only
  when it ends the write. A newline *inside* the message is escaped as
  `\#12`, matching log.c:128-129, which strips exactly one trailing
  newline. This is the injection itself: every line the sink emits gets
  a real timestamp and pid, so a name carrying `\n` would otherwise
  produce a second, entirely authentic-looking record.

Tests: a daemon integration test pins the half a renderer-side escaper
cannot reach (peer-supplied operand in the `client args:` line), with a
printable control operand so the assertions cannot pass vacuously. It
fails with a raw ESC in the log when the sink escaping is reverted. The
existing 3.5.0 oracle test for `--log-file` still passes unchanged,
which is what proves the move did not perturb the client half.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant