pseudoroot-core 0.2.3

Inode-keyed fake-root state, daemon IPC protocol, and shared-memory session map for pseudoroot
Documentation
//! IPC protocol for communication between pseudoroot-lib and pseudoroot-daemon
//!
//! This module provides a complete IPC implementation using Unix domain sockets
//! for communication between the interposed library and the daemon process.

use serde::{Deserialize, Serialize};
use serde_wincode::SerdeCompat;
use std::collections::HashMap;
use std::io::{Read, Write};
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use wincode::{Deserialize as WincodeDeserialize, Serialize as WincodeSerialize};

/// Serialize `value` with the bincode-compatible wincode encoding.
fn serialize_wincode<T: Serialize>(value: &T) -> Vec<u8> {
    <SerdeCompat<T> as WincodeSerialize>::serialize(value).expect("Failed to serialize message")
}

/// Deserialize a value previously produced by [`serialize_wincode`].
fn deserialize_wincode<T: for<'de> Deserialize<'de>>(bytes: &[u8]) -> Option<T> {
    <SerdeCompat<T> as WincodeDeserialize>::deserialize(bytes).ok()
}

/// Default socket path for daemon communication
pub const DEFAULT_SOCKET_PATH: &str = "/tmp/pseudoroot.sock";

/// Message types for IPC communication
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MessageType {
    /// Register fake ownership for a path
    RegisterOwnership,
    /// Set current UID/GID
    SetCurrentUidGid,
    /// Get current UID/GID
    GetCurrentUidGid,
    /// Ping message for health check
    Ping,
    /// Response to a message
    Response,
    /// Error response
    Error,
    /// Get file ownership by path
    GetOwnership,
    /// Remove file ownership by path
    RemoveOwnership,
    /// Initialize connection with UID/GID
    Init,
    /// Merge uid/gid into an inode entry in one update
    UpsertChown,
    /// Ask a standalone daemon to shut itself down
    Shutdown,
}

/// Request ID generator
static REQUEST_ID: AtomicU64 = AtomicU64::new(1);

/// Generate a new request ID
#[must_use]
pub fn next_request_id() -> u64 {
    REQUEST_ID.fetch_add(1, Ordering::Relaxed)
}

/// A message in the IPC protocol
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolMessage {
    /// The type of message
    pub message_type: MessageType,
    /// The payload data (serialized)
    pub payload: Vec<u8>,
    /// Request ID for matching responses
    pub request_id: u64,
}

impl ProtocolMessage {
    /// Create a new protocol message
    #[must_use]
    pub fn new(message_type: MessageType, payload: Vec<u8>, request_id: u64) -> Self {
        Self {
            message_type,
            payload,
            request_id,
        }
    }

    /// Create a ping message
    #[must_use]
    pub fn ping() -> Self {
        Self::new(MessageType::Ping, Vec::new(), next_request_id())
    }

    /// Create a response message
    #[must_use]
    pub fn response(request_id: u64, payload: Vec<u8>) -> Self {
        Self::new(MessageType::Response, payload, request_id)
    }

    /// Create an error message
    #[must_use]
    pub fn error(request_id: u64, error_message: &str) -> Self {
        Self::new(
            MessageType::Error,
            error_message.as_bytes().to_vec(),
            request_id,
        )
    }

    /// Serialize the message to bytes
    #[must_use]
    pub fn to_bytes(&self) -> Vec<u8> {
        serialize_wincode(self)
    }

    /// Deserialize a message from bytes
    pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
        deserialize_wincode(bytes)
    }
}

/// Write a length-prefixed frame (4-byte big-endian length + payload).
pub fn write_framed(stream: &mut impl Write, bytes: &[u8]) -> std::io::Result<()> {
    let len = (bytes.len() as u32).to_be_bytes();
    stream.write_all(&len)?;
    stream.write_all(bytes)?;
    stream.flush()
}

/// Read a length-prefixed frame (4-byte big-endian length + payload).
pub fn read_framed(stream: &mut impl Read) -> std::io::Result<Vec<u8>> {
    let mut len_buf = [0u8; 4];
    stream.read_exact(&mut len_buf)?;
    let len = u32::from_be_bytes(len_buf) as usize;
    let mut buf = vec![0u8; len];
    stream.read_exact(&mut buf)?;
    Ok(buf)
}

/// IPC Channel for communication with the daemon
pub struct IpcChannel {
    stream: Option<UnixStream>,
    socket_path: PathBuf,
}

impl IpcChannel {
    /// Create a new IPC channel connected to the daemon socket
    pub fn new(socket_path: impl Into<PathBuf>) -> Self {
        Self {
            stream: None,
            socket_path: socket_path.into(),
        }
    }

    /// Connect to the daemon socket
    pub fn connect(&mut self) -> Result<(), std::io::Error> {
        if self.stream.is_some() {
            return Ok(());
        }
        let stream = UnixStream::connect(&self.socket_path)?;
        stream.set_nonblocking(false)?;
        self.stream = Some(stream);
        Ok(())
    }

    /// Check if connected
    #[must_use]
    pub fn is_connected(&self) -> bool {
        self.stream.is_some()
    }

    /// Send a message
    pub fn send(&mut self, message: ProtocolMessage) -> Result<(), std::io::Error> {
        if let Some(stream) = &mut self.stream {
            write_framed(stream, &message.to_bytes())
        } else {
            Err(std::io::Error::new(
                std::io::ErrorKind::NotConnected,
                "Not connected to daemon",
            ))
        }
    }

