From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796382609; b=Gl4yD7T9FRo+g8nUuqkJPPIlSvVKueEOkAktHCRXISm/CtOZr9Vb8E0MNshgVbV8tXb2 WE0rMd+1RKMD7CAqYBC1SBIvoUjY5PQnJhYiq6o2lGZdW2dkXPkiY3QmN6w3PlwLzrlaW xNZ72t9dH8fyaSDUTRb9+hYmggAtbDfu4hbVB9onIzFSjHK4K+CMUW2Hx3TspcQhOno7W l2Sk67EHFUKNzlzPceDIeRsZOCrfdvzSKRy5whVMTSwBlsLF0CjRM41IOadl5tISkYuI3 L6qIAW1fYm+6j8mLzg+AaTva5AlcDncT/p1JKai6Y1Xf4+1nywgXpBdKukfyUuHtiIQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382609; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=qjkZui/C2uchJNyC7vKKoe8pjCC7mkFEBckSvxZHZY0=; b=XsvKpzFtk34Pe+naymhO0VzPgY1+TaAakKti7agklxJ8rBQV2UtJyNAyI9EuNoRjcDn/ C0i8uQ1pIqio3s69SRyjk/cP1Yc6ySdclv8k11vRmqE2aWK4a+ss95t7dxSOuq0PlzaLs XYzHzJ4+KJsFqAP0g6v+fpwXWos0SOz2V/zuQRBEfn0+9mLZHwAxCp6/EkQQqD3OOBa/y r8WWNqWY+9t6DF7PvcMft099ZnJK1aLyY72OQ3drgK0IqmpuabVmDGXbWxT6N6MbuiV3b kbr9KO1R8O/auIYoa5J1diFG/TM6g0dcrvwKePKs5w3mK/dQpPH7R7aDXchP57rAvKw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Z4oaKOv5; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 303F6606A32D; Fri, 4 Dec 2026 11:10:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Z4oaKOv5 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F6C51F000C2; Fri, 4 Dec 2026 11:10:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382609; bh=qjkZui/C2uchJNyC7vKKoe8pjCC7mkFEBckSvxZHZY0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Z4oaKOv5twHyzrO4eJRKneMkYyz4Zz0TGyUedyI73nHFdV5r0hOCIrrWVeisR2/e/ K70rDn3+BPpvvMdj+TE6RT+P79nYV049+JT/A56NavBaU7yf1P7unnND/7kGlvXgVn P2W7Evg48/sT2VAvRiyMEyMN2D9V5vxghDYI2aGw= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Yves Bieri , Xin Long , Jakub Kicinski Subject: [PATCH 6.18 526/703] sctp: do not free a new asoc peeled off while sendmsg slept Date: Fri, 4 Dec 2026 19:07:05 +0900 Message-ID: <20261204100641.784868010@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Yves Bieri commit f2540da93a113d3f0a70fd0961e00eb849ce8342 upstream. When sctp_sendmsg() creates a new association and sending on it fails with anything but -ESRCH, it frees the association. The send can sleep in sctp_wait_for_sndbuf(), which releases the socket lock, and meanwhile another thread can peel the new association off to a new socket. The wait then returns -EPIPE, and sctp_sendmsg() frees an association that now belongs to the peeled-off socket, without holding that socket's lock. If the peeled-off socket frees the association first, e.g. on close(), sctp_sendmsg() frees it a second time. Otherwise a syscall running concurrently on the peeled-off socket can use it after sctp_sendmsg() freed it. Both are reachable by an unprivileged user. The double free on net with KASAN: BUG: KASAN: double-free in sctp_tsnmap_free+0x5c/0x80 sctp_association_free+0x1eb/0x750 sctp_sendmsg+0x15ff/0x2020 Freed by task 836: sctp_association_free+0x1eb/0x750 sctp_primitive_ABORT+0x85/0xe0 sctp_close+0x22a/0x8d0 Commit dfcb9f4f99f1 ("sctp: deny peeloff operation on asocs with threads sleeping on it") fixed this double free by denying the peeloff. Commit cea0cc80a677 ("sctp: use the right sk after waking up from wait_buf sleep") replaced that check, and commit a0ff660058b8 ("sctp: return error if the asoc has been peeled off in sctp_wait_for_sndbuf") reverted it to return -EPIPE, without restoring the check. Fix this by holding a reference on the new association while sending on it, and by freeing it on error only if this socket still owns it. asoc->base.sk only changes in sctp_sock_migrate(), which runs with this socket locked, so the ownership check is stable. The reference keeps the check safe if the new owner frees the association meanwhile. Userspace still gets -EPIPE, and the association stays with the socket it was peeled off to. This was found by Xint Code, an AI code review agent, and reproduced as an unprivileged user on net with KASAN (trace above), without modifying the kernel or using error injection. With this patch the sender gets -EPIPE and the association keeps working on the peeled-off socket. Without a peeloff, a failed send that created the association (EAGAIN, EINTR) still frees it, and a peer ABORT while the sender sleeps still frees it exactly once. The SCTP selftest sctp_vrf.sh passes. The reproducer is available on request. Fixes: a0ff660058b8 ("sctp: return error if the asoc has been peeled off in sctp_wait_for_sndbuf") Cc: stable@vger.kernel.org Signed-off-by: Yves Bieri Acked-by: Xin Long Link: https://patch.msgid.link/20261112172149.27355-1-yves.bieri@theori.io Signed-off-by: Jakub Kicinski Signed-off-by: Greg Kroah-Hartman --- net/sctp/socket.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -2062,6 +2062,11 @@ static int sctp_sendmsg(struct sock *sk, asoc = transport->asoc; new = true; + /* sctp_sendmsg_to_asoc() may release the socket lock, + * and the new asoc can be peeled off meanwhile. Hold + * it, and free it on error only if sk still owns it. + */ + sctp_association_hold(asoc); } if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER)) @@ -2083,8 +2088,11 @@ static int sctp_sendmsg(struct sock *sk, /* Send msg to the asoc */ err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo); - if (err < 0 && err != -ESRCH && new) - sctp_association_free(asoc); + if (new) { + if (err < 0 && err != -ESRCH && asoc->base.sk == sk) + sctp_association_free(asoc); + sctp_association_put(asoc); + } out_unlock: release_sock(sk);