diff --git a/Cargo.toml b/Cargo.toml index 440fdd9..8e2c066 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,8 @@ windows-sys = { version = "0.61", features = [ "Win32_System_Diagnostics_ToolHelp", "Win32_System_IO", "Win32_System_JobObjects", + # The pipes camless drives the LCD's browser through. + "Win32_System_Pipes", "Win32_System_Registry", "Win32_System_SystemInformation", "Win32_System_Threading", diff --git a/docs/how-it-works.md b/docs/how-it-works.md index beceaa9..9a9bd47 100644 --- a/docs/how-it-works.md +++ b/docs/how-it-works.md @@ -135,7 +135,13 @@ Nexus has an MCP server for its cooling too, and this one was inspired by it. - `window.nzxt.v1` has `width`, `height`, `shape` and `targetFps`, and `window.nzxt.v1.onMonitoringDataUpdate` gets CAM's monitoring object every second; - what the page draws goes to the screen at up to `fps` frames per second, and a page that stops changing is sent again every second. -The browser runs as the signed-in user even when camless runs as administrator, with its own profile in `%LOCALAPPDATA%\camless\browser` (on Linux `~/.local/share/camless/browser`), and it ends with camless. Its window sits off every screen: headless Chromium still makes one on Windows, and some versions show it as a blank white square on the desktop. On Linux camless drives it through two pipes (`--remote-debugging-pipe`) rather than a debugging port on 127.0.0.1, which other users of the computer could reach too. If the page does not load, the screen shows liquid temperature and camless tries again every 10 s. `camless lcd page URL` renders a page once, without a device, to check it. +The browser runs as the signed-in user even when camless runs as administrator, with its own profile in `%LOCALAPPDATA%\camless\browser` (on Linux `~/.local/share/camless/browser`), and it ends with camless. Its window sits off every screen: headless Chromium still makes one on Windows, and some versions show it as a blank white square on the desktop. camless drives it through two pipes (`--remote-debugging-pipe`) rather than a debugging port on 127.0.0.1, which every user and program on the computer could reach too. On Windows the browser takes the pipes as two inherited handles (`--remote-debugging-io-pipes`), which needs Edge or Chrome 118 or newer. If the page does not load, the screen shows liquid temperature and camless tries again every 10 s. `camless lcd page URL` renders a page once, without a device, to check it. + +A camless that runs as administrator starts the browser as a child of Explorer, so it gets the signed-in user's rights and can still inherit the pipes. Windows then writes who may open the new process, its first thread and its token from camless's administrator token, and camless puts all three back to what Explorer's own children get. What did not work: + +- `CreateProcessWithTokenW` with Explorer's token, which camless used with the debugging port: it passes no handles on, so the browser cannot get its pipes. +- Explorer as the parent with nothing else changed: Edge 157 could not copy its own token for its sandbox, logged "GPU process launch failed: error_code=49" (`SBOX_ERROR_CANNOT_CREATE_RESTRICTED_TOKEN`) and stopped at a breakpoint dialog on the desktop. +- The token put right but not the process: the page rendered, but the process kept an administrator program's integrity level, and Edge could not open itself to start its crash handler. A browser is the heavy part of camless: KrakenDial with a video background takes Edge about 170 MB of memory and 40 % of one CPU core. Edge runs without a GPU, as CAM's Web Integration does, which saves it about 150 MB. The other LCD modes need no browser. diff --git a/src/cdp.rs b/src/cdp.rs index 53e52d6..f2557ff 100644 --- a/src/cdp.rs +++ b/src/cdp.rs @@ -1,87 +1,40 @@ -//! Just enough of the Chrome DevTools Protocol to drive one headless page: a -//! WebSocket client for the browser's loopback debugging port, or on Linux the -//! browser's own pipes, commands with replies, and events in between. +//! Just enough of the Chrome DevTools Protocol to drive one headless page over the +//! browser's own pipes (`--remote-debugging-pipe`): commands with replies, and +//! events in between. Each message is JSON that ends in a NUL byte. +//! +//! The pipes, and not a debugging port on 127.0.0.1: any user of the computer, and +//! any program on it, can connect to such a port and then drive a browser that runs +//! as the signed-in user. Nobody else can reach a pipe. use anyhow::{anyhow, bail, Context, Result}; use serde_json::{json, Value}; use std::collections::VecDeque; +use std::fs::File; use std::io::{ErrorKind, Read, Write}; -use std::net::TcpStream; use std::time::{Duration, Instant}; /// Screencast frames are base64 PNGs of a few hundred kB; anything far beyond that /// is not a frame of a 640×640 screen. const MAX_MESSAGE: usize = 32 << 20; const CALL_TIMEOUT: Duration = Duration::from_secs(15); +/// How much of the browser's output is read at once. +const CHUNK: usize = 256 << 10; pub struct Cdp { - link: Link, - /// Bytes read but not yet parsed into frames. + /// The pipe the browser reads commands from. + to: File, + from: Replies, + /// Bytes read but not yet split into messages. buf: Vec, - chunk: Vec, - /// A text message split over continuation frames. - partial: Vec, events: VecDeque, next_id: u64, - mask_seed: u32, -} - -/// How the messages travel. -enum Link { - /// WebSocket frames on the debugging port. - #[cfg_attr(not(windows), allow(dead_code))] - Socket(TcpStream), - /// `--remote-debugging-pipe`: JSON messages, each ending in a NUL byte, written to - /// the browser's fd 3 and read from its fd 4. No other user can reach a pipe, as - /// they can a port on 127.0.0.1. - #[cfg(unix)] - Pipe { to: std::fs::File, from: std::fs::File }, } impl Cdp { - fn new(link: Link, buf: Vec, mask_seed: u32) -> Cdp { - Cdp { link, buf, chunk: vec![0; 256 << 10], partial: Vec::new(), events: VecDeque::new(), next_id: 1, mask_seed } - } - - /// Talks to a browser started with `--remote-debugging-pipe` over its two pipes. - #[cfg(unix)] - pub fn pipe(to: std::fs::File, from: std::fs::File) -> Cdp { - Cdp::new(Link::Pipe { to, from }, Vec::new(), 1) - } - - /// Connects to `ws://127.0.0.1:{port}{path}`, as written in `DevToolsActivePort`. - #[cfg_attr(not(windows), allow(dead_code))] - pub fn connect(port: u16, path: &str) -> Result { - let mut stream = TcpStream::connect(("127.0.0.1", port)).with_context(|| format!("connecting to port {port}"))?; - stream.set_nodelay(true)?; - let seed = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().subsec_nanos(); - let key = base64_encode(&seed.to_le_bytes().repeat(4)); - let request = format!( - "GET {path} HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\ - Sec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n\r\n" - ); - stream.write_all(request.as_bytes())?; - stream.set_read_timeout(Some(Duration::from_secs(10)))?; - let mut buf = Vec::new(); - let end = loop { - let mut chunk = [0u8; 4096]; - let n = stream.read(&mut chunk).context("reading the WebSocket handshake")?; - if n == 0 { - bail!("the browser closed the connection during the handshake"); - } - buf.extend_from_slice(&chunk[..n]); - if let Some(i) = buf.windows(4).position(|w| w == b"\r\n\r\n") { - break i + 4; - } - if buf.len() > 16 << 10 { - bail!("no end to the handshake reply"); - } - }; - let head = String::from_utf8_lossy(&buf[..end]); - if !head.starts_with("HTTP/1.1 101") { - bail!("the browser refused the WebSocket: {}", head.lines().next().unwrap_or_default()); - } - Ok(Cdp::new(Link::Socket(stream), buf[end..].to_vec(), seed | 1)) + /// Talks to a browser started with `--remote-debugging-pipe` over its two pipes: + /// `to` is the one it reads, `from` the one it writes. + pub fn pipe(to: File, from: File) -> Result { + Ok(Cdp { to, from: Replies::new(from, CHUNK)?, buf: Vec::new(), events: VecDeque::new(), next_id: 1 }) } /// Sends a command and returns its id without waiting for the reply. @@ -92,7 +45,7 @@ impl Cdp { if let Some(s) = session { msg["sessionId"] = json!(s); } - self.write_message(msg.to_string().as_bytes())?; + self.to.write_all(&[msg.to_string().as_bytes(), b"\0"].concat()).context("writing to the browser")?; Ok(id) } @@ -113,7 +66,7 @@ impl Cdp { let deadline = Instant::now() + timeout; loop { let left = deadline.saturating_duration_since(Instant::now()); - match self.read_message(left)? { + match read_message(&mut self.from, &mut self.buf, left)? { Some(msg) if msg["id"].as_u64() == Some(id) => return Ok(Some(msg)), Some(msg) if msg.get("method").is_some() => self.events.push_back(msg), Some(_) => {} @@ -131,7 +84,7 @@ impl Cdp { let deadline = Instant::now() + timeout; loop { let left = deadline.saturating_duration_since(Instant::now()); - match self.read_message(left)? { + match read_message(&mut self.from, &mut self.buf, left)? { Some(msg) if msg.get("method").is_some() => return Ok(Some(msg)), Some(_) => {} None if left.is_zero() => return Ok(None), @@ -139,86 +92,10 @@ impl Cdp { } } } - - /// One whole message, or None if none arrived within `timeout`. - fn read_message(&mut self, timeout: Duration) -> Result> { - #[cfg(unix)] - if let Link::Pipe { from, .. } = &mut self.link { - return read_piped(from, &mut self.buf, &mut self.chunk, timeout); - } - self.read_frames(timeout) - } - - fn read_frames(&mut self, timeout: Duration) -> Result> { - let deadline = Instant::now() + timeout; - loop { - while let Some((opcode, fin, payload, used)) = parse_frame(&self.buf)? { - self.buf.drain(..used); - match opcode { - 0x0 | 0x1 | 0x2 => { - self.partial.extend_from_slice(&payload); - if self.partial.len() > MAX_MESSAGE { - bail!("a DevTools message over {} MB", MAX_MESSAGE >> 20); - } - if fin { - let text = std::mem::take(&mut self.partial); - return Ok(Some(serde_json::from_slice(&text).context("a DevTools message that is not JSON")?)); - } - } - 0x8 => bail!("the browser closed the DevTools connection"), - 0x9 => self.write_frame(0xA, &payload)?, - _ => {} - } - } - let left = deadline.saturating_duration_since(Instant::now()); - let stream = match &mut self.link { - Link::Socket(s) => s, - #[cfg(unix)] - Link::Pipe { .. } => unreachable!("frames come on the socket only"), - }; - // A zero timeout means "block" to the socket, so wait at least a moment. - stream.set_read_timeout(Some(left.max(Duration::from_millis(1))))?; - match stream.read(&mut self.chunk) { - Ok(0) => bail!("the browser closed the DevTools connection"), - Ok(n) => self.buf.extend_from_slice(&self.chunk[..n]), - Err(e) if matches!(e.kind(), ErrorKind::WouldBlock | ErrorKind::TimedOut) => { - if Instant::now() >= deadline { - return Ok(None); - } - } - Err(e) => return Err(e).context("reading from the browser"), - } - } - } - - fn write_message(&mut self, text: &[u8]) -> Result<()> { - #[cfg(unix)] - if let Link::Pipe { to, .. } = &mut self.link { - return to.write_all(&[text, b"\0"].concat()).context("writing to the browser"); - } - self.write_frame(0x1, text) - } - - fn write_frame(&mut self, opcode: u8, payload: &[u8]) -> Result<()> { - // Clients must mask what they send. The mask only matters to proxies, and - // there are none on loopback, so a cheap generator will do. - self.mask_seed ^= self.mask_seed << 13; - self.mask_seed ^= self.mask_seed >> 17; - self.mask_seed ^= self.mask_seed << 5; - let frame = encode_frame(opcode, payload, self.mask_seed.to_le_bytes()); - let stream = match &mut self.link { - Link::Socket(s) => s, - #[cfg(unix)] - Link::Pipe { .. } => unreachable!("frames go on the socket only"), - }; - stream.write_all(&frame).context("writing to the browser") - } } -/// One message from the browser's pipe, or None if none arrived within `timeout`. -#[cfg(unix)] -fn read_piped(from: &mut std::fs::File, buf: &mut Vec, chunk: &mut [u8], timeout: Duration) -> Result> { - use std::os::fd::AsRawFd; +/// One whole message from the browser, or None if none arrived within `timeout`. +fn read_message(from: &mut Replies, buf: &mut Vec, timeout: Duration) -> Result> { let deadline = Instant::now() + timeout; // A frame arrives over many reads: each looks for the end in what it added. let mut searched = 0; @@ -232,87 +109,104 @@ fn read_piped(from: &mut std::fs::File, buf: &mut Vec, chunk: &mut [u8], tim if buf.len() > MAX_MESSAGE { bail!("a DevTools message over {} MB", MAX_MESSAGE >> 20); } - let left = deadline.saturating_duration_since(Instant::now()); - let mut fd = libc::pollfd { fd: from.as_raw_fd(), events: libc::POLLIN, revents: 0 }; - let ms = left.as_millis().min(i32::MAX as u128) as i32; + if !from.more(buf, deadline.saturating_duration_since(Instant::now()))? { + return Ok(None); + } + } +} + +const CLOSED: &str = "the browser closed the DevTools connection"; + +/// The pipe the browser writes to, read with a time limit. +#[cfg(unix)] +struct Replies { + from: File, + chunk: Vec, +} + +#[cfg(unix)] +impl Replies { + fn new(from: File, chunk: usize) -> Result { + Ok(Replies { from, chunk: vec![0; chunk] }) + } + + /// Waits up to `timeout` for the browser to write, and adds what it wrote to + /// `buf`. False if the time passed first. + fn more(&mut self, buf: &mut Vec, timeout: Duration) -> Result { + use std::os::fd::AsRawFd; + let mut fd = libc::pollfd { fd: self.from.as_raw_fd(), events: libc::POLLIN, revents: 0 }; + let ms = timeout.as_millis().min(i32::MAX as u128) as i32; // SAFETY: one pollfd that lives through the call. let ready = unsafe { libc::poll(&mut fd, 1, ms) }; if ready < 0 { let e = std::io::Error::last_os_error(); if e.kind() == ErrorKind::Interrupted { - continue; + return Ok(true); } return Err(e).context("waiting for the browser"); } if ready == 0 { - return Ok(None); + return Ok(false); } - match from.read(chunk) { - Ok(0) => bail!("the browser closed the DevTools connection"), - Ok(n) => buf.extend_from_slice(&chunk[..n]), + match self.from.read(&mut self.chunk) { + Ok(0) => bail!(CLOSED), + Ok(n) => buf.extend_from_slice(&self.chunk[..n]), Err(e) if e.kind() == ErrorKind::Interrupted => {} Err(e) => return Err(e).context("reading from the browser"), } + Ok(true) } } -/// A client frame: final, masked. -fn encode_frame(opcode: u8, payload: &[u8], mask: [u8; 4]) -> Vec { - let mut out = Vec::with_capacity(payload.len() + 14); - out.push(0x80 | opcode); - match payload.len() { - n if n < 126 => out.push(0x80 | n as u8), - n if n <= 0xffff => { - out.push(0x80 | 126); - out.extend_from_slice(&(n as u16).to_be_bytes()); - } - n => { - out.push(0x80 | 127); - out.extend_from_slice(&(n as u64).to_be_bytes()); - } - } - out.extend_from_slice(&mask); - out.extend(payload.iter().enumerate().map(|(i, b)| b ^ mask[i % 4])); - out -} +/// The pipe the browser writes to, read with a time limit. A read of an anonymous +/// pipe on Windows cannot be given one, so a thread reads and hands on what it got. +/// It ends with the browser, which closes the pipe. +#[cfg(windows)] +struct Replies(std::sync::mpsc::Receiver>>); -/// (opcode, final, payload, bytes used) of the first whole frame in `buf`. -fn parse_frame(buf: &[u8]) -> Result, usize)>> { - if buf.len() < 2 { - return Ok(None); - } - let (fin, opcode, masked) = (buf[0] & 0x80 != 0, buf[0] & 0x0f, buf[1] & 0x80 != 0); - let (len, mut at) = match buf[1] & 0x7f { - 126 if buf.len() >= 4 => (u16::from_be_bytes([buf[2], buf[3]]) as u64, 4), - 127 if buf.len() >= 10 => (u64::from_be_bytes(buf[2..10].try_into().unwrap()), 10), - 126 | 127 => return Ok(None), - n => (n as u64, 2), - }; - if len > MAX_MESSAGE as u64 { - bail!("a WebSocket frame of {len} bytes"); +#[cfg(windows)] +impl Replies { + fn new(mut from: File, chunk: usize) -> Result { + // A few chunks ahead at most: the browser waits with the rest, as it would + // on a pipe nobody reads. + let (tx, rx) = std::sync::mpsc::sync_channel(8); + let read = move || { + let mut chunk = vec![0; chunk]; + loop { + let got = match from.read(&mut chunk) { + Ok(0) => return, + Ok(n) => Ok(chunk[..n].to_vec()), + Err(e) if e.kind() == ErrorKind::Interrupted => continue, + Err(e) => Err(e), + }; + let failed = got.is_err(); + if tx.send(got).is_err() || failed { + return; + } + } + }; + std::thread::Builder::new().name("browser-pipe".into()).spawn(read).context("starting the thread that reads from the browser")?; + Ok(Replies(rx)) } - let mask = if masked { - if buf.len() < at + 4 { - return Ok(None); + + /// Waits up to `timeout` for the browser to write, and adds what it wrote to + /// `buf`. False if the time passed first. + fn more(&mut self, buf: &mut Vec, timeout: Duration) -> Result { + use std::sync::mpsc::RecvTimeoutError; + match self.0.recv_timeout(timeout) { + Ok(Ok(bytes)) => buf.extend_from_slice(&bytes), + Ok(Err(e)) => return Err(e).context("reading from the browser"), + Err(RecvTimeoutError::Timeout) => return Ok(false), + Err(RecvTimeoutError::Disconnected) => bail!(CLOSED), } - at += 4; - Some([buf[at - 4], buf[at - 3], buf[at - 2], buf[at - 1]]) - } else { - None - }; - let end = at + len as usize; - if buf.len() < end { - return Ok(None); - } - let mut payload = buf[at..end].to_vec(); - if let Some(m) = mask { - payload.iter_mut().enumerate().for_each(|(i, b)| *b ^= m[i % 4]); + Ok(true) } - Ok(Some((opcode, fin, payload, end))) } const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +/// What the tests make their data URLs with; camless itself only decodes. +#[cfg(test)] pub fn base64_encode(data: &[u8]) -> String { let mut out = String::with_capacity(data.len().div_ceil(3) * 4); for c in data.chunks(3) { @@ -365,45 +259,45 @@ mod tests { assert!(base64_decode("a$b").is_err()); } + /// A pipe as (read end, write end). #[cfg(unix)] - #[test] - fn piped_messages_split_over_reads_come_out_whole_and_in_order() { + fn pipe() -> (File, File) { use std::os::fd::FromRawFd; let mut fds = [0; 2]; assert_eq!(unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) }, 0); - let (mut from, mut to) = unsafe { (std::fs::File::from_raw_fd(fds[0]), std::fs::File::from_raw_fd(fds[1])) }; + unsafe { (File::from_raw_fd(fds[0]), File::from_raw_fd(fds[1])) } + } + + #[cfg(windows)] + fn pipe() -> (File, File) { + use std::os::windows::io::FromRawHandle; + use windows_sys::Win32::System::Pipes::CreatePipe; + let (mut read, mut write) = (std::ptr::null_mut(), std::ptr::null_mut()); + assert_ne!(unsafe { CreatePipe(&mut read, &mut write, std::ptr::null(), 0) }, 0); + unsafe { (File::from_raw_handle(read), File::from_raw_handle(write)) } + } + + #[test] + fn piped_messages_split_over_reads_come_out_whole_and_in_order() { + let (from, mut to) = pipe(); + let mut from = Replies::new(from, 7).unwrap(); let mut buf = Vec::new(); - let mut chunk = vec![0; 7]; to.write_all(b"{\"id\":1,\"a\":\"long enough for many reads\"}\0{\"id\":2}\0{\"id\"").unwrap(); - let first = read_piped(&mut from, &mut buf, &mut chunk, Duration::from_secs(5)).unwrap().unwrap(); + let first = read_message(&mut from, &mut buf, Duration::from_secs(5)).unwrap().unwrap(); assert_eq!(first["id"], 1); - assert_eq!(read_piped(&mut from, &mut buf, &mut chunk, Duration::from_secs(5)).unwrap().unwrap()["id"], 2); - assert_eq!(read_piped(&mut from, &mut buf, &mut chunk, Duration::from_millis(1)).unwrap(), None); + assert_eq!(read_message(&mut from, &mut buf, Duration::from_secs(5)).unwrap().unwrap()["id"], 2); + assert_eq!(read_message(&mut from, &mut buf, Duration::from_millis(50)).unwrap(), None); to.write_all(b":3}\0").unwrap(); - assert_eq!(read_piped(&mut from, &mut buf, &mut chunk, Duration::from_secs(5)).unwrap().unwrap()["id"], 3); + assert_eq!(read_message(&mut from, &mut buf, Duration::from_secs(5)).unwrap().unwrap()["id"], 3); assert!(buf.is_empty()); } #[test] - fn frames_round_trip_at_every_length_form() { - for len in [0usize, 125, 126, 65535, 65536, 300_000] { - let payload: Vec = (0..len).map(|i| i as u8).collect(); - let frame = encode_frame(0x1, &payload, [1, 2, 3, 4]); - let (op, fin, got, used) = parse_frame(&frame).unwrap().unwrap(); - assert_eq!((op, fin, used), (0x1, true, frame.len())); - assert_eq!(got, payload); - // Half a frame is not a frame yet. - assert!(parse_frame(&frame[..frame.len() - 1]).unwrap().is_none() || len == 0); - } - } - - #[test] - fn reads_unmasked_server_frames() { - let mut frame = vec![0x81, 126, 0x01, 0x00]; - frame.extend(std::iter::repeat_n(b'x', 256)); - frame.extend_from_slice(&[0x89, 0]); - let (op, _, payload, used) = parse_frame(&frame).unwrap().unwrap(); - assert_eq!((op, payload.len(), used), (1, 256, 260)); - assert_eq!(parse_frame(&frame[used..]).unwrap().unwrap().0, 0x9); + fn a_closed_pipe_is_an_error_and_not_a_wait() { + let (from, to) = pipe(); + let mut from = Replies::new(from, 64).unwrap(); + drop(to); + let e = read_message(&mut from, &mut Vec::new(), Duration::from_secs(5)).unwrap_err(); + assert_eq!(e.to_string(), CLOSED); } } diff --git a/src/web.rs b/src/web.rs index ffcad3a..521e3e2 100644 --- a/src/web.rs +++ b/src/web.rs @@ -481,22 +481,14 @@ struct Browser { name: String, } -/// How camless reaches the browser's DevTools. On Windows, a port on 127.0.0.1, which -/// the browser writes in `DevToolsActivePort`. On Linux, two pipes, as other users on -/// the same machine could connect to that port too. -#[cfg(windows)] -const DEVTOOLS: &str = "--remote-debugging-port=0"; -#[cfg(not(windows))] +/// How camless reaches the browser's DevTools: two pipes of its own, and not a port +/// on 127.0.0.1, which every user and program on the computer could connect to. const DEVTOOLS: &str = "--remote-debugging-pipe"; impl Browser { /// Starts the browser and connects to its DevTools. fn launch(installed: &Installed, dir: &Path, w: u16, h: u16, stop: &AtomicBool) -> Result<(Browser, Cdp)> { let (exe, name) = (&installed.exe, installed.name); - #[cfg(windows)] - let port_file = dir.join("DevToolsActivePort"); - #[cfg(windows)] - let _ = std::fs::remove_file(&port_file); let args = [ "--headless=new".to_string(), DEVTOOLS.into(), @@ -528,47 +520,30 @@ impl Browser { "about:blank".into(), ]; let deadline = Instant::now() + Duration::from_secs(20); - #[cfg(windows)] - { - let browser = Browser { process: os::spawn(exe, &args)?, name: name.into() }; - loop { - // The file has the port, then the browser's WebSocket path. - if let Ok(text) = std::fs::read_to_string(&port_file) { - let mut lines = text.lines(); - if let (Some(Ok(port)), Some(path)) = (lines.next().map(str::parse), lines.next()) { - let cdp = Cdp::connect(port, path)?; - return Ok((browser, cdp)); - } - } - browser.starting(stop, deadline, "did not open its DevTools port")?; - std::thread::sleep(Duration::from_millis(100)); + let (process, to, from) = os::spawn_piped(exe, &args)?; + let browser = Browser { process, name: name.into() }; + let mut cdp = Cdp::pipe(to, from)?; + // Its first reply says it is up. + let up = (|| -> Result<()> { + let id = cdp.send("Browser.getVersion", json!({}), None)?; + while cdp.reply(id, Duration::from_millis(100))?.is_none() { + browser.starting(stop, deadline, "did not answer on its DevTools pipe")?; } - } - #[cfg(not(windows))] - { - let (process, to, from) = os::spawn_piped(exe, &args)?; - let browser = Browser { process, name: name.into() }; - let mut cdp = Cdp::pipe(to, from); - // Its first reply says it is up. - let up = (|| -> Result<()> { - let id = cdp.send("Browser.getVersion", json!({}), None)?; - while cdp.reply(id, Duration::from_millis(100))?.is_none() { - browser.starting(stop, deadline, "did not answer on its DevTools pipe")?; - } - Ok(()) - })(); - match up { - Ok(()) => Ok((browser, cdp)), - // A browser gone at start closes its pipes; how it ended says more. - Err(_) if !stop.load(Ordering::SeqCst) && browser.process.wait(Duration::from_secs(1)) => { - bail!("{name} exited at start (code {})", browser.process.exit_code().unwrap_or_default()) - } - // Not yet tried: snap-confine may not pass the pipes on. - Err(e) if exe.starts_with("/snap/") => { - Err(e.context("camless has not been tried with the Chromium snap yet; Google Chrome's .deb works")) - } - Err(e) => Err(e), + Ok(()) + })(); + match up { + Ok(()) => Ok((browser, cdp)), + // A browser gone at start closes its pipes; how it ended says more. + Err(_) if !stop.load(Ordering::SeqCst) && browser.process.wait(Duration::from_secs(1)) => { + // Chrome before 118 cannot be handed its pipes on Windows. + let needs = if cfg!(windows) { "; camless needs version 118 or newer" } else { "" }; + bail!("{name} exited at start (code {}){needs}", browser.process.exit_code().unwrap_or_default()) + } + // Not yet tried: snap-confine may not pass the pipes on. + Err(e) if exe.starts_with("/snap/") => { + Err(e.context("camless has not been tried with the Chromium snap yet; Google Chrome's .deb works")) } + Err(e) => Err(e), } } @@ -603,28 +578,39 @@ use win as os; #[cfg(not(windows))] use unix as os; -/// Starting the browser: as the signed-in user, inside a kill-on-close job. +/// Starting the browser: as the signed-in user, inside a kill-on-close job, with the +/// two pipes camless drives it through. #[cfg(windows)] mod win { - use anyhow::{bail, Result}; + use anyhow::{bail, Context, Result}; use std::ffi::c_void; + use std::fs::File; use std::os::windows::ffi::OsStrExt; + use std::os::windows::io::FromRawHandle; use std::path::Path; use std::ptr::{null, null_mut}; use std::time::Duration; - use windows_sys::Win32::Foundation::{CloseHandle, HANDLE, STILL_ACTIVE, WAIT_OBJECT_0}; + use windows_sys::Win32::Foundation::{ + CloseHandle, DuplicateHandle, SetHandleInformation, DUPLICATE_CLOSE_SOURCE, DUPLICATE_SAME_ACCESS, HANDLE, HANDLE_FLAG_INHERIT, + STILL_ACTIVE, WAIT_OBJECT_0, + }; use windows_sys::Win32::Security::{ - DuplicateTokenEx, GetTokenInformation, SecurityImpersonation, TokenElevation, TokenPrimary, TOKEN_ADJUST_DEFAULT, - TOKEN_ADJUST_SESSIONID, TOKEN_ASSIGN_PRIMARY, TOKEN_DUPLICATE, TOKEN_ELEVATION, TOKEN_QUERY, + GetKernelObjectSecurity, GetTokenInformation, InitializeSecurityDescriptor, SetKernelObjectSecurity, SetSecurityDescriptorDacl, + SetSecurityDescriptorOwner, TokenDefaultDacl, TokenElevation, TokenUser, DACL_SECURITY_INFORMATION, GROUP_SECURITY_INFORMATION, + LABEL_SECURITY_INFORMATION, OWNER_SECURITY_INFORMATION, SECURITY_ATTRIBUTES, SECURITY_DESCRIPTOR, TOKEN_DEFAULT_DACL, + TOKEN_ELEVATION, TOKEN_INFORMATION_CLASS, TOKEN_QUERY, TOKEN_USER, }; use windows_sys::Win32::System::JobObjects::{ AssignProcessToJobObject, CreateJobObjectW, JobObjectExtendedLimitInformation, SetInformationJobObject, TerminateJobObject, JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, }; + use windows_sys::Win32::System::Pipes::CreatePipe; use windows_sys::Win32::System::Threading::{ - CreateProcessW, CreateProcessWithTokenW, GetCurrentProcess, GetExitCodeProcess, OpenProcess, OpenProcessToken, ResumeThread, - TerminateProcess, WaitForSingleObject, CREATE_NO_WINDOW, CREATE_SUSPENDED, PROCESS_INFORMATION, - PROCESS_QUERY_LIMITED_INFORMATION, STARTUPINFOW, + CreateProcessW, DeleteProcThreadAttributeList, GetCurrentProcess, GetExitCodeProcess, InitializeProcThreadAttributeList, + OpenProcess, OpenProcessToken, ResumeThread, TerminateProcess, UpdateProcThreadAttribute, WaitForSingleObject, + CREATE_NO_WINDOW, CREATE_SUSPENDED, EXTENDED_STARTUPINFO_PRESENT, PROCESS_CREATE_PROCESS, PROCESS_DUP_HANDLE, + PROCESS_QUERY_LIMITED_INFORMATION, + PROCESS_INFORMATION, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, STARTF_USESTDHANDLES, STARTUPINFOEXW, }; use windows_sys::Win32::UI::WindowsAndMessaging::{GetShellWindow, GetWindowThreadProcessId}; @@ -713,9 +699,44 @@ mod win { } } - /// A primary token of the desktop's user, from Explorer: an elevated camless + /// A handle of camless's own, closed when dropped. + struct Owned(HANDLE); + + impl Drop for Owned { + fn drop(&mut self) { + unsafe { CloseHandle(self.0) }; + } + } + + /// A copy of one of camless's handles in another process, for a child of that + /// process to inherit. Taken out of that process again when dropped. + struct Lent<'a> { + holder: &'a Owned, + handle: HANDLE, + } + + impl<'a> Lent<'a> { + fn new(holder: &'a Owned, ours: &Owned) -> Result> { + let mut handle = null_mut(); + let ok = unsafe { DuplicateHandle(GetCurrentProcess(), ours.0, holder.0, &mut handle, 0, 1, DUPLICATE_SAME_ACCESS) } != 0; + if !ok { + bail!("lending a pipe to the desktop shell: {}", std::io::Error::last_os_error()); + } + Ok(Lent { holder, handle }) + } + } + + impl Drop for Lent<'_> { + fn drop(&mut self) { + // With no target, this only closes the handle where it is. + unsafe { DuplicateHandle(self.holder.0, self.handle, null_mut(), null_mut(), 0, 0, DUPLICATE_CLOSE_SOURCE) }; + } + } + + /// The desktop shell's process, Explorer, to start the browser as its child: the + /// browser then runs with the signed-in user's own rights, as an elevated camless /// must not hand administrator rights to a web page's browser. - fn shell_token() -> Result { + fn shell_process() -> Result { unsafe { let shell = GetShellWindow(); if shell.is_null() { @@ -723,51 +744,229 @@ mod win { } let mut pid = 0; GetWindowThreadProcessId(shell, &mut pid); - let process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid); + let process = OpenProcess(PROCESS_CREATE_PROCESS | PROCESS_DUP_HANDLE | PROCESS_QUERY_LIMITED_INFORMATION | READ_CONTROL, 0, pid); if process.is_null() { bail!("opening the desktop shell: {}", std::io::Error::last_os_error()); } + Ok(Owned(process)) + } + } + + /// Who may open a program started under the desktop shell, its first thread and + /// its token: what they would be for a program the shell had started itself. + /// + /// Windows writes those from the token of whoever calls `CreateProcessW`, not from + /// the parent's. From an elevated camless they let administrators in, carry the + /// integrity level of an administrator's program, and leave the program's own + /// user, who is not one there, with little. Edge 157 could then not copy its own + /// token for its sandbox: "GPU process launch failed: error_code=49" in its log, + /// `SBOX_ERROR_CANNOT_CREATE_RESTRICTED_TOKEN`, and a breakpoint dialog on the + /// desktop. With the token put right it rendered, but could not open itself to + /// start its crash handler. + struct Rights { + /// TOKEN_USER and TOKEN_DEFAULT_DACL of the shell's token. + user: Vec, + access: Vec, + /// The security descriptor of the shell's token itself. + token: Vec, + /// The integrity level on the shell's process. + level: Vec, + } + + const READ_CONTROL: u32 = 0x2_0000; + const WRITE_DAC: u32 = 0x4_0000; + const WRITE_OWNER: u32 = 0x8_0000; + /// The parts of a token's security descriptor that say who may open it. + const WHO: u32 = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION | LABEL_SECURITY_INFORMATION; + + /// Parts of the security descriptor of a process, thread or token. + fn security_of(object: HANDLE, parts: u32) -> Result> { + unsafe { + let mut len = 0; + GetKernelObjectSecurity(object, parts, null_mut(), 0, &mut len); + let mut security = vec![0usize; (len as usize).div_ceil(size_of::())]; + if GetKernelObjectSecurity(object, parts, security.as_mut_ptr() as *mut c_void, len, &mut len) == 0 { + bail!("reading who may open the desktop shell: {}", std::io::Error::last_os_error()); + } + Ok(security) + } + } + + impl Rights { + fn of(shell: &Owned) -> Result { let mut token = null_mut(); - let opened = OpenProcessToken(process, TOKEN_DUPLICATE, &mut token) != 0; - CloseHandle(process); - if !opened { + if unsafe { OpenProcessToken(shell.0, TOKEN_QUERY | READ_CONTROL, &mut token) } == 0 { bail!("reading the desktop user's token: {}", std::io::Error::last_os_error()); } - let mut primary = null_mut(); - let access = TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY | TOKEN_ADJUST_DEFAULT | TOKEN_ADJUST_SESSIONID; - let ok = DuplicateTokenEx(token, access, null(), SecurityImpersonation, TokenPrimary, &mut primary) != 0; - CloseHandle(token); - if !ok { - bail!("copying the desktop user's token: {}", std::io::Error::last_os_error()); + let token = Owned(token); + Ok(Rights { + user: token_information(&token, TokenUser)?, + access: token_information(&token, TokenDefaultDacl)?, + token: security_of(token.0, WHO)?, + level: security_of(shell.0, LABEL_SECURITY_INFORMATION)?, + }) + } + + /// A security descriptor for the process and its first thread, which points + /// into `self`. + fn descriptor(&self) -> Result { + unsafe { + let user = (*(self.user.as_ptr() as *const TOKEN_USER)).User.Sid; + let access = (*(self.access.as_ptr() as *const TOKEN_DEFAULT_DACL)).DefaultDacl; + // Without a list, the descriptor would let everyone in. + if access.is_null() { + bail!("the desktop user's token has no default access list"); + } + let mut descriptor: SECURITY_DESCRIPTOR = std::mem::zeroed(); + let d = &mut descriptor as *mut SECURITY_DESCRIPTOR as *mut c_void; + if InitializeSecurityDescriptor(d, SECURITY_DESCRIPTOR_REVISION) == 0 + || SetSecurityDescriptorOwner(d, user, 0) == 0 + || SetSecurityDescriptorDacl(d, 1, access, 0) == 0 + { + bail!("describing the browser's rights: {}", std::io::Error::last_os_error()); + } + Ok(descriptor) + } + } + + /// Puts right what the descriptor at the start cannot: the integrity level on + /// `process` and `thread`, not yet running, and who may open their token. + fn hand_over(&self, process: HANDLE, thread: HANDLE) -> Result<()> { + unsafe { + let mut token = null_mut(); + if OpenProcessToken(process, WRITE_DAC | WRITE_OWNER, &mut token) == 0 { + bail!("opening the browser's token: {}", std::io::Error::last_os_error()); + } + let token = Owned(token); + let level = self.level.as_ptr() as *mut c_void; + if SetKernelObjectSecurity(token.0, WHO, self.token.as_ptr() as *mut c_void) == 0 + || SetKernelObjectSecurity(process, LABEL_SECURITY_INFORMATION, level) == 0 + || SetKernelObjectSecurity(thread, LABEL_SECURITY_INFORMATION, level) == 0 + { + bail!("handing the browser over to the desktop user: {}", std::io::Error::last_os_error()); + } + Ok(()) } - Ok(primary) } } - pub fn spawn(exe: &Path, args: &[String]) -> Result { + const SECURITY_DESCRIPTOR_REVISION: u32 = 1; + + /// One piece of a token's information, in memory aligned for its struct. + fn token_information(token: &Owned, class: TOKEN_INFORMATION_CLASS) -> Result> { + unsafe { + let mut len = 0; + GetTokenInformation(token.0, class, null_mut(), 0, &mut len); + let mut buf = vec![0usize; (len as usize).div_ceil(size_of::())]; + if GetTokenInformation(token.0, class, buf.as_mut_ptr() as *mut c_void, len, &mut len) == 0 { + bail!("reading the desktop user's token: {}", std::io::Error::last_os_error()); + } + Ok(buf) + } + } + + /// A pipe as (read end, write end), neither inherited by other programs. + fn pipe() -> Result<(Owned, Owned)> { + let (mut read, mut write) = (null_mut(), null_mut()); + // The size is a hint; a screencast frame is a few hundred kB. + if unsafe { CreatePipe(&mut read, &mut write, null(), 1 << 20) } == 0 { + return Err(std::io::Error::last_os_error()).context("making a pipe for the browser"); + } + Ok((Owned(read), Owned(write))) + } + + /// Starts the browser with the pipes `--remote-debugging-pipe` takes. On Windows + /// it inherits them as two handles and learns their numbers from + /// `--remote-debugging-io-pipes`: the one it reads commands from, then the one it + /// writes to (`AdoptPipes` in Chromium's `devtools_agent_host_impl.cc`, since + /// Chrome 118). Returns the ends camless writes to and reads from. + pub fn spawn_piped(exe: &Path, args: &[String]) -> Result<(Process, File, File)> { + let shell = if is_elevated() { Some(shell_process()?) } else { None }; + spawn_with_pipes(exe, shell, |commands, replies| { + std::iter::once(format!("--remote-debugging-io-pipes={commands},{replies}")).chain(args.iter().cloned()).collect() + }) + } + + /// Starts `exe` with two pipes: one it reads from and one it writes to. `args` + /// gets the numbers of those two handles in the new process and returns its + /// arguments. Returns the ends camless writes to and reads from. + /// + /// A program inherits handles from its parent. With `shell`, that process is the + /// parent and not camless, so the program's ends are lent to it for the start. + fn spawn_with_pipes(exe: &Path, shell: Option, args: impl Fn(usize, usize) -> Vec) -> Result<(Process, File, File)> { + let (commands, to) = pipe()?; + let (from, replies) = pipe()?; + let rights = match &shell { + Some(shell) => Some(Rights::of(shell)?), + None => None, + }; + let mut descriptor = match &rights { + Some(rights) => Some(rights.descriptor()?), + None => None, + }; + let lent = match &shell { + Some(shell) => Some((Lent::new(shell, &commands)?, Lent::new(shell, &replies)?)), + None => None, + }; + let inherited: [HANDLE; 2] = match &lent { + Some((commands, replies)) => [commands.handle, replies.handle], + None => { + for end in [&commands, &replies] { + if unsafe { SetHandleInformation(end.0, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT) } == 0 { + bail!("passing a pipe to {}: {}", exe.display(), std::io::Error::last_os_error()); + } + } + [commands.0, replies.0] + } + }; let mut cmdline = quote(&exe.to_string_lossy()); - for a in args { + for a in args(inherited[0] as usize, inherited[1] as usize) { cmdline.push(' '); - cmdline.push_str("e(a)); + cmdline.push_str("e(&a)); } let app = wide(exe.as_os_str()); let mut cmd = wide(std::ffi::OsStr::new(&cmdline)); unsafe { - let mut si: STARTUPINFOW = std::mem::zeroed(); - si.cb = size_of::() as u32; + // The list names the only handles the program inherits, and its parent. + let count = if shell.is_some() { 2 } else { 1 }; + let mut size = 0; + InitializeProcThreadAttributeList(null_mut(), count, 0, &mut size); + let mut list = vec![0usize; size.div_ceil(size_of::())]; + let attributes = list.as_mut_ptr() as *mut c_void; + if InitializeProcThreadAttributeList(attributes, count, 0, &mut size) == 0 { + bail!("starting {}: {}", exe.display(), std::io::Error::last_os_error()); + } + let handles = (PROC_THREAD_ATTRIBUTE_HANDLE_LIST as usize, inherited.as_ptr() as *const c_void, size_of_val(&inherited)); + let parent = shell.as_ref().map(|s| &s.0 as *const HANDLE as *const c_void); + let parent = parent.map(|shell| (PROC_THREAD_ATTRIBUTE_PARENT_PROCESS as usize, shell, size_of::())); + let mut si: STARTUPINFOEXW = std::mem::zeroed(); + si.StartupInfo.cb = size_of::() as u32; + // No standard input or output, said outright. Left unsaid, Windows gives the + // program camless's own by their numbers, and under another parent those + // numbers are that parent's handles, whatever they are. + si.StartupInfo.dwFlags = STARTF_USESTDHANDLES; + si.lpAttributeList = attributes; let mut pi: PROCESS_INFORMATION = std::mem::zeroed(); - let started = if is_elevated() { - let token = shell_token()?; - let ok = CreateProcessWithTokenW(token, 0, app.as_ptr(), cmd.as_mut_ptr(), CREATE_SUSPENDED, null(), null(), &si, &mut pi); - CloseHandle(token); - ok != 0 - } else { - let flags = CREATE_SUSPENDED | CREATE_NO_WINDOW; - CreateProcessW(app.as_ptr(), cmd.as_mut_ptr(), null(), null(), 0, flags, null(), null(), &si, &mut pi) != 0 + let flags = CREATE_SUSPENDED | CREATE_NO_WINDOW | EXTENDED_STARTUPINFO_PRESENT; + // For the process and for its first thread. + let attributes_of = |d: &mut SECURITY_DESCRIPTOR| SECURITY_ATTRIBUTES { + nLength: size_of::() as u32, + lpSecurityDescriptor: d as *mut SECURITY_DESCRIPTOR as *mut c_void, + bInheritHandle: 0, }; + let security = descriptor.as_mut().map(attributes_of); + let security = security.as_ref().map_or(null(), |s| s as *const SECURITY_ATTRIBUTES); + let started = [Some(handles), parent] + .into_iter() + .flatten() + .all(|(attribute, value, len)| UpdateProcThreadAttribute(attributes, 0, attribute, value, len, null_mut(), null()) != 0) + && CreateProcessW(app.as_ptr(), cmd.as_mut_ptr(), security, security, 1, flags, null(), null(), &si.StartupInfo, &mut pi) != 0; + let error = std::io::Error::last_os_error(); + DeleteProcThreadAttributeList(attributes); if !started { - bail!("starting {}: {}", exe.display(), std::io::Error::last_os_error()); + bail!("starting {}: {error}", exe.display()); } + let thread = Owned(pi.hThread); // Suspended until it is in the job, so none of its child processes escape it. let job = CreateJobObjectW(null(), null()); let mut limits: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = std::mem::zeroed(); @@ -779,15 +978,57 @@ mod win { if !in_job && !job.is_null() { CloseHandle(job); } - ResumeThread(pi.hThread); - CloseHandle(pi.hThread); - Ok(Process { handle: pi.hProcess, job: in_job.then_some(job) }) + // From here on, an early return ends the program. + let process = Process { handle: pi.hProcess, job: in_job.then_some(job) }; + if let Some(rights) = &rights { + rights.hand_over(process.handle, thread.0)?; + } + ResumeThread(thread.0); + // The program has its ends now. The copies here and in the shell close, so + // each side sees the other one go. + drop(lent); + drop((commands, replies)); + let ends = [to, from].map(|end| File::from_raw_handle(std::mem::ManuallyDrop::new(end).0)); + let [to, from] = ends; + Ok((process, to, from)) } } #[cfg(test)] mod tests { - use super::quote; + use super::*; + + /// What camless writes to one pipe comes back from the other. The program in + /// between is this test program again, running only the test below. + #[test] + fn devtools_messages_go_through_the_pipes() { + let me = std::env::current_exe().unwrap(); + // The numbers travel as a name to skip, which no test has. + let args = |read, write| { + let only = ["web::win::tests::copies_its_pipes_when_asked", "--exact", "--skip"].map(String::from); + only.into_iter().chain([format!("pipes={read},{write}")]).collect() + }; + let (p, to, from) = spawn_with_pipes(&me, None, args).unwrap(); + let mut cdp = crate::cdp::Cdp::pipe(to, from).unwrap(); + let id = cdp.send("Browser.getVersion", serde_json::json!({}), None).unwrap(); + let echo = cdp.reply(id, Duration::from_secs(30)).unwrap().expect("the message came back"); + assert_eq!(echo["method"], "Browser.getVersion"); + // Closing camless's end ends the copy, and with it the program. + drop(cdp); + assert!(p.wait(Duration::from_secs(30))); + assert_eq!(p.exit_code(), Some(0)); + } + + /// The other half of the test above; on its own it has nothing to do. + #[test] + fn copies_its_pipes_when_asked() { + let Some(pipes) = std::env::args().find_map(|a| a.strip_prefix("pipes=").map(str::to_string)) else { + return; + }; + let (read, write) = pipes.split_once(',').unwrap(); + let open = |number: &str| unsafe { File::from_raw_handle(number.parse::().unwrap() as HANDLE) }; + std::io::copy(&mut open(read), &mut open(write)).unwrap(); + } #[test] fn quotes_like_the_c_runtime() { @@ -920,7 +1161,7 @@ mod unix { #[test] fn devtools_messages_go_through_the_pipes() { let (p, to, from) = spawn_piped(Path::new("/bin/sh"), &["-c".into(), "cat <&3 >&4".into()]).unwrap(); - let mut cdp = crate::cdp::Cdp::pipe(to, from); + let mut cdp = crate::cdp::Cdp::pipe(to, from).unwrap(); let id = cdp.send("Browser.getVersion", serde_json::json!({}), None).unwrap(); let echo = cdp.reply(id, Duration::from_secs(5)).unwrap().expect("the message came back"); assert_eq!(echo["method"], "Browser.getVersion");