From: Peter Maydell <peter.maydell@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Michael S. Tsirkin" <mst@redhat.com>,
Jason Wang <jasowang@redhat.com>,
Dmitry Fleytman <dmitry.fleytman@gmail.com>,
Akihiko Odaki <akihiko.odaki@daynix.com>
Subject: Re: [PATCH 1/2] hw/net/virtio-net.c: Don't assume IP length field is aligned
Date: Thu, 7 Nov 2024 20:47:53 +0000 [thread overview]
Message-ID: <CAFEAcA8mu0fbOtmqCm6pV_pBvAzW4UCe_1xg7P7RpL3NQY4ohw@mail.gmail.com> (raw)
In-Reply-To: <20241107163210.3620697-2-peter.maydell@linaro.org>
On Thu, 7 Nov 2024 at 16:32, Peter Maydell <peter.maydell@linaro.org> wrote:
>
> In virtio-net.c we assume that the IP length field in the packet is
> aligned, and we copy its address into a uint16_t* in the
> VirtioNetRscUnit struct which we then dereference later. This isn't
> a safe assumption; it will also result in compilation failures if we
> mark the ip_header struct as QEMU_PACKED because the compiler will
> not let you take the address of an unaligned struct field.
>
> Make the ip_plen field in VirtioNetRscUnit a void*, and make all the
> places where we read or write through that pointer instead use some
> new accessor functions read_unit_ip_len() and write_unit_ip_len()
> which account for the pointer being potentially unaligned and also do
> the network-byte-order conversion we were previously using htons() to
> perform.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> include/hw/virtio/virtio-net.h | 2 +-
> hw/net/virtio-net.c | 23 +++++++++++++++++++----
> 2 files changed, 20 insertions(+), 5 deletions(-)
>
> diff --git a/include/hw/virtio/virtio-net.h b/include/hw/virtio/virtio-net.h
> index 060c23c04d2..b9ea9e824e3 100644
> --- a/include/hw/virtio/virtio-net.h
> +++ b/include/hw/virtio/virtio-net.h
> @@ -102,7 +102,7 @@ typedef struct VirtioNetRscStat {
> /* Rsc unit general info used to checking if can coalescing */
> typedef struct VirtioNetRscUnit {
> void *ip; /* ip header */
> - uint16_t *ip_plen; /* data len pointer in ip header field */
> + void *ip_plen; /* pointer to unaligned uint16_t data len in ip header */
> struct tcp_header *tcp; /* tcp header */
> uint16_t tcp_hdrlen; /* tcp header len */
> uint16_t payload; /* pure payload without virtio/eth/ip/tcp */
> diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
> index f2104ed364a..11cf462180d 100644
> --- a/hw/net/virtio-net.c
> +++ b/hw/net/virtio-net.c
> @@ -2049,6 +2049,21 @@ static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf,
> return virtio_net_receive_rcu(nc, buf, size, false);
> }
>
> +/*
> + * Accessors to read and write the IP packet data length field. This
> + * is a potentially unaligned network-byte-order 16 bit unsigned integer
> + * pointed to by unit->ip_len.
> + */
> +static uint16_t read_unit_ip_len(VirtioNetRscUnit *unit)
> +{
> + return ldl_be_p(unit->ip_plen);
> +}
> +
> +static void write_unit_ip_len(VirtioNetRscUnit *unit, uint16_t l)
> +{
> + stl_be_p(unit->ip_plen, l);
> +}
These should of course be lduw_be_p() and stw_be_p(), since
it's a 16 bit field.
Interestingly nothing fails in "make check" or "make check-functional"
if this breaks, suggesting we aren't exercising virtio-net very hard.
-- PMM
next prev parent reply other threads:[~2024-11-07 20:48 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-07 16:32 [PATCH 0/2] net: Make ip_header struct QEMU_PACKED Peter Maydell
2024-11-07 16:32 ` [PATCH 1/2] hw/net/virtio-net.c: Don't assume IP length field is aligned Peter Maydell
2024-11-07 20:47 ` Peter Maydell [this message]
2024-11-11 1:25 ` Jason Wang
[not found] ` <CAGoVJZyPiwoxFuo0UM9cRLh0PUunL9Tzq=2jhsSE3tao2W5pCg@mail.gmail.com>
2024-11-11 14:54 ` Yuri Benditovich
2024-11-07 16:32 ` [PATCH 2/2] net: mark struct ip_header as QEMU_PACKED Peter Maydell
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=CAFEAcA8mu0fbOtmqCm6pV_pBvAzW4UCe_1xg7P7RpL3NQY4ohw@mail.gmail.com \
--to=peter.maydell@linaro.org \
--cc=akihiko.odaki@daynix.com \
--cc=dmitry.fleytman@gmail.com \
--cc=jasowang@redhat.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
/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).