Skip to contentSkip to conversation

idpf: fix potential memory leak on kcalloc() failure

PATCH In review Michal Swiatkowski 4 participants 7 messages Apr 4, 2025 – Apr 25, 2025

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

PATCH 1/1 idpf: fix potential memory leak on kcalloc() failure

In case of failing on rss_data->rss_key allocation the function is freeing vport without freeing earlier allocated q_vector_idxs. Fix it.

Move from freeing in error branch to goto scheme.

Fixes 95af467d9a4e ("idpf: configure resources for RX queues")
---
 drivers/net/ethernet/intel/idpf/idpf_lib.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
drivers/net/ethernet/intel/idpf/idpf_lib.c +11 -8
index aa755dedb41d..329ba53e86fd 100644
@@ -1132,11 +1132,9 @@ static struct idpf_vport *idpf_vport_alloc(struct idpf_adapter *adapter,
 
 	num_max_q = max(max_q->max_txq, max_q->max_rxq);
 	vport->q_vector_idxs = kcalloc(num_max_q, sizeof(u16), GFP_KERNEL);
-	if (!vport->q_vector_idxs) {
-		kfree(vport);
+	if (!vport->q_vector_idxs)
+		goto free_vport;
 
-		return NULL;
-	}
 	idpf_vport_init(vport, max_q);
 
 	/* This alloc is done separate from the LUT because it's not strictly
@@ -1146,11 +1144,9 @@ static struct idpf_vport *idpf_vport_alloc(struct idpf_adapter *adapter,
 	 */
 	rss_data = &adapter->vport_config[idx]->user_config.rss_data;
 	rss_data->rss_key = kzalloc(rss_data->rss_key_size, GFP_KERNEL);
-	if (!rss_data->rss_key) {
-		kfree(vport);
+	if (!rss_data->rss_key)
+		goto free_vector_idxs;
 
-		return NULL;
-	}
 	/* Initialize default rss key */
 	netdev_rss_key_fill((void *)rss_data->rss_key, rss_data->rss_key_size);
 
@@ -1163,6 +1159,13 @@ static struct idpf_vport *idpf_vport_alloc(struct idpf_adapter *adapter,
 	adapter->next_vport = idpf_get_free_slot(adapter);
 
 	return vport;
+
+free_vector_idxs:
+	kfree(vport->q_vector_idxs);
+free_vport:
+	kfree(vport);
+
+	return NULL;
 }
 
 /**
Signature · 1 line
2.42.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Michal Swiatkowski

In case of failing on rss_data->rss_key allocation the function is freeing vport without freeing earlier allocated q_vector_idxs. Fix it.

Move from freeing in error branch to goto scheme.

Fixes 95af467d9a4e ("idpf: configure resources for RX queues")

Hi Michal,

WRT leaking q_vector_indxs, that allocation is not present at the commit cited above, so I think the correct Fixes tag for that problem is the following, where that allocation was added:

Fixes d4d558718266 ("idpf: initialize interrupts and enable vport")

I do note that adapter->vport_config[idx] may be allocated but not freed on error in idpf_vport_alloc(). But I assume that this is not a leak as it will eventually be cleaned up by idpf_remove().

So the Fixes tag not withstanding this looks good to me.

Quoted 4 lines

...

maintainer
#2 lore raw
Quoted 8 lines · Simon Horman
Quoted 6 lines · Michal Swiatkowski

In case of failing on rss_data->rss_key allocation the function is freeing vport without freeing earlier allocated q_vector_idxs. Fix it.

Move from freeing in error branch to goto scheme.

Fixes 95af467d9a4e ("idpf: configure resources for RX queues")
Quoted 7 lines

Hi Michal,

WRT leaking q_vector_indxs, that allocation is not present at the commit cited above, so I think the correct Fixes tag for that problem is the following, where that allocation was added:

Fixes d4d558718266 ("idpf: initialize interrupts and enable vport")

Patch applied. I do agree with Simon's assessment so plan to use this fixes.

Thanks,
Tony

#3 lore raw
Quoted 11 lines · Tony Nguyen
Quoted 10 lines · Simon Horman
Quoted 6 lines · Michal Swiatkowski

In case of failing on rss_data->rss_key allocation the function is freeing vport without freeing earlier allocated q_vector_idxs. Fix it.

Move from freeing in error branch to goto scheme.

Fixes 95af467d9a4e ("idpf: configure resources for RX queues")

Hi Michal,

Quoted 8 lines
Quoted 5 lines

WRT leaking q_vector_indxs, that allocation is not present at the commit cited above, so I think the correct Fixes tag for that problem is the following, where that allocation was added:

Fixes d4d558718266 ("idpf: initialize interrupts and enable vport")

Patch applied. I do agree with Simon's assessment so plan to use this fixes.

Thanks Tony, I also agree.

Quoted 3 lines

Thanks,
Tony

#4 lore raw
In reply to Simon Horman
Quoted 8 lines · Simon Horman
Quoted 6 lines · Michal Swiatkowski

In case of failing on rss_data->rss_key allocation the function is freeing vport without freeing earlier allocated q_vector_idxs. Fix it.

Move from freeing in error branch to goto scheme.

Fixes 95af467d9a4e ("idpf: configure resources for RX queues")
Quoted 7 lines

Hi Michal,

WRT leaking q_vector_indxs, that allocation is not present at the commit cited above, so I think the correct Fixes tag for that problem is the following, where that allocation was added:

Fixes d4d558718266 ("idpf: initialize interrupts and enable vport")

Thanks for checking that. I agree, my fixes is wrong.

Quoted 4 lines

I do note that adapter->vport_config[idx] may be allocated but not freed on error in idpf_vport_alloc(). But I assume that this is not a leak as it will eventually be cleaned up by idpf_remove().

Right, it will be better to free it directly for better readable. Probably candidate for net-next changes.

Quoted 5 lines

So the Fixes tag not withstanding this looks good to me.

Thanks for review.

Quoted 6 lines
Quoted 4 lines

...

reviewer
#5 lore raw
Quoted 20 lines · Michal Swiatkowski
Quoted 8 lines · Simon Horman
Quoted 6 lines · Michal Swiatkowski

In case of failing on rss_data->rss_key allocation the function is freeing vport without freeing earlier allocated q_vector_idxs. Fix it.

Move from freeing in error branch to goto scheme.

Fixes 95af467d9a4e ("idpf: configure resources for RX queues")
Quoted 7 lines

Hi Michal,

WRT leaking q_vector_indxs, that allocation is not present at the commit cited above, so I think the correct Fixes tag for that problem is the following, where that allocation was added:

Fixes d4d558718266 ("idpf: initialize interrupts and enable vport")

Thanks for checking that. I agree, my fixes is wrong.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

I do note that adapter->vport_config[idx] may be allocated but not freed on error in idpf_vport_alloc(). But I assume that this is not a leak as it will eventually be cleaned up by idpf_remove().

Right, it will be better to free it directly for better readable. Probably candidate for net-next changes.

Thanks, that does sound like a nice idea.

...

Tested-by on v1 1/1
#6 lore raw
In reply to Michal Swiatkowski
Quoted 19 lines

-----Original Message-----
From: Intel-wired-lan <[email protected]> On Behalf Of Michal Swiatkowski
Sent: Friday, April 4, 2025 3:54 AM
To: [email protected]
Cc: [email protected]; Michal Swiatkowski <[email protected]>; Linga, Pavan Kumar <[email protected]>; Loktionov, Aleksandr <[email protected]>
Subject: [Intel-wired-lan] [PATCH iwl-net v1] idpf: fix potential memory leak on kcalloc() failure

In case of failing on rss_data->rss_key allocation the function is freeing vport without freeing earlier allocated q_vector_idxs. Fix it.

Move from freeing in error branch to goto scheme.

Quoted 7 lines
Fixes 95af467d9a4e ("idpf: configure resources for RX queues")

---
2.42.0