See rust-lang/rust#72175 and nix-rust/nix#1421: it seems an external library is allowed to assume a file descriptor is private, for example:
#![allow(clippy::blacklisted_name)]#![deny(unsafe_code)]use socket2::SockRef;use std::mem;#[allow(unsafe_code)]mod external_library {use std::{mem, os::unix::io::RawFd, ptr};staticDATA:i32 = -1;pubstructFoo([RawFd;2]);implFoo{pubfnnew() -> Self{letmut sockets = [-1;2];assert_eq!(unsafe{
libc::socketpair(
libc::AF_UNIX,
libc::SOCK_SEQPACKET | libc::SOCK_CLOEXEC,0,
sockets.as_mut_ptr(),)},0);Self(sockets)}pubfnfoo(&self){let data:*const_ = &DATA;assert_eq!(unsafe{
libc::send(self.0[0],
ptr::addr_of!(data).cast(),
mem::size_of_val(&data),0,)},
mem::size_of_val(&data)as _
);}pubfnbar(&self) -> i32{letmut ptr:*consti32 = ptr::null();assert_eq!(unsafe{
libc::recv(self.0[1],
ptr::addr_of_mut!(ptr).cast(),
mem::size_of_val(&ptr),0,)},
mem::size_of_val(&ptr)as _
);unsafe{*ptr }}}}fnmain(){let foo = external_library::Foo::new();SockRef::from(&3).send(&[0; mem::size_of::<*consti32>()]).unwrap();
foo.foo();dbg!(foo.bar());}Here, the safe SockRef::from allows to crash a (fake) "external library" by letting it dereference a null pointer using only safe code. Making functions that accept arbitrary file descriptors or SOCKETs unsafe can probably solve this problem.
See rust-lang/rust#72175 and nix-rust/nix#1421: it seems an external library is allowed to assume a file descriptor is private, for example:
Here, the safe
SockRef::fromallows to crash a (fake) "external library" by letting it dereference a null pointer using only safe code. Making functions that accept arbitrary file descriptors orSOCKETs unsafe can probably solve this problem.