linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] net: key: af_key: Fix possible null-pointer dereferences in pfkey_send_policy_notify()
@ 2019-07-24  9:35 Jia-Ju Bai
  2019-07-26  9:45 ` Steffen Klassert
  0 siblings, 1 reply; 4+ messages in thread
From: Jia-Ju Bai @ 2019-07-24  9:35 UTC (permalink / raw)
  To: steffen.klassert, herbert, davem; +Cc: netdev, linux-kernel, Jia-Ju Bai

In pfkey_send_policy_notify(), there is an if statement on line 3081 to
check whether xp is NULL:
    if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)

When xp is NULL, it is used by key_notify_policy() on line 3090:
    key_notify_policy(xp, ...)
        pfkey_xfrm_policy2msg_prep(xp) -- line 2211
            pfkey_xfrm_policy2msg_size(xp) -- line 2046
                for (i=0; i<xp->xfrm_nr; i++) -- line 2026
                t = xp->xfrm_vec + i; -- line 2027
    key_notify_policy(xp, ...)
        xp_net(xp) -- line 2231
            return read_pnet(&xp->xp_net); -- line 534

Thus, possible null-pointer dereferences may occur.

To fix these bugs, xp is checked before calling key_notify_policy().

These bugs are found by a static analysis tool STCheck written by us.

Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
---
 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 b67ed3a8486c..ced54144d5fd 100644
--- a/net/key/af_key.c
+++ b/net/key/af_key.c
@@ -3087,6 +3087,8 @@ static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struc
 	case XFRM_MSG_DELPOLICY:
 	case XFRM_MSG_NEWPOLICY:
 	case XFRM_MSG_UPDPOLICY:
+		if (!xp)
+			break;
 		return key_notify_policy(xp, dir, c);
 	case XFRM_MSG_FLUSHPOLICY:
 		if (c->data.type != XFRM_POLICY_TYPE_MAIN)
-- 
2.17.0


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

* Re: [PATCH] net: key: af_key: Fix possible null-pointer dereferences in pfkey_send_policy_notify()
  2019-07-24  9:35 [PATCH] net: key: af_key: Fix possible null-pointer dereferences in pfkey_send_policy_notify() Jia-Ju Bai
@ 2019-07-26  9:45 ` Steffen Klassert
  2019-07-26 20:15   ` Jeremy Sowden
  0 siblings, 1 reply; 4+ messages in thread
From: Steffen Klassert @ 2019-07-26  9:45 UTC (permalink / raw)
  To: Jia-Ju Bai; +Cc: herbert, davem, netdev, linux-kernel

On Wed, Jul 24, 2019 at 05:35:09PM +0800, Jia-Ju Bai wrote:
> In pfkey_send_policy_notify(), there is an if statement on line 3081 to
> check whether xp is NULL:
>     if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
> 
> When xp is NULL, it is used by key_notify_policy() on line 3090:
>     key_notify_policy(xp, ...)
>         pfkey_xfrm_policy2msg_prep(xp) -- line 2211
>             pfkey_xfrm_policy2msg_size(xp) -- line 2046
>                 for (i=0; i<xp->xfrm_nr; i++) -- line 2026
>                 t = xp->xfrm_vec + i; -- line 2027
>     key_notify_policy(xp, ...)
>         xp_net(xp) -- line 2231
>             return read_pnet(&xp->xp_net); -- line 534

Please don't quote random code lines, explain the
problem instead.

> 
> Thus, possible null-pointer dereferences may occur.
> 
> To fix these bugs, xp is checked before calling key_notify_policy().
> 
> These bugs are found by a static analysis tool STCheck written by us.
> 
> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> ---
>  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 b67ed3a8486c..ced54144d5fd 100644
> --- a/net/key/af_key.c
> +++ b/net/key/af_key.c
> @@ -3087,6 +3087,8 @@ static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struc
>  	case XFRM_MSG_DELPOLICY:
>  	case XFRM_MSG_NEWPOLICY:
>  	case XFRM_MSG_UPDPOLICY:
> +		if (!xp)
> +			break;

I think this can not happen. Who sends one of these notifications
without a pointer to the policy?


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

* Re: [PATCH] net: key: af_key: Fix possible null-pointer dereferences in pfkey_send_policy_notify()
  2019-07-26  9:45 ` Steffen Klassert
@ 2019-07-26 20:15   ` Jeremy Sowden
  2019-07-27  8:32     ` Steffen Klassert
  0 siblings, 1 reply; 4+ messages in thread
From: Jeremy Sowden @ 2019-07-26 20:15 UTC (permalink / raw)
  To: Steffen Klassert; +Cc: Jia-Ju Bai, herbert, davem, netdev, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2003 bytes --]

