Skip to content

Commit

Permalink
fix: gracefully handle invalid UDP packet (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
XOR-op authored Feb 8, 2024
2 parents 0e7b2d5 + 5a4e7e8 commit 78238c8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions boltconn/src/network/packet/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ impl IPPkt {
}
}

pub fn ip_header_len(&self) -> usize {
match self {
IPPkt::V4(_) => Ipv4Packet::new_unchecked(self.packet_data()).header_len() as usize,
IPPkt::V6(_) => Ipv6Packet::new_unchecked(self.packet_data()).header_len(),
}
}

pub fn pkt_total_len(&self) -> usize {
match self {
IPPkt::V4(_) => Ipv4Packet::new_unchecked(self.packet_data()).total_len() as usize,
Expand Down
4 changes: 4 additions & 0 deletions boltconn/src/network/tun_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ impl TunDevice {
}
}
IpProtocol::Udp => {
if pkt.pkt_total_len() < pkt.ip_header_len() + 8 {
// drop invalid UDP packet
return;
}
let pkt = UdpPkt::new(pkt);
if pkt.dst_port() == 53 && dst == self.fake_dns_addr {
// fake ip
Expand Down

0 comments on commit 78238c8

Please sign in to comment.