diff --git a/doc/bin/relay/config.md b/doc/bin/relay/config.md index 30ed2be99de..2235b4c874d 100644 --- a/doc/bin/relay/config.md +++ b/doc/bin/relay/config.md @@ -244,6 +244,10 @@ secret = "./iroh-secret.key" # Persist the key so the endpoint id surviv # disable_relay = true # Direct addresses only. Right on a LAN, wrong on the internet. ``` +The secret path must be a regular file, not a symlink. A missing file is created +exclusively, with mode `0600` on Unix. Existing Unix files readable by group or +others are refused; use `chmod 600` to make an existing key private before startup. + See [Transport](/concept/transport#iroh-peer-to-peer-experimental). ## Shutdown diff --git a/rs/moq-relay/src/uring.rs b/rs/moq-relay/src/uring.rs index 9e6766f4f69..74f2c439a40 100644 --- a/rs/moq-relay/src/uring.rs +++ b/rs/moq-relay/src/uring.rs @@ -659,6 +659,13 @@ async fn serve_connection( .await .context("moq handshake failed")?; + // WebTransport carries the path in its URL, so a SETUP path too is a protocol + // violation rather than a second path to authorize. + if url.is_some() && !request.path().is_empty() { + request.close(moq_net::Error::ProtocolViolation); + anyhow::bail!("WebTransport SETUP carried a path"); + } + // The path + `?jwt=` ride the URL for WebTransport and the SETUP for raw // QUIC; either way the grant comes from the shared runtime, which owns the // auth client. diff --git a/rs/moq-tokio/src/iroh.rs b/rs/moq-tokio/src/iroh.rs index 43cfb02c2c3..533a366c113 100644 --- a/rs/moq-tokio/src/iroh.rs +++ b/rs/moq-tokio/src/iroh.rs @@ -4,7 +4,7 @@ //! falling back to an iroh relay. Both WebTransport-over-H3 and raw QUIC are //! negotiated via ALPN. -use std::{net, path::PathBuf, str::FromStr, sync::Arc}; +use std::{net, path::Path, str::FromStr, sync::Arc}; use crate::quic::CongestionControl; use url::Url; @@ -150,6 +150,8 @@ pub struct Config { /// Secret key for the iroh endpoint, either a hex-encoded string or a path to a file. /// If the file does not exist, a random key will be generated and written to the path. + /// Files must be regular, with no symlinks; on Unix, new files use mode 0600 and + /// existing files readable by group or others are refused without changing permissions. #[usage( name = "iroh-secret", long = "iroh-secret", @@ -227,17 +229,9 @@ impl Config { let secret_key = if let Some(secret) = self.secret.as_ref().and_then(|s| SecretKey::from_str(s).ok()) { secret } else if let Some(path) = self.secret { - let path = PathBuf::from(path); - if !path.exists() { - // Generate a new random secret and write it to the file. - let secret = SecretKey::generate(); - tokio::fs::write(path, hex::encode(secret.to_bytes())).await?; - secret - } else { - // Otherwise, read the secret from a file. - let key_str = tokio::fs::read_to_string(&path).await?; - SecretKey::from_str(&key_str).map_err(|err| Error::Secret(crate::error::message(err)))? - } + let generated = hex::encode(SecretKey::generate().to_bytes()); + let key_str = load_or_create(Path::new(&path), &generated).await?; + SecretKey::from_str(&key_str).map_err(|err| Error::Secret(crate::error::message(err)))? } else { // Otherwise, generate a new random secret. SecretKey::generate() @@ -445,10 +439,139 @@ fn url_set_scheme(url: Url, scheme: &str) -> Result { Ok(url) } +/// Load a persisted secret, or exclusively create its private file. +async fn load_or_create(path: &Path, generated: &str) -> std::io::Result { + use std::io; + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + + let mut options = std::fs::OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.mode(0o600); + } + match tokio::fs::OpenOptions::from(options).open(path).await { + Ok(mut file) => { + file.write_all(generated.as_bytes()).await?; + file.flush().await?; + return Ok(generated.to_owned()); + } + Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {} + Err(err) => return Err(err), + } + + let mut options = std::fs::OpenOptions::new(); + options.read(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + // Validate the opened inode, without following a symlink or blocking on a FIFO. + options.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK); + } + #[cfg(windows)] + { + use std::os::windows::fs::OpenOptionsExt; + // FILE_FLAG_OPEN_REPARSE_POINT opens the link itself for validation. + options.custom_flags(0x00200000); + } + let mut file = tokio::fs::OpenOptions::from(options).open(path).await?; + let metadata = file.metadata().await?; + if !metadata.is_file() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "secret must be a regular file", + )); + } + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + if metadata.permissions().mode() & 0o044 != 0 { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + "secret must not be readable by group or others; use chmod 600", + )); + } + } + let mut contents = String::new(); + file.read_to_string(&mut contents).await?; + Ok(contents) +} + #[cfg(test)] mod tests { use super::*; + const KEY: &str = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"; + + #[tokio::test] + async fn persisted_secret_is_private_and_reused() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("secret.key"); + assert_eq!(load_or_create(&path, KEY).await.unwrap(), KEY); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let metadata = std::fs::metadata(&path).unwrap(); + assert_eq!(metadata.permissions().mode() & 0o177, 0); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o400)).unwrap(); + } + assert_eq!(load_or_create(&path, "replacement").await.unwrap(), KEY); + assert_eq!(std::fs::read_to_string(&path).unwrap(), KEY); + } + + #[cfg(unix)] + #[tokio::test] + async fn persisted_secret_refuses_shared_read_permissions() { + use std::os::unix::fs::PermissionsExt; + + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("secret.key"); + std::fs::write(&path, KEY).unwrap(); + for mode in [0o640, 0o604, 0o644] { + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(mode)).unwrap(); + let err = load_or_create(&path, "replacement").await.unwrap_err(); + assert_eq!(err.kind(), std::io::ErrorKind::PermissionDenied); + assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, mode); + assert_eq!(std::fs::read_to_string(&path).unwrap(), KEY); + } + } + + #[cfg(unix)] + #[tokio::test] + async fn persisted_secret_refuses_symlinks_without_touching_the_target() { + use std::os::unix::fs::symlink; + + let dir = tempfile::tempdir().unwrap(); + let target = dir.path().join("target.key"); + let link = dir.path().join("secret.key"); + symlink(&target, &link).unwrap(); + assert!(load_or_create(&link, KEY).await.is_err()); + assert!(!target.exists()); + std::fs::write(&target, KEY).unwrap(); + assert!(load_or_create(&link, "replacement").await.is_err()); + assert_eq!(std::fs::read_to_string(&target).unwrap(), KEY); + } + + #[tokio::test] + async fn persisted_secret_refuses_nonregular_files() { + let dir = tempfile::tempdir().unwrap(); + assert!(load_or_create(dir.path(), KEY).await.is_err()); + #[cfg(unix)] + { + let path = dir.path().join("socket"); + let _socket = std::os::unix::net::UnixListener::bind(&path).unwrap(); + assert!(load_or_create(&path, KEY).await.is_err()); + + use std::os::unix::ffi::OsStrExt; + let path = dir.path().join("fifo"); + let name = std::ffi::CString::new(path.as_os_str().as_bytes()).unwrap(); + // SAFETY: name is a valid, NUL-terminated path and mkfifo retains no pointer. + assert_eq!(unsafe { libc::mkfifo(name.as_ptr(), 0o600) }, 0); + assert!(load_or_create(&path, KEY).await.is_err()); + } + } + /// A public builder taking a config this crate did not check, so it refuses /// before it touches the key file or a socket. /// diff --git a/rs/moq-tokio/src/server.rs b/rs/moq-tokio/src/server.rs index 07e5977533c..e60d201c558 100644 --- a/rs/moq-tokio/src/server.rs +++ b/rs/moq-tokio/src/server.rs @@ -645,7 +645,7 @@ impl Server { // (like the stream bindings). let Accepted { session, url, identity, authority, mut link } = within(deadline, super::noq::accept(_conn, alpns)).await?; link.local = local; - let (request, deadline) = setup.accept(session, deadline).await?; + let (request, deadline) = setup.accept(session, deadline, url.is_some()).await?; Ok(Request { transport: Transport::Quic, url, identity, authority, link, kind: RequestKind::Noq(Box::new(request)), deadline }) }.boxed()); } @@ -657,7 +657,7 @@ impl Server { let deadline = setup.deadline(); self.accept.push(async move { let Accepted { session, url, identity, authority, link } = within(deadline, super::iroh::accept(_conn, alpns)).await?; - let (request, deadline) = setup.accept(session, deadline).await?; + let (request, deadline) = setup.accept(session, deadline, url.is_some()).await?; Ok(Request { transport: Transport::Iroh, url, identity, authority, link, kind: RequestKind::Iroh(Box::new(request)), deadline }) }.boxed()); } @@ -673,7 +673,7 @@ impl Server { let deadline = setup.deadline(); self.accept.push(async move { let (session, url, accepted) = within(deadline, _pending).await?; - let (request, deadline) = setup.accept(session, deadline).await?; + let (request, deadline) = setup.accept(session, deadline, true).await?; let authority = url.host_str().filter(|h| !h.is_empty()).map(str::to_owned); let link = Link { remote: Some(accepted.remote), local, alpn: accepted.protocol, ..Default::default() }; Ok(Request { transport: Transport::WebSocket, url: Some(url), authority, identity: None, link, kind: RequestKind::Qmux(Box::new(request)), deadline }) @@ -868,10 +868,15 @@ impl Setup { /// Read the peer's MoQ SETUP by `deadline`, closing the session with a timeout /// code if it hasn't arrived. The deadline comes back too, since answering the /// SETUP is part of the handshake it bounds. + /// + /// `uri` marks a binding whose request URI carries the path (WebTransport and + /// WebSocket). Its SETUP must not carry one too, so a SETUP path is refused + /// with a protocol violation rather than authorized in place of the URI. async fn accept( &self, session: S, deadline: Option, + uri: bool, ) -> crate::Result<(PendingRequest>, Option)> where S: web_transport_trait::Session, @@ -890,6 +895,12 @@ impl Setup { .await?) }; let request = Deadline::bound(deadline.as_ref(), accept).await?; + // The SETUP path is empty when the peer sent none, and an empty one never + // overrides the URI, so only a non-empty path conflicts with it. + if uri && !request.path().is_empty() { + request.close(moq_net::Error::ProtocolViolation); + return Err(moq_net::Error::ProtocolViolation.into()); + } Ok((request, deadline)) } } @@ -1264,7 +1275,7 @@ async fn stream_request( deadline: Option, tx: tokio::sync::mpsc::Sender, ) { - match setup.accept(session, deadline).await { + match setup.accept(session, deadline, false).await { Ok((request, deadline)) => { let request = Request { transport, @@ -1373,8 +1384,7 @@ impl std::fmt::Display for Transport { /// session, or [Self::reject] to reject it (which closes the just-established session). pub struct Request { transport: Transport, - /// The request URL, for transports that carry one (QUIC/WebTransport/WebSocket). `None` for the - /// URL-less stream bindings, whose request path rides the SETUP instead. + /// The request URL for WebTransport/WebSocket. `None` for bindings whose path rides SETUP. url: Option, /// The authority the client dialed, when it offered one: the TLS SNI on raw QUIC, the CONNECT /// authority on WebTransport. `None` on the URL-less stream bindings and on iroh. @@ -1505,10 +1515,9 @@ impl Request { self.transport } - /// Returns the request URL for transports that carry one (QUIC/WebTransport/WebSocket). + /// Returns the request URL for WebTransport/WebSocket. /// - /// `None` for the URL-less stream bindings (`tcp`/`unix`); use [`Self::path`] for their - /// in-band request path. + /// `None` for bindings without a request URI; use [`Self::path`] for their in-band path. pub fn url(&self) -> Option<&Url> { self.url.as_ref() } @@ -1527,18 +1536,14 @@ impl Request { /// The request path the client advertised, uniform across transports. /// - /// Taken from the SETUP for the URL-less stream bindings (and moq-transport, which - /// carries it in-band), or the request [`url`](Self::url) for - /// WebTransport/QUIC/WebSocket. + /// Taken from the request [`url`](Self::url) for WebTransport/WebSocket, or SETUP + /// for bindings without a request URI. /// The missing or root path is returned as an empty string. pub fn path(&self) -> &str { - // An empty SETUP path means the client advertised none, so fall back to the - // request URL. URL-carrying bindings are the ones that must not send a path at - // all, so this never discards a path the client meant us to use. - let setup = request_ref!(self, r => r.path()); - let path = if setup.is_empty() { - self.url.as_ref().map(Url::path).unwrap_or("") + let path = if let Some(url) = &self.url { + url.path() } else { + let setup = request_ref!(self, r => r.path()); setup.split_once('?').map_or(setup, |(path, _)| path) }; if path == "/" { "" } else { path } @@ -1548,11 +1553,12 @@ impl Request { /// /// Query values can contain credentials. Avoid logging this value. pub fn query(&self) -> Option<&str> { - let setup = request_ref!(self, r => r.path()); - if setup.is_empty() { - self.url.as_ref().and_then(Url::query) + if let Some(url) = &self.url { + url.query() } else { - setup.split_once('?').map(|(_, query)| query) + request_ref!(self, r => r.path()) + .split_once('?') + .map(|(_, query)| query) } } @@ -1768,6 +1774,85 @@ mod tests { ) } + /// A WebSocket request carries its path in the URL, so a SETUP path is refused + /// rather than authorized in its place. An empty one names nothing and is accepted. + #[cfg(feature = "websocket")] + #[tokio::test(start_paused = true)] + async fn websocket_refuses_setup_path() { + use qmux::ws::tokio_tungstenite::{WebSocketStream, tungstenite::protocol::Role}; + use web_transport_trait::{SendStream as _, Session as _}; + + let url: Url = "wss://example.com/room?jwt=url-token".parse().unwrap(); + for protocol in ["qmux-01.moq-lite-06", "qmux-01.moqt-18", "qmux-01.moqt-22"] { + for path in [ + None, + Some(""), + Some("/room?jwt=url-token"), + Some("/other?jwt=setup-token"), + ] { + let (client, server) = tokio::io::duplex(64 * 1024); + let client = WebSocketStream::from_raw_socket(client, Role::Client, None).await; + let server = WebSocketStream::from_raw_socket(server, Role::Server, None).await; + let client = qmux::ws::Upgraded::new(client).with_alpn(protocol).connect(); + let server = qmux::ws::Upgraded::new(server).with_alpn(protocol).accept(); + + // At most a single Path parameter: lite uses a counted parameter bag; + // IETF uses a u16-sized SETUP option block. + let lite = protocol.contains("moq-lite"); + let mut params = if lite { + vec![u8::from(path.is_some())] + } else { + Vec::new() + }; + if let Some(path) = path { + params.extend_from_slice(&[if lite { 2 } else { 1 }, path.len() as u8]); + params.extend_from_slice(path.as_bytes()); + } + let mut bytes = if lite { + vec![1, params.len() as u8] + } else { + vec![0xaf, 0, 0, params.len() as u8] + }; + bytes.extend(params); + let mut setup = client.open_uni().await.unwrap(); + setup.write_all(&bytes).await.unwrap(); + + let setup = Setup { + server: moq_net::Server::new(), + timeout: None, + }; + let result = setup.accept(server, None, true).await; + if path.is_some_and(|path| !path.is_empty()) { + let Err(err) = result else { + panic!("{protocol} accepted SETUP path {path:?}"); + }; + assert!( + matches!(err, Error::MoqNet(moq_net::Error::ProtocolViolation)), + "{protocol}, {path:?}: {err}" + ); + // qmux 0.5 reports the peer's close without its code here, so only + // check that the session closes. + tokio::time::timeout(std::time::Duration::from_secs(1), client.closed()) + .await + .unwrap_or_else(|_| panic!("{protocol}, {path:?}: session stayed open")); + } else { + let (request, deadline) = result.unwrap_or_else(|err| panic!("{protocol}, {path:?}: {err}")); + let request = Request { + transport: Transport::WebSocket, + url: Some(url.clone()), + authority: None, + identity: None, + link: Link::default(), + kind: RequestKind::Qmux(Box::new(request)), + deadline, + }; + assert_eq!(request.path(), "/room", "{protocol}, {path:?}"); + assert_eq!(request.query(), Some("jwt=url-token"), "{protocol}, {path:?}"); + } + } + } + } + /// A scripted peer, recording the code it was closed with. /// /// With `setup` it is a moq-transport peer whose bidi SETUP stream delivers those @@ -1933,7 +2018,7 @@ mod tests { timeout: Some(timeout), }; let start = tokio::time::Instant::now(); - let Err(err) = setup.accept(peer.clone(), setup.deadline()).await else { + let Err(err) = setup.accept(peer.clone(), setup.deadline(), false).await else { panic!("a peer that never sent SETUP was accepted"); }; assert!(matches!(err, Error::MoqNet(moq_net::Error::Timeout)), "{err}"); @@ -1974,7 +2059,7 @@ mod tests { timeout: Some(timeout), }; let start = tokio::time::Instant::now(); - let Ok((request, deadline)) = setup.accept(peer.clone(), setup.deadline()).await else { + let Ok((request, deadline)) = setup.accept(peer.clone(), setup.deadline(), false).await else { panic!("a readable SETUP was refused"); }; assert_eq!(start.elapsed(), std::time::Duration::ZERO); @@ -2016,7 +2101,7 @@ mod tests { session }); - let Ok((request, _)) = setup.accept(server, deadline).await else { + let Ok((request, _)) = setup.accept(server, deadline, false).await else { panic!("a prompt SETUP was refused"); }; let (_session, driver) = request.ok().await.expect("server handshake");