fix memory safety: use-after-free in DHT, buffer overflows in TURN,
integer truncation
This commit is contained in:
@@ -134,7 +134,9 @@ pub const SignedDhtMessage = struct {
|
||||
|
||||
/// Create signing data from message components
|
||||
pub fn createSigningData(allocator: Allocator, request_id: u64, sender_id: NodeId, msg_type: u8, payload: []const u8, timestamp: i64) ![]u8 {
|
||||
const data = try allocator.alloc(u8, 8 + 32 + 1 + payload.len + 8);
|
||||
// SECURITY: Prevent integer overflow in allocation size calculation
|
||||
const total_size = std.math.add(usize, 49, payload.len) catch return error.OutOfMemory;
|
||||
const data = try allocator.alloc(u8, total_size);
|
||||
var offset: usize = 0;
|
||||
|
||||
// Request ID
|
||||
@@ -220,7 +222,9 @@ pub const DhtStore = struct {
|
||||
|
||||
/// Create data to sign for value ownership proof
|
||||
pub fn createValueSigningData(allocator: Allocator, key: [32]u8, value: []const u8, ttl: u32) ![]u8 {
|
||||
const data = try allocator.alloc(u8, 32 + value.len + 4);
|
||||
// SECURITY: Prevent integer overflow in allocation size calculation
|
||||
const total_size = std.math.add(usize, 36, value.len) catch return error.OutOfMemory;
|
||||
const data = try allocator.alloc(u8, total_size);
|
||||
@memcpy(data[0..32], &key);
|
||||
@memcpy(data[32..][0..value.len], value);
|
||||
std.mem.writeInt(u32, data[32 + value.len ..][0..4], ttl, .little);
|
||||
@@ -1143,18 +1147,24 @@ pub const DhtCoordinator = struct {
|
||||
}
|
||||
|
||||
const state = entry.?.value;
|
||||
|
||||
if (!state.done) {
|
||||
// Not done yet - put it back (do NOT destroy)
|
||||
self.pending_lookups.put(lookup_id, state) catch {
|
||||
// Can't reinsert - must clean up to avoid leak
|
||||
state.deinit();
|
||||
self.allocator.destroy(state);
|
||||
return error.OutOfMemory;
|
||||
};
|
||||
return null;
|
||||
}
|
||||
|
||||
// Lookup is complete - extract results then destroy state
|
||||
defer {
|
||||
state.deinit();
|
||||
self.allocator.destroy(state);
|
||||
}
|
||||
|
||||
if (!state.done) {
|
||||
// Not done yet - put it back
|
||||
try self.pending_lookups.put(lookup_id, state);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Sort by distance and return K closest
|
||||
const target_peer_id = PeerId{ .bytes = state.target };
|
||||
std.mem.sort(NodeId, state.closest_seen.items, target_peer_id, compareNodeDistance);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user