[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240815195645.43808-1-kuniyu@amazon.com>
Date: Thu, 15 Aug 2024 12:56:45 -0700
From: Kuniyuki Iwashima <kuniyu@...zon.com>
To: <sunyiqixm@...il.com>
CC: <davem@...emloft.net>, <edumazet@...gle.com>, <kuba@...nel.org>,
<linux-kernel@...r.kernel.org>, <netdev@...r.kernel.org>,
<pabeni@...hat.com>, <kuniyu@...zon.com>
Subject: Re: [PATCH] net: do not release sk in sk_wait_event
From: sunyiqi <sunyiqixm@...il.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.
---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<---
We can allow another thread to complete building skb by the
following but it doesn't sound correct to me.
---8<---
diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c
index 2f191e50d4fc..51f2409d6113 100644
--- a/net/kcm/kcmsock.c
+++ b/net/kcm/kcmsock.c
@@ -748,7 +748,7 @@ static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
{
struct sock *sk = sock->sk;
struct kcm_sock *kcm = kcm_sk(sk);
- struct sk_buff *skb = NULL, *head = NULL;
+ struct sk_buff *skb = NULL, *head = NULL, *seq_skb;
size_t copy, copied = 0;
long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
int eor = (sock->type == SOCK_DGRAM) ?
@@ -763,6 +763,7 @@ static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
if (sk->sk_err)
goto out_error;
+ seq_skb = kcm->seq_skb;
if (kcm->seq_skb) {
/* Previously opened message */
head = kcm->seq_skb;
@@ -888,6 +889,8 @@ static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
err = sk_stream_wait_memory(sk, &timeo);
if (err)
goto out_error;
+ if (seq_skb && seq_skb != kcm->seq_skb)
+ goto out_stolen;
}
if (eor) {
@@ -943,7 +946,7 @@ static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
kfree_skb(head);
kcm->seq_skb = NULL;
}
-
+out_stolen:
err = sk_stream_error(sk, msg->msg_flags, err);
/* make sure we wake any epoll edge trigger waiter */
---8<---
Powered by blists - more mailing lists