Skip to content

Commit 2a6a870

Browse files
Paolo Abenidavem330
Paolo Abeni
authored andcommitted
mptcp: stops worker on unaccepted sockets at listener close
This is a partial revert of the blamed commit, with a relevant change: mptcp_subflow_queue_clean() now just change the msk socket status and stop the worker, so that the UaF issue addressed by the blamed commit is not re-introduced. The above prevents the mptcp worker from running concurrently with inet_csk_listen_stop(), as such race would trigger a warning, as reported by Christoph: RSP: 002b:00007f784fe09cd8 EFLAGS: 00000246 ORIG_RAX: 000000000000002e WARNING: CPU: 0 PID: 25807 at net/ipv4/inet_connection_sock.c:1387 inet_csk_listen_stop+0x664/0x870 net/ipv4/inet_connection_sock.c:1387 RAX: ffffffffffffffda RBX: 00000000006bc050 RCX: 00007f7850afd6a9 RDX: 0000000000000000 RSI: 0000000020000340 RDI: 0000000000000004 Modules linked in: RBP: 0000000000000002 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 00000000006bc05c R13: fffffffffffffea8 R14: 00000000006bc050 R15: 000000000001fe40 </TASK> CPU: 0 PID: 25807 Comm: syz-executor.7 Not tainted 6.2.0-g778e54711659 #7 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.0-2.el7 04/01/2014 RIP: 0010:inet_csk_listen_stop+0x664/0x870 net/ipv4/inet_connection_sock.c:1387 RAX: 0000000000000000 RBX: ffff888100dfbd40 RCX: 0000000000000000 RDX: ffff8881363aab80 RSI: ffffffff81c494f4 RDI: 0000000000000005 RBP: ffff888126dad080 R08: 0000000000000005 R09: 0000000000000000 R10: 0000000000000001 R11: 0000000000000000 R12: ffff888100dfe040 R13: 0000000000000001 R14: 0000000000000000 R15: ffff888100dfbdd8 FS: 00007f7850a2c800(0000) GS:ffff88813bc00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000001b32d26000 CR3: 000000012fdd8006 CR4: 0000000000770ef0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 PKRU: 55555554 Call Trace: <TASK> __tcp_close+0x5b2/0x620 net/ipv4/tcp.c:2875 __mptcp_close_ssk+0x145/0x3d0 net/mptcp/protocol.c:2427 mptcp_destroy_common+0x8a/0x1c0 net/mptcp/protocol.c:3277 mptcp_destroy+0x41/0x60 net/mptcp/protocol.c:3304 __mptcp_destroy_sock+0x56/0x140 net/mptcp/protocol.c:2965 __mptcp_close+0x38f/0x4a0 net/mptcp/protocol.c:3057 mptcp_close+0x24/0xe0 net/mptcp/protocol.c:3072 inet_release+0x53/0xa0 net/ipv4/af_inet.c:429 __sock_release+0x4e/0xf0 net/socket.c:651 sock_close+0x15/0x20 net/socket.c:1393 __fput+0xff/0x420 fs/file_table.c:321 task_work_run+0x8b/0xe0 kernel/task_work.c:179 resume_user_mode_work include/linux/resume_user_mode.h:49 [inline] exit_to_user_mode_loop kernel/entry/common.c:171 [inline] exit_to_user_mode_prepare+0x113/0x120 kernel/entry/common.c:203 __syscall_exit_to_user_mode_work kernel/entry/common.c:285 [inline] syscall_exit_to_user_mode+0x1d/0x40 kernel/entry/common.c:296 do_syscall_64+0x46/0x90 arch/x86/entry/common.c:86 entry_SYSCALL_64_after_hwframe+0x72/0xdc RIP: 0033:0x7f7850af70dc RAX: 0000000000000000 RBX: 0000000000000004 RCX: 00007f7850af70dc RDX: 00007f7850a2c800 RSI: 0000000000000002 RDI: 0000000000000003 RBP: 00000000006bd980 R08: 0000000000000000 R09: 00000000000018a0 R10: 00000000316338a4 R11: 0000000000000293 R12: 0000000000211e31 R13: 00000000006bc05c R14: 00007f785062c000 R15: 0000000000211af0 Fixes: 0a3f4f1 ("mptcp: fix UaF in listener shutdown") Cc: [email protected] Reported-by: Christoph Paasch <[email protected]> Link: multipath-tcp/mptcp_net-next#371 Signed-off-by: Paolo Abeni <[email protected]> Reviewed-by: Matthieu Baerts <[email protected]> Signed-off-by: Matthieu Baerts <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 4e006c7 commit 2a6a870

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

