Skip to content

Commit

Permalink
ScmCredentials now wraps UnixCredentials instead of libc::ucred
Browse files Browse the repository at this point in the history
  • Loading branch information
asomers committed Dec 2, 2019
1 parent 0f7be3e commit 6cc90b3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Added `User::from_uid`, `User::from_name`, `User::from_gid` and
`Group::from_name`,
([#1139](https://github.com/nix-rust/nix/pull/1139))

- Added `linkat`
([#1101](https://github.com/nix-rust/nix/pull/1101))

Expand All @@ -39,6 +40,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Changed
- `sys::termios::BaudRate` now implements `TryFrom<speed_t>` instead of
`From<speed_t>`. The old `From` implementation would panic on failure.

- `sys::socket::ControlMessage::ScmCredentials` and
`sys::socket::ControlMessageOwned::ScmCredentials` now wrap `UnixCredentials`
rather than `libc::ucred`.
([#1159](https://github.com/nix-rust/nix/pull/1159))

- `sys::socket::recvmsg` now takes a plain `Vec` instead of a `CmsgBuffer`
Expand Down
12 changes: 5 additions & 7 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ cfg_if! {
/// Unix credentials of the sending process.
///
/// This struct is used with the `SO_PEERCRED` ancillary message for UNIX sockets.
#[repr(C)]
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct UnixCredentials(libc::ucred);

Expand Down Expand Up @@ -368,7 +368,7 @@ pub enum ControlMessageOwned {
/// Received version of
/// [`ControlMessage::ScmCredentials`][#enum.ControlMessage.html#variant.ScmCredentials]
#[cfg(any(target_os = "android", target_os = "linux"))]
ScmCredentials(libc::ucred),
ScmCredentials(UnixCredentials),
/// A message of type `SCM_TIMESTAMP`, containing the time the
/// packet was received by the kernel.
///
Expand Down Expand Up @@ -496,7 +496,7 @@ impl ControlMessageOwned {
#[cfg(any(target_os = "android", target_os = "linux"))]
(libc::SOL_SOCKET, libc::SCM_CREDENTIALS) => {
let cred: libc::ucred = ptr::read_unaligned(p as *const _);
ControlMessageOwned::ScmCredentials(cred)
ControlMessageOwned::ScmCredentials(cred.into())
}
(libc::SOL_SOCKET, libc::SCM_TIMESTAMP) => {
let tv: libc::timeval = ptr::read_unaligned(p as *const _);
Expand Down Expand Up @@ -584,10 +584,8 @@ pub enum ControlMessage<'a> {
///
/// For further information, please refer to the
/// [`unix(7)`](http://man7.org/linux/man-pages/man7/unix.7.html) man page.
// FIXME: When `#[repr(transparent)]` is stable, use it on `UnixCredentials`
// and put that in here instead of a raw ucred.
#[cfg(any(target_os = "android", target_os = "linux"))]
ScmCredentials(&'a libc::ucred),
ScmCredentials(&'a UnixCredentials),

/// Set IV for `AF_ALG` crypto API.
///
Expand Down Expand Up @@ -655,7 +653,7 @@ impl<'a> ControlMessage<'a> {
},
#[cfg(any(target_os = "android", target_os = "linux"))]
ControlMessage::ScmCredentials(creds) => {
creds as *const libc::ucred as *const u8
&creds.0 as *const libc::ucred as *const u8
}
#[cfg(any(target_os = "android", target_os = "linux"))]
ControlMessage::AlgSetIv(iv) => {
Expand Down
16 changes: 8 additions & 8 deletions test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ fn test_scm_credentials() {
pid: getpid().as_raw(),
uid: getuid().as_raw(),
gid: getgid().as_raw(),
};
}.into();
let cmsg = ControlMessage::ScmCredentials(&cred);
assert_eq!(sendmsg(send, &iov, &[cmsg], MsgFlags::empty(), None).unwrap(), 5);
close(send).unwrap();
Expand All @@ -577,9 +577,9 @@ fn test_scm_credentials() {
for cmsg in msg.cmsgs() {
if let ControlMessageOwned::ScmCredentials(cred) = cmsg {
assert!(received_cred.is_none());
assert_eq!(cred.pid, getpid().as_raw());
assert_eq!(cred.uid, getuid().as_raw());
assert_eq!(cred.gid, getgid().as_raw());
assert_eq!(cred.pid(), getpid().as_raw());
assert_eq!(cred.uid(), getuid().as_raw());
assert_eq!(cred.gid(), getgid().as_raw());
received_cred = Some(cred);
} else {
panic!("unexpected cmsg");
Expand Down Expand Up @@ -641,7 +641,7 @@ fn test_impl_scm_credentials_and_rights(mut space: Vec<u8>) {
pid: getpid().as_raw(),
uid: getuid().as_raw(),
gid: getgid().as_raw(),
};
}.into();
let fds = [r];
let cmsgs = [
ControlMessage::ScmCredentials(&cred),
Expand Down Expand Up @@ -669,9 +669,9 @@ fn test_impl_scm_credentials_and_rights(mut space: Vec<u8>) {
}
ControlMessageOwned::ScmCredentials(cred) => {
assert!(received_cred.is_none());
assert_eq!(cred.pid, getpid().as_raw());
assert_eq!(cred.uid, getuid().as_raw());
assert_eq!(cred.gid, getgid().as_raw());
assert_eq!(cred.pid(), getpid().as_raw());
assert_eq!(cred.uid(), getuid().as_raw());
assert_eq!(cred.gid(), getgid().as_raw());
received_cred = Some(cred);
}
_ => panic!("unexpected cmsg"),
Expand Down

0 comments on commit 6cc90b3

Please sign in to comment.