|
| 1 | +// Copyright 2020 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +//! Unix peer credentials authenticator |
| 4 | +//! |
| 5 | +//! The `UnixPeerCredentialsAuthenticator` uses Unix peer credentials to perform authentication. As |
| 6 | +//! such, it uses the effective Unix user ID (UID) to authenticate the connecting process. Unix |
| 7 | +//! peer credentials also allow us to access the effective Unix group ID (GID) of the connecting |
| 8 | +//! process, although this information is currently unused. |
| 9 | +//! |
| 10 | +//! Currently, the stringified UID is used as the application name. |
| 11 | +
|
| 12 | +use super::ApplicationName; |
| 13 | +use super::Authenticate; |
| 14 | +use crate::front::listener::ConnectionMetadata; |
| 15 | +use log::error; |
| 16 | +use parsec_interface::operations::list_authenticators; |
| 17 | +use parsec_interface::requests::request::RequestAuth; |
| 18 | +use parsec_interface::requests::AuthType; |
| 19 | +use parsec_interface::requests::{ResponseStatus, Result}; |
| 20 | +use parsec_interface::secrecy::ExposeSecret; |
| 21 | +use std::convert::TryInto; |
| 22 | + |
| 23 | +/// Unix peer credentials authenticator. |
| 24 | +#[derive(Copy, Clone, Debug)] |
| 25 | +pub struct UnixPeerCredentialsAuthenticator; |
| 26 | + |
| 27 | +impl Authenticate for UnixPeerCredentialsAuthenticator { |
| 28 | + fn describe(&self) -> Result<list_authenticators::AuthenticatorInfo> { |
| 29 | + Ok(list_authenticators::AuthenticatorInfo { |
| 30 | + description: String::from( |
| 31 | + "Uses Unix peer credentials to authenticate the client. Verifies that the self-declared \ |
| 32 | + Unix user identifier (UID) in the request's authentication header matches that which is \ |
| 33 | + found from the peer credentials." |
| 34 | + ), |
| 35 | + version_maj: 0, |
| 36 | + version_min: 1, |
| 37 | + version_rev: 0, |
| 38 | + id: AuthType::PeerCredentials, |
| 39 | + }) |
| 40 | + } |
| 41 | + |
| 42 | + fn authenticate( |
| 43 | + &self, |
| 44 | + auth: &RequestAuth, |
| 45 | + meta: Option<ConnectionMetadata>, |
| 46 | + ) -> Result<ApplicationName> { |
| 47 | + // Parse authentication request. |
| 48 | + let expected_uid_bytes = auth.buffer.expose_secret(); |
| 49 | + |
| 50 | + const EXPECTED_UID_SIZE_BYTES: usize = 4; |
| 51 | + let expected_uid: [u8; EXPECTED_UID_SIZE_BYTES] = |
| 52 | + expected_uid_bytes.as_slice().try_into().map_err(|_| { |
| 53 | + error!( |
| 54 | + "UID in authentication request is not the right size (expected: {}, got: {}).", |
| 55 | + EXPECTED_UID_SIZE_BYTES, |
| 56 | + expected_uid_bytes.len() |
| 57 | + ); |
| 58 | + ResponseStatus::AuthenticationError |
| 59 | + })?; |
| 60 | + let expected_uid = u32::from_le_bytes(expected_uid); |
| 61 | + |
| 62 | + let meta = meta.ok_or_else(|| { |
| 63 | + error!("Authenticator did not receive any metadata; cannot perform authentication."); |
| 64 | + ResponseStatus::AuthenticationError |
| 65 | + })?; |
| 66 | + |
| 67 | + #[allow(unreachable_patterns)] |
| 68 | + let (uid, _gid, _pid) = match meta { |
| 69 | + ConnectionMetadata::UnixPeerCredentials { uid, gid, pid } => (uid, gid, pid), |
| 70 | + _ => { |
| 71 | + error!("Wrong metadata type given to Unix peer credentials authenticator."); |
| 72 | + return Err(ResponseStatus::AuthenticationError); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + // Authentication is successful if the _actual_ UID from the Unix peer credentials equals |
| 77 | + // the self-declared UID in the authentication request. |
| 78 | + if uid == expected_uid { |
| 79 | + Ok(ApplicationName(uid.to_string())) |
| 80 | + } else { |
| 81 | + error!("Declared UID in authentication request does not match the process's UID."); |
| 82 | + Err(ResponseStatus::AuthenticationError) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(test)] |
| 88 | +mod test { |
| 89 | + use super::super::Authenticate; |
| 90 | + use super::UnixPeerCredentialsAuthenticator; |
| 91 | + use crate::front::listener::ConnectionMetadata; |
| 92 | + use parsec_interface::requests::request::RequestAuth; |
| 93 | + use parsec_interface::requests::ResponseStatus; |
| 94 | + use rand::Rng; |
| 95 | + use std::os::unix::net::UnixStream; |
| 96 | + use users::get_current_uid; |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn successful_authentication() { |
| 100 | + // This test should PASS; we are verifying that our username gets set as the application |
| 101 | + // secret when using Unix peer credentials authentication with Unix domain sockets. |
| 102 | + |
| 103 | + // Create two connected sockets. |
| 104 | + let (sock_a, _sock_b) = UnixStream::pair().unwrap(); |
| 105 | + let (cred_a, _cred_b) = (sock_a.peer_cred().unwrap(), _sock_b.peer_cred().unwrap()); |
| 106 | + |
| 107 | + let authenticator = UnixPeerCredentialsAuthenticator {}; |
| 108 | + |
| 109 | + let req_auth_data = cred_a.uid.to_le_bytes().to_vec(); |
| 110 | + let req_auth = RequestAuth::new(req_auth_data); |
| 111 | + let conn_metadata = Some(ConnectionMetadata::UnixPeerCredentials { |
| 112 | + uid: cred_a.uid, |
| 113 | + gid: cred_a.gid, |
| 114 | + pid: None, |
| 115 | + }); |
| 116 | + |
| 117 | + let auth_name = authenticator |
| 118 | + .authenticate(&req_auth, conn_metadata) |
| 119 | + .expect("Failed to authenticate"); |
| 120 | + |
| 121 | + assert_eq!(auth_name.get_name(), get_current_uid().to_string()); |
| 122 | + } |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn unsuccessful_authentication_wrong_declared_uid() { |
| 126 | + // This test should FAIL; we are trying to authenticate, but we are declaring the wrong |
| 127 | + // UID. |
| 128 | + |
| 129 | + // Create two connected sockets. |
| 130 | + let (sock_a, _sock_b) = UnixStream::pair().unwrap(); |
| 131 | + let (cred_a, _cred_b) = (sock_a.peer_cred().unwrap(), _sock_b.peer_cred().unwrap()); |
| 132 | + |
| 133 | + let authenticator = UnixPeerCredentialsAuthenticator {}; |
| 134 | + |
| 135 | + let wrong_uid = cred_a.uid + 1; |
| 136 | + let wrong_req_auth_data = wrong_uid.to_le_bytes().to_vec(); |
| 137 | + let req_auth = RequestAuth::new(wrong_req_auth_data); |
| 138 | + let conn_metadata = Some(ConnectionMetadata::UnixPeerCredentials { |
| 139 | + uid: cred_a.uid, |
| 140 | + gid: cred_a.gid, |
| 141 | + pid: cred_a.pid, |
| 142 | + }); |
| 143 | + |
| 144 | + let auth_result = authenticator.authenticate(&req_auth, conn_metadata); |
| 145 | + assert_eq!(auth_result, Err(ResponseStatus::AuthenticationError)); |
| 146 | + } |
| 147 | + |
| 148 | + #[test] |
| 149 | + fn unsuccessful_authentication_garbage_data() { |
| 150 | + // This test should FAIL; we are sending garbage (random) data in the request. |
| 151 | + |
| 152 | + // Create two connected sockets. |
| 153 | + let (sock_a, _sock_b) = UnixStream::pair().unwrap(); |
| 154 | + let (cred_a, _cred_b) = (sock_a.peer_cred().unwrap(), _sock_b.peer_cred().unwrap()); |
| 155 | + |
| 156 | + let authenticator = UnixPeerCredentialsAuthenticator {}; |
| 157 | + |
| 158 | + let garbage_data = rand::thread_rng().gen::<[u8; 32]>().to_vec(); |
| 159 | + let req_auth = RequestAuth::new(garbage_data); |
| 160 | + let conn_metadata = Some(ConnectionMetadata::UnixPeerCredentials { |
| 161 | + uid: cred_a.uid, |
| 162 | + gid: cred_a.gid, |
| 163 | + pid: cred_a.pid, |
| 164 | + }); |
| 165 | + |
| 166 | + let auth_result = authenticator.authenticate(&req_auth, conn_metadata); |
| 167 | + assert_eq!(auth_result, Err(ResponseStatus::AuthenticationError)); |
| 168 | + } |
| 169 | + |
| 170 | + #[test] |
| 171 | + fn unsuccessful_authentication_no_metadata() { |
| 172 | + let authenticator = UnixPeerCredentialsAuthenticator {}; |
| 173 | + let req_auth = RequestAuth::new("secret".into()); |
| 174 | + |
| 175 | + let conn_metadata = None; |
| 176 | + let auth_result = authenticator.authenticate(&req_auth, conn_metadata); |
| 177 | + assert_eq!(auth_result, Err(ResponseStatus::AuthenticationError)); |
| 178 | + } |
| 179 | + |
| 180 | + #[test] |
| 181 | + fn unsuccessful_authentication_wrong_metadata() { |
| 182 | + // TODO(new_metadata_variant): this test needs implementing when we have more than one |
| 183 | + // metadata type. At the moment, the compiler just complains with an 'unreachable branch' |
| 184 | + // message. |
| 185 | + } |
| 186 | +} |
0 commit comments