Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚑️ Fix a few recent regressions #757

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
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
25 changes: 10 additions & 15 deletions zbus/src/connection/handshake/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,16 @@ impl Client {
#[instrument(skip(self))]
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
async fn send_zero_byte(&mut self) -> Result<()> {
let written = self
.common
.socket_mut()
.write_mut()
.send_zero_byte()
.await
.map_err(|e| {
Error::Handshake(format!("Could not send zero byte with credentials: {}", e))
})
.and_then(|n| match n {
None => Err(Error::Handshake(
"Could not send zero byte with credentials".to_string(),
)),
Some(n) => Ok(n),
})?;
let write = self.common.socket_mut().write_mut();

let written = match write.send_zero_byte().await.map_err(|e| {
Error::Handshake(format!("Could not send zero byte with credentials: {}", e))
})? {
// This likely means that the socket type is unable to send SCM_CREDS.
// Let's try to send the 0 byte as a regular message.
None => write.sendmsg(&[0], &[]).await?,
Some(n) => n,
};

if written != 1 {
return Err(Error::Handshake(
Expand Down
16 changes: 6 additions & 10 deletions zbus/src/connection/handshake/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,13 @@ impl Common {
if self.first_command {
// The first command is sent by the client so we can assume it's the server.
self.first_command = false;
// leading 0 is sent separately for `freebsd` and `dragonfly`.
#[cfg(not(any(target_os = "freebsd", target_os = "dragonfly")))]
{
if self.recv_buffer[0] != b'\0' {
return Err(Error::Handshake(
"First client byte is not NUL!".to_string(),
));
}

start_index = 1;
if self.recv_buffer[0] != b'\0' {
return Err(Error::Handshake(
"First client byte is not NUL!".to_string(),
));
}

start_index = 1;
};

let line_bytes = self.recv_buffer.drain(..=lf_index);
Expand Down
43 changes: 18 additions & 25 deletions zbus/src/connection/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,25 @@ impl ReadHalf for Arc<Async<TcpStream>> {
}
}

#[cfg(windows)]
async fn peer_credentials(&mut self) -> io::Result<crate::fdo::ConnectionCredentials> {
#[cfg(windows)]
let creds = {
let stream = self.clone();
crate::Task::spawn_blocking(
move || {
use crate::win32::{tcp_stream_get_peer_pid, ProcessToken};

let pid = tcp_stream_get_peer_pid(stream.get_ref())? as _;
let sid = ProcessToken::open(if pid != 0 { Some(pid as _) } else { None })
.and_then(|process_token| process_token.sid())?;
io::Result::Ok(
crate::fdo::ConnectionCredentials::default()
.set_process_id(pid)
.set_windows_sid(sid),
)
},
"peer credentials",
)
.await
}?;

#[cfg(not(windows))]
let creds = crate::fdo::ConnectionCredentials::default();

Ok(creds)
let stream = self.clone();
crate::Task::spawn_blocking(
move || {
use crate::win32::{tcp_stream_get_peer_pid, ProcessToken};

let pid = tcp_stream_get_peer_pid(stream.get_ref())? as _;
let sid = ProcessToken::open(if pid != 0 { Some(pid as _) } else { None })
.and_then(|process_token| process_token.sid())?;
io::Result::Ok(
crate::fdo::ConnectionCredentials::default()
.set_process_id(pid)
.set_windows_sid(sid),
)
},
"peer credentials",
)
.await
}
}

Expand Down