From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=194.37.255.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796126520; b=x8EW9Mk45r4idrVKW+a0PXv7X0Xaqss8ghRBt25dT6F9IsiFNfjFrt7udBYs91gfWlne cV8TtXF1mW3ztxEpgTEriuhIelCmrxX4o9QPHf9HRR1oW72PdyLi9SeMB6wq6D3H4eUYE qe/iBj+BP2helqg7vex2QF7jkn34wrF4ljxW44aVX6+37IGpGvKb8duPFdzZ2CsI94y+k Yl7NnAXNyX9sKIfcsSxYGysZxgeRDhUDb/JwvAZcpEtx4nrDjrkJdh51khQUH7u5Sleu8 P2Y47Rm43P9a0W1J+hDJdixBYgiVPnkinID301TJeiZ1cYiAdMomfOMvJlsi1PDL/ag== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796126520; h=DMARC-Filter:Received:Received:DKIM-Signature:Received:Received: Received:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-ID:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key:X-Spam-Status: X-Spam-Checker-Version:X-purgate-type:X-purgate-ID:X-purgate; bh=6BN5dXEKGcmdDgjy7EgY+H2t1HnfALecEbBYfS6ymFc=; b=SDbjgcoTeeuRvpwldDuVvvuHNiR7lj+1o0m2prxGK25d59Bgl6DvXks2NF/XhtB4J5N4 YL2oT4YAKaYTfl9mA1dM9fdeH2HkUsA7TcPz9lZQndFI99TAMAQbrUeqQH4XzUS/hbpH3 JdiSSxCSfqNzopajBSd+0J7PDIlSQ+WRXCEEeC8EDt+3xVmgxmplAVXy3/M8fxE5pFplz 2wDd2FVJK11ILAIJ/bJelOCLGPiNIZBrPP/3N1pg2n55mrLvYC/Vlw8VfX15WuxLst+py CHIFZA1HJbrG9Zg8JyqseEpPhykxDptErSJvCQ3k5SKY94oqSOdGq6FOljQb+m0wY8A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=dev.tdt.de; spf=pass smtp.mailfrom=dev.tdt.de; dkim=pass header.d=dev.tdt.de header.i=@dev.tdt.de header.a=rsa-sha256 header.s=z1-selector1 header.b=kaTX6nCz; arc=none smtp.remote-ip=194.37.255.70 Received: from mxout70.expurgate.net (mxout70.expurgate.net [194.37.255.70]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3E3BC606A320; Tue, 1 Dec 2026 12:02:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3E3BC606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=dev.tdt.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=dev.tdt.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=dev.tdt.de header.i=@dev.tdt.de header.a=rsa-sha256 header.s=z1-selector1 header.b=kaTX6nCz Received: from [194.37.255.9] (helo=mxout.expurgate.net) by relay.expurgate.net with smtp (Exim 4.92) (envelope-from ) id 1xYMYD-009McE-Gu; Tue, 01 Dec 2026 13:01:37 +0100 Received: from [195.243.126.94] (helo=securemail.tdt.de) by relay.expurgate.net with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1xYMYC-003zEG-OF; Tue, 01 Dec 2026 13:01:36 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dev.tdt.de; s=z1-selector1; t=1796126496; bh=6BN5dXEKGcmdDgjy7EgY+H2t1HnfALecEbBYfS6ymFc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=kaTX6nCz0r2ZzsxxARiZ8B/TqKOHk1T1vM/qzDJIrSi1iFRaBS7GVXLzv5RXMYEBB VogxNpT9SMjHPG7maSjXgy0LnMQhxLOnROT80WIJaEC9bdsHZrDZvLy9no+sALKjGN mNkuK0o2tgd3WiDKdQet65RqGPokMkxabP3/Z9dmMhpiXxB7Iva8W9gY+jeNudhoBY fm3mbVG9iGkXH9GYj/bLS6LMSLjZyaX/FB9ZAkG/0mF2hJMCPT1Z/cDexzeuIlTak7 mJR1lYEtw5HGWEt3O2xac5AUiPLvWWQ+uSOQ6oUrLktYaga9L1pxMn+NfjrsCaJNX0 lVUWj4mfVGeow== Received: from securemail.tdt.de (localhost [127.0.0.1]) by securemail.tdt.de (Postfix) with ESMTP id 05289240046; Tue, 1 Dec 2026 13:01:36 +0100 (CET) Received: from mail.dev.tdt.de (unknown [10.2.4.42]) by securemail.tdt.de (Postfix) with ESMTP id EBDDB240036; Tue, 1 Dec 2026 13:01:35 +0100 (CET) Received: from [127.0.1.1] (unknown [10.2.3.19]) by mail.dev.tdt.de (Postfix) with ESMTPSA id 9E2462B315; Tue, 1 Dec 2026 13:01:35 +0100 (CET) From: Martin Schiller Date: Tue, 1 Dec 2026 13:01:24 +0100 (CET) Subject: [PATCH net v3 2/2] net/x25: re-check x25->neighbour after re-acquiring the socket lock X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-ID: <20261201-x25-kill-by-neigh-v3-2-7f05bdff15b1@dev.tdt.de> References: <20261201-x25-kill-by-neigh-v3-0-7f05bdff15b1@dev.tdt.de> In-Reply-To: <20261201-x25-kill-by-neigh-v3-0-7f05bdff15b1@dev.tdt.de> To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Simon Horman , Lin Ma , Duoming Zhou , Xiyu Yang , Xin Tan , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt Cc: linux-x25@vger.kernel.org, netdev@vger.kernel.org, Nguyen Ngoc Thang , Shuai Liu <847807867@qq.com>, Krystian Kaniewski , syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com, syzbot+9faa82ae2e94c5c7d2ef@syzkaller.appspotmail.com, linux-kernel@vger.kernel.org, linux-rt-devel@lists.linux.dev, Martin Schiller X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796126493; l=4008; i=ms@dev.tdt.de; s=20260220; h=from:subject:message-id; bh=7EDoegCMK5qFsXUiAjxxx+1N7l7VaoFQBYATq30Xrgw=; b=VDW5o3LCWNS6KJRH+9au7vETsg5OaNszSc9qAsCNtptqhqo0eQLVuMw1EWllNfDy79AlMeZNA MVEYaahK77mCJWreCVYzSMLnsZSSDpqQKO4bXSiHqEiNtShN3pwNG81 X-Developer-Key: i=ms@dev.tdt.de; a=ed25519; pk=MAojd7D5IafMnqCYSFC7hY/u/jppX58CLIEhsEsSOYE= X-purgate-type: clean X-purgate-ID: 151534::1796126497-C0E8B872-B627B028/0/0 X-purgate: clean x25_sendmsg(), x25_output() and x25_recvmsg() release the socket lock while they wait for memory or data, and use x25->neighbour once they own the socket again. The call may have been cleared in the meantime: x25_disconnect() drops the neighbour reference and sets x25->neighbour to NULL, for example when a CLEAR REQUEST from the peer or a link failure is processed in softirq context while the socket is not owned. sock_alloc_send_skb() only notices this if the disconnect happened before its error checks. A disconnect during the allocation, or before lock_sock() returns, leads to a NULL pointer dereference when x25_sendmsg() builds the packet header or in the fragmentation loop of x25_output(). In x25_recvmsg(), x25_disconnect() leaves x25->condition alone, so if the receiver was busy, x25_check_rbuf() sends an RR through x25_write_internal(), which dereferences x25->neighbour too. Re-check x25->neighbour after lock_sock(): fail with -ENOTCONN in x25_sendmsg() and x25_output(), and skip x25_check_rbuf() in x25_recvmsg(), which still returns the data it has dequeued. Fragments already queued by x25_output() have been purged by x25_disconnect() at that point. The x25_sendmsg() and x25_output() cases were also pointed out by the netdev AI review of an earlier version of the x25_kill_by_neigh() fix. Krystian Kaniewski found the x25_recvmsg() case by code inspection and reproduced all three in QEMU without modifying the kernel: a second thread owns the socket, stalled in a userfaultfd copy, while sendmsg() or recvmsg() waits with the lock dropped, and a CLEAR REQUEST from the peer goes to the backlog. Without this patch, KASAN reports a NULL pointer dereference in x25_sendmsg(), in x25_output() or, called from x25_check_rbuf(), in x25_write_internal(). With it, send() fails with ENOTCONN and recv() returns the data. Fixes: 4becb7ee5b3d ("net/x25: Fix x25_neigh refcnt leak when x25 disconnect") Reported-by: Krystian Kaniewski Closes: https://lore.kernel.org/all/02419845-bab8-4e36-9a98-6830761da1b9@gmail.com/ Link: https://lore.kernel.org/all/179121364205.434549.10310203820251738113@kernel.org/ Tested-by: Krystian Kaniewski Tested-by: Shuai Liu <847807867@qq.com> Reviewed-by: Shuai Liu <847807867@qq.com> Signed-off-by: Martin Schiller --- net/x25/af_x25.c | 10 +++++++++- net/x25/x25_out.c | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c index 43c606a6bfa5a3cf69c905f230edf2c37a85e25d..1f9d5f7faffe26289f5723ade5d4d568a522fae6 100644 --- a/net/x25/af_x25.c +++ b/net/x25/af_x25.c @@ -1248,6 +1248,12 @@ static int x25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) lock_sock(sk); if (!skb) goto out; + + /* The call may have been cleared while the socket was unlocked */ + rc = -ENOTCONN; + if (!x25->neighbour) + goto out_kfree_skb; + X25_SKB_CB(skb)->flags = msg->msg_flags; skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN); @@ -1433,7 +1439,9 @@ static int x25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, msg->msg_namelen = sizeof(*sx25); } - x25_check_rbuf_owned(sk); + /* The call may have been cleared while the socket was unlocked */ + if (x25->neighbour) + x25_check_rbuf_owned(sk); rc = copied; out_free_dgram: skb_free_datagram(sk, skb); diff --git a/net/x25/x25_out.c b/net/x25/x25_out.c index f710d015fcc2ffd6a6922a0c29876bb248125666..89858a3fdf428b3eae5f9c8ba54fee82f73fd0d9 100644 --- a/net/x25/x25_out.c +++ b/net/x25/x25_out.c @@ -78,6 +78,12 @@ int x25_output(struct sock *sk, struct sk_buff *skb) return err; } + /* The call may have been cleared while unlocked */ + if (!x25->neighbour) { + kfree_skb(skbn); + return -ENOTCONN; + } + skb_reserve(skbn, frontlen); len = max_len > skb->len ? skb->len : max_len; -- 2.47.3