From fe0bea2cc191fcdf8ca79a17168d696c664eded3 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Tue, 24 Nov 2020 13:46:51 -0500 Subject: [PATCH 1/2] ext/ucred: Support PID in peer creds on macOS --- library/std/src/sys/unix/ext/ucred.rs | 51 +++++++++++++++++++-- library/std/src/sys/unix/ext/ucred/tests.rs | 19 +++++++- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/library/std/src/sys/unix/ext/ucred.rs b/library/std/src/sys/unix/ext/ucred.rs index ed7516c7f28..e2aeb39f995 100644 --- a/library/std/src/sys/unix/ext/ucred.rs +++ b/library/std/src/sys/unix/ext/ucred.rs @@ -31,12 +31,16 @@ pub use self::impl_linux::peer_cred; #[cfg(any( target_os = "dragonfly", target_os = "freebsd", - target_os = "ios", - target_os = "macos", target_os = "openbsd" ))] pub use self::impl_bsd::peer_cred; +#[cfg(any( + target_os = "macos", + target_os = "ios", +))] +pub use self::impl_mac::peer_cred; + #[cfg(any(target_os = "linux", target_os = "android"))] pub mod impl_linux { use super::UCred; @@ -75,8 +79,6 @@ pub mod impl_linux { #[cfg(any( target_os = "dragonfly", - target_os = "macos", - target_os = "ios", target_os = "freebsd", target_os = "openbsd" ))] @@ -95,3 +97,44 @@ pub mod impl_bsd { } } } + +#[cfg(any( + target_os = "macos", + target_os = "ios", +))] +pub mod impl_mac { + use super::UCred; + use crate::{io, mem}; + use crate::os::unix::io::AsRawFd; + use crate::os::unix::net::UnixStream; + use libc::{c_void, getpeereid, getsockopt, pid_t, socklen_t, SOL_LOCAL, LOCAL_PEERPID}; + + pub fn peer_cred(socket: &UnixStream) -> io::Result { + let mut cred = UCred { uid: 1, gid: 1, pid: None }; + unsafe { + let ret = getpeereid(socket.as_raw_fd(), &mut cred.uid, &mut cred.gid); + + if ret != 0 { + return Err(io::Error::last_os_error()); + } + + let mut pid: pid_t = 1; + let mut pid_size = mem::size_of::() as socklen_t; + + let ret = getsockopt( + socket.as_raw_fd(), + SOL_LOCAL, + LOCAL_PEERPID, + &mut pid as *mut pid_t as *mut c_void, + &mut pid_size + ); + + if ret == 0 && pid_size as usize == mem::size_of::() { + cred.pid = Some(pid); + Ok(cred) + } else { + Err(io::Error::last_os_error()) + } + } + } +} diff --git a/library/std/src/sys/unix/ext/ucred/tests.rs b/library/std/src/sys/unix/ext/ucred/tests.rs index 451b534b266..b3e45d3337c 100644 --- a/library/std/src/sys/unix/ext/ucred/tests.rs +++ b/library/std/src/sys/unix/ext/ucred/tests.rs @@ -1,5 +1,5 @@ use crate::os::unix::net::UnixStream; -use libc::{getegid, geteuid}; +use libc::{getegid, geteuid, getpid}; #[test] #[cfg(any( @@ -23,3 +23,20 @@ fn test_socket_pair() { assert_eq!(cred_a.uid, uid); assert_eq!(cred_a.gid, gid); } + +#[test] +#[cfg(any( + target_os = "linux", + target_os = "ios", + target_os = "macos", +))] +fn test_socket_pair_pids(arg: Type) -> RetType { + // Create two connected sockets and get their peer credentials. + let (sock_a, sock_b) = UnixStream::pair().unwrap(); + let (cred_a, cred_b) = (sock_a.peer_cred().unwrap(), sock_b.peer_cred().unwrap()); + + // On supported platforms (see the cfg above), the credentials should always include the PID. + let pid = unsafe { getpid() }; + assert_eq!(cred_a.pid, Some(pid)); + assert_eq!(cred_b.pid, Some(pid)); +} From 3d8329f6fc678024fc74754f4e483d6a83fee098 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Tue, 24 Nov 2020 14:55:35 -0500 Subject: [PATCH 2/2] ext/ucred: fmt check --- library/std/src/sys/unix/ext/ucred.rs | 28 ++++++--------------- library/std/src/sys/unix/ext/ucred/tests.rs | 6 +---- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/library/std/src/sys/unix/ext/ucred.rs b/library/std/src/sys/unix/ext/ucred.rs index e2aeb39f995..1b4c18d3d84 100644 --- a/library/std/src/sys/unix/ext/ucred.rs +++ b/library/std/src/sys/unix/ext/ucred.rs @@ -28,17 +28,10 @@ pub struct UCred { #[cfg(any(target_os = "android", target_os = "linux"))] pub use self::impl_linux::peer_cred; -#[cfg(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "openbsd" -))] +#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))] pub use self::impl_bsd::peer_cred; -#[cfg(any( - target_os = "macos", - target_os = "ios", -))] +#[cfg(any(target_os = "macos", target_os = "ios",))] pub use self::impl_mac::peer_cred; #[cfg(any(target_os = "linux", target_os = "android"))] @@ -77,11 +70,7 @@ pub mod impl_linux { } } -#[cfg(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "openbsd" -))] +#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))] pub mod impl_bsd { use super::UCred; use crate::io; @@ -98,16 +87,13 @@ pub mod impl_bsd { } } -#[cfg(any( - target_os = "macos", - target_os = "ios", -))] +#[cfg(any(target_os = "macos", target_os = "ios",))] pub mod impl_mac { use super::UCred; - use crate::{io, mem}; use crate::os::unix::io::AsRawFd; use crate::os::unix::net::UnixStream; - use libc::{c_void, getpeereid, getsockopt, pid_t, socklen_t, SOL_LOCAL, LOCAL_PEERPID}; + use crate::{io, mem}; + use libc::{c_void, getpeereid, getsockopt, pid_t, socklen_t, LOCAL_PEERPID, SOL_LOCAL}; pub fn peer_cred(socket: &UnixStream) -> io::Result { let mut cred = UCred { uid: 1, gid: 1, pid: None }; @@ -126,7 +112,7 @@ pub mod impl_mac { SOL_LOCAL, LOCAL_PEERPID, &mut pid as *mut pid_t as *mut c_void, - &mut pid_size + &mut pid_size, ); if ret == 0 && pid_size as usize == mem::size_of::() { diff --git a/library/std/src/sys/unix/ext/ucred/tests.rs b/library/std/src/sys/unix/ext/ucred/tests.rs index b3e45d3337c..42d79418cf7 100644 --- a/library/std/src/sys/unix/ext/ucred/tests.rs +++ b/library/std/src/sys/unix/ext/ucred/tests.rs @@ -25,11 +25,7 @@ fn test_socket_pair() { } #[test] -#[cfg(any( - target_os = "linux", - target_os = "ios", - target_os = "macos", -))] +#[cfg(any(target_os = "linux", target_os = "ios", target_os = "macos",))] fn test_socket_pair_pids(arg: Type) -> RetType { // Create two connected sockets and get their peer credentials. let (sock_a, sock_b) = UnixStream::pair().unwrap();