From nobody Fri Apr 19 19:51:32 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5CEF179D0 for ; Wed, 8 Feb 2023 21:44:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675892659; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=P2f11UH+ovsC9lb7JNzsG62bYFjoMeJfnN7KO4dshPQ=; b=GGtlss7FCakV361dww0tfOMJKes3mmCUfXlSC9ejUZFx7kks61VWo1qaRLF3L/MxHKtAe+ uZCJNtczkAibW++Uv+Gdps+wS3k8JpZDHMtUFTGvlewS8yeu1StTChq0AkpkSDwqKaFDm/ wtzsAWoy+Xh1uW68txt8rwUn9rdaddA= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-491-pQERzo9uOICGG-bWv-rJ-A-1; Wed, 08 Feb 2023 16:44:17 -0500 X-MC-Unique: pQERzo9uOICGG-bWv-rJ-A-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 799ED29A9D3F; Wed, 8 Feb 2023 21:44:17 +0000 (UTC) Received: from gerbillo.redhat.com (unknown [10.39.193.120]) by smtp.corp.redhat.com (Postfix) with ESMTP id D9022492C3E; Wed, 8 Feb 2023 21:44:16 +0000 (UTC) From: Paolo Abeni To: mptcp@lists.linux.dev Cc: Christoph Paasch Subject: [PATCH mptcp-net 1/2] mptcp: use the workqueue to destroy unaccepted sockets. Date: Wed, 8 Feb 2023 22:44:09 +0100 Message-Id: In-Reply-To: References: Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8"; x-default="true" Christoph reported a UaF while disposing unaccepted MPC subflow. We need to properly clean-up all the paired MPTCP-level resources and be sure to release the msk last, even when the unaccepted subflow is destroyed by the TCP internals via inet_child_forget(). We can re-use the existing MPTCP_WORK_CLOSE_SUBFLOW infra, explicitly checking that for the critical scenario: the closed subflow is the MPC one, the msk is not accepted and eventually going through full cleanup. With such change, __mptcp_destroy_sock() is always called on msk sockets, even on accepted ones. We don't need anymore to transiently drop one sk reference at msk clone time. Signed-off-by: Paolo Abeni Tested-by: Christoph Paasch --- net/mptcp/protocol.c | 26 +++++++++++++++++--------- net/mptcp/protocol.h | 3 ++- net/mptcp/subflow.c | 11 +++++++++-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c index 546df81c162f..d7588535bf6d 100644 --- a/net/mptcp/protocol.c +++ b/net/mptcp/protocol.c @@ -2444,9 +2444,10 @@ static unsigned int mptcp_sync_mss(struct sock *sk, = u32 pmtu) return 0; } =20 -static void __mptcp_close_subflow(struct mptcp_sock *msk) +static void __mptcp_close_subflow(struct sock *sk) { struct mptcp_subflow_context *subflow, *tmp; + struct mptcp_sock *msk =3D mptcp_sk(sk); =20 might_sleep(); =20 @@ -2460,7 +2461,15 @@ static void __mptcp_close_subflow(struct mptcp_sock = *msk) if (!skb_queue_empty_lockless(&ssk->sk_receive_queue)) continue; =20 - mptcp_close_ssk((struct sock *)msk, ssk, subflow); + mptcp_close_ssk(sk, ssk, subflow); + } + + /* if the MPC subflow has been closed before the msk is accepted, + * msk will never be accept-ed, close it now + */ + if (!msk->first && msk->in_accept_queue) { + sock_set_flag(sk, SOCK_DEAD); + sk->sk_state =3D TCP_CLOSE; } } =20 @@ -2684,6 +2693,9 @@ static void mptcp_worker(struct work_struct *work) __mptcp_check_send_data_fin(sk); mptcp_check_data_fin(sk); =20 + if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) + __mptcp_close_subflow(sk); + /* There is no point in keeping around an orphaned sk timedout or * closed, but we need the msk around to reply to incoming DATA_FIN, * even if it is orphaned and in FIN_WAIT2 state @@ -2699,9 +2711,6 @@ static void mptcp_worker(struct work_struct *work) } } =20 - if (test_and_clear_bit(MPTCP_WORK_CLOSE_SUBFLOW, &msk->flags)) - __mptcp_close_subflow(msk); - if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags)) __mptcp_retrans(sk); =20 @@ -3149,6 +3158,7 @@ struct sock *mptcp_sk_clone(const struct sock *sk, msk->local_key =3D subflow_req->local_key; msk->token =3D subflow_req->token; msk->subflow =3D NULL; + msk->in_accept_queue =3D 1; WRITE_ONCE(msk->fully_established, false); if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD) WRITE_ONCE(msk->csum_enabled, true); @@ -3169,8 +3179,7 @@ struct sock *mptcp_sk_clone(const struct sock *sk, security_inet_csk_clone(nsk, req); bh_unlock_sock(nsk); =20 - /* keep a single reference */ - __sock_put(nsk); + /* note: the newly allocated socket refcount is 2 now */ return nsk; } =20 @@ -3226,8 +3235,6 @@ static struct sock *mptcp_accept(struct sock *sk, int= flags, int *err, goto out; } =20 - /* acquire the 2nd reference for the owning socket */ - sock_hold(new_mptcp_sock); newsk =3D new_mptcp_sock; MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPCAPABLEPASSIVEACK); } else { @@ -3781,6 +3788,7 @@ static int mptcp_stream_accept(struct socket *sock, s= truct socket *newsock, struct mptcp_subflow_context *subflow; =20 set_bit(SOCK_CUSTOM_SOCKOPT, &newsock->flags); + msk->in_accept_queue =3D 0; =20 /* set ssk->sk_socket of accept()ed flows to mptcp socket. * This is needed so NOSPACE flag can be set from tcp stack. diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h index 3a055438c65e..252f050c96c6 100644 --- a/net/mptcp/protocol.h +++ b/net/mptcp/protocol.h @@ -294,7 +294,8 @@ struct mptcp_sock { u8 recvmsg_inq:1, cork:1, nodelay:1, - fastopening:1; + fastopening:1, + in_accept_queue:1; int connect_flags; struct work_struct work; struct sk_buff *ooo_last_skb; diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c index f075607adad4..1dff2ea6d6d3 100644 --- a/net/mptcp/subflow.c +++ b/net/mptcp/subflow.c @@ -693,9 +693,10 @@ static bool subflow_hmac_valid(const struct request_so= ck *req, =20 static void mptcp_force_close(struct sock *sk) { - /* the msk is not yet exposed to user-space */ + /* the msk is not yet exposed to user-space, and refcount is 2 */ inet_sk_state_store(sk, TCP_CLOSE); sk_common_release(sk); + sock_put(sk); } =20 static void subflow_ulp_fallback(struct sock *sk, @@ -1854,7 +1855,6 @@ void mptcp_subflow_queue_clean(struct sock *listener_= sk, struct sock *listener_s struct sock *sk =3D (struct sock *)msk; bool do_cancel_work; =20 - sock_hold(sk); lock_sock_nested(sk, SINGLE_DEPTH_NESTING); next =3D msk->dl_next; msk->dl_next =3D NULL; @@ -1949,6 +1949,13 @@ static void subflow_ulp_release(struct sock *ssk) * the ctx alive, will be freed by __mptcp_close_ssk() */ release =3D ctx->disposable; + + /* inet_child_forget() does not call sk_state_change(), + * explicitly trigger the socket close machinery + */ + if (!release && !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, + &mptcp_sk(sk)->flags)) + schedule_work(&mptcp_sk(sk)->work); sock_put(sk); } =20 --=20 2.39.1 From nobody Fri Apr 19 19:51:32 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D5AE679D0 for ; Wed, 8 Feb 2023 21:44:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675892661; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mu6xg/l6LaHsvOLwb8Vx4BuJxmlMz73SN50KzypRPY4=; b=Ex8wsSjfnifRgNjOp1JdccijM2moV/zW5CEyGxNNjwn/LT3WCvxc6BZibUs8CBS+Sfjo37 rrJ0qp/9lSkMuVexshC3obB+eZ6KrXukVE1IBp0JtEk7OsqgfGKIWF/H25fRdVsCNVz3Fx q5yPGK9bWzt/VQcZOApkNDbs6qZxpSU= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-33-G086qq-2P8a3TP0sjg3anA-1; Wed, 08 Feb 2023 16:44:18 -0500 X-MC-Unique: G086qq-2P8a3TP0sjg3anA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 63D153813F22; Wed, 8 Feb 2023 21:44:18 +0000 (UTC) Received: from gerbillo.redhat.com (unknown [10.39.193.120]) by smtp.corp.redhat.com (Postfix) with ESMTP id C354B492C3E; Wed, 8 Feb 2023 21:44:17 +0000 (UTC) From: Paolo Abeni To: mptcp@lists.linux.dev Cc: Christoph Paasch Subject: [PATCH mptcp-net 2/2] mptcp: fix UaF in listener shutdown Date: Wed, 8 Feb 2023 22:44:10 +0100 Message-Id: In-Reply-To: References: Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8"; x-default="true" After the previous patch we don't need anymore special-casing msk listener socket cleanup: the mptcp worker will process each of the unaccepted msk sockets. Just drop the now unnecessary code. Signed-off-by: Paolo Abeni Tested-by: Christoph Paasch --- net/mptcp/protocol.c | 1 - net/mptcp/protocol.h | 1 - net/mptcp/subflow.c | 80 -------------------------------------------- 3 files changed, 82 deletions(-) diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c index d7588535bf6d..ae2d715f82b5 100644 --- a/net/mptcp/protocol.c +++ b/net/mptcp/protocol.c @@ -2400,7 +2400,6 @@ static void __mptcp_close_ssk(struct sock *sk, struct= sock *ssk, /* otherwise tcp will dispose of the ssk and subflow ctx */ if (ssk->sk_state =3D=3D TCP_LISTEN) { tcp_set_state(ssk, TCP_CLOSE); - mptcp_subflow_queue_clean(sk, ssk); inet_csk_listen_stop(ssk); mptcp_event_pm_listener(ssk, MPTCP_EVENT_LISTENER_CLOSED); } diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h index 252f050c96c6..ba4d22ffd228 100644 --- a/net/mptcp/protocol.h +++ b/net/mptcp/protocol.h @@ -631,7 +631,6 @@ void mptcp_close_ssk(struct sock *sk, struct sock *ssk, struct mptcp_subflow_context *subflow); void __mptcp_subflow_send_ack(struct sock *ssk); void mptcp_subflow_reset(struct sock *ssk); -void mptcp_subflow_queue_clean(struct sock *sk, struct sock *ssk); void mptcp_sock_graft(struct sock *sk, struct socket *parent); struct socket *__mptcp_nmpc_socket(struct mptcp_sock *msk); bool __mptcp_close(struct sock *sk, long timeout); diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c index 1dff2ea6d6d3..4f397d336811 100644 --- a/net/mptcp/subflow.c +++ b/net/mptcp/subflow.c @@ -1814,86 +1814,6 @@ static void subflow_state_change(struct sock *sk) } } =20 -void mptcp_subflow_queue_clean(struct sock *listener_sk, struct sock *list= ener_ssk) -{ - struct request_sock_queue *queue =3D &inet_csk(listener_ssk)->icsk_accept= _queue; - struct mptcp_sock *msk, *next, *head =3D NULL; - struct request_sock *req; - - /* build a list of all unaccepted mptcp sockets */ - spin_lock_bh(&queue->rskq_lock); - for (req =3D queue->rskq_accept_head; req; req =3D req->dl_next) { - struct mptcp_subflow_context *subflow; - struct sock *ssk =3D req->sk; - struct mptcp_sock *msk; - - if (!sk_is_mptcp(ssk)) - continue; - - subflow =3D mptcp_subflow_ctx(ssk); - if (!subflow || !subflow->conn) - continue; - - /* skip if already in list */ - msk =3D mptcp_sk(subflow->conn); - if (msk->dl_next || msk =3D=3D head) - continue; - - msk->dl_next =3D head; - head =3D msk; - } - spin_unlock_bh(&queue->rskq_lock); - if (!head) - return; - - /* can't acquire the msk socket lock under the subflow one, - * or will cause ABBA deadlock - */ - release_sock(listener_ssk); - - for (msk =3D head; msk; msk =3D next) { - struct sock *sk =3D (struct sock *)msk; - bool do_cancel_work; - - lock_sock_nested(sk, SINGLE_DEPTH_NESTING); - next =3D msk->dl_next; - msk->dl_next =3D NULL; - - /* The upcoming mptcp_close is going to drop all the references - * to the first subflow, ignoring that one of such reference is - * owned by the request socket still in the accept queue and that - * later inet_csk_listen_stop will drop it. - * Acquire an extra reference here to avoid an UaF at that point. - */ - if (msk->first) - sock_hold(msk->first); - - do_cancel_work =3D __mptcp_close(sk, -1); - release_sock(sk); - if (do_cancel_work) { - /* lockdep will report a false positive ABBA deadlock - * between cancel_work_sync and the listener socket. - * The involved locks belong to different sockets WRT - * the existing AB chain. - * Using a per socket key is problematic as key - * deregistration requires process context and must be - * performed at socket disposal time, in atomic - * context. - * Just tell lockdep to consider the listener socket - * released here. - */ - mutex_release(&listener_sk->sk_lock.dep_map, _RET_IP_); - mptcp_cancel_work(sk); - mutex_acquire(&listener_sk->sk_lock.dep_map, - SINGLE_DEPTH_NESTING, 0, _RET_IP_); - } - sock_put(sk); - } - - /* we are still under the listener msk socket lock */ - lock_sock_nested(listener_ssk, SINGLE_DEPTH_NESTING); -} - static int subflow_ulp_init(struct sock *sk) { struct inet_connection_sock *icsk =3D inet_csk(sk); --=20 2.39.1