Skip to main content

hatchery_node/
keystore_proxy.rs

1//! Node-side keystore proxy (was `m4a_signer_proxy`) (option A, `specs/m4a-session-keys-2026-10-16.md`).
2//!
3//! The node holds NO m4a private keys. For each PTY session it keeps the session's capability
4//! (issued by the hq-core signer) in its own memory and gives the PTY only a **handle**
5//! (`M4A_SESSION_HANDLE`) plus the nick. A handle is useless on its own: the proxy accepts a
6//! request on its UDS only if the kernel-reported peer (`SO_PEERCRED`) has the session's uid
7//! and its pid is the session's PTY root process or a descendant of it. A neighbour shell on
8//! the same node that steals the handle (env, `/proc/<pid>/environ`, a prompt injection) is
9//! refused, and nothing it could steal lets it sign: the capability never enters the PTY and
10//! the key never leaves hq-core.
11
12use std::collections::HashMap;
13use std::sync::{Arc, Mutex};
14
15use serde::{Deserialize, Serialize};
16use zeroize::Zeroizing;
17
18pub const HANDLE_ENV: &str = "M4A_SESSION_HANDLE";
19pub const SOCKET_ENV: &str = "M4A_SIGNER_SOCK";
20
21/// The hq-core signer as seen from the node (in production: a call over the C2 uplink).
22pub trait CoreSigner: Send + Sync {
23    fn sign(&self, token: &str, op: &str, payload: &[u8]) -> Result<SignedReply, String>;
24    /// Identity for a fresh session: (nick, token). Default: not supported.
25    fn provision(&self, _provider: &str, _session: &str) -> Result<(String, String), String> {
26        Err("provision not supported".into())
27    }
28    /// Signer-signed letter delivered by core from the token's own nick.
29    fn send(&self, _token: &str, _letter: &serde_json::Value) -> Result<serde_json::Value, String> {
30        Err("send not supported".into())
31    }
32    fn revoke(&self, _session: &str) -> Result<(), String> {
33        Ok(())
34    }
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
38pub struct SignedReply {
39    pub nick: String,
40    pub public_key_hex: String,
41    pub signature_hex: String,
42}
43
44/// Parent-pid lookup; `/proc` on Linux, injectable for tests.
45pub trait ProcTree: Send + Sync {
46    fn parent(&self, pid: u32) -> Option<u32>;
47}
48
49pub struct LinuxProc;
50
51impl ProcTree for LinuxProc {
52    fn parent(&self, pid: u32) -> Option<u32> {
53        let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?;
54        // `pid (comm) state ppid ...` — comm may contain spaces/parens; split after the last ')'.
55        let rest = &stat[stat.rfind(')')? + 1..];
56        rest.split_whitespace().nth(1)?.parse().ok()
57    }
58}
59
60struct Binding {
61    session: String,
62    provider: String,
63    nick: String,
64    /// 0 until the PTY child is running (requests before that are refused).
65    root_pid: u32,
66    uid: u32,
67    /// Empty until the first request provisions the identity at core.
68    token: Zeroizing<String>,
69}
70
71#[derive(Debug, Clone, PartialEq, Eq)]
72pub enum Refusal {
73    UnknownHandle,
74    WrongUser,
75    NotInSession,
76    Upstream(String),
77}
78
79pub struct SignerProxy {
80    bindings: Mutex<HashMap<String, Binding>>,
81    proc_tree: Box<dyn ProcTree>,
82    core: Arc<dyn CoreSigner>,
83}
84
85impl SignerProxy {
86    pub fn new(core: Arc<dyn CoreSigner>) -> Self {
87        Self::with_proc(core, Box::new(LinuxProc))
88    }
89
90    pub fn with_proc(core: Arc<dyn CoreSigner>, proc_tree: Box<dyn ProcTree>) -> Self {
91        Self { bindings: Mutex::new(HashMap::new()), proc_tree, core }
92    }
93
94    /// Called by spawn once the PTY child exists. Returns the handle for the PTY env.
95    /// A rebind of the same session drops its previous handle.
96    pub fn bind(&self, session: &str, nick: &str, root_pid: u32, uid: u32, token: String) -> String {
97        let mut raw = [0u8; 24];
98        fill_random(&mut raw);
99        let handle = hex::encode(raw);
100        let mut b = self.bindings.lock().unwrap_or_else(|p| p.into_inner());
101        b.retain(|_, v| v.session != session);
102        b.insert(
103            handle.clone(),
104            Binding { session: session.to_owned(), provider: String::new(), nick: nick.to_owned(), root_pid, uid, token: Zeroizing::new(token) },
105        );
106        handle
107    }
108
109    /// At spawn, before the PTY exists: a handle for its env; identity is provisioned lazily.
110    pub fn reserve(&self, session: &str, provider: &str, uid: u32) -> String {
111        let h = self.bind(session, "", 0, uid, String::new());
112        if let Some(b) = self.bindings.lock().unwrap_or_else(|p| p.into_inner()).get_mut(&h) {
113            b.provider = provider.to_owned();
114        }
115        h
116    }
117
118    /// The PTY child is running: bind the handle to its process tree.
119    pub fn attach_pid(&self, session: &str, pid: u32) {
120        for b in self.bindings.lock().unwrap_or_else(|p| p.into_inner()).values_mut() {
121            if b.session == session && b.root_pid == 0 {
122                b.root_pid = pid;
123            }
124        }
125    }
126
127    pub fn has_session(&self, session: &str) -> bool {
128        self.bindings.lock().unwrap_or_else(|p| p.into_inner()).values().any(|b| b.session == session)
129    }
130
131    /// Stop: drop the handle and revoke at core.
132    pub fn release(&self, session: &str) {
133        if self.has_session(session) {
134            self.unbind(session);
135            if let Err(e) = self.core.revoke(session) {
136                tracing::warn!(error = %e, "m4a signer revoke failed");
137            }
138        }
139    }
140
141    /// Authorize the peer and return (token, nick), provisioning at core on first use.
142    fn authorize(&self, handle: &str, peer_pid: u32, peer_uid: u32) -> Result<(Zeroizing<String>, String), Refusal> {
143        let (session, provider, token, nick) = {
144            let b = self.bindings.lock().unwrap_or_else(|p| p.into_inner());
145            let v = b.iter().find(|(h, _)| ct_eq(h.as_bytes(), handle.as_bytes())).map(|(_, v)| v).ok_or(Refusal::UnknownHandle)?;
146            if v.uid != peer_uid {
147                return Err(Refusal::WrongUser);
148            }
149            if v.root_pid == 0 || !self.in_tree(v.root_pid, peer_pid) {
150                return Err(Refusal::NotInSession);
151            }
152            (v.session.clone(), v.provider.clone(), v.token.clone(), v.nick.clone())
153        };
154        if !token.is_empty() {
155            return Ok((token, nick));
156        }
157        let (nick, token) = self.core.provision(&provider, &session).map_err(Refusal::Upstream)?;
158        let mut b = self.bindings.lock().unwrap_or_else(|p| p.into_inner());
159        let v = b.get_mut(handle).ok_or(Refusal::UnknownHandle)?;
160        v.token = Zeroizing::new(token.clone());
161        v.nick = nick.clone();
162        Ok((Zeroizing::new(token), nick))
163    }
164
165    /// Send a letter as the peer's own session (core signs and delivers).
166    pub fn send_for_peer(&self, handle: &str, peer_pid: u32, peer_uid: u32, letter: &serde_json::Value) -> Result<serde_json::Value, Refusal> {
167        let (token, nick) = self.authorize(handle, peer_pid, peer_uid)?;
168        let out = self.core.send(&token, letter).map_err(Refusal::Upstream)?;
169        if out.get("from").and_then(|v| v.as_str()) != Some(nick.as_str()) {
170            return Err(Refusal::Upstream("core answered for another nick".into()));
171        }
172        Ok(out)
173    }
174
175    /// Session stopped: its handle stops working at once.
176    pub fn unbind(&self, session: &str) {
177        self.bindings.lock().unwrap_or_else(|p| p.into_inner()).retain(|_, v| v.session != session);
178    }
179
180    fn in_tree(&self, root: u32, mut pid: u32) -> bool {
181        for _ in 0..64 {
182            if pid == root {
183                return true;
184            }
185            match self.proc_tree.parent(pid) {
186                Some(p) if p != 0 && p != pid => pid = p,
187                _ => return false,
188            }
189        }
190        false
191    }
192
193    /// Authorize a peer (kernel-reported pid/uid) presenting `handle`, then ask core to sign.
194    pub fn sign_for_peer(&self, handle: &str, peer_pid: u32, peer_uid: u32, op: &str, payload: &[u8]) -> Result<SignedReply, Refusal> {
195        let (token, nick) = self.authorize(handle, peer_pid, peer_uid)?;
196        let reply = self.core.sign(&token, op, payload).map_err(Refusal::Upstream)?;
197        if reply.nick != nick {
198            return Err(Refusal::Upstream("core answered for another nick".into()));
199        }
200        Ok(reply)
201    }
202}
203
204fn ct_eq(a: &[u8], b: &[u8]) -> bool {
205    a.len() == b.len() && a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0
206}
207
208fn fill_random(buf: &mut [u8]) {
209    use std::io::Read;
210    std::fs::File::open("/dev/urandom").and_then(|mut f| f.read_exact(buf)).expect("/dev/urandom");
211}
212
213#[derive(Debug, Serialize, Deserialize)]
214pub struct ProxyRequest {
215    pub handle: String,
216    pub op: String,
217    pub payload_hex: String,
218}
219
220#[derive(Debug, Serialize, Deserialize)]
221#[serde(tag = "status", rename_all = "snake_case")]
222pub enum ProxyResponse {
223    Signed(SignedReply),
224    Sent { result: serde_json::Value },
225    Refused { reason: String },
226}
227
228/// Serve the proxy on a Unix socket (0600 dir/socket owned by the node user). One JSON line
229/// request per connection; peer identity comes from `SO_PEERCRED`, never from the request.
230#[cfg(unix)]
231pub async fn serve(proxy: Arc<SignerProxy>, listener: tokio::net::UnixListener) {
232    use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
233    loop {
234        let Ok((stream, _)) = listener.accept().await else { continue };
235        let proxy = proxy.clone();
236        tokio::spawn(async move {
237            let cred = match stream.peer_cred() {
238                Ok(c) => c,
239                Err(_) => return,
240            };
241            let (r, mut w) = stream.into_split();
242            let mut line = String::new();
243            let mut reader = BufReader::new(r.take(64 * 1024));
244            if reader.read_line(&mut line).await.is_err() {
245                return;
246            }
247            let resp = match serde_json::from_str::<ProxyRequest>(&line) {
248                Err(_) => ProxyResponse::Refused { reason: "bad request".into() },
249                Ok(req) => match (hex::decode(&req.payload_hex), cred.pid()) {
250                    (Ok(payload), Some(pid)) if pid > 0 => {
251                        let proxy = proxy.clone();
252                        let uid = cred.uid();
253                        let res = tokio::task::spawn_blocking(move || {
254                            if req.op == "send" {
255                                let letter: serde_json::Value = serde_json::from_slice(&payload).map_err(|_| Refusal::Upstream("bad letter".into()))?;
256                                proxy.send_for_peer(&req.handle, pid as u32, uid, &letter).map(|result| ProxyResponse::Sent { result })
257                            } else {
258                                proxy.sign_for_peer(&req.handle, pid as u32, uid, &req.op, &payload).map(ProxyResponse::Signed)
259                            }
260                        })
261                        .await;
262                        match res {
263                            Ok(Ok(r)) => r,
264                            Ok(Err(e)) => {
265                                tracing::warn!(peer_pid = pid, reason = ?e, "m4a signer proxy refused a request");
266                                ProxyResponse::Refused { reason: format!("{e:?}") }
267                            }
268                            Err(_) => ProxyResponse::Refused { reason: "internal".into() },
269                        }
270                    }
271                    _ => ProxyResponse::Refused { reason: "bad request".into() },
272                },
273            };
274            let mut out = serde_json::to_vec(&resp).unwrap_or_default();
275            out.push(b'\n');
276            let _ = w.write_all(&out).await;
277        });
278    }
279}
280
281/// Blocking client (what the session-side `m4a` tool does).
282#[cfg(unix)]
283pub fn request(sock: &std::path::Path, handle: &str, op: &str, payload: &[u8]) -> std::io::Result<ProxyResponse> {
284    use std::io::{BufRead, Write};
285    let mut s = std::os::unix::net::UnixStream::connect(sock)?;
286    let mut req = serde_json::to_vec(&ProxyRequest { handle: handle.into(), op: op.into(), payload_hex: hex::encode(payload) })?;
287    req.push(b'\n');
288    s.write_all(&req)?;
289    let mut line = String::new();
290    std::io::BufReader::new(s).read_line(&mut line)?;
291    serde_json::from_str(&line).map_err(std::io::Error::other)
292}
293
294#[cfg(test)]
295mod tests {
296    use super::*;
297
298    struct FakeTree(HashMap<u32, u32>);
299    impl ProcTree for FakeTree {
300        fn parent(&self, pid: u32) -> Option<u32> {
301            self.0.get(&pid).copied()
302        }
303    }
304    struct Echo;
305    impl CoreSigner for Echo {
306        fn sign(&self, token: &str, _op: &str, _p: &[u8]) -> Result<SignedReply, String> {
307            Ok(SignedReply { nick: token.trim_start_matches("tok-").into(), public_key_hex: String::new(), signature_hex: String::new() })
308        }
309    }
310
311    #[test]
312    fn handle_works_only_inside_its_session_tree_and_for_its_uid() {
313        // 100 = PTY root of A, 101 = child of A, 200 = PTY root of B, 201 = neighbour shell in B.
314        let tree = FakeTree([(101, 100), (100, 1), (201, 200), (200, 1)].into_iter().collect());
315        let p = SignerProxy::with_proc(Arc::new(Echo), Box::new(tree));
316        let ha = p.bind("sa", "a", 100, 1000, "tok-a".into());
317        let hb = p.bind("sb", "b", 200, 1000, "tok-b".into());
318        assert_eq!(p.sign_for_peer(&ha, 100, 1000, "send_as", b"x").unwrap().nick, "a");
319        assert_eq!(p.sign_for_peer(&ha, 101, 1000, "send_as", b"x").unwrap().nick, "a");
320        assert_eq!(p.sign_for_peer(&ha, 201, 1000, "send_as", b"x"), Err(Refusal::NotInSession));
321        assert_eq!(p.sign_for_peer(&ha, 100, 1001, "send_as", b"x"), Err(Refusal::WrongUser));
322        assert_eq!(p.sign_for_peer(&hb, 201, 1000, "send_as", b"x").unwrap().nick, "b");
323        assert_eq!(p.sign_for_peer("guess", 100, 1000, "send_as", b"x"), Err(Refusal::UnknownHandle));
324        p.unbind("sa");
325        assert_eq!(p.sign_for_peer(&ha, 100, 1000, "send_as", b"x"), Err(Refusal::UnknownHandle));
326        let ha2 = p.bind("sb", "b", 200, 1000, "tok-b".into());
327        assert_eq!(p.sign_for_peer(&hb, 200, 1000, "send_as", b"x"), Err(Refusal::UnknownHandle), "rebind kills the old handle");
328        assert!(p.sign_for_peer(&ha2, 200, 1000, "send_as", b"x").is_ok());
329    }
330}
331
332/// Core signer over the node-call channel (Noise over the uplink, opaque to C2; core takes
333/// the node id from the stream). Called from blocking threads only.
334pub struct UplinkCoreSigner {
335    calls: crate::uplink_calls::NodeCalls,
336    rt: tokio::runtime::Handle,
337}
338
339impl UplinkCoreSigner {
340    pub fn new(calls: crate::uplink_calls::NodeCalls, rt: tokio::runtime::Handle) -> Self {
341        Self { calls, rt }
342    }
343    fn call(&self, op: &str, payload: serde_json::Value) -> Result<serde_json::Value, String> {
344        self.rt.block_on(self.calls.call(op, &payload)).map_err(|e| format!("{e:?}"))
345    }
346}
347
348impl CoreSigner for UplinkCoreSigner {
349    fn sign(&self, token: &str, op: &str, payload: &[u8]) -> Result<SignedReply, String> {
350        let v = self.call("signer.sign", serde_json::json!({ "token": token, "op": op, "payload_hex": hex::encode(payload) }))?;
351        serde_json::from_value(v).map_err(|e| e.to_string())
352    }
353    fn provision(&self, provider: &str, session: &str) -> Result<(String, String), String> {
354        let v = self.call("signer.provision", serde_json::json!({ "provider": provider, "session": session }))?;
355        Ok((v["nick"].as_str().ok_or("no nick")?.to_owned(), v["token"].as_str().ok_or("no token")?.to_owned()))
356    }
357    fn send(&self, token: &str, letter: &serde_json::Value) -> Result<serde_json::Value, String> {
358        let mut p = letter.clone();
359        p["token"] = serde_json::Value::String(token.to_owned());
360        self.call("signer.send", p)
361    }
362    fn revoke(&self, session: &str) -> Result<(), String> {
363        self.call("signer.revoke", serde_json::json!({ "session": session })).map(|_| ())
364    }
365}