usb: eth: Check rx header lengths in r8152, asix88179 and lan7x
cover usb: eth: Check rx header lengths in r8152, asix88179 and lan7x
This is a resend of the series posted on 23 October, now with the patches inline: the first posting carried them as attachments, which b4 and Patchwork cannot use. The patches are unchanged and still apply to main.
Tom asked on another of my threads for CI results with each series:
this one ran unchanged in U-Boot's Azure CI through a CI-only pull request, together with three of my other series (ext4, EFI capsule and block/disk), and all 91 jobs passed:
https://github.com/u-boot/u-boot/pull/1064
https://dev.azure.com/u-boot/u-boot/_build/results?buildId=13698
Three of the USB Ethernet host drivers take the lengths in the headers the adapter sends with each received frame without checking them against the transfer that carried them:
- r8152 reads the rx descriptor before checking that one is there, and its bound on the packet length wraps when fewer than 28 bytes are left.
- asix88179 bounds neither the number of packet headers nor the packet lengths, as Linux ax88179_rx_fixup() has done since 2022.
- lan7x checks the frame length against what follows the first 4 bytes of its 10-byte header, and lan7x_free_pkt() steps over only 4 bytes. On LAN75xx the length also counts 2 bytes of padding, so every frame was handed up 2 bytes too long.
An adapter that sends a short or inconsistent transfer then gets the driver to hand the network stack a packet that runs past the received data, and for r8152 past the receive buffer.
Patches 1-3 add the checks, one driver each, following what the Linux drivers check. Patch 4 adds DM tests that bind each driver without a USB device behind it and feed its recv() and free_pkt() prepared transfers, and enables the drivers in sandbox_defconfig so they are built and run there. Without patches 1-3, seven of the eleven tests fail: the short and oversized transfers, the lan7x step to the next frame and the LAN75xx length. The well-formed cases pass on both, as does the r8152 descriptor length below the CRC size, which the old code rejected through its u16 wrap and which guards the change to int. With the series all eleven pass, and the rest of "ut dm" under test/py gives the same results with and without it.
I tested this in sandbox only; I have none of these adapters.
Jonas, your series "usb: eth: asix88179: Use a fixed URB buffer size and misc cleanup" from January bounds the packet header count in its ax88179_eth_recv() rewrite as well ([5/6]). Patch 2 here keeps to the checks alone, so either should rebase easily on top of the other.
Ana Bobillo (4):
usb: eth: r8152: Check the rx descriptor fits in the transfer
usb: eth: asix88179: Bound the rx packet headers and packets
usb: eth: lan7x: Account for the whole rx command header
test: dm: Add tests for the USB Ethernet rx header checks
configs/sandbox_defconfig | 5 + drivers/usb/eth/asix88179.c | 13 +- drivers/usb/eth/lan7x.c | 31 +-- drivers/usb/eth/lan7x.h | 4 + drivers/usb/eth/r8152.c | 13 +- test/dm/Makefile | 1 + test/dm/usb_eth.c | 375 ++++++++++++++++++++++++++++++++++++ 7 files changed, 426 insertions(+), 16 deletions(-) create mode 100644 test/dm/usb_eth.c
base-commit: acc808479730e13e74199119eb9b2b9113999244
PATCH RESEND 1/4 usb: eth: r8152: Check the rx descriptor fits in the transfer
From: Ana Bobillo <[email protected]>
r8152_eth_recv() reads the rx descriptor at the start of what is left of the transfer without checking that a whole descriptor is there, and then bounds the packet length with
if (packet_len > len - (sizeof(struct rx_desc) + CRC_SIZE))
which is evaluated in size_t: when fewer than 28 bytes are left it wraps and lets any packet length through, including one that wrapped in its u16 because the descriptor gave a length below CRC_SIZE. A short transfer from the adapter then hands the network stack a packet whose start and length run past the received data, and past the end of the 2 KiB receive buffer.
Require a whole descriptor and CRC before reading the descriptor, and work in int so that neither length can wrap.
--- drivers/usb/eth/r8152.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/usb/eth/r8152.c +9 -4
@@ -1471,9 +1471,8 @@ int r8152_eth_recv(struct udevice *dev, int flags, uchar **packetp)struct r8152 *tp =dev_get_priv (dev);struct ueth_data *ueth = &tp->ueth;uint8_t *ptr; -int ret, len; +int ret, len, packet_len;struct rx_desc *rx_desc; -u16 packet_len; len =usb_ether_get_rx_bytes (ueth, &ptr);debug ("%s: first try, len=%d\n" , __func__, len);
@@ -1488,12 +1487,18 @@ int r8152_eth_recv(struct udevice *dev, int flags, uchar **packetp)debug ("%s: second try, len=%d\n" , __func__, len); } +if (len < (int )(sizeof (struct rx_desc ) +CRC_SIZE )) { +debug ("Rx: incomplete packet: %d\n" , len); + goto err; + } + rx_desc = (struct rx_desc *)ptr; packet_len =le32_to_cpu (rx_desc->opts1) &RX_LEN_MASK ; packet_len -=CRC_SIZE ; -if (packet_len > len - (sizeof (struct rx_desc ) +CRC_SIZE )) { -debug ("Rx: too large packet: %d\n" , packet_len); +if (packet_len <0 || + packet_len > len - (int )(sizeof (struct rx_desc ) +CRC_SIZE )) { +debug ("Rx: bad packet length: %d\n" , packet_len); goto err; }
PATCH RESEND 2/4 usb: eth: asix88179: Bound the rx packet headers and packets
In reply to Ana BobilloFrom: Ana Bobillo <[email protected]>
ax88179_eth_recv() takes the packet count and the offset of the packet headers from the rx header at the end of the transfer, and checks only that the offset lies inside the transfer. The count is not bounded, so the driver walks packet headers past the rx header and the received data, and no packet length is bounded either, so a packet, and the start of the next one, can run past the received data.
Check what Linux ax88179_rx_fixup() checks since commit 57bc3d3ae8c1 ("net: usb: ax88179_178a: Fix out-of-bounds accesses in RX fixup"): the packet headers must fit between their offset and the rx header, and each packet with its padding in front of the packet headers. Keep the end of the packet data in asix_private for that, and drop the rest of the transfer when a packet does not fit.
--- drivers/usb/eth/asix88179.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/usb/eth/asix88179.c +12 -1
@@ -204,6 +204,7 @@ struct asix_private {struct ueth_data ueth;unsigned pkt_cnt;uint8_t *pkt_data; +uint8_t *pkt_end;uint32_t *pkt_hdr;int flags;int rx_urb_size;
@@ -543,6 +544,7 @@ void ax88179_eth_stop(struct udevice *dev)usb_ether_advance_rxbuf (ueth,-1 ); priv->pkt_cnt =0 ; priv->pkt_data =NULL ; + priv->pkt_end =NULL ; priv->pkt_hdr =NULL ; }
@@ -595,14 +597,16 @@ int ax88179_eth_recv(struct udevice *dev, int flags, uchar **packetp)return 0 ; } +/* The packet headers must lie in front of rx_hdr */ hdr_off = (u16)(rx_hdr >>16 ); -if (hdr_off > len -4 ) { +if (hdr_off +4 * pkt_cnt > len -4 ) {usb_ether_advance_rxbuf (ueth,-1 );return -EIO ; } priv->pkt_cnt = pkt_cnt; priv->pkt_data = ptr; + priv->pkt_end = ptr + hdr_off; priv->pkt_hdr = (u32 *)(ptr + hdr_off);debug ("%s: %d packets received, pkt header at %d\n" , __func__, (int )priv->pkt_cnt, (int )hdr_off);
@@ -611,6 +615,13 @@ int ax88179_eth_recv(struct udevice *dev, int flags, uchar **packetp)le32_to_cpus (priv->pkt_hdr); pkt_len = (*priv->pkt_hdr >>16 ) &0x1fff ; +/* The packet and its padding must lie in front of the packet headers */ +if (((pkt_len +7 ) &0xFFF8 ) > priv->pkt_end - priv->pkt_data) { + priv->pkt_cnt =0 ; +usb_ether_advance_rxbuf (ueth,-1 ); +return -EIO ; + } + *packetp = priv->pkt_data +2 ; priv->pkt_data += (pkt_len +7 ) &0xFFF8 ;
PATCH RESEND 3/4 usb: eth: lan7x: Account for the whole rx command header
In reply to Ana BobilloFrom: Ana Bobillo <[email protected]>
lan7x_eth_recv() returns the frame that follows rx command A, B and C, 10 bytes into the transfer, but checks only that the transfer holds command A and that the frame fits in what follows command A. A transfer of 4 to 9 bytes, or a frame length up to 6 bytes longer than what follows the header, hands the network stack a frame that runs past the received data.
On LAN75xx, command A and B are followed by 2 bytes of padding rather than command C, and the length in command A counts them, as Linux smsc75xx_rx_fixup() has it: the frame handed up was 2 bytes longer than the one received, running past the transfer. Take the padding out of the length there, so that the frame can be bounded by what follows the header on both.
lan7x_free_pkt() also steps over only 4 bytes of header, so the tail of a frame is parsed as the header of another. Step over the 10 bytes of header, the frame and the padding that aligns the next header to 4 bytes, as Linux lan78xx_rx() and smsc75xx_rx_fixup() do.
--- drivers/usb/eth/lan7x.c | 31 ++++++++++++++++++++----------- drivers/usb/eth/lan7x.h | 4 ++++ 2 files changed, 24 insertions(+), 11 deletions(-)
drivers/usb/eth/lan7x.c +20 -11
@@ -446,8 +446,8 @@ int lan7x_eth_recv(struct udevice *dev, int flags, uchar **packetp)* 1st 4 bytes contain the length of the actual data plus error info. * Extract data length. */ -if (len <sizeof (packet_len)) { -debug ("Rx: incomplete packet length\n" ); +if (len <RX_CMD_LEN ) { +debug ("Rx: incomplete packet header\n" ); goto err; }memcpy (&rx_cmd_a, ptr,sizeof (rx_cmd_a));
@@ -458,18 +458,26 @@ int lan7x_eth_recv(struct udevice *dev, int flags, uchar **packetp) } packet_len = (u16) (rx_cmd_a &RX_CMD_A_LEN_MASK ); -if (packet_len > len -sizeof (packet_len)) { -debug ("Rx: too large packet: %d\n" , packet_len); - goto err; - } -/* * For LAN7x, the length in command A does not * include command A, B, and C length. -* So use it as is. +* On LAN75xx, command A and B are followed by 2 bytes of +* padding instead, which the length does include. */ +if (priv->chipid ==ID_REV_CHIP_ID_7500 ) { +if (packet_len <RX_LAN75XX_PADDING ) { +debug ("Rx: too small packet: %d\n" , packet_len); + goto err; + } + packet_len -=RX_LAN75XX_PADDING ; + } + +if (packet_len > len -RX_CMD_LEN ) { +debug ("Rx: too large packet: %d\n" , packet_len); + goto err; + } - *packetp = ptr +10 ; + *packetp = ptr +RX_CMD_LEN ;return packet_len; err:
@@ -481,8 +489,9 @@ int lan7x_free_pkt(struct udevice *dev, uchar *packet, int packet_len) {struct lan7x_private *priv =dev_get_priv (dev); - packet_len =ALIGN (packet_len,4 ); -usb_ether_advance_rxbuf (&priv->ueth,sizeof (u32) + packet_len); +/* Skip command A, B and C, the frame and its padding to 4 bytes */ + packet_len =ALIGN (RX_CMD_LEN + packet_len,4 ); +usb_ether_advance_rxbuf (&priv->ueth, packet_len);return 0 ; }
drivers/usb/eth/lan7x.h +4 -0
@@ -23,6 +23,10 @@#define RX_CMD_A_RXE BIT(18)#define RX_CMD_A_LEN_MASK 0x00003FFF +/* Rx Command A, B and C (or padding) in front of each received frame */ +#define RX_CMD_LEN 10 +#define RX_LAN75XX_PADDING 2 +/* SCSRs */ #define ID_REV 0x00#define ID_REV_CHIP_ID_MASK 0xFFFF0000
PATCH RESEND 4/4 test: dm: Add tests for the USB Ethernet rx header checks
In reply to Ana BobilloFrom: Ana Bobillo <[email protected]>
Add DM tests that bind the r8152, ax88179, lan75xx and lan78xx drivers without probing them and hand their recv() and free_pkt() prepared transfers in the receive buffer: well-formed ones, which must come back as the packets they hold, and ones whose header or lengths run past the transfer, which must be dropped.
Enable CONFIG_USB_HOST_ETHER and these drivers in sandbox_defconfig so that they are built and tested under sandbox.
--- configs/sandbox_defconfig | 5 + test/dm/Makefile | 1 + test/dm/usb_eth.c | 375 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 381 insertions(+) create mode 100644 test/dm/usb_eth.c
configs/sandbox_defconfig +5 -0
@@ -359,6 +359,11 @@ CONFIG_USB=y CONFIG_DM_USB_GADGET=y CONFIG_USB_EMUL=y CONFIG_USB_KEYBOARD=y +CONFIG_USB_HOST_ETHER=y +CONFIG_USB_ETHER_ASIX88179=y +CONFIG_USB_ETHER_LAN75XX=y +CONFIG_USB_ETHER_LAN78XX=y +CONFIG_USB_ETHER_RTL8152=y CONFIG_USB_GADGET=y CONFIG_USB_GADGET_DOWNLOAD=y CONFIG_USB_ETHER=y
test/dm/Makefile +1 -0
@@ -150,6 +150,7 @@ obj-$(CONFIG_TEE) += tee.o obj-$(CONFIG_TIMER) += timer.o obj-$(CONFIG_TPM_V2) += tpm.o obj-$(CONFIG_DM_USB) += usb.o +obj-$(CONFIG_USB_HOST_ETHER) += usb_eth.o obj-$(CONFIG_VIDEO_SANDBOX_SDL) += video.o ifeq ($(CONFIG_VIRTIO_SANDBOX),y) obj-y += virtio.o
test/dm/usb_eth.c new file +375 -0
375 lines not on this page · raw message