mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 net] sfc: fix potential memory leak in efx_mae_process_mport()
@ 2025-10-23 14:18 Abdun Nihaal
  2025-10-24  0:48 ` Jacob Keller
  2025-10-28  1:00 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 6+ messages in thread
From: Abdun Nihaal @ 2025-10-23 14:18 UTC (permalink / raw)
  To: ecree.xilinx
  Cc: Abdun Nihaal, andrew+netdev, davem, edumazet, kuba, pabeni,
	habetsm.xilinx, alejandro.lucero-palau, netdev,
	linux-net-drivers, linux-kernel

In efx_mae_enumerate_mports(), memory allocated for mae_mport_desc is
passed as a argument to efx_mae_process_mport(), but when the error path
in efx_mae_process_mport() gets executed, the memory allocated for desc
gets leaked.

Fix that by freeing the memory allocation before returning error.

Fixes: a6a15aca4207 ("sfc: enumerate mports in ef100")
Acked-by: Edward Cree <ecree.xilinx@gmail.com>
Signed-off-by: Abdun Nihaal <nihaal@cse.iitm.ac.in>
---

v1->v2:
- Added a comment to tell that efx_mae_process_mport takes ownership of
   @desc, as suggested by Edward
- Also added Acked-by tag from Edward

Link to v1 patch:
https://patchwork.kernel.org/project/netdevbpf/patch/20251022163525.86362-1-nihaal@cse.iitm.ac.in/

 drivers/net/ethernet/sfc/mae.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/net/ethernet/sfc/mae.c b/drivers/net/ethernet/sfc/mae.c
index 6fd0c1e9a7d5..7cfd9000f79d 100644
--- a/drivers/net/ethernet/sfc/mae.c
+++ b/drivers/net/ethernet/sfc/mae.c
@@ -1090,6 +1090,9 @@ void efx_mae_remove_mport(void *desc, void *arg)
 	kfree(mport);
 }
 
+/*
+ * Takes ownership of @desc, even if it returns an error
+ */
 static int efx_mae_process_mport(struct efx_nic *efx,
 				 struct mae_mport_desc *desc)
 {
@@ -1100,6 +1103,7 @@ static int efx_mae_process_mport(struct efx_nic *efx,
 	if (!IS_ERR_OR_NULL(mport)) {
 		netif_err(efx, drv, efx->net_dev,
 			  "mport with id %u does exist!!!\n", desc->mport_id);
+		kfree(desc);
 		return -EEXIST;
 	}
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 net] sfc: fix potential memory leak in efx_mae_process_mport()
  2025-10-23 14:18 [PATCH v2 net] sfc: fix potential memory leak in efx_mae_process_mport() Abdun Nihaal
@ 2025-10-24  0:48 ` Jacob Keller
  2025-10-24 14:36   ` Abdun Nihaal
  2025-10-24 14:48   ` Edward Cree
  2025-10-28  1:00 ` patchwork-bot+netdevbpf
  1 sibling, 2 replies; 6+ messages in thread
From: Jacob Keller @ 2025-10-24  0:48 UTC (permalink / raw)
  To: Abdun Nihaal, ecree.xilinx
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, habetsm.xilinx,
	alejandro.lucero-palau, netdev, linux-net-drivers, linux-kernel


[-- Attachment #1.1: Type: text/plain, Size: 433 bytes --]



On 10/23/2025 7:18 AM, Abdun Nihaal wrote:
> In efx_mae_enumerate_mports(), memory allocated for mae_mport_desc is
> passed as a argument to efx_mae_process_mport(), but when the error path
> in efx_mae_process_mport() gets executed, the memory allocated for desc
> gets leaked.
> 
> Fix that by freeing the memory allocation before returning error.
> 

Why not make the caller responsible for freeing desc on failure?

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 net] sfc: fix potential memory leak in efx_mae_process_mport()
  2025-10-24  0:48 ` Jacob Keller
@ 2025-10-24 14:36   ` Abdun Nihaal
  2025-10-24 14:48   ` Edward Cree
  1 sibling, 0 replies; 6+ messages in thread
From: Abdun Nihaal @ 2025-10-24 14:36 UTC (permalink / raw)
  To: Jacob Keller
  Cc: ecree.xilinx, andrew+netdev, davem, edumazet, kuba, pabeni,
	habetsm.xilinx, alejandro.lucero-palau, netdev,
	linux-net-drivers, linux-kernel

On Thu, Oct 23, 2025 at 05:48:12PM -0700, Jacob Keller wrote:
> Why not make the caller responsible for freeing desc on failure?

