Skip to content

Commit

Permalink
Fix underflow in socketAddr::address
Browse files Browse the repository at this point in the history
With a regression test for issue #1403.
  • Loading branch information
Thomasdezeeuw committed Dec 1, 2020
1 parent 6fad879 commit 6d3fa69
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/sys/unix/uds/socketaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ enum AddressKind<'a> {
impl SocketAddr {
fn address(&self) -> AddressKind<'_> {
let offset = path_offset(&self.sockaddr);
// Don't underflow in `len` below.
if (self.socklen as usize) < offset {
return AddressKind::Unnamed;
}
let len = self.socklen as usize - offset;
let path = unsafe { &*(&self.sockaddr.sun_path as *const [libc::c_char] as *const [u8]) };

Expand Down
26 changes: 23 additions & 3 deletions tests/regressions.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#![cfg(all(feature = "os-poll", feature = "net"))]

use mio::net::{TcpListener, TcpStream};
use mio::{Events, Interest, Poll, Token, Waker};
use std::io::{self, Read};
use std::sync::Arc;
use std::time::Duration;
use std::{net, thread};

use mio::net::{TcpListener, TcpStream};
use mio::{Events, Interest, Poll, Token, Waker};

mod util;
use util::{any_local_address, init, init_with_poll};
use util::{any_local_address, init, init_with_poll, temp_file};

const ID1: Token = Token(1);
const WAKE_TOKEN: Token = Token(10);
Expand Down Expand Up @@ -103,3 +104,22 @@ fn issue_1205() {
assert_eq!(waker_event.token(), WAKE_TOKEN);
handle.join().unwrap();
}

#[test]
#[cfg(unix)]
fn issue_1403() {
use mio::net::UnixDatagram;

init();

let path = temp_file("issue_1403");
let datagram1 = UnixDatagram::bind(&path).unwrap();
let datagram2 = UnixDatagram::unbound().unwrap();

let mut buf = [1u8; 1024];
let n = datagram2.send_to(&buf, &path).unwrap();

let (got, addr) = datagram1.recv_from(&mut buf).unwrap();
assert_eq!(got, n);
assert_eq!(addr.as_pathname(), None);
}

0 comments on commit 6d3fa69

Please sign in to comment.