From 2bd4fd35b8bca2b814fc8c5b276d873f3e4c95ca Mon Sep 17 00:00:00 2001 From: Simon Hull <5488089+simonhull@users.noreply.github.com> Date: Tue, 3 Nov 2026 13:17:16 -0600 Subject: [PATCH 1/2] Control channel: harden the token file and the app's control server The app wrote its control token to the temp folder with default permissions from a hash-based generator, compared it with ==, spawned unlimited threads and let an unauthenticated client hold a connection for 10 minutes. The shared token file was opened with create+truncate, which follows a planted symlink. - Token files are created new (remove, then O_EXCL) with mode 0600; clients only read a regular, owner-only, non-symlink file. - The app's control server uses the shared security module: OS-random token, constant-time compare, bounded lines, at most 16 connections, one auth attempt within 10 s, close on malformed JSON before auth. - serve --port and the app bind before writing the token file. Ported from TextureCraft 5a065c1 and 836e3f2. --- Cargo.lock | 2 + Cargo.toml | 1 + apps/materialcraft-cli/src/main.rs | 18 +- apps/materialcraft/Cargo.toml | 1 + apps/materialcraft/src/control_server.rs | 254 ++++++++++++++++------- crates/automation/Cargo.toml | 3 + crates/automation/src/rpc.rs | 18 +- crates/automation/src/security.rs | 182 +++++++++++++++- docs/control-protocol.md | 10 +- 9 files changed, 400 insertions(+), 89 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ce3b0fc..c0a2238 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2313,6 +2313,7 @@ dependencies = [ "egui", "image", "log", + "materialcraft-automation", "materialcraft-engine", "materialcraft-ui-egui", "serde_json", @@ -2332,6 +2333,7 @@ dependencies = [ "materialcraft-render", "rayon", "rmcp", + "rustix 1.1.6", "schemars", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 663ec4d..284c172 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,6 +68,7 @@ eframe = { version = "0.37", default-features = false } egui = "0.37" wgpu = "30" rfd = "0.17" +rustix = { version = "1", default-features = false, features = ["std", "fs", "process"] } zip = { version = "2", default-features = false, features = ["deflate"] } gltf = { version = "1.4", default-features = false, features = ["import", "utils"] } tobj = { version = "4", default-features = false } diff --git a/apps/materialcraft-cli/src/main.rs b/apps/materialcraft-cli/src/main.rs index fef8f39..6d2b24e 100644 --- a/apps/materialcraft-cli/src/main.rs +++ b/apps/materialcraft-cli/src/main.rs @@ -274,14 +274,16 @@ fn run(cmd: &str, a: &Args) -> Result<(), String> { let h = Arc::new(Mutex::new(Headless::new(Headless::default_session(), workspace(a)?))); match a.num::("--port")? { Some(port) => { - let (token, file) = server_token(port).map_err(|e| e.to_string())?; - rpc::serve_tcp(port, h, token, |addr| { - eprintln!("materialcraft-cli: serving JSON lines on {addr}"); - if let Some(f) = &file { - eprintln!("materialcraft-cli: token in {}", f.display()); - } - }) - .map_err(|e| e.to_string())?; + // Bind first: a port in use must not replace its server's token file, and port 0's + // token file is named after the port actually picked. + let listener = rpc::bind_tcp(port).map_err(|e| e.to_string())?; + let addr = listener.local_addr().map_err(|e| e.to_string())?; + let (token, file) = server_token(addr.port()).map_err(|e| e.to_string())?; + eprintln!("materialcraft-cli: serving JSON lines on {addr}"); + if let Some(f) = &file { + eprintln!("materialcraft-cli: token in {}", f.display()); + } + rpc::serve_listener(listener, h, token).map_err(|e| e.to_string())?; } None => rpc::serve_lines(&h, std::io::stdin().lock(), std::io::stdout().lock()).map_err(|e| e.to_string())?, } diff --git a/apps/materialcraft/Cargo.toml b/apps/materialcraft/Cargo.toml index 64a1ca4..7b5a2ca 100644 --- a/apps/materialcraft/Cargo.toml +++ b/apps/materialcraft/Cargo.toml @@ -12,6 +12,7 @@ workspace = true [dependencies] materialcraft-engine = { workspace = true } +materialcraft-automation = { workspace = true } materialcraft-ui-egui = { workspace = true } eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit", "wayland", "x11", "persistence"] } egui = { workspace = true } diff --git a/apps/materialcraft/src/control_server.rs b/apps/materialcraft/src/control_server.rs index a094e2d..0f841c4 100644 --- a/apps/materialcraft/src/control_server.rs +++ b/apps/materialcraft/src/control_server.rs @@ -1,42 +1,26 @@ //! Loopback JSON-lines control server (`--control `): one request per line, //! `{"id", "method", "params"}`, one reply per line. The first request must be //! `{"method":"auth","params":{"token":"…"}}` with the token from `MATERIALCRAFT_CONTROL_TOKEN` -//! or, when that is unset, the token written to `/materialcraft-control-.token`. +//! or, when that is unset, the token written (owner-only) to +//! `/materialcraft-control-.token`. Token handling, request framing and connection +//! limits are shared with `materialcraft-cli serve` (`materialcraft_automation::security`). -use std::io::{BufRead, BufReader, Read, Write}; +use std::io::{BufReader, Write}; use std::net::{TcpListener, TcpStream}; use std::sync::mpsc::{Receiver, Sender, channel}; use std::time::Duration; +use materialcraft_automation::security::{ + AUTH_TIMEOUT, AuthDeadline, ConnectionLimiter, LineRead, MAX_CONNECTIONS, read_bounded_line, server_token, token_matches, +}; use materialcraft_ui_egui::ControlRequest; use serde_json::{Value, json}; -/// Requests larger than this are rejected (screenshots go the other way). -const MAX_REQUEST: u64 = 8 << 20; - -fn token(port: u16) -> String { - if let Ok(t) = std::env::var("MATERIALCRAFT_CONTROL_TOKEN") - && t.len() >= 16 - { - return t; - } - use std::hash::{BuildHasher, Hasher}; - let mut out = String::new(); - for i in 0..4u64 { - let mut h = std::collections::hash_map::RandomState::new().build_hasher(); - h.write_u64(i ^ u64::from(std::process::id())); - h.write_u128(std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0)); - out.push_str(&format!("{:016x}", h.finish())); - } - let path = std::env::temp_dir().join(format!("materialcraft-control-{port}.token")); - if let Err(e) = std::fs::write(&path, &out) { - eprintln!("materialcraft: could not write the control token to {}: {e}", path.display()); - } else { - eprintln!("materialcraft: control token written to {}", path.display()); - } - out -} +/// How long an authenticated connection may stay idle. +const IDLE_TIMEOUT: Duration = Duration::from_secs(600); +/// Listen on `127.0.0.1:port` and return the requests for the UI thread to answer +/// ([`ControlRequest::reply`]). Bind or token failures are printed and leave the channel empty. pub fn start(port: u16, ctx: egui::Context) -> Receiver { let (tx, rx) = channel::(); let listener = match TcpListener::bind(("127.0.0.1", port)) { @@ -46,35 +30,65 @@ pub fn start(port: u16, ctx: egui::Context) -> Receiver { return rx; } }; - let token = token(port); - eprintln!("materialcraft: control server listening on 127.0.0.1:{port}"); - std::thread::spawn(move || { - for stream in listener.incoming().flatten() { - let (tx, ctx, token) = (tx.clone(), ctx.clone(), token.clone()); - std::thread::spawn(move || serve(stream, &token, tx, ctx)); + // After binding, so an app that cannot listen never replaces another server's token. + let token = match server_token(port) { + Ok((token, Some(path))) => { + eprintln!("materialcraft: control token written to {}", path.display()); + token } - }); + Ok((token, None)) => token, + Err(e) => { + eprintln!("materialcraft: control server disabled: {e}"); + return rx; + } + }; + eprintln!("materialcraft: control server listening on 127.0.0.1:{port}"); + std::thread::spawn(move || accept(listener, &token, &tx, &ctx)); rx } -fn serve(stream: TcpStream, token: &str, tx: Sender, ctx: egui::Context) { - let _ = stream.set_read_timeout(Some(Duration::from_secs(600))); - let Ok(read) = stream.try_clone() else { return }; - let mut reader = BufReader::new(read.take(u64::MAX)); - let mut out = stream; - let mut authed = false; - loop { - let mut line = String::new(); - let n = match reader.by_ref().take(MAX_REQUEST).read_line(&mut line) { - Ok(n) => n, - Err(_) => break, +/// Serve connections from `listener` (at most [`MAX_CONNECTIONS`] at once). +fn accept(listener: TcpListener, token: &str, tx: &Sender, ctx: &egui::Context) { + let limiter = ConnectionLimiter::default(); + for stream in listener.incoming() { + let Ok(mut stream) = stream else { continue }; + let Some(permit) = limiter.try_acquire(MAX_CONNECTIONS) else { + let _ = writeln!(stream, "{}", json!({"id": null, "ok": false, "error": "too many connections"})); + continue; }; - if n == 0 { - break; - } - if !line.ends_with('\n') && n as u64 >= MAX_REQUEST { - let _ = writeln!(out, "{}", json!({"id": null, "ok": false, "error": "request too large"})); - break; + let (tx, ctx, token) = (tx.clone(), ctx.clone(), token.to_string()); + std::thread::spawn(move || { + let _permit = permit; + serve(stream, &token, &tx, &ctx); + }); + } +} + +/// Send `reply` with the request's `id`; false when the client is gone. +fn reply(out: &mut TcpStream, id: Value, mut reply: Value) -> bool { + if let Some(o) = reply.as_object_mut() { + o.insert("id".into(), id); + } + writeln!(out, "{reply}").is_ok() && out.flush().is_ok() +} + +/// One connection: `auth` first (within [`AUTH_TIMEOUT`], one attempt), then requests +/// forwarded to the UI thread one at a time until the client disconnects or idles out. +fn serve(stream: TcpStream, token: &str, tx: &Sender, ctx: &egui::Context) { + let Ok(mut deadline) = AuthDeadline::start(&stream, AUTH_TIMEOUT).map(Some) else { return }; + let _ = stream.set_read_timeout(Some(IDLE_TIMEOUT)); + let Ok(read) = stream.try_clone() else { return }; + let mut reader = BufReader::new(read); + let mut out = stream; + let mut line = String::new(); + loop { + match read_bounded_line(&mut reader, &mut line) { + Ok(LineRead::Line) => {} + Ok(LineRead::TooLong) => { + reply(&mut out, Value::Null, json!({"ok": false, "error": "request too large"})); + return; + } + Ok(LineRead::Eof) | Err(_) => return, } if line.trim().is_empty() { continue; @@ -82,33 +96,133 @@ fn serve(stream: TcpStream, token: &str, tx: Sender, ctx: egui:: let msg: Value = match serde_json::from_str(&line) { Ok(v) => v, Err(e) => { - let _ = writeln!(out, "{}", json!({"id": null, "ok": false, "error": format!("bad JSON: {e}")})); + let sent = reply(&mut out, Value::Null, json!({"ok": false, "error": format!("bad JSON: {e}")})); + // Before `auth` a malformed line is the one refused attempt, as on `serve --port`. + if !sent || deadline.is_some() { + return; + } continue; } }; let id = msg.get("id").cloned().unwrap_or(Value::Null); let method = msg.get("method").and_then(Value::as_str).unwrap_or("").to_string(); let params = msg.get("params").cloned().unwrap_or(json!({})); - let mut reply = if !authed { - let ok = method == "auth" && params.get("token").and_then(Value::as_str) == Some(token); - authed = ok; - if ok { json!({"ok": true}) } else { json!({"ok": false, "error": "authenticate first: {\"method\":\"auth\",\"params\":{\"token\":…}}"}) } - } else { - let (req, rrx) = ControlRequest::new(method, params); - if tx.send(req).is_err() { - break; + if deadline.is_some() { + let supplied = params.get("token").and_then(Value::as_str).unwrap_or_default(); + if method != "auth" || !token_matches(token, supplied) { + // One attempt per connection, as on `materialcraft-cli serve`. + reply(&mut out, id, json!({"ok": false, "error": "authenticate first: {\"method\":\"auth\",\"params\":{\"token\":…}}"})); + return; } - ctx.request_repaint(); - rrx.recv_timeout(Duration::from_secs(120)).unwrap_or_else(|_| json!({"ok": false, "error": "timeout"})) - }; - if let Some(o) = reply.as_object_mut() { - o.insert("id".into(), id); + drop(deadline.take()); + if !reply(&mut out, id, json!({"ok": true})) { + return; + } + continue; } - if writeln!(out, "{reply}").is_err() || out.flush().is_err() || !authed { - if !authed { - continue; - } - break; + let (req, rrx) = ControlRequest::new(method, params); + if tx.send(req).is_err() { + return; + } + ctx.request_repaint(); + let answer = rrx.recv_timeout(Duration::from_secs(120)).unwrap_or_else(|_| json!({"ok": false, "error": "timeout"})); + if !reply(&mut out, id, answer) { + return; } } } + +#[cfg(test)] +mod tests { + use std::io::BufRead; + + use super::*; + + const TOKEN: &str = "0123456789abcdef0123456789abcdef"; + + /// A server on a free port whose app side answers every request with its method name. + fn server() -> std::net::SocketAddr { + let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap(); + let addr = listener.local_addr().unwrap(); + let (tx, rx) = channel::(); + std::thread::spawn(move || accept(listener, TOKEN, &tx, &egui::Context::default())); + std::thread::spawn(move || { + for r in rx { + let _ = r.reply.send(json!({"ok": true, "result": r.method})); + } + }); + addr + } + + fn connect(addr: std::net::SocketAddr) -> (TcpStream, BufReader) { + let s = TcpStream::connect(addr).unwrap(); + s.set_read_timeout(Some(Duration::from_secs(20))).unwrap(); + let r = BufReader::new(s.try_clone().unwrap()); + (s, r) + } + + fn call(w: &mut TcpStream, r: &mut BufReader, line: &str) -> Option { + writeln!(w, "{line}").ok()?; + let mut reply = String::new(); + (r.read_line(&mut reply).ok()? > 0).then(|| serde_json::from_str(&reply).unwrap()) + } + + #[test] + fn requests_need_the_token() { + let addr = server(); + // Anything before `auth` is refused and the connection closed. + let (mut w, mut r) = connect(addr); + let denied = call(&mut w, &mut r, r#"{"id":1,"method":"command.run","params":{"id":"file.open"}}"#).unwrap(); + assert_eq!(denied["ok"], false); + assert_eq!(denied["id"], 1); + assert!(call(&mut w, &mut r, r#"{"id":2,"method":"ping"}"#).is_none(), "closed after a refused request"); + // A wrong token is refused and gets no second try. + let (mut w, mut r) = connect(addr); + let wrong = format!(r#"{{"id":1,"method":"auth","params":{{"token":"{}x"}}}}"#, &TOKEN[1..]); + assert_eq!(call(&mut w, &mut r, &wrong).unwrap()["ok"], false); + let right = format!(r#"{{"id":2,"method":"auth","params":{{"token":"{TOKEN}"}}}}"#); + assert!(call(&mut w, &mut r, &right).is_none()); + // So is a malformed first line: no further lines before `auth`. + let (mut w, mut r) = connect(addr); + assert_eq!(call(&mut w, &mut r, "not json").unwrap()["ok"], false); + assert!(call(&mut w, &mut r, &right).is_none(), "closed after malformed JSON before auth"); + // The right token unlocks the connection. + let (mut w, mut r) = connect(addr); + let ok = call(&mut w, &mut r, &right).unwrap(); + assert_eq!((ok["ok"].clone(), ok["id"].clone()), (json!(true), json!(2))); + let pong = call(&mut w, &mut r, r#"{"id":3,"method":"ping"}"#).unwrap(); + assert_eq!((pong["result"].clone(), pong["id"].clone()), (json!("ping"), json!(3))); + assert_eq!(call(&mut w, &mut r, "not json").unwrap()["ok"], false); + assert_eq!(call(&mut w, &mut r, r#"{"id":4,"method":"ping"}"#).unwrap()["id"], 4, "bad JSON keeps an authenticated connection"); + } + + #[test] + fn connections_are_limited_and_idle_ones_time_out() { + let addr = server(); + let idle: Vec<_> = (0..MAX_CONNECTIONS).map(|_| connect(addr)).collect(); + // Let the server take every idle connection before the one over the limit. + std::thread::sleep(Duration::from_millis(300)); + // Read without writing: the server closes at once, and unread input would reset the + // connection before its reply arrives. + let (_, mut r) = connect(addr); + let mut full = String::new(); + r.read_line(&mut full).unwrap(); + assert_eq!(serde_json::from_str::(&full).unwrap()["error"], "too many connections"); + // Unauthenticated connections are closed after AUTH_TIMEOUT, which frees their slots. + for (_, mut r) in idle { + let mut rest = String::new(); + assert_eq!(r.read_line(&mut rest).unwrap(), 0); + } + // A slot frees once its thread has ended, which can trail the client's EOF a little. + let right = format!(r#"{{"id":1,"method":"auth","params":{{"token":"{TOKEN}"}}}}"#); + let authed = (0..50).any(|_| { + let (mut w, mut r) = connect(addr); + let ok = call(&mut w, &mut r, &right).is_some_and(|v| v["ok"] == true); + if !ok { + std::thread::sleep(Duration::from_millis(100)); + } + ok + }); + assert!(authed, "slots are freed after the auth deadline"); + } +} diff --git a/crates/automation/Cargo.toml b/crates/automation/Cargo.toml index 994b5a6..d56a4fb 100644 --- a/crates/automation/Cargo.toml +++ b/crates/automation/Cargo.toml @@ -24,5 +24,8 @@ base64 = { workspace = true } cap-std = { workspace = true } getrandom = { workspace = true } +[target.'cfg(unix)'.dependencies] +rustix = { workspace = true } + [dev-dependencies] rmcp = { workspace = true, features = ["client"] } diff --git a/crates/automation/src/rpc.rs b/crates/automation/src/rpc.rs index 0c156b7..0d4253d 100644 --- a/crates/automation/src/rpc.rs +++ b/crates/automation/src/rpc.rs @@ -18,7 +18,9 @@ use serde_json::{Value, json}; use crate::budgets::json_text; use crate::headless::RenderOptions; -use crate::security::{ConnectionLimiter, LineRead, MAX_BATCH_STEPS, MAX_CONNECTIONS, MAX_REQUEST_BYTES, read_bounded_line, token_matches}; +use crate::security::{ + AUTH_TIMEOUT, AuthDeadline, ConnectionLimiter, LineRead, MAX_BATCH_STEPS, MAX_CONNECTIONS, MAX_REQUEST_BYTES, read_bounded_line, token_matches, +}; use crate::{AutomationError, Headless, bad}; pub const METHODS: &[&str] = &[ @@ -160,6 +162,7 @@ fn serve_connection(h: &Mutex, stream: std::net::TcpStream, token: &st let mut w = stream; let mut line = String::new(); let mut authed = false; + let mut deadline = Some(AuthDeadline::start(&w, AUTH_TIMEOUT)?); loop { match read_bounded_line(&mut r, &mut line)? { LineRead::Eof => return Ok(()), @@ -178,15 +181,26 @@ fn serve_connection(h: &Mutex, stream: std::net::TcpStream, token: &st if !authed { return write_reply(&mut w, &json!({"id": id, "ok": false, "error": "authenticate first: {\"method\":\"auth\",\"params\":{\"token\":…}}"})); } + drop(deadline.take()); write_reply(&mut w, &json!({"id": id, "ok": true}))?; } } +/// Listen on `127.0.0.1:port` (0 picks a port). +pub fn bind_tcp(port: u16) -> Result { + TcpListener::bind(("127.0.0.1", port)).map_err(|e| AutomationError::Io(format!("cannot listen on 127.0.0.1:{port}: {e}"))) +} + /// Serve authenticated JSON lines on `127.0.0.1:port` (0 picks a port) with one shared /// session. `ready` gets the bound address. pub fn serve_tcp(port: u16, h: Arc>, token: String, ready: impl FnOnce(std::net::SocketAddr)) -> Result<(), AutomationError> { - let listener = TcpListener::bind(("127.0.0.1", port)).map_err(|e| AutomationError::Io(format!("cannot listen on 127.0.0.1:{port}: {e}")))?; + let listener = bind_tcp(port)?; ready(listener.local_addr().map_err(|e| AutomationError::Io(e.to_string()))?); + serve_listener(listener, h, token) +} + +/// Serve authenticated JSON lines on a bound listener (see [`bind_tcp`]) with one shared session. +pub fn serve_listener(listener: TcpListener, h: Arc>, token: String) -> Result<(), AutomationError> { let limiter = ConnectionLimiter::default(); let token = Arc::new(token); for stream in listener.incoming() { diff --git a/crates/automation/src/security.rs b/crates/automation/src/security.rs index 165fda1..41bec54 100644 --- a/crates/automation/src/security.rs +++ b/crates/automation/src/security.rs @@ -1,10 +1,13 @@ //! Tokens and bounded framing for the loopback JSON-lines transports (the app's control channel //! and `materialcraft-cli serve` share the token conventions). -use std::io::BufRead; -use std::path::PathBuf; +use std::io::{BufRead, Read, Write}; +use std::net::{Shutdown, TcpStream}; +use std::path::{Path, PathBuf}; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::mpsc::{RecvTimeoutError, Sender, channel}; +use std::time::Duration; use crate::{AutomationError, bad}; @@ -14,6 +17,10 @@ pub const TOKEN_ENV: &str = "MATERIALCRAFT_CONTROL_TOKEN"; pub const MAX_REQUEST_BYTES: usize = 8 << 20; /// Simultaneous TCP connections served by `serve --port`. pub const MAX_CONNECTIONS: usize = 16; +/// Largest token file read (a token is at most 1024 characters, plus whitespace). +const MAX_TOKEN_FILE_BYTES: u64 = 4096; +/// Time a TCP client has to authenticate before its connection is closed. +pub const AUTH_TIMEOUT: Duration = Duration::from_secs(10); /// Steps in one `command_batch`. pub const MAX_BATCH_STEPS: usize = 256; @@ -44,7 +51,7 @@ pub fn client_token(port: u16, supplied: Option) -> Result t, None => { let path = token_file(port); - std::fs::read_to_string(&path) + read_token_file(&path) .map_err(|e| bad(format!("no control token: set {TOKEN_ENV} or start the app with `--control {port}` so it writes {} ({e})", path.display())))? } }; @@ -54,7 +61,8 @@ pub fn client_token(port: u16, supplied: Option) -> Result Result<(String, Option), AutomationError> { if let Ok(t) = std::env::var(TOKEN_ENV) { validate_token(t.trim())?; @@ -62,13 +70,60 @@ pub fn server_token(port: u16) -> Result<(String, Option), AutomationEr } let token = generate_token()?; let path = token_file(port); + write_token_file(&path, &token).map_err(|e| { + AutomationError::Io(format!("cannot write the control token to {} ({e}); set {TOKEN_ENV} to a token of your own instead", path.display())) + })?; + Ok((token, Some(path))) +} + +/// Write `token` to a new file at `path`, readable by the owner only on Unix. Whatever was at +/// `path` is removed first and the file is created exclusively (`O_EXCL`, which never follows +/// a symlink), so a file or link planted in a shared temp folder can neither keep its +/// permissions nor redirect the write; one that cannot be removed (another user's, in a sticky +/// `/tmp`) fails the call. +fn write_token_file(path: &Path, token: &str) -> std::io::Result<()> { + match std::fs::remove_file(path) { + Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e), + _ => {} + } let mut o = std::fs::OpenOptions::new(); - o.write(true).create(true).truncate(true); + o.write(true).create_new(true); #[cfg(unix)] std::os::unix::fs::OpenOptionsExt::mode(&mut o, 0o600); - let res = o.open(&path).and_then(|mut f| std::io::Write::write_all(&mut f, token.as_bytes())); - res.map_err(|e| AutomationError::Io(format!("cannot write the control token to {}: {e}", path.display())))?; - Ok((token, Some(path))) + let mut f = o.open(path)?; + f.write_all(token.as_bytes())?; + f.sync_all() +} + +/// Read a token file written by [`write_token_file`]. On Unix the file must be a regular file +/// (not a symlink, FIFO or device) owned by this user with no group or other permissions, so a +/// file another user planted is never trusted and a link can't make a client send one of its +/// own secrets as the token. +fn read_token_file(path: &Path) -> std::io::Result { + #[cfg(unix)] + let file = { + use rustix::fs::{Mode, OFlags}; + use std::os::unix::fs::MetadataExt; + let flags = OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC; + let file = std::fs::File::from(rustix::fs::open(path, flags, Mode::empty())?); + let meta = file.metadata()?; + let refuse = |why: &str| Err(std::io::Error::new(std::io::ErrorKind::PermissionDenied, format!("refusing the token file: {why}"))); + if !meta.is_file() { + return refuse("not a regular file"); + } + if meta.uid() != rustix::process::geteuid().as_raw() { + return refuse("owned by another user"); + } + if meta.mode() & 0o077 != 0 { + return refuse("readable or writable by other users"); + } + file + }; + #[cfg(not(unix))] + let file = std::fs::File::open(path)?; + let mut out = String::new(); + file.take(MAX_TOKEN_FILE_BYTES).read_to_string(&mut out)?; + Ok(out) } /// Compare equal-length tokens without an early exit. @@ -134,6 +189,27 @@ impl Drop for Permit { } } +/// Closes a connection that has not authenticated in time, so idle or trickling clients can't +/// hold the few connection slots. Drop it once the client is authenticated (or gone). +pub struct AuthDeadline { + _cancel: Sender<()>, +} + +impl AuthDeadline { + /// Shut `stream` down after `timeout` unless the returned deadline is dropped first. + pub fn start(stream: &TcpStream, timeout: Duration) -> std::io::Result { + let stream = stream.try_clone()?; + let (tx, rx) = channel::<()>(); + std::thread::spawn(move || { + // Dropping the deadline disconnects the channel and ends this thread at once. + if let Err(RecvTimeoutError::Timeout) = rx.recv_timeout(timeout) { + let _ = stream.shutdown(Shutdown::Both); + } + }); + Ok(AuthDeadline { _cancel: tx }) + } +} + #[cfg(test)] mod tests { use super::*; @@ -149,6 +225,76 @@ mod tests { assert!(client_token(1, Some(format!(" {a}\n"))).is_ok()); } + fn scratch(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("mc-token-{}-{name}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir + } + + #[test] + fn token_files_round_trip_and_replace_old_ones() { + let dir = scratch("round-trip"); + let path = dir.join("t.token"); + write_token_file(&path, "first-token-0123456789").unwrap(); + assert_eq!(read_token_file(&path).unwrap(), "first-token-0123456789"); + write_token_file(&path, "second-token-0123456789").unwrap(); + assert_eq!(read_token_file(&path).unwrap(), "second-token-0123456789"); + let _ = std::fs::remove_dir_all(dir); + } + + #[cfg(unix)] + #[test] + fn token_files_are_owner_only_even_over_an_open_file() { + use std::os::unix::fs::PermissionsExt; + let dir = scratch("perms"); + let path = dir.join("t.token"); + std::fs::write(&path, "old").unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o666)).unwrap(); + write_token_file(&path, "fresh-token-0123456789").unwrap(); + assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600); + let _ = std::fs::remove_dir_all(dir); + } + + #[cfg(unix)] + #[test] + fn token_files_never_write_through_a_symlink() { + let dir = scratch("symlink-write"); + let (path, victim) = (dir.join("t.token"), dir.join("victim")); + std::fs::write(&victim, "keep me").unwrap(); + std::os::unix::fs::symlink(&victim, &path).unwrap(); + write_token_file(&path, "fresh-token-0123456789").unwrap(); + assert_eq!(std::fs::read_to_string(&victim).unwrap(), "keep me"); + assert!(!std::fs::symlink_metadata(&path).unwrap().file_type().is_symlink()); + let _ = std::fs::remove_dir_all(dir); + } + + #[cfg(unix)] + #[test] + fn unsafe_token_files_are_refused() { + use std::os::unix::fs::PermissionsExt; + let dir = scratch("refuse"); + // Readable by others: another user may have planted or read it. + let open = dir.join("open.token"); + std::fs::write(&open, "planted-token-0123456789").unwrap(); + std::fs::set_permissions(&open, std::fs::Permissions::from_mode(0o644)).unwrap(); + assert!(read_token_file(&open).is_err()); + // A symlink, even to an owner-only file: a client must not send that file as its token. + let secret = dir.join("secret"); + write_token_file(&secret, "private-0123456789abcdef").unwrap(); + let link = dir.join("link.token"); + std::os::unix::fs::symlink(&secret, &link).unwrap(); + assert!(read_token_file(&link).is_err()); + // A FIFO is refused without blocking on open or read. + let fifo = dir.join("fifo.token"); + if std::process::Command::new("mkfifo").arg(&fifo).status().is_ok_and(|s| s.success()) { + assert!(read_token_file(&fifo).is_err()); + } + // A directory is not a token. + assert!(read_token_file(&dir).is_err()); + let _ = std::fs::remove_dir_all(dir); + } + #[test] fn oversized_lines_are_skipped() { let input = format!("{}\n{{\"ok\":1}}\n", "x".repeat(MAX_REQUEST_BYTES + 10)); @@ -160,6 +306,26 @@ mod tests { assert!(matches!(read_bounded_line(&mut r, &mut line).unwrap(), LineRead::Eof)); } + #[test] + fn auth_deadline_closes_idle_connections_only() { + let listener = std::net::TcpListener::bind(("127.0.0.1", 0)).unwrap(); + let addr = listener.local_addr().unwrap(); + let idle = TcpStream::connect(addr).unwrap(); + let (server_idle, _) = listener.accept().unwrap(); + let _deadline = AuthDeadline::start(&server_idle, Duration::from_millis(200)).unwrap(); + let mut buf = [0u8; 1]; + idle.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); + assert_eq!(std::io::Read::read(&mut &idle, &mut buf).unwrap(), 0, "closed by the deadline"); + + let kept = TcpStream::connect(addr).unwrap(); + let (server_kept, _) = listener.accept().unwrap(); + drop(AuthDeadline::start(&server_kept, Duration::from_millis(200)).unwrap()); + std::thread::sleep(Duration::from_millis(400)); + (&server_kept).write_all(b"x").unwrap(); + kept.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); + assert_eq!(std::io::Read::read(&mut &kept, &mut buf).unwrap(), 1, "a met deadline leaves the connection open"); + } + #[test] fn limiter_releases() { let l = ConnectionLimiter::default(); diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 8c0fba6..0713ee0 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -11,7 +11,15 @@ Agents drive MaterialCraft three ways, all dispatching the same engine commands: 3. **The app's control channel** (`materialcraft --control `): one JSON request per line, `{"id", "method", "params"}` → `{"id", "ok": true, "result"}` or `{"id", "ok": false, "error"}`. The first request must be `{"method":"auth","params":{"token":"…"}}` with the token from - `MATERIALCRAFT_CONTROL_TOKEN` or the file `/materialcraft-control-.token`. + `MATERIALCRAFT_CONTROL_TOKEN` (16 to 1024 printable ASCII characters) or the file + `/materialcraft-control-.token`, a fresh random 256-bit token written after the port + is bound. On Unix the file is created new with mode 0600 (owner only); if an existing file there + cannot be replaced, the control server stays off and `MATERIALCRAFT_CONTROL_TOKEN` is the + alternative. Clients (`materialcraft-cli mcp --bridge`) refuse a token file that is a symlink, + not a regular file, not owned by them, or accessible to group or others. A connection gets one + authentication attempt, within 10 s; a wrong token, any other first request or malformed JSON + closes it. At most 16 connections are served at once. Whoever holds the token has full + engine-command authority, including host file access. Set `MATERIALCRAFT_NO_PREFS=1` for scripted runs so the user's layout is left alone. ## Methods From 56aaea24ae05750f75f72e9d218a50da6b695a05 Mon Sep 17 00:00:00 2001 From: Rafael Mendes <57309097+RafaelMendes1407@users.noreply.github.com> Date: Thu, 5 Nov 2026 10:50:37 -0300 Subject: [PATCH 2/2] Use the bound control port and handle thread creation failures --- apps/materialcraft/src/control_server.rs | 39 ++++++++++++++++++++---- crates/automation/src/rpc.rs | 6 ++-- crates/automation/src/security.rs | 4 +-- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/apps/materialcraft/src/control_server.rs b/apps/materialcraft/src/control_server.rs index 0f841c4..c424f84 100644 --- a/apps/materialcraft/src/control_server.rs +++ b/apps/materialcraft/src/control_server.rs @@ -22,16 +22,27 @@ const IDLE_TIMEOUT: Duration = Duration::from_secs(600); /// Listen on `127.0.0.1:port` and return the requests for the UI thread to answer /// ([`ControlRequest::reply`]). Bind or token failures are printed and leave the channel empty. pub fn start(port: u16, ctx: egui::Context) -> Receiver { - let (tx, rx) = channel::(); let listener = match TcpListener::bind(("127.0.0.1", port)) { Ok(l) => l, Err(e) => { eprintln!("materialcraft: control server failed to bind 127.0.0.1:{port}: {e}"); + return channel().1; + } + }; + start_listener(listener, ctx) +} + +fn start_listener(listener: TcpListener, ctx: egui::Context) -> Receiver { + let (tx, rx) = channel::(); + let addr = match listener.local_addr() { + Ok(addr) => addr, + Err(e) => { + eprintln!("materialcraft: could not determine the control port: {e}"); return rx; } }; // After binding, so an app that cannot listen never replaces another server's token. - let token = match server_token(port) { + let token = match server_token(addr.port()) { Ok((token, Some(path))) => { eprintln!("materialcraft: control token written to {}", path.display()); token @@ -42,8 +53,10 @@ pub fn start(port: u16, ctx: egui::Context) -> Receiver { return rx; } }; - eprintln!("materialcraft: control server listening on 127.0.0.1:{port}"); - std::thread::spawn(move || accept(listener, &token, &tx, &ctx)); + eprintln!("materialcraft: control server listening on {addr}"); + if let Err(e) = std::thread::Builder::new().name("materialcraft-control".into()).spawn(move || accept(listener, &token, &tx, &ctx)) { + eprintln!("materialcraft: could not start the control server: {e}"); + } rx } @@ -57,10 +70,12 @@ fn accept(listener: TcpListener, token: &str, tx: &Sender, ctx: continue; }; let (tx, ctx, token) = (tx.clone(), ctx.clone(), token.to_string()); - std::thread::spawn(move || { + if let Err(e) = std::thread::Builder::new().name("materialcraft-control-client".into()).spawn(move || { let _permit = permit; serve(stream, &token, &tx, &ctx); - }); + }) { + log::warn!("could not start a control connection: {e}"); + } } } @@ -140,6 +155,18 @@ mod tests { const TOKEN: &str = "0123456789abcdef0123456789abcdef"; + #[test] + fn ephemeral_listener_uses_its_actual_port_for_the_token() { + let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap(); + let addr = listener.local_addr().unwrap(); + assert_ne!(addr.port(), 0); + let _requests = start_listener(listener, egui::Context::default()); + let token = materialcraft_automation::security::client_token(addr.port(), None).unwrap(); + let (mut w, mut r) = connect(addr); + let auth = json!({"method": "auth", "params": {"token": token}}).to_string(); + assert_eq!(call(&mut w, &mut r, &auth).unwrap()["ok"], true); + } + /// A server on a free port whose app side answers every request with its method name. fn server() -> std::net::SocketAddr { let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap(); diff --git a/crates/automation/src/rpc.rs b/crates/automation/src/rpc.rs index 0d4253d..1f04a4d 100644 --- a/crates/automation/src/rpc.rs +++ b/crates/automation/src/rpc.rs @@ -210,10 +210,12 @@ pub fn serve_listener(listener: TcpListener, h: Arc>, token: Str continue; }; let (h, token) = (h.clone(), token.clone()); - std::thread::spawn(move || { + if let Err(e) = std::thread::Builder::new().name("materialcraft-rpc-client".into()).spawn(move || { let _permit = permit; let _ = serve_connection(&h, stream, &token); - }); + }) { + eprintln!("materialcraft: could not start an RPC connection: {e}"); + } } Ok(()) } diff --git a/crates/automation/src/security.rs b/crates/automation/src/security.rs index 41bec54..33040fe 100644 --- a/crates/automation/src/security.rs +++ b/crates/automation/src/security.rs @@ -200,12 +200,12 @@ impl AuthDeadline { pub fn start(stream: &TcpStream, timeout: Duration) -> std::io::Result { let stream = stream.try_clone()?; let (tx, rx) = channel::<()>(); - std::thread::spawn(move || { + std::thread::Builder::new().name("materialcraft-auth-deadline".into()).spawn(move || { // Dropping the deadline disconnects the channel and ends this thread at once. if let Err(RecvTimeoutError::Timeout) = rx.recv_timeout(timeout) { let _ = stream.shutdown(Shutdown::Both); } - }); + })?; Ok(AuthDeadline { _cancel: tx }) } }