diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-05-06 08:27:51 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-06 08:49:35 +0200 |
commit | 9213d1e92686ad650aa093e7e853b3996c926779 (patch) | |
tree | f0227320c4f6cbcf0b0f7bfbedb5de9a7f038a5c /Kernel | |
parent | 928f16d3608138480def073188210e7c3b6790a2 (diff) | |
download | serenity-9213d1e92686ad650aa093e7e853b3996c926779.zip |
Kernel: Truncate UDP packets on read
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
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/Net/UDPSocket.cpp | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/Kernel/Net/UDPSocket.cpp b/Kernel/Net/UDPSocket.cpp index 70a96e5066..fb08f20180 100644 --- a/Kernel/Net/UDPSocket.cpp +++ b/Kernel/Net/UDPSocket.cpp @@ -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) |