Skip to content

Commit

Permalink
Fix a possible overflow due to use of fionread in poll_oneoff on Unix. (
Browse files Browse the repository at this point in the history
#881)

Closes #578.
  • Loading branch information
marmistrz committed Feb 26, 2020
1 parent 5bed476 commit 150a3e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
17 changes: 16 additions & 1 deletion crates/wasi-common/src/sys/unix/hostcalls_impl/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,24 @@ fn poll_oneoff_handle_fd_event<'a>(
ready_events: impl Iterator<Item = (FdEventData<'a>, yanix::poll::PollFd)>,
events: &mut Vec<wasi::__wasi_event_t>,
) -> Result<()> {
use crate::fdentry::Descriptor;
use std::{convert::TryInto, os::unix::prelude::AsRawFd};
use yanix::{file::fionread, poll::PollFlags};

fn query_nbytes(fd: &Descriptor) -> Result<u64> {
// fionread may overflow for large files, so use another way for regular files.
if let Descriptor::OsHandle(os_handle) = fd {
let meta = os_handle.metadata()?;
if meta.file_type().is_file() {
use yanix::file::tell;
let len = meta.len();
let host_offset = unsafe { tell(os_handle.as_raw_fd())? };
return Ok(len - host_offset);
}
}
unsafe { Ok(fionread(fd.as_raw_fd())?.into()) }
}

for (fd_event, poll_fd) in ready_events {
log::debug!("poll_oneoff_handle_fd_event fd_event = {:?}", fd_event);
log::debug!("poll_oneoff_handle_fd_event poll_fd = {:?}", poll_fd);
Expand All @@ -139,7 +154,7 @@ fn poll_oneoff_handle_fd_event<'a>(
log::debug!("poll_oneoff_handle_fd_event revents = {:?}", revents);

let nbytes = if fd_event.r#type == wasi::__WASI_EVENTTYPE_FD_READ {
unsafe { fionread(fd_event.descriptor.as_raw_fd())? }
query_nbytes(fd_event.descriptor)?
} else {
0
};
Expand Down
9 changes: 8 additions & 1 deletion crates/wasi-common/yanix/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,15 @@ pub unsafe fn fstat(fd: RawFd) -> Result<libc::stat> {
}

/// `fionread()` function, equivalent to `ioctl(fd, FIONREAD, *bytes)`.
pub unsafe fn fionread(fd: RawFd) -> Result<usize> {
pub unsafe fn fionread(fd: RawFd) -> Result<u32> {
let mut nread: libc::c_int = 0;
Errno::from_result(libc::ioctl(fd, libc::FIONREAD, &mut nread as *mut _))?;
Ok(nread.try_into()?)
}

/// This function is unsafe because it operates on a raw file descriptor.
/// It's provided, because std::io::Seek requires a mutable borrow.
pub unsafe fn tell(fd: RawFd) -> Result<u64> {
let offset: i64 = Errno::from_result(libc::lseek(fd, 0, libc::SEEK_CUR))?;
Ok(offset.try_into()?)
}

0 comments on commit 150a3e5

Please sign in to comment.