Skip to content

Commit

Permalink
Kernel: Truncate UDP packets on read
Browse files Browse the repository at this point in the history
When reading UDP packets from userspace with recvmsg()/recv() we
would hit a VERIFY() if the supplied buffer is smaller than the
received UDP packet. Instead we should just return truncated data
to the caller.

This can be reproduced with:

    $ dd if=/dev/zero bs=1k count=1 | nc -u 192.168.3.190 68
  • Loading branch information
gunnarbeutner authored and awesomekling committed May 6, 2021
1 parent 928f16d commit 9213d1e
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Kernel/Net/UDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ KResultOr<size_t> UDPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, Use
auto& ipv4_packet = *(const IPv4Packet*)(raw_ipv4_packet.data());
auto& udp_packet = *static_cast<const UDPPacket*>(ipv4_packet.payload());
VERIFY(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier.
VERIFY(buffer_size >= (udp_packet.length() - sizeof(UDPPacket)));
if (!buffer.write(udp_packet.payload(), udp_packet.length() - sizeof(UDPPacket)))
size_t read_size = min(buffer_size, udp_packet.length() - sizeof(UDPPacket));
if (!buffer.write(udp_packet.payload(), read_size))
return EFAULT;
return udp_packet.length() - sizeof(UDPPacket);
return read_size;
}

KResultOr<size_t> UDPSocket::protocol_send(const UserOrKernelBuffer& data, size_t data_length)
Expand Down

0 comments on commit 9213d1e

Please sign in to comment.