From: sunyiqi <sunyiqixm@gmail.com>
To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
pabeni@redhat.com
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
sunyiqi <sunyiqixm@gmail.com>
Subject: [PATCH] net: do not release sk in sk_wait_event
Date: Thu, 15 Aug 2024 16:49:07 +0800 [thread overview]
Message-ID: <20240815084907.167870-1-sunyiqixm@gmail.com> (raw)
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.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Link: https://syzkaller.appspot.com/bug?extid=b72d86aa5df17ce74c60
Signed-off-by: sunyiqi <sunyiqixm@gmail.com>
---
include/net/sock.h | 2 --
1 file changed, 2 deletions(-)
diff --git a/include/net/sock.h b/include/net/sock.h
index cce23ac4d514..08d3b204b019 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -1145,7 +1145,6 @@ static inline void sock_rps_reset_rxhash(struct sock *sk)
#define sk_wait_event(__sk, __timeo, __condition, __wait) \
({ int __rc, __dis = __sk->sk_disconnects; \
- release_sock(__sk); \
__rc = __condition; \
if (!__rc) { \
*(__timeo) = wait_woken(__wait, \
@@ -1153,7 +1152,6 @@ static inline void sock_rps_reset_rxhash(struct sock *sk)
*(__timeo)); \
} \
sched_annotate_sleep(); \
- lock_sock(__sk); \
__rc = __dis == __sk->sk_disconnects ? __condition : -EPIPE; \
__rc; \
})
--
2.34.1
next reply other threads:[~2024-08-15 8:49 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-15 8:49 sunyiqi [this message]
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
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=20240815084907.167870-1-sunyiqixm@gmail.com \
--to=sunyiqixm@gmail.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 \
/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
all inboxes | Powered by JetHome®