hw/net/rtl8139: fix vlan tag related bugs
cover hw/net/rtl8139: fix vlan tag related bugs
This patchset fixes a couple of bugs in the rtl8139 device related to its handling of vlan tags.
The first is one that showed up as
https://gitlab.com/qemu-project/qemu/-/work_items/3518 -- a guest can send a carefully constructed packet to itself over loopback (or receive on over the network) that makes the vlan tag extraction code overread the packet buffer by 4 bytes, leaking that data into the guest. This is a regression that we introduced when we moved "pad short packets" from individual network devices into the core net code. Bin and I had a discussion about this:
https://lore.kernel.org/qemu-devel/CAFEAcA_UhmCxJc16CHE=4ZR1+PA0=4-29=fEe+d+iUmhLTzpuQ@mail.gmail.com/
where we started off thinking that we should fix this in the core net code, but after looking at the 802.1Q spec I think that it is a bug in the rtl8139 code, and it shouldn't be trying to pad the packet as much as it is. So patch 1 fixes this by dropping that attempt at padding entirely, so we always write exactly what we get from the net backend into the receive buffer.
Patch 2 fixes a bug that Bin pointed out during that conversation:
when we insert a vlan tag while sending a packet over loopback we don't adjust the size that we pass to qemu_receive_packet(), so we truncate the last 4 bytes of data.
Big disclaimer here: I have tested these with "make check" and with the buffer-overrun repro case from the bug report, and with a simple guest VM. But we don't seem to document how to set up a guest which receives and handles vlan-tagged packets, so I have no idea how to actually test that part.
I don't think we need to be in a tearing hurry to get these fixes into git, so although I've tagged them for stable I'm expecting to get them in after we re-open for 11.2 rather than trying to squeeze them into 11.1 at this point.
thanks
-- PMM
Peter Maydell (2):
hw/net/rtl8139: Fix handling of VLAN tags on incoming short packets hw/net/rtl8139: Send whole of vlan-tagged packet when doing loopback
hw/net/rtl8139.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/2 hw/net/rtl8139: Fix handling of VLAN tags on incoming short packets
The rtl8139 receive code handles VLAN tags in incoming packets by copying the VLAN tag to a special field in the receive descriptor, and copying only the actual payload data to the receive buffer. This code tries to ensure that it pads out the payload to at least MIN_BUF_SIZE bytes.
In commit 63b901bfd30 we removed the main "pad short frames" code from this device because we switched to requiring net backends to do the padding. However we didn't notice that this broke the VLAN tag handling, which relied on the old code making the buffer at least MIN_BUF_SIZE + VLAN_HLEN bytes so that it could copy MIN_BUF_SIZE bytes into the receive buffer even after removing the VLAN tag. The result is that the guest can make us read 4 bytes off the end of a buffer by feeding itself a suitable short packet in loopback mode.
The old behaviour is actually not correct, because the IEEE802.1Q standard says that the minimum ethernet frame size remains 64 bytes including the 4 checksum bytes, and so when a tag is present the payload data only needs to be 56 bytes. (A bridge implementation can choose to pad tagged frames out to 68 bytes, but it doesn't have to, and so all devices have to correctly handle incoming tagged frames that are 64 bytes long.)
The RTL8139 datasheet isn't very communicative on this topic, but there's nothing that suggests it adds extra padding on receive that didn't exist in the incoming packet.
Drop the last remnants of the padding handling from this device; this avoids overcopying into the guest when we receive a short VLAN tagged packet.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3518
---
I had been looking at this from the POV of making the generic net code add extra padding when a VLAN tag is present, but I went and read the 802.1Q spec and I now think this is just a place where the rtl8139 code got it wrong.
--- hw/net/rtl8139.c | 5 ----- 1 file changed, 5 deletions(-)
hw/net/rtl8139.c +0 -5
@@ -778,7 +778,6 @@ static void rtl8139_write_buffer(RTL8139State *s, const void *buf, int size) s->RxBufAddr += size; } -#define MIN_BUF_SIZE 60static inline dma_addr_t rtl8139_addr64 (uint32_t low,uint32_t high) {return low | ((uint64_t )high <<32 );
@@ -1007,10 +1006,6 @@ static ssize_t rtl8139_receive(NetClientState *nc, lduw_be_p(&buf[ETH_ALEN * 2]) == ETH_P_VLAN) { dot1q_buf = &buf[ETH_ALEN *2 ]; size -=VLAN_HLEN ; -/* if too small buffer, use the tailroom added duing expansion */ -if (size <MIN_BUF_SIZE ) { - size =MIN_BUF_SIZE ; - } rxdw1 &= ~CP_RX_VLAN_TAG_MASK ;/* BE + ~le_to_cpu()~ + cpu_to_le() = BE */
Signature · 1 line
2.43.0
Re: [PATCH 1/2] hw/net/rtl8139: Fix handling of VLAN tags on incoming short packets
Quoted 32 lines · Peter Maydell
The rtl8139 receive code handles VLAN tags in incoming packets by copying the VLAN tag to a special field in the receive descriptor, and copying only the actual payload data to the receive buffer. This code tries to ensure that it pads out the payload to at least
MIN_BUF_SIZEbytes.In commit
63b901bfd30we removed the main "pad short frames" code from this device because we switched to requiring net backends to do the padding. However we didn't notice that this broke the VLAN tag handling, which relied on the old code making the buffer at leastMIN_BUF_SIZE+VLAN_HLENbytes so that it could copyMIN_BUF_SIZEbytes into the receive buffer even after removing the VLAN tag. The result is that the guest can make us read 4 bytes off the end of a buffer by feeding itself a suitable short packet in loopback mode.The old behaviour is actually not correct, because the IEEE802.1Q standard says that the minimum ethernet frame size remains 64 bytes including the 4 checksum bytes, and so when a tag is present the payload data only needs to be 56 bytes. (A bridge implementation can choose to pad tagged frames out to 68 bytes, but it doesn't have to, and so all devices have to correctly handle incoming tagged frames that are 64 bytes long.)
The RTL8139 datasheet isn't very communicative on this topic, but there's nothing that suggests it adds extra padding on receive that didn't exist in the incoming packet.
Drop the last remnants of the padding handling from this device; this avoids overcopying into the guest when we receive a short VLAN tagged packet.
Quoted 8 lines
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3518
Signed-off-by Peter Maydell <[email protected]>---
I had been looking at this from the POV of making the generic net code add extra padding when a VLAN tag is present, but I went and read the 802.1Q spec and I now think this is just a place where the rtl8139 code got it wrong.
--
I forgot the
Cc: [email protected]
on this one.
-- PMM
Re: [PATCH 1/2] hw/net/rtl8139: Fix handling of VLAN tags on incoming short packets
In reply to Peter MaydellQuoted 34 lines · Peter Maydell
The rtl8139 receive code handles VLAN tags in incoming packets by copying the VLAN tag to a special field in the receive descriptor, and copying only the actual payload data to the receive buffer. This code tries to ensure that it pads out the payload to at least
MIN_BUF_SIZEbytes.In commit
63b901bfd30we removed the main "pad short frames" code from this device because we switched to requiring net backends to do the padding. However we didn't notice that this broke the VLAN tag handling, which relied on the old code making the buffer at leastMIN_BUF_SIZE+VLAN_HLENbytes so that it could copyMIN_BUF_SIZEbytes into the receive buffer even after removing the VLAN tag. The result is that the guest can make us read 4 bytes off the end of a buffer by feeding itself a suitable short packet in loopback mode.The old behaviour is actually not correct, because the IEEE802.1Q standard says that the minimum ethernet frame size remains 64 bytes including the 4 checksum bytes, and so when a tag is present the payload data only needs to be 56 bytes. (A bridge implementation can choose to pad tagged frames out to 68 bytes, but it doesn't have to, and so all devices have to correctly handle incoming tagged frames that are 64 bytes long.)
The RTL8139 datasheet isn't very communicative on this topic, but there's nothing that suggests it adds extra padding on receive that didn't exist in the incoming packet.
Drop the last remnants of the padding handling from this device; this avoids overcopying into the guest when we receive a short VLAN tagged packet.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3518
Signed-off-by Peter Maydell <[email protected]>
Quoted 9 lines
---
I had been looking at this from the POV of making the generic net code add extra padding when a VLAN tag is present, but I went and read the 802.1Q spec and I now think this is just a place where the rtl8139 code got it wrong.--- hw/net/rtl8139.c | 5 ----- 1 file changed, 5 deletions(-)
PATCH 2/2 hw/net/rtl8139: Send whole of vlan-tagged packet when doing loopback
In reply to Peter MaydellIn rtl8139_transfer_frame(), if we are transmitting a frame over loopback then we do this by calling qemu_receive_packet(). If we have an iovec rather than a simple buffer (which happens only when we're sending a packet where we are inserting a vlan tag), we have to convert this into a simple buffer first using iov_to_buf(). However, when we do this we forget to also update the 'size' local variable to the size of the new simple buffer, so we will truncate the packet by 4 bytes (the size of the vlan tag).
Correct the logic so we don't truncate vlan-tagged packets when sending them over loopback.
--- hw/net/rtl8139.c | 1 + 1 file changed, 1 insertion(+)
hw/net/rtl8139.c +1 -0
@@ -1765,6 +1765,7 @@ static void rtl8139_transfer_frame(RTL8139State *s, uint8_t *buf, int size, buf2 =g_malloc (buf2_size);iov_to_buf (iov,3 ,0 , buf2, buf2_size); buf = buf2; + size = buf2_size; }DPRINTF ("+++ transmit loopback mode\n" );
Signature · 1 line
2.43.0
Re: [PATCH 2/2] hw/net/rtl8139: Send whole of vlan-tagged packet when doing loopback
Quoted 13 lines · Peter Maydell
In
rtl8139_transfer_frame(), if we are transmitting a frame over loopback then we do this by callingqemu_receive_packet(). If we have an iovec rather than a simple buffer (which happens only when we're sending a packet where we are inserting a vlan tag), we have to convert this into a simple buffer first usingiov_to_buf(). However, when we do this we forget to also update the 'size' local variable to the size of the new simple buffer, so we will truncate the packet by 4 bytes (the size of the vlan tag).Correct the logic so we don't truncate vlan-tagged packets when sending them over loopback.
Quoted 7 lines
Reported-by Bin Meng <[email protected]>Signed-off-by Peter Maydell <[email protected]>--- hw/net/rtl8139.c | 1 + 1 file changed, 1 insertion(+)
Philippe Mathieu-Daudé
AppliedQuoted 9 lines · Peter Maydell
This patchset fixes a couple of bugs in the rtl8139 device related to its handling of vlan tags.
I don't think we need to be in a tearing hurry to get these fixes into git, so although I've tagged them for stable I'm expecting to get them in after we re-open for 11.2 rather than trying to squeeze them into 11.1 at this point.
Quoted 6 lines
thanks
-- PMMPeter Maydell (2):
hw/net/rtl8139: Fix handling of VLAN tags on incoming short packets hw/net/rtl8139: Send whole of vlan-tagged packet when doing loopback
Queued via hw-misc tree, thanks.