Skip to content

Commit

Permalink
Fix Selector::try_clone
Browse files Browse the repository at this point in the history
Calls fcntl F_DUPFD_CLOEXEC expects two arguments; the command
(F_DUPFD_CLOEXEC) and an argument for the command. In this case an lower
bound for the resulting file descriptor. Because we didn't provide a
value it would take whatever value was left in the register from
whatever code used it before the system call.

This caused Waker::new to fail, see issue
#1497.
  • Loading branch information
Thomasdezeeuw committed Jun 12, 2021
1 parent 7adfb75 commit 2883f5c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/sys/unix/selector/epoll.rs
Expand Up @@ -41,7 +41,7 @@ impl Selector {
}

pub fn try_clone(&self) -> io::Result<Selector> {
syscall!(fcntl(self.ep, libc::F_DUPFD_CLOEXEC)).map(|ep| Selector {
syscall!(fcntl(self.ep, libc::F_DUPFD_CLOEXEC, super::LOWEST_FD)).map(|ep| Selector {
// It's the same selector, so we use the same id.
#[cfg(debug_assertions)]
id: self.id,
Expand Down
2 changes: 1 addition & 1 deletion src/sys/unix/selector/kqueue.rs
Expand Up @@ -87,7 +87,7 @@ impl Selector {
}

pub fn try_clone(&self) -> io::Result<Selector> {
syscall!(fcntl(self.kq, libc::F_DUPFD_CLOEXEC)).map(|kq| Selector {
syscall!(fcntl(self.kq, libc::F_DUPFD_CLOEXEC, super::LOWEST_FD)).map(|kq| Selector {
// It's the same selector, so we use the same id.
#[cfg(debug_assertions)]
id: self.id,
Expand Down
10 changes: 10 additions & 0 deletions src/sys/unix/selector/mod.rs
Expand Up @@ -33,3 +33,13 @@ mod kqueue;
target_os = "openbsd"
))]
pub(crate) use self::kqueue::{event, Event, Events, Selector};

/// Lowest file descriptor used in `Selector::try_clone`.
///
/// # Notes
///
/// Usually fds 0, 1 and 2 are standard in, out and error. Some application
/// blindly assume this to be true, which means using any one of those a select
/// could result in some interesting and unexpected errors. Avoid that by using
/// an fd that doesn't have a pre-determined usage.
const LOWEST_FD: libc::c_int = 3;

0 comments on commit 2883f5c

Please sign in to comment.