net, hw/net: Harden common code against bogus offsets
cover net, hw/net: Harden common code against bogus offsets
This patchset is a set of patches which try to make the common code in net/eth.c and hw/net/net_{tx,rx}_pkt.c a bit more robust against malicious packets with bogus offsets and similar problems. In theory if other parts of the code are correctly checking offsets and sizes to ensure they're within the packet before relying on them, this should make no difference. But a defence-in-depth approach seems like a good idea.
Two things in this are fixing demonstrable bugs:
* the "igb" network device's use of the SCTP checksum validation function allowed a malicious packet to trigger a read off the end of an iovec, likely triggering a segfault (patch 1, issue #4138)
* net_tx_pkt_build_vheader() could overflow a 16-bit field when calculating the virt header length, and net_tx_tcp_fragment_init() would trust that and attempt an extremely large allocation as a result, triggering an abort() (patches 8 and 9, issue #3648)
Neither of those are security issues from QEMU's point of view, because "igb" isn't covered under the virtualization use case and "guest root can trigger self-denial-of-service" is also excluded.
The rest are just sanity-checking, mostly based on suggestions from issue #4156.
The disclaimer here is that this hasn't had much testing beyond "make check" and "make check-functional" plus "make sure virtio-net isn't obviously completely broken", which I am pretty sure do not exercise much of this code at all, especially not the stuff about L3 and L4 and segment-offload. So I'm trusting the net maintainers to have a more extensive testsuite.
thanks
-- PMM
Peter Maydell (11):
hw/net/net_rx_pkt: Handle bogus l4hdr_off when validating SCTP
checksum
net/eth.c: In eth_parse_ipv6_hdr(), fix check of input size net/eth.c: In eth_get_protocols(), don't report SCTP header off end of
packet
net/eth.c: Check for undersized IPv4 headers in eth_get_protocols() net/eth.c: Harden _eth_tcp_has_data() against short lengths net/eth.c: Tighten size checks in eth_get_l3_proto() include/net/eth.h: Make eth_get_l2_hdr_length_iov() check protocol hw/net/net_tx_pkt: Sanity check GSO headers in
net_tx_pkt_build_vheader()
hw/net/net_tx_pkt: Sanity check virt hdr len in
net_tx_tcp_fragment_init
hw/net/net_tx_pkt: Sanity check L2+L3 header len in
net_tx_pkt_parse_headers()
hw/net/net_rx_pkt: Check for overflow in _net_rx_pkt_calc_l4_csum()
include/net/eth.h | 4 +++- hw/net/net_rx_pkt.c | 22 ++++++++++++++-------- hw/net/net_tx_pkt.c | 20 ++++++++++++++++++-- net/eth.c | 39 ++++++++++++++++++++++++++++++--------- 4 files changed, 65 insertions(+), 20 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/11 hw/net/net_rx_pkt: Handle bogus l4hdr_off when validating SCTP checksum
In _net_rx_pkt_validate_sctp_sum() we calculate a checksum that starts at the NetRxPkt::l4hdr_off offset. If this offset (which might be guest controlled) is larger than the size of the packet, the loop that walks through the iovec can walk off the end of it. The resulting out-of-bounds read in attempting to convert the iov to a buffer might cause a segfault.
We also weren't correctly checking the return values of iov_to_buf() and iov_from_buf() when reading and writing the checksum word: if the l4hdr_off claims the checksum word is half off the end of the packet these functions will return the count of how much data they were able to access, and we should fail if they don't write the entire 4 byte word.
Return "checksum not valid" if the l4hdr_off is too large or the iov accesses don't do the full read/write.
Note that this affects only the igb network device; the other user of this function (e1000e) does its own check on the l4 header and won't calculate a checksum if it's out of range.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4138
--- hw/net/net_rx_pkt.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
hw/net/net_rx_pkt.c +8 -4
@@ -549,19 +549,23 @@ _net_rx_pkt_validate_sctp_sum(struct NetRxPkt *pkt)uint32_t original;bool valid; -for (vec = pkt->vec; vec->iov_len < off; vec++) { +for (vec = pkt->vec; vec_len >0 && vec->iov_len < off; vec++) { off -= vec->iov_len; vec_len--; } +if (vec_len ==0 ) { +return false; + } csum_off = off +8 ; -if (!iov_to_buf (vec, vec_len, csum_off, &original,sizeof (original))) { +if (iov_to_buf (vec, vec_len, csum_off, + &original,sizeof (original)) !=sizeof (original)) {return false; } -if (!iov_from_buf (vec, vec_len, csum_off, - &calculated,sizeof (calculated))) { +if (iov_from_buf (vec, vec_len, csum_off, + &calculated,sizeof (calculated)) !=sizeof (calculated)) {return false; }
Signature · 1 line
2.43.0
PATCH 2/11 net/eth.c: In eth_parse_ipv6_hdr(), fix check of input size
In reply to Peter MaydellIn the eth_parse_ipv6_hdr() function, when parsing the extension headers we check
if (input_size < ip6hdr_off + info->full_hdr_len) {
return false;
}This is checking that an extension header did not specify an extension header length that claims to extend off the end of the actual packet. However, we do it at the wrong end of the loop:
* In the first iteration, full_hdr_len is sizeof(struct ipv6_header) (i.e. just the initial fixed IPv6 header); but this check is redundant because the initial part of the function did an iov_to_buf() of sizeof(ip6_hdr) bytes from offset ip6hdr_off, and checked that it read that many bytes. If input_size was small enough to fail the check here then the iov_to_buf() would have reported a short read
* In the last iteration, we update full_hdr_len at the end of the loop, and then exit the loop without checking that that final extension header had a valid length
Moving the length check to the end of the loop fixes this.
This bug has no visible effects provided that all the callers validate accessses into the packet before doing them, but spotting the invalid headers at this level provides a defence in depth against bugs elsewhere; so it's cc'd to stable on that basis.
Bugs #4138 and #4516 both note this mistake (in amongst a pile of other things).
--- net/eth.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/eth.c +4 -4
@@ -485,10 +485,6 @@ bool eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags, info->has_ext_hdrs = true; do { - if (input_size < ip6hdr_off + info->full_hdr_len) { - return false; - } - bytes_read = iov_to_buf(pkt, pkt_frags, ip6hdr_off + info->full_hdr_len, &ext_hdr, sizeof(ext_hdr));
@@ -514,6 +510,10 @@ bool eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags, info->full_hdr_len += (ext_hdr.ip6r_len +1 ) *IP6_EXT_GRANULARITY ; curr_ext_hdr_type = ext_hdr.ip6r_nxt; +if (input_size < ip6hdr_off + info->full_hdr_len) { +/* This extension header's length field was bogus */ +return false; + } }while (eth_is_ip6_extension_header_type (curr_ext_hdr_type)); info->l4proto = ext_hdr.ip6r_nxt;
Signature · 1 line
2.43.0
Re: [PATCH 02/11] net/eth.c: In eth_parse_ipv6_hdr(), fix check of input size
Quoted 51 lines · Peter Maydell
In the
eth_parse_ipv6_hdr()function, when parsing the extension headers we checkif (input_size < ip6hdr_off + info->full_hdr_len) { return false; }This is checking that an extension header did not specify an extension header length that claims to extend off the end of the actual packet. However, we do it at the wrong end of the loop:
* In the first iteration,
full_hdr_lenissizeof(struct ipv6_header)(i.e. just the initial fixed IPv6 header); but this check is redundant because the initial part of the function did aniov_to_buf()ofsizeof(ip6_hdr)bytes from offsetip6hdr_off, and checked that it read that many bytes. Ifinput_sizewas small enough to fail the check here then theiov_to_buf()would have reported a short read
* In the last iteration, we updatefull_hdr_lenat the end of the loop, and then exit the loop without checking that that final extension header had a valid lengthMoving the length check to the end of the loop fixes this.
This bug has no visible effects provided that all the callers validate accessses into the packet before doing them, but spotting the invalid headers at this level provides a defence in depth against bugs elsewhere; so it's cc'd to stable on that basis.
Bugs #4138 and #4516 both note this mistake (in amongst a pile of other things).
Fixes eb700029c78367 ("net_pkt: Extend packet abstraction as required by e1000e functionality")1 file changed, 4 insertions(+), 4 deletions(-)net/eth.c +0
-4@@ -485,10 +485,6 @@ bool eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags, info->has_ext_hdrs = true; do { - if (input_size < ip6hdr_off + info->full_hdr_len) { - return false; - } - bytes_read = iov_to_buf(pkt, pkt_frags, ip6hdr_off + info->full_hdr_len, &ext_hdr, sizeof(ext_hdr));
net/eth.c bool eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags, · line 519
Quoted 10 lines
net/eth.c +4
-0@@ -514,6 +510,10 @@ bool eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags, info->full_hdr_len += (ext_hdr.ip6r_len +1 ) *IP6_EXT_GRANULARITY ; curr_ext_hdr_type = ext_hdr.ip6r_nxt; +if (input_size < ip6hdr_off + info->full_hdr_len) { +/* This extension header's length field was bogus */ +return false; + } }while (eth_is_ip6_extension_header_type (curr_ext_hdr_type)); info->l4proto = ext_hdr.ip6r_nxt;
This is a duplicate of [1], which also contains additional changes to avoid rejecting a valid packet with the check moved, so please review it.
[1] https://lore.kernel.org/qemu-devel/[email protected]/
Regards,
Akihiko Odaki
PATCH 3/11 net/eth.c: In eth_get_protocols(), don't report SCTP header off end of packet
In reply to Peter Maydelleth_get_protocols() parses an ethernet packet and tells the caller about various offsets into it and other properties of the packet. For the L4 protocol, in both the IP_PROTO_TCP and IP_PROTO_UDP branches we implicitly check that the packet does contain a whole TCP or UDP header at the l4hdr_off offset, because _eth_copy_chunk() will fail if the input packet size isn't big enough for the whole header we are copying out of it. But for IP_PROTO_SCTP we don't copy the SCTP common header. In this case we should still do the length check, so we don't set l4hdr_info->proto to say there's an SCTP header present when there isn't.
This shouldn't have any visible effects if the downstream consumers of the l4hdr_info are conservative about checking before they access into the packet, but checking here too adds defence in depth, hence the cc to stable.
--- include/net/eth.h | 2 ++ net/eth.c | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-)
include/net/eth.h +2 -0
@@ -394,6 +394,8 @@ typedef struct eth_l4_hdr_info_st {bool has_tcp_data; } eth_l4_hdr_info; +#define SCTP_COMMON_HEADER_LEN 12 + void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff, bool *hasip4, bool *hasip6, size_t *l3hdr_off,
net/eth.c +8 -1
@@ -213,7 +213,14 @@ void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff,break ;case IP_PROTO_SCTP : - l4hdr_info->proto =ETH_L4_HDR_PROTO_SCTP ; +/* +* We don't copy the SCTP common header into l4hdr_info, but +* don't report SCTP as present if the packet isn't big enough +* to actually have an SCTP header. +*/ +if (*l4hdr_off > input_size -SCTP_COMMON_HEADER_LEN ) { + l4hdr_info->proto =ETH_L4_HDR_PROTO_SCTP ; + }break ; } }
Signature · 1 line
2.43.0
Re: [PATCH 03/11] net/eth.c: In eth_get_protocols(), don't report SCTP header off end of packet
Quoted 42 lines · Peter Maydell
eth_get_protocols()parses an ethernet packet and tells the caller about various offsets into it and other properties of the packet. For the L4 protocol, in both theIP_PROTO_TCPandIP_PROTO_UDPbranches we implicitly check that the packet does contain a whole TCP or UDP header at thel4hdr_offoffset, because_eth_copy_chunk()will fail if the input packet size isn't big enough for the whole header we are copying out of it. But forIP_PROTO_SCTPwe don't copy the SCTP common header. In this case we should still do the length check, so we don't setl4hdr_info->prototo say there's an SCTP header present when there isn't.This shouldn't have any visible effects if the downstream consumers of the
l4hdr_infoare conservative about checking before they access into the packet, but checking here too adds defence in depth, hence the cc to stable.Fixes 907209e311 ("igb: Implement Rx SCTP CSO")Signed-off-by Peter Maydell <[email protected]>--- include/net/eth.h | 2 ++ net/eth.c | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-)include/net/eth.h +2
-0@@ -394,6 +394,8 @@ typedef struct eth_l4_hdr_info_st {bool has_tcp_data; } eth_l4_hdr_info; +#define SCTP_COMMON_HEADER_LEN 12 + void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff, bool *hasip4, bool *hasip6, size_t *l3hdr_off,net/eth.c +0
-0
net/eth.c void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff, · line 221
Quoted 10 lines
net/eth.c +6
-1@@ -213,7 +213,14 @@ void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff,break ;case IP_PROTO_SCTP : - l4hdr_info->proto =ETH_L4_HDR_PROTO_SCTP ; +/* +* We don't copy the SCTP common header into l4hdr_info, but +* don't report SCTP as present if the packet isn't big enough +* to actually have an SCTP header. +*/ +if (*l4hdr_off > input_size -SCTP_COMMON_HEADER_LEN ) {
Hmm don't we want the opposite "is SCTP common header complete" check?
if (input_size >= *l4hdr_off
&& input_size - *l4hdr_off >= SCTP_COMMON_HEADER_LEN) {
Quoted 5 lines
net/eth.c +2
-0+ l4hdr_info->proto =ETH_L4_HDR_PROTO_SCTP ; + }break ; } }
Re: [PATCH 03/11] net/eth.c: In eth_get_protocols(), don't report SCTP header off end of packet
Quoted 57 lines · Philippe Mathieu-Daudé
Quoted 42 lines · Peter Maydell
eth_get_protocols()parses an ethernet packet and tells the caller about various offsets into it and other properties of the packet. For the L4 protocol, in both theIP_PROTO_TCPandIP_PROTO_UDPbranches we implicitly check that the packet does contain a whole TCP or UDP header at thel4hdr_offoffset, because_eth_copy_chunk()will fail if the input packet size isn't big enough for the whole header we are copying out of it. But forIP_PROTO_SCTPwe don't copy the SCTP common header. In this case we should still do the length check, so we don't setl4hdr_info->prototo say there's an SCTP header present when there isn't.This shouldn't have any visible effects if the downstream consumers of the
l4hdr_infoare conservative about checking before they access into the packet, but checking here too adds defence in depth, hence the cc to stable.Fixes 907209e311 ("igb: Implement Rx SCTP CSO")Signed-off-by Peter Maydell <[email protected]>--- include/net/eth.h | 2 ++ net/eth.c | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-)include/net/eth.h +2
-0@@ -394,6 +394,8 @@ typedef struct eth_l4_hdr_info_st {bool has_tcp_data; } eth_l4_hdr_info; +#define SCTP_COMMON_HEADER_LEN 12 + void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff, bool *hasip4, bool *hasip6, size_t *l3hdr_off,net/eth.c +0
-0net/eth.c void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff, · line 221
Quoted 10 lines
net/eth.c +6
-1@@ -213,7 +213,14 @@ void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff,break ;case IP_PROTO_SCTP : - l4hdr_info->proto =ETH_L4_HDR_PROTO_SCTP ; +/* +* We don't copy the SCTP common header into l4hdr_info, but +* don't report SCTP as present if the packet isn't big enough +* to actually have an SCTP header. +*/ +if (*l4hdr_off > input_size -SCTP_COMMON_HEADER_LEN ) {Hmm don't we want the opposite "is SCTP common header complete" check?
Quoted 2 lines
if (
input_size>=*l4hdr_off
&&input_size-*l4hdr_off>=SCTP_COMMON_HEADER_LEN) {
Whoops, yes, I have the sense of the test the wrong way around, should be
(*l4hdr_off <= input_size - SCTP_COMMON_HEADER_LEN)
Doing it this way we know we aren't going to underflow on the subtraction (input_size must be bigger than 12 if we got here) so we don't need to explictly also check forinput_size >= *l4hdr_off, but maybe that's being too clever and your way of writing it is better?
-- PMM
Philippe Mathieu-Daudé
Reviewed-byRe: [PATCH 03/11] net/eth.c: In eth_get_protocols(), don't report SCTP header off end of packet
Quoted 66 lines · Peter Maydell
Quoted 57 lines · Philippe Mathieu-Daudé
Quoted 42 lines · Peter Maydell
eth_get_protocols()parses an ethernet packet and tells the caller about various offsets into it and other properties of the packet. For the L4 protocol, in both theIP_PROTO_TCPandIP_PROTO_UDPbranches we implicitly check that the packet does contain a whole TCP or UDP header at thel4hdr_offoffset, because_eth_copy_chunk()will fail if the input packet size isn't big enough for the whole header we are copying out of it. But forIP_PROTO_SCTPwe don't copy the SCTP common header. In this case we should still do the length check, so we don't setl4hdr_info->prototo say there's an SCTP header present when there isn't.This shouldn't have any visible effects if the downstream consumers of the
l4hdr_infoare conservative about checking before they access into the packet, but checking here too adds defence in depth, hence the cc to stable.Fixes 907209e311 ("igb: Implement Rx SCTP CSO")Signed-off-by Peter Maydell <[email protected]>--- include/net/eth.h | 2 ++ net/eth.c | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-)include/net/eth.h +2
-0@@ -394,6 +394,8 @@ typedef struct eth_l4_hdr_info_st {bool has_tcp_data; } eth_l4_hdr_info; +#define SCTP_COMMON_HEADER_LEN 12 + void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff, bool *hasip4, bool *hasip6, size_t *l3hdr_off,net/eth.c +0
-0net/eth.c void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff, · line 221
Quoted 10 lines
net/eth.c +6
-1@@ -213,7 +213,14 @@ void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff,break ;case IP_PROTO_SCTP : - l4hdr_info->proto =ETH_L4_HDR_PROTO_SCTP ; +/* +* We don't copy the SCTP common header into l4hdr_info, but +* don't report SCTP as present if the packet isn't big enough +* to actually have an SCTP header. +*/ +if (*l4hdr_off > input_size -SCTP_COMMON_HEADER_LEN ) {Hmm don't we want the opposite "is SCTP common header complete" check?
Quoted 2 lines
if (
input_size>=*l4hdr_off
&&input_size-*l4hdr_off>=SCTP_COMMON_HEADER_LEN) {Whoops, yes, I have the sense of the test the wrong way around, should be
(*l4hdr_off<=input_size-SCTP_COMMON_HEADER_LEN)
Quoted 5 lines
Doing it this way we know we aren't going to underflow on the subtraction (
input_sizemust be bigger than 12 if we got here) so we don't need to explictly also check forinput_size>=*l4hdr_off, but maybe that's being too clever and your way of writing it is better?
Ah indeed I didn't read the full function body, we already have a complete IP header so we are sure input_size >= 12. Let's go with your way otherwise static analyzers might complain later about impossible check.
Re: [PATCH 03/11] net/eth.c: In eth_get_protocols(), don't report SCTP header off end of packet
Quoted 74 lines · Philippe Mathieu-Daudé
Quoted 67 lines · Peter Maydell
Quoted 58 lines · Philippe Mathieu-Daudé
Quoted 45 lines · Peter Maydell
eth_get_protocols()parses an ethernet packet and tells the caller about various offsets into it and other properties of the packet. For the L4 protocol, in both theIP_PROTO_TCPandIP_PROTO_UDPbranches we implicitly check that the packet does contain a whole TCP or UDP header at thel4hdr_offoffset, because_eth_copy_chunk()will fail if the input packet size isn't big enough for the whole header we are copying out of it. But forIP_PROTO_SCTPwe don't copy the SCTP common header. In this case we should still do the length check, so we don't setl4hdr_info->prototo say there's an SCTP header present when there isn't.This shouldn't have any visible effects if the downstream consumers of the
l4hdr_infoare conservative about checking before they access into the packet, but checking here too adds defence in depth, hence the cc to stable.Fixes 907209e311 ("igb: Implement Rx SCTP CSO")Signed-off-by Peter Maydell <[email protected]>---
include/net/eth.h| 2 ++
net/eth.c | 9 ++++++++-2 files changed, 10 insertions(+), 1 deletion(-)include/net/eth.h +2
-0@@ -394,6 +394,8 @@ typedef struct eth_l4_hdr_info_st { bool has_tcp_data; } eth_l4_hdr_info; +#define SCTP_COMMON_HEADER_LEN 12 + void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff, bool *hasip4, bool *hasip6, size_t *l3hdr_off,net/eth.c +0
-0@@ -213,7 +213,14 @@ void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff, break;Quoted 8 lines
case
IP_PROTO_SCTP:
-l4hdr_info->proto=ETH_L4_HDR_PROTO_SCTP;
+ /*
+ * We don't copy the SCTP common header intol4hdr_info, but + * don't report SCTP as present if the packet isn't big enough + * to actually have an SCTP header.
+ */
+ if (*l4hdr_off>input_size-SCTP_COMMON_HEADER_LEN) {Hmm don't we want the opposite "is SCTP common header complete" check?
Quoted 2 lines
if (
input_size>=*l4hdr_off
&&input_size-*l4hdr_off>=SCTP_COMMON_HEADER_LEN) {Whoops, yes, I have the sense of the test the wrong way around, should be
(*l4hdr_off<=input_size-SCTP_COMMON_HEADER_LEN)Quoted 5 lines
Doing it this way we know we aren't going to underflow on the subtraction (
input_sizemust be bigger than 12 if we got here) so we don't need to explictly also check forinput_size>=*l4hdr_off, but maybe that's being too clever and your way of writing it is better?
Quoted 5 lines
Ah indeed I didn't read the full function body, we already have a complete IP header so we are sure
input_size>= 12. Let's go with your way otherwise static analyzers might complain later about impossible check.>Reviewed-by Philippe Mathieu-Daudé <[email protected]>
With [1], I think the following would be better:
input_size - *l4hdr_off <= SCTP_COMMON_HEADER_LEN
input_size >= l4hdr_off is already enforced for IPv6, but not for IPv4. [1] enforces that for IPv4 since failing to do so is inconsistent and led to an out-of-bounds access [2].
SCTP header length and IP header lengths are independent values, and we can avoid making assumptions between them this way.
Regards,
Akihiko Odaki
[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://gitlab.com/qemu-project/qemu/-/work_items/4155
PATCH 4/11 net/eth.c: Check for undersized IPv4 headers in eth_get_protocols()
In reply to Peter MaydellIn the IPv6 path of eth_get_protocols(), eth_parse_ipv6_hdr() does various sanity checks on the packet's IPv6 header, so we won't tell the caller we have an IPv6 header when we have a malformed one. However for IPv4 we only check the version and that we have at least the minimum header size.
Harden this codepath by checking also that the header size declared in the packet's own headers is neither smaller than the minimum nor larger than the actual packet size.
Missing these checks shouldn't cause problems if the caller is validating all offsets and lengths before use, but checking here too is defence-in-depth, hence the cc to stable.
--- net/eth.c | 2 ++ 1 file changed, 2 insertions(+)
net/eth.c +2 -0
@@ -156,6 +156,8 @@ void eth_get_protocols(const struct iovec *iov, size_t iovcnt, size_t iovoff, copied =iov_to_buf (iov, iovcnt, *l3hdr_off, iphdr,sizeof (*iphdr));if (copied <sizeof (*iphdr) || +IP_HDR_GET_LEN (iphdr) <sizeof (*iphdr) || + input_size - *l3hdr_off <IP_HDR_GET_LEN (iphdr) ||IP_HEADER_VERSION (iphdr) !=IP_HEADER_VERSION_4 ) {return ; }
Signature · 1 line
2.43.0
PATCH 5/11 net/eth.c: Harden _eth_tcp_has_data() against short lengths
In reply to Peter MaydellIn _eth_tcp_has_data() we calculate the length of the rest of the packet after the ipv4 header by subtracting the header offset from the packet length. If this subtraction underflows we'll return the wrong answer. This shouldn't cause any problems if other layers of the code are checking lengths and offsets, but defence-in-depth suggests doing a checked-subtraction and returning "has no data" if the subtraction underflows.
--- net/eth.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
net/eth.c +12 -2
@@ -117,10 +117,20 @@ _eth_tcp_has_data(bool is_ip4,uint32_t l4len;if (is_ip4) { - l4len =be16_to_cpu (ip4_hdr->ip_len) -IP_HDR_GET_LEN (ip4_hdr); +if (usub32_overflow (be16_to_cpu (ip4_hdr->ip_len), +IP_HDR_GET_LEN (ip4_hdr), &l4len)) { +/* packet too short for claimed IP header: no data */ +return false; + } }else {size_t opts_len = full_ip6hdr_len -sizeof (struct ip6_header ); - l4len =be16_to_cpu (ip6_hdr->ip6_ctlun.ip6_un1.ip6_un1_plen) - opts_len; +uint64_t l4len_64; +if (usub64_overflow (be16_to_cpu (ip6_hdr->ip6_ctlun.ip6_un1.ip6_un1_plen), + opts_len, &l4len_64)) { +/* packet too short for claimed IP header: no data */ +return false; + } + l4len = l4len_64; }return l4len >TCP_HEADER_DATA_OFFSET (tcp);
Signature · 1 line
2.43.0
PATCH 6/11 net/eth.c: Tighten size checks in eth_get_l3_proto()
In reply to Peter MaydellCurrently eth_get_l3_proto() has a check that the offset of the protocol field is not off the end of the iovec; this is actually unnecessary because iov_to_buf() guarantees to return 0 for offsets off the end of the iovec. On the other hand, it assumes that the l2hdr_len it is passed is not so small that the protocol offset calculation underflows and it doesn't check that the packet is actually big enough for the l2hdr_len. This shouldn't matter if other parts of the code are checking, but for defence in depth we tighten this check.
--- net/eth.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
net/eth.c +4 -2
@@ -74,11 +74,13 @@ eth_get_l3_proto(const struct iovec *l2hdr_iov, int iovcnt, size_t l2hdr_len)uint16_t proto;size_t copied;size_t size =iov_size (l2hdr_iov, iovcnt); -size_t proto_offset = l2hdr_len -sizeof (proto); +size_t proto_offset; -if (size < proto_offset) { +if (l2hdr_len <sizeof (struct eth_header ) || size < l2hdr_len) { +/* We don't have a complete ethernet header */ return ETH_P_UNKNOWN ; } + proto_offset = l2hdr_len -sizeof (proto); copied =iov_to_buf (l2hdr_iov, iovcnt, proto_offset, &proto,sizeof (proto));
Signature · 1 line
2.43.0
PATCH 7/11 include/net/eth.h: Make eth_get_l2_hdr_length_iov() check protocol
In reply to Peter MaydellIn eth_get_l2_hdr_length() we return sizeof(eth_header) for any protocol other than VLAN or DVLAN. However, in the iov version of this function, if the packet is larger than the ethernet header but not large enough for both the eth_header and the vlan_header we'll end up returning the actual packet size rather than the eth_header size. Fix the "not enough data" return case.
This will only cause issues if the caller isn't already checking offsets and sizes, so this is defence-in-depth. It also in theory corrects the result for the hypothetical case of an ethernet-level protocol that allows a payload of less than 4 bytes.
--- include/net/eth.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/net/eth.h +1 -1
@@ -321,7 +321,7 @@ eth_get_l2_hdr_length_iov(const struct iovec *iov, size_t iovcnt, size_t iovoff)size_t copied =iov_to_buf (iov, iovcnt, iovoff, p,ARRAY_SIZE (p));if (copied <ARRAY_SIZE (p)) { -return copied; +return MIN (copied,sizeof (struct eth_header )); }return eth_get_l2_hdr_length (p);
Signature · 1 line
2.43.0
PATCH 8/11 hw/net/net_tx_pkt: Sanity check GSO headers in net_tx_pkt_build_vheader()
In reply to Peter MaydellIn net_tx_pkt_build_vheader(), we are insufficiently cautious about headers from the packet from the guest. Add more checking:
* that the segment offload payload gso_size isn't 0
* that the packet is not too small
* that the TCP th_off is inside the payload
* that we aren't going to overflow in our header length calculation
Of these, the most significant extra check is in calculation of the virt_hdr length. We calculate this with
pkt->virt_hdr.hdr_len = pkt->hdr_len + l4hdr.th_off * sizeof(uint32_t);
The struct field is 16 bits, and so this can overflow if the guest hands us a packet of close to 64K in size and an appropriate l4hdr.th_off. The effect is that we record a bogus virt_hdr.hdr_len, and later on in net_tx_pkt_tcp_fragment_init() this will cause us to make a very large allocation that causes g_malloc() to abort.
This is a hardening issue rather than a security issue, because the amount of guest control over the allocation size is such that it can only generate allocations very close to SIZE_MAX which always make g_malloc() abort, and so it's a guest self-denial-of-service only.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3648
--- hw/net/net_tx_pkt.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
hw/net/net_tx_pkt.c +9 -1
@@ -335,6 +335,11 @@ bool net_tx_pkt_build_vheader(struct NetTxPkt *pkt, bool tso_enable,break ;case VIRTIO_NET_HDR_GSO_UDP : +if (IP_FRAG_ALIGN_SIZE (gso_size) ==0 || + pkt->payload_len <sizeof (struct udp_header ) || + pkt->hdr_len +sizeof (struct udp_header ) >UINT16_MAX ) { +return false; + } pkt->virt_hdr.gso_size = gso_size; pkt->virt_hdr.hdr_len = pkt->hdr_len +sizeof (struct udp_header );break ;
@@ -344,7 +349,10 @@ bool net_tx_pkt_build_vheader(struct NetTxPkt *pkt, bool tso_enable, bytes_read =iov_to_buf (&pkt->vec[NET_TX_PKT_PL_START_FRAG ], pkt->payload_frags,0 , &l4hdr,sizeof (l4hdr));if (bytes_read <sizeof (l4hdr) || - l4hdr.th_off *sizeof (uint32_t ) <sizeof (l4hdr)) { + gso_size ==0 || + l4hdr.th_off *sizeof (uint32_t ) <sizeof (l4hdr) || + l4hdr.th_off *sizeof (uint32_t ) > pkt->payload_len || + pkt->hdr_len + l4hdr.th_off *sizeof (uint32_t ) >UINT16_MAX ) {return false; }
Signature · 1 line
2.43.0
PATCH 9/11 hw/net/net_tx_pkt: Sanity check virt hdr len in net_tx_tcp_fragment_init
In reply to Peter MaydellIn net_tx_pkt_tcp_fragment_init() we calculate the l4 header size by subtracting the packet's virt_hdr.hdr_len from its overall length, and then we g_malloc() that many bytes. This shouldn't underflow if the caller has been sanity checking sizes and offsets, but as defence-in-depth, check again here before relying on it for the malloc size.
This change would suffice to avoid the guest self-denial-of-service via overflowing the virt_hdr.hdr_len calculation in net_tx_pkt_tcp_fragment_init() which we fixed in the preceding commit.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3648
--- hw/net/net_tx_pkt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/net_tx_pkt.c +1 -1
@@ -604,7 +604,7 @@ static bool net_tx_pkt_tcp_fragment_init(struct NetTxPkt *pkt,size_t bytes_read =0 ;struct tcp_hdr *th; -if (!pkt->payload_frags) { +if (!pkt->payload_frags || pkt->virt_hdr.hdr_len <= pkt->hdr_len) {return false; }
Signature · 1 line
2.43.0
PATCH 10/11 hw/net/net_tx_pkt: Sanity check L2+L3 header len in net_tx_pkt_parse_headers()
In reply to Peter MaydellIn net_tx_pkt_parse_headers() we examine the L2 and L3 headers one after the other. Add a sanity check that the two together don't exceed the packet length or the maximum IP packet size. This shouldn't happen if the L3 parsing was done right, but is worth doing for defence-in-depth.
--- hw/net/net_tx_pkt.c | 8 ++++++++ 1 file changed, 8 insertions(+)
hw/net/net_tx_pkt.c +8 -0
@@ -267,6 +267,14 @@ static bool net_tx_pkt_parse_headers(struct NetTxPkt *pkt)break ; } +if (l2_hdr->iov_len + l3_hdr->iov_len >ETH_MAX_IP_DGRAM_LEN || + l2_hdr->iov_len + l3_hdr->iov_len > +iov_size (pkt->raw, pkt->raw_frags)) { + l2_hdr->iov_len =0 ; + l3_hdr->iov_len =0 ; +return false; + } +net_tx_pkt_calculate_hdr_len (pkt);return true; }
Signature · 1 line
2.43.0
PATCH 11/11 hw/net/net_rx_pkt: Check for overflow in _net_rx_pkt_calc_l4_csum()
In reply to Peter MaydellIn _net_rx_pkt_calc_l4_csum() we calculate the number of bytes to run the checksum over by subtracting the IP header length from the total IP packet size. This shouldn't underflow if other parts of the code have been correctly checking and calculating sizes and offsets, but for defence in depth check here, and don't checksum any bytes if we would otherwise underflow.
--- hw/net/net_rx_pkt.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
hw/net/net_rx_pkt.c +6 -4
@@ -501,8 +501,10 @@ _net_rx_pkt_calc_l4_csum(struct NetRxPkt *pkt) csl =be16_to_cpu (pkt->l4hdr_info.hdr.udp.uh_ulen);trace_net_rx_pkt_l4_csum_calc_ip4_udp (); }else { - csl =be16_to_cpu (pkt->ip4hdr_info.ip4_hdr.ip_len) - -IP_HDR_GET_LEN (&pkt->ip4hdr_info.ip4_hdr); +uint16_t iplen =be16_to_cpu (pkt->ip4hdr_info.ip4_hdr.ip_len); +uint16_t iphdr_len =IP_HDR_GET_LEN (&pkt->ip4hdr_info.ip4_hdr); + + csl = (iplen >= iphdr_len) ? (iplen - iphdr_len) :0 ;trace_net_rx_pkt_l4_csum_calc_ip4_tcp (); }
@@ -517,9 +519,9 @@ _net_rx_pkt_calc_l4_csum(struct NetRxPkt *pkt)struct ip6_header *ip6hdr = &pkt->ip6hdr_info.ip6_hdr;size_t full_ip6hdr_len = pkt->l4hdr_off - pkt->l3hdr_off;size_t ip6opts_len = full_ip6hdr_len -sizeof (struct ip6_header ); +uint16_t plen =be16_to_cpu (ip6hdr->ip6_ctlun.ip6_un1.ip6_un1_plen); - csl =be16_to_cpu (ip6hdr->ip6_ctlun.ip6_un1.ip6_un1_plen) - - ip6opts_len; + csl = (plen >= ip6opts_len) ? (plen - ip6opts_len) :0 ;trace_net_rx_pkt_l4_csum_calc_ip6_tcp (); }
Signature · 1 line
2.43.0