public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: <kuniyu@amazon.com>
Cc: <davem@davemloft.net>, <edumazet@google.com>, <kuba@kernel.org>,
	<linux-kernel@vger.kernel.org>, <netdev@vger.kernel.org>,
	<pabeni@redhat.com>, <sunyiqixm@gmail.com>
Subject: Re: [PATCH] net: do not release sk in sk_wait_event
Date: Thu, 15 Aug 2024 15:07:59 -0700	[thread overview]
Message-ID: <20240815220759.70089-1-kuniyu@amazon.com> (raw)
In-Reply-To: <20240815195645.43808-1-kuniyu@amazon.com>

From: Kuniyuki Iwashima <kuniyu@amazon.com>
Date: Thu, 15 Aug 2024 12:56:45 -0700
> From: sunyiqi <sunyiqixm@gmail.com>
> Date: Thu, 15 Aug 2024 18:23:29 +0800
> > On Thu, 15 Aug 2024 12:03:37 +0200, Paolo Abeni wrote:
> > > On 8/15/24 10:49, sunyiqi wrote:
> > > > When investigating the kcm socket UAF which is also found by syzbot,
> > > > I found that the root cause of this problem is actually in
> > > > sk_wait_event.
> > > > 
> > > > In sk_wait_event, sk is released and relocked and called by
> > > > sk_stream_wait_memory. Protocols like tcp, kcm, etc., called it in some
> > > > ops function like *sendmsg which will lock the sk at the beginning.
> > > > But sk_stream_wait_memory releases sk unexpectedly and destroy
> > > > the thread safety. Finally it causes the kcm sk UAF.
> > > > 
> > > > If at the time when a thread(thread A) calls sk_stream_wait_memory
> > > > and the other thread(thread B) is waiting for lock in lock_sock,
> > > > thread B will successfully get the sk lock as thread A release sk lock
> > > > in sk_wait_event.
> > > > 
> > > > The thread B may change the sk which is not thread A expecting.
> > > > 
> > > > As a result, it will lead kernel to the unexpected behavior. Just like
> > > > the kcm sk UAF, which is actually cause by sk_wait_event in
> > > > sk_stream_wait_memory.
> > > > 
> > > > Previous commit d9dc8b0f8b4e ("net: fix sleeping for sk_wait_event()")
> > > > in 2016 seems do not solved this problem. Is it necessary to release
> > > > sock in sk_wait_event? Or just delete it to make the protocol ops
> > > > thread-secure.
> > > 
> > > As a I wrote previously, please describe the suspected race more 
> > > clearly, with the exact calls sequence that lead to the UAF.
> > > 
> > > Releasing the socket lock is not enough to cause UAF.
> > 
> > Thread A                 Thread B
> > kcm_sendmsg
> >  lock_sock               kcm_sendmsg
> >                           lock_sock (blocked & waiting)
> >  head = sk->seq_buf
> >  sk_stream_wait_memory
> >   sk_wait_event
> >    release_sock
> >                           lock_sock (get the lock)
> >                           head = sk->seq_buf
> >                           add head to sk->sk_write_queue
> >                           release_sock
> >    lock_sock              return
> >  err_out to free(head)
> >  release_sock
> >  return
> > // ...
> > kcm_release
> >  // ...
> >  __skb_queue_purge(&sk->sk_write_queue) // <--- UAF
> >  // ...
> > 
> > The repro can be downloaded here:
> > https://syzkaller.appspot.com/bug?extid=b72d86aa5df17ce74c60
> 
> When a thread is building a skb with MSG_MORE, another thread
> must not touch it nor complete building it by queuing it to
> write queue and setting NULL to kcm->seq_skb.
> 
> I think the correct fix is simply serialise them with mutex.

FTR, syzbot was happy with the change below, so I posted it officially.
https://lore.kernel.org/netdev/20240815220437.69511-1-kuniyu@amazon.com/


> ---8<---
> diff --git a/include/net/kcm.h b/include/net/kcm.h
> index 90279e5e09a5..441e993be634 100644
> --- a/include/net/kcm.h
> +++ b/include/net/kcm.h
> @@ -70,6 +70,7 @@ struct kcm_sock {
>  	struct work_struct tx_work;
>  	struct list_head wait_psock_list;
>  	struct sk_buff *seq_skb;
> +	struct mutex tx_mutex;
>  	u32 tx_stopped : 1;
>  
>  	/* Don't use bit fields here, these are set under different locks */
> diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c
> index 2f191e50d4fc..d4118c796290 100644
> --- a/net/kcm/kcmsock.c
> +++ b/net/kcm/kcmsock.c
> @@ -755,6 +755,7 @@ static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
>  		  !(msg->msg_flags & MSG_MORE) : !!(msg->msg_flags & MSG_EOR);
>  	int err = -EPIPE;
>  
> +	mutex_lock(&kcm->tx_mutex);
>  	lock_sock(sk);
>  
>  	/* Per tcp_sendmsg this should be in poll */
> @@ -926,6 +927,7 @@ static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
>  	KCM_STATS_ADD(kcm->stats.tx_bytes, copied);
>  
>  	release_sock(sk);
> +	mutex_unlock(&kcm->tx_mutex);
>  	return copied;
>  
>  out_error:
> @@ -951,6 +953,7 @@ static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
>  		sk->sk_write_space(sk);
>  
>  	release_sock(sk);
> +	mutex_unlock(&kcm->tx_mutex);
>  	return err;
>  }
>  
> @@ -1204,6 +1207,7 @@ static void init_kcm_sock(struct kcm_sock *kcm, struct kcm_mux *mux)
>  	spin_unlock_bh(&mux->lock);
>  
>  	INIT_WORK(&kcm->tx_work, kcm_tx_work);
> +	mutex_init(&kcm->tx_mutex);
>  
>  	spin_lock_bh(&mux->rx_lock);
>  	kcm_rcv_ready(kcm);
> ---8<---

  reply	other threads:[~2024-08-15 22:08 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-15  8:49 [PATCH] net: do not release sk in sk_wait_event sunyiqi
2024-08-15 10:03 ` Paolo Abeni
2024-08-15 10:23   ` sunyiqi
2024-08-15 19:56     ` Kuniyuki Iwashima
2024-08-15 22:07       ` Kuniyuki Iwashima [this message]
2024-08-15 11:14 ` Jason Xing
2024-08-27  8:59   ` sunyiqi
2024-08-21  7:38 ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240815220759.70089-1-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sunyiqixm@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox