From 6aa1d4bbf1882564e27ab659e6bdf646c0566027 Mon Sep 17 00:00:00 2001 From: DeFiDude <59237470+DeFiDude@users.noreply.github.com> Date: Sun, 25 Oct 2026 11:50:58 -0600 Subject: [PATCH] transport: keep each IFAC key in one wiped allocation shared by its interface records --- CHANGELOG.md | 1 + api/migrations/1.4.md | 5 ++ api/snapshots/rns-identity.txt | 13 ++++ api/snapshots/rns-transport.txt | 2 +- api/stability.json | 7 +- crates/rns-identity/src/ifac.rs | 73 ++++++++++++++++++- crates/rns-runtime/src/reticulum.rs | 54 +++++++------- .../src/actor/announce_admission.rs | 4 +- crates/rns-transport/src/actor/inbound.rs | 2 +- crates/rns-transport/src/actor/maintenance.rs | 2 +- crates/rns-transport/src/actor/mod.rs | 6 +- .../src/actor/tests/announce_admission.rs | 4 +- .../src/actor/tests/held_announces.rs | 4 +- .../src/actor/tests/interface_stats.rs | 2 +- .../src/actor/tests/packet_capacity.rs | 6 +- crates/rns-transport/src/messages.rs | 4 +- crates/rns-transport/tests/secret_residue.rs | 35 ++++++++- 17 files changed, 173 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56f4379..be8036b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ - A 64-byte identity file always loads as a raw key, as in Python Reticulum. About one key in 29,000 began with bytes that were misread as the older envelope format, and its identity failed to load. - Writing, verifying and recovering state files retries a read that a signal interrupts instead of failing. - Interface options wipe their IFAC passphrase when dropped, and the debug output of IFAC settings no longer shows it. +- Each interface's IFAC key lives in one allocation that is wiped when dropped, so interface tables no longer leave copies of it in freed memory. ## 1.4.0 - 2026-10-12 diff --git a/api/migrations/1.4.md b/api/migrations/1.4.md index 61e0ffb..f8796fb 100644 --- a/api/migrations/1.4.md +++ b/api/migrations/1.4.md @@ -124,6 +124,11 @@ file larger than 1 KiB. passphrase is wiped when the options are dropped: read it with `.as_deref().map(String::as_str)` and wrap a new one in `Zeroizing::new`. `RuntimeInterfaceIfacConfig` prints `` in place of its passphrase. +`InterfaceEntry::ifac_key` is `Option`, one +allocation that clones share and that is wiped when the last one drops, so +interface tables never copy the key. `with_ifac` still takes the key bytes; +otherwise set `Some(key.into())` or `IfacKey::derive(network_name, +passphrase)`, and read the bytes through `&*key`. rns-crypto enables the `zeroize` features of `aes` and `cbc`, so AES key schedules and CBC state are wiped when dropped; dependents that build with `--locked` refresh their lock files. diff --git a/api/snapshots/rns-identity.txt b/api/snapshots/rns-identity.txt index f4a2f99..7f0251a 100644 --- a/api/snapshots/rns-identity.txt +++ b/api/snapshots/rns-identity.txt @@ -248,6 +248,19 @@ pub mod rns_identity::ifac pub enum rns_identity::ifac::IfacError pub rns_identity::ifac::IfacError::HkdfFailed(rns_crypto::hkdf::HkdfError) pub rns_identity::ifac::IfacError::InvalidSalt +pub struct rns_identity::ifac::IfacKey(_) +impl rns_identity::ifac::IfacKey +pub fn rns_identity::ifac::IfacKey::derive(core::option::Option<&str>, core::option::Option<&str>) -> core::result::Result +impl core::cmp::Eq for rns_identity::ifac::IfacKey +impl core::cmp::PartialEq for rns_identity::ifac::IfacKey +pub fn rns_identity::ifac::IfacKey::eq(&self, &Self) -> bool +impl core::convert::From<[u8; 64]> for rns_identity::ifac::IfacKey +pub fn rns_identity::ifac::IfacKey::from([u8; 64]) -> Self +impl core::fmt::Debug for rns_identity::ifac::IfacKey +pub fn rns_identity::ifac::IfacKey::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result +impl core::ops::deref::Deref for rns_identity::ifac::IfacKey +pub type rns_identity::ifac::IfacKey::Target = [u8; 64] +pub fn rns_identity::ifac::IfacKey::deref(&self) -> &[u8; 64] pub const rns_identity::ifac::IFAC_SALT: &str pub fn rns_identity::ifac::derive_ifac_identity(core::option::Option<&str>, core::option::Option<&str>) -> core::result::Result<(rns_identity::identity::Identity, [u8; 64], [u8; 64]), rns_identity::ifac::IfacError> pub fn rns_identity::ifac::derive_ifac_key(core::option::Option<&str>, core::option::Option<&str>) -> core::result::Result<[u8; 64], rns_identity::ifac::IfacError> diff --git a/api/snapshots/rns-transport.txt b/api/snapshots/rns-transport.txt index 132c2f2..c12b28d 100644 --- a/api/snapshots/rns-transport.txt +++ b/api/snapshots/rns-transport.txt @@ -1362,7 +1362,7 @@ pub rns_transport::messages::InterfaceEntry::announce_rate_target: core::option: pub rns_transport::messages::InterfaceEntry::announces_from_internal: bool pub rns_transport::messages::InterfaceEntry::bitrate: u64 pub rns_transport::messages::InterfaceEntry::direction: rns_transport::constants::InterfaceDirection -pub rns_transport::messages::InterfaceEntry::ifac_key: core::option::Option<[u8; 64]> +pub rns_transport::messages::InterfaceEntry::ifac_key: core::option::Option pub rns_transport::messages::InterfaceEntry::ifac_size: usize pub rns_transport::messages::InterfaceEntry::ifac_violations: u64 pub rns_transport::messages::InterfaceEntry::ingress: rns_transport::ingress::IngressController diff --git a/api/stability.json b/api/stability.json index ac12fe7..f6bed06 100644 --- a/api/stability.json +++ b/api/stability.json @@ -42,8 +42,8 @@ "tier": "candidate-stable", "compatibility": "reviewed-snapshot", "snapshot": "api/snapshots/rns-identity.txt", - "itemCount": 417, - "sha256": "7bafe01c7a5a5a88cedb1ed3207008a5373a691b3b014dd3ebd1ee40b4ac4293" + "itemCount": 430, + "sha256": "3ea675b37cbbbb57d60659316d56f2582da42558b66760a87092018bca3d15e2" }, { "name": "rns-transport", @@ -52,7 +52,7 @@ "compatibility": "reviewed-snapshot", "snapshot": "api/snapshots/rns-transport.txt", "itemCount": 1947, - "sha256": "43b699055b574dd1c04ef5b0add1c49cdeef65570023cfed9de3f0baf6bc5396" + "sha256": "79cd60b1cde9c92e97f58ded693d3754b53311ec16f161ffa5b90775ec3c4a45" }, { "name": "rns-link", @@ -134,6 +134,7 @@ "pub rns_transport::discovery::app_data::DiscoveryInfo::height: f64", "pub rns_transport::discovery::app_data::DiscoveryInfo::latitude: f64", "pub rns_transport::discovery::app_data::DiscoveryInfo::longitude: f64", + "pub rns_transport::messages::InterfaceEntry::ifac_key: core::option::Option<[u8; 64]>", "pub rns_transport::tunnel::TunnelEntry::interface_id: rns_transport::messages::InterfaceId" ], "rns-link": [ diff --git a/crates/rns-identity/src/ifac.rs b/crates/rns-identity/src/ifac.rs index 28c843c..b07a968 100644 --- a/crates/rns-identity/src/ifac.rs +++ b/crates/rns-identity/src/ifac.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use rns_crypto::hkdf::hkdf_sha256; use rns_crypto::sha::sha256; use thiserror::Error; @@ -28,6 +30,16 @@ pub fn derive_ifac_key( network_name: Option<&str>, passphrase: Option<&str>, ) -> Result<[u8; 64], IfacError> { + let mut key = [0u8; 64]; + derive_into(network_name, passphrase, &mut key)?; + Ok(key) +} + +fn derive_into( + network_name: Option<&str>, + passphrase: Option<&str>, + key: &mut [u8; 64], +) -> Result<(), IfacError> { let mut ifac_origin = Zeroizing::new(Vec::with_capacity(64)); if let Some(name) = network_name { @@ -42,9 +54,55 @@ pub fn derive_ifac_key( let salt = hex_decode(IFAC_SALT).ok_or(IfacError::InvalidSalt)?; let derived = hkdf_sha256(64, &*ifac_origin_hash, Some(&salt), None)?; - let mut key = [0u8; 64]; key.copy_from_slice(&derived); - Ok(key) + Ok(()) +} + +/// A derived IFAC key in one heap allocation, wiped when its last clone +/// drops. Clones share the allocation, so tables and queues that move or copy +/// an interface's settings never copy the key. +#[derive(Clone)] +pub struct IfacKey(Arc>); + +impl IfacKey { + /// [`derive_ifac_key`], written straight into the shared allocation. + pub fn derive(network_name: Option<&str>, passphrase: Option<&str>) -> Result { + let mut key = Arc::new(Zeroizing::new([0u8; 64])); + derive_into( + network_name, + passphrase, + Arc::get_mut(&mut key).expect("a new Arc is unique"), + )?; + Ok(Self(key)) + } +} + +impl From<[u8; 64]> for IfacKey { + fn from(key: [u8; 64]) -> Self { + Self(Arc::new(Zeroizing::new(key))) + } +} + +impl std::ops::Deref for IfacKey { + type Target = [u8; 64]; + + fn deref(&self) -> &[u8; 64] { + &self.0 + } +} + +impl PartialEq for IfacKey { + fn eq(&self, other: &Self) -> bool { + self.0[..] == other.0[..] + } +} + +impl Eq for IfacKey {} + +impl std::fmt::Debug for IfacKey { + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + formatter.write_str("IfacKey()") + } } /// Build the IFAC identity plus a self-signature over `SHA-256(ifac_key)`. @@ -92,6 +150,17 @@ fn hex_decode(s: &str) -> Option> { mod tests { use super::*; + #[test] + fn shared_key_matches_the_derived_bytes() { + let key = IfacKey::derive(Some("testnet"), Some("password")).unwrap(); + assert_eq!( + *key, + derive_ifac_key(Some("testnet"), Some("password")).unwrap() + ); + assert_eq!(key, IfacKey::from(*key)); + assert_eq!(format!("{key:?}"), "IfacKey()"); + } + #[test] fn test_ifac_derivation_deterministic() { let key1 = derive_ifac_key(Some("testnet"), Some("password")).unwrap(); diff --git a/crates/rns-runtime/src/reticulum.rs b/crates/rns-runtime/src/reticulum.rs index bc8321c..19c8e7e 100644 --- a/crates/rns-runtime/src/reticulum.rs +++ b/crates/rns-runtime/src/reticulum.rs @@ -3,6 +3,7 @@ //! to a Shared instance over a local socket; **Standalone** owns its //! interfaces and exposes no IPC. Python: `RNS/Reticulum.py`. +use rns_identity::ifac::IfacKey; use std::collections::HashMap; use std::future::Future; use std::path::{Path, PathBuf}; @@ -61,7 +62,7 @@ struct InterfaceControlMetadata { ingress_overrides: rns_transport::ingress::IngressOverrides, // Parent IFAC, inherited by accepted child connections (Python parity: // spawned interfaces copy the listener's ifac_size/netname/netkey). - ifac_key: Option<[u8; 64]>, + ifac_key: Option, ifac_size: usize, ifac_netname: Option, /// The host and port a client interface connects to (Python @@ -96,7 +97,7 @@ impl InterfaceIfacStats { .get(&id) .and_then(|control| { Some(( - control.ifac_key?, + control.ifac_key.clone()?, control.ifac_size, control.ifac_netname.clone(), )) @@ -3895,7 +3896,7 @@ fn child_registration_from_parent( ) -> ( rns_transport::messages::InterfaceRole, rns_transport::ingress::IngressOverrides, - Option<[u8; 64]>, + Option, usize, ) { let parent_control = parent_id.and_then(|parent_id| { @@ -3917,7 +3918,7 @@ fn child_registration_from_parent( .map(|control| { ( control.ingress_overrides, - control.ifac_key, + control.ifac_key.clone(), control.ifac_size, ) }) @@ -4457,7 +4458,7 @@ async fn register_interface_handle_with_role_and_overrides( handle: rns_interface::traits::InterfaceHandle, role: rns_transport::messages::InterfaceRole, ingress_overrides: rns_transport::ingress::IngressOverrides, - ifac_key: Option<[u8; 64]>, + ifac_key: Option, ifac_size: usize, interface_controls: &InterfaceControlMap, interface_registry: &InterfaceRegistry, @@ -4487,7 +4488,7 @@ async fn register_interface_handle_with_role_and_overrides_and_spawn_permit( handle: rns_interface::traits::InterfaceHandle, role: rns_transport::messages::InterfaceRole, ingress_overrides: rns_transport::ingress::IngressOverrides, - ifac_key: Option<[u8; 64]>, + ifac_key: Option, ifac_size: usize, interface_controls: &InterfaceControlMap, interface_registry: &InterfaceRegistry, @@ -4517,7 +4518,7 @@ async fn register_owned_interface_handle_with_role_and_overrides( owned: OwnedInterfaceHandle, role: rns_transport::messages::InterfaceRole, ingress_overrides: rns_transport::ingress::IngressOverrides, - ifac_key: Option<[u8; 64]>, + ifac_key: Option, ifac_size: usize, interface_controls: &InterfaceControlMap, interface_registry: &InterfaceRegistry, @@ -4549,7 +4550,7 @@ async fn prepare_interface_with_role_and_overrides( owned: OwnedInterfaceHandle, role: rns_transport::messages::InterfaceRole, ingress_overrides: rns_transport::ingress::IngressOverrides, - ifac_key: Option<[u8; 64]>, + ifac_key: Option, ifac_size: usize, interface_controls: &InterfaceControlMap, interface_registry: &InterfaceRegistry, @@ -4627,6 +4628,7 @@ async fn prepare_interface_with_role_and_overrides( .expect("interface_controls mutex poisoned"); // A spawned child's IFAC is its parent's, network name included. let ifac_netname = ifac_key + .as_ref() .and(handle.parent_id) .and_then(|parent| controls.get(&parent)) .and_then(|parent| parent.ifac_netname.clone()); @@ -4637,7 +4639,7 @@ async fn prepare_interface_with_role_and_overrides( registry_owner, role, ingress_overrides: ingress_overrides.clone(), - ifac_key, + ifac_key: ifac_key.clone(), ifac_size, ifac_netname, client_target: None, @@ -4701,7 +4703,7 @@ async fn register_interface_with_post_init( transport_tx: &mpsc::Sender, handle: rns_interface::traits::InterfaceHandle, post_init: &interface_factory::InterfacePostInit, - ifac_key: Option<[u8; 64]>, + ifac_key: Option, interface_controls: &InterfaceControlMap, interface_registry: &InterfaceRegistry, kind: InterfaceKind, @@ -4724,7 +4726,7 @@ async fn register_interface_with_post_init_and_spawn_permit( transport_tx: &mpsc::Sender, handle: rns_interface::traits::InterfaceHandle, post_init: &interface_factory::InterfacePostInit, - ifac_key: Option<[u8; 64]>, + ifac_key: Option, interface_controls: &InterfaceControlMap, interface_registry: &InterfaceRegistry, kind: InterfaceKind, @@ -4748,7 +4750,7 @@ async fn register_owned_interface_with_post_init( transport_tx: &mpsc::Sender, owned: OwnedInterfaceHandle, post_init: &interface_factory::InterfacePostInit, - ifac_key: Option<[u8; 64]>, + ifac_key: Option, interface_controls: &InterfaceControlMap, interface_registry: &InterfaceRegistry, kind: InterfaceKind, @@ -4839,7 +4841,7 @@ fn configured_peer_post_init( async fn prepare_interface_with_post_init( owned: OwnedInterfaceHandle, post_init: &interface_factory::InterfacePostInit, - ifac_key: Option<[u8; 64]>, + ifac_key: Option, interface_controls: &InterfaceControlMap, interface_registry: &InterfaceRegistry, kind: InterfaceKind, @@ -4944,9 +4946,9 @@ async fn prepare_interface_with_post_init( registry_owner, role: rns_transport::messages::InterfaceRole::Normal, ingress_overrides: post_init.ingress_overrides.clone(), - ifac_key, + ifac_netname: ifac_key.as_ref().and(post_init.ifac_network_name.clone()), + ifac_key: ifac_key.clone(), ifac_size: post_init.ifac_size.unwrap_or(post_init.default_ifac_size), - ifac_netname: ifac_key.and(post_init.ifac_network_name.clone()), client_target: None, autoconnect_source: None, }, @@ -5049,7 +5051,7 @@ enum SingleRegistrationSpec { owned: OwnedInterfaceHandle, role: rns_transport::messages::InterfaceRole, ingress_overrides: rns_transport::ingress::IngressOverrides, - ifac_key: Option<[u8; 64]>, + ifac_key: Option, ifac_size: usize, kind: InterfaceKind, multipoint: bool, @@ -5057,7 +5059,7 @@ enum SingleRegistrationSpec { PostInit { owned: OwnedInterfaceHandle, post_init: interface_factory::InterfacePostInit, - ifac_key: Option<[u8; 64]>, + ifac_key: Option, kind: InterfaceKind, }, } @@ -5441,7 +5443,7 @@ async fn register_interfaces_with_post_init_batch( transport_tx: &mpsc::Sender, handles: Vec, post_init: &interface_factory::InterfacePostInit, - ifac_key: Option<[u8; 64]>, + ifac_key: Option, interface_controls: &InterfaceControlMap, interface_registry: &InterfaceRegistry, kind: InterfaceKind, @@ -5487,7 +5489,7 @@ async fn batch_registration_worker( transport_tx: mpsc::Sender, handles: Vec, post_init: interface_factory::InterfacePostInit, - ifac_key: Option<[u8; 64]>, + ifac_key: Option, interface_controls: InterfaceControlMap, interface_registry: InterfaceRegistry, kind: InterfaceKind, @@ -5525,7 +5527,7 @@ async fn register_interfaces_with_post_init_batch_transaction( transport_tx: &mpsc::Sender, handles: Vec, post_init: &interface_factory::InterfacePostInit, - ifac_key: Option<[u8; 64]>, + ifac_key: Option, interface_controls: &InterfaceControlMap, interface_registry: &InterfaceRegistry, kind: InterfaceKind, @@ -5537,7 +5539,7 @@ async fn register_interfaces_with_post_init_batch_transaction( match prepare_interface_with_post_init( owned, post_init, - ifac_key, + ifac_key.clone(), interface_controls, interface_registry, kind, @@ -5720,9 +5722,9 @@ fn remove_interface_control_if_owner( fn derive_ifac_key_from_post_init( post_init: &interface_factory::InterfacePostInit, -) -> Option<[u8; 64]> { +) -> Option { if post_init.ifac_network_name.is_some() || post_init.ifac_passphrase.is_some() { - rns_identity::ifac::derive_ifac_key( + IfacKey::derive( post_init.ifac_network_name.as_deref(), post_init.ifac_passphrase.as_deref().map(String::as_str), ) @@ -9525,7 +9527,7 @@ mod tests { prepare_interface_with_post_init( handle.into(), &post_init, - Some([1; 64]), + Some([1; 64].into()), &controls, ®istry, InterfaceKind::Standard, @@ -9536,7 +9538,7 @@ mod tests { handle.into(), rns_transport::messages::InterfaceRole::Normal, Default::default(), - Some([1; 64]), + Some([1; 64].into()), size, &controls, ®istry, @@ -14254,7 +14256,7 @@ egress_control = Yes &transport_tx, test_interface_handle(parent_id, None, "ifac-parent"), &post_init, - ifac_key, + ifac_key.clone(), &interface_controls, &interface_registry, InterfaceKind::Standard, diff --git a/crates/rns-transport/src/actor/announce_admission.rs b/crates/rns-transport/src/actor/announce_admission.rs index daadde2..4624652 100644 --- a/crates/rns-transport/src/actor/announce_admission.rs +++ b/crates/rns-transport/src/actor/announce_admission.rs @@ -42,7 +42,7 @@ pub(super) struct OfferedAnnounce { pub raw: Bytes, pub destination: [u8; 16], pub route: Option, - pub policy: (u64, u64, Option<[u8; 64]>, usize), + pub policy: (u64, u64, Option, usize), pub receipt: crate::background_egress::BackgroundOfferReceipt, } @@ -589,7 +589,7 @@ impl TransportActor { ( entry.bitrate, entry.announce_cap.to_bits(), - entry.ifac_key, + entry.ifac_key.clone(), entry.ifac_size, ) == offered.policy && now_f64() >= entry.announce_allowed_at diff --git a/crates/rns-transport/src/actor/inbound.rs b/crates/rns-transport/src/actor/inbound.rs index aa762d4..da5cdda 100644 --- a/crates/rns-transport/src/actor/inbound.rs +++ b/crates/rns-transport/src/actor/inbound.rs @@ -113,7 +113,7 @@ impl TransportActor { } else { self.interfaces .get(&packet.interface_id) - .map(|entry| (entry.ifac_key, entry.ifac_size)) + .map(|entry| (entry.ifac_key.clone(), entry.ifac_size)) }; let raw: bytes::Bytes = match ifac { Some((Some(ifac_key), ifac_size)) => { diff --git a/crates/rns-transport/src/actor/maintenance.rs b/crates/rns-transport/src/actor/maintenance.rs index e776571..c304227 100644 --- a/crates/rns-transport/src/actor/maintenance.rs +++ b/crates/rns-transport/src/actor/maintenance.rs @@ -538,7 +538,7 @@ impl TransportActor { policy: ( entry.bitrate, entry.announce_cap.to_bits(), - entry.ifac_key, + entry.ifac_key.clone(), entry.ifac_size, ), receipt, diff --git a/crates/rns-transport/src/actor/mod.rs b/crates/rns-transport/src/actor/mod.rs index 499b067..77dbb5f 100644 --- a/crates/rns-transport/src/actor/mod.rs +++ b/crates/rns-transport/src/actor/mod.rs @@ -5362,7 +5362,7 @@ mod tests { // Configure IFAC on this interface let ifac_key = rns_identity::ifac::derive_ifac_key(Some("test"), Some("pass")).unwrap(); - entry.ifac_key = Some(ifac_key); + entry.ifac_key = Some(ifac_key.into()); entry.ifac_size = 4; actor.interfaces.insert(1, entry); @@ -5385,7 +5385,7 @@ mod tests { let ifac_size = 4; let (mut entry1, _rx1) = make_test_interface("ifac_in"); - entry1.ifac_key = Some(ifac_key); + entry1.ifac_key = Some(ifac_key.into()); entry1.ifac_size = ifac_size; actor.interfaces.insert(1, entry1); @@ -5425,7 +5425,7 @@ mod tests { let ifac_size = 4; let (mut entry1, _rx1) = make_test_interface("ifac_in"); - entry1.ifac_key = Some(ifac_key); + entry1.ifac_key = Some(ifac_key.into()); entry1.ifac_size = ifac_size; actor.interfaces.insert(1, entry1); diff --git a/crates/rns-transport/src/actor/tests/announce_admission.rs b/crates/rns-transport/src/actor/tests/announce_admission.rs index 191d8b9..88a4da6 100644 --- a/crates/rns-transport/src/actor/tests/announce_admission.rs +++ b/crates/rns-transport/src/actor/tests/announce_admission.rs @@ -39,7 +39,7 @@ async fn announce_admission_partial_targets_keep_exact_wire_without_replay() { let (mut actor, _) = TransportActor::new(); let (first, mut first_rx) = limited(); let (mut second, mut second_rx) = limited(); - second.ifac_key = Some([9; 64]); + second.ifac_key = Some([9; 64].into()); second.ifac_size = 16; second.tx.try_send(Bytes::from_static(b"ordinary")).unwrap(); actor.interfaces.insert(1, first); @@ -61,7 +61,7 @@ async fn announce_admission_partial_targets_keep_exact_wire_without_replay() { &raw, rns_wire::flags::HeaderType::Header1 ))); - actor.interfaces.get_mut(&2).unwrap().ifac_key = Some([7; 64]); + actor.interfaces.get_mut(&2).unwrap().ifac_key = Some([7; 64].into()); second_rx.try_recv().unwrap(); actor.process_pending_announce_admissions(); assert_eq!(second_rx.try_recv().unwrap().as_ref(), expected); diff --git a/crates/rns-transport/src/actor/tests/held_announces.rs b/crates/rns-transport/src/actor/tests/held_announces.rs index 9a21ead..b213d1e 100644 --- a/crates/rns-transport/src/actor/tests/held_announces.rs +++ b/crates/rns-transport/src/actor/tests/held_announces.rs @@ -10,7 +10,7 @@ fn ifac_hub() -> (TransportActor, mpsc::Receiver) { actor.transport_identity_hash = Some([0x42; 16]); let (mut entry, rx) = make_test_interface("held_ifac"); entry.ifac_key = - Some(rns_identity::ifac::derive_ifac_key(Some("held"), Some("announces")).unwrap()); + Some(rns_identity::ifac::IfacKey::derive(Some("held"), Some("announces")).unwrap()); entry.ifac_size = 8; actor.interfaces.insert(1, entry); (actor, rx) @@ -87,7 +87,7 @@ fn a_wire_announce_without_valid_ifac_is_dropped_and_never_held() { ingress.received_announce(); } assert!(ingress.should_ingress_limit(), "precondition: a burst"); - let ifac_key = actor.interfaces[&1].ifac_key.unwrap(); + let ifac_key = actor.interfaces[&1].ifac_key.clone().unwrap(); let (signed, signed_destination) = make_valid_announce("held.ifac.signed", 1); let (unsigned, unsigned_destination) = make_valid_announce("held.ifac.unsigned", 1); diff --git a/crates/rns-transport/src/actor/tests/interface_stats.rs b/crates/rns-transport/src/actor/tests/interface_stats.rs index 4e4d975..32f79ff 100644 --- a/crates/rns-transport/src/actor/tests/interface_stats.rs +++ b/crates/rns-transport/src/actor/tests/interface_stats.rs @@ -21,7 +21,7 @@ fn attach(actor: &mut TransportActor, id: InterfaceId) -> mpsc::Receiver fn attach_ifac(actor: &mut TransportActor, id: InterfaceId, ifac_key: [u8; 64]) { let (mut entry, _rx) = make_test_interface(&format!("ifac_{id}")); - entry.ifac_key = Some(ifac_key); + entry.ifac_key = Some(ifac_key.into()); entry.ifac_size = 8; actor.interfaces.insert(id, entry); } diff --git a/crates/rns-transport/src/actor/tests/packet_capacity.rs b/crates/rns-transport/src/actor/tests/packet_capacity.rs index 5696683..b13dad0 100644 --- a/crates/rns-transport/src/actor/tests/packet_capacity.rs +++ b/crates/rns-transport/src/actor/tests/packet_capacity.rs @@ -26,7 +26,7 @@ fn payload(mode: u8, mtu: u32) -> Vec { fn packet_capacity_attenuates_two_relays_without_changing_key_body() { let input = capacity(10_000, 10_000, true); let mut middle = capacity(1500, 1500, true); - middle.ifac_key = Some([1; 64]); + middle.ifac_key = Some([1; 64].into()); middle.ifac_size = 16; let output = capacity(1000, 1000, true); let original = payload(1, 4096); @@ -75,7 +75,7 @@ fn packet_capacity_opaque_modes_only_transit_with_exact_supported_shape() { ); } let mut narrow = capacity(500, 508, false); - narrow.ifac_key = Some([1; 64]); + narrow.ifac_key = Some([1; 64].into()); narrow.ifac_size = 9; assert_eq!(narrow.effective_clear_mtu().unwrap(), 499); assert!( @@ -88,7 +88,7 @@ fn packet_capacity_checks_clear_and_wire_with_ifac_before_admission() { let (mut actor, _) = TransportActor::new(); let (mut entry, mut rx) = make_test_interface("radio"); entry.packet_capacity = InterfacePacketCapacity::new(500, 508, false); - entry.ifac_key = Some([5; 64]); + entry.ifac_key = Some([5; 64].into()); entry.ifac_size = 8; actor.interfaces.insert(1, entry); let mut raw = make_data_packet([9; 16], 0).to_vec(); diff --git a/crates/rns-transport/src/messages.rs b/crates/rns-transport/src/messages.rs index 873aa41..2e5cc4a 100644 --- a/crates/rns-transport/src/messages.rs +++ b/crates/rns-transport/src/messages.rs @@ -252,7 +252,7 @@ pub struct InterfaceEntry { pub bitrate: u64, pub mtu: u32, pub tx: mpsc::Sender, - pub ifac_key: Option<[u8; 64]>, + pub ifac_key: Option, pub ifac_size: usize, pub announce_cap: f64, /// Earliest Unix time at which the next announce may be sent — enforces @@ -388,7 +388,7 @@ impl InterfaceEntry { } pub fn with_ifac(mut self, key: [u8; 64], size: usize) -> Self { - self.ifac_key = Some(key); + self.ifac_key = Some(key.into()); self.ifac_size = size; self } diff --git a/crates/rns-transport/tests/secret_residue.rs b/crates/rns-transport/tests/secret_residue.rs index 6b10d06..5710def 100644 --- a/crates/rns-transport/tests/secret_residue.rs +++ b/crates/rns-transport/tests/secret_residue.rs @@ -1,14 +1,18 @@ -//! IFAC masks, derived from the interface key, must not survive in freed heap -//! memory once a packet is signed or verified. +//! IFAC keys, and the masks derived from them, must not survive in freed heap +//! memory once a packet is signed or verified or an interface is dropped. #[path = "../../rns-crypto/tests/residue/mod.rs"] mod residue; +use std::collections::HashMap; + use residue::{assert_no_residue, freed_by, pseudo_random, serial}; use rns_crypto::ed25519::Ed25519PrivateKey; use rns_crypto::hkdf::hkdf_sha256; use rns_identity::ifac::derive_ifac_key; +use rns_transport::constants::{InterfaceDirection, InterfaceMode}; use rns_transport::ifac::{ifac_sign, ifac_verify}; +use rns_transport::messages::InterfaceEntry; const IFAC_SIZE: usize = 16; @@ -53,3 +57,30 @@ fn ifac_verify_frees_no_mask() { assert_no_residue(&freed, &[("IFAC mask", &mask)]); assert_eq!(verified.unwrap(), packet); } + +#[test] +fn an_interface_table_frees_no_ifac_keys() { + let _serial = serial(); + let keys: Vec<[u8; 64]> = (0..48).map(|seed| pseudo_random(200 + seed)).collect(); + let (tx, _rx) = tokio::sync::mpsc::channel(1); + + // Growing the table moves every entry, so each key is moved several times. + let ((), freed) = freed_by(|| { + let mut interfaces = HashMap::new(); + for (id, key) in keys.iter().enumerate() { + let entry = InterfaceEntry::new( + format!("ifac-{id}"), + InterfaceMode::Full, + InterfaceDirection::bidirectional(), + 0, + 500, + tx.clone(), + ) + .with_ifac(*key, IFAC_SIZE); + interfaces.insert(id as u64, entry); + } + }); + + let secrets: Vec<(&str, &[u8])> = keys.iter().map(|key| ("IFAC key", &key[..])).collect(); + assert_no_residue(&freed, &secrets); +}