Skip to content

feat: add platform crate for unsafe code isolation - #3042

Merged
oferchen merged 13 commits into
masterfrom
feature/platform-crate-unsafe-isolation
Mar 29, 2026
Merged

oferchen merged 13 commits into
masterfrom
feature/platform-crate-unsafe-isolation

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Introduces the platform crate to absorb all platform-specific unsafe FFI from the daemon crate, making the daemon 100% safe Rust (zero #[allow(unsafe_code)] annotations)
  • Uses nix 0.29 for safe Unix wrappers (chroot, setuid, setgid, getuid, setsid, dup2, close, open), keeping #[allow(unsafe_code)] only for fork, macOS setgroups, and Windows FFI
  • Fixes metadata id_lookup to be cross-platform by restructuring the module with nss_stub.rs for non-unix platforms

Platform crate modules

Module Safe API Unsafe surface
privilege apply_chroot, drop_privileges macOS libc::setgroups, Windows LogonUserW
group lookup_group_members libc::getgrnam_r, gr_mem traversal
signal SignalFlags, register_signal_handlers Windows SetConsoleCtrlHandler
daemonize become_daemon, redirect_stdio_to_devnull libc::fork only
secrets check_secrets_file_permissions None (nix safe wrapper)
name_resolution name_to_rid, rid_to_name Windows LookupAccountNameW
env EnvGuard env::set_var/remove_var (Rust 2024)

Daemon changes

All 17 #[allow(unsafe_code)] annotations removed across 7 files. Daemon now delegates to platform crate for all FFI, keeping only orchestration and logging.

Test plan

  • cargo check -p platform passes on all platforms
  • cargo check -p daemon passes with zero #[allow(unsafe_code)]
  • Platform crate tests pass (cargo nextest run -p platform)
  • Daemon crate tests pass (cargo nextest run -p daemon)
  • All CI matrices pass (Linux, macOS, Windows)
  • Metadata compiles on all platforms (id_lookup cross-platform fix)

Remove #[cfg(unix)] gate on id_lookup module, restructure so converter.rs
(trait + thread-local) is available on all platforms. NSS libc calls stay
unix-only, with nss_stub.rs providing no-op lookups on non-unix that still
check NAME_CONVERTER_SLOT. Deletes the old id_lookup_stub.rs.
New crate absorbing all platform-specific unsafe FFI from the daemon:
- privilege: chroot, setuid/setgid (nix safe wrappers), setgroups (libc on macOS)
- group: getgrnam_r with ERANGE retry, gr_mem pointer traversal
- signal: SignalFlags + register_signal_handlers (signal-hook on Unix, SetConsoleCtrlHandler on Windows)
- daemonize: fork + setsid + stdio redirect (nix safe wrappers where possible)
- secrets: permission checks using nix::unistd::getuid (zero unsafe)
- name_resolution: Windows LookupAccountNameW + GetSidSubAuthority
- env: EnvGuard for Rust 2024 unsafe env::set_var/remove_var

Uses nix 0.29 for safe Unix wrappers, eliminating most #[allow(unsafe_code)].
Only fork, macOS setgroups, and Windows FFI remain unsafe.
Delegate all platform FFI from daemon to the new platform crate:
- privilege: apply_chroot/drop_privileges -> platform::privilege
- group_expansion: lookup_group_members -> platform::group
- signals: register_signal_handlers -> platform::signal (kept daemon SignalFlags wrapper with From conversion)
- daemonize: become_daemon -> platform::daemonize
- authentication: check_secrets_file_permissions -> platform::secrets
- test_env: EnvGuard re-exported from platform::env

Daemon crate now has zero #[allow(unsafe_code)] annotations.
Removed signal-hook and Win32_System_Console deps (moved to platform).
@github-actions github-actions Bot added the enhancement New feature or request label Mar 29, 2026
oferchen added 10 commits March 29, 2026 09:47
- Move PSID, GetSidSubAuthority, GetSidSubAuthorityCount, LookupAccountNameW
  imports from Foundation/Authorization to Win32::Security (where they live
  in windows 0.61)
- Move LogonUserW, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT from
  Security::Authentication::Identity to Win32::Security
- Add missing Ordering import in Windows signal handler callback
- Use Option<PSID>/Option<PWSTR> for LookupAccountNameW null parameters
- Remove unused Win32_Security_Authentication_Identity and
  Win32_NetworkManagement_NetManagement features
BOOL moved from Win32::Foundation to windows::core in windows 0.61.
CTRL_C_EVENT/CTRL_CLOSE_EVENT/CTRL_BREAK_EVENT are plain u32 constants,
not newtypes with a .0 field.
The comprehensive interop test matrix (3 versions x ~45 scenarios x 2
directions x 5 protocol versions + standalone tests) exceeds the
default 30-minute timeout. This caused the interop job to be cancelled
on both master and feature branches.
These failures exist on master - oc-rsync client hardlink detection in
daemon push mode does not yet produce correct inode/device mapping for
upstream daemon verification. Not a regression from the platform crate
changes.
Upstream flist.c:send_file_entry() only updates lastname at the_end
label for abbreviated followers - the modtime, mode, uid, gid state
variables are NOT updated because the goto skips the metadata writes.

oc-rsync was updating all compression state (including mode, mtime,
uid, gid) for abbreviated followers, causing the sender's state to
diverge from the receiver's. Subsequent entries would have incorrect
XMIT_SAME_MODE/XMIT_SAME_TIME/XMIT_SAME_UID/XMIT_SAME_GID flags,
leading to metadata decode errors and upstream's "ABORTING due to
invalid path from sender" rejection.

Also removes 5 now-passing tests from KNOWN_FAILURES:
- oc:hardlinks, oc:hardlinks-relative (this fix)
- standalone:file-vanished, standalone:info-progress2, standalone:iconv
When upstream rsync pushes with -avi, it sends --log-format=%i as a
server arg. The oc-rsync daemon must parse this to set info_flags.itemize
so the receiver emits MSG_INFO frames with itemize output back to the
client. Also handles --out-format= (canonical name) and --log-format=%i%I
(uppercase I variant for server-side itemize).

upstream: options.c:2750-2761 - server_options() sends --log-format
upstream: options.c:2327-2331 - server parses stdout_format for %i
When the client pushes to an upstream daemon with -avi, itemize lines
must be written to process stdout (upstream rwrite() FCLIENT path),
not sent via MSG_INFO which is the server-to-client direction.

Add ItemizeCallback trait alongside existing TransferProgressCallback.
Thread it through run_server_with_handshake -> GeneratorContext::run ->
run_transfer_loop -> maybe_emit_itemize. In client mode, the callback
writes formatted lines to stdout; in server mode, MSG_INFO is used as
before.

Remove oc:itemize from KNOWN_FAILURES in run_interop.sh.
The -FF flag generates 'dir-merge /.rsync-filter' where the leading '/'
means anchor-to-root. However, the '/' was stored as part of the filename
in DirMergeConfig, causing Path::join() to produce an absolute path
'/.rsync-filter' instead of '<directory>/.rsync-filter' when entering
directories during file list walk.

Fix: strip leading '/' from the filename at parse time (CLI) and at wire
decode time (generator), setting anchor_root flag instead. This matches
upstream exclude.c semantics where leading '/' on merge filenames means
the file is only looked for in the transfer root directory.

Remove oc:merge-filter from interop KNOWN_FAILURES.
…op failures

Both tests should now pass after recent fixes:
- Batch mode: delta token stream decoding (d9c7cc6), daemon batch
  wiring (333a1a5), header/flist parsing (fa0fd67, 4b6a692)
- Large file: 64-bit varint encoding (330d9e0), wire format
  corrections (d524b0d)

KNOWN_FAILURES reduced from 7 to 5 (all environment-dependent:
ACLs, xattrs, protocol-31).
@oferchen
oferchen merged commit e161c97 into master Mar 29, 2026
42 of 43 checks passed
@oferchen
oferchen deleted the feature/platform-crate-unsafe-isolation branch March 29, 2026 20:59
oferchen added a commit that referenced this pull request May 1, 2026
* fix: make metadata id_lookup cross-platform for Windows compilation

Remove #[cfg(unix)] gate on id_lookup module, restructure so converter.rs
(trait + thread-local) is available on all platforms. NSS libc calls stay
unix-only, with nss_stub.rs providing no-op lookups on non-unix that still
check NAME_CONVERTER_SLOT. Deletes the old id_lookup_stub.rs.

* feat: add platform crate for unsafe code isolation

New crate absorbing all platform-specific unsafe FFI from the daemon:
- privilege: chroot, setuid/setgid (nix safe wrappers), setgroups (libc on macOS)
- group: getgrnam_r with ERANGE retry, gr_mem pointer traversal
- signal: SignalFlags + register_signal_handlers (signal-hook on Unix, SetConsoleCtrlHandler on Windows)
- daemonize: fork + setsid + stdio redirect (nix safe wrappers where possible)
- secrets: permission checks using nix::unistd::getuid (zero unsafe)
- name_resolution: Windows LookupAccountNameW + GetSidSubAuthority
- env: EnvGuard for Rust 2024 unsafe env::set_var/remove_var

Uses nix 0.29 for safe Unix wrappers, eliminating most #[allow(unsafe_code)].
Only fork, macOS setgroups, and Windows FFI remain unsafe.

* feat: wire daemon crate to platform, remove all #[allow(unsafe_code)]

Delegate all platform FFI from daemon to the new platform crate:
- privilege: apply_chroot/drop_privileges -> platform::privilege
- group_expansion: lookup_group_members -> platform::group
- signals: register_signal_handlers -> platform::signal (kept daemon SignalFlags wrapper with From conversion)
- daemonize: become_daemon -> platform::daemonize
- authentication: check_secrets_file_permissions -> platform::secrets
- test_env: EnvGuard re-exported from platform::env

Daemon crate now has zero #[allow(unsafe_code)] annotations.
Removed signal-hook and Win32_System_Console deps (moved to platform).

* fix: correct Windows API paths for windows crate 0.61

- Move PSID, GetSidSubAuthority, GetSidSubAuthorityCount, LookupAccountNameW
  imports from Foundation/Authorization to Win32::Security (where they live
  in windows 0.61)
- Move LogonUserW, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT from
  Security::Authentication::Identity to Win32::Security
- Add missing Ordering import in Windows signal handler callback
- Use Option<PSID>/Option<PWSTR> for LookupAccountNameW null parameters
- Remove unused Win32_Security_Authentication_Identity and
  Win32_NetworkManagement_NetManagement features

* fix: use windows::core::BOOL and plain u32 ctrl events in signal handler

BOOL moved from Win32::Foundation to windows::core in windows 0.61.
CTRL_C_EVENT/CTRL_CLOSE_EVENT/CTRL_BREAK_EVENT are plain u32 constants,
not newtypes with a .0 field.

* ci: increase interop job timeout from 30 to 45 minutes

The comprehensive interop test matrix (3 versions x ~45 scenarios x 2
directions x 5 protocol versions + standalone tests) exceeds the
default 30-minute timeout. This caused the interop job to be cancelled
on both master and feature branches.

* ci: add oc:hardlinks and oc:hardlinks-relative to known interop failures

These failures exist on master - oc-rsync client hardlink detection in
daemon push mode does not yet produce correct inode/device mapping for
upstream daemon verification. Not a regression from the platform crate
changes.

* fix: skip metadata state update for abbreviated hardlink followers

Upstream flist.c:send_file_entry() only updates lastname at the_end
label for abbreviated followers - the modtime, mode, uid, gid state
variables are NOT updated because the goto skips the metadata writes.

oc-rsync was updating all compression state (including mode, mtime,
uid, gid) for abbreviated followers, causing the sender's state to
diverge from the receiver's. Subsequent entries would have incorrect
XMIT_SAME_MODE/XMIT_SAME_TIME/XMIT_SAME_UID/XMIT_SAME_GID flags,
leading to metadata decode errors and upstream's "ABORTING due to
invalid path from sender" rejection.

Also removes 5 now-passing tests from KNOWN_FAILURES:
- oc:hardlinks, oc:hardlinks-relative (this fix)
- standalone:file-vanished, standalone:info-progress2, standalone:iconv

* fix: parse --log-format=%i in daemon to enable itemize MSG_INFO emission

When upstream rsync pushes with -avi, it sends --log-format=%i as a
server arg. The oc-rsync daemon must parse this to set info_flags.itemize
so the receiver emits MSG_INFO frames with itemize output back to the
client. Also handles --out-format= (canonical name) and --log-format=%i%I
(uppercase I variant for server-side itemize).

upstream: options.c:2750-2761 - server_options() sends --log-format
upstream: options.c:2327-2331 - server parses stdout_format for %i

* feat: wire client-side itemize output for daemon push transfers

When the client pushes to an upstream daemon with -avi, itemize lines
must be written to process stdout (upstream rwrite() FCLIENT path),
not sent via MSG_INFO which is the server-to-client direction.

Add ItemizeCallback trait alongside existing TransferProgressCallback.
Thread it through run_server_with_handshake -> GeneratorContext::run ->
run_transfer_loop -> maybe_emit_itemize. In client mode, the callback
writes formatted lines to stdout; in server mode, MSG_INFO is used as
before.

Remove oc:itemize from KNOWN_FAILURES in run_interop.sh.

* fix: strip leading '/' from dir-merge filename to fix -FF filter path

The -FF flag generates 'dir-merge /.rsync-filter' where the leading '/'
means anchor-to-root. However, the '/' was stored as part of the filename
in DirMergeConfig, causing Path::join() to produce an absolute path
'/.rsync-filter' instead of '<directory>/.rsync-filter' when entering
directories during file list walk.

Fix: strip leading '/' from the filename at parse time (CLI) and at wire
decode time (generator), setting anchor_root flag instead. This matches
upstream exclude.c semantics where leading '/' on merge filenames means
the file is only looked for in the transfer root directory.

Remove oc:merge-filter from interop KNOWN_FAILURES.

* ci: remove write-batch-read-batch and large-file-2gb from known interop failures

Both tests should now pass after recent fixes:
- Batch mode: delta token stream decoding (d3b172e), daemon batch
  wiring (0233afb), header/flist parsing (50940d4, a1e8dab)
- Large file: 64-bit varint encoding (06b3b0c), wire format
  corrections (e834916)

KNOWN_FAILURES reduced from 7 to 5 (all environment-dependent:
ACLs, xattrs, protocol-31).

* style: rename non-snake-case test function to fix clippy
oferchen added a commit that referenced this pull request May 5, 2026
* fix: make metadata id_lookup cross-platform for Windows compilation

Remove #[cfg(unix)] gate on id_lookup module, restructure so converter.rs
(trait + thread-local) is available on all platforms. NSS libc calls stay
unix-only, with nss_stub.rs providing no-op lookups on non-unix that still
check NAME_CONVERTER_SLOT. Deletes the old id_lookup_stub.rs.

* feat: add platform crate for unsafe code isolation

New crate absorbing all platform-specific unsafe FFI from the daemon:
- privilege: chroot, setuid/setgid (nix safe wrappers), setgroups (libc on macOS)
- group: getgrnam_r with ERANGE retry, gr_mem pointer traversal
- signal: SignalFlags + register_signal_handlers (signal-hook on Unix, SetConsoleCtrlHandler on Windows)
- daemonize: fork + setsid + stdio redirect (nix safe wrappers where possible)
- secrets: permission checks using nix::unistd::getuid (zero unsafe)
- name_resolution: Windows LookupAccountNameW + GetSidSubAuthority
- env: EnvGuard for Rust 2024 unsafe env::set_var/remove_var

Uses nix 0.29 for safe Unix wrappers, eliminating most #[allow(unsafe_code)].
Only fork, macOS setgroups, and Windows FFI remain unsafe.

* feat: wire daemon crate to platform, remove all #[allow(unsafe_code)]

Delegate all platform FFI from daemon to the new platform crate:
- privilege: apply_chroot/drop_privileges -> platform::privilege
- group_expansion: lookup_group_members -> platform::group
- signals: register_signal_handlers -> platform::signal (kept daemon SignalFlags wrapper with From conversion)
- daemonize: become_daemon -> platform::daemonize
- authentication: check_secrets_file_permissions -> platform::secrets
- test_env: EnvGuard re-exported from platform::env

Daemon crate now has zero #[allow(unsafe_code)] annotations.
Removed signal-hook and Win32_System_Console deps (moved to platform).

* fix: correct Windows API paths for windows crate 0.61

- Move PSID, GetSidSubAuthority, GetSidSubAuthorityCount, LookupAccountNameW
  imports from Foundation/Authorization to Win32::Security (where they live
  in windows 0.61)
- Move LogonUserW, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT from
  Security::Authentication::Identity to Win32::Security
- Add missing Ordering import in Windows signal handler callback
- Use Option<PSID>/Option<PWSTR> for LookupAccountNameW null parameters
- Remove unused Win32_Security_Authentication_Identity and
  Win32_NetworkManagement_NetManagement features

* fix: use windows::core::BOOL and plain u32 ctrl events in signal handler

BOOL moved from Win32::Foundation to windows::core in windows 0.61.
CTRL_C_EVENT/CTRL_CLOSE_EVENT/CTRL_BREAK_EVENT are plain u32 constants,
not newtypes with a .0 field.

* ci: increase interop job timeout from 30 to 45 minutes

The comprehensive interop test matrix (3 versions x ~45 scenarios x 2
directions x 5 protocol versions + standalone tests) exceeds the
default 30-minute timeout. This caused the interop job to be cancelled
on both master and feature branches.

* ci: add oc:hardlinks and oc:hardlinks-relative to known interop failures

These failures exist on master - oc-rsync client hardlink detection in
daemon push mode does not yet produce correct inode/device mapping for
upstream daemon verification. Not a regression from the platform crate
changes.

* fix: skip metadata state update for abbreviated hardlink followers

Upstream flist.c:send_file_entry() only updates lastname at the_end
label for abbreviated followers - the modtime, mode, uid, gid state
variables are NOT updated because the goto skips the metadata writes.

oc-rsync was updating all compression state (including mode, mtime,
uid, gid) for abbreviated followers, causing the sender's state to
diverge from the receiver's. Subsequent entries would have incorrect
XMIT_SAME_MODE/XMIT_SAME_TIME/XMIT_SAME_UID/XMIT_SAME_GID flags,
leading to metadata decode errors and upstream's "ABORTING due to
invalid path from sender" rejection.

Also removes 5 now-passing tests from KNOWN_FAILURES:
- oc:hardlinks, oc:hardlinks-relative (this fix)
- standalone:file-vanished, standalone:info-progress2, standalone:iconv

* fix: parse --log-format=%i in daemon to enable itemize MSG_INFO emission

When upstream rsync pushes with -avi, it sends --log-format=%i as a
server arg. The oc-rsync daemon must parse this to set info_flags.itemize
so the receiver emits MSG_INFO frames with itemize output back to the
client. Also handles --out-format= (canonical name) and --log-format=%i%I
(uppercase I variant for server-side itemize).

upstream: options.c:2750-2761 - server_options() sends --log-format
upstream: options.c:2327-2331 - server parses stdout_format for %i

* feat: wire client-side itemize output for daemon push transfers

When the client pushes to an upstream daemon with -avi, itemize lines
must be written to process stdout (upstream rwrite() FCLIENT path),
not sent via MSG_INFO which is the server-to-client direction.

Add ItemizeCallback trait alongside existing TransferProgressCallback.
Thread it through run_server_with_handshake -> GeneratorContext::run ->
run_transfer_loop -> maybe_emit_itemize. In client mode, the callback
writes formatted lines to stdout; in server mode, MSG_INFO is used as
before.

Remove oc:itemize from KNOWN_FAILURES in run_interop.sh.

* fix: strip leading '/' from dir-merge filename to fix -FF filter path

The -FF flag generates 'dir-merge /.rsync-filter' where the leading '/'
means anchor-to-root. However, the '/' was stored as part of the filename
in DirMergeConfig, causing Path::join() to produce an absolute path
'/.rsync-filter' instead of '<directory>/.rsync-filter' when entering
directories during file list walk.

Fix: strip leading '/' from the filename at parse time (CLI) and at wire
decode time (generator), setting anchor_root flag instead. This matches
upstream exclude.c semantics where leading '/' on merge filenames means
the file is only looked for in the transfer root directory.

Remove oc:merge-filter from interop KNOWN_FAILURES.

* ci: remove write-batch-read-batch and large-file-2gb from known interop failures

Both tests should now pass after recent fixes:
- Batch mode: delta token stream decoding (7b3df7b), daemon batch
  wiring (85f061c), header/flist parsing (6a38b68, 6f29a03)
- Large file: 64-bit varint encoding (540d6f8), wire format
  corrections (7e4b9e6)

KNOWN_FAILURES reduced from 7 to 5 (all environment-dependent:
ACLs, xattrs, protocol-31).

* style: rename non-snake-case test function to fix clippy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant