Skip to content

fix: walk unchrooted daemon module roots and destinations - #8904

Merged
oferchen merged 1 commit into
masterfrom
fix/daemon-module-change-dir
Nov 28, 2026
Merged

oferchen merged 1 commit into
masterfrom
fix/daemon-module-change-dir

Conversation

@oferchen

@oferchen oferchen commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Summary

A daemon serving a module with use chroot = no had two gaps against rsync 3.5.2, one destructive bug, and one pre-existing rendering defect on its privileged leg.

Data loss: an opted-out module's symlinked root was replaced. With insecure links = yes and path = naming a symlink, oc replaced the operator's symlink with a new empty directory and wrote the transfer into it. rsync 3.5.2 follows the link. oc now does the same: the symlink is still there afterward and the data lands in its target.

The module root was never walked by a rootless daemon. rsync 3.5.2 enters the module root with change_dir(module_chdir) on every session (clientserver.c:1063). For an unchrooted module that is the ownership walk (util1.c:1351-1361): a symlink in path = owned by another user is refused. The client gets @ERROR: chdir failed, and the daemon log gets chdir <path> failed (clientserver.c:692-699). oc had this walk, but apply_privilege_restrictions_with_upstream_errors returned early when the session neither chrooted nor dropped privileges. A rootless daemon therefore served a foreign-owned symlinked module root, on push and pull alike. The early path now runs the same walk and keeps the root's descriptor. It does not chdir (see "Working directory" below).

The destination beneath the root was not walked either. rsync 3.5.2 enters the client's destination with the confined walk secure_relative_dirfd() (util1.c:1430-1446). That walk follows an in-tree relative symlink and refuses one that leaves the module with ELOOP, whoever owns it. get_local_name() then prints change_dir#1/#2/#3 "<dest>" (in <module>) failed and exits 3. Under --mkpath it prints mkdir ... failed and exits 11 (main.c:757-869). oc instead did a plain stat and failed later in its anchored sandbox, with either an oc-only code-11 line or a landlock EACCES. The receiver now walks a daemon destination beneath the module root's descriptor and prints 3.5.2's lines.

Pre-existing defect on the privileged leg. A root daemon serving an unchrooted module with uid = enters the module root and served the transfer as .. Two renderers took the module root from that spelling:

  • full_fname() rendered a missing source as link_stat "/d/d/a1" (in m). 3.5.2 prints "/d/a1" (util1.c:1540-1547, p1 = curr_dir + module_dirlen).
  • A relative merge-file name was not joined onto the module root. oc logged failed to open exclude file etc/hostname; 3.5.2 logs <module path>/etc/hostname (exclude.c:735-746).

Both reproduce on master (c87bc25) under sudo. Both renderers now read one absolute module root, recorded once per session.

insecure links = yes keeps the plain open for both the root and the tail (syscall.c:139, :433-436; util1.c:1447-1451). A chrooted module also keeps the plain chdir for the tail.

Difference from rsync 3.5.2's process model

The observable semantics are 3.5.2's, taken from its C source:

  • the full_fname() strip (util1.c:1543)
  • the merge-file join (exclude.c:735-746)
  • entering the module root before the privilege drop (clientserver.c:1063)

Only the mechanism differs, in one place. A rootless unchrooted session does not change directory; it walks relative to the module root's descriptor, because oc's threaded async daemon cannot share a process working directory.

  • What 3.5.2 does: every session runs change_dir(module_chdir, CD_NORMAL) (clientserver.c:1063). That is safe there because each connection is its own process: start_accept_loop() forks per connection (socket.c:761).
  • Why oc differs: under --features async-daemon, oc serves sessions as threads of one process, and those threads share one working directory. A chdir/fchdir in one session would move every other session.
  • What oc does instead: a rootless session runs the same ownership walk, keeps the module root's descriptor, and resolves the client's destination beneath that descriptor.
  • Privileged sessions are unchanged: they still fchdir into the module root before the privilege drop, as 3.5.2 does (clientserver.c:1063 before the setgid/setuid at :1102 and :1127). A module below a directory its uid cannot search works only that way.
  • No async session reaches that fchdir: a privileged session always runs in its own forked process, because the async daemon refuses uid, gid, use chroot and running as root (daemon.rs, run_async_daemon). No new task is needed.

