net: fec: reject oversized fragments before bounce-buffer memcpy
Subsystems (5)
PATCH v2 1/1 net: fec: reject oversized fragments before bounce-buffer memcpy
fec_enet_txq_submit_frag_skb() and fec_enet_txq_submit_skb() copy transmit data into a per-ring bounce buffer when the source address is misaligned or the FEC_QUIRK_SWAP_FRAME quirk is active. The bounce buffers are allocated as kmalloc(FEC_ENET_TX_FRSIZE) where FEC_ENET_TX_FRSIZE is 2048 bytes.
However, skb_frag_size() can return up to PAGE_SIZE (4096 on most architectures) for scatter-gather fragments, and skb_headlen() can exceed 2048 for certain GSO packets. When this happens the memcpy overflows the bounce buffer by up to 2048 bytes, corrupting adjacent heap objects.
Add a length check before each bounce-buffer copy. If the data exceeds the bounce buffer capacity, fall through to the error path rather than performing the out-of-bounds write.
--- drivers/net/ethernet/freescale/fec_main.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/net/ethernet/freescale/fec_main.c +4 -0
@@ -558,6 +558,8 @@ fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq, index =fec_enet_get_bd_index (bdp, &txq->bd);if (((unsigned long ) bufaddr) & fep->tx_align || fep->quirks &FEC_QUIRK_SWAP_FRAME ) { +if (frag_len >FEC_ENET_TX_FRSIZE ) + goto dma_mapping_error;memcpy (txq->tx_bounce[index], bufaddr, frag_len); bufaddr = txq->tx_bounce[index];
@@ -634,6 +636,8 @@ static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq, index =fec_enet_get_bd_index (bdp, &txq->bd);if (((unsigned long ) bufaddr) & fep->tx_align || fep->quirks &FEC_QUIRK_SWAP_FRAME ) { +if (buflen >FEC_ENET_TX_FRSIZE ) + goto release;memcpy (txq->tx_bounce[index], skb->data, buflen); bufaddr = txq->tx_bounce[index];
Signature · 1 line
2.43.0
Quoted 3 lines · Aldo Ariel Panzardo
fec_enet_txq_submit_frag_skb()andfec_enet_txq_submit_skb()copy transmit data into a per-ring bounce buffer when the source address is misaligned or theFEC_QUIRK_SWAP_FRAMEquirk is active.
ndev->max_mtu = fep->max_buf_size - VLAN_ETH_HLEN - ETH_FCS_LEN;
What value is this? Is it greater than, or smaller than FEC_ENET_TX_FRSIZE?
Andrew
A message that is not in the archive (sent off-list or not yet received): CAP48Hfto4qc285B3Jw7ZRuC-wGf57JOBPZOdrrAt-aDfGcas8Q@mail.gmail.com
Quoted 8 lines · Aldo Ariel
Hi Andrew,
PKT_MAXBUF_SIZE=round_down(2048 - 64, 64)= 1984max_mtu(non-jumbo) = 1984 -VLAN_ETH_HLEN-ETH_FCS_LEN= 1962So for standard MTU,
max_mtu<FEC_ENET_TX_FRSIZEand individual fragments should not exceed the bounce buffer.
Quoted 7 lines
However, when the device supports jumbo frames,
max_buf_size=MAX_JUMBO_BUF_SIZE(~16256) andmax_mtureaches ~16234. Scatter-gather fragments in that case can exceedFEC_ENET_TX_FRSIZEwhile still needing the bounce path for alignment or byte-swap.Should I respin with a check only for the jumbo path, or is the unconditional guard acceptable?
Can bounce buffers and jumbo be used at the same time?
How is the scatter-gather size determined? Maybe the better fix is to set the fragment size based on the size of the bounce buffer when it is in use?
Andrew
Shenwei Wang
reviewerNXP Confidential
Quoted 30 lines
-----Original Message-----
From: Andrew Lunn <[email protected]>
Sent: Thursday, September 24, 2026 7:41 AM
To: Aldo Ariel <[email protected]>
Cc: Wei Fang <[email protected]>; Frank Li <[email protected]>; Shenwei Wang <[email protected]>; [email protected]; [email protected]; linux- [email protected]; [email protected]; Sashiko <sashiko- [email protected]>
Subject: [EXT] Re: [PATCH net v2] net: fec: reject oversized fragments before bounce-buffer memcpyCaution: This is an external email. Please take care when clicking links or opening attachments. When in doubt, report the message using the 'Report this email' button
Quoted 13 lines · Aldo Ariel
Hi Andrew,
PKT_MAXBUF_SIZE=round_down(2048 - 64, 64)= 1984max_mtu(non-jumbo) = 1984 -VLAN_ETH_HLEN-ETH_FCS_LEN= 1962So for standard MTU,
max_mtu<FEC_ENET_TX_FRSIZEand individual fragments should not exceed the bounce buffer.However, when the device supports jumbo frames,
max_buf_size=MAX_JUMBO_BUF_SIZE(~16256) andmax_mtureaches ~16234. Scatter-gather fragments in that case can exceedFEC_ENET_TX_FRSIZEwhile still needing the bounce path for alignment or byte-swap.
Quoted 5 lines
Quoted 2 lines
Should I respin with a check only for the jumbo path, or is the unconditional guard acceptable?
Can bounce buffers and jumbo be used at the same time?
No. Bounce buffer is only available on i.mx28.
Shenwei
Quoted 4 lines
How is the scatter-gather size determined? Maybe the better fix is to set the fragment size based on the size of the bounce buffer when it is in use?
Andrew
A message that is not in the archive (sent off-list or not yet received): CAP48HfuR9_d2KZ7ygG4fMq5ekSb2zaaiEzo_fq_mOWODN5Os6A@mail.gmail.com
Quoted 11 lines · Aldo Ariel
Hi Andrew, Wei, Shenwei,
Thank you for the review. Shenwei confirmed that bounce buffers are only available on i.MX28 and cannot coexist with jumbo frames. That means the overflow path I described (jumbo fragments exceeding
FEC_ENET_TX_FRSIZE) is not reachable in practice: on i.MX28 themax_mtustays below the bounce buffer capacity.Given that, should I respin as a defense-in-depth guard (with the goto label fixed per Wei's note), or would you prefer I withdraw the patch?
I suggest adding a comment somewhere to explain that jumbo frames and bounce buffers are mutually exclusive by hardware design.
We don't use defensive code, because that suggests we don't actually understand the code!
Andrew
Wei Fang
maintainerQuoted 40 lines
fec_enet_txq_submit_frag_skb()andfec_enet_txq_submit_skb()copy transmit data into a per-ring bounce buffer when the source address is misaligned or theFEC_QUIRK_SWAP_FRAMEquirk is active. The bounce buffers are allocated askmalloc(FEC_ENET_TX_FRSIZE)whereFEC_ENET_TX_FRSIZEis 2048 bytes.However,
skb_frag_size()can return up toPAGE_SIZE(4096 on most architectures) for scatter-gather fragments, andskb_headlen()can exceed 2048 for certain GSO packets. When this happens the memcpy overflows the bounce buffer by up to 2048 bytes, corrupting adjacent heap objects.Add a length check before each bounce-buffer copy. If the data exceeds the bounce buffer capacity, fall through to the error path rather than performing the out-of-bounds write.
Fixes 6605b730c061 ("FEC: Add alignment handling for FEC driver")Reported-by Sashiko <[email protected]>Signed-off-by Aldo Ariel Panzardo <[email protected]>--- drivers/net/ethernet/freescale/fec_main.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/net/ethernet/freescale/fec_main.c +2
-0@@ -558,6 +558,8 @@ fec_enet_txq_submit_frag_skb(structfec_enet_priv_tx_q *txq, index =fec_enet_get_bd_index (bdp, &txq->bd);if (((unsigned long ) bufaddr) & fep->tx_align || fep->quirks &FEC_QUIRK_SWAP_FRAME ) { +if (frag_len >FEC_ENET_TX_FRSIZE ) + goto dma_mapping_error;memcpy (txq->tx_bounce[index], bufaddr, frag_len); bufaddr = txq->tx_bounce[index];
drivers/net/ethernet/freescale/fec_main.c static int fec_enet_txq_submit_skb(struct · line 641
Quoted 6 lines
drivers/net/ethernet/freescale/fec_main.c +2
-0@@ -634,6 +636,8 @@ static int fec_enet_txq_submit_skb(structfec_enet_priv_tx_q *txq, index =fec_enet_get_bd_index (bdp, &txq->bd);if (((unsigned long ) bufaddr) & fep->tx_align || fep->quirks &FEC_QUIRK_SWAP_FRAME ) { +if (buflen >FEC_ENET_TX_FRSIZE ) + goto release;
Where is the 'release' label, I could not find it in latest net tree.
Also, do not repost a new version within 24 hours.
https://elixir.bootlin.com/linux/v7.3-rc3/source/Documentation/process/maintainer-netdev.rst#L15
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] net: fec: build failure due to undefined label release --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] net: fec: build failure due to undefined label release
--
commit 07a88506b223c9e67d1b8c1a1dfa027140cd954a
Author: Aldo Ariel Panzardo <[email protected]>
net: fec: reject oversized fragments before bounce-buffer memcpy
This patch adds length checks before bounce-buffer copies in fec_enet_txq_submit_frag_skb() and fec_enet_txq_submit_skb(). It aims to prevent buffer overflows by jumping to the error path when the data exceeds the bounce buffer capacity.
Quoted 4 lines
drivers/net/ethernet/freescale/fec_main.c +0
-0
[ ... ]
drivers/net/ethernet/freescale/fec_main.c static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq, · line 640
Quoted 6 lines
drivers/net/ethernet/freescale/fec_main.c +2
-0@@ -634,6 +636,8 @@ static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq, index =fec_enet_get_bd_index (bdp, &txq->bd);if (((unsigned long ) bufaddr) & fep->tx_align || fep->quirks &FEC_QUIRK_SWAP_FRAME ) { +if (buflen >FEC_ENET_TX_FRSIZE ) + goto release;
[Severity: Low]
Does the label 'release' exist in fec_enet_txq_submit_skb()? It looks like this might cause a build failure due to an undefined label.
Additionally, if a release path is added, would we also need to ensure the skb is freed (for example, via dev_kfree_skb_any) to avoid a memory leak?
Quoted 2 lines
drivers/net/ethernet/freescale/fec_main.c +0
-0memcpy (txq->tx_bounce[index], skb->data, buflen); bufaddr = txq->tx_bounce[index];
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1