2020-08-27 02:38:35

by Dinghao Liu

[permalink] [raw]
Subject: [PATCH] ice: Fix memleak in ice_set_ringparam

When kcalloc() on rx_rings fails, we should free tx_rings
and xdp_rings to prevent memleak. Similarly, when
ice_alloc_rx_bufs() fails, we should free xdp_rings.

Signed-off-by: Dinghao Liu <[email protected]>
---
drivers/net/ethernet/intel/ice/ice_ethtool.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/intel/ice/ice_ethtool.c b/drivers/net/ethernet/intel/ice/ice_ethtool.c
index 9e8e9531cd87..caf64eb5e96d 100644
--- a/drivers/net/ethernet/intel/ice/ice_ethtool.c
+++ b/drivers/net/ethernet/intel/ice/ice_ethtool.c
@@ -2863,7 +2863,7 @@ ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
rx_rings = kcalloc(vsi->num_rxq, sizeof(*rx_rings), GFP_KERNEL);
if (!rx_rings) {
err = -ENOMEM;
- goto done;
+ goto free_xdp;
}

ice_for_each_rxq(vsi, i) {
@@ -2892,7 +2892,7 @@ ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
}
kfree(rx_rings);
err = -ENOMEM;
- goto free_tx;
+ goto free_xdp;
}
}

@@ -2943,6 +2943,15 @@ ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
}
goto done;

+free_xdp:
+ if (xdp_rings) {
+ for (i = 0; i < vsi->num_xdp_txq; i++) {
+ ice_free_tx_ring(vsi->xdp_rings[i]);
+ *vsi->xdp_rings[i] = xdp_rings[i];
+ }
+ kfree(xdp_rings);
+ }
+
free_tx:
/* error cleanup if the Rx allocations failed after getting Tx */
if (tx_rings) {
--
2.17.1


2020-09-05 02:56:56

by Brown, Aaron F

[permalink] [raw]
Subject: RE: [PATCH] ice: Fix memleak in ice_set_ringparam

> From: Dinghao Liu <[email protected]>
> Sent: Wednesday, August 26, 2020 7:34 PM
> To: [email protected]; [email protected]
> Cc: Kirsher, Jeffrey T <[email protected]>; David S. Miller
> <[email protected]>; Jakub Kicinski <[email protected]>; Alexei
> Starovoitov <[email protected]>; Daniel Borkmann <[email protected]>;
> Jesper Dangaard Brouer <[email protected]>; John Fastabend
> <[email protected]>; [email protected];
> [email protected]; [email protected];
> [email protected]
> Subject: [PATCH] ice: Fix memleak in ice_set_ringparam
>
> When kcalloc() on rx_rings fails, we should free tx_rings
> and xdp_rings to prevent memleak. Similarly, when
> ice_alloc_rx_bufs() fails, we should free xdp_rings.
>
> Signed-off-by: Dinghao Liu <[email protected]>
> ---
> drivers/net/ethernet/intel/ice/ice_ethtool.c | 13 +++++++++++--
> 1 file changed, 11 insertions(+), 2 deletions(-)

Tested-by: Aaron Brown <[email protected]>