From 10f2ef426e2042a042de88531042e52175a20e35 Mon Sep 17 00:00:00 2001 From: Vladimir Stoilov Date: Mon, 9 Jun 2025 20:11:26 +0300 Subject: [PATCH] [kext] Resolve verdict of pending conn on shutdown --- windows_kext/driver/src/device.rs | 14 +++++++++++++- windows_kext/driver/src/id_cache.rs | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/windows_kext/driver/src/device.rs b/windows_kext/driver/src/device.rs index 801b7a98..bc1fafec 100644 --- a/windows_kext/driver/src/device.rs +++ b/windows_kext/driver/src/device.rs @@ -296,9 +296,21 @@ impl Device { } } - pub fn shutdown(&self) { + pub fn shutdown(&mut self) { // End blocking operations from the queue. This will end pending read requests. self.event_queue.rundown(); + + // Resolve all pending packets. This is important for proper driver unload. + let pending_packets = self.packet_cache.pop_all(); + for el in pending_packets { + let key = el.value.0; + let packet = el.value.1; + // Set any verdict. Driver will unload after that and the filter will not be active. + _ = self + .connection_cache + .update_connection(key, crate::connection::Verdict::PermanentBlock); + _ = self.inject_packet(packet, true); // Blocked must be set, so it only handles the ALE layer. + } } pub fn inject_packet(&mut self, packet: Packet, blocked: bool) -> Result<(), String> { diff --git a/windows_kext/driver/src/id_cache.rs b/windows_kext/driver/src/id_cache.rs index 9a0c4b27..e8d4f509 100644 --- a/windows_kext/driver/src/id_cache.rs +++ b/windows_kext/driver/src/id_cache.rs @@ -1,3 +1,5 @@ +use core::mem; + use alloc::collections::VecDeque; use protocol::info::Info; use smoltcp::wire::{IpAddress, IpProtocol}; @@ -5,8 +7,8 @@ use wdk::rw_spin_lock::RwSpinLock; use crate::{connection::Direction, connection_map::Key, device::Packet}; -struct Entry { - value: T, +pub struct Entry { + pub value: T, id: u64, } @@ -54,6 +56,14 @@ impl IdCache { let _guard = self.lock.read_lock(); return self.values.len(); } + + pub fn pop_all(&mut self) -> VecDeque> { + let mut values = VecDeque::with_capacity(1); + let _guard = self.lock.write_lock(); + mem::swap(&mut self.values, &mut values); + + return values; + } } fn get_payload(packet: &Packet) -> Option<&[u8]> {