Skip to content

Commit b2b4c84

Browse files
Paolo Abenigregkh
Paolo Abeni
authored andcommitted
mptcp: fix disconnect vs accept race
commit 511b90e upstream. Despite commit 0ad529d ("mptcp: fix possible divide by zero in recvmsg()"), the mptcp protocol is still prone to a race between disconnect() (or shutdown) and accept. The root cause is that the mentioned commit checks the msk-level flag, but mptcp_stream_accept() does acquire the msk-level lock, as it can rely directly on the first subflow lock. As reported by Christoph than can lead to a race where an msk socket is accepted after that mptcp_subflow_queue_clean() releases the listener socket lock and just before it takes destructive actions leading to the following splat: BUG: kernel NULL pointer dereference, address: 0000000000000012 PGD 5a4ca067 P4D 5a4ca067 PUD 37d4c067 PMD 0 Oops: 0000 [#1] PREEMPT SMP CPU: 2 PID: 10955 Comm: syz-executor.5 Not tainted 6.5.0-rc1-gdc7b257ee5dd #37 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.0-2.el7 04/01/2014 RIP: 0010:mptcp_stream_accept+0x1ee/0x2f0 include/net/inet_sock.h:330 Code: 0a 09 00 48 8b 1b 4c 39 e3 74 07 e8 bc 7c 7f fe eb a1 e8 b5 7c 7f fe 4c 8b 6c 24 08 eb 05 e8 a9 7c 7f fe 49 8b 85 d8 09 00 00 <0f> b6 40 12 88 44 24 07 0f b6 6c 24 07 bf 07 00 00 00 89 ee e8 89 RSP: 0018:ffffc90000d07dc0 EFLAGS: 00010293 RAX: 0000000000000000 RBX: ffff888037e8d020 RCX: ffff88803b093300 RDX: 0000000000000000 RSI: ffffffff833822c5 RDI: ffffffff8333896a RBP: 0000607f82031520 R08: ffff88803b093300 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000003e83 R12: ffff888037e8d020 R13: ffff888037e8c680 R14: ffff888009af7900 R15: ffff888009af6880 FS: 00007fc26d708640(0000) GS:ffff88807dd00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000000012 CR3: 0000000066bc5001 CR4: 0000000000370ee0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: <TASK> do_accept+0x1ae/0x260 net/socket.c:1872 __sys_accept4+0x9b/0x110 net/socket.c:1913 __do_sys_accept4 net/socket.c:1954 [inline] __se_sys_accept4 net/socket.c:1951 [inline] __x64_sys_accept4+0x20/0x30 net/socket.c:1951 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x47/0xa0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x6e/0xd8 Address the issue by temporary removing the pending request socket from the accept queue, so that racing accept() can't touch them. After depleting the msk - the ssk still exists, as plain TCP sockets, re-insert them into the accept queue, so that later inet_csk_listen_stop() will complete the tcp socket disposal. Fixes: 2a6a870 ("mptcp: stops worker on unaccepted sockets at listener close") Cc: [email protected] Reported-by: Christoph Paasch <[email protected]> Closes: multipath-tcp/mptcp_net-next#423 Signed-off-by: Paolo Abeni <[email protected]> Reviewed-by: Matthieu Baerts <[email protected]> Signed-off-by: Matthieu Baerts <[email protected]> Link: https://lore.kernel.org/r/20230803-upstream-net-20230803-misc-fixes-6-5-v1-4-6671b1ab11cc@tessares.net Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 3c78824 commit b2b4c84

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

net/mptcp/protocol.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ struct mptcp_sock {
320320

321321
u32 setsockopt_seq;
322322
char ca_name[TCP_CA_NAME_MAX];
323-
struct mptcp_sock *dl_next;
324323
};
325324

326325
#define mptcp_data_lock(sk) spin_lock_bh(&(sk)->sk_lock.slock)

net/mptcp/subflow.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,49 +1785,42 @@ static void subflow_state_change(struct sock *sk)
17851785
void mptcp_subflow_queue_clean(struct sock *listener_sk, struct sock *listener_ssk)
17861786
{
17871787
struct request_sock_queue *queue = &inet_csk(listener_ssk)->icsk_accept_queue;
1788-
struct mptcp_sock *msk, *next, *head = NULL;
1789-
struct request_sock *req;
1790-
struct sock *sk;
1788+
struct request_sock *req, *head, *tail;
1789+
struct mptcp_subflow_context *subflow;
1790+
struct sock *sk, *ssk;
17911791

1792-
/* build a list of all unaccepted mptcp sockets */
1792+
/* Due to lock dependencies no relevant lock can be acquired under rskq_lock.
1793+
* Splice the req list, so that accept() can not reach the pending ssk after
1794+
* the listener socket is released below.
1795+
*/
17931796
spin_lock_bh(&queue->rskq_lock);
1794-
for (req = queue->rskq_accept_head; req; req = req->dl_next) {
1795-
struct mptcp_subflow_context *subflow;
1796-
struct sock *ssk = req->sk;
1797+
head = queue->rskq_accept_head;
1798+
tail = queue->rskq_accept_tail;
1799+
queue->rskq_accept_head = NULL;
1800+
queue->rskq_accept_tail = NULL;
1801+
spin_unlock_bh(&queue->rskq_lock);
1802+
1803+
if (!head)
1804+
return;
17971805

1806+
/* can't acquire the msk socket lock under the subflow one,
1807+
* or will cause ABBA deadlock
1808+
*/
1809+
release_sock(listener_ssk);
1810+
1811+
for (req = head; req; req = req->dl_next) {
1812+
ssk = req->sk;
17981813
if (!sk_is_mptcp(ssk))
17991814
continue;
18001815

18011816
subflow = mptcp_subflow_ctx(ssk);
18021817
if (!subflow || !subflow->conn)
18031818
continue;
18041819

1805-
/* skip if already in list */
18061820
sk = subflow->conn;
1807-
msk = mptcp_sk(sk);
1808-
if (msk->dl_next || msk == head)
1809-
continue;
1810-
18111821
sock_hold(sk);
1812-
msk->dl_next = head;
1813-
head = msk;
1814-
}
1815-
spin_unlock_bh(&queue->rskq_lock);
1816-
if (!head)
1817-
return;
1818-
1819-
/* can't acquire the msk socket lock under the subflow one,
1820-
* or will cause ABBA deadlock
1821-
*/
1822-
release_sock(listener_ssk);
1823-
1824-
for (msk = head; msk; msk = next) {
1825-
sk = (struct sock *)msk;
18261822

18271823
lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1828-
next = msk->dl_next;
1829-
msk->dl_next = NULL;
1830-
18311824
__mptcp_unaccepted_force_close(sk);
18321825
release_sock(sk);
18331826

@@ -1851,6 +1844,13 @@ void mptcp_subflow_queue_clean(struct sock *listener_sk, struct sock *listener_s
18511844

18521845
/* we are still under the listener msk socket lock */
18531846
lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING);
1847+
1848+
/* restore the listener queue, to let the TCP code clean it up */
1849+
spin_lock_bh(&queue->rskq_lock);
1850+
WARN_ON_ONCE(queue->rskq_accept_head);
1851+
queue->rskq_accept_head = head;
1852+
queue->rskq_accept_tail = tail;
1853+
spin_unlock_bh(&queue->rskq_lock);
18541854
}
18551855

18561856
static int subflow_ulp_init(struct sock *sk)

0 commit comments

Comments
 (0)