Skip to contentSkip to conversation

net: enetc: fix array underflow in error handling code

PATCH Applied Dan Carpenter 4 participants 4 messages Apr 9, 2021 – Apr 10, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: enetc: fix array underflow in error handling code

This loop will try to unmap enetc_unmap_tx_buff[-1] and crash.

Fixes 9d2b68cc108d ("net: enetc: add support for XDP_REDIRECT")
---
 drivers/net/ethernet/freescale/enetc/enetc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/freescale/enetc/enetc.c +1 -1
index 57049ae97201..d86395775ed0 100644
@@ -895,7 +895,7 @@ static int enetc_xdp_frame_to_xdp_tx_swbd(struct enetc_bdr *tx_ring,
 		dma = dma_map_single(tx_ring->dev, data, len, DMA_TO_DEVICE);
 		if (unlikely(dma_mapping_error(tx_ring->dev, dma))) {
 			/* Undo the DMA mapping for all fragments */
-			while (n-- >= 0)
+			while (--n >= 0)
 				enetc_unmap_tx_buff(tx_ring, &xdp_tx_arr[n]);
 
 			netdev_err(tx_ring->ndev, "DMA map error\n");
Signature · 1 line
2.30.2
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Dan Carpenter
Quoted 24 lines · Dan Carpenter

This loop will try to unmap enetc_unmap_tx_buff[-1] and crash.

Fixes 9d2b68cc108d ("net: enetc: add support for XDP_REDIRECT")
---
 drivers/net/ethernet/freescale/enetc/enetc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/freescale/enetc/enetc.c +1 -1
index 57049ae97201..d86395775ed0 100644
@@ -895,7 +895,7 @@ static int enetc_xdp_frame_to_xdp_tx_swbd(struct enetc_bdr *tx_ring,
 		dma = dma_map_single(tx_ring->dev, data, len, DMA_TO_DEVICE);
 		if (unlikely(dma_mapping_error(tx_ring->dev, dma))) {
 			/* Undo the DMA mapping for all fragments */
-			while (n-- >= 0)
+			while (--n >= 0)
 				enetc_unmap_tx_buff(tx_ring, &xdp_tx_arr[n]);
 
 			netdev_err(tx_ring->ndev, "DMA map error\n");
Signature · 1 line
2.30.2

I have no idea what went wrong. I distinctly remember "testing" this by writing up a small program:

#include <stdio.h>

int main(void)

{
	int n = 5;

	while (n-- >= 0)
		printf("%d\n", n);

	return 0;
}

I find myself doing that most of the time when there is a need for an unrolling loop. I do see that this goes down to -1, I wonder how I missed it.

#3 lore raw
In reply to Dan Carpenter
This patch was applied to netdev/net-next.git (refs/heads/master):

Hello:

This patch was applied to netdev/net-next.git (refs/heads/master):

Quoted 7 lines · On Fri, 9 Apr 2021 15:24:28 +0300 you wrote:

This loop will try to unmap enetc_unmap_tx_buff[-1] and crash.

Fixes 9d2b68cc108d ("net: enetc: add support for XDP_REDIRECT")
---
 drivers/net/ethernet/freescale/enetc/enetc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Here is the summary with links:
- [net-next] net: enetc: fix array underflow in error handling code

    https://git.kernel.org/netdev/net-next/c/626b598aa8be

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html