Skip to content
Draft
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
30 changes: 30 additions & 0 deletions libdd-crashtracker/src/receiver/entry_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::CrashtrackerConfiguration;
#[cfg(target_os = "linux")]
use crate::StacktraceCollection;
use anyhow::Context;
#[cfg(target_os = "linux")]
use std::os::fd::AsRawFd;
use std::time::Duration;
use tokio::{
io::{AsyncBufReadExt, BufReader},
Expand All @@ -30,10 +32,38 @@ pub async fn async_receiver_entry_point_unix_listener(
listener: &UnixListener,
) -> anyhow::Result<()> {
let (unix_stream, _) = listener.accept().await?;
#[cfg(target_os = "linux")]
ensure_same_user(&unix_stream)?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add tests verifying the new enforcement

let stream = BufReader::new(unix_stream);
receiver_entry_point(receiver_timeout(), stream).await
}

#[cfg(target_os = "linux")]
fn ensure_same_user(unix_stream: &tokio::net::UnixStream) -> anyhow::Result<()> {
let mut ucred = libc::ucred {
pid: 0,
uid: 0,
gid: 0,
};
let mut ucred_len = std::mem::size_of::<libc::ucred>() as libc::socklen_t;
let getsockopt_res = unsafe {
libc::getsockopt(
unix_stream.as_raw_fd(),
libc::SOL_SOCKET,
libc::SO_PEERCRED,
&mut ucred as *mut libc::ucred as *mut libc::c_void,
&mut ucred_len,
)
};

anyhow::ensure!(getsockopt_res == 0, "Failed to get unix peer credentials");
anyhow::ensure!(
ucred.uid == unsafe { libc::geteuid() },
"Refusing crash report from another user"
);
Ok(())
}

pub async fn async_receiver_entry_point_unix_socket(
socket_path: impl AsRef<str>,
one_shot: bool,
Expand Down
Loading