Skip to content
This repository has been archived by the owner on May 1, 2021. It is now read-only.

Invalid assumption of SocketAddrV{4,6} layout #1

Closed
Nemo157 opened this issue Dec 15, 2020 · 4 comments · Fixed by #5
Closed

Invalid assumption of SocketAddrV{4,6} layout #1

Nemo157 opened this issue Dec 15, 2020 · 4 comments · Fixed by #5

Comments

@Nemo157
Copy link

Nemo157 commented Dec 15, 2020

This code assumes that Rust's std::net::{SocketAddrV4, SocketAddrV6} are layout compatible with sockaddr:

nb-connect/src/lib.rs

Lines 61 to 84 in f706c11

impl Addr {
/// Creates a raw socket address from `SocketAddr`.
fn new(addr: SocketAddr) -> Self {
let (addr, len): (*const sockaddr, socklen_t) = match &addr {
SocketAddr::V4(addr) => (addr as *const _ as *const _, mem::size_of_val(addr) as _),
SocketAddr::V6(addr) => (addr as *const _ as *const _, mem::size_of_val(addr) as _),
};
unsafe { Self::from_raw_parts(addr, len) }
}
/// Creates an `Addr` from its raw parts.
unsafe fn from_raw_parts(addr: *const sockaddr, len: socklen_t) -> Self {
let mut storage = MaybeUninit::<sockaddr_storage>::uninit();
ptr::copy_nonoverlapping(
addr as *const _ as *const u8,
&mut storage as *mut _ as *mut u8,
len as usize,
);
Self {
storage: storage.assume_init(),
len,
}
}
}

This is not guaranteed by the standard library, see rust-lang/rust#78802 for more details.

@faern
Copy link

faern commented Feb 14, 2021

Awesome 🎉
Will there be a semver compatible patch release coming out? So people can get rid of the invalid memory assumption with a simple cargo update.

@taiki-e
Copy link
Collaborator

taiki-e commented Feb 14, 2021

Published in 1.0.3 & yanked old versions.

@faern
Copy link

faern commented Feb 14, 2021

Thanks!

@faern
Copy link

faern commented Feb 14, 2021

I submitted a security advisory on the older versions. rustsec/advisory-db#765

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants