Skip to content

Commit

Permalink
Add stack CMSG space to Stream recvmsg
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed Jul 23, 2023
1 parent a20217f commit 663a896
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions x11rb/src/rust_connection/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,18 @@ fn do_write(

let res = if !fds.is_empty() {
let fds = fds.iter().map(|fd| fd.as_fd()).collect::<Vec<_>>();

// Most sendmsg implementations put a limit of at least 0xFF file descriptors.
if fds.len() > 0xFF {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"Cannot send more than 0xFF FDs at once, tried to send {}",
fds.len()
),
));
}

let rights = SendAncillaryMessage::ScmRights(&fds);

let mut cmsg_space = vec![0u8; rights.size()];
Expand Down Expand Up @@ -396,14 +408,14 @@ impl Stream for DefaultStream {
fn read(&self, buf: &mut [u8], fd_storage: &mut Vec<RawFdContainer>) -> Result<usize> {
#[cfg(unix)]
{
use rustix::cmsg_space;
use rustix::io::Errno;
use rustix::net::{recvmsg, RecvAncillaryBuffer, RecvAncillaryMessage};

Check warning on line 412 in x11rb/src/rust_connection/stream.rs

View check run for this annotation

Codecov / codecov/patch

x11rb/src/rust_connection/stream.rs#L411-L412

Added lines #L411 - L412 were not covered by tests
use std::io::IoSliceMut;

// Chosen by checking what libxcb does
const MAX_FDS_RECEIVED: usize = 16;
let mut cmsg = vec![0u8; cmsg_space!(ScmRights(MAX_FDS_RECEIVED))];
// 1024 bytes on the stack should be enough for more file descriptors than the X server will ever
// send, as well as the header for the ancillary data. If you can find a case where this can
// overflow with an actual production X11 server, I'll buy you a steak dinner.
let mut cmsg = [0u8; 1024];

Check warning on line 418 in x11rb/src/rust_connection/stream.rs

View check run for this annotation

Codecov / codecov/patch

x11rb/src/rust_connection/stream.rs#L415-L418

Added lines #L415 - L418 were not covered by tests
let mut iov = [IoSliceMut::new(buf)];
let mut cmsg_buffer = RecvAncillaryBuffer::new(&mut cmsg);

Check warning on line 420 in x11rb/src/rust_connection/stream.rs

View check run for this annotation

Codecov / codecov/patch

x11rb/src/rust_connection/stream.rs#L420

Added line #L420 was not covered by tests

Expand Down

0 comments on commit 663a896

Please sign in to comment.