diff --git a/rust/contracts/remote-workspace-protocol-v1.md b/rust/contracts/remote-workspace-protocol-v1.md index 539b1b68..d9dab27e 100644 --- a/rust/contracts/remote-workspace-protocol-v1.md +++ b/rust/contracts/remote-workspace-protocol-v1.md @@ -349,7 +349,7 @@ connection with the same key starts a fresh session. `server.hello` returns `host` with `os` (`linux` / `macos`), `arch` (`x86_64` / `aarch64`), `hostName`, `userName`, `userId`, `homeDirectory`, -`defaultShell`, `pathSeparator`, `process.orphanProtection` +`defaultShell`, `pathSeparator`, `windowsInterop`, `process.orphanProtection` (`parentDeathSignal` / `serverShutdown`), `watch.backend` (`inotify` / `fsevents`), `watch.directoryLimit` and the sorted `features` list. Clients branch on these facts, never on `os` alone, and check @@ -360,6 +360,14 @@ display only. `userId` is the numeric account from the kernel; clients key per-account state, such as workspace trust, by it. Servers that predate it omit the field. +`windowsInterop` is `true` when the host can start Windows programs through +WSL interoperability, that is when a `binfmt_misc` entry WSL registers +(`WSLInterop`, or `WSLInterop-late` on some WSL releases with systemd) is +enabled, and `false` when such an entry exists but none is enabled. The field +is omitted when there is no such entry: outside WSL, on macOS, in a container +that does not see it, and from servers that predate it. WSL is not a security +boundary, so clients show the value and never relax trust for it. + ## Error codes `PROTOCOL_MISMATCH`, `INVALID_SESSION_TOKEN`, `INVALID_REQUEST`, diff --git a/rust/contracts/rust-core-api.md b/rust/contracts/rust-core-api.md index 29e1c9c2..4a6a2582 100644 --- a/rust/contracts/rust-core-api.md +++ b/rust/contracts/rust-core-api.md @@ -6124,9 +6124,12 @@ and `{ stage: "installed", uploaded }` once the server is in place, where failed attempt stops after the last stage reached, so a host without a matching build stops after `platformChecked`. The result is `{ status, session, host, diagnostics }`; `session` is `{ id, uploaded, hostName, userName, -homeDirectory, sessionKey, sessionToken, resumed, keptPrograms }` and is set -only for `connected`. A host without a matching build is -`unsupportedPlatform` and nothing is uploaded. +homeDirectory, windowsInterop, sessionKey, sessionToken, resumed, keptPrograms }` +and is set only for `connected`. A host without a matching build is +`unsupportedPlatform` and nothing is uploaded. `windowsInterop` is the host +capability of the same name from the remote workspace protocol: `true` or +`false` on WSL, `null` elsewhere. It is for display only; a WSL host gets no +more trust than any other. The server runs a persistent session (`--connect `, with `--state-dir ` when one is given), so what it runs survives the diff --git a/rust/crates/remote/src/protocol/methods.rs b/rust/crates/remote/src/protocol/methods.rs index 7b855fb2..2c802b51 100644 --- a/rust/crates/remote/src/protocol/methods.rs +++ b/rust/crates/remote/src/protocol/methods.rs @@ -401,6 +401,17 @@ pub struct HostCapabilities { /// Separator used by host paths. Always `/` for current hosts; carried so /// that clients do not hard-code it. pub path_separator: String, + /// Whether programs on this host can start Windows programs through WSL + /// interoperability. `Some(true)` when a `binfmt_misc` entry that WSL + /// registers is enabled: `WSLInterop`, or `WSLInterop-late`, the name some + /// WSL releases use on distributions that run systemd; either or both can + /// exist. `Some(false)` when such an entry exists but none is enabled. + /// `None` when there is no such entry: the host is not WSL, WSL did not + /// register one, a container does not see it, the host is macOS, or the + /// server predates the field. WSL is not a security boundary either way: + /// clients show this and never relax trust for it (S31). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub windows_interop: Option, /// How spawned processes are kept from outliving the server. pub process: ProcessCapabilities, /// How file changes are watched. @@ -1459,4 +1470,30 @@ mod tests { } ); } + + // Servers before the field, and hosts outside WSL, send no + // `windowsInterop`; a WSL host sends it either way. + #[test] + fn windows_interop_is_omitted_unless_the_host_is_wsl() { + let host = json!({"os": "linux", "arch": "x86_64", "hostName": "test-host", + "userName": "dev", "homeDirectory": "/home/dev", "defaultShell": "/bin/sh", + "pathSeparator": "/", "process": {"orphanProtection": "parentDeathSignal"}, + "watch": {"backend": "inotify", "directoryLimit": 128}, "features": []}); + let older: HostCapabilities = serde_json::from_value(host.clone()).unwrap(); + assert_eq!(older.windows_interop, None); + assert!(serde_json::to_value(&older) + .unwrap() + .get("windowsInterop") + .is_none()); + for interop in [true, false] { + let mut wsl = host.clone(); + wsl["windowsInterop"] = json!(interop); + let parsed: HostCapabilities = serde_json::from_value(wsl).unwrap(); + assert_eq!(parsed.windows_interop, Some(interop)); + assert_eq!( + serde_json::to_value(&parsed).unwrap()["windowsInterop"], + json!(interop) + ); + } + } } diff --git a/rust/crates/remote/src/server/platform/linux.rs b/rust/crates/remote/src/server/platform/linux.rs index 4b25b891..8077c238 100644 --- a/rust/crates/remote/src/server/platform/linux.rs +++ b/rust/crates/remote/src/server/platform/linux.rs @@ -76,6 +76,36 @@ pub(crate) fn case_sensitive(path: &Path) -> Option { unix::probe_case_sensitivity(path) } +/// Where the kernel lists `binfmt_misc` entries. +const BINFMT_MISC: &str = "/proc/sys/fs/binfmt_misc"; + +/// Entries WSL registers to start Windows programs. Some WSL releases name it +/// `WSLInterop-late` on distributions that run systemd; either or both can +/// exist. +const WSL_INTEROP_ENTRIES: [&str; 2] = ["WSLInterop", "WSLInterop-late"]; + +/// See [`crate::protocol::methods::HostCapabilities::windows_interop`]. +pub(crate) fn windows_interop() -> Option { + windows_interop_in(Path::new(BINFMT_MISC)) +} + +/// Reads the WSL entries below `directory`. The first line of a +/// `binfmt_misc` entry is `enabled` or `disabled`; an entry that cannot be +/// read is treated as absent. +fn windows_interop_in(directory: &Path) -> Option { + let mut registered = false; + for entry in WSL_INTEROP_ENTRIES { + let Ok(text) = std::fs::read_to_string(directory.join(entry)) else { + continue; + }; + registered = true; + if text.lines().next().map(str::trim) == Some("enabled") { + return Some(true); + } + } + registered.then_some(false) +} + /// Processes whose session id is `session`, read from `/proc//stat`. pub(crate) fn session_members(session: u32) -> Vec { process_sessions() @@ -333,4 +363,58 @@ mod tests { assert_eq!(own.pid, Some(std::process::id())); assert!(own.process.is_some(), "{own:?}"); } + + /// A fresh directory standing in for `/proc/sys/fs/binfmt_misc`, holding + /// `entries` as `(name, contents)`. + fn binfmt_misc_with(entries: &[(&str, &str)]) -> std::path::PathBuf { + use std::sync::atomic::{AtomicU64, Ordering}; + static SEQUENCE: AtomicU64 = AtomicU64::new(0); + let directory = std::env::temp_dir().join(format!( + "lithe-remote-binfmt-{}-{}", + std::process::id(), + SEQUENCE.fetch_add(1, Ordering::Relaxed) + )); + let _ = std::fs::remove_dir_all(&directory); + std::fs::create_dir_all(&directory).unwrap(); + for (name, contents) in entries { + std::fs::write(directory.join(name), contents).unwrap(); + } + directory + } + + // Contents as WSL leaves them: the state line, then the interpreter. + const ENABLED: &str = "enabled\ninterpreter /init\nflags: PF\noffset 0\nmagic 4d5a\n"; + const DISABLED: &str = "disabled\ninterpreter /init\nflags: PF\noffset 0\nmagic 4d5a\n"; + + #[test] + fn windows_interop_follows_whichever_wsl_entry_is_enabled() { + let check = |entries: &[(&str, &str)], expected: Option| { + let directory = binfmt_misc_with(entries); + assert_eq!(windows_interop_in(&directory), expected, "{entries:?}"); + std::fs::remove_dir_all(&directory).unwrap(); + }; + check(&[("WSLInterop", ENABLED)], Some(true)); + check(&[("WSLInterop", DISABLED)], Some(false)); + check(&[("WSLInterop-late", ENABLED)], Some(true)); + check( + &[("WSLInterop", DISABLED), ("WSLInterop-late", ENABLED)], + Some(true), + ); + check( + &[("WSLInterop", DISABLED), ("WSLInterop-late", DISABLED)], + Some(false), + ); + // Not a state the kernel writes; a registered entry still counts. + check(&[("WSLInterop", "unexpected\n")], Some(false)); + // Other interpreters do not make a host WSL. + check(&[("python3.12", ENABLED), ("status", "enabled\n")], None); + } + + #[test] + fn windows_interop_is_unknown_without_binfmt_misc() { + let directory = binfmt_misc_with(&[]); + assert_eq!(windows_interop_in(&directory), None); + std::fs::remove_dir_all(&directory).unwrap(); + assert_eq!(windows_interop_in(&directory), None); + } } diff --git a/rust/crates/remote/src/server/platform/macos.rs b/rust/crates/remote/src/server/platform/macos.rs index 6551ab69..5c4411e8 100644 --- a/rust/crates/remote/src/server/platform/macos.rs +++ b/rust/crates/remote/src/server/platform/macos.rs @@ -50,6 +50,11 @@ pub(crate) fn rename_no_replace(from: &Path, to: &Path) -> io::Result<()> { } } +/// macOS hosts have no WSL, so no Windows interoperability. +pub(crate) fn windows_interop() -> Option { + None +} + /// APFS and HFS+ volumes are case-insensitive by default but can be /// formatted case-sensitive, so the answer is per volume. pub(crate) fn case_sensitive(path: &Path) -> Option { diff --git a/rust/crates/remote/src/server/platform/mod.rs b/rust/crates/remote/src/server/platform/mod.rs index b919c8d7..7cf3ad88 100644 --- a/rust/crates/remote/src/server/platform/mod.rs +++ b/rust/crates/remote/src/server/platform/mod.rs @@ -71,6 +71,7 @@ pub(crate) fn host_capabilities() -> HostCapabilities { home_directory: account.home_directory, default_shell: account.shell, path_separator: "/".into(), + windows_interop: host::windows_interop(), process: ProcessCapabilities { orphan_protection: host::ORPHAN_PROTECTION, }, diff --git a/rust/crates/workspace/src/sessions.rs b/rust/crates/workspace/src/sessions.rs index 75cbf583..8df4cfb1 100644 --- a/rust/crates/workspace/src/sessions.rs +++ b/rust/crates/workspace/src/sessions.rs @@ -164,6 +164,10 @@ pub struct OpenedSession { host_name: String, user_name: Option, home_directory: Option, + /// Whether the host can start Windows programs through WSL + /// interoperability; `null` outside WSL. Shown to the user only: a WSL + /// host gets no more trust than any other (S31). + windows_interop: Option, /// Names the persistent session on the host, for a later `resume`. session_key: String, /// Secret a later `resume` presents; the client stores it like a @@ -1074,6 +1078,7 @@ fn connect_logged( host_name: capabilities.host_name, user_name: capabilities.user_name, home_directory: capabilities.home_directory, + windows_interop: capabilities.windows_interop, session_key, session_token: info.session_token, resumed: info.resumed,