net: bgmac: fix RX frame length check
Subsystems (3)
PATCH v5 1/1 net: bgmac: fix RX frame length check
While reviewing commit 77dac783f113
("net: bgmac: rate limit RX error messages"), Florian spotted that the RX length sanity check compares the length reported in the RX header against BGMAC_RX_ALLOC_SIZE. That is the size of the whole fragment, including the space reserved for struct skb_shared_info. The data area handed to the hardware is only BGMAC_RX_BUF_SIZE bytes, and the frame starts BGMAC_RX_FRAME_OFFSET bytes into it, so at most BGMAC_RX_MAX_FRAME_SIZE (1536) bytes of a frame fit in one RX buffer.
A corrupted length well beyond that limit passes the check, and the following skb_put() runs past the skb tailroom and calls skb_over_panic() instead of dropping the frame. Lengths only slightly above the limit fit in the alignment padding at the end of the buffer and were delivered, with the data past the buffer end being stale memory.
The check has no lower bound either. A length below ETH_FCS_LEN wraps when the FCS is subtracted, so skb_put() asks for almost 64 KiB and hits the same skb_over_panic(). A length that leaves less than an Ethernet header makes eth_type_trans() parse bytes the hardware never wrote.
The other limits in the driver do not agree with the buffer size either. The MAC is programmed to accept up to 32 + ETHER_MAX_LEN = 1550 bytes in bgmac_chip_init(), and up to 32 + mtu = 1564 bytes after bgmac_change_mtu(). max_mtu is BGMAC_RX_MAX_FRAME_SIZE - ETH_FCS_LEN, which leaves out the Ethernet header and any VLAN tag.
Such frames do not fit in one RX buffer. The DMA engine is the same dma64 engine used by b43 and brcmsmac, and with BGMAC_DMA_RX_OVERFLOW_CONT set it continues the frame into the next descriptor's buffer. bgmac_dma_rx_read() consumes only the first descriptor and parses the continuation buffer as a new frame, taking its RX header from frame data the sender controls. A bounds check on the length cannot catch that, and the stale memory in the rest of that buffer is passed up the stack.
Fix all of this:
- Program UMAC_MAX_FRAME_LEN from the MTU plus a VLAN tagged Ethernet header and the FCS, and limit max_mtu so that the resulting frame fits in one RX buffer. At the default MTU of 1500 this gives 1522 bytes. A DSA conduit carrying a 4 byte Broadcom tag (MTU 1504) gives 1526 bytes. max_mtu drops from 1532 to 1514. bgmac_chip_init() runs on every ndo_open and used to reset the limit to the fixed default, so use the current MTU there. bgmac_change_mtu() also never updated net_dev->mtu, so the MTU seen by the stack never changed. Set it.
- Check the RX header flags. The MAC is not set up to discard errored frames (CMD_RX_ERR_DISC is cleared), so frames above the programmed maximum still reach the host. The layout of the flags is taken from bcmgmacrxh.h in the Broadcom SDK: bits 11:8 hold the descriptor count minus one, bit 7 flags an RX FIFO overflow, bit 4 a frame longer than the maximum frame length and bit 3 a CRC error. Drop frames that have any of the error flags set and count them in the matching statistics. Drop frames that span more than one descriptor and recycle their continuation buffers without parsing them. The skip count is kept in the ring so that it carries over into the next poll. The poison check stays first, since its 0xbeef pattern has descriptor count bits set. The skip count is also read when no replacement buffer can be allocated, since that path drops the frame without parsing its header.
- Reject lengths below ETH_HLEN + ETH_FCS_LEN or above BGMAC_RX_MAX_FRAME_SIZE, and print the bad length.
Only compile tested. Not tested on hardware.
---
v5:
- read the descriptor count from the RX header before poisoning it when no replacement buffer can be allocated, so the continuation buffers of that frame are not parsed as frames either
- v4: https://lore.kernel.org/netdev/[email protected]/
v4:
- program UMAC_MAX_FRAME_LEN from the MTU plus a VLAN Ethernet header and FCS, and limit max_mtu to 1514, so every frame the MAC accepts by its programmed limit fits in one RX buffer
- check the RX header flags: drop errored frames and recycle the continuation buffers of frames that span more than one descriptor instead of parsing them as frames
- correct the commit message: lengths just above the limit fit in the SKB_DATA_ALIGN padding and were delivered, not panicked on
- AI coding assistant (Claude) used to help analyse the review and prepare this version; I have reviewed the result
v3: add a lower bound
v2: replace Suggested-by with description.
drivers/net/ethernet/broadcom/bgmac.c | 72 +++++++++++++++++++++++---- drivers/net/ethernet/broadcom/bgmac.h | 15 +++++- 2 files changed, 76 insertions(+), 11 deletions(-)
drivers/net/ethernet/broadcom/bgmac.c +63 -9
@@ -10,6 +10,7 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/bcma/bcma.h> +#include <linux/bitfield.h> #include <linux/etherdevice.h> #include <linux/interrupt.h> #include <linux/bcm47xx_nvram.h>
@@ -401,13 +402,25 @@ static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac, ring->end = desc_idx; } +/* If rx_skip is given, the buffer holds the start of a frame. Store the +* number of continuation descriptors of that frame, so that they are not +* parsed as frames. +*/ static void bgmac_dma_rx_poison_buf (struct device *dma_dev, -struct bgmac_slot_info *slot) +struct bgmac_slot_info *slot, +u8 *rx_skip) {struct bgmac_rx_header *rx = slot->buf +BGMAC_RX_BUF_OFFSET ; +u16 len, flags;dma_sync_single_for_cpu (dma_dev, slot->dma_addr,BGMAC_RX_BUF_SIZE ,DMA_FROM_DEVICE ); +if (rx_skip) { + len =le16_to_cpu (rx->len); + flags =le16_to_cpu (rx->flags); +if (len !=0xdead || flags !=0xbeef ) + *rx_skip =FIELD_GET (BGMAC_RXF_DC_MASK , flags); + } rx->len =cpu_to_le16 (0xdead ); rx->flags =cpu_to_le16 (0xbeef ); dma_sync_single_for_device(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
@@ -435,10 +448,18 @@ static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,dma_addr_t dma_addr = slot->dma_addr;u16 len, flags; +/* The rest of a multi descriptor frame that was dropped */ +if (ring->rx_skip) { + ring->rx_skip--; +bgmac_dma_rx_poison_buf (dma_dev, slot,NULL ); + goto next; + } + do { /* Prepare new skb as replacement */ if (bgmac_dma_rx_skb_for_slot(bgmac, slot)) { - bgmac_dma_rx_poison_buf(dma_dev, slot); + bgmac_dma_rx_poison_buf(dma_dev, slot, + &ring->rx_skip); break; }
@@ -460,10 +481,34 @@ static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,break ; } -if (len >BGMAC_RX_ALLOC_SIZE ) { +/* The frame did not fit in one buffer. The following +* buffers hold the rest of its data, not RX headers. +*/ +if (flags &BGMAC_RXF_DC_MASK ) { + ring->rx_skip =FIELD_GET (BGMAC_RXF_DC_MASK , flags); +put_page (virt_to_head_page (buf)); + bgmac->net_dev->stats.rx_length_errors++; + bgmac->net_dev->stats.rx_errors++; +break ; + } + +if (flags &BGMAC_RXF_ERR ) { +put_page (virt_to_head_page (buf)); +if (flags &BGMAC_RXF_OVF ) + bgmac->net_dev->stats.rx_fifo_errors++; +if (flags &BGMAC_RXF_OVERSIZE ) + bgmac->net_dev->stats.rx_length_errors++; +if (flags &BGMAC_RXF_CRC ) + bgmac->net_dev->stats.rx_crc_errors++; + bgmac->net_dev->stats.rx_errors++; +break ; + } + +if (len <ETH_HLEN +ETH_FCS_LEN || + len >BGMAC_RX_MAX_FRAME_SIZE ) {if (net_ratelimit ()) -netdev_err (bgmac->net_dev,"Found oversized packet at slot %d, DMA issue!\n" , - ring->start); +netdev_err (bgmac->net_dev,"Found packet with invalid length %u at slot %d, DMA issue!\n" , + len, ring->start);put_page (virt_to_head_page (buf)); bgmac->net_dev->stats.rx_length_errors++; bgmac->net_dev->stats.rx_errors++;
@@ -493,6 +538,7 @@ static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring, handled++; }while (0 ); +next:bgmac_dma_rx_setup_desc (bgmac, ring, ring->start);if (++ring->start >=BGMAC_RX_RING_SLOTS )
@@ -735,6 +781,7 @@ static int bgmac_dma_init(struct bgmac *bgmac) ring->start =0 ; ring->end =0 ; + ring->rx_skip =0 ;for (j =0 ; j <BGMAC_RX_RING_SLOTS ; j++) { err =bgmac_dma_rx_skb_for_slot (bgmac, &ring->slots[j]);if (err)
@@ -1110,6 +1157,13 @@ static void bgmac_enable(struct bgmac *bgmac) } } +/* The MAC counts the FCS in the maximum frame length. */ +static void bgmac_set_max_frame_len (struct bgmac *bgmac,int mtu) +{ +bgmac_umac_write (bgmac,UMAC_MAX_FRAME_LEN , + mtu +VLAN_ETH_HLEN +ETH_FCS_LEN ); +} +/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */ static void bgmac_chip_init (struct bgmac *bgmac) {
@@ -1131,7 +1185,7 @@ static void bgmac_chip_init(struct bgmac *bgmac)else bgmac_umac_cmd_maskset (bgmac, ~CMD_LCL_LOOP_EN ,0 , false); -bgmac_umac_write (bgmac,UMAC_MAX_FRAME_LEN ,32 +ETHER_MAX_LEN ); +bgmac_set_max_frame_len (bgmac, bgmac->net_dev->mtu);bgmac_chip_intrs_on (bgmac);
@@ -1267,7 +1321,8 @@ static int bgmac_change_mtu(struct net_device *net_dev, int mtu) {struct bgmac *bgmac =netdev_priv (net_dev); -bgmac_umac_write (bgmac,UMAC_MAX_FRAME_LEN ,32 + mtu); +bgmac_set_max_frame_len (bgmac, mtu); +WRITE_ONCE (net_dev->mtu, mtu);return 0 ; }
@@ -1559,8 +1614,7 @@ int bgmac_enet_probe(struct bgmac *bgmac) net_dev->hw_features = net_dev->features; net_dev->vlan_features = net_dev->features; -/* Omit FCS from max MTU size */ - net_dev->max_mtu =BGMAC_RX_MAX_FRAME_SIZE -ETH_FCS_LEN ; + net_dev->max_mtu =BGMAC_RX_MAX_MTU ; bgmac->in_init = false;
drivers/net/ethernet/broadcom/bgmac.h +13 -2
@@ -2,6 +2,7 @@#ifndef _BGMAC_H#define _BGMAC_H +#include <linux/if_vlan.h> #include <linux/netdevice.h> #include "unimac.h"
@@ -330,6 +331,9 @@ BGMAC_RX_FRAME_OFFSET)#define BGMAC_RX_MAX_FRAME_SIZE 1536#define BGMAC_RX_BUF_SIZE (BGMAC_RX_FRAME_OFFSET + BGMAC_RX_MAX_FRAME_SIZE) +/* Largest MTU whose VLAN tagged frames, FCS included, fit in one RX buffer */ +#define BGMAC_RX_MAX_MTU (BGMAC_RX_MAX_FRAME_SIZE - VLAN_ETH_HLEN - \ + ETH_FCS_LEN)#define BGMAC_RX_ALLOC_SIZE (SKB_DATA_ALIGN(BGMAC_RX_BUF_SIZE + BGMAC_RX_BUF_OFFSET) + \ SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
@@ -363,8 +367,6 @@#define BGMAC_CHIPCTL_7_IF_TYPE_MII 0x00000040#define BGMAC_CHIPCTL_7_IF_TYPE_RGMII 0x00000080 -#define ETHER_MAX_LEN (ETH_FRAME_LEN + ETH_FCS_LEN) -/* Feature Flags */ #define BGMAC_FEAT_TX_MASK_SETUP BIT(0)#define BGMAC_FEAT_RX_MASK_SETUP BIT(1)
@@ -426,6 +428,7 @@ struct bgmac_dma_ring {u32 index_base;/* Used for unaligned rings only, otherwise 0 */ u16 mmio_base;bool unaligned; +u8 rx_skip;/* Continuation slots of a dropped RX frame */ struct bgmac_slot_info slots[BGMAC_RX_RING_SLOTS ]; };
@@ -436,6 +439,14 @@ struct bgmac_rx_header {__le16 pad[12 ]; }; +/* bgmac_rx_header::flags */ +#define BGMAC_RXF_DC_MASK 0x0f00/* Descriptors used - 1 */ +#define BGMAC_RXF_OVF 0x0080/* RX FIFO overflow */ +#define BGMAC_RXF_OVERSIZE 0x0010/* Frame > max frame length */ +#define BGMAC_RXF_CRC 0x0008/* CRC error */ +#define BGMAC_RXF_ERR (BGMAC_RXF_OVF | BGMAC_RXF_OVERSIZE | \ + BGMAC_RXF_CRC) + struct bgmac { union { struct {
base-commit: fdb6fcabdbb8247f1647d54fb1b9dc3575d7fcc8 --
2.56.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] The continuation-buffer skip added by this patch still lets continuation buffers be parsed as new frames, which is the bug class…
- [Medium] Tying UMAC_MAX_FRAME_LEN to the MTU and also dropping every frame flagged BGMAC_RXF_OVERSIZE makes the driver drop frames that fit…
- [Low] Some of the new RX error accounting in bgmac_dma_rx_read() does not follow the rtnl_link_stats64 guidance in…
--
net: bgmac: fix RX frame length check
This patch limits the RX length check in bgmac_dma_rx_read() to the RX buffer size and adds a lower bound. It also parses the RX header flags so that errored frames are dropped and the continuation buffers of multi-descriptor frames are skipped. UMAC_MAX_FRAME_LEN is now programmed from the MTU plus a VLAN header and FCS, and max_mtu is lowered to match.
Quoted 4 lines
drivers/net/ethernet/broadcom/bgmac.c +0
-0
[ ... ]
Quoted 5 lines
drivers/net/ethernet/broadcom/bgmac.c +0
-1@@ -460,10 +481,34 @@ static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,break ; } -if (len >BGMAC_RX_ALLOC_SIZE ) {
drivers/net/ethernet/broadcom/bgmac.c static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring, · line 493
Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.c +10
-0+/* The frame did not fit in one buffer. The following +* buffers hold the rest of its data, not RX headers. +*/ +if (flags &BGMAC_RXF_DC_MASK ) { + ring->rx_skip =FIELD_GET (BGMAC_RXF_DC_MASK , flags); +put_page (virt_to_head_page (buf)); + bgmac->net_dev->stats.rx_length_errors++; + bgmac->net_dev->stats.rx_errors++; +break ; + }
[Severity: Medium]
Can continuation buffers still be parsed as new frames here? The only source for ring->rx_skip is the 4-bit descriptor count of a first-buffer header that is not poisoned. The len in that same header is never used to derive the count or cross-check it.
bgmac_dma_rx_enable() sets BGMAC_DMA_RX_OVERFLOW_CONT. With that set, there seem to be a few ways for rx_skip to stay 0, or to be too small, while continuation buffers are still in the ring.
First, the first slot's header may still read as poison when the driver reaches it. The poison branch just before this hunk then drops the slot without setting rx_skip:
if (len == 0xdead && flags == 0xbeef) {
...
put_page(virt_to_head_page(buf));
bgmac->net_dev->stats.rx_errors++;
break;
}The alloc-failure path in bgmac_dma_rx_poison_buf() has the same gap:
if (len != 0xdead || flags != 0xbeef) *rx_skip = FIELD_GET(BGMAC_RXF_DC_MASK, flags);
b43's dma_rx() runs on the same dma64 engine. It retries with udelay(2) while frame_len is still 0, which suggests a slot can become visible before its RX header is written. For a multi-descriptor frame, the header can only be written at end of frame.
Second, len may be above BGMAC_RX_MAX_FRAME_SIZE while the DC field reads as 0. The length check below then drops the frame without setting rx_skip, even though a frame that long must have spilled into the next buffer.
Third, a frame may use more than 16 descriptors. The 0x0f00 mask caps the skip count at 15.
In each case the next slot is handled as a frame start, and its len/flags are the first 4 bytes of payload the sender controls. A value like len=1536, flags=0 passes the poison, DC, error and length checks. Up to 1532 bytes are then built into an skb. If the DMA engine wrote only a short tail into that buffer, the rest is stale memory from netdev_alloc_frag().
It has not been confirmed that bgmac hardware produces each of these conditions. b43 dma_rx() and brcmsmac dma_rx() work out the continuation count from the length instead (resid = len - (rxbufsize - rxoffset)).
Should rx_skip be derived from len here as well?
drivers/net/ethernet/broadcom/bgmac.c static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring, · line 498
Quoted 5 lines
drivers/net/ethernet/broadcom/bgmac.c +5
-0+ +if (flags &BGMAC_RXF_ERR ) { +put_page (virt_to_head_page (buf)); +if (flags &BGMAC_RXF_OVF ) + bgmac->net_dev->stats.rx_fifo_errors++;
[Severity: Low]
Should this be rx_over_errors instead? The rtnl_link_stats64 kernel-doc in include/uapi/linux/if_link.h describes rx_over_errors as the "Receiver FIFO overflow event counter". It also says rx_fifo_errors is "Not recommended for use in drivers for high speed interfaces".
The BGMAC_RXF_DC_MASK branch above has a similar issue. It counts frames that spanned more than one buffer in rx_length_errors. The recommended interpretation of rx_over_errors is the "number of packets dropped because they did not fit into buffers provided by the host".
Quoted 10 lines
drivers/net/ethernet/broadcom/bgmac.c +10
-0+if (flags &BGMAC_RXF_OVERSIZE ) + bgmac->net_dev->stats.rx_length_errors++; +if (flags &BGMAC_RXF_CRC ) + bgmac->net_dev->stats.rx_crc_errors++; + bgmac->net_dev->stats.rx_errors++; +break ; + } + +if (len <ETH_HLEN +ETH_FCS_LEN || + len >BGMAC_RX_MAX_FRAME_SIZE ) {
[ ... ]
Quoted 15 lines
drivers/net/ethernet/broadcom/bgmac.c +7
-0@@ -1110,6 +1157,13 @@ static void bgmac_enable(struct bgmac *bgmac) } } +/* The MAC counts the FCS in the maximum frame length. */ +static void bgmac_set_max_frame_len (struct bgmac *bgmac,int mtu) +{ +bgmac_umac_write (bgmac,UMAC_MAX_FRAME_LEN , + mtu +VLAN_ETH_HLEN +ETH_FCS_LEN ); +} +/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */ static void bgmac_chip_init (struct bgmac *bgmac) {
drivers/net/ethernet/broadcom/bgmac.c static void bgmac_chip_init(struct bgmac *bgmac) · line 1188
Quoted 5 lines
drivers/net/ethernet/broadcom/bgmac.c +1
-1@@ -1131,7 +1185,7 @@ static void bgmac_chip_init(struct bgmac *bgmac)else bgmac_umac_cmd_maskset (bgmac, ~CMD_LCL_LOOP_EN ,0 , false); -bgmac_umac_write (bgmac,UMAC_MAX_FRAME_LEN ,32 +ETHER_MAX_LEN ); +bgmac_set_max_frame_len (bgmac, bgmac->net_dev->mtu);
[Severity: Medium]
Will frames that fit in one RX buffer now be dropped? This ties UMAC_MAX_FRAME_LEN to the MTU, and bgmac_dma_rx_read() now drops every frame that has BGMAC_RXF_OVERSIZE set.
Before this patch, bgmac_chip_init() programmed a fixed
32 + ETHER_MAX_LEN = 1550, and the RX flags were never checked. Any frame up to BGMAC_RX_MAX_FRAME_SIZE (1536 including FCS) was delivered.
At MTU 1500 the limit is now 1522. bgmac_chip_reset() clears CMD_RX_ERR_DISC, so longer frames still reach the host with OVERSIZE set. The new BGMAC_RXF_ERR branch in bgmac_dma_rx_read() then drops them as rx_length_errors.
For example, an 802.1ad double-tagged frame with a 1500 byte payload is 14 + 8 + 1500 + 4 = 1526 bytes. It fits in the 1536 byte buffer and was received before this patch, but now it looks like it gets dropped.
Lowering the MTU in bgmac_change_mtu() (for example to 1400) also makes the MAC reject every inbound frame longer than MTU + 22 bytes. Before this patch, the MTU had no effect on RX.
The commit message states the goal as every frame the MAC accepts fitting in one RX buffer. A fixed limit of BGMAC_RX_MAX_FRAME_SIZE plus max_mtu = BGMAC_RX_MAX_MTU would meet that goal without adding an MTU-based RX filter. Would that work here?
If the MTU-based RX filter is intended, could the commit message say so?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122080257.119640-1-rosenp%40gmail.com