2019-06-14 08:26:37

by Young Xiao

[permalink] [raw]
Subject: [PATCH] af_key: Fix memory leak in key_notify_policy.

We leak the allocated out_skb in case pfkey_xfrm_policy2msg() fails.
Fix this by freeing it on error.

Signed-off-by: Young Xiao <[email protected]>
---
net/key/af_key.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/net/key/af_key.c b/net/key/af_key.c
index 4af1e1d..ec414f6 100644
--- a/net/key/af_key.c
+++ b/net/key/af_key.c
@@ -2443,6 +2443,7 @@ static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, const struc
}
err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
if (err < 0)
+ kfree_skb(out_skb);
goto out;

out_hdr = (struct sadb_msg *) out_skb->data;
@@ -2695,6 +2696,7 @@ static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)

err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
if (err < 0)
+ kfree_skb(out_skb);
return err;

out_hdr = (struct sadb_msg *) out_skb->data;
--
2.7.4


2019-06-14 08:56:07

by Steffen Klassert

[permalink] [raw]
Subject: Re: [PATCH] af_key: Fix memory leak in key_notify_policy.

On Fri, Jun 14, 2019 at 04:26:26PM +0800, Young Xiao wrote:
> We leak the allocated out_skb in case pfkey_xfrm_policy2msg() fails.
> Fix this by freeing it on error.
>
> Signed-off-by: Young Xiao <[email protected]>
> ---
> net/key/af_key.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/net/key/af_key.c b/net/key/af_key.c
> index 4af1e1d..ec414f6 100644
> --- a/net/key/af_key.c
> +++ b/net/key/af_key.c
> @@ -2443,6 +2443,7 @@ static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, const struc
> }
> err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
> if (err < 0)
> + kfree_skb(out_skb);
> goto out;

Did you test this?

You need to add braces, otherwise 'goto out' will happen unconditionally.

>
> out_hdr = (struct sadb_msg *) out_skb->data;
> @@ -2695,6 +2696,7 @@ static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
>
> err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
> if (err < 0)
> + kfree_skb(out_skb);
> return err;

Same here.

2019-06-14 10:01:31

by Jeremy Sowden

[permalink] [raw]
Subject: Re: [PATCH] af_key: Fix memory leak in key_notify_policy.

On 2019-06-14, at 10:53:46 +0200, Steffen Klassert wrote:
> On Fri, Jun 14, 2019 at 04:26:26PM +0800, Young Xiao wrote:
> > We leak the allocated out_skb in case pfkey_xfrm_policy2msg() fails.
> > Fix this by freeing it on error.
> >
> > Signed-off-by: Young Xiao <[email protected]>
> > ---
> > net/key/af_key.c | 2 ++
> > 1 file changed, 2 insertions(+)
> >
> > diff --git a/net/key/af_key.c b/net/key/af_key.c
> > index 4af1e1d..ec414f6 100644
> > --- a/net/key/af_key.c
> > +++ b/net/key/af_key.c
> > @@ -2443,6 +2443,7 @@ static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, const struc
> > }
> > err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
> > if (err < 0)
> > + kfree_skb(out_skb);
> > goto out;
>
> Did you test this?
>
> You need to add braces, otherwise 'goto out' will happen unconditionally.
>
> >
> > out_hdr = (struct sadb_msg *) out_skb->data;
> > @@ -2695,6 +2696,7 @@ static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
> >
> > err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
> > if (err < 0)
> > + kfree_skb(out_skb);
> > return err;
>
> Same here.

There's already a patch for this:

https://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec.git/commit/?id=7c80eb1c7e2b8420477fbc998971d62a648035d9

J.


Attachments:
(No filename) (1.30 kB)
signature.asc (849.00 B)
Download all attachments