All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ana Bobillo <anabobillo3@gmail.com>
To: Marek Vasut <marek.vasut+usb@mailbox.org>
Cc: Tom Rini <trini@konsulko.com>, Simon Glass <sjg@chromium.org>,
	Jonas Karlman <jonas@kwiboo.se>,
	u-boot@lists.u-boot-project.org
Subject: [PATCH RESEND 1/4] usb: eth: r8152: Check the rx descriptor fits in the transfer
Date: Thu, 29 Oct 2026 06:06:48 +0000	[thread overview]
Message-ID: <a9853b68-9331-4b1d-b202-847b8829c468@gmail.com> (raw)
In-Reply-To: <f9afa215-645f-400f-8743-eaeca3f6a2f5@gmail.com>

From: Ana Bobillo <anabobillo3@gmail.com>

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.

Signed-off-by: Ana Bobillo <anabobillo3@gmail.com>
---
 drivers/usb/eth/r8152.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/eth/r8152.c b/drivers/usb/eth/r8152.c
index e3f20e08c33..19b3f0bc22a 100644
--- a/drivers/usb/eth/r8152.c
+++ b/drivers/usb/eth/r8152.c
@@ -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;
 	}
 

  reply	other threads:[~2026-10-29  6:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-29  6:06 [PATCH RESEND 0/4] usb: eth: Check rx header lengths in r8152, asix88179 and lan7x Ana Bobillo
2026-10-29  6:06 ` Ana Bobillo [this message]
2026-10-29  6:06 ` [PATCH RESEND 2/4] usb: eth: asix88179: Bound the rx packet headers and packets Ana Bobillo
2026-10-29  6:06 ` [PATCH RESEND 3/4] usb: eth: lan7x: Account for the whole rx command header Ana Bobillo
2026-10-29  6:07 ` [PATCH RESEND 4/4] test: dm: Add tests for the USB Ethernet rx header checks Ana Bobillo

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=a9853b68-9331-4b1d-b202-847b8829c468@gmail.com \
    --to=anabobillo3@gmail.com \
    --cc=jonas@kwiboo.se \
    --cc=marek.vasut+usb@mailbox.org \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.u-boot-project.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.