Skip to content

Commit 6ea9a16

Browse files
author
Joe Ellis
committed
Cherry-pick Rust standard library peer credentials feature into Parsec
The code introduced in this patch has been cherry-picked from the following PR: rust-lang/rust#75148 At the time of writing (16/09/20), this patch is in the nightly Rust channel. To avoid needing to use the nightly compiler to build Parsec, this patch includes the change from the standard library to allow us to use this feature 'early'. Once the feature hits the stable branch, it should be safe to revert this commit with `git revert`. Signed-off-by: Joe Ellis <[email protected]>
1 parent 72adeef commit 6ea9a16

File tree

2 files changed

+139
-5
lines changed

2 files changed

+139
-5
lines changed

src/authenticators/unix_peer_credentials_authenticator/mod.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl Authenticate for UnixPeerCredentialsAuthenticator {
8484
mod test {
8585
use super::super::Authenticate;
8686
use super::UnixPeerCredentialsAuthenticator;
87+
use crate::front::domain_socket::peer_credentials;
8788
use crate::front::listener::ConnectionMetadata;
8889
use parsec_interface::requests::request::RequestAuth;
8990
use parsec_interface::requests::ResponseStatus;
@@ -98,7 +99,10 @@ mod test {
9899

99100
// Create two connected sockets.
100101
let (sock_a, _sock_b) = UnixStream::pair().unwrap();
101-
let (cred_a, _cred_b) = (sock_a.peer_cred().unwrap(), _sock_b.peer_cred().unwrap());
102+
let (cred_a, _cred_b) = (
103+
peer_credentials::peer_cred(&sock_a).unwrap(),
104+
peer_credentials::peer_cred(&_sock_b).unwrap(),
105+
);
102106

103107
let authenticator = UnixPeerCredentialsAuthenticator {};
104108

@@ -123,7 +127,10 @@ mod test {
123127

124128
// Create two connected sockets.
125129
let (sock_a, _sock_b) = UnixStream::pair().unwrap();
126-
let (cred_a, _cred_b) = (sock_a.peer_cred().unwrap(), _sock_b.peer_cred().unwrap());
130+
let (cred_a, _cred_b) = (
131+
peer_credentials::peer_cred(&sock_a).unwrap(),
132+
peer_credentials::peer_cred(&_sock_b).unwrap(),
133+
);
127134

128135
let authenticator = UnixPeerCredentialsAuthenticator {};
129136

@@ -145,7 +152,10 @@ mod test {
145152

146153
// Create two connected sockets.
147154
let (sock_a, _sock_b) = UnixStream::pair().unwrap();
148-
let (cred_a, _cred_b) = (sock_a.peer_cred().unwrap(), _sock_b.peer_cred().unwrap());
155+
let (cred_a, _cred_b) = (
156+
peer_credentials::peer_cred(&sock_a).unwrap(),
157+
peer_credentials::peer_cred(&_sock_b).unwrap(),
158+
);
149159

150160
let authenticator = UnixPeerCredentialsAuthenticator {};
151161

src/front/domain_socket.rs

+126-2
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ impl Listen for DomainSocketListener {
202202
format_error!("Failed to set stream as blocking", err);
203203
None
204204
} else {
205-
let ucred = stream
206-
.peer_cred()
205+
let ucred = peer_credentials::peer_cred(&stream)
207206
.map_err(|err| {
208207
format_error!(
209208
"Failed to grab peer credentials metadata from UnixStream",
@@ -259,3 +258,128 @@ impl DomainSocketListenerBuilder {
259258
})?)
260259
}
261260
}
261+
262+
// == IMPORTANT NOTE ==
263+
//
264+
// The code below has been cherry-picked from the following PR:
265+
//
266+
// https://github.com/rust-lang/rust/pull/75148
267+
//
268+
// At the time of writing (16/09/20), this patch is in the nightly Rust channel. To avoid needing
269+
// to use the nightly compiler to build Parsec, we have instead opted to cherry-pick the change
270+
// from the patch to allow us to use this feature 'early'.
271+
//
272+
// Once the feature hits stable, it should be safe to revert the commit that introduced the changes
273+
// below with `git revert`. You can find the stabilizing Rust issue here:
274+
//
275+
// https://github.com/rust-lang/rust/issues/42839
276+
277+
/// Implementation of peer credentials fetching for Unix domain socket.
278+
pub mod peer_credentials {
279+
use libc::{gid_t, pid_t, uid_t};
280+
281+
/// Credentials for a UNIX process for credentials passing.
282+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
283+
pub struct UCred {
284+
/// The UID part of the peer credential. This is the effective UID of the process at the domain
285+
/// socket's endpoint.
286+
pub uid: uid_t,
287+
/// The GID part of the peer credential. This is the effective GID of the process at the domain
288+
/// socket's endpoint.
289+
pub gid: gid_t,
290+
/// The PID part of the peer credential. This field is optional because the PID part of the
291+
/// peer credentials is not supported on every platform. On platforms where the mechanism to
292+
/// discover the PID exists, this field will be populated to the PID of the process at the
293+
/// domain socket's endpoint. Otherwise, it will be set to None.
294+
pub pid: Option<pid_t>,
295+
}
296+
297+
#[cfg(any(target_os = "android", target_os = "linux"))]
298+
pub use self::impl_linux::peer_cred;
299+
300+
#[cfg(any(
301+
target_os = "dragonfly",
302+
target_os = "freebsd",
303+
target_os = "ios",
304+
target_os = "macos",
305+
target_os = "openbsd"
306+
))]
307+
pub use self::impl_bsd::peer_cred;
308+
309+
#[cfg(any(target_os = "linux", target_os = "android"))]
310+
#[allow(missing_docs, trivial_casts)] // docs not required; only used for selective compilation.
311+
pub mod impl_linux {
312+
use super::UCred;
313+
use libc::{c_void, getsockopt, socklen_t, ucred, SOL_SOCKET, SO_PEERCRED};
314+
use std::os::unix::io::AsRawFd;
315+
use std::os::unix::net::UnixStream;
316+
use std::{io, mem};
317+
318+
pub fn peer_cred(socket: &UnixStream) -> io::Result<UCred> {
319+
let ucred_size = mem::size_of::<ucred>();
320+
321+
// Trivial sanity checks.
322+
assert!(mem::size_of::<u32>() <= mem::size_of::<usize>());
323+
assert!(ucred_size <= u32::MAX as usize);
324+
325+
let mut ucred_size = ucred_size as socklen_t;
326+
let mut ucred: ucred = ucred {
327+
pid: 1,
328+
uid: 1,
329+
gid: 1,
330+
};
331+
332+
unsafe {
333+
let ret = getsockopt(
334+
socket.as_raw_fd(),
335+
SOL_SOCKET,
336+
SO_PEERCRED,
337+
&mut ucred as *mut ucred as *mut c_void,
338+
&mut ucred_size,
339+
);
340+
341+
if ret == 0 && ucred_size as usize == mem::size_of::<ucred>() {
342+
Ok(UCred {
343+
uid: ucred.uid,
344+
gid: ucred.gid,
345+
pid: Some(ucred.pid),
346+
})
347+
} else {
348+
Err(io::Error::last_os_error())
349+
}
350+
}
351+
}
352+
}
353+
354+
#[cfg(any(
355+
target_os = "dragonfly",
356+
target_os = "macos",
357+
target_os = "ios",
358+
target_os = "freebsd",
359+
target_os = "openbsd"
360+
))]
361+
#[allow(missing_docs)] // docs not required; only used for selective compilation.
362+
pub mod impl_bsd {
363+
use super::UCred;
364+
use std::io;
365+
use std::os::unix::io::AsRawFd;
366+
use std::os::unix::net::UnixStream;
367+
368+
pub fn peer_cred(socket: &UnixStream) -> io::Result<UCred> {
369+
let mut cred = UCred {
370+
uid: 1,
371+
gid: 1,
372+
pid: None,
373+
};
374+
unsafe {
375+
let ret = libc::getpeereid(socket.as_raw_fd(), &mut cred.uid, &mut cred.gid);
376+
377+
if ret == 0 {
378+
Ok(cred)
379+
} else {
380+
Err(io::Error::last_os_error())
381+
}
382+
}
383+
}
384+
}
385+
}

0 commit comments

Comments
 (0)