From: Jakub Kicinski <kuba@kernel.org>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com,
Jakub Kicinski <kuba@kernel.org>,
willemdebruijn.kernel@gmail.com, mst@redhat.com,
jasowang@redhat.com, xuanzhuo@linux.alibaba.com,
eperezma@redhat.com, shuah@kernel.org, arefev@swemel.ru,
virtualization@lists.linux.dev, linux-kselftest@vger.kernel.org
Subject: [PATCH net] virtio: fix GSO with frames unaligned to size
Date: Tue, 23 Jul 2024 15:31:09 -0700 [thread overview]
Message-ID: <20240723223109.2196886-1-kuba@kernel.org> (raw)
The commit under fixes added a questionable check to
virtio_net_hdr_to_skb(). I'm guessing the check was supposed
to protect from csum offset being outside of a segment
(especially if len is not multiple of segment size).
The condition can't be right, tho, as it breaks previously
working sending of GSO frames with only one segment
(i.e. when gso_size <= len we silently ignore the GSO
request and send a single non-GSO frame).
Fix the logic and move it to the GSO part.
This has been caught by net/tap and net/psock_send.sh tests.
Fixes: e269d79c7d35 ("net: missing check virtio")
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
CC: willemdebruijn.kernel@gmail.com
CC: mst@redhat.com
CC: jasowang@redhat.com
CC: xuanzhuo@linux.alibaba.com
CC: eperezma@redhat.com
CC: shuah@kernel.org
CC: arefev@swemel.ru
CC: virtualization@lists.linux.dev
CC: linux-kselftest@vger.kernel.org
---
include/linux/virtio_net.h | 27 ++++++++++++++++-----------
tools/testing/selftests/net/tap.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 11 deletions(-)
diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h
index d1d7825318c3..c54b7aa42921 100644
--- a/include/linux/virtio_net.h
+++ b/include/linux/virtio_net.h
@@ -52,11 +52,11 @@ static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
bool little_endian)
{
unsigned int nh_min_len = sizeof(struct iphdr);
+ unsigned int csum_needed = 0;
unsigned int gso_type = 0;
unsigned int thlen = 0;
unsigned int p_off = 0;
unsigned int ip_proto;
- u64 ret, remainder, gso_size;
if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
@@ -99,16 +99,6 @@ static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
u32 off = __virtio16_to_cpu(little_endian, hdr->csum_offset);
u32 needed = start + max_t(u32, thlen, off + sizeof(__sum16));
- if (hdr->gso_size) {
- gso_size = __virtio16_to_cpu(little_endian, hdr->gso_size);
- ret = div64_u64_rem(skb->len, gso_size, &remainder);
- if (!(ret && (hdr->gso_size > needed) &&
- ((remainder > needed) || (remainder == 0)))) {
- return -EINVAL;
- }
- skb_shinfo(skb)->tx_flags |= SKBFL_SHARED_FRAG;
- }
-
if (!pskb_may_pull(skb, needed))
return -EINVAL;
@@ -119,6 +109,8 @@ static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
p_off = nh_min_len + thlen;
if (!pskb_may_pull(skb, p_off))
return -EINVAL;
+
+ csum_needed = needed;
} else {
/* gso packets without NEEDS_CSUM do not set transport_offset.
* probe and drop if does not match one of the above types.
@@ -188,6 +180,19 @@ static inline int virtio_net_hdr_to_skb(struct sk_buff *skb,
if (gso_size == GSO_BY_FRAGS)
return -EINVAL;
+ if (csum_needed) {
+ unsigned int p_rem, p_size;
+
+ p_size = gso_size;
+ p_rem = (skb->len - nh_off) % gso_size;
+ if (p_rem)
+ p_size = p_rem;
+
+ /* Make sure csum still within packet after GSO */
+ if (p_size + nh_off < csum_needed)
+ return -EINVAL;
+ }
+
/* Too small packets are not really GSO ones. */
if (skb->len - nh_off > gso_size) {
shinfo->gso_size = gso_size;
diff --git a/tools/testing/selftests/net/tap.c b/tools/testing/selftests/net/tap.c
index 247c3b3ac1c9..8527d51449cf 100644
--- a/tools/testing/selftests/net/tap.c
+++ b/tools/testing/selftests/net/tap.c
@@ -418,6 +418,36 @@ TEST_F(tap, test_packet_valid_udp_csum)
ASSERT_EQ(ret, off);
}
+TEST_F(tap, test_packet_invalid_udp_gso_csum)
+{
+ uint8_t pkt[TEST_PACKET_SZ];
+ uint16_t payload;
+ size_t off;
+ int ret;
+ int i;
+
+ payload = ETH_DATA_LEN - sizeof(struct iphdr) - sizeof(struct udphdr);
+
+ memset(pkt, 0, sizeof(pkt));
+ off = build_test_packet_valid_udp_gso(pkt, payload);
+
+ for (i = -16; i < 16; i++) {
+ ret = write(self->fd, pkt, off + i);
+
+ if (i <= 0 ||
+ i > __builtin_offsetof(struct udphdr, check) + 1) {
+ EXPECT_EQ(ret, off + i)
+ TH_LOG("mismatch with offset: %d (%zd)",
+ i, off + i);
+ } else {
+ EXPECT_EQ(ret, -1)
+ TH_LOG("mismatch with offset: %d (%zd)",
+ i, off + i);
+ EXPECT_EQ(errno, 22);
+ }
+ }
+}
+
TEST_F(tap, test_packet_crash_tap_invalid_eth_proto)
{
uint8_t pkt[TEST_PACKET_SZ];
--
2.45.2
next reply other threads:[~2024-07-23 22:31 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-23 22:31 Jakub Kicinski [this message]
2024-07-24 3:48 ` [PATCH net] virtio: fix GSO with frames unaligned to size Willem de Bruijn
2024-07-24 14:41 ` Jakub Kicinski
2024-07-25 2:52 ` Willem de Bruijn
2024-07-25 9:22 ` Denis Arefev
2024-07-25 14:27 ` Willem de Bruijn
2024-07-25 21:02 ` Willem de Bruijn
2024-07-25 10:17 ` Denis Arefev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240723223109.2196886-1-kuba@kernel.org \
--to=kuba@kernel.org \
--cc=arefev@swemel.ru \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=eperezma@redhat.com \
--cc=jasowang@redhat.com \
--cc=linux-kselftest@vger.kernel.org \
--cc=mst@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=shuah@kernel.org \
--cc=virtualization@lists.linux.dev \
--cc=willemdebruijn.kernel@gmail.com \
--cc=xuanzhuo@linux.alibaba.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).