I put the free inside because another function efx_mae_add_mport 
called by efx_mae_process_mport was freeing desc on failure, so I
followed the same style of code, but yes making the caller responsible
would be more cleaner. I'll send a revised patch.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 net] sfc: fix potential memory leak in efx_mae_process_mport()
  2025-10-24  0:48 ` Jacob Keller
  2025-10-24 14:36   ` Abdun Nihaal
@ 2025-10-24 14:48   ` Edward Cree
  2025-10-24 20:13     ` Jacob Keller
  1 sibling, 1 reply; 6+ messages in thread
From: Edward Cree @ 2025-10-24 14:48 UTC (permalink / raw)
  To: Jacob Keller, Abdun Nihaal
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, habetsm.xilinx,
	alejandro.lucero-palau, netdev, linux-net-drivers, linux-kernel

On 24/10/2025 01:48, Jacob Keller wrote:
> On 10/23/2025 7:18 AM, Abdun Nihaal wrote:
>> In efx_mae_enumerate_mports(), memory allocated for mae_mport_desc is
>> passed as a argument to efx_mae_process_mport(), but when the error path
>> in efx_mae_process_mport() gets executed, the memory allocated for desc
>> gets leaked.
>>
>> Fix that by freeing the memory allocation before returning error.
> 
> Why not make the caller responsible for freeing desc on failure?

Since the callee takes ownership of desc on success (it stashes it in a
 table), arguably it's cleaner to have it do so in all cases; it's an
 aesthetic judgment call but I think I'd rather keep it this way and just
 fix this one failure path than change all the existing failure paths and
 the caller.
Alejandro (original author of this code) might have a different opinion
 in which case I'll defer to him but otherwise I'd say v2 is fine to apply
 as-is.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 net] sfc: fix potential memory leak in efx_mae_process_mport()
  2025-10-24 14:48   ` Edward Cree
@ 2025-10-24 20:13     ` Jacob Keller
  0 siblings, 0 replies; 6+ messages in thread
From: Jacob Keller @ 2025-10-24 20:13 UTC (permalink / raw)
  To: Edward Cree, Abdun Nihaal
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, habetsm.xilinx,
	alejandro.lucero-palau, netdev, linux-net-drivers, linux-kernel


[-- Attachment #1.1: Type: text/plain, Size: 1037 bytes --]



On 10/24/2025 7:48 AM, Edward Cree wrote:
> On 24/10/2025 01:48, Jacob Keller wrote:
>> On 10/23/2025 7:18 AM, Abdun Nihaal wrote:
>>> In efx_mae_enumerate_mports(), memory allocated for mae_mport_desc is
>>> passed as a argument to efx_mae_process_mport(), but when the error path
>>> in efx_mae_process_mport() gets executed, the memory allocated for desc
>>> gets leaked.
>>>
>>> Fix that by freeing the memory allocation before returning error.
>>
>> Why not make the caller responsible for freeing desc on failure?
> 
> Since the callee takes ownership of desc on success (it stashes it in a
>  table), arguably it's cleaner to have it do so in all cases; it's an
>  aesthetic judgment call but I think I'd rather keep it this way and just
>  fix this one failure path than change all the existing failure paths and
>  the caller.
> Alejandro (original author of this code) might have a different opinion
>  in which case I'll defer to him but otherwise I'd say v2 is fine to apply
>  as-is.

Fair enough.

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 net] sfc: fix potential memory leak in efx_mae_process_mport()
  2025-10-23 14:18 [PATCH v2 net] sfc: fix potential memory leak in efx_mae_process_mport() Abdun Nihaal
  2025-10-24  0:48 ` Jacob Keller
@ 2025-10-28  1:00 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-10-28  1:00 UTC (permalink / raw)
  To: Abdun Nihaal
  Cc: ecree.xilinx, andrew+netdev, davem, edumazet, kuba, pabeni,
	habetsm.xilinx, alejandro.lucero-palau, netdev,
	linux-net-drivers, linux-kernel

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Thu, 23 Oct 2025 19:48:42 +0530 you wrote:
> In efx_mae_enumerate_mports(), memory allocated for mae_mport_desc is
> passed as a argument to efx_mae_process_mport(), but when the error path
> in efx_mae_process_mport() gets executed, the memory allocated for desc
> gets leaked.
> 
> Fix that by freeing the memory allocation before returning error.
> 
> [...]

Here is the summary with links:
  - [v2,net] sfc: fix potential memory leak in efx_mae_process_mport()
    https://git.kernel.org/netdev/net/c/46a499aaf8c2

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



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-10-28  1:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-10-23 14:18 [PATCH v2 net] sfc: fix potential memory leak in efx_mae_process_mport() Abdun Nihaal
2025-10-24  0:48 ` Jacob Keller
2025-10-24 14:36   ` Abdun Nihaal
2025-10-24 14:48   ` Edward Cree
2025-10-24 20:13     ` Jacob Keller
2025-10-28  1:00 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®