fix: harden ratchet sessions and throttle BitTorrent DHT traffic
- Ratchet: cap outbound NSR pending entries per peer and globally, age them out after 180s, and prune expired or excess sessions. - Ratchet: keep NSR pending entries across multi-NSR replies, skip failed decrypts instead of aborting, and bound inbound pending entries. - Ratchet: expire sessions with stale receive state or high message counts; only mark sessions confirmed when no reply has been received. - Torrent DHT: add per-node cooldowns for get_peers and announce, limit fanout and pending transactions, and skip nodes already in the routing table. - Torrent DHT: use slower maintenance and query intervals in public mode. - Torrent peers: mark ratchet sessions stale after stalled SYN, connect timeout, handshake timeout, or idle timeout so they re-key. - Torrent peers: raise the per-torrent peer limit to 16 and log I2CP decode errors in debug mode.
This commit is contained in:
1 parent
4bb81f1b80
commit
5d97ad8b28
4 files changed
+474
-71
No files matched your search
+159
-28
@@ -634,6 +634,7 @@ pub struct RatchetSessionManager {
|
||||
local_static : @noise.KeyPair
|
||||
sessions : Array[RatchetPeerSession]
|
||||
outbound_pending : Array[(Bytes, PendingNewSession)] // (nsr_tag_8, pending)
|
||||
priv outbound_pending_sec : Array[UInt32]
|
||||
inbound_pending : Array[PendingNewSession] // waiting to send NSR to remote_static_pub
|
||||
}
|
||||
|
||||
@@ -641,7 +642,13 @@ pub struct RatchetSessionManager {
|
||||
pub fn RatchetSessionManager::new(
|
||||
local_static : @noise.KeyPair,
|
||||
) -> RatchetSessionManager {
|
||||
{ local_static, sessions: [], outbound_pending: [], inbound_pending: [], }
|
||||
{
|
||||
local_static,
|
||||
sessions: [],
|
||||
outbound_pending: [],
|
||||
outbound_pending_sec: [],
|
||||
inbound_pending: [],
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -652,15 +659,129 @@ fn is_ratchet_session_expired(
|
||||
if now_sec == 0U || sess.last_used_sec == 0U {
|
||||
return false
|
||||
}
|
||||
if sess.send_tagset.msg_num >= 60000U {
|
||||
return true
|
||||
}
|
||||
if now_sec > sess.last_used_sec + 300U {
|
||||
return true
|
||||
}
|
||||
if !sess.confirmed && now_sec > sess.last_used_sec + 45U {
|
||||
return true
|
||||
}
|
||||
if sess.last_recv_sec > 0U &&
|
||||
!sess.confirmed &&
|
||||
now_sec > sess.last_recv_sec + 45U {
|
||||
return true
|
||||
}
|
||||
if sess.last_recv_sec > 0U && now_sec > sess.last_recv_sec + 180U {
|
||||
return true
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
///|
|
||||
fn RatchetSessionManager::remove_outbound_pending(
|
||||
self : RatchetSessionManager,
|
||||
idx : Int32,
|
||||
) -> Unit {
|
||||
let _ = self.outbound_pending.remove(idx)
|
||||
if idx < self.outbound_pending_sec.length() {
|
||||
let _ = self.outbound_pending_sec.remove(idx)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn RatchetSessionManager::prune_outbound_pending(
|
||||
self : RatchetSessionManager,
|
||||
now_sec : UInt32,
|
||||
) -> Unit {
|
||||
if now_sec == 0U {
|
||||
return
|
||||
}
|
||||
let mut i = 0
|
||||
while i < self.outbound_pending.length() {
|
||||
let ts = if i < self.outbound_pending_sec.length() {
|
||||
self.outbound_pending_sec[i]
|
||||
} else {
|
||||
0U
|
||||
}
|
||||
if ts > 0U && now_sec > ts + 180U {
|
||||
self.remove_outbound_pending(i)
|
||||
} else {
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn RatchetSessionManager::cap_peer_outbound_pending(
|
||||
self : RatchetSessionManager,
|
||||
remote_static_pub : Bytes,
|
||||
max_tags : Int32,
|
||||
) -> Unit {
|
||||
while true {
|
||||
let mut count = 0
|
||||
let mut oldest_idx = -1
|
||||
for i = 0; i < self.outbound_pending.length(); i = i + 1 {
|
||||
if @crypto.ct_eq(
|
||||
self.outbound_pending[i].1.remote_static_pub,
|
||||
remote_static_pub,
|
||||
) {
|
||||
count += 1
|
||||
if oldest_idx < 0 {
|
||||
oldest_idx = i
|
||||
}
|
||||
}
|
||||
}
|
||||
if count <= max_tags || oldest_idx < 0 {
|
||||
break
|
||||
}
|
||||
self.remove_outbound_pending(oldest_idx)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn RatchetSessionManager::push_outbound_pending(
|
||||
self : RatchetSessionManager,
|
||||
tag : Bytes,
|
||||
pending : PendingNewSession,
|
||||
now_sec : UInt32,
|
||||
) -> Unit {
|
||||
while self.outbound_pending.length() >= 2048 {
|
||||
self.remove_outbound_pending(0)
|
||||
}
|
||||
self.outbound_pending.push((tag, pending))
|
||||
self.outbound_pending_sec.push(now_sec)
|
||||
}
|
||||
|
||||
///|
|
||||
fn RatchetSessionManager::prune_expired_sessions(
|
||||
self : RatchetSessionManager,
|
||||
now_sec : UInt32,
|
||||
) -> Unit {
|
||||
if now_sec > 0U && self.sessions.length() > 128 {
|
||||
let mut i = 0
|
||||
while i < self.sessions.length() {
|
||||
if is_ratchet_session_expired(self.sessions[i], now_sec) {
|
||||
let _ = self.sessions.remove(i)
|
||||
} else {
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
while self.sessions.length() > 512 {
|
||||
let mut oldest_idx = 0
|
||||
let mut oldest_ts = self.sessions[0].last_used_sec
|
||||
for i = 1; i < self.sessions.length(); i = i + 1 {
|
||||
if self.sessions[i].last_used_sec < oldest_ts {
|
||||
oldest_ts = self.sessions[i].last_used_sec
|
||||
oldest_idx = i
|
||||
}
|
||||
}
|
||||
let _ = self.sessions.remove(oldest_idx)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn RatchetSessionManager::find_session(
|
||||
self : RatchetSessionManager,
|
||||
@@ -782,7 +903,7 @@ pub fn RatchetSessionManager::drop_session(
|
||||
self.outbound_pending[pi].1.remote_static_pub,
|
||||
remote_static_pub,
|
||||
) {
|
||||
let _ = self.outbound_pending.remove(pi)
|
||||
self.remove_outbound_pending(pi)
|
||||
} else {
|
||||
pi += 1
|
||||
}
|
||||
@@ -860,6 +981,7 @@ pub fn RatchetSessionManager::encrypt_to_peer(
|
||||
now_sec : UInt32,
|
||||
next_rand_32 : () -> Bytes,
|
||||
) -> Bytes raise RatchetError {
|
||||
self.prune_expired_sessions(now_sec)
|
||||
let mut ei = 0
|
||||
while ei < self.sessions.length() {
|
||||
let s = self.sessions[ei]
|
||||
@@ -875,7 +997,9 @@ pub fn RatchetSessionManager::encrypt_to_peer(
|
||||
let sess = self.sessions[sidx]
|
||||
sess.last_used_sec = now_sec
|
||||
sess.sent_es = true
|
||||
sess.confirmed = true
|
||||
if sess.last_recv_sec == 0U {
|
||||
sess.confirmed = true
|
||||
}
|
||||
return sess.encrypt_existing(blocks)
|
||||
}
|
||||
// Check if we owe this peer an NSR
|
||||
@@ -916,15 +1040,15 @@ pub fn RatchetSessionManager::encrypt_to_peer(
|
||||
ns_blocks,
|
||||
)
|
||||
match pending_opt {
|
||||
Some(p) =>
|
||||
Some(p) => {
|
||||
self.prune_outbound_pending(now_sec)
|
||||
// Pre-register first 6 NSR tags so any reply (or multi-NSR) matches
|
||||
for _k = 0; _k < 6; _k = _k + 1 {
|
||||
let (_n, tag, _k32) = p.nsr_tagset.next_entry()
|
||||
while self.outbound_pending.length() >= 128 {
|
||||
let _ = self.outbound_pending.remove(0)
|
||||
}
|
||||
self.outbound_pending.push((tag, p))
|
||||
self.push_outbound_pending(tag, p, now_sec)
|
||||
}
|
||||
self.cap_peer_outbound_pending(remote_static_pub, 24)
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
wire
|
||||
@@ -964,31 +1088,35 @@ pub fn RatchetSessionManager::decrypt_incoming(
|
||||
}
|
||||
si -= 1
|
||||
}
|
||||
// 2. New Session Reply lookup
|
||||
// 2. New Session Reply lookup (keep entries across multi-NSR until aged out)
|
||||
for i = 0; i < self.outbound_pending.length(); i = i + 1 {
|
||||
let (expected_tag, p) = self.outbound_pending[i]
|
||||
if @crypto.ct_eq(expected_tag, tag_8) {
|
||||
let (blocks, sess) = decrypt_new_session_reply(
|
||||
self.local_static,
|
||||
p,
|
||||
garlic_data,
|
||||
)
|
||||
let _ = self.outbound_pending.remove(i)
|
||||
sess.last_used_sec = now_sec
|
||||
sess.last_recv_sec = now_sec
|
||||
let mut already_has_active_es = false
|
||||
for s in self.sessions {
|
||||
if s.send_enabled &&
|
||||
s.sent_es &&
|
||||
@crypto.ct_eq(s.remote_static_pub, sess.remote_static_pub) {
|
||||
already_has_active_es = true
|
||||
break
|
||||
let nsr_res = Some(
|
||||
decrypt_new_session_reply(self.local_static, p, garlic_data),
|
||||
) catch {
|
||||
_ => None
|
||||
}
|
||||
match nsr_res {
|
||||
Some((blocks, sess)) => {
|
||||
sess.last_used_sec = now_sec
|
||||
sess.last_recv_sec = now_sec
|
||||
let mut already_has_active_es = false
|
||||
for s in self.sessions {
|
||||
if s.send_enabled &&
|
||||
s.sent_es &&
|
||||
@crypto.ct_eq(s.remote_static_pub, sess.remote_static_pub) {
|
||||
already_has_active_es = true
|
||||
break
|
||||
}
|
||||
}
|
||||
sess.send_enabled = !already_has_active_es
|
||||
self.sessions.push(sess)
|
||||
self.cap_peer_sessions(sess.remote_static_pub, 4)
|
||||
return (Some(sess.remote_static_pub), blocks)
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
sess.send_enabled = !already_has_active_es
|
||||
self.sessions.push(sess)
|
||||
self.cap_peer_sessions(sess.remote_static_pub, 4)
|
||||
return (Some(sess.remote_static_pub), blocks)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1011,6 +1139,9 @@ pub fn RatchetSessionManager::decrypt_incoming(
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
while self.inbound_pending.length() >= 256 {
|
||||
let _ = self.inbound_pending.remove(0)
|
||||
}
|
||||
self.inbound_pending.push(p)
|
||||
(Some(p.remote_static_pub), blocks)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user