net/mptcp/protocol.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2365,8 +2365,12 @@ static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
23652365
mptcp_subflow_drop_ctx(ssk);
23662366
} else {
23672367
/* otherwise tcp will dispose of the ssk and subflow ctx */
2368-
if (ssk->sk_state == TCP_LISTEN)
2368+
if (ssk->sk_state == TCP_LISTEN) {
2369+
tcp_set_state(ssk, TCP_CLOSE);
2370+
mptcp_subflow_queue_clean(sk, ssk);
2371+
inet_csk_listen_stop(ssk);
23692372
mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED);
2373+
}
23702374

23712375
__tcp_close(ssk, 0);
23722376

net/mptcp/protocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ void mptcp_close_ssk(struct sock *sk, struct sock *ssk,
629629
struct mptcp_subflow_context *subflow);
630630
void __mptcp_subflow_send_ack(struct sock *ssk);
631631
void mptcp_subflow_reset(struct sock *ssk);
632+
void mptcp_subflow_queue_clean(struct sock *sk, struct sock *ssk);
632633
void mptcp_sock_graft(struct sock *sk, struct socket *parent);
633634
struct socket *__mptcp_nmpc_socket(const struct mptcp_sock *msk);
634635
bool __mptcp_close(struct sock *sk, long timeout);

net/mptcp/subflow.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,78 @@ static void subflow_state_change(struct sock *sk)
18191819
}
18201820
}
18211821

1822+
void mptcp_subflow_queue_clean(struct sock *listener_sk, struct sock *listener_ssk)
1823+
{
1824+
struct request_sock_queue *queue = &inet_csk(listener_ssk)->icsk_accept_queue;
1825+
struct mptcp_sock *msk, *next, *head = NULL;
1826+
struct request_sock *req;
1827+
1828+
/* build a list of all unaccepted mptcp sockets */
1829+
spin_lock_bh(&queue->rskq_lock);
1830+
for (req = queue->rskq_accept_head; req; req = req->dl_next) {
1831+
struct mptcp_subflow_context *subflow;
1832+
struct sock *ssk = req->sk;
1833+
1834+
if (!sk_is_mptcp(ssk))
1835+
continue;
1836+
1837+
subflow = mptcp_subflow_ctx(ssk);
1838+
if (!subflow || !subflow->conn)
1839+
continue;
1840+
1841+
/* skip if already in list */
1842+
msk = mptcp_sk(subflow->conn);
1843+
if (msk->dl_next || msk == head)
1844+
continue;
1845+
1846+
sock_hold(subflow->conn);
1847+
msk->dl_next = head;
1848+
head = msk;
1849+
}
1850+
spin_unlock_bh(&queue->rskq_lock);
1851+
if (!head)
1852+
return;
1853+
1854+
/* can't acquire the msk socket lock under the subflow one,
1855+
* or will cause ABBA deadlock
1856+
*/
1857+
release_sock(listener_ssk);
1858+
1859+
for (msk = head; msk; msk = next) {
1860+
struct sock *sk = (struct sock *)msk;
1861+
1862+
lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1863+
next = msk->dl_next;
1864+
msk->dl_next = NULL;
1865+
1866+
/* prevent the stack from later re-schedule the worker for
1867+
* this socket
1868+
*/
1869+
inet_sk_state_store(sk, TCP_CLOSE);
1870+
release_sock(sk);
1871+
1872+
/* lockdep will report a false positive ABBA deadlock
1873+
* between cancel_work_sync and the listener socket.
1874+
* The involved locks belong to different sockets WRT
1875+
* the existing AB chain.
1876+
* Using a per socket key is problematic as key
1877+
* deregistration requires process context and must be
1878+
* performed at socket disposal time, in atomic
1879+
* context.
1880+
* Just tell lockdep to consider the listener socket
1881+
* released here.
1882+
*/
1883+
mutex_release(&listener_sk->sk_lock.dep_map, _RET_IP_);
1884+
mptcp_cancel_work(sk);
1885+
mutex_acquire(&listener_sk->sk_lock.dep_map, 0, 0, _RET_IP_);
1886+
1887+
sock_put(sk);
1888+
}
1889+
1890+
/* we are still under the listener msk socket lock */
1891+
lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING);
1892+
}
1893+
18221894
static int subflow_ulp_init(struct sock *sk)
18231895
{
18241896
struct inet_connection_sock *icsk = inet_csk(sk);

0 commit comments

Comments
 (0)