Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions crates/nono/src/supervisor/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ impl SupervisorSocket {
NonoError::SandboxInit(format!("Failed to accept supervisor connection: {e}"))
})?;

check_peer_uid(&stream, "supervisor")?;

Ok(SupervisorSocket {
stream,
socket_path: Some(path.to_path_buf()),
Expand Down Expand Up @@ -472,6 +474,23 @@ pub fn peer_credentials(sock_fd: RawFd) -> Result<PeerCredentials> {
}
}

/// Reject a connection whose peer UID does not match the current process's UID.
///
/// `label` identifies the connection kind (e.g. "supervisor", "URL open") in the
/// error message.
fn check_peer_uid(stream: &UnixStream, label: &str) -> Result<()> {
let peer = peer_credentials(stream.as_raw_fd())?;
// SAFETY: getuid() is always safe to call.
let our_uid = unsafe { libc::getuid() };
if peer.uid != our_uid {
return Err(NonoError::SandboxInit(format!(
"Rejected {label} connection from uid {} (expected {})",
peer.uid, our_uid
)));
}
Ok(())
}

#[doc(hidden)]
#[cfg(target_os = "linux")]
pub fn peer_in_same_user_namespace(peer_pid: u32) -> Result<bool> {
Expand Down Expand Up @@ -608,15 +627,7 @@ impl SupervisorListener {
))
})?;

let peer = peer_credentials(stream.as_raw_fd())?;
// SAFETY: getuid() is always safe to call.
let our_uid = unsafe { libc::getuid() };
if peer.uid != our_uid {
return Err(NonoError::SandboxInit(format!(
"Rejected URL open connection from uid {} (expected {})",
peer.uid, our_uid
)));
}
check_peer_uid(&stream, "URL open")?;

stream
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
Expand Down