    /// Receive a message (blocking)
    pub fn recv(&mut self) -> Result<ProtocolMessage, std::io::Error> {
        if let Some(stream) = &mut self.stream {
            let buf = read_framed(stream)?;
            ProtocolMessage::from_bytes(&buf).ok_or_else(|| {
                std::io::Error::new(std::io::ErrorKind::InvalidData, "Invalid message")
            })
        } else {
            Err(std::io::Error::new(
                std::io::ErrorKind::NotConnected,
                "Not connected to daemon",
            ))
        }
    }

    /// Send a request and wait for response
    pub fn request(&mut self, message: ProtocolMessage) -> Result<ProtocolMessage, std::io::Error> {
        self.send(message)?;
        let response = self.recv()?;
        Ok(response)
    }

    /// Get the socket path
    #[must_use]
    pub fn socket_path(&self) -> &PathBuf {
        &self.socket_path
    }
}

impl Default for IpcChannel {
    fn default() -> Self {
        Self::new(DEFAULT_SOCKET_PATH)
    }
}

/// Helper trait for types that can be sent as IPC payloads
pub trait IpcPayload: Serialize + for<'a> Deserialize<'a> {
    /// Convert to payload bytes
    fn to_payload(&self) -> Vec<u8> {
        serialize_wincode(self)
    }

    /// Convert from payload bytes
    fn from_payload(bytes: &[u8]) -> Option<Self> {
        deserialize_wincode(bytes)
    }
}

impl<T: Serialize + for<'a> Deserialize<'a>> IpcPayload for T {}

/// Inode identity payload for lookup and removal RPCs.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct InodeKeyPayload {
    /// Filesystem device id (`st_dev`).
    pub dev: u64,
    /// Inode number (`st_ino`).
    pub ino: u64,
}

/// Full inode metadata sent with [`MessageType::RegisterOwnership`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InodeStatePayload {
    /// Filesystem device id (`st_dev`).
    pub dev: u64,
    /// Inode number (`st_ino`).
    pub ino: u64,
    /// Fake owner uid.
    pub uid: u32,
    /// Fake owner gid.
    pub gid: u32,
    /// Fake mode (type + permission bits), if set.
    pub mode: Option<u32>,
    /// Fake device id for device nodes, if set.
    pub rdev: Option<u64>,
    /// Faked extended attributes.
    pub xattrs: HashMap<String, Vec<u8>>,
}

/// Result of [`MessageType::GetOwnership`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InodeStateResult {
    /// Whether an entry exists for the requested inode key.
    pub found: bool,
    /// Fake owner uid (meaningful only when `found` is true).
    pub uid: u32,
    /// Fake owner gid (meaningful only when `found` is true).
    pub gid: u32,
    /// Fake mode, if recorded.
    pub mode: Option<u32>,
    /// Fake device id, if recorded.
    pub rdev: Option<u64>,
    /// Faked extended attributes.
    pub xattrs: HashMap<String, Vec<u8>>,
}

/// Current fake uid/gid for credential RPCs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UidGidPayload {
    /// Fake uid.
    pub uid: u32,
    /// Fake gid.
    pub gid: u32,
}

/// Chown merge payload for [`MessageType::UpsertChown`].
///
/// `uid`/`gid` of `u32::MAX` ([`crate::state::FakeRootState::ID_UNCHANGED`])
/// leave that field unchanged. `default_uid`/`default_gid` seed a new entry.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ChownPayload {
    /// Filesystem device id (`st_dev`).
    pub dev: u64,
    /// Inode number (`st_ino`).
    pub ino: u64,
    /// New uid, or `u32::MAX` to keep the existing value.
    pub uid: u32,
    /// New gid, or `u32::MAX` to keep the existing value.
    pub gid: u32,
    /// Default uid when creating a new inode entry.
    pub default_uid: u32,
    /// Default gid when creating a new inode entry.
    pub default_gid: u32,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_message_serialization() {
        let msg = ProtocolMessage::new(MessageType::Ping, vec![1, 2, 3], 42);
        let bytes = msg.to_bytes();
        let decoded = ProtocolMessage::from_bytes(&bytes).unwrap();
        assert_eq!(decoded.message_type, MessageType::Ping);
        assert_eq!(decoded.payload, vec![1, 2, 3]);
        assert_eq!(decoded.request_id, 42);
    }

    #[test]
    fn test_inode_state_payload() {
        let payload = InodeStatePayload {
            dev: 1,
            ino: 42,
            uid: 1000,
            gid: 2000,
            mode: Some(0o4755),
            rdev: Some(0x0803),
            xattrs: HashMap::from([("security.capability".to_string(), vec![1, 2, 3])]),
        };
        let bytes = payload.to_payload();
        let decoded: InodeStatePayload = deserialize_wincode(&bytes).unwrap();
        assert_eq!(decoded.dev, 1);
        assert_eq!(decoded.ino, 42);
        assert_eq!(decoded.uid, 1000);
        assert_eq!(decoded.gid, 2000);
        assert_eq!(decoded.mode, Some(0o4755));
        assert_eq!(decoded.rdev, Some(0x0803));
        assert_eq!(decoded.xattrs["security.capability"], vec![1, 2, 3]);
    }

    #[test]
    fn test_request_id_generation() {
        let id1 = next_request_id();
        let id2 = next_request_id();
        assert_eq!(id1 + 1, id2);
    }
}