net: ethernet: nixge: fix NULL dereference

Message ID 20221124084303.2075092-1-YKarpov@ispras.ru
State New
Headers
Series net: ethernet: nixge: fix NULL dereference |

Commit Message

Yuri Karpov Nov. 24, 2022, 8:43 a.m. UTC
  In function nixge_hw_dma_bd_release() dereference of NULL pointer
priv->rx_bd_v is possible for the case of its allocation failure in
nixge_hw_dma_bd_init().

Move for() loop with priv->rx_bd_v dereference under the check for
its validity.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 492caffa8a1a ("net: ethernet: nixge: Add support for National Instruments XGE netdev")
Signed-off-by: Yuri Karpov <YKarpov@ispras.ru>
---
 drivers/net/ethernet/ni/nixge.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
  

Comments

Maciej Fijalkowski Nov. 25, 2022, 1:30 p.m. UTC | #1
On Thu, Nov 24, 2022 at 11:43:03AM +0300, Yuri Karpov wrote:
> In function nixge_hw_dma_bd_release() dereference of NULL pointer
> priv->rx_bd_v is possible for the case of its allocation failure in
> nixge_hw_dma_bd_init().
> 
> Move for() loop with priv->rx_bd_v dereference under the check for
> its validity.
> 
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
> 
> Fixes: 492caffa8a1a ("net: ethernet: nixge: Add support for National Instruments XGE netdev")
> Signed-off-by: Yuri Karpov <YKarpov@ispras.ru>

Reviewed-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>

> ---
>  drivers/net/ethernet/ni/nixge.c | 29 +++++++++++++++--------------
>  1 file changed, 15 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/net/ethernet/ni/nixge.c b/drivers/net/ethernet/ni/nixge.c
> index 19d043b593cc..62320be4de5a 100644
> --- a/drivers/net/ethernet/ni/nixge.c
> +++ b/drivers/net/ethernet/ni/nixge.c
> @@ -249,25 +249,26 @@ static void nixge_hw_dma_bd_release(struct net_device *ndev)
>  	struct sk_buff *skb;
>  	int i;
>  
> -	for (i = 0; i < RX_BD_NUM; i++) {
> -		phys_addr = nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[i],
> -						     phys);
> -
> -		dma_unmap_single(ndev->dev.parent, phys_addr,
> -				 NIXGE_MAX_JUMBO_FRAME_SIZE,
> -				 DMA_FROM_DEVICE);
> -
> -		skb = (struct sk_buff *)(uintptr_t)
> -			nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[i],
> -						 sw_id_offset);
> -		dev_kfree_skb(skb);
> -	}
> +	if (priv->rx_bd_v) {
> +		for (i = 0; i < RX_BD_NUM; i++) {
> +			phys_addr = nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[i],
> +							     phys);
> +
> +			dma_unmap_single(ndev->dev.parent, phys_addr,
> +					 NIXGE_MAX_JUMBO_FRAME_SIZE,
> +					 DMA_FROM_DEVICE);
> +
> +			skb = (struct sk_buff *)(uintptr_t)
> +				nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[i],
> +							 sw_id_offset);
> +			dev_kfree_skb(skb);
> +		}
>  
> -	if (priv->rx_bd_v)
>  		dma_free_coherent(ndev->dev.parent,
>  				  sizeof(*priv->rx_bd_v) * RX_BD_NUM,
>  				  priv->rx_bd_v,
>  				  priv->rx_bd_p);
> +	}
>  
>  	if (priv->tx_skb)
>  		devm_kfree(ndev->dev.parent, priv->tx_skb);
> -- 
> 2.34.1
>
  
patchwork-bot+netdevbpf@kernel.org Nov. 28, 2022, 11:20 a.m. UTC | #2
Hello:

This patch was applied to netdev/net.git (master)
by David S. Miller <davem@davemloft.net>:

On Thu, 24 Nov 2022 11:43:03 +0300 you wrote:
> In function nixge_hw_dma_bd_release() dereference of NULL pointer
> priv->rx_bd_v is possible for the case of its allocation failure in
> nixge_hw_dma_bd_init().
> 
> Move for() loop with priv->rx_bd_v dereference under the check for
> its validity.
> 
> [...]

Here is the summary with links:
  - net: ethernet: nixge: fix NULL dereference
    https://git.kernel.org/netdev/net/c/9256db4e45e8

You are awesome, thank you!
  

Patch

diff --git a/drivers/net/ethernet/ni/nixge.c b/drivers/net/ethernet/ni/nixge.c
index 19d043b593cc..62320be4de5a 100644
--- a/drivers/net/ethernet/ni/nixge.c
+++ b/drivers/net/ethernet/ni/nixge.c
@@ -249,25 +249,26 @@  static void nixge_hw_dma_bd_release(struct net_device *ndev)
 	struct sk_buff *skb;
 	int i;
 
-	for (i = 0; i < RX_BD_NUM; i++) {
-		phys_addr = nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[i],
-						     phys);
-
-		dma_unmap_single(ndev->dev.parent, phys_addr,
-				 NIXGE_MAX_JUMBO_FRAME_SIZE,
-				 DMA_FROM_DEVICE);
-
-		skb = (struct sk_buff *)(uintptr_t)
-			nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[i],
-						 sw_id_offset);
-		dev_kfree_skb(skb);
-	}
+	if (priv->rx_bd_v) {
+		for (i = 0; i < RX_BD_NUM; i++) {
+			phys_addr = nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[i],
+							     phys);
+
+			dma_unmap_single(ndev->dev.parent, phys_addr,
+					 NIXGE_MAX_JUMBO_FRAME_SIZE,
+					 DMA_FROM_DEVICE);
+
+			skb = (struct sk_buff *)(uintptr_t)
+				nixge_hw_dma_bd_get_addr(&priv->rx_bd_v[i],
+							 sw_id_offset);
+			dev_kfree_skb(skb);
+		}
 
-	if (priv->rx_bd_v)
 		dma_free_coherent(ndev->dev.parent,
 				  sizeof(*priv->rx_bd_v) * RX_BD_NUM,
 				  priv->rx_bd_v,
 				  priv->rx_bd_p);
+	}
 
 	if (priv->tx_skb)
 		devm_kfree(ndev->dev.parent, priv->tx_skb);