On 11/12/26 11:02, Peter Maydell wrote:
In net_checksum_calculate() we calculate the position of the TCP or
UDP header in the buffer with:
tcp_header *tcp = (tcp_header *)(ip + 1);
where ip is a 'struct ip_header' (and similarly for udp).
This only gives the correct location if the IP packet has no options
and its header is the minimum length. Otherwise we will corrupt some
part of the packet in writing the checksum word.
Fortunately packets with IP options are rare (and often get blocked
by routers or firewalls), so this probably didn't often cause
problems, and the incorrect offset we write to is always inside the
packet, not out-of-bounds.
Use the IP header length value from the packet to locate the TCP or
UDP header, instead of the fixed minimum size. While we're adjusting
this code, make sure that we don't assign to the tcp_header or
udp_header pointer until after we've made the length check that the
buffer is big enough (the previous ordering of operations was
harmless as we never used the resulting pointer, but not ideal).
This was mentioned in passing in issue #4791 (which is otherwise
a duplicate of #4720).
Cc: [email protected]
Fixes: 50dbce6538d1 ("net: improve UDP/TCP checksum computation.")
Reported-by: Alex Meiburg <https://gitlab.com/timeroot.alex>
Signed-off-by: Peter Maydell <[email protected]>
---
net/checksum.c | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/net/checksum.c b/net/checksum.c
index 997a0dd2350..277b2e652d9 100644
--- a/net/checksum.c
+++ b/net/checksum.c
@@ -62,6 +62,7 @@ void net_checksum_calculate(void *data, int length, int
csum_flag)
int mac_hdr_len, ip_len, ip_hdr_len;
struct ip_header *ip;
uint16_t csum;
+ uint8_t *ip_payload;
Why uint8_t*z and not void*?
With that...
/*
* Note: We cannot assume "data" is aligned, so the all code uses
@@ -142,20 +143,23 @@ void net_checksum_calculate(void *data, int length, int
csum_flag)
}
ip_len -= ip_hdr_len;
+ ip_payload = (uint8_t *)ip + ip_hdr_len;
switch (ip->ip_p) {
case IP_PROTO_TCP:
{
+ tcp_header *tcp;
+
if (!(csum_flag & CSUM_TCP)) {
return;
}
- tcp_header *tcp = (tcp_header *)(ip + 1);
-
if (ip_len < sizeof(tcp_header)) {
return;
}
+ tcp = (tcp_header *)ip_payload;
... no cast required.
This could also plausibly be reordered to
case IP_PROTO_TCP:
if ((csum & CSUM_TCP) && ip_len >= sizeof(tcg_header)) {
tcp_header *tcp = ip_payload;
...
}
r~