IPv4: Truncate raw socket reads past buffer length

In addition to being the proper POSIX etiquette, it seems like a bad idea
for issues like the one seen in #3428 to result in a kernel crash. This patch
replaces the current behavior of failing on insufficient buffer size to truncating
SOCK_RAW messages to the buffer size. This will have to change if/when MSG_PEEK
is implemented, but for now this behavior is more compliant and logical than
just bailing.
This commit is contained in:
Avery 2020-09-09 22:12:50 -06:00 committed by Andreas Kling
parent 61060c0da8
commit 06218a4074
Notes: sideshowbarker 2024-07-19 02:47:44 +09:00

View file

@ -338,9 +338,9 @@ KResultOr<size_t> IPv4Socket::receive_packet_buffered(FileDescription& descripti
}
if (type() == SOCK_RAW) {
ASSERT(buffer_length >= ipv4_packet.payload_size());
memcpy(buffer, ipv4_packet.payload(), ipv4_packet.payload_size());
return ipv4_packet.payload_size();
size_t bytes_written = min((size_t) ipv4_packet.payload_size(), buffer_length);
memcpy(buffer, ipv4_packet.payload(), bytes_written);
return bytes_written;
}
return protocol_receive(packet.data.value(), buffer, buffer_length, flags);