network reliability and security hardening. auto-cleanup expired backoff
entries, atomic file writes for key pinning, forward secrecy via rekeying, peer eviction when at max connections, NAT-PMP source address verification, UPnP response timeout
This commit is contained in:
@@ -137,6 +137,12 @@ pub const NatPmp = struct {
|
||||
return null;
|
||||
};
|
||||
|
||||
// SECURITY: Verify response came from our gateway (prevents LAN spoofing)
|
||||
if (!verifySourceAddress(&src_addr, self.gateway_ip)) {
|
||||
std.log.warn("NAT-PMP: Response from unexpected source, ignoring (possible spoofing)", .{});
|
||||
return null;
|
||||
}
|
||||
|
||||
if (recv_len < 12) {
|
||||
std.log.warn("NAT-PMP: Short response ({} bytes)", .{recv_len});
|
||||
return null;
|
||||
@@ -217,6 +223,12 @@ pub const NatPmp = struct {
|
||||
return null;
|
||||
};
|
||||
|
||||
// SECURITY: Verify response came from our gateway (prevents LAN spoofing)
|
||||
if (!verifySourceAddress(&src_addr, self.gateway_ip)) {
|
||||
std.log.warn("NAT-PMP: Response from unexpected source, ignoring (possible spoofing)", .{});
|
||||
return null;
|
||||
}
|
||||
|
||||
if (recv_len < 16) {
|
||||
std.log.warn("NAT-PMP: Short response ({} bytes)", .{recv_len});
|
||||
return null;
|
||||
@@ -296,6 +308,15 @@ pub const NatPmp = struct {
|
||||
}
|
||||
};
|
||||
|
||||
/// Verify that a received UDP response came from the expected gateway IP.
|
||||
/// Prevents LAN-based spoofing of NAT-PMP responses.
|
||||
fn verifySourceAddress(src: *const posix.sockaddr.storage, expected_ip: [4]u8) bool {
|
||||
if (src.family != posix.AF.INET) return false;
|
||||
const in_addr: *const posix.sockaddr.in = @ptrCast(src);
|
||||
const src_ip: [4]u8 = @bitCast(in_addr.addr);
|
||||
return std.mem.eql(u8, &src_ip, &expected_ip);
|
||||
}
|
||||
|
||||
/// Discover the default gateway IP address
|
||||
pub fn discoverGateway() ![4]u8 {
|
||||
// Platform-specific gateway discovery
|
||||
@@ -350,19 +371,21 @@ fn discoverGatewayDarwin() ![4]u8 {
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: assume common gateway
|
||||
return .{ 192, 168, 1, 1 };
|
||||
// No gateway responded — return error instead of guessing
|
||||
std.log.warn("NAT-PMP: No gateway responded to probes, gateway discovery failed", .{});
|
||||
return error.GatewayNotFound;
|
||||
}
|
||||
|
||||
fn discoverGatewayLinux() ![4]u8 {
|
||||
// On Linux, read /proc/net/route
|
||||
const file = std.fs.openFileAbsolute("/proc/net/route", .{}) catch {
|
||||
return .{ 192, 168, 1, 1 }; // Fallback
|
||||
std.log.warn("NAT-PMP: Cannot open /proc/net/route, gateway discovery failed", .{});
|
||||
return error.GatewayNotFound;
|
||||
};
|
||||
defer file.close();
|
||||
|
||||
var buf: [4096]u8 = undefined;
|
||||
const bytes_read = file.readAll(&buf) catch return .{ 192, 168, 1, 1 };
|
||||
const bytes_read = file.readAll(&buf) catch return error.GatewayNotFound;
|
||||
|
||||
var lines = std.mem.splitScalar(u8, buf[0..bytes_read], '\n');
|
||||
_ = lines.next(); // Skip header
|
||||
@@ -386,7 +409,8 @@ fn discoverGatewayLinux() ![4]u8 {
|
||||
}
|
||||
}
|
||||
|
||||
return .{ 192, 168, 1, 1 }; // Fallback
|
||||
std.log.warn("NAT-PMP: No default route found in /proc/net/route, gateway discovery failed", .{});
|
||||
return error.GatewayNotFound;
|
||||
}
|
||||
|
||||
// Tests
|
||||
|
||||
Reference in New Issue
Block a user