Skip to content

fix(daemon): pin the module root before the privilege drop - #7605

Merged
oferchen merged 4 commits into
masterfrom
fix/daemon-module-dirfd-pin
Sep 2, 2026
Merged

oferchen merged 4 commits into
masterfrom
fix/daemon-module-dirfd-pin

Conversation

@oferchen

@oferchen oferchen commented Sep 1, 2026

Copy link
Copy Markdown
Owner

The gap

A daemon module whose path sits under a directory the served uid cannot search - path = /home/backup/data under a 0700 home - could not be served at all. The sender re-walked the absolute module path after setuid, so every lookup re-traversed the unsearchable ancestor:

rsync: [sender] link_stat "." (in m) failed: Permission denied (13)
statx(AT_FDCWD, "/tmp/dmpp/private/mod", AT_STATX_SYNC_AS_STAT|AT_SYMLINK_NOFOLLOW, ...) = -1 EACCES

Upstream 3.5.0 serves the same module fine. It avoids this by ordering, not by permissions.

The upstream shape being mirrored

clientserver.c:1059-1065 enters the module with change_dir(module_chdir, CD_NORMAL) and then pins it,

module_dirfd = open(".", O_RDONLY | O_DIRECTORY | O_CLOEXEC);

both above the setgid/setuid at :1093. Three consumers resolve against that descriptor: flist.c:2035-2059 (the directory scan), sender.c:293-295 (the content open), and syscall.c:85-90 open_anchor_dirfd(), which dup(module_dirfd)s rather than re-resolving the absolute root.

oc had neither half - server_config.rs:289 recorded the reason ("oc has no chdir, so it keeps the absolute form").

What changed

  • fast_io::confinement gains SESSION_ROOT_FD beside the existing SESSION_ROOT: the same root, as a descriptor. install_session drops it whenever it replaces the path, so a pin that is present is always this session's. It stores the root's literal spelling as well as the physical one, because the sender's absolute paths are built from the former and the two differ wherever an ancestor is a symlink.
  • The pin goes through the ownership walk, not a plain open. Upstream's . is the directory change_dir() entered, and a non-chrooted daemon enters it with open_no_attacker_symlinks() (util1.c:1254-1263). A plain open would let a foreign-uid symlink planted at the configured path = redirect the pin - measured: it turns daemon-module-chdir-symlink red, an ancestor-traversal fix becoming a module-root escape.
  • fast_io::pinned_root (new) issues the sender's lstat / stat / opendir against the pin when the path lies beneath it, and is the ordinary std::fs call otherwise. A .. remainder declines to anchor. The stat arm needs O_PATH and is therefore Linux-only; the scan anchors on every Unix.
  • open_trusted_dir duplicates the pin when asked for the pinned root - open_anchor_dirfd() exactly. That is what fixes SourceOpen::open and read_source_link, which reconstructed a module-relative path but then re-opened the root by absolute path on every call, plus the openat2 fast path in open_source_confined.

The three consumers, as fixed

consumer upstream here
walk lstat/stat (walk.rs:593) flist.c:1362 link_stat() on . after change_dir pinned_root::{symlink_metadata,metadata}
walk read_dir + per-child stat flist.c:2028-2059 secure_opendir() pinned_root::read_dir, batch_stat_dir_entries
SourceOpen::open, read_source_link sender.c:293-295, syscall.c:87-88 open_trusted_dir dups the pin

Second symptom, same cause, security-relevant

The Landlock rule-path open ran post-drop too, so restrict_to_module_paths failed with EACCES before adding a single rule. The caller only warns and serves the connection anyway, so the module was served with no kernel sandbox at all - the hardening disappeared on exactly the layout it was added for. The rule path is now opened through the pin.

Measured on the same fixture:

before: oc-rsync warning: module 'm': landlock setup failed for root /tmp/dmpp/private/mod: Permission denied
after:  module 'm': landlock fully enforced over 1 root(s)

Evidence

Linux 7.0 aarch64, upstream testsuite 3.5.0, root/pipe leg.

  • Upstream control - testsuite/daemon-module-private-parent_test.py passes against a real 3.5.0 build.
  • Baseline - the cell fails on master (its manifest row said fail).
  • After - it passes, so tools/ci/upstream-3.5.0-expect.root.txt flips to pass in this commit. The only remaining mismatches on this host, batch-file-symlink and simd-checksum, reproduce identically on an unmodified master build.
  • Mutation - removing only the pin_session_root_fd call and rebuilding restores both symptoms: the link_stat EACCES and landlock setup failed.
  • Ownership-walk half - daemon-module-chdir-symlink is red with a plain-open pin and green with the walk.

