From ee2e1deff805692c4a4cc9172b458b8a5edca44f Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Wed, 26 Apr 2023 12:13:29 +0100 Subject: [PATCH 1/9] Use .bits() method rather than field for bitflags. --- src/mount/bsd.rs | 4 ++-- src/mount/linux.rs | 4 ++-- src/mqueue.rs | 2 +- src/sys/stat.rs | 4 ++-- src/unistd.rs | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/mount/bsd.rs b/src/mount/bsd.rs index d124f1f9ab..dbff654112 100644 --- a/src/mount/bsd.rs +++ b/src/mount/bsd.rs @@ -392,7 +392,7 @@ impl<'a> Nmount<'a> { let niov = self.iov.len() as c_uint; let iovp = self.iov.as_mut_ptr() as *mut libc::iovec; - let res = unsafe { libc::nmount(iovp, niov, flags.bits) }; + let res = unsafe { libc::nmount(iovp, niov, flags.bits()) }; match Errno::result(res) { Ok(_) => Ok(()), Err(error) => { @@ -446,7 +446,7 @@ where P: ?Sized + NixPath, { let res = mountpoint.with_nix_path(|cstr| unsafe { - libc::unmount(cstr.as_ptr(), flags.bits) + libc::unmount(cstr.as_ptr(), flags.bits()) })?; Errno::result(res).map(drop) diff --git a/src/mount/linux.rs b/src/mount/linux.rs index 5f19a5f1f7..e987603786 100644 --- a/src/mount/linux.rs +++ b/src/mount/linux.rs @@ -132,7 +132,7 @@ pub fn mount< s, t.as_ptr(), ty, - flags.bits, + flags.bits(), d as *const libc::c_void, ) }) @@ -156,7 +156,7 @@ pub fn umount(target: &P) -> Result<()> { /// See also [`umount`](https://man7.org/linux/man-pages/man2/umount.2.html) pub fn umount2(target: &P, flags: MntFlags) -> Result<()> { let res = target.with_nix_path(|cstr| unsafe { - libc::umount2(cstr.as_ptr(), flags.bits) + libc::umount2(cstr.as_ptr(), flags.bits()) })?; Errno::result(res).map(drop) diff --git a/src/mqueue.rs b/src/mqueue.rs index ac183eb55a..7ce7d5e8bc 100644 --- a/src/mqueue.rs +++ b/src/mqueue.rs @@ -139,7 +139,7 @@ impl MqAttr { /// Open a message queue /// /// See also [`mq_open(2)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_open.html) -// The mode.bits cast is only lossless on some OSes +// The mode.bits() cast is only lossless on some OSes #[allow(clippy::cast_lossless)] pub fn mq_open( name: &CStr, diff --git a/src/sys/stat.rs b/src/sys/stat.rs index 78203bfbe3..7e51c03a3f 100644 --- a/src/sys/stat.rs +++ b/src/sys/stat.rs @@ -177,7 +177,7 @@ pub fn mknod( dev: dev_t, ) -> Result<()> { let res = path.with_nix_path(|cstr| unsafe { - libc::mknod(cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev) + libc::mknod(cstr.as_ptr(), kind.bits() | perm.bits() as mode_t, dev) })?; Errno::result(res).map(drop) @@ -202,7 +202,7 @@ pub fn mknodat( libc::mknodat( dirfd, cstr.as_ptr(), - kind.bits | perm.bits() as mode_t, + kind.bits() | perm.bits() as mode_t, dev, ) })?; diff --git a/src/unistd.rs b/src/unistd.rs index 7230c3370f..afa80e9b9d 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -3375,7 +3375,7 @@ feature! { /// See [access(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html) pub fn access(path: &P, amode: AccessFlags) -> Result<()> { let res = path.with_nix_path(|cstr| unsafe { - libc::access(cstr.as_ptr(), amode.bits) + libc::access(cstr.as_ptr(), amode.bits()) })?; Errno::result(res).map(drop) } @@ -3422,7 +3422,7 @@ pub fn faccessat( ))] pub fn eaccess(path: &P, mode: AccessFlags) -> Result<()> { let res = path.with_nix_path(|cstr| unsafe { - libc::eaccess(cstr.as_ptr(), mode.bits) + libc::eaccess(cstr.as_ptr(), mode.bits()) })?; Errno::result(res).map(drop) } From 2e5bc8b2038b6847baad1cf11b0697b5bc67a028 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Wed, 26 Apr 2023 14:04:44 +0100 Subject: [PATCH 2/9] Update to bitflags 2.3.1. This is a new major version and requires some code changes. --- Cargo.toml | 2 +- src/macros.rs | 1 + src/sys/termios.rs | 2 +- src/sys/time.rs | 2 ++ 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ee3882acfc..243f309d58 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ targets = [ [dependencies] libc = { version = "0.2.141", features = ["extra_traits"] } -bitflags = "1.1" +bitflags = "2.3.1" cfg-if = "1.0" pin-utils = { version = "0.1.0", optional = true } static_assertions = "1" diff --git a/src/macros.rs b/src/macros.rs index 5d83a5ac3c..2d3564416a 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -63,6 +63,7 @@ macro_rules! libc_bitflags { } ) => { ::bitflags::bitflags! { + #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] $(#[$outer])* pub struct $BitFlags: $T { $( diff --git a/src/sys/termios.rs b/src/sys/termios.rs index b0286f51f1..0f1c995090 100644 --- a/src/sys/termios.rs +++ b/src/sys/termios.rs @@ -309,7 +309,7 @@ impl Termios { let termios = *self.inner.borrow_mut(); self.input_flags = InputFlags::from_bits_truncate(termios.c_iflag); self.output_flags = OutputFlags::from_bits_truncate(termios.c_oflag); - self.control_flags = ControlFlags::from_bits_truncate(termios.c_cflag); + self.control_flags = ControlFlags::from_bits_retain(termios.c_cflag); self.local_flags = LocalFlags::from_bits_truncate(termios.c_lflag); self.control_chars = termios.c_cc; #[cfg(any( diff --git a/src/sys/time.rs b/src/sys/time.rs index 30ee5fd1b5..a0160e21ff 100644 --- a/src/sys/time.rs +++ b/src/sys/time.rs @@ -91,6 +91,7 @@ pub(crate) mod timer { #[cfg(any(target_os = "android", target_os = "linux"))] bitflags! { /// Flags that are used for arming the timer. + #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct TimerSetTimeFlags: libc::c_int { const TFD_TIMER_ABSTIME = libc::TFD_TIMER_ABSTIME; const TFD_TIMER_CANCEL_ON_SET = libc::TFD_TIMER_CANCEL_ON_SET; @@ -104,6 +105,7 @@ pub(crate) mod timer { ))] bitflags! { /// Flags that are used for arming the timer. + #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct TimerSetTimeFlags: libc::c_int { const TFD_TIMER_ABSTIME = libc::TIMER_ABSTIME; } From 1a89311dd5dde148d70709ce49913a283f46b39f Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Wed, 26 Apr 2023 14:31:55 +0100 Subject: [PATCH 3/9] Use repr(transparent) for bitflags. --- src/macros.rs | 1 + src/sys/statvfs.rs | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/macros.rs b/src/macros.rs index 2d3564416a..adff2bc6be 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -64,6 +64,7 @@ macro_rules! libc_bitflags { ) => { ::bitflags::bitflags! { #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] + #[repr(transparent)] $(#[$outer])* pub struct $BitFlags: $T { $( diff --git a/src/sys/statvfs.rs b/src/sys/statvfs.rs index c2c86624c8..35424e5e27 100644 --- a/src/sys/statvfs.rs +++ b/src/sys/statvfs.rs @@ -12,7 +12,6 @@ use crate::{errno::Errno, NixPath, Result}; #[cfg(not(target_os = "redox"))] libc_bitflags!( /// File system mount Flags - #[repr(C)] #[derive(Default)] pub struct FsFlags: c_ulong { /// Read Only From aef996a454467f188348447707e8e7598b6c4e98 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Mon, 22 May 2023 11:23:39 +0100 Subject: [PATCH 4/9] Fix warnings. --- src/sys/event.rs | 6 +++--- test/sys/test_socket.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/sys/event.rs b/src/sys/event.rs index 5dcf121ae2..ec7f7e277a 100644 --- a/src/sys/event.rs +++ b/src/sys/event.rs @@ -71,7 +71,7 @@ impl Kqueue { timeout as *const timespec } else { ptr::null() - } + }, ) }; Errno::result(res).map(|r| r as usize) @@ -86,7 +86,7 @@ impl Kqueue { target_os = "openbsd" ))] type type_of_udata = *mut libc::c_void; -#[cfg(any(target_os = "netbsd"))] +#[cfg(target_os = "netbsd")] type type_of_udata = intptr_t; #[cfg(target_os = "netbsd")] @@ -171,7 +171,7 @@ libc_enum! { ))] #[doc(hidden)] pub type type_of_event_flag = u16; -#[cfg(any(target_os = "netbsd"))] +#[cfg(target_os = "netbsd")] #[doc(hidden)] pub type type_of_event_flag = u32; libc_bitflags! { diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index 0a8d0544cb..9fb7e89a57 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -10,7 +10,7 @@ use std::path::Path; use std::slice; use std::str::FromStr; -#[cfg(any(target_os = "linux"))] +#[cfg(target_os = "linux")] #[cfg_attr(qemu, ignore)] #[test] pub fn test_timestamping() { @@ -2082,7 +2082,7 @@ pub fn test_vsock() { // Disable the test on emulated platforms because it fails in Cirrus-CI. Lack // of QEMU support is suspected. #[cfg_attr(qemu, ignore)] -#[cfg(all(target_os = "linux"))] +#[cfg(target_os = "linux")] #[test] fn test_recvmsg_timestampns() { use nix::sys::socket::*; @@ -2137,7 +2137,7 @@ fn test_recvmsg_timestampns() { // Disable the test on emulated platforms because it fails in Cirrus-CI. Lack // of QEMU support is suspected. #[cfg_attr(qemu, ignore)] -#[cfg(all(target_os = "linux"))] +#[cfg(target_os = "linux")] #[test] fn test_recvmmsg_timestampns() { use nix::sys::socket::*; From 3147e67b6797ff56bee5001ce61cf508eb8129ea Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 5 Jun 2023 16:59:13 -0600 Subject: [PATCH 5/9] Update CHANGELOG for patch release 0.26.2 [skip ci] --- CHANGELOG.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c87abc4b8c..22a968dcc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,9 +36,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ([#2012](https://github.com/nix-rust/nix/pull/2012)) ### Fixed -- Fix `SockaddrIn6` bug that was swapping flowinfo and scope_id byte ordering. - ([#1964](https://github.com/nix-rust/nix/pull/1964)) -- Fix: send ETH_P_ALL in htons format +- Fix: send `ETH_P_ALL` in htons format ([#1925](https://github.com/nix-rust/nix/pull/1925)) ### Removed @@ -51,6 +49,14 @@ This project adheres to [Semantic Versioning](https://semver.org/). `nix::sys::signalfd::SignalFd` instead. ([#1938](https://github.com/nix-rust/nix/pull/1938)) +## [0.26.2] - 2023-01-18 + +### Fixed + +- Fix `SockaddrIn6` bug that was swapping `flowinfo` and `scope_id` byte + ordering. + ([#1964](https://github.com/nix-rust/nix/pull/1964)) + ## [0.26.1] - 2022-11-29 ### Fixed - Fix UB with `sys::socket::sockopt::SockType` using `SOCK_PACKET`. From e63dd8fa5944e945557c1605b633345d07aeea27 Mon Sep 17 00:00:00 2001 From: Niels Sascha Reedijk Date: Sat, 17 Jun 2023 15:04:46 +0000 Subject: [PATCH 6/9] Haiku: `speed_t` is defined as `u8` for 32 and 64 bit systems --- src/sys/termios.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sys/termios.rs b/src/sys/termios.rs index b0286f51f1..694a1338d3 100644 --- a/src/sys/termios.rs +++ b/src/sys/termios.rs @@ -355,9 +355,9 @@ libc_enum! { /// enum. /// /// B0 is special and will disable the port. - #[cfg_attr(all(any(target_os = "haiku"), target_pointer_width = "64"), repr(u8))] + #[cfg_attr(target_os = "haiku", repr(u8))] #[cfg_attr(all(any(target_os = "ios", target_os = "macos"), target_pointer_width = "64"), repr(u64))] - #[cfg_attr(not(all(any(target_os = "ios", target_os = "macos", target_os = "haiku"), target_pointer_width = "64")), repr(u32))] + #[cfg_attr(all(not(all(any(target_os = "ios", target_os = "macos"), target_pointer_width = "64")), not(target_os = "haiku")), repr(u32))] #[non_exhaustive] pub enum BaudRate { B0, From fccc89f9f04a8ec6404defcd5a0882078ae75643 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Tue, 27 Jun 2023 17:50:31 -0600 Subject: [PATCH 7/9] Disable the doc test for sys::personality::personality on aarch64 It's failing in CI, and we don't yet know why. Possibly the cloud provider just turned on seccomp. Issue #2060 --- src/sys/personality.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/sys/personality.rs b/src/sys/personality.rs index f295a05fad..30231dd7b8 100644 --- a/src/sys/personality.rs +++ b/src/sys/personality.rs @@ -80,7 +80,10 @@ pub fn get() -> Result { /// /// Example: /// -/// ``` +// Disable test on aarch64 until we know why it fails. +// https://github.com/nix-rust/nix/issues/2060 +#[cfg_attr(target_arch = "aarch64", doc = " ```no_run")] +#[cfg_attr(not(target_arch = "aarch64"), doc = " ```")] /// # use nix::sys::personality::{self, Persona}; /// let mut pers = personality::get().unwrap(); /// assert!(!pers.contains(Persona::ADDR_NO_RANDOMIZE)); From 1546857f8c6a8ccd444eb656f4421003a376f5cd Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Tue, 27 Jun 2023 17:30:35 -0600 Subject: [PATCH 8/9] For invalid IP address conversions with future Rust versions Rust's standard library no longer guarantees that Ipv4Addr and Ipv6Addr are wrappers around the C types (though for now at least, they are identical on all platforms I'm aware of). So do the conversions explicitly instead of transmuting. Fixes #2053 --- CHANGELOG.md | 5 +++++ Cargo.toml | 1 - src/sys/socket/addr.rs | 16 ++++++---------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c87abc4b8c..7e450900e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,11 @@ This project adheres to [Semantic Versioning](https://semver.org/). ([#1964](https://github.com/nix-rust/nix/pull/1964)) - Fix: send ETH_P_ALL in htons format ([#1925](https://github.com/nix-rust/nix/pull/1925)) +- Fix potentially invalid conversions in + `SockaddrIn::from`, + `SockaddrIn6::from`, `IpMembershipRequest::new`, and + `Ipv6MembershipRequest::new` with future Rust versions. + ([#2061](https://github.com/nix-rust/nix/pull/2061)) ### Removed diff --git a/Cargo.toml b/Cargo.toml index ee3882acfc..996d1d30c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,6 @@ libc = { version = "0.2.141", features = ["extra_traits"] } bitflags = "1.1" cfg-if = "1.0" pin-utils = { version = "0.1.0", optional = true } -static_assertions = "1" memoffset = { version = "0.9", optional = true } [features] diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 4830974933..2b3a1ed439 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -39,18 +39,17 @@ use std::{fmt, mem, net, ptr, slice}; /// Convert a std::net::Ipv4Addr into the libc form. #[cfg(feature = "net")] pub(crate) const fn ipv4addr_to_libc(addr: net::Ipv4Addr) -> libc::in_addr { - static_assertions::assert_eq_size!(net::Ipv4Addr, libc::in_addr); - // Safe because both types have the same memory layout, and no fancy Drop - // impls. - unsafe { mem::transmute(addr) } + libc::in_addr { + s_addr: u32::from_ne_bytes(addr.octets()) + } } /// Convert a std::net::Ipv6Addr into the libc form. #[cfg(feature = "net")] pub(crate) const fn ipv6addr_to_libc(addr: &net::Ipv6Addr) -> libc::in6_addr { - static_assertions::assert_eq_size!(net::Ipv6Addr, libc::in6_addr); - // Safe because both are Newtype wrappers around the same libc type - unsafe { mem::transmute(*addr) } + libc::in6_addr { + s6_addr: addr.octets() + } } /// These constants specify the protocol family to be used @@ -949,9 +948,6 @@ impl SockaddrLike for () { } /// An IPv4 socket address -// This is identical to net::SocketAddrV4. But the standard library -// doesn't allow direct access to the libc fields, which we need. So we -// reimplement it here. #[cfg(feature = "net")] #[repr(transparent)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] From 5b2e4af76e7eb9318cc9fdb75a453e1970405d12 Mon Sep 17 00:00:00 2001 From: Jason Dusek Date: Sun, 2 Jul 2023 00:17:58 -0500 Subject: [PATCH 9/9] Address seeming typo in lib.rs I believe the `my` here was intended to be `many` -- but am not sure. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 6349d37e0f..8649a34d44 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ //! * `fs` - File system functionality //! * `hostname` - Get and set the system's hostname //! * `inotify` - Linux's `inotify` file system notification API -//! * `ioctl` - The `ioctl` syscall, and wrappers for my specific instances +//! * `ioctl` - The `ioctl` syscall, and wrappers for many specific instances //! * `kmod` - Load and unload kernel modules //! * `mman` - Stuff relating to memory management //! * `mount` - Mount and unmount file systems