diff --git a/crates/openshell-policy/src/lib.rs b/crates/openshell-policy/src/lib.rs index a2ddd4c45b..35e9cddb56 100644 --- a/crates/openshell-policy/src/lib.rs +++ b/crates/openshell-policy/src/lib.rs @@ -1374,7 +1374,7 @@ impl fmt::Display for PolicyViolation { /// - UI clipboard access must use a recognized enum value /// - Filesystem paths must be absolute (start with `/`) /// - Filesystem paths must not contain `..` components -/// - Read-write paths must not be overly broad (just `/`) +/// - Read-write paths must not be overly broad (a filesystem root) /// - Individual path lengths must not exceed [`MAX_PATH_LENGTH`] /// - Total path count must not exceed [`MAX_FILESYSTEM_PATHS`] /// - Network endpoint hosts must not use TLD wildcards (e.g. `*.com`) @@ -1431,8 +1431,11 @@ pub fn validate_filesystem_paths( } for path_str in read_write { - // Repeated separators still designate the filesystem root. - if path_str.trim_end_matches('/').is_empty() { + // Oversized paths already have a violation; do not parse them again. + if path_str.len() > MAX_PATH_LENGTH { + continue; + } + if is_filesystem_root(path_str) { violations.push(PolicyViolation::OverlyBroadPath { path: path_str.clone(), }); @@ -1445,6 +1448,153 @@ pub fn validate_filesystem_paths( } } +/// Return whether `path` designates a filesystem root, such as `/`, `C:\`, `\`, +/// `\\?\C:\`, `\\?\GLOBALROOT\Device\HarddiskVolume4\` or `\\server\share`. +/// The check is lexical rather than host-OS based +/// because the sandbox platform, not the gateway, decides what a path means. +/// Windows spellings are matched in any mix of separators, because the MXC +/// driver converts `/` to `\` before it uses a path. +fn is_filesystem_root(path: &str) -> bool { + // Win32 drops trailing dots and spaces from a component, so `C:\...` and + // `/ ` name a root. Verbatim (`\\?\`) and NT (`\??\`) paths keep every + // component as written. + fn only_root_components(rest: &str, literal: bool) -> bool { + rest.split('/').all(|c| { + let c = if literal { + c + } else { + c.trim_end_matches(['.', ' ']) + }; + // NTFS directory streams name the directory itself, including in + // verbatim paths: `C:\::$INDEX_ALLOCATION` still names `C:\`. + let c = c + .split_once(':') + .filter(|(_, stream)| { + stream.eq_ignore_ascii_case(":$INDEX_ALLOCATION") + || stream.eq_ignore_ascii_case("$I30:$INDEX_ALLOCATION") + }) + .map_or(c, |(directory, _)| directory); + let c = if literal { + c + } else { + c.trim_end_matches(['.', ' ']) + }; + c.is_empty() || c == "." + }) + } + // Skip up to `count` leading names (volume, server or share). + fn skip_names(mut rest: &str, count: usize) -> &str { + for _ in 0..count { + rest = rest.trim_start_matches('/'); + rest = rest.split_once('/').map_or("", |(_, tail)| tail); + } + rest + } + + // `rest` follows a `\\?\`, `\\.\` or `\??\` prefix, which all name the + // `\??` object directory: drive letters, `UNC`, `Global` (a link to + // `\GLOBAL??`) and `GLOBALROOT` (a link to the object namespace root). + fn is_namespace_root(mut rest: &str, literal: bool) -> bool { + // Namespace links can repeat. Consume them iteratively so stack usage + // does not depend on the number of components in an authored path. + loop { + let (name, tail) = rest.split_once('/').unwrap_or((rest, "")); + if name.eq_ignore_ascii_case("Global") { + rest = tail; + continue; + } + if !name.eq_ignore_ascii_case("GLOBALROOT") { + return if name.eq_ignore_ascii_case("UNC") { + only_root_components(skip_names(tail, 2), literal) + } else { + only_root_components(tail, literal) + }; + } + + // GLOBALROOT enters the object namespace. Drive-letter + // directories lead back to this loop; Device names a volume. + let object_path = tail.trim_start_matches('/'); + let (dir, tail) = object_path.split_once('/').unwrap_or((object_path, "")); + if ["??", "GLOBAL??", "DosDevices"] + .iter() + .any(|name| dir.eq_ignore_ascii_case(name)) + { + rest = tail; + continue; + } + // This well-known link names the Windows directory, not its + // containing volume. Preserve it and its subdirectories. + if dir.eq_ignore_ascii_case("SystemRoot") { + return false; + } + // Other object paths can be links to whole volumes. + return !dir.eq_ignore_ascii_case("Device") || is_device_root(tail, literal); + } + } + // `rest` follows `\Device\`, such as `HarddiskVolume4\`. + fn is_device_root(rest: &str, literal: bool) -> bool { + let (device, mut tail) = rest.split_once('/').unwrap_or((rest, "")); + // A disk is `Harddisk0\Partition2`; a network redirector is followed + // by optional `;`-prefixed connection names, a server and a share. + let names = if device + .get(..8) + .is_some_and(|name| name.eq_ignore_ascii_case("Harddisk")) + && device.len() > 8 + && device[8..].bytes().all(|b| b.is_ascii_digit()) + { + 1 + } else if ["Mup", "LanmanRedirector", "WebDavRedirector"] + .iter() + .any(|name| device.eq_ignore_ascii_case(name)) + { + while let Some(after) = tail.strip_prefix(';') { + tail = after.split_once('/').map_or("", |(_, after)| after); + } + 2 + } else { + 0 + }; + only_root_components(skip_names(tail, names), literal) + } + + let normalized = path.replace('\\', "/"); + let namespace = [("//?/", true), ("/??/", true), ("//./", false)] + .into_iter() + .find_map(|(prefix, literal)| normalized.strip_prefix(prefix).map(|tail| (tail, literal))); + if let Some((tail, literal)) = namespace { + if is_namespace_root(tail, literal) { + return true; + } + if !literal { + // Win32 also normalizes the namespace names themselves: + // `\\.\Global.\.\C:\` and `\\.\Global\\C:\` name `C:\`. + // Check this spelling in addition to the original so unknown + // object directories keep their conservative rejection. + let components = tail + .split('/') + .map(|name| name.trim_end_matches(['.', ' '])) + .filter(|name| !name.is_empty()) + .collect::>() + .join("/"); + return is_namespace_root(&components, false); + } + return false; + } + // Two leading separators start a UNC path when either is a backslash; a + // leading `//` keeps its POSIX meaning. + if normalized.starts_with("//") && path.as_bytes()[..2].contains(&b'\\') { + return only_root_components(skip_names(&normalized[2..], 2), false); + } + match normalized.as_bytes() { + [drive, b':', b'/', ..] if drive.is_ascii_alphabetic() => { + only_root_components(&normalized[2..], false) + } + // On Windows a leading `/` or `\` is the root of the current drive. + [b'/', ..] => only_root_components(&normalized, false), + _ => false, + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum McpVersionPresence { RequireMaterialized, @@ -3645,6 +3795,240 @@ network_policies: ); } + #[test] + fn validate_rejects_every_root_spelling_as_read_write_path() { + for root in [ + "/", + "//", + "/.", + "C:/", + r"C:\", + "c:/", + "D:/", + "C:/.", + "C://", + r"\", + r"\\?\C:\", + r"\\?\C:", + r"\\.\C:\", + r"\\localhost\C$", + r"\\server\share\", + r"\\?\UNC\server\share", + ] { + let violations = validate_filesystem_paths(&[], &[root.to_string()]) + .expect_err("root read_write path must be rejected"); + assert!( + violations + .iter() + .any(|v| matches!(v, PolicyViolation::OverlyBroadPath { .. })), + "{root} should be overly broad, got {violations:?}" + ); + } + } + + #[test] + fn filesystem_root_detection_keeps_narrower_paths() { + for path in [ + "/tmp", + "/work/demo", + "C:", + "C:/work/demo", + r"C:\work\demo", + r"\\?\C:\work", + r"\\localhost\C$\work", + r"\\?\UNC\server\share\work", + r"\\?\C:\...", + r"\??\C:\ ", + "//tmp/x", + "/tmp/...", + ] { + assert!(!is_filesystem_root(path), "{path} is not a filesystem root"); + } + } + + #[test] + fn validate_accepts_systemroot_directory_aliases() { + for prefix in ["//?/", "//./", "/??/", "//?/Global/"] { + for directory in ["SystemRoot", "systemroot"] { + for suffix in ["", "/", "/Temp"] { + let path = format!("{prefix}GLOBALROOT/{directory}{suffix}"); + assert!( + validate_filesystem_paths(&[], std::slice::from_ref(&path)).is_ok(), + "{path} names the Windows directory or a subdirectory" + ); + } + } + } + } + + #[test] + fn validate_rejects_windows_root_aliases_as_read_write_path() { + for root in [ + "//?/C:/", + r"/\?\C:\", + r"\??\C:\", + "//./C:/", + "C:/...", + "C:/ ", + "C:/. ", + r"\...", + "/...", + "/ ", + r"\\.\C:\...", + r"\\server\share\. ", + r"/\server\share", + ] { + let violations = validate_filesystem_paths(&[], &[root.to_string()]) + .expect_err("root alias read_write path must be rejected"); + assert!( + violations + .iter() + .any(|v| matches!(v, PolicyViolation::OverlyBroadPath { .. })), + "{root:?} should be overly broad, got {violations:?}" + ); + } + } + + #[test] + fn validate_rejects_normalized_namespace_roots() { + for root in [ + r"\\.\Global\.\C:\", + r"\\.\.\C:\", + r"\\.\Global.\C:\", + r"\\.\Global\\C:\", + r"\\.\GLOBALROOT\??\.\C:\", + r"\\.\GLOBALROOT\??\\C:\", + "//./global././c:/", + ] { + let violations = validate_filesystem_paths(&[], &[root.to_string()]) + .expect_err("normalized namespace root must be rejected"); + assert!( + violations + .iter() + .any(|v| matches!(v, PolicyViolation::OverlyBroadPath { .. })), + "{root:?} should be overly broad, got {violations:?}" + ); + assert!(!is_filesystem_root(&format!("{root}work"))); + } + + // Verbatim namespace components retain their literal meaning. + for path in [r"\\?\Global.\C:\", r"\\?\Global\.\C:\", r"\??\Global.\C:\"] { + assert!(!is_filesystem_root(path), "{path:?} is literal"); + } + // Normalization must not weaken the conservative unknown-alias rule. + for path in [ + r"\\.\GLOBALROOT\UnknownAlias\work", + r"\\.\GLOBALROOT\SystemRoot.\work", + ] { + assert!(is_filesystem_root(path), "{path:?} remains unsupported"); + } + } + + #[test] + fn validate_rejects_directory_stream_roots() { + for root in [ + "C:/", + r"C:\", + r"\\?\C:\", + r"\\.\C:\", + r"\??\C:\", + r"\\?\GLOBALROOT\Device\HarddiskVolume4\", + r"\\.\Global\.\C:\", + r"\\?\UNC\server\share\", + r"\\server\share\", + ] { + for stream in [ + "::$INDEX_ALLOCATION", + ":$I30:$INDEX_ALLOCATION", + "::$index_allocation", + ] { + let path = format!("{root}{stream}"); + let violations = validate_filesystem_paths(&[], std::slice::from_ref(&path)) + .expect_err("directory stream of a root must be rejected"); + assert!( + violations + .iter() + .any(|v| matches!(v, PolicyViolation::OverlyBroadPath { .. })), + "{path:?} should be overly broad, got {violations:?}" + ); + let narrower = format!("{root}work{stream}"); + assert!(!is_filesystem_root(&narrower), "{narrower:?} is narrower"); + } + } + } + + #[test] + fn validate_rejects_object_namespace_roots_as_read_write_path() { + for root in [ + r"\\?\GLOBALROOT\Device\HarddiskVolume4\", + r"\\.\GLOBALROOT\Device\HarddiskVolume4\", + r"\??\GLOBALROOT\Device\HarddiskVolume4\", + "//?/globalroot/device/harddiskvolume4", + r"\\.\GLOBALROOT\Device\HarddiskVolume4\. ", + r"\\?\GLOBALROOT\Device\Harddisk0\Partition2\", + r"\\?\GLOBALROOT\??\C:\", + r"\\?\GLOBALROOT\GLOBAL??\C:\", + r"\\?\GLOBALROOT\DosDevices\C:\", + r"\\?\Global\C:\", + r"\\?\GLOBALROOT\??\Global\C:\", + r"\\?\GLOBALROOT\Device\Mup\server\share\", + r"\\?\GLOBALROOT\Device\LanmanRedirector\;Z:0000000000012345\server\share", + r"\\?\GLOBALROOT\ArcName\multi(0)disk(0)rdisk(0)partition(3)\", + r"\\?\GLOBALROOT", + ] { + let violations = validate_filesystem_paths(&[], &[root.to_string()]) + .expect_err("object namespace root read_write path must be rejected"); + assert!( + violations + .iter() + .any(|v| matches!(v, PolicyViolation::OverlyBroadPath { .. })), + "{root:?} should be overly broad, got {violations:?}" + ); + } + for path in [ + r"\\?\GLOBALROOT\Device\HarddiskVolume4\work", + r"\\.\GLOBALROOT\Device\HarddiskVolume4\work", + r"\??\GLOBALROOT\Device\HarddiskVolume4\work", + r"\\?\GLOBALROOT\Device\HarddiskVolume4\...", + r"\\?\GLOBALROOT\Device\Harddisk0\Partition2\work", + r"\\?\GLOBALROOT\??\C:\work", + r"\\?\Global\C:\work", + r"\\?\GLOBALROOT\Device\Mup\server\share\work", + ] { + assert!( + !is_filesystem_root(path), + "{path:?} is narrower than a volume" + ); + } + } + + #[test] + fn validate_accepts_read_only_filesystem_root() { + assert!(validate_filesystem_paths(&["/".to_string()], &[]).is_ok()); + } + + #[test] + fn validate_rejects_oversized_namespace_paths_without_parsing() { + for segment in ["Global/", "GLOBALROOT/??/"] { + let path = format!("//?/{}C:/", segment.repeat(20_000)); + let length = path.len(); + let violations = validate_filesystem_paths(&[], &[path]).unwrap_err(); + assert!( + matches!(violations.as_slice(), [PolicyViolation::FieldTooLong { length: actual, .. }] if *actual == length), + "oversized namespace path must return only its length violation" + ); + } + } + + #[test] + fn filesystem_root_detection_handles_deep_namespace_links() { + for segment in ["Global/", "GLOBALROOT/??/", "GLOBALROOT/GLOBAL??/Global/"] { + let root = format!("//?/{}C:/", segment.repeat(20_000)); + assert!(is_filesystem_root(&root)); + assert!(!is_filesystem_root(&format!("{root}work"))); + } + } + #[test] fn validate_accepts_valid_policy() { let policy = restrictive_default_policy(); diff --git a/crates/openshell-server/src/grpc/validation.rs b/crates/openshell-server/src/grpc/validation.rs index 4d47b95105..dc149bb6a1 100644 --- a/crates/openshell-server/src/grpc/validation.rs +++ b/crates/openshell-server/src/grpc/validation.rs @@ -2165,6 +2165,26 @@ mod tests { assert!(err.message().contains("broad")); } + #[test] + fn validate_policy_safety_rejects_windows_roots() { + use openshell_core::proto::FilesystemPolicy; + + for root in ["C:/", "//./Global/./C:/", "C:/::$INDEX_ALLOCATION"] { + let policy = ProtoSandboxPolicy { + version: 1, + filesystem: Some(FilesystemPolicy { + include_workdir: false, + read_only: vec![], + read_write: vec![root.into()], + }), + ..Default::default() + }; + let err = validate_policy_safety(&policy).unwrap_err(); + assert_eq!(err.code(), Code::InvalidArgument); + assert!(err.message().contains("broad"), "{root}: {err}"); + } + } + #[test] fn validate_policy_safety_accepts_valid_policy() { let policy = openshell_policy::restrictive_default_policy(); diff --git a/docs/security/best-practices.mdx b/docs/security/best-practices.mdx index 53d2f469f7..fd46722a00 100644 --- a/docs/security/best-practices.mdx +++ b/docs/security/best-practices.mdx @@ -194,10 +194,23 @@ OpenShell validates policies before they take effect. |---|---| | Paths must be absolute (start with `/`). | Rejected with `INVALID_ARGUMENT`. | | Paths must not contain `..` traversal. | Rejected with `INVALID_ARGUMENT`. | -| Read-write paths must not be overly broad (for example, `/` alone). | Rejected with `INVALID_ARGUMENT`. | -| Each path must not exceed 4096 characters. | Rejected with `INVALID_ARGUMENT`. | +| Read-write paths must not name a filesystem root, including Windows drive, share, and device-namespace roots. | Rejected with `INVALID_ARGUMENT`. | +| Each path must not exceed 4096 bytes. | Rejected with `INVALID_ARGUMENT`. | | Combined `read_only` + `read_write` paths must not exceed 256. | Rejected with `INVALID_ARGUMENT`. | +Windows root checks include normalized device-namespace paths such as +`\\.\Global\.\C:\` and NTFS directory streams such as `C:\::$INDEX_ALLOCATION`. +These spellings still name the drive root and cannot be used as read-write grants. + +The well-known Windows `GLOBALROOT/SystemRoot` alias names the Windows directory, +so it and its subdirectories remain valid read-write paths. Other object-namespace +aliases outside the recognized drive, volume, partition, and share forms are +rejected conservatively: an alias can point to an entire volume, and the validator +cannot resolve it on every gateway platform. If an existing policy uses one, +replace it with the intended directory's absolute drive or share path, such as +`C:/work/project`, before creating a sandbox or applying the policy. Keep the grant +scoped to the specific directory the workload needs. + ## Process Controls The sandbox supervisor drops privileges, applies seccomp filters, and enforces process-level restrictions during startup. diff --git a/nix/pkgs/vm-runtime.nix b/nix/pkgs/vm-runtime.nix index eb3819c09f..bf3d2a50a7 100644 --- a/nix/pkgs/vm-runtime.nix +++ b/nix/pkgs/vm-runtime.nix @@ -13,7 +13,7 @@ let { x86_64-linux = { platform = "linux-x86_64"; - hash = "sha256-kA6ZQz53geBp2zHx8tv+D8VcGkSrnC74Op/60H4fmck="; + hash = "sha256-mZwSUhs9IZ0pPLxzVs4AbRLdYWkUlHwP4lmfsbeiUGw="; artifacts = [ "libkrun.so" "libkrunfw.so.5" @@ -22,7 +22,7 @@ let }; aarch64-linux = { platform = "linux-aarch64"; - hash = "sha256-7FNqUtC6ixfw202EyoYCgZOLdyxJYfg/YzdlkTT/+sM="; + hash = "sha256-Yk5Rxn6JWxMu82Vkyx3eSAxh5/mnfUwcNDNeDIQJz4c="; artifacts = [ "libkrun.so" "libkrunfw.so.5" @@ -31,7 +31,7 @@ let }; aarch64-darwin = { platform = "darwin-aarch64"; - hash = "sha256-cPfogd7QiPFk76kR1JJeKLaWwFksAZ9BeW/LsmDgsjo="; + hash = "sha256-Sm0zwSkctPETKd46zqOqDO02jLapQ3ST1hafqIVkVjU="; artifacts = [ "libkrun.dylib" "libkrunfw.5.dylib" diff --git a/tests/ansible/playbooks/k3s.yaml b/tests/ansible/playbooks/k3s.yaml index e1a2aa3397..957dba612a 100644 --- a/tests/ansible/playbooks/k3s.yaml +++ b/tests/ansible/playbooks/k3s.yaml @@ -21,10 +21,12 @@ state: present update_cache: true + # get.k3s.io serves the latest script, so pin the one from the k3s_version + # release tag. Update the checksum whenever k3s_version changes. - name: Download K3s installer ansible.builtin.get_url: - url: https://get.k3s.io - checksum: sha256:e5cc3b3d9dfc1662c2d9be6da5abc9a4cd317d6abc3a5ffc02e3dd3248207fee + url: "https://raw.githubusercontent.com/k3s-io/k3s/v{{ k3s_version | urlencode }}/install.sh" + checksum: sha256:46177d4c99440b4c0311b67233823a8e8a2fc09693f6c89af1a7161e152fbfad dest: /tmp/install-k3s.sh mode: "0755"