* [PATCH v2] xfrm: fix memory leak in xfrm_add_acquire()
@ 2025-11-10 6:41 Zilin Guan
2025-11-10 23:07 ` Sabrina Dubroca
0 siblings, 1 reply; 3+ messages in thread
From: Zilin Guan @ 2025-11-10 6:41 UTC (permalink / raw)
To: steffen.klassert
Cc: herbert, davem, edumazet, kuba, pabeni, horms, netdev,
linux-kernel, jianhao.xu, Zilin Guan
The xfrm_add_acquire() function constructs an xfrm policy by calling
xfrm_policy_construct(). This allocates the policy structure and
potentially associates a security context and a device policy with it.
However, at the end of the function, the policy object is freed using
only kfree() . This skips the necessary cleanup for the security context
and device policy, leading to a memory leak.
To fix this, invoke the proper cleanup functions xfrm_dev_policy_delete(),
xfrm_dev_policy_free(), and security_xfrm_policy_free() before freeing the
policy object. This approach mirrors the error handling path in
xfrm_add_policy(), ensuring that all associated resources are correctly
released.
Fixes: 980ebd25794f ("[IPSEC]: Sync series - acquire insert")
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
Changes in v2:
- Use the correct cleanup functions as per xfrm_add_policy().
---
net/xfrm/xfrm_user.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/net/xfrm/xfrm_user.c b/net/xfrm/xfrm_user.c
index 010c9e6638c0..441d5fa319f4 100644
--- a/net/xfrm/xfrm_user.c
+++ b/net/xfrm/xfrm_user.c
@@ -3035,6 +3035,9 @@ static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
}
xfrm_state_free(x);
+ xfrm_dev_policy_delete(xp);
+ xfrm_dev_policy_free(xp);
+ security_xfrm_policy_free(xp->security);
kfree(xp);
return 0;
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] xfrm: fix memory leak in xfrm_add_acquire()
2025-11-10 6:41 [PATCH v2] xfrm: fix memory leak in xfrm_add_acquire() Zilin Guan
@ 2025-11-10 23:07 ` Sabrina Dubroca
2025-11-14 9:14 ` Steffen Klassert
0 siblings, 1 reply; 3+ messages in thread
From: Sabrina Dubroca @ 2025-11-10 23:07 UTC (permalink / raw)
To: Zilin Guan
Cc: steffen.klassert, herbert, davem, edumazet, kuba, pabeni, horms,
netdev, linux-kernel, jianhao.xu
note: the subject prefix should be [PATCH ipsec vX] for fixes to the
ipsec tree. (sorry, I forgot to mention that in v1)
2025-11-10, 06:41:25 +0000, Zilin Guan wrote:
> The xfrm_add_acquire() function constructs an xfrm policy by calling
> xfrm_policy_construct(). This allocates the policy structure and
> potentially associates a security context and a device policy with it.
>
> However, at the end of the function, the policy object is freed using
> only kfree() . This skips the necessary cleanup for the security context
> and device policy, leading to a memory leak.
>
> To fix this, invoke the proper cleanup functions xfrm_dev_policy_delete(),
> xfrm_dev_policy_free(), and security_xfrm_policy_free() before freeing the
> policy object. This approach mirrors the error handling path in
> xfrm_add_policy(), ensuring that all associated resources are correctly
> released.
>
> Fixes: 980ebd25794f ("[IPSEC]: Sync series - acquire insert")
> Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> ---
> Changes in v2:
> - Use the correct cleanup functions as per xfrm_add_policy().
> ---
> net/xfrm/xfrm_user.c | 3 +++
> 1 file changed, 3 insertions(+)
Reviewed-by: Sabrina Dubroca <sd@queasysnail.net>
--
Sabrina
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] xfrm: fix memory leak in xfrm_add_acquire()
2025-11-10 23:07 ` Sabrina Dubroca
@ 2025-11-14 9:14 ` Steffen Klassert
0 siblings, 0 replies; 3+ messages in thread
From: Steffen Klassert @ 2025-11-14 9:14 UTC (permalink / raw)
To: Sabrina Dubroca
Cc: Zilin Guan, herbert, davem, edumazet, kuba, pabeni, horms, netdev,
linux-kernel, jianhao.xu
On Tue, Nov 11, 2025 at 12:07:40AM +0100, Sabrina Dubroca wrote:
> note: the subject prefix should be [PATCH ipsec vX] for fixes to the
> ipsec tree. (sorry, I forgot to mention that in v1)
>
> 2025-11-10, 06:41:25 +0000, Zilin Guan wrote:
> > The xfrm_add_acquire() function constructs an xfrm policy by calling
> > xfrm_policy_construct(). This allocates the policy structure and
> > potentially associates a security context and a device policy with it.
> >
> > However, at the end of the function, the policy object is freed using
> > only kfree() . This skips the necessary cleanup for the security context
> > and device policy, leading to a memory leak.
> >
> > To fix this, invoke the proper cleanup functions xfrm_dev_policy_delete(),
> > xfrm_dev_policy_free(), and security_xfrm_policy_free() before freeing the
> > policy object. This approach mirrors the error handling path in
> > xfrm_add_policy(), ensuring that all associated resources are correctly
> > released.
> >
> > Fixes: 980ebd25794f ("[IPSEC]: Sync series - acquire insert")
> > Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> > ---
> > Changes in v2:
> > - Use the correct cleanup functions as per xfrm_add_policy().
> > ---
> > net/xfrm/xfrm_user.c | 3 +++
> > 1 file changed, 3 insertions(+)
>
> Reviewed-by: Sabrina Dubroca <sd@queasysnail.net>
Applied to the ipsec tree, thanks everyone!
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-11-14 9:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-10 6:41 [PATCH v2] xfrm: fix memory leak in xfrm_add_acquire() Zilin Guan
2025-11-10 23:07 ` Sabrina Dubroca
2025-11-14 9:14 ` Steffen Klassert
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).