Skip to contentSkip to conversation

xfrm: fix memory leak in xfrm_add_policy()

PATCH In review Deepanshu Kartikey 2 participants 5 messages Apr 12 – Apr 14

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

PATCH 1/1 xfrm: fix memory leak in xfrm_add_policy()

When xfrm_policy_insert() fails, the error path performs manual cleanup by calling xfrm_dev_policy_free(), security_xfrm_policy_free() and kfree() directly. This is incorrect because xfrm_policy_destroy() already handles all of these, causing a memory leak detected by kmemleak.

Replace the open-coded cleanup with xfrm_policy_destroy(), consistent with the error handling in xfrm_policy_construct(). The walk.dead flag must be set before calling xfrm_policy_destroy() as it requires it via BUG_ON(!policy->walk.dead).

Fixes 94b95dfaa814 ("xfrm: release all offloaded policy memory")
Reported-by [email protected]
---
 net/xfrm/xfrm_user.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
net/xfrm/xfrm_user.c +2 -3
index d56450f61669..ae144d1e4a65 100644
@@ -2267,9 +2267,8 @@ static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	if (err) {
 		xfrm_dev_policy_delete(xp);
-		xfrm_dev_policy_free(xp);
-		security_xfrm_policy_free(xp->security);
-		kfree(xp);
+		xp->walk.dead = 1;
+		xfrm_policy_destroy(xp);
 		return err;
 	}
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · Deepanshu Kartikey

When xfrm_policy_insert() fails, the error path performs manual cleanup by calling xfrm_dev_policy_free(), security_xfrm_policy_free() and kfree() directly. This is incorrect because xfrm_policy_destroy() already handles all of these, causing a memory leak detected by kmemleak.

What is missing in the current code? "we have a better way to do this" is not a bugfix, it's a clean up. The kmemleak report says that we're leaking the xfrm_policy struct on this codepath, which doesn't make sense, that's covered by the existing kfree(xp).

Also, please use "PATCH ipsec" for fixes to net/xfrm and the rest of the IPsec implementation.

Signature · 1 line
Sabrina
#2 lore raw
Quoted 7 lines · Sabrina Dubroca

What is missing in the current code? "we have a better way to do this" is not a bugfix, it's a clean up. The kmemleak report says that we're leaking the xfrm_policy struct on this codepath, which doesn't make sense, that's covered by the existing kfree(xp).

Quoted 5 lines

Also, please use "PATCH ipsec" for fixes to net/xfrm and the rest of the IPsec implementation.

--
Sabrina

Hi Sabrina,

Thanks for the review!

You are right, the existing kfree(xp) already covers the struct itself, so my commit message was incorrect in claiming a memory leak fix. I will resend this as a cleanup patch to replace the open-coded manual cleanup with xfrm_policy_destroy(), which is more consistent with xfrm_policy_construct() error handling.

I am also separately investigating the root cause of the actual kmemleak report and will send a proper fix once identified.

Sorry for the noise and thanks again for the guidance.

Deepanshu

#3 lore raw
Quoted 16 lines · Deepanshu Kartikey
Quoted 7 lines · Sabrina Dubroca

What is missing in the current code? "we have a better way to do this" is not a bugfix, it's a clean up. The kmemleak report says that we're leaking the xfrm_policy struct on this codepath, which doesn't make sense, that's covered by the existing kfree(xp).

Quoted 5 lines

Also, please use "PATCH ipsec" for fixes to net/xfrm and the rest of the IPsec implementation.

--
Sabrina

Hi Sabrina,

Quoted 7 lines

Thanks for the review!

You are right, the existing kfree(xp) already covers the struct itself, so my commit message was incorrect in claiming a memory leak fix. I will resend this as a cleanup patch to replace the open-coded manual cleanup with xfrm_policy_destroy(), which is more consistent with xfrm_policy_construct() error handling.

Ok. Then you should wait 2 weeks until the merge window is over:
https://lore.kernel.org/netdev/[email protected]/

and use "[PATCH ipsec-next]" as prefix for the cleanup patch (+ drop the syzbot references).

Quoted 2 lines

I am also separately investigating the root cause of the actual kmemleak report and will send a proper fix once identified.

Ok, thanks.

Signature · 1 line
Sabrina