[v2] af_key: Fix send_acquire race with pfkey_register

Message ID Y1YrVGP+5TP7V1/R@gondor.apana.org.au
State New
Headers
Series [v2] af_key: Fix send_acquire race with pfkey_register |

Commit Message

Herbert Xu Oct. 24, 2022, 6:06 a.m. UTC
  On Sun, Oct 23, 2022 at 10:21:05PM -0700, Eric Dumazet wrote:
>
> Are you sure we can sleep in mutex_lock() ?
> 
> Use of GFP_ATOMIC would suggest otherwise :/

Good point.  Acquires are triggered from the network stack so
it may be in BH context.

---8<---
With name space support, it is possible for a pfkey_register to
occur in the middle of a send_acquire, thus changing the number
of supported algorithms.

This can be fixed by taking a lock to make it single-threaded
again.  As this lock can be taken from both thread and softirq
contexts, we need to take the necessary precausions with disabling
BH and make it a spin lock.

Reported-by: syzbot+1e9af9185d8850e2c2fa@syzkaller.appspotmail.com
Fixes: 283bc9f35bbb ("xfrm: Namespacify xfrm state/policy locks")
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
  

Comments

Eric Dumazet Oct. 24, 2022, 6:31 a.m. UTC | #1
On Sun, Oct 23, 2022 at 11:06 PM Herbert Xu <herbert@gondor.apana.org.au> wrote:
>
> On Sun, Oct 23, 2022 at 10:21:05PM -0700, Eric Dumazet wrote:
> >
> > Are you sure we can sleep in mutex_lock() ?
> >
> > Use of GFP_ATOMIC would suggest otherwise :/
>
> Good point.  Acquires are triggered from the network stack so
> it may be in BH context.
>
> ---8<---
> With name space support, it is possible for a pfkey_register to
> occur in the middle of a send_acquire, thus changing the number
> of supported algorithms.
>
> This can be fixed by taking a lock to make it single-threaded
> again.  As this lock can be taken from both thread and softirq
> contexts, we need to take the necessary precausions with disabling
> BH and make it a spin lock.
>
> Reported-by: syzbot+1e9af9185d8850e2c2fa@syzkaller.appspotmail.com
> Fixes: 283bc9f35bbb ("xfrm: Namespacify xfrm state/policy locks")
> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
>
> diff --git a/net/key/af_key.c b/net/key/af_key.c
> index c85df5b958d2..4e0d21e2045e 100644
> --- a/net/key/af_key.c
> +++ b/net/key/af_key.c
> @@ -23,6 +23,7 @@
>  #include <linux/proc_fs.h>
>  #include <linux/init.h>
>  #include <linux/slab.h>
> +#include <linux/spinlock.h>
>  #include <net/net_namespace.h>
>  #include <net/netns/generic.h>
>  #include <net/xfrm.h>
> @@ -39,6 +40,7 @@ struct netns_pfkey {
>         atomic_t socks_nr;
>  };
>  static DEFINE_MUTEX(pfkey_mutex);
> +static DEFINE_SPINLOCK(pfkey_alg_lock);
>
>  #define DUMMY_MARK 0
>  static const struct xfrm_mark dummy_mark = {0, 0};
> @@ -1697,11 +1699,11 @@ static int pfkey_register(struct sock *sk, struct sk_buff *skb, const struct sad
>                 pfk->registered |= (1<<hdr->sadb_msg_satype);
>         }
>
> -       mutex_lock(&pfkey_mutex);
> +       spin_lock_bh(&pfkey_alg_lock);
>         xfrm_probe_algs();
>
>         supp_skb = compose_sadb_supported(hdr, GFP_KERNEL | __GFP_ZERO);

s/GFP_KERNEL/GFP_ATOMIC/

