Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading only the needed TCP/UDP header bytes #1730

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions windows_kext/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ mod stream_callouts;

use wdk::allocator::WindowsAllocator;

// For constancy of development debug mode should not be used.
// Certain behavior of the compiler will change and this can result in errors and different behavior in debug and release mode.
#[cfg(debug_assertions)]
compile_error!("build in release mode");

#[cfg(not(test))]
use core::panic::PanicInfo;

Expand Down
5 changes: 1 addition & 4 deletions windows_kext/driver/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ use core::{
};
use protocol::info::{Info, Severity};

#[cfg(not(debug_assertions))]
pub const LOG_LEVEL: u8 = Severity::Warning as u8;

#[cfg(debug_assertions)]
pub const LOG_LEVEL: u8 = Severity::Trace as u8;
// pub const LOG_LEVEL: u8 = Severity::Trace as u8;

pub const MAX_LOG_LINE_SIZE: usize = 150;

Expand Down
13 changes: 7 additions & 6 deletions windows_kext/driver/src/packet_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ fn print_packet(packet: &[u8]) {
///
/// * `Ok(Key)` - A key containing the protocol, local and remote addresses and ports.
/// * `Err(String)` - An error message if the function fails to get net_buffer data.
const HEADERS_LEN: usize = smoltcp::wire::IPV4_HEADER_LEN + smoltcp::wire::TCP_HEADER_LEN;

fn get_ports(packet: &[u8], protocol: smoltcp::wire::IpProtocol) -> (u16, u16) {
match protocol {
smoltcp::wire::IpProtocol::Tcp => {
Expand All @@ -262,12 +260,13 @@ fn get_ports(packet: &[u8], protocol: smoltcp::wire::IpProtocol) -> (u16, u16) {
}

pub fn get_key_from_nbl_v4(nbl: &NetBufferList, direction: Direction) -> Result<Key, String> {
// Get bytes
let mut headers = [0; HEADERS_LEN];
// Get first bytes of the packet. IP header + src port (2 bytes) + dst port (2 bytes)
let mut headers = [0; smoltcp::wire::IPV4_HEADER_LEN + 4];
if nbl.read_bytes(&mut headers).is_err() {
return Err("failed to get net_buffer data".to_string());
}

// This will panic in debug mode, probably because of runtime checks.
// Parse packet
let ip_packet = Ipv4Packet::new_unchecked(&headers);
let (src_port, dst_port) = get_ports(
Expand Down Expand Up @@ -307,11 +306,13 @@ pub fn get_key_from_nbl_v4(nbl: &NetBufferList, direction: Direction) -> Result<
/// * `Ok(Key)` - A key containing the protocol, local and remote addresses and ports.
/// * `Err(String)` - An error message if the function fails to get net_buffer data.
pub fn get_key_from_nbl_v6(nbl: &NetBufferList, direction: Direction) -> Result<Key, String> {
// Get bytes
let mut headers = [0; smoltcp::wire::IPV6_HEADER_LEN + smoltcp::wire::TCP_HEADER_LEN];
// Get first bytes of the packet. IP header + src port (2 bytes) + dst port (2 bytes)
let mut headers = [0; smoltcp::wire::IPV6_HEADER_LEN + 4];
let Ok(()) = nbl.read_bytes(&mut headers) else {
return Err("failed to get net_buffer data".to_string());
};

// This will panic in debug mode, probably because of runtime checks.
// Parse packet
let ip_packet = Ipv6Packet::new_unchecked(&headers);
let (src_port, dst_port) = get_ports(
Expand Down
Loading