Client-visible output is byte-identical to 3.5.2. Both renderers take the module root from its absolute path, so they print what 3.5.2 prints from its working directory:

  • full_fname() strips the module root the way util1.c:1540-1547 does (p1 = curr_dir + module_dirlen).
  • A relative merge-file name is joined onto the module root, as exclude.c:735-746 joins it onto dirbuf.

The oracle cells compare every line against 3.5.2, in both legs and in all four client/daemon pairings.

Changes

  • transfer config/served_root.rs: new ServedRoot. It holds the module root's absolute path, its canonical form, whether the session runs from that directory, and the root's descriptor. It is set once per connection and replaces daemon_module_dir.
  • transfer full_fname.rs: FullFnamePaths::daemon takes the ServedRoot. The module strip always uses the absolute path. A session served from its working directory roots the relative spellings first; the sender's paths and curr_dir are both cwd-space (.sender()), the receiver's curr_dir only.
  • transfer receiver/dest_root.rs: new Entry::Confined(ModuleRoot); Entry::Walked is Unix-only, as its only constructor is. The destination is walked beneath the root's descriptor:
    • change_dir#1 and Add coverage for duplicate protocol offers in negotiation #3 through open_dir_beneath
    • the change_dir#2 mkdir through create_dir_beneath
    • --mkpath through make_path_beneath, whose ELOOP stays make_path()'s own failure (exit 11), as the relative make_path of 3.5.2 reports it (syscall.c:2306-2332)
    • an ELOOP from change_dir is the refusal that names the step
  • transfer receiver/directory/mod.rs: the destination root itself is stat'd through a symlink that names it. Upstream stats . in the directory it entered (main.c:776-781). Without this, serving the absolute path brought the opted-out data-loss bug back.
  • transfer setup/context.rs: destination_entry picks Confined for an unchrooted daemon module that has not opted out. The merge-name filter strips the canonical and the absolute module root.
  • daemon module_access/transfer/sandbox.rs:
    • The rootless early path walks and opens the module root with no chdir.
    • The privileged path pins, then fchdirs, as before. Both return the root's descriptor for the ServedRoot.
    • On a refusal it logs the walk's own notice, then [Receiver] chdir <path> failed: <strerror> (<errno>) in rsyserr form (clientserver.c:695).
  • daemon orchestration.rs, server_config.rs: build the ServedRoot and hand it to the server config. A relative merge-file name is joined onto the absolute root (exclude.c:735-746).
  • daemon module_access/helpers.rs: log_pending_diagnostics drains session diagnostics into the daemon log. It is shared with streams.rs, so a notice raised before the transfer reaches the log, not stderr.
  • daemon module_access/transfer/streams.rs: a fatal cause and its trailer are written to the daemon log before they are framed for the client. This matches upstream rwrite(), which calls logit() before sending (log.c:390-407).
    • A 3.5.2 client exits on the first error frame and kills its remote-shell child (cleanup.c:203-204 kill_all()). Over a remote-shell daemon, that child is the oc server, so the line never reached the log.
    • Measured: the cell with a 3.5.2 client and an oc daemon over a remote shell failed on that log line before this change.
  • fast_io at_syscalls/create.rs: open_dir_beneath, create_dir_beneath and make_path_beneath take the anchor descriptor. EINVAL for an absolute path or .., as at syscall.c:3477-3484. make_path_beneath_cwd delegates to make_path_beneath(CWD, ...). The cwd-only open_dir_beneath_cwd and create_dir_beneath_cwd are removed, so there are no duplicates.

Deviations

Case rsync 3.5.2 oc
Push to mod/esc/new/, where esc leaves the module do_mkdir() (main.c:817) follows esc and creates outside/new, then change_dir#2 "esc/new" (in mod) failed: Too many levels of symbolic links (40), exit 3 Refuses before the mkdir: the same change_dir#2 line, exit 3, nothing created outside the module

This deviation is owner-approved and matches #8858 and #8899. Every other cell below matches 3.5.2.

Measured

The 3.5.2 client drove a 3.5.2 daemon and an oc daemon, each with use chroot = no and a global log file. The rootless rows ran the daemon as an unprivileged user, with flink/esc owned by nobody.

