about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-12-09 17:27:35 +0000
committerbors <bors@rust-lang.org>2020-12-09 17:27:35 +0000
commitc16d52db7778fba1c8b6c07b06510cfd6c32ae4f (patch)
tree6e9169604a2d082aa815aa5b07a45e28688d5c34
parentfa55f668e5ea5388ec98b9340969527252239151 (diff)
parent3d8329f6fc678024fc74754f4e483d6a83fee098 (diff)
downloadrust-c16d52db7778fba1c8b6c07b06510cfd6c32ae4f.tar.gz
rust-c16d52db7778fba1c8b6c07b06510cfd6c32ae4f.zip
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 https://github.com/rust-lang/rust/pull/75148 (RFC: https://github.com/rust-lang/rust/issues/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.
-rw-r--r--library/std/src/sys/unix/ext/ucred.rs57
-rw-r--r--library/std/src/sys/unix/ext/ucred/tests.rs15
2 files changed, 57 insertions, 15 deletions
diff --git a/library/std/src/sys/unix/ext/ucred.rs b/library/std/src/sys/unix/ext/ucred.rs
index ed7516c7f28..1b4c18d3d84 100644
--- a/library/std/src/sys/unix/ext/ucred.rs
+++ b/library/std/src/sys/unix/ext/ucred.rs
@@ -28,15 +28,12 @@ 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 = "ios",
-    target_os = "macos",
-    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",))]
+pub use self::impl_mac::peer_cred;
+
 #[cfg(any(target_os = "linux", target_os = "android"))]
 pub mod impl_linux {
     use super::UCred;
@@ -73,13 +70,7 @@ pub mod impl_linux {
     }
 }
 
-#[cfg(any(
-    target_os = "dragonfly",
-    target_os = "macos",
-    target_os = "ios",
-    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;
@@ -95,3 +86,41 @@ pub mod impl_bsd {
         }
     }
 }
+
+#[cfg(any(target_os = "macos", target_os = "ios",))]
+pub mod impl_mac {
+    use super::UCred;
+    use crate::os::unix::io::AsRawFd;
+    use crate::os::unix::net::UnixStream;
+    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<UCred> {
+        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::<pid_t>() 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::<pid_t>() {
+                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..42d79418cf7 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,16 @@ 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));
+}