On 2019-07-26, at 11:45:14 +0200, Steffen Klassert wrote:
> On Wed, Jul 24, 2019 at 05:35:09PM +0800, Jia-Ju Bai wrote:
> > In pfkey_send_policy_notify(), there is an if statement on line 3081
> > to check whether xp is NULL:
> >     if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
> >
> > When xp is NULL, it is used by key_notify_policy() on line 3090:
> >     key_notify_policy(xp, ...)
> >         pfkey_xfrm_policy2msg_prep(xp) -- line 2211
> >             pfkey_xfrm_policy2msg_size(xp) -- line 2046
> >                 for (i=0; i<xp->xfrm_nr; i++) -- line 2026
> >                 t = xp->xfrm_vec + i; -- line 2027
> >     key_notify_policy(xp, ...)
> >         xp_net(xp) -- line 2231
> >             return read_pnet(&xp->xp_net); -- line 534
>
> Please don't quote random code lines, explain the problem instead.
>
> >
> > Thus, possible null-pointer dereferences may occur.
> >
> > To fix these bugs, xp is checked before calling key_notify_policy().
> >
> > These bugs are found by a static analysis tool STCheck written by
> > us.
> >
> > Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> > ---
> >  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 b67ed3a8486c..ced54144d5fd 100644
> > --- a/net/key/af_key.c
> > +++ b/net/key/af_key.c
> > @@ -3087,6 +3087,8 @@ static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struc
> >  	case XFRM_MSG_DELPOLICY:
> >  	case XFRM_MSG_NEWPOLICY:
> >  	case XFRM_MSG_UPDPOLICY:
> > +		if (!xp)
> > +			break;
>
> I think this can not happen. Who sends one of these notifications
> without a pointer to the policy?

I had a quick grep and found two places where km_policy_notify is passed
NULL as the policy:

  $ grep -rn '\<km_policy_notify(NULL,' net/
  net/xfrm/xfrm_user.c:2154:      km_policy_notify(NULL, 0, &c);
  net/key/af_key.c:2788:  km_policy_notify(NULL, 0, &c);

They occur in xfrm_flush_policy() and pfkey_spdflush() respectively.

J.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH] net: key: af_key: Fix possible null-pointer dereferences in pfkey_send_policy_notify()
  2019-07-26 20:15   ` Jeremy Sowden
@ 2019-07-27  8:32     ` Steffen Klassert
  0 siblings, 0 replies; 4+ messages in thread
From: Steffen Klassert @ 2019-07-27  8:32 UTC (permalink / raw)
  To: Jeremy Sowden; +Cc: Jia-Ju Bai, herbert, davem, netdev, linux-kernel

On Fri, Jul 26, 2019 at 09:15:55PM +0100, Jeremy Sowden wrote:
> On 2019-07-26, at 11:45:14 +0200, Steffen Klassert wrote:
> > On Wed, Jul 24, 2019 at 05:35:09PM +0800, Jia-Ju Bai wrote:
> > >
> > > diff --git a/net/key/af_key.c b/net/key/af_key.c
> > > index b67ed3a8486c..ced54144d5fd 100644
> > > --- a/net/key/af_key.c
> > > +++ b/net/key/af_key.c
> > > @@ -3087,6 +3087,8 @@ static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struc
> > >  	case XFRM_MSG_DELPOLICY:
> > >  	case XFRM_MSG_NEWPOLICY:
> > >  	case XFRM_MSG_UPDPOLICY:
> > > +		if (!xp)
> > > +			break;
> >
> > I think this can not happen. Who sends one of these notifications
> > without a pointer to the policy?
> 
> I had a quick grep and found two places where km_policy_notify is passed
> NULL as the policy:
> 
>   $ grep -rn '\<km_policy_notify(NULL,' net/
>   net/xfrm/xfrm_user.c:2154:      km_policy_notify(NULL, 0, &c);
>   net/key/af_key.c:2788:  km_policy_notify(NULL, 0, &c);
> 
> They occur in xfrm_flush_policy() and pfkey_spdflush() respectively.

Yes, but these two send a XFRM_MSG_FLUSHPOLICY notify.
This does not trigger the code that is changed here.

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

end of thread, other threads:[~2019-07-27  8:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-24  9:35 [PATCH] net: key: af_key: Fix possible null-pointer dereferences in pfkey_send_policy_notify() Jia-Ju Bai
2019-07-26  9:45 ` Steffen Klassert
2019-07-26 20:15   ` Jeremy Sowden
2019-07-27  8:32     ` 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).