> -       mutex_unlock(&pfkey_mutex);
> +       spin_unlock_bh(&pfkey_alg_lock);
>
>         if (!supp_skb) {
>                 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
> @@ -3160,6 +3162,7 @@ static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct
>                 (sockaddr_size * 2) +
>                 sizeof(struct sadb_x_policy);
>
> +       spin_lock_bh(&pfkey_alg_lock);
>         if (x->id.proto == IPPROTO_AH)
>                 size += count_ah_combs(t);
>         else if (x->id.proto == IPPROTO_ESP)
> @@ -3171,8 +3174,10 @@ static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct
>         }
>
>         skb =  alloc_skb(size + 16, GFP_ATOMIC);
> -       if (skb == NULL)
> +       if (skb == NULL) {
> +               spin_unlock_bh(&pfkey_alg_lock);
>                 return -ENOMEM;
> +       }
>
>         hdr = skb_put(skb, sizeof(struct sadb_msg));
>         hdr->sadb_msg_version = PF_KEY_V2;
> @@ -3228,6 +3233,7 @@ static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct
>                 dump_ah_combs(skb, t);
>         else if (x->id.proto == IPPROTO_ESP)
>                 dump_esp_combs(skb, t);
> +       spin_unlock_bh(&pfkey_alg_lock);
>
>         /* security context */
>         if (xfrm_ctx) {
> --
> Email: Herbert Xu <herbert@gondor.apana.org.au>
> Home Page: http://gondor.apana.org.au/~herbert/
> PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
  
Sabrina Dubroca Oct. 24, 2022, 7:20 a.m. UTC | #2
2022-10-24, 14:06:12 +0800, Herbert Xu wrote:
> @@ -1697,11 +1699,11 @@ static int pfkey_register(struct sock *sk, struct sk_buff *skb, const struct sad
>  		pfk->registered |= (1<<hdr->sadb_msg_satype);
>  	}
>  
> -	mutex_lock(&pfkey_mutex);
> +	spin_lock_bh(&pfkey_alg_lock);
>  	xfrm_probe_algs();

I don't think we can do that:

void xfrm_probe_algs(void)
{
	int i, status;

	BUG_ON(in_softirq());


>  
>  	supp_skb = compose_sadb_supported(hdr, GFP_KERNEL | __GFP_ZERO);
> -	mutex_unlock(&pfkey_mutex);
> +	spin_unlock_bh(&pfkey_alg_lock);
>  
>  	if (!supp_skb) {
>  		if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
  
Herbert Xu Oct. 25, 2022, 5:46 a.m. UTC | #3
On Sun, Oct 23, 2022 at 11:31:00PM -0700, Eric Dumazet wrote:
>
> s/GFP_KERNEL/GFP_ATOMIC/

Thanks, I clearly wasn't thinking when I made this patch :)
  

Patch

diff --git a/net/key/af_key.c b/net/key/af_key.c
index c85df5b958d2..4e0d21e2045e 100644
--- a/net/key/af_key.c
+++ b/net/key/af_key.c
@@ -23,6 +23,7 @@ 
 #include <linux/proc_fs.h>
 #include <linux/init.h>
 #include <linux/slab.h>
+#include <linux/spinlock.h>
 #include <net/net_namespace.h>
 #include <net/netns/generic.h>
 #include <net/xfrm.h>
@@ -39,6 +40,7 @@  struct netns_pfkey {
 	atomic_t socks_nr;
 };
 static DEFINE_MUTEX(pfkey_mutex);
+static DEFINE_SPINLOCK(pfkey_alg_lock);
 
 #define DUMMY_MARK 0
 static const struct xfrm_mark dummy_mark = {0, 0};
@@ -1697,11 +1699,11 @@  static int pfkey_register(struct sock *sk, struct sk_buff *skb, const struct sad
 		pfk->registered |= (1<<hdr->sadb_msg_satype);
 	}
 
-	mutex_lock(&pfkey_mutex);
+	spin_lock_bh(&pfkey_alg_lock);
 	xfrm_probe_algs();
 
 	supp_skb = compose_sadb_supported(hdr, GFP_KERNEL | __GFP_ZERO);
-	mutex_unlock(&pfkey_mutex);
+	spin_unlock_bh(&pfkey_alg_lock);
 
 	if (!supp_skb) {
 		if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
@@ -3160,6 +3162,7 @@  static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct
 		(sockaddr_size * 2) +
 		sizeof(struct sadb_x_policy);
 
+	spin_lock_bh(&pfkey_alg_lock);
 	if (x->id.proto == IPPROTO_AH)
 		size += count_ah_combs(t);
 	else if (x->id.proto == IPPROTO_ESP)
@@ -3171,8 +3174,10 @@  static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct
 	}
 
 	skb =  alloc_skb(size + 16, GFP_ATOMIC);
-	if (skb == NULL)
+	if (skb == NULL) {
+		spin_unlock_bh(&pfkey_alg_lock);
 		return -ENOMEM;
+	}
 
 	hdr = skb_put(skb, sizeof(struct sadb_msg));
 	hdr->sadb_msg_version = PF_KEY_V2;
@@ -3228,6 +3233,7 @@  static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct
 		dump_ah_combs(skb, t);
 	else if (x->id.proto == IPPROTO_ESP)
 		dump_esp_combs(skb, t);
+	spin_unlock_bh(&pfkey_alg_lock);
 
 	/* security context */
 	if (xfrm_ctx) {