Cell 3.5.2 oc before oc after
root-flink (module root is a foreign symlink) @ERROR: chdir failed, exit 5 transfer through the link, exit 0 same as 3.5.2
root-fmid (flink/sub) exit 5 exit 0 same
pull-flink exit 5 exit 0 same
root-olink (own symlink) exit 0 exit 0 same
opt-root-flink (insecure links = yes) follows, exit 0 replaced the symlink with an empty dir same as 3.5.2
tail-rel (rel -> inner) exit 0 exit 0 same
tail-esc / tail-oesc (foreign / own escaping link) change_dir#1 "esc/" (in mod) ... (40), exit 3 oc-only line, exit 11 same line, exit 3
tail-escfile (mod/esc/f) change_dir#3 "esc" (in mod) ... (40), exit 3 exit 11 same
tail-escmk (--mkpath mod/esc/a/b/) mkdir "esc/a/b/" (in mod) ... (40), exit 11 ... Permission denied (13), exit 11 same
tail-escnew see Deviations mkdir ... (13), exit 11 see Deviations
opt-tail-esc (insecure links = yes) follows, exit 0 exit 0 same
priv: pull missing d/a1 (root daemon, uid = the user) link_stat "/d/a1" (in m), exit 23 link_stat "/d/d/a1" (in m) same as 3.5.2
priv: -M--filter=. /etc/hostname log: failed to open exclude file <module>/etc/hostname, exit 11 log: ... etc/hostname same as 3.5.2

Daemon log after the change: line for line equal to 3.5.2's, apart from the program name (oc-rsync:) and the trailer location. That covers the untrusted-symlink notice, [Receiver] chdir <path> failed: ..., each change_dir#N/mkdir line with its trailer, and the merge-file line.

Tests

  • crates/cli/tests/daemon_module_change_dir.rs. It runs each cell oc to oc and, when OC_RSYNC_UPSTREAM_BIN_3_5_2 resolves, with a 3.5.2 client, a 3.5.2 daemon, and both. Results must be equal, except for the deviation cell, which pins oc's daemon. The cells cover:
    • the three refused tails (exit code, line, trailer, daemon-log line, nothing written outside)
    • the new-directory deviation
    • in-tree and opted-out links
    • a Linux-only foreign-owned module root: @ERROR: chdir failed, exit 5, nothing written, and the untrusted-symlink notice followed by the chdir <path> failed log line
    • a Linux-only opted-out foreign root (the data-loss bug): exit 0, the root is still a symlink to the same target, and the data is in the target
    • Linux-only privileged leg under sudo -n: a missing source renders link_stat "/inner/a1" (in priv), and the merge file is logged as <module>/etc/hostname. Red against the master binary ("/inner/inner/a1"), green after.
    • Linux-only privileged module below a directory its uid cannot search (mode 000): push, pull and a new destination directory all exit 0 and land in the module. This pins why the privileged session keeps its working-directory entry.
    • The root and privileged cells need sudo -n. They fail rather than skip when it is unavailable.
  • Log-line cells compare each daemon log line byte for byte with 3.5.2's line. Only the program name is substituted (rsync vs oc-rsync), and the log stamp before ] is stripped. A trailer is compared up to its source location, which names each program's own file and line.
  • crates/daemon/src/tests/chunks/daemon_async_push_pull.rs: concurrent_async_sessions_keep_their_own_module_roots_and_the_process_cwd. Two sessions on different modules run at once in one async daemon. Each pushes 200 files and pulls a seed from its own module, and the process working directory is unchanged afterwards. Red on this PR's first head (a7ac723), where the rootless path fchdir'd: the cwd moved to module-a. Green on master and after.
  • crates/fast_io/tests/daemon_destination_beneath_root.rs (renamed from ..._beneath_cwd.rs): the three walks beneath a descriptor, with no chdir. Covers the root and an in-tree link, an escape, EINVAL, mkdir errors, a whole make_path, nothing made outside, and a cell showing the anchor, not the cwd, decides where a name resolves.
  • full_fname.rs unit tests: a session served from its cwd renders the sender's ./d/a1 once below the root; the receiver roots only its curr_dir; a session not served from its cwd roots nothing.
  • Before and after: on the base sources, 4 of the 5 rootless cli cells fail. The foreign module root exits 0, the opted-out root's symlink is replaced, and the tails and the new dir exit 11 with oc's own lines. The in-tree and opted-out-tail cell passes on both, as it should. After the change all pass.
  • Mutations, each red on the new cells:
    • (a) plain open of the module root: the root cells red (measured before the cell split, 4/4)
    • (b) the tail follows the escaping link: only the 2 tail cells red
    • (c) swapped step mapping, change_dir#2 reported as Add protocol negotiation utilities #1: only the new-dir cell red
  • Measured locally on Linux, with OC_RSYNC_UPSTREAM_COMPAT=1 and the 3.5.2 oracle (cargo nextest run -p <crate> --all-features):
    • cli: 3773 passed, 2 skipped
    • bin: 1092 passed, 29 skipped (with the 3.5.1 oracle as well)
    • fast_io: 1381 passed, 1 skipped
    • transfer: 3515 passed, 0 skipped
    • daemon: 2209 passed, 20 skipped
    • The skips are existing ones. None of the new cells skips; the root and privileged cells fail without sudo -n.
  • Gates:
    • fmt and check_rustfmt_all
    • workspace clippy -D warnings on the host and on x86_64-pc-windows-gnu
    • rustdoc -D warnings
    • no-placeholders
    • citation_drift_audit --ratchet

Follow-ups

  • A chrooted module with a /./ inner path (module_dirlen > 0). 3.5.2 confines the tail there too (util1.c:1430). oc keeps its anchored sandbox for chrooted modules, unchanged here.

@oferchen oferchen added this to the 0.6.5 milestone Nov 28, 2026
@github-actions github-actions Bot added the bug Something isn't working label Nov 28, 2026
@oferchen
oferchen force-pushed the fix/daemon-module-change-dir branch from a7ac723 to 919e657 Compare November 28, 2026 15:51
rsync 3.5.2 enters the module root with change_dir(module_chdir) for
every session (clientserver.c:1063). For use chroot = no that is the
ownership walk (util1.c:1351-1361): a symlink in the module path owned
by another user is refused, and the client gets @error: chdir failed.

oc ran that walk only when the session also chrooted or dropped
privileges. A rootless daemon with use chroot = no returned early and
served the module through the plain path, so a foreign-owned symlink
in path = was followed on push and pull alike. The early path now runs
the same walk and keeps the root's descriptor. It does not chdir: a
session served as a thread of an async daemon would move every other
session's working directory. The refusal is logged as 3.5.2 logs it:
the walk's untrusted-symlink notice, then "[Receiver] chdir <path>
failed: <errno text>" (clientserver.c:695).

A privileged session still enters the module root before its privilege
drop, as upstream does (clientserver.c:1063 before :1102, :1127), so a
module below a directory its uid cannot search keeps working.

Inside the module, 3.5.2 enters the client's destination with the
confined walk, secure_relative_dirfd() and fchdir() (util1.c:1430-1446).
An in-tree relative symlink is followed; one that leaves the module is
refused with ELOOP whoever owns it. get_local_name() reports that as
change_dir#1, #2 or #3 "<dest>" (in <module>) and exits 3, or as the
make_path() mkdir failure with exit 11 under --mkpath. oc did a plain
stat and failed later with its own code-11 line. The receiver now walks
a daemon destination beneath the module root's descriptor through
fast_io's open_dir_beneath, create_dir_beneath and make_path_beneath,
and prints 3.5.2's lines.

Each session records its module root once as a ServedRoot: the
absolute module path, its canonical form and the root's descriptor.
full_fname() strips the module root from that path, as util1.c:1543
strips module_dirlen from curr_dir, and a relative merge-file name is
joined onto it (exclude.c:735-746). This also fixes the privileged
leg, which served "." and printed link_stat "/d/d/a1" and "failed to
open exclude file etc/hostname" where 3.5.2 prints "/d/a1" and the
module-rooted name.

insecure links = yes keeps the plain open for the root and the tail
(syscall.c:139, :433-436), so an opted-out module's symlinked root is
followed instead of being replaced by a new directory.

The daemon now logs a fatal cause and trailer before framing them for
the client, as rwrite() calls logit() before sending (log.c:390-407).
A 3.5.2 client kills its remote-shell child on exit (cleanup.c:203-204),
so a daemon reached that way lost the line when it logged afterwards.

Deliberate deviation: 3.5.2's do_mkdir() (main.c:817) follows an
escaping link and makes a new destination outside the module before
change_dir#2 refuses it. oc refuses first, with the same line and exit
code, and makes nothing.
@oferchen
oferchen force-pushed the fix/daemon-module-change-dir branch from 919e657 to 5daed2c Compare November 28, 2026 16:52
@oferchen
oferchen merged commit 413543b into master Nov 28, 2026
91 checks passed
@oferchen
oferchen deleted the fix/daemon-module-change-dir branch November 28, 2026 17:35
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