Skip to content

Commit c16d52d

Browse files
committed
Auto merge of #79387 - woodruffw-forks:ww/peer-cred-pid-macos, r=Amanieu
ext/ucred: Support PID in peer creds on macOS This is a follow-up to #75148 (RFC: #42839). The original PR used `getpeereid` on macOS and the BSDs, since they don't (generally) support the `SO_PEERCRED` mechanism that Linux supplies. This PR splits the macOS/iOS implementation of `peer_cred()` from that of the BSDs, since macOS supplies the `LOCAL_PEERPID` sockopt as a source of the missing PID. It also adds a `cfg`-gated tests that ensures that platforms with support for PIDs in `UCred` have the expected data.
2 parents fa55f66 + 3d8329f commit c16d52d

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

library/std/src/sys/unix/ext/ucred.rs

+43-14
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@ pub struct UCred {
2828
#[cfg(any(target_os = "android", target_os = "linux"))]
2929
pub use self::impl_linux::peer_cred;
3030

31-
#[cfg(any(
32-
target_os = "dragonfly",
33-
target_os = "freebsd",
34-
target_os = "ios",
35-
target_os = "macos",
36-
target_os = "openbsd"
37-
))]
31+
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))]
3832
pub use self::impl_bsd::peer_cred;
3933

34+
#[cfg(any(target_os = "macos", target_os = "ios",))]
35+
pub use self::impl_mac::peer_cred;
36+
4037
#[cfg(any(target_os = "linux", target_os = "android"))]
4138
pub mod impl_linux {
4239
use super::UCred;
@@ -73,13 +70,7 @@ pub mod impl_linux {
7370
}
7471
}
7572

76-
#[cfg(any(
77-
target_os = "dragonfly",
78-
target_os = "macos",
79-
target_os = "ios",
80-
target_os = "freebsd",
81-
target_os = "openbsd"
82-
))]
73+
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))]
8374
pub mod impl_bsd {
8475
use super::UCred;
8576
use crate::io;
@@ -95,3 +86,41 @@ pub mod impl_bsd {
9586
}
9687
}
9788
}
89+
90+
#[cfg(any(target_os = "macos", target_os = "ios",))]
91+
pub mod impl_mac {
92+
use super::UCred;
93+
use crate::os::unix::io::AsRawFd;
94+
use crate::os::unix::net::UnixStream;
95+
use crate::{io, mem};
96+
use libc::{c_void, getpeereid, getsockopt, pid_t, socklen_t, LOCAL_PEERPID, SOL_LOCAL};
97+
98+
pub fn peer_cred(socket: &UnixStream) -> io::Result<UCred> {
99+
let mut cred = UCred { uid: 1, gid: 1, pid: None };
100+
unsafe {
101+
let ret = getpeereid(socket.as_raw_fd(), &mut cred.uid, &mut cred.gid);
102+
103+
if ret != 0 {
104+
return Err(io::Error::last_os_error());
105+
}
106+
107+
let mut pid: pid_t = 1;
108+
let mut pid_size = mem::size_of::<pid_t>() as socklen_t;
109+
110+
let ret = getsockopt(
111+
socket.as_raw_fd(),
112+
SOL_LOCAL,
113+
LOCAL_PEERPID,
114+
&mut pid as *mut pid_t as *mut c_void,
115+
&mut pid_size,
116+
);
117+
118+
if ret == 0 && pid_size as usize == mem::size_of::<pid_t>() {
119+
cred.pid = Some(pid);
120+
Ok(cred)
121+
} else {
122+
Err(io::Error::last_os_error())
123+
}
124+
}
125+
}
126+
}

library/std/src/sys/unix/ext/ucred/tests.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::os::unix::net::UnixStream;
2-
use libc::{getegid, geteuid};
2+
use libc::{getegid, geteuid, getpid};
33

44
#[test]
55
#[cfg(any(
@@ -23,3 +23,16 @@ fn test_socket_pair() {
2323
assert_eq!(cred_a.uid, uid);
2424
assert_eq!(cred_a.gid, gid);
2525
}
26+
27+
#[test]
28+
#[cfg(any(target_os = "linux", target_os = "ios", target_os = "macos",))]
29+
fn test_socket_pair_pids(arg: Type) -> RetType {
30+
// Create two connected sockets and get their peer credentials.
31+
let (sock_a, sock_b) = UnixStream::pair().unwrap();
32+
let (cred_a, cred_b) = (sock_a.peer_cred().unwrap(), sock_b.peer_cred().unwrap());
33+
34+
// On supported platforms (see the cfg above), the credentials should always include the PID.
35+
let pid = unsafe { getpid() };
36+
assert_eq!(cred_a.pid, Some(pid));
37+
assert_eq!(cred_b.pid, Some(pid));
38+
}

0 commit comments

Comments
 (0)