From 281628957f26b5c1899de4a65a356d1c6892681a Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Wed, 7 Oct 2026 19:57:51 +0800 Subject: [PATCH] fix(collectors): trust protected root-owned sticky ancestors --- crates/gpapi/src/process/collection.rs | 40 ++++++++++++++++++++++++-- nix/tests/native-runtime.nix | 3 -- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/crates/gpapi/src/process/collection.rs b/crates/gpapi/src/process/collection.rs index 9d90b65..68dc390 100644 --- a/crates/gpapi/src/process/collection.rs +++ b/crates/gpapi/src/process/collection.rs @@ -88,7 +88,7 @@ impl<'a> CollectorCommands<'a> { } /// Run an installed helper with the same trust checks as system collectors. - /// Root never executes a helper in a user-writable directory. + /// Root only executes helpers protected from unprivileged modification. pub fn run_executable(&self, path: &Path, args: &[&str]) -> io::Result> { self.control.check()?; let Some(path) = resolve_executable(path, uzers::get_effective_uid() == 0) else { @@ -159,7 +159,12 @@ pub fn command_path(require_root: bool) -> io::Result { fn root_owned_path(path: &Path) -> bool { path.ancestors().all(|part| { fs::symlink_metadata(part).is_ok_and(|metadata| { - metadata.uid() == 0 && metadata.permissions().mode() & 0o022 == 0 && !metadata.file_type().is_symlink() + let mode = metadata.permissions().mode(); + let writable = mode & 0o022 != 0; + // A root-owned sticky directory protects each root-owned descendant + // from replacement, including entries in the Nix store. + let protected_ancestor = part != path && metadata.is_dir() && mode & 0o1000 != 0; + metadata.uid() == 0 && (!writable || protected_ancestor) && !metadata.file_type().is_symlink() }) }) } @@ -168,6 +173,37 @@ fn root_owned_path(path: &Path) -> bool { mod tests { use super::*; + #[test] + fn root_owned_sticky_directories_protect_collectors_without_trusting_writable_files() { + // CI runs Rust tests as root; an unprivileged process cannot create these fixtures. + if uzers::get_effective_uid() != 0 { + return; + } + let directory = tempfile::tempdir().unwrap(); + let tool = directory.path().join("collector"); + fs::write(&tool, "#!/bin/sh\nexit 0\n").unwrap(); + fs::set_permissions(&tool, fs::Permissions::from_mode(0o755)).unwrap(); + fs::set_permissions(directory.path(), fs::Permissions::from_mode(0o1775)).unwrap(); + assert_eq!(resolve_executable(&tool, true), Some(fs::canonicalize(&tool).unwrap())); + // A PATH directory must itself reject unprivileged additions, even with the sticky bit. + assert!(!root_owned_path(directory.path())); + + fs::set_permissions(directory.path(), fs::Permissions::from_mode(0o775)).unwrap(); + assert!(resolve_executable(&tool, true).is_none()); + + fs::set_permissions(directory.path(), fs::Permissions::from_mode(0o1777)).unwrap(); + assert!(resolve_executable(&tool, true).is_some()); + fs::set_permissions(&tool, fs::Permissions::from_mode(0o1775)).unwrap(); + assert!(resolve_executable(&tool, true).is_none()); + + fs::set_permissions(&tool, fs::Permissions::from_mode(0o755)).unwrap(); + std::os::unix::fs::chown(&tool, Some(1), None).unwrap(); + assert!(resolve_executable(&tool, true).is_none()); + std::os::unix::fs::chown(&tool, Some(0), None).unwrap(); + std::os::unix::fs::chown(directory.path(), Some(1), None).unwrap(); + assert!(resolve_executable(&tool, true).is_none()); + } + #[test] fn configured_directories_resolve_tools_without_using_the_process_path() { let dir = tempfile::tempdir().unwrap(); diff --git a/nix/tests/native-runtime.nix b/nix/tests/native-runtime.nix index 04c49c6..c2e11f4 100644 --- a/nix/tests/native-runtime.nix +++ b/nix/tests/native-runtime.nix @@ -77,9 +77,6 @@ pkgs.testers.runNixOSTest { machine.wait_until_succeeds("! kill -0 $(cat /tmp/gp-gui-ready)", timeout=timedelta(seconds=30)) # The optional collector lives in the NixOS system profile, outside the package's tool closure. - print(machine.succeed("stat -c '%a:%u:%n' / /nix /nix/store /run/current-system/sw/bin")) - print(machine.succeed("stat -Lc '%a:%u:%n' /run/current-system/sw/bin/clamscan")) - print(machine.succeed("/run/current-system/sw/bin/clamscan --version")) report = machine.succeed(package + "/bin/gpclient hip --client-version 6.3.3-619 " "--client-os Linux --cookie fixture --md5 0123456789abcdef0123456789abcdef") assert "ClamAV" in report, report