fix correctness: IPv6 :: parsing, AF_INET6 on macOS, health check units,
DHT heap fallback
This commit is contained in:
@@ -21,6 +21,12 @@ const protocol = @import("protocol");
|
||||
// Import P2P type from sibling module
|
||||
const P2P = @import("p2p.zig").P2P;
|
||||
|
||||
// SECURITY: Limits for DHT message deserialization to prevent DoS
|
||||
/// Maximum number of nodes in a single DHT response (matches k_bucket_size)
|
||||
const MAX_DHT_NODES_PER_RESPONSE: u32 = 20;
|
||||
/// Maximum size of a DHT value in bytes (1 MB)
|
||||
const MAX_DHT_VALUE_SIZE: u32 = 1024 * 1024;
|
||||
|
||||
// =========================================================================
|
||||
// DHT (Distributed Hash Table) API
|
||||
// =========================================================================
|
||||
@@ -263,6 +269,23 @@ pub const DhtMsgType = enum(u8) {
|
||||
find_value_response = 0x13,
|
||||
};
|
||||
|
||||
/// Compute the serialized size of a DHT message (for heap allocation when stack buffer is too small)
|
||||
fn dhtMessageSize(msg: protocol.message.Message) ?usize {
|
||||
const node_size = 32 + 46 + 1;
|
||||
return switch (msg) {
|
||||
.dht_find_node => |m| 1 + 8 + 32 + 32 + 1 + @as(usize, @min(m.target_observed_addr_len, 18)),
|
||||
.dht_store => |m| 1 + 8 + 32 + 32 + 4 + 4 + m.value.len + 1 + @as(usize, @min(m.target_observed_addr_len, 18)),
|
||||
.dht_find_value => |m| 1 + 8 + 32 + 32 + 1 + @as(usize, @min(m.target_observed_addr_len, 18)),
|
||||
.dht_find_node_response => |m| 1 + 8 + 4 + (m.closest_nodes.len * node_size) + 1 + @as(usize, @min(m.observed_addr_len, 18)),
|
||||
.dht_store_response => |m| 1 + 8 + 1 + 1 + @as(usize, @min(m.observed_addr_len, 18)),
|
||||
.dht_find_value_response => |m| blk: {
|
||||
const value_len = if (m.value) |v| v.len else 0;
|
||||
break :blk 1 + 8 + 1 + 4 + value_len + 4 + (m.closest_nodes.len * node_size) + 1 + @as(usize, @min(m.observed_addr_len, 18));
|
||||
},
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
|
||||
/// Serialize a DHT message to simple binary format
|
||||
/// Format: [1 byte type][8 bytes request_id][32 bytes sender_id][payload...]
|
||||
/// Public for testing IP Reflection variable-length encoding
|
||||
@@ -491,6 +514,8 @@ pub fn deserializeDhtMessage(data: []const u8, allocator: std.mem.Allocator) ?pr
|
||||
offset += 4;
|
||||
const value_len = std.mem.readInt(u32, data[offset..][0..4], .big);
|
||||
offset += 4;
|
||||
// SECURITY: Reject excessive value sizes to prevent DoS
|
||||
if (value_len > MAX_DHT_VALUE_SIZE) return null;
|
||||
if (data.len < offset + value_len) return null;
|
||||
// Allocate and copy value
|
||||
const value = allocator.dupe(u8, data[offset..][0..value_len]) catch return null;
|
||||
@@ -559,8 +584,12 @@ pub fn deserializeDhtMessage(data: []const u8, allocator: std.mem.Allocator) ?pr
|
||||
offset += 8;
|
||||
const node_count = std.mem.readInt(u32, data[offset..][0..4], .big);
|
||||
offset += 4;
|
||||
// SECURITY: Reject excessive node counts to prevent DoS
|
||||
if (node_count > MAX_DHT_NODES_PER_RESPONSE) return null;
|
||||
const node_size = 32 + 46 + 1;
|
||||
if (data.len < offset + (node_count * node_size) + 1) return null;
|
||||
// SECURITY: Use checked multiplication to prevent integer overflow
|
||||
const total_node_bytes = std.math.mul(usize, node_count, node_size) catch return null;
|
||||
if (data.len < offset + total_node_bytes + 1) return null;
|
||||
// Allocate nodes array
|
||||
const nodes = allocator.alloc(protocol.message.DhtFindNodeResponse.NodeInfo, node_count) catch return null;
|
||||
for (0..node_count) |i| {
|
||||
@@ -625,6 +654,8 @@ pub fn deserializeDhtMessage(data: []const u8, allocator: std.mem.Allocator) ?pr
|
||||
offset += 1;
|
||||
const value_len = std.mem.readInt(u32, data[offset..][0..4], .big);
|
||||
offset += 4;
|
||||
// SECURITY: Reject excessive value sizes to prevent DoS
|
||||
if (value_len > MAX_DHT_VALUE_SIZE) return null;
|
||||
if (data.len < offset + value_len + 4 + 1) return null;
|
||||
// Read value (may be empty)
|
||||
const value: ?[]const u8 = if (value_len > 0) blk: {
|
||||
@@ -634,10 +665,26 @@ pub fn deserializeDhtMessage(data: []const u8, allocator: std.mem.Allocator) ?pr
|
||||
offset += value_len;
|
||||
const node_count = std.mem.readInt(u32, data[offset..][0..4], .big);
|
||||
offset += 4;
|
||||
// SECURITY: Reject excessive node counts to prevent DoS
|
||||
if (node_count > MAX_DHT_NODES_PER_RESPONSE) {
|
||||
if (value) |v| allocator.free(v);
|
||||
return null;
|
||||
}
|
||||
const node_size = 32 + 46 + 1;
|
||||
if (data.len < offset + (node_count * node_size) + 1) return null;
|
||||
// SECURITY: Use checked multiplication to prevent integer overflow
|
||||
const total_node_bytes = std.math.mul(usize, node_count, node_size) catch {
|
||||
if (value) |v| allocator.free(v);
|
||||
return null;
|
||||
};
|
||||
if (data.len < offset + total_node_bytes + 1) {
|
||||
if (value) |v| allocator.free(v);
|
||||
return null;
|
||||
}
|
||||
// Allocate nodes array
|
||||
const nodes = allocator.alloc(protocol.message.DhtFindNodeResponse.NodeInfo, node_count) catch return null;
|
||||
const nodes = allocator.alloc(protocol.message.DhtFindNodeResponse.NodeInfo, node_count) catch {
|
||||
if (value) |v| allocator.free(v);
|
||||
return null;
|
||||
};
|
||||
for (0..node_count) |i| {
|
||||
nodes[i] = .{
|
||||
.node_id = data[offset..][0..32].*,
|
||||
@@ -713,8 +760,27 @@ pub fn sendDhtMessageImpl(p2p: *P2P, dht_node_id: [32]u8, msg: protocol.message.
|
||||
};
|
||||
|
||||
// Use simple binary serialization (no HMAC - Noise transport provides security)
|
||||
var buffer: [4096]u8 = undefined;
|
||||
const len = serializeDhtMessage(msg, &buffer) orelse {
|
||||
// Use stack buffer for small messages, heap for large (e.g. dht_store with big values)
|
||||
var stack_buffer: [4096]u8 = undefined;
|
||||
var heap_buffer: ?[]u8 = null;
|
||||
defer if (heap_buffer) |hb| p2p.allocator.free(hb);
|
||||
|
||||
var buffer: []u8 = &stack_buffer;
|
||||
|
||||
// Try stack buffer first; if too small, compute needed size and heap-allocate
|
||||
if (serializeDhtMessage(msg, buffer) == null) {
|
||||
const needed = dhtMessageSize(msg) orelse {
|
||||
std.log.warn("DHT: Failed to compute message size", .{});
|
||||
return false;
|
||||
};
|
||||
heap_buffer = p2p.allocator.alloc(u8, needed) catch {
|
||||
std.log.warn("DHT: Failed to allocate {d} bytes for message", .{needed});
|
||||
return false;
|
||||
};
|
||||
buffer = heap_buffer.?;
|
||||
}
|
||||
|
||||
const len = serializeDhtMessage(msg, buffer) orelse {
|
||||
std.log.warn("DHT: Failed to serialize message", .{});
|
||||
return false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user