Skip to main content

tilo_secrets/
device_proof.rs

1//! How a process that holds the device identity is recognised by
2//! `tilo-secret-service`, although it cannot look at it.
3//!
4//! The store names each caller by its executable, read from `/proc/<pid>/exe`, and a
5//! process that loads the device key makes itself non-dumpable, so the kernel refuses
6//! that link to every other process of the user, the store included. Such a process
7//! proves the identity instead.
8
9#[cfg(feature = "host-keyring")]
10use std::os::unix::ffi::OsStringExt;
11use std::sync::Mutex;
12
13use tilo_seal::secret_service::CallerKey;
14
15#[cfg(feature = "host-keyring")]
16const SERVICE: &str = "org.freedesktop.secrets";
17#[cfg(feature = "host-keyring")]
18const MANAGEMENT_PATH: &str = "/so/tilo/Secrets1";
19#[cfg(feature = "host-keyring")]
20const MANAGEMENT: &str = "so.tilo.Secrets1";
21
22static ARMED: Mutex<Option<CallerKey>> = Mutex::new(None);
23
24/// Arm this process with the device secret it just read; only the key derived for
25/// proofs is kept.
26pub fn arm(device_secret: &[u8]) {
27    let Ok(store) = CallerKey::derive(device_secret) else {
28        return;
29    };
30    if let Ok(mut armed) = ARMED.lock() {
31        *armed = Some(store);
32    }
33}
34
35#[cfg(feature = "host-keyring")]
36fn dumpable() -> bool {
37    // SAFETY: PR_GET_DUMPABLE takes no pointers and only reads a flag.
38    unsafe { libc::prctl(libc::PR_GET_DUMPABLE, 0, 0, 0, 0) == 1 }
39}
40
41#[cfg(feature = "host-keyring")]
42fn key() -> Option<CallerKey> {
43    if dumpable() {
44        return None;
45    }
46    ARMED.lock().ok()?.clone()
47}
48
49/// Whether this process identifies itself to the store by proof. A refusal is then
50/// worth one more attempt on a fresh connection, since a store that restarted has
51/// forgotten the proof.
52#[cfg(feature = "host-keyring")]
53pub(crate) fn in_use() -> bool {
54    key().is_some()
55}
56
57#[cfg(feature = "host-keyring")]
58fn answer(key: &CallerKey, challenge: &[u8], caller: &str) -> Option<(Vec<u8>, Vec<u8>)> {
59    // A process may always read its own link, dumpable or not.
60    let program = std::fs::read_link("/proc/self/exe")
61        .ok()?
62        .into_os_string()
63        .into_vec();
64    let proof = key.prove(challenge, caller, &program).ok()?;
65    Some((program, proof.to_vec()))
66}
67
68/// Prove the device identity on `connection`, if this process must.
69/// `Ok(false)` when it need not or cannot (dumpable, or never read the
70/// device key); a store that does not speak `so.tilo.Secrets1` (another
71/// Secret Service) answers with an error, and the caller carries on
72/// unproven.
73#[cfg(feature = "host-keyring")]
74pub(crate) fn prove_blocking(connection: &zbus::blocking::Connection) -> zbus::Result<bool> {
75    let Some(key) = key() else {
76        return Ok(false);
77    };
78    let caller = connection
79        .unique_name()
80        .ok_or_else(|| zbus::Error::Failure("a bus connection has no unique name".into()))?
81        .to_string();
82    let store = zbus::blocking::Proxy::new(connection, SERVICE, MANAGEMENT_PATH, MANAGEMENT)?;
83    let challenge: Vec<u8> = store.call("CallerChallenge", &())?;
84    let Some((program, proof)) = answer(&key, &challenge, &caller) else {
85        return Ok(false);
86    };
87    let _identity: String = store.call("ProveCaller", &(program, proof))?;
88    Ok(true)
89}
90
91#[cfg(feature = "host-keyring")]
92pub async fn prove(connection: &zbus::Connection) -> zbus::Result<bool> {
93    let Some(key) = key() else {
94        return Ok(false);
95    };
96    let caller = connection
97        .unique_name()
98        .ok_or_else(|| zbus::Error::Failure("a bus connection has no unique name".into()))?
99        .to_string();
100    let store = zbus::Proxy::new(connection, SERVICE, MANAGEMENT_PATH, MANAGEMENT).await?;
101    let challenge: Vec<u8> = store.call("CallerChallenge", &()).await?;
102    let Some((program, proof)) = answer(&key, &challenge, &caller) else {
103        return Ok(false);
104    };
105    let _identity: String = store.call("ProveCaller", &(program, proof)).await?;
106    Ok(true)
107}