Skip to contentSkip to conversation

xfrm_user: use xfrm_state_put to free xfrm_state_alloc return value

net Applied Pan Bian 4 participants 5 messages Nov 21, 2018 – Nov 27, 2018

Subsystems (3)
lore
Share & tools
lore

Pan Bian

author
#0 lore raw

From: Pan Bian <[email protected]>

The memory chunk allocated by xfrm_state_alloc() should be released with xfrm_state_put(), not kfree.

---
 net/xfrm/xfrm_user.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/xfrm/xfrm_user.c +2 -2
index c9a84e2..267922c 100644
@@ -2288,13 +2288,13 @@ static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	}
 
-	kfree(x);
+	xfrm_state_put(x);
 	kfree(xp);
 
 	return 0;
 
 free_state:
-	kfree(x);
+	xfrm_state_put(x);
 nomem:
 	return err;
 }
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 6 lines · Pan Bian

From: Pan Bian <[email protected]>

The memory chunk allocated by xfrm_state_alloc() should be released with xfrm_state_put(), not kfree.

This bug was introduced by

	commit 565f0fa902b64020d5d147ff1708567e9e0b6e49
	Author: Mathias Krause <[email protected]>
	Date:   Thu May 3 10:55:07 2018 +0200

While using xfrm_state_put may work it's certainly not the designed to do this. We should instead export a function that calls kmem_cache_free on xfrm_state directly and use that here.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#2 lore raw
Quoted 6 lines · Herbert Xu
Quoted 5 lines · Pan Bian

From: Pan Bian <[email protected]>

The memory chunk allocated by xfrm_state_alloc() should be released with xfrm_state_put(), not kfree.

Quoted 8 lines
Quoted 1 line

This bug was introduced by

	commit 565f0fa902b64020d5d147ff1708567e9e0b6e49
	Author: Mathias Krause <[email protected]>
	Date:   Thu May 3 10:55:07 2018 +0200

Oh, snap. You're totally right. I missed the kfree() in xfrm_user.c. Sorry for that!

Quoted 3 lines

While using xfrm_state_put may work it's certainly not the designed to do this. We should instead export a function that calls kmem_cache_free on xfrm_state directly and use that here.

Maybe something like the below patch? Steffen?

-- >8 --

Subject: [PATCH] xfrm_user: fix freeing of xfrm states on acquire

Commit 565f0fa902b6 ("xfrm: use a dedicated slab cache for struct xfrm_state") moved xfrm state objects to use their own slab cache. However, it missed to adapt xfrm_user to use this new cache when freeing xfrm states.

Fix this by introducing and make use of a new helper for freeing xfrm_state objects.

Fixes 565f0fa902b6 ("xfrm: use a dedicated slab cache for struct xfrm_state")
Cc <[email protected]> # v4.18+
---
 include/net/xfrm.h    | 1 +
 net/xfrm/xfrm_state.c | 8 +++++++-
 net/xfrm/xfrm_user.c  | 4 ++--
 3 files changed, 10 insertions(+), 3 deletions(-)
include/net/xfrm.h +1 -0
index 0eb390c205af..da588def3c61 100644
@@ -1552,6 +1552,7 @@ int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
 		    int (*func)(struct xfrm_state *, int, void*), void *);
 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net);
 struct xfrm_state *xfrm_state_alloc(struct net *net);
+void xfrm_state_free(struct xfrm_state *x);
 struct xfrm_state *xfrm_state_find(const xfrm_address_t *daddr,
 				   const xfrm_address_t *saddr,
 				   const struct flowi *fl,
net/xfrm/xfrm_state.c +7 -1
index dc4a9f1fb941..0a0b01b688d7 100644
@@ -426,6 +426,12 @@ static void xfrm_put_mode(struct xfrm_mode *mode)
 	module_put(mode->owner);
 }
 
+void xfrm_state_free(struct xfrm_state *x)
+{
+	kmem_cache_free(xfrm_state_cache, x);
+}
+EXPORT_SYMBOL(xfrm_state_free);
+
 static void xfrm_state_gc_destroy(struct xfrm_state *x)
 {
 	tasklet_hrtimer_cancel(&x->mtimer);
@@ -452,7 +458,7 @@ static void xfrm_state_gc_destroy(struct xfrm_state *x)
 	}
 	xfrm_dev_state_free(x);
 	security_xfrm_state_free(x);
-	kmem_cache_free(xfrm_state_cache, x);
+	xfrm_state_free(x);
 }
 
 static void xfrm_state_gc_task(struct work_struct *work)
net/xfrm/xfrm_user.c +2 -2
index c9a84e22f5d5..277c1c46fe94 100644
@@ -2288,13 +2288,13 @@ static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	}
 
-	kfree(x);
+	xfrm_state_free(x);
 	kfree(xp);
 
 	return 0;
 
 free_state:
-	kfree(x);
+	xfrm_state_free(x);
 nomem:
 	return err;
 }
maintainer Acked-by
#3 lore raw
Quoted 10 lines · Mathias Krause

-- >8 --

Subject: [PATCH] xfrm_user: fix freeing of xfrm states on acquire

Commit 565f0fa902b6 ("xfrm: use a dedicated slab cache for struct xfrm_state") moved xfrm state objects to use their own slab cache. However, it missed to adapt xfrm_user to use this new cache when freeing xfrm states.

Quoted 7 lines

Fix this by introducing and make use of a new helper for freeing xfrm_state objects.

Fixes 565f0fa902b6 ("xfrm: use a dedicated slab cache for struct xfrm_state")
Cc <[email protected]> # v4.18+

Looks good to me. Thanks!

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer Applied
#4 lore raw
Quoted 14 lines · Herbert Xu
Quoted 13 lines · Mathias Krause

-- >8 --

Subject: [PATCH] xfrm_user: fix freeing of xfrm states on acquire

Commit 565f0fa902b6 ("xfrm: use a dedicated slab cache for struct xfrm_state") moved xfrm state objects to use their own slab cache. However, it missed to adapt xfrm_user to use this new cache when freeing xfrm states.

Fix this by introducing and make use of a new helper for freeing xfrm_state objects.

Quoted 6 lines
Quoted 4 lines
Fixes 565f0fa902b6 ("xfrm: use a dedicated slab cache for struct xfrm_state")
Cc <[email protected]> # v4.18+

Applied, thanks everyone!