Skip to content

Commit 9a05de5

Browse files
committed
Workaround XNU bug in getifaddrs netmasks
1 parent 1eb589f commit 9a05de5

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/ifaddrs.rs

+58
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! of interfaces and their associated addresses.
55
66
use cfg_if::cfg_if;
7+
use std::convert::TryFrom;
78
use std::ffi;
89
use std::iter::Iterator;
910
use std::mem;
@@ -42,11 +43,46 @@ cfg_if! {
4243
}
4344
}
4445

46+
/// Workaround a bug in XNU where netmasks will always have the wrong size in
47+
/// the sa_len field due to the kernel ignoring trailing zeroes in the structure
48+
/// when setting the field. See https://github.com/nix-rust/nix/issues/1709#issuecomment-1199304470
49+
///
50+
/// To fix this, we stack-allocate a new sockaddr_storage, zero it out, and
51+
/// memcpy sa_len of the netmask to that new storage. Finally, we reset the
52+
/// ss_len field to sizeof(sockaddr_storage). This is supposedly valid as all
53+
/// members of the sockaddr_storage are "ok" with being zeroed out (there are
54+
/// no pointers).
55+
#[cfg(any(target_os = "ios", target_os = "macos"))]
56+
unsafe fn workaround_xnu_bug(info: &libc::ifaddrs) -> Option<SockaddrStorage> {
57+
let src_sock = info.ifa_netmask;
58+
if src_sock.is_null() {
59+
return None;
60+
}
61+
62+
let mut dst_sock = mem::MaybeUninit::<libc::sockaddr_storage>::zeroed();
63+
64+
// memcpy only sa_len bytes, assume the rest is zero
65+
std::ptr::copy_nonoverlapping(src_sock as *const u8, dst_sock.as_mut_ptr() as *mut u8, (*src_sock).sa_len.into());
66+
67+
// Initialize ss_len to sizeof(libc::sockaddr_storage).
68+
(*dst_sock.as_mut_ptr()).ss_len = u8::try_from(mem::size_of::<libc::sockaddr_storage>()).unwrap();
69+
let dst_sock = dst_sock.assume_init();
70+
71+
let dst_sock_ptr = &dst_sock as *const libc::sockaddr_storage as *const libc::sockaddr;
72+
73+
SockaddrStorage::from_raw(dst_sock_ptr, None)
74+
}
75+
4576
impl InterfaceAddress {
4677
/// Create an `InterfaceAddress` from the libc struct.
4778
fn from_libc_ifaddrs(info: &libc::ifaddrs) -> InterfaceAddress {
4879
let ifname = unsafe { ffi::CStr::from_ptr(info.ifa_name) };
4980
let address = unsafe { SockaddrStorage::from_raw(info.ifa_addr, None) };
81+
#[cfg(any(target_os = "ios", target_os = "macos"))]
82+
let netmask = unsafe {
83+
workaround_xnu_bug(info)
84+
};
85+
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
5086
let netmask = unsafe { SockaddrStorage::from_raw(info.ifa_netmask, None) };
5187
let mut addr = InterfaceAddress {
5288
interface_name: ifname.to_string_lossy().to_string(),
@@ -144,4 +180,26 @@ mod tests {
144180
fn test_getifaddrs() {
145181
let _ = getifaddrs();
146182
}
183+
184+
// Ensures getting the netmask works, and in particular that
185+
// `workaround_xnu_bug` works properly.
186+
#[test]
187+
fn test_getifaddrs_netmask_correct() {
188+
let addrs = getifaddrs().unwrap();
189+
for iface in addrs {
190+
let sock = if let Some(sock) = iface.netmask {
191+
sock
192+
} else {
193+
continue
194+
};
195+
if sock.family() == Some(crate::sys::socket::AddressFamily::Inet) {
196+
let _ = sock.as_sockaddr_in().unwrap();
197+
return;
198+
} else if sock.family() == Some(crate::sys::socket::AddressFamily::Inet6) {
199+
let _ = sock.as_sockaddr_in6().unwrap();
200+
return;
201+
}
202+
}
203+
panic!("No address?");
204+
}
147205
}

0 commit comments

Comments
 (0)