The TCP manifest row stays fail. There the cell opens a second connection, and oc's threaded daemon has already applied a process-wide setuid where upstream forks per connection, so the second connection reports @ERROR: module 'm' path does not exist. That is a separate defect and is not touched here.

Tests

Two new cells build the barrier with chmod 0 on the parent rather than a uid drop, so they need no root (and skip as root, where no barrier can exist):

  • pinned_module_root_under_unsearchable_parent - asserts the barrier is real first, then that every anchored lookup crosses it, and that each reverts to EACCES with the pin dropped. Includes a traversable-parent companion and an operator-owned-symlink module root, so a change that broke the ordinary deployment or hardened the pin into a blanket O_NOFOLLOW cannot hide.
  • landlock_rule_under_unsearchable_parent - asserts the ruleset installs (Enforced), which is a distinct claim from "the transfer succeeded": the transfer succeeds either way. Carries the same mutation (Error/EACCES without the pin) and the same traversable-parent companion.

@oferchen
oferchen force-pushed the fix/daemon-module-dirfd-pin branch from 3a58339 to 1a17576 Compare September 1, 2026 11:26
A daemon module whose path sits under a directory the served uid cannot
search (`path = /home/backup/data` under a 0700 home) could not be
served: the sender re-walked the ABSOLUTE module path after `setuid`, so
every lookup re-traversed the unsearchable ancestor and failed with
`link_stat "." (in m) failed: Permission denied`.

Upstream avoids this by ordering, not by permissions. clientserver.c:
1059-1065 enters the module with `change_dir(module_chdir, CD_NORMAL)`
and then pins it - `module_dirfd = open(".", O_RDONLY | O_DIRECTORY |
O_CLOEXEC)` - both ABOVE the setgid/setuid at :1093. Its three consumers
resolve against that descriptor: flist.c:2035-2059 (the scan),
sender.c:293-295 (the content open), and syscall.c:85-90, which
`dup(module_dirfd)`s instead of re-resolving the absolute root.

oc has neither half. It now takes the same pin in the same position and
gives all three consumers the same anchor:

- `fast_io::confinement` gains `SESSION_ROOT_FD` beside `SESSION_ROOT` -
  the same root, as a descriptor. `install_session` drops it whenever it
  replaces the path, so a pin that is present is always this session's.
- The pin is taken through the OWNERSHIP WALK, not a plain open, because
  upstream's `.` is the directory `change_dir()` entered and a
  non-chrooted daemon enters it with `open_no_attacker_symlinks()`
  (util1.c:1254-1263). A plain open would let a foreign-uid symlink
  planted at the configured `path =` redirect the pin, turning an
  ancestor-traversal fix into a module-root escape.
- `fast_io::pinned_root` issues the sender's `lstat`/`stat`/`opendir`
  against the pin when the path lies beneath it, and is the ordinary
  `std::fs` call otherwise. The stat arm needs `O_PATH` and so is
  Linux-only; the scan anchors on every Unix.
- `open_trusted_dir` duplicates the pin when asked for the pinned root,
  mirroring `open_anchor_dirfd()`. That covers `SourceOpen::open` and
  `read_source_link`, both of which re-opened the root by absolute path
  on every call, plus the `openat2` fast path in `open_source_confined`.

Same cause, second symptom, security-relevant: the Landlock rule-path
open ran post-drop too, so `restrict_to_module_paths` failed with EACCES
and the caller only WARNED - the module was then served with no kernel
sandbox at all, on exactly the layout the sandbox was added for. The
rule path is now opened through the pin as well.

Measured on Linux 7.0 aarch64, upstream testsuite 3.5.0 root/pipe:
`daemon-module-private-parent` fails on master and passes here, so its
expect-manifest row flips in this commit. Removing only the
`pin_session_root_fd` call restores both symptoms - the `link_stat`
EACCES and `landlock setup failed` - and `daemon-module-chdir-symlink`
pins the ownership-walk half. The TCP row stays `fail`: there the cell's
second connection hits a separate defect (the threaded daemon's `setuid`
is process-wide and persists across connections, where upstream forks).

Two new cells build the barrier with `chmod 0` rather than a uid drop,
so they run without root: one asserts every anchored lookup survives it
and reverts to EACCES with the pin dropped, the other asserts the
Landlock ruleset still installs. Both carry a traversable-parent
companion so a change that broke the ordinary deployment cannot hide.
@oferchen
oferchen force-pushed the fix/daemon-module-dirfd-pin branch from 1a17576 to abf7133 Compare September 1, 2026 12:33
The parenthetical "without granting - or requiring - any access" wrapped
so that "- or requiring" began a line, which markdown parses as a list
item and turns the four following lines into lazy continuations.
doc_lazy_continuation is deny-level under -D warnings, so the whole
crate failed to compile in the lint job.

Reflowed so the hyphen stays mid-line. Prose is unchanged.
oferchen added a commit that referenced this pull request Sep 1, 2026
…ng them

`resolve_upstream_path` returns `ForeignUpstream` for any cited path carrying a
`/` that is not a manifest key, on the rule that a directory rsync does not ship
names a different project. A citation written as
`target/interop/upstream-src/rsync-3.5.0/delete.c:191` matches that rule, so the
gate skips it - and the skip `continue`s above `tally.names_checked`, so it
leaves no trace in a report whose stated job is telling "clean" apart from
"examined nothing".

Measured on master at 92e50ab: 8718 names and 8718 ranges checked, and 379
citations skipped. 113 of those 379 name the PINNED release - 34 spelling the
full unpack prefix, 79 spelling the bare `rsync-3.5.0/` directory - so their
line numbers have never been checked by anything. That set includes the
citations #7601, #7603 and #7605 have just rewritten: the retarget sweep landed
its new line numbers into the one shape the gate does not read.

Three changes:

- A leading `[target/interop/upstream-src/]rsync-<VER>/` is split off before
  resolution, so a prefixed citation of the pinned release resolves to a
  manifest key and is range-checked exactly as the bare form is. A `rsync-`
  directory that is not a version number is not stripped.
- A citation naming any other release becomes `Violation::UnpinnedRelease`, a
  hard failure. It names rsync at a version this tree no longer builds against,
  which is a stale citation, not an out-of-scope one.
- Under a pinned prefix, a tail that resolves nowhere is `Missing` rather than
  `ForeignUpstream`: the prefix asserts the release outright, so it cannot be
  another project.
- The skip is counted before the `continue`, and `execute` prints the skipped
  and permitted totals, so a population that grows again grows in the open.

After: 8831 names and 8831 ranges checked, 10 skipped - all of them zsync's
genuinely foreign `librcksum/`.

Two exemptions from the unpinned-release arm, both scoped, neither covering the
phantom-file or out-of-range arms. `CONTRIBUTING.md` and
`xtask/src/commands/citations.rs` document this gate and have to be able to
spell a rejected citation to show what it rejects. `docs/` mixes live reference
with historical record, and a note describing upstream at 3.4.1 is accurate
because it names the old release; 253 citations across 70 files there name a
non-pinned release, and sorting record from reference needs a reader per file.

Non-vacuity, both arms, end to end. A probe file citing
`rsync-3.4.1/flist.c:999999`, `rsync-3.5.0/flist.c:999999` and the bare
`rsync-3.5.0/` spelling of the same - a line no rsync release has - was added to
the tree. Against master's gate: exit 0, tally `8718 name(s) and 8718 line
range(s)`, byte for byte the tally without the probe, so the probe was invisible
rather than tolerated. Against this gate: exit 1, the 3.4.1 line reported as
unpinned and both 3.5.0 spellings reported as out of range, and those three are
the only violations in the tree. The probe was removed before commit.

Also sweeps nine citations of `checksum.c:285`, which claims the `schar *buf`
cast. At the pin that cast is line 307; 285 is now `case CSUM_SHA256:` inside a
digest switch. #7601 corrected three instances of this string and left nine,
which is the failure mode the gate's own docs name: a finder only surfaces what
its heuristic matched, so working a finding list is not a sweep. All nine are in
range and were green under both gates.
`Windows (nightly)` failed to compile the daemon lib with

    error[E0425]: cannot find function `pin_session_root_fd`
                  in module `fast_io::confinement`

`fast_io::confinement` splits its surface deliberately: the held-dirfd
family (`pin_session_root_fd`, `clear_session_root_fd`,
`pinned_root_fd_for`, `session_root_is_pinned`, `pinned_root_relative`)
is `#[cfg(unix)]`, while the policy functions (`install_session`,
`outside_session_root`, `session_confinement_root`, ...) are
cross-platform. This branch added a call to the unix-only half from
unconditional daemon code.

The call site is gated to match, so the policy half above it still runs
everywhere. A Windows arm returning `Ok(())` was deliberately NOT added:
there is no dirfd to hold and no setgid/setuid drop to precede, so it
would report a pin that never happened - the opposite of what the
adjacent warning exists to tell the operator - and it would answer the
open question of what path confinement means on Windows as a side
effect of a build fix.

Verified by cross-compiling the failing target on macOS: `cargo check
-p daemon --target x86_64-pc-windows-gnu` succeeds with the gate and
reproduces E0425 verbatim without it. `cargo check -p daemon` on unix
is unaffected.
oferchen added a commit that referenced this pull request Sep 2, 2026
…ng them (#7607)

`resolve_upstream_path` returns `ForeignUpstream` for any cited path carrying a
`/` that is not a manifest key, on the rule that a directory rsync does not ship
names a different project. A citation written as
`target/interop/upstream-src/rsync-3.5.0/delete.c:191` matches that rule, so the
gate skips it - and the skip `continue`s above `tally.names_checked`, so it
leaves no trace in a report whose stated job is telling "clean" apart from
"examined nothing".

Measured on master at 92e50ab: 8718 names and 8718 ranges checked, and 379
citations skipped. 113 of those 379 name the PINNED release - 34 spelling the
full unpack prefix, 79 spelling the bare `rsync-3.5.0/` directory - so their
line numbers have never been checked by anything. That set includes the
citations #7601, #7603 and #7605 have just rewritten: the retarget sweep landed
its new line numbers into the one shape the gate does not read.

Three changes:

- A leading `[target/interop/upstream-src/]rsync-<VER>/` is split off before
  resolution, so a prefixed citation of the pinned release resolves to a
  manifest key and is range-checked exactly as the bare form is. A `rsync-`
  directory that is not a version number is not stripped.
- A citation naming any other release becomes `Violation::UnpinnedRelease`, a
  hard failure. It names rsync at a version this tree no longer builds against,
  which is a stale citation, not an out-of-scope one.
- Under a pinned prefix, a tail that resolves nowhere is `Missing` rather than
  `ForeignUpstream`: the prefix asserts the release outright, so it cannot be
  another project.
- The skip is counted before the `continue`, and `execute` prints the skipped
  and permitted totals, so a population that grows again grows in the open.

After: 8831 names and 8831 ranges checked, 10 skipped - all of them zsync's
genuinely foreign `librcksum/`.

Two exemptions from the unpinned-release arm, both scoped, neither covering the
phantom-file or out-of-range arms. `CONTRIBUTING.md` and
`xtask/src/commands/citations.rs` document this gate and have to be able to
spell a rejected citation to show what it rejects. `docs/` mixes live reference
with historical record, and a note describing upstream at 3.4.1 is accurate
because it names the old release; 253 citations across 70 files there name a
non-pinned release, and sorting record from reference needs a reader per file.

Non-vacuity, both arms, end to end. A probe file citing
`rsync-3.4.1/flist.c:999999`, `rsync-3.5.0/flist.c:999999` and the bare
`rsync-3.5.0/` spelling of the same - a line no rsync release has - was added to
the tree. Against master's gate: exit 0, tally `8718 name(s) and 8718 line
range(s)`, byte for byte the tally without the probe, so the probe was invisible
rather than tolerated. Against this gate: exit 1, the 3.4.1 line reported as
unpinned and both 3.5.0 spellings reported as out of range, and those three are
the only violations in the tree. The probe was removed before commit.

Also sweeps nine citations of `checksum.c:285`, which claims the `schar *buf`
cast. At the pin that cast is line 307; 285 is now `case CSUM_SHA256:` inside a
digest switch. #7601 corrected three instances of this string and left nine,
which is the failure mode the gate's own docs name: a finder only surfaces what
its heuristic matched, so working a finding list is not a sweep. All nine are in
range and were green under both gates.
@oferchen
oferchen merged commit b494b81 into master Sep 2, 2026
74 checks passed
@oferchen
oferchen deleted the fix/daemon-module-dirfd-pin branch September 2, 2026 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant