Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview: 0.24.3 #1889

Merged
merged 16 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ build: &BUILD
- $TOOL +$TOOLCHAIN $BUILD $ZFLAGS --target $TARGET --all-targets
- $TOOL +$TOOLCHAIN doc $ZFLAGS --no-deps --target $TARGET
- $TOOL +$TOOLCHAIN clippy $ZFLAGS --target $TARGET -- -D warnings
- if [ -z "$NOHACK" ]; then $TOOL +$TOOLCHAIN install cargo-hack; fi
- if [ -z "$NOHACK" ]; then $TOOL +$TOOLCHAIN install --version 0.5.14 cargo-hack; fi
- if [ -z "$NOHACK" ]; then $TOOL +$TOOLCHAIN hack $ZFLAGS check --target $TARGET --each-feature; fi

# Tests that do require executing the binaries
Expand Down
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased] - ReleaseDate
### Added

- Added `SockaddrStorage::{as_unix_addr, as_unix_addr_mut}`
([#1871](https://github.com/nix-rust/nix/pull/1871))

### Changed

### Fixed

- Workaround XNU bug causing netmasks returned by `getifaddrs` to misbehave.
([#1788](https://github.com/nix-rust/nix/pull/1788))
- Fix microsecond calculation for `TimeSpec`.
([#1801](https://github.com/nix-rust/nix/pull/1801))
- Fix `User::from_name` and `Group::from_name` panicking
when given a name containing a nul.
([#1815](https://github.com/nix-rust/nix/pull/1815))
- Fix `User::from_uid` and `User::from_name` crash on Android platform.
([#1824](https://github.com/nix-rust/nix/pull/1824))
- Fixed using `SockaddrStorage` to store a Unix-domain socket address on Linux.
([#1871](https://github.com/nix-rust/nix/pull/1871))
- Fix UB with `sys::socket::sockopt::SockType` using `SOCK_PACKET`.
([#1821](https://github.com/nix-rust/nix/pull/1821))

### Removed

## [0.24.2] - 2022-07-17
### Added
### Changed
Expand Down
2 changes: 2 additions & 0 deletions src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ pub enum Type {
impl Entry {
/// Returns the inode number (`d_ino`) of the underlying `dirent`.
#[allow(clippy::useless_conversion)] // Not useless on all OSes
// The cast is not unnecessary on all platforms.
#[allow(clippy::unnecessary_cast)]
pub fn ino(&self) -> u64 {
cfg_if! {
if #[cfg(any(target_os = "android",
Expand Down
4 changes: 1 addition & 3 deletions src/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ fn clear() {

/// Returns the platform-specific value of errno
pub fn errno() -> i32 {
unsafe {
(*errno_location()) as i32
}
unsafe { *errno_location() }
}

impl Errno {
Expand Down
4 changes: 1 addition & 3 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,9 +816,7 @@ pub fn fspacectl_all(fd: RawFd, offset: libc::off_t, len: libc::off_t)
0, // No flags are currently supported
&mut rqsr
)};
if let Err(e) = Errno::result(res) {
return Err(e);
}
Errno::result(res)?;
}
Ok(())
}
Expand Down
68 changes: 67 additions & 1 deletion src/ifaddrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//! of interfaces and their associated addresses.

use cfg_if::cfg_if;
#[cfg(any(target_os = "ios", target_os = "macos"))]
use std::convert::TryFrom;
use std::ffi;
use std::iter::Iterator;
use std::mem;
Expand Down Expand Up @@ -42,12 +44,52 @@ cfg_if! {
}
}

/// Workaround a bug in XNU where netmasks will always have the wrong size in
/// the sa_len field due to the kernel ignoring trailing zeroes in the structure
/// when setting the field. See https://github.com/nix-rust/nix/issues/1709#issuecomment-1199304470
///
/// To fix this, we stack-allocate a new sockaddr_storage, zero it out, and
/// memcpy sa_len of the netmask to that new storage. Finally, we reset the
/// ss_len field to sizeof(sockaddr_storage). This is supposedly valid as all
/// members of the sockaddr_storage are "ok" with being zeroed out (there are
/// no pointers).
#[cfg(any(target_os = "ios", target_os = "macos"))]
unsafe fn workaround_xnu_bug(info: &libc::ifaddrs) -> Option<SockaddrStorage> {
let src_sock = info.ifa_netmask;
if src_sock.is_null() {
return None;
}

let mut dst_sock = mem::MaybeUninit::<libc::sockaddr_storage>::zeroed();

// memcpy only sa_len bytes, assume the rest is zero
std::ptr::copy_nonoverlapping(
src_sock as *const u8,
dst_sock.as_mut_ptr() as *mut u8,
(*src_sock).sa_len.into(),
);

// Initialize ss_len to sizeof(libc::sockaddr_storage).
(*dst_sock.as_mut_ptr()).ss_len =
u8::try_from(mem::size_of::<libc::sockaddr_storage>()).unwrap();
let dst_sock = dst_sock.assume_init();

let dst_sock_ptr =
&dst_sock as *const libc::sockaddr_storage as *const libc::sockaddr;

SockaddrStorage::from_raw(dst_sock_ptr, None)
}

impl InterfaceAddress {
/// Create an `InterfaceAddress` from the libc struct.
fn from_libc_ifaddrs(info: &libc::ifaddrs) -> InterfaceAddress {
let ifname = unsafe { ffi::CStr::from_ptr(info.ifa_name) };
let address = unsafe { SockaddrStorage::from_raw(info.ifa_addr, None) };
let netmask = unsafe { SockaddrStorage::from_raw(info.ifa_netmask, None) };
#[cfg(any(target_os = "ios", target_os = "macos"))]
let netmask = unsafe { workaround_xnu_bug(info) };
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
let netmask =
unsafe { SockaddrStorage::from_raw(info.ifa_netmask, None) };
let mut addr = InterfaceAddress {
interface_name: ifname.to_string_lossy().to_string(),
flags: InterfaceFlags::from_bits_truncate(info.ifa_flags as i32),
Expand Down Expand Up @@ -144,4 +186,28 @@ mod tests {
fn test_getifaddrs() {
let _ = getifaddrs();
}

// Ensures getting the netmask works, and in particular that
// `workaround_xnu_bug` works properly.
#[test]
fn test_getifaddrs_netmask_correct() {
let addrs = getifaddrs().unwrap();
for iface in addrs {
let sock = if let Some(sock) = iface.netmask {
sock
} else {
continue;
};
if sock.family() == Some(crate::sys::socket::AddressFamily::Inet) {
let _ = sock.as_sockaddr_in().unwrap();
return;
} else if sock.family()
== Some(crate::sys::socket::AddressFamily::Inet6)
{
let _ = sock.as_sockaddr_in6().unwrap();
return;
}
}
panic!("No address?");
}
}
1 change: 1 addition & 0 deletions src/sys/pthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ feature! {
/// won't send any signal.
///
/// [`pthread_kill(3)`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_kill.html
#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[cfg(not(target_os = "redox"))]
pub fn pthread_kill<T>(thread: Pthread, signal: T) -> Result<()>
where T: Into<Option<crate::sys::signal::Signal>>
Expand Down
Loading