mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Martin Schiller <ms@dev.tdt.de>
To: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>, Lin Ma <linma@zju.edu.cn>,
	Duoming Zhou <duoming@zju.edu.cn>,
	Xiyu Yang <xiyuyang19@fudan.edu.cn>,
	Xin Tan <tanxin.ctf@gmail.com>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Clark Williams <clrkwllms@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>
Cc: linux-x25@vger.kernel.org, netdev@vger.kernel.org,
	Nguyen Ngoc Thang <ngocthang2710.1999@gmail.com>,
	Shuai Liu <847807867@qq.com>,
	Krystian Kaniewski <krystianmkaniewski@gmail.com>,
	syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com,
	syzbot+9faa82ae2e94c5c7d2ef@syzkaller.appspotmail.com,
	linux-kernel@vger.kernel.org, linux-rt-devel@lists.linux.dev,
	Martin Schiller <ms@dev.tdt.de>
Subject: [PATCH net v3 2/2] net/x25: re-check x25->neighbour after re-acquiring the socket lock
Date: Tue, 1 Dec 2026 13:01:24 +0100 (CET)	[thread overview]
Message-ID: <20261201-x25-kill-by-neigh-v3-2-7f05bdff15b1@dev.tdt.de> (raw)
In-Reply-To: <20261201-x25-kill-by-neigh-v3-0-7f05bdff15b1@dev.tdt.de>

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 <krystianmkaniewski@gmail.com>
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 <krystianmkaniewski@gmail.com>
Tested-by: Shuai Liu <847807867@qq.com>
Reviewed-by: Shuai Liu <847807867@qq.com>
Signed-off-by: Martin Schiller <ms@dev.tdt.de>
---
 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


      parent reply	other threads:[~2026-12-01 12:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 12:01 [PATCH net v3 0/2] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh() Martin Schiller
2026-12-01 12:01 ` [PATCH net v3 1/2] " Martin Schiller
2026-12-02 12:02   ` sashiko-bot
2026-12-02 12:18     ` Martin Schiller
2026-12-01 12:01 ` Martin Schiller [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201-x25-kill-by-neigh-v3-2-7f05bdff15b1@dev.tdt.de \
    --to=ms@dev.tdt.de \
    --cc=847807867@qq.com \
    --cc=bigeasy@linutronix.de \
    --cc=clrkwllms@kernel.org \
    --cc=davem@davemloft.net \
    --cc=duoming@zju.edu.cn \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=krystianmkaniewski@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linma@zju.edu.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=linux-x25@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=ngocthang2710.1999@gmail.com \
    --cc=pabeni@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com \
    --cc=syzbot+9faa82ae2e94c5c7d2ef@syzkaller.appspotmail.com \
    --cc=tanxin.ctf@gmail.com \
    --cc=willemb@google.com \
    --cc=xiyuyang19@fudan.edu.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®