Netdev List
 help / color / mirror / Atom feed
* [PATCH net v3 0/2] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh()
@ 2026-12-01 12:01 Martin Schiller
  2026-12-01 12:01 ` [PATCH net v3 1/2] " Martin Schiller
  2026-12-01 12:01 ` [PATCH net v3 2/2] net/x25: re-check x25->neighbour after re-acquiring the socket lock Martin Schiller
  0 siblings, 2 replies; 4+ messages in thread
From: Martin Schiller @ 2026-12-01 12:01 UTC (permalink / raw)
  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, netdev, Nguyen Ngoc Thang, Shuai Liu,
	Krystian Kaniewski, syzbot+300375dfd44665f063a6,
	syzbot+9faa82ae2e94c5c7d2ef, linux-kernel, linux-rt-devel,
	Martin Schiller

x25_kill_by_neigh() takes lock_sock() on all sockets of a neighbour,
but it also runs in softirq context when the link layer reports a
disconnect or the peer restarts the link. syzbot hits this on mainline,
net, net-next and the LTS trees. A plain "ip link set lapb0 down" with a
connected peer is enough: LAPB sends a DISC, and the peer clears its
calls from softirq.

Patch 1 is based on Nguyen Ngoc Thang's fix [1]. Sockets that are not
owned are cleared right away under the socket spinlock, owned sockets
are flagged and cleared from a new release_cb, and NETDEV_DOWN still
waits for owners. A flagged socket no longer matches incoming frames and
no longer transmits, so a new call on its LCI after a restart reaches
the listener, and child sockets do not inherit the flag. v3 addresses
the AI review of v2 [6]: the flag is only cleared once the socket's LCI
has been reset, and transmissions in process context and the backlog
processing take the socket spinlock, so that an owner cannot send
frames of the old call after the restart has been confirmed.

Patch 2 fixes a pre-existing NULL pointer dereference in x25_sendmsg(),
x25_output() and x25_recvmsg() after they retake the socket lock. The
AI review of v1 [2] pointed out the first two, Krystian Kaniewski found
the x25_recvmsg() case and reproduced all three at runtime [7]. It does
not depend on patch 1. The AI review of v2 also notes [8] that a
reconnect on the same socket while x25_output() waits would pass the
new check; that is a separate, pre-existing issue that this series does
not address.

Deferring the whole cleanup to a workqueue [3][4] also avoids the
splat, but the restart is then confirmed while the old calls are still
bound to their LCIs. [5] had the same design as v1. Both have been
withdrawn in favour of this series.

Tested in QEMU (single CPU) with lockdep and DEBUG_ATOMIC_SLEEP, using
two lapbether devices on a veth pair. A sender stalled in x25_sendmsg()
via userfaultfd keeps the calling socket owned during the link event:

                                       unpatched  v1    v3    wq [4]
  1  link loss, calls idle             splat      ok    ok    ok
  2  link loss, caller owned           -          FAIL  ok    FAIL
  3  restart, caller owned, then a
     CALL REQUEST on the caller's LCI  -          FAIL  ok    FAIL
  4  ip link set lapb0 down            splat      ok    ok    ok

In test 2, the old call brings the link back up (SABM) before it is
cleared. In test 3, the new call does not reach the listener, and the
old call sends a DATA packet on the reused LCI.

Tests 2 and 3 were not run unpatched, as that kernel would sleep in
softirq. With v3, lockdep reports nothing in these tests, including the
new uses of the socket spinlock. Krystian tested v2 with the syzbot
reproducers of both reports, also with the PREEMPT_RT config of the
second one [7], and Shuai Liu tested and reviewed v2; their tags are
kept for patch 2 only, as patch 1 changed. The two races fixed in v3
are not covered by a test.

Changes in v3:
- clear X25_KILL_FLAG only after x25_disconnect() has reset the LCI
- take the socket spinlock around transmissions in process context and
  around the backlog processing
- update the comments of __x25_find_socket() and x25_new_lci()
- patch 2: describe the runtime reproduction, add Tested-by and
  Reviewed-by
- Link to v2: https://lore.kernel.org/all/20261127-x25-kill-by-neigh-v2-0-c7cdcc9f1320@dev.tdt.de/

Changes in v2:
- skip flagged sockets in __x25_find_socket()
- no transmission from flagged sockets in x25_kick() and
  x25_write_internal()
- clear the flag in x25_make_new()
- add the current syzbot report
- new patch 2

The changes on top of v1, patch 2 and the tests were prepared with the
help of an AI assistant (Claude, Anthropic).

[1] https://lore.kernel.org/all/20261001150118.128470-1-ngocthang2710.1999@gmail.com/
[2] https://lore.kernel.org/all/179121364205.434549.10310203820251738113@kernel.org/
[3] https://lore.kernel.org/all/tencent_F7C2DC0FA42B5866E5C39A9E70D3C52C2609@qq.com/
[4] https://lore.kernel.org/all/tencent_8BA4FF587EE5579AB67149C5062618DC7109@qq.com/
[5] https://lore.kernel.org/all/00f667e3-a158-4fb5-84a3-9ecda4718ce8@mail.kernel.org/
[6] https://lore.kernel.org/all/179585053119.3135.1065383095936227619@kernel.org/
[7] https://lore.kernel.org/all/5ad2c46b-4ed9-4689-950f-93753fa1586e@gmail.com/
[8] https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127-x25-kill-by-neigh-v2-0-c7cdcc9f1320%40dev.tdt.de

---
Martin Schiller (1):
      net/x25: re-check x25->neighbour after re-acquiring the socket lock

Nguyen Ngoc Thang (1):
      net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh()

 include/net/x25.h  |   1 +
 net/x25/af_x25.c   | 123 +++++++++++++++++++++++++++++++++++++++++++++++------
 net/x25/x25_in.c   |   7 ++-
 net/x25/x25_out.c  |   9 +++-
 net/x25/x25_subr.c |   4 ++
 5 files changed, 129 insertions(+), 15 deletions(-)
---
base-commit: d73525e6950b982fb67c74a1a64163529305a003
change-id: 20261124-x25-kill-by-neigh-2b70f6d19961

Best regards,
-- 
Martin Schiller <ms@dev.tdt.de>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH net v3 1/2] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh()
  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 ` Martin Schiller
       [not found]   ` <sashiko-outbox-245652@kernel.org>
  2026-12-01 12:01 ` [PATCH net v3 2/2] net/x25: re-check x25->neighbour after re-acquiring the socket lock Martin Schiller
  1 sibling, 1 reply; 4+ messages in thread
From: Martin Schiller @ 2026-12-01 12:01 UTC (permalink / raw)
  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, netdev, Nguyen Ngoc Thang, Shuai Liu,
	Krystian Kaniewski, syzbot+300375dfd44665f063a6,
	syzbot+9faa82ae2e94c5c7d2ef, linux-kernel, linux-rt-devel,
	Martin Schiller

From: Nguyen Ngoc Thang <ngocthang2710.1999@gmail.com>

x25_kill_by_neigh() is reached from the receive path when the link layer
goes away or the peer restarts:

  net_rx_action()
    lapbeth_napi_poll()
      x25_lapb_receive_frame()
        x25_link_terminated() / x25_link_control()
          x25_kill_by_neigh()
            lock_sock()

lock_sock() may sleep, which is not allowed in softirq context:

  BUG: sleeping function called from invalid context at net/core/sock.c:3832
  in_atomic(): 0, irqs_disabled(): 0, non_block: 0, pid: 29, name: ktimers/1
  RCU nest depth: 2, expected: 0
  ...
   lock_sock_nested+0x56/0x130 net/core/sock.c:3832
   x25_kill_by_neigh+0x134/0x2a0 net/x25/af_x25.c:1778
   x25_lapb_receive_frame+0x1b0/0xfb0 net/x25/x25_dev.c:138

Without PREEMPT_RT, lockdep reports an inconsistent lock state on
sk_lock-AF_X25 instead. syzbot found this, and it is easy to trigger:
"ip link set lapb0 down" on a lapbether device with a connected peer
makes LAPB send a DISC, and the peer clears its calls from softirq.

Take the socket spinlock instead, the same way the x25 timers and receive
path already do. If the socket is not owned by user, disconnect it right
away. If it is, mark it with X25_KILL_FLAG and let a new release_cb do
the disconnect when the owner releases the socket. This keeps the
serialization against x25_sendmsg()/x25_recvmsg()/x25_connect() that
commit 7781607938c8 ("net/x25: Fix null-ptr-deref caused by
x25_disconnect") added lock_sock() for. Sockets that already have the
flag set are skipped, so the rescan loop always terminates.

NETDEV_DOWN runs in process context and the device may be freed right
after, so keep waiting for socket owners there as before: a sleeping
sweep makes sure no socket still uses the neighbour on return.

Until its owner releases it, a flagged socket still holds the LCI of a
call that no longer exists, while x25_link_control() may already have
confirmed the restart. The LCI can then be reused, by the peer or
locally through x25_new_lci(). Skip flagged sockets in
__x25_find_socket(), so that frames for a reused LCI no longer end up in
the old socket, and clear the flag only after x25_disconnect() has reset
the LCI.

Keep flagged sockets from transmitting in x25_kick() and
x25_write_internal(). As the flag is set under the socket spinlock, take
it around transmissions in process context and around the backlog
processing too; otherwise a transmission that was already under way
could still send frames of the old call after the restart has been
confirmed. With that, the deferred x25_disconnect() only changes local
state, and the old call is gone on the wire before the restart is
answered, as it was before.

x25_connect() accepts a listening socket, so a listener can carry the
flag. Clear it in x25_make_new(), which copies the listener's flags.

Fixes: 7781607938c8 ("net/x25: Fix null-ptr-deref caused by x25_disconnect")
Reported-by: syzbot+9faa82ae2e94c5c7d2ef@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=9faa82ae2e94c5c7d2ef
Reported-by: syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=300375dfd44665f063a6
Signed-off-by: Nguyen Ngoc Thang <ngocthang2710.1999@gmail.com>
[ms: hide flagged sockets and keep them silent, add the current report]
Signed-off-by: Martin Schiller <ms@dev.tdt.de>
---
 include/net/x25.h  |   1 +
 net/x25/af_x25.c   | 115 +++++++++++++++++++++++++++++++++++++++++++++++------
 net/x25/x25_in.c   |   7 +++-
 net/x25/x25_out.c  |   3 +-
 net/x25/x25_subr.c |   4 ++
 5 files changed, 115 insertions(+), 15 deletions(-)

diff --git a/include/net/x25.h b/include/net/x25.h
index 414f3fd99345455dd30f0cc827736fbce61fd99a..ed8ba01fa3cb396f80344a23f4de8d23d5e4a010 100644
--- a/include/net/x25.h
+++ b/include/net/x25.h
@@ -118,6 +118,7 @@ enum {
 #define X25_Q_BIT_FLAG		0
 #define X25_INTERRUPT_FLAG	1
 #define X25_ACCPT_APPRV_FLAG	2
+#define X25_KILL_FLAG		3
 
 /**
  *	struct x25_route - x25 routing entry
diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
index 802688d5ad5687850764d0b779c72f039097b3d5..43c606a6bfa5a3cf69c905f230edf2c37a85e25d 100644
--- a/net/x25/af_x25.c
+++ b/net/x25/af_x25.c
@@ -200,6 +200,32 @@ static void x25_remove_socket(struct sock *sk)
 	write_unlock_bh(&x25_list_lock);
 }
 
+/*
+ * Process context only: wait for owners that x25_kill_by_neigh() deferred,
+ * so no socket still uses nb once the device goes away.
+ */
+static void x25_kill_by_neigh_sync(struct x25_neigh *nb)
+{
+	struct sock *s;
+
+again:
+	write_lock_bh(&x25_list_lock);
+
+	sk_for_each(s, &x25_list) {
+		if (x25_sk(s)->neighbour == nb) {
+			sock_hold(s);
+			write_unlock_bh(&x25_list_lock);
+			lock_sock(s);
+			if (x25_sk(s)->neighbour == nb)
+				x25_disconnect(s, ENETUNREACH, 0, 0);
+			release_sock(s);
+			sock_put(s);
+			goto again;
+		}
+	}
+	write_unlock_bh(&x25_list_lock);
+}
+
 /*
  *	Handle device status changes.
  */
@@ -222,6 +248,7 @@ static int x25_device_event(struct notifier_block *this, unsigned long event,
 			nb = x25_get_neigh(dev);
 			if (nb) {
 				x25_link_terminated(nb);
+				x25_kill_by_neigh_sync(nb);
 				x25_neigh_put(nb);
 			}
 			x25_route_device_down(dev);
@@ -305,13 +332,16 @@ static struct sock *x25_find_listener(struct x25_address *addr,
 
 /*
  *	Find a connected X.25 socket given my LCI and neighbour.
+ *	Sockets flagged by x25_kill_by_neigh() are skipped: their call no
+ *	longer exists, and the LCI may already belong to a new one.
  */
 static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
 {
 	struct sock *s;
 
 	sk_for_each(s, &x25_list)
-		if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
+		if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb &&
+		    !test_bit(X25_KILL_FLAG, &x25_sk(s)->flags)) {
 			sock_hold(s);
 			goto found;
 		}
@@ -331,7 +361,8 @@ struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
 }
 
 /*
- *	Find a unique LCI for a given device.
+ *	Find a unique LCI for a given device. The LCI of a socket flagged
+ *	by x25_kill_by_neigh() counts as free, its call has already ended.
  */
 static unsigned int x25_new_lci(struct x25_neigh *nb)
 {
@@ -492,10 +523,25 @@ static int x25_listen(struct socket *sock, int backlog)
 	return rc;
 }
 
+/* Finish a link teardown that hit while the socket was owned by user. */
+static void x25_release_cb(struct sock *sk)
+{
+	struct x25_sock *x25 = x25_sk(sk);
+
+	if (!test_bit(X25_KILL_FLAG, &x25->flags))
+		return;
+
+	/* Stay hidden from __x25_find_socket() until the LCI is reset */
+	if (x25->neighbour)
+		x25_disconnect(sk, ENETUNREACH, 0, 0);
+	clear_bit(X25_KILL_FLAG, &x25->flags);
+}
+
 static struct proto x25_proto = {
 	.name	  = "X25",
 	.owner	  = THIS_MODULE,
 	.obj_size = sizeof(struct x25_sock),
+	.release_cb = x25_release_cb,
 };
 
 static struct sock *x25_alloc_socket(struct net *net, int kern)
@@ -611,11 +657,39 @@ static struct sock *x25_make_new(struct sock *osk)
 	x25->cudmatchlength = ox25->cudmatchlength;
 
 	clear_bit(X25_INTERRUPT_FLAG, &x25->flags);
+	clear_bit(X25_KILL_FLAG, &x25->flags);
 	x25_init_timers(sk);
 out:
 	return sk;
 }
 
+/*
+ * x25_kill_sock() flags a socket that is owned by user while holding the
+ * socket spinlock. Transmissions in process context take it as well, so
+ * that a flagged socket sends nothing more, not even from a transmission
+ * that was already under way when the flag was set.
+ */
+static void x25_write_internal_owned(struct sock *sk, int frametype)
+{
+	spin_lock_bh(&sk->sk_lock.slock);
+	x25_write_internal(sk, frametype);
+	spin_unlock_bh(&sk->sk_lock.slock);
+}
+
+static void x25_kick_owned(struct sock *sk)
+{
+	spin_lock_bh(&sk->sk_lock.slock);
+	x25_kick(sk);
+	spin_unlock_bh(&sk->sk_lock.slock);
+}
+
+static void x25_check_rbuf_owned(struct sock *sk)
+{
+	spin_lock_bh(&sk->sk_lock.slock);
+	x25_check_rbuf(sk);
+	spin_unlock_bh(&sk->sk_lock.slock);
+}
+
 static int x25_release(struct socket *sock)
 {
 	struct sock *sk = sock->sk;
@@ -640,7 +714,7 @@ static int x25_release(struct socket *sock)
 		case X25_STATE_3:
 		case X25_STATE_4:
 			x25_clear_queues(sk);
-			x25_write_internal(sk, X25_CLEAR_REQUEST);
+			x25_write_internal_owned(sk, X25_CLEAR_REQUEST);
 			x25_start_t23timer(sk);
 			x25->state = X25_STATE_2;
 			sk->sk_state	= TCP_CLOSE;
@@ -651,7 +725,7 @@ static int x25_release(struct socket *sock)
 			break;
 
 		case X25_STATE_5:
-			x25_write_internal(sk, X25_CLEAR_REQUEST);
+			x25_write_internal_owned(sk, X25_CLEAR_REQUEST);
 			x25_disconnect(sk, 0, 0, 0);
 			__x25_destroy_socket(sk);
 			goto out;
@@ -805,7 +879,7 @@ static int x25_connect(struct socket *sock, struct sockaddr_unsized *uaddr,
 
 	x25->state = X25_STATE_1;
 
-	x25_write_internal(sk, X25_CALL_REQUEST);
+	x25_write_internal_owned(sk, X25_CALL_REQUEST);
 
 	x25_start_heartbeat(sk);
 	x25_start_t21timer(sk);
@@ -1257,7 +1331,7 @@ static int x25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
 			len++;
 	}
 
-	x25_kick(sk);
+	x25_kick_owned(sk);
 	rc = len;
 out:
 	release_sock(sk);
@@ -1359,7 +1433,7 @@ static int x25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
 		msg->msg_namelen = sizeof(*sx25);
 	}
 
-	x25_check_rbuf(sk);
+	x25_check_rbuf_owned(sk);
 	rc = copied;
 out_free_dgram:
 	skb_free_datagram(sk, skb);
@@ -1598,7 +1672,7 @@ static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 		/* must call accptapprv above */
 		if (test_bit(X25_ACCPT_APPRV_FLAG, &x25->flags))
 			goto out_sendcallaccpt_release;
-		x25_write_internal(sk, X25_CALL_ACCEPTED);
+		x25_write_internal_owned(sk, X25_CALL_ACCEPTED);
 		x25->state = X25_STATE_3;
 		rc = 0;
 out_sendcallaccpt_release:
@@ -1762,6 +1836,23 @@ static struct notifier_block x25_dev_notifier = {
 	.notifier_call = x25_device_event,
 };
 
+/* May run in softirq, so lock_sock() is not an option. */
+static void x25_kill_sock(struct sock *sk, struct x25_neigh *nb)
+{
+	struct x25_sock *x25 = x25_sk(sk);
+
+	local_bh_disable();
+	bh_lock_sock(sk);
+	if (x25->neighbour == nb) {
+		if (sock_owned_by_user(sk))
+			set_bit(X25_KILL_FLAG, &x25->flags);
+		else
+			x25_disconnect(sk, ENETUNREACH, 0, 0);
+	}
+	bh_unlock_sock(sk);
+	local_bh_enable();
+}
+
 void x25_kill_by_neigh(struct x25_neigh *nb)
 {
 	struct sock *s;
@@ -1770,13 +1861,11 @@ void x25_kill_by_neigh(struct x25_neigh *nb)
 	write_lock_bh(&x25_list_lock);
 
 	sk_for_each(s, &x25_list) {
-		if (x25_sk(s)->neighbour == nb) {
+		if (x25_sk(s)->neighbour == nb &&
+		    !test_bit(X25_KILL_FLAG, &x25_sk(s)->flags)) {
 			sock_hold(s);
 			write_unlock_bh(&x25_list_lock);
-			lock_sock(s);
-			if (x25_sk(s)->neighbour == nb)
-				x25_disconnect(s, ENETUNREACH, 0, 0);
-			release_sock(s);
+			x25_kill_sock(s, nb);
 			sock_put(s);
 			goto again;
 		}
diff --git a/net/x25/x25_in.c b/net/x25/x25_in.c
index e47ebd8acd21bff8d8fa1f5f48163145e9ac00f2..297acfdbfb05da00894651bb7ee27d8753ef3339 100644
--- a/net/x25/x25_in.c
+++ b/net/x25/x25_in.c
@@ -450,7 +450,12 @@ int x25_process_rx_frame(struct sock *sk, struct sk_buff *skb)
 
 int x25_backlog_rcv(struct sock *sk, struct sk_buff *skb)
 {
-	int queued = x25_process_rx_frame(sk, skb);
+	int queued;
+
+	/* Serialize against x25_kill_sock() like the softirq receive path */
+	spin_lock_bh(&sk->sk_lock.slock);
+	queued = x25_process_rx_frame(sk, skb);
+	spin_unlock_bh(&sk->sk_lock.slock);
 
 	if (!queued)
 		kfree_skb(skb);
diff --git a/net/x25/x25_out.c b/net/x25/x25_out.c
index f8922b0e23a4f17c1f5f74aa24806a35325e92dd..f710d015fcc2ffd6a6922a0c29876bb248125666 100644
--- a/net/x25/x25_out.c
+++ b/net/x25/x25_out.c
@@ -140,7 +140,8 @@ void x25_kick(struct sock *sk)
 	int modulus;
 	struct x25_sock *x25 = x25_sk(sk);
 
-	if (x25->state != X25_STATE_3)
+	if (x25->state != X25_STATE_3 ||
+	    test_bit(X25_KILL_FLAG, &x25->flags))
 		return;
 
 	/*
diff --git a/net/x25/x25_subr.c b/net/x25/x25_subr.c
index 159708d9ad20cb2e6db24ead67daf1e9d6258f64..dba3b8eb10b0f21402592fe5775e1670f68ed4ee 100644
--- a/net/x25/x25_subr.c
+++ b/net/x25/x25_subr.c
@@ -120,6 +120,10 @@ void x25_write_internal(struct sock *sk, int frametype)
 	 */
 	int len = X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
 
+	/* The call has already been cleared by a link failure or restart */
+	if (test_bit(X25_KILL_FLAG, &x25->flags))
+		return;
+
 	/*
 	 *	Adjust frame size.
 	 */

-- 
2.47.3


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH net v3 2/2] net/x25: re-check x25->neighbour after re-acquiring the socket lock
  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-01 12:01 ` Martin Schiller
  1 sibling, 0 replies; 4+ messages in thread
From: Martin Schiller @ 2026-12-01 12:01 UTC (permalink / raw)
  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, netdev, Nguyen Ngoc Thang, Shuai Liu,
	Krystian Kaniewski, syzbot+300375dfd44665f063a6,
	syzbot+9faa82ae2e94c5c7d2ef, linux-kernel, linux-rt-devel,
	Martin Schiller

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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net v3 1/2] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh()
       [not found]   ` <sashiko-outbox-245652@kernel.org>
@ 2026-12-02 12:18     ` Martin Schiller
  0 siblings, 0 replies; 4+ messages in thread
From: Martin Schiller @ 2026-12-02 12:18 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: linux-rt-devel, linux-kernel, netdev, linux-x25

On Wed, 02 Dec 2026 13:02:09 +0100, sashiko-bot@kernel.org wrote:
> Should x25_process_rx_frame() also bail out when X25_KILL_FLAG is set?

No. Frames on the backlog arrived before the link event and belong to
the call, so they are still handled: data is delivered to the user, and
a CLEAR REQUEST from the peer clears the call. Anything they would make
the socket send is suppressed by X25_KILL_FLAG.

> For example, a backlogged
> X25_CLEAR_REQUEST or X25_CLEAR_CONFIRMATION frame calls
> x25_disconnect(sk, 0, ...), clearing x25->neighbour and setting
> sk->sk_err = 0, so x25_release_cb() then skips
> x25_disconnect(sk, ENETUNREACH, 0, 0) and clears X25_KILL_FLAG, losing
> ENETUNREACH

That matches the current behaviour: x25_kill_by_neigh() takes
lock_sock(), so release_sock() runs the backlog first, and it then
only disconnects the socket if x25->neighbour is still the neighbour.
If the peer cleared the call before the link went away, the socket
reports that clear, with its cause and diagnostic, in both cases. The
NULL pointer dereference in x25_recvmsg() is what patch 2 fixes.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-02 12:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
     [not found]   ` <sashiko-outbox-245652@kernel.org>
2026-12-02 12:18     ` Martin Schiller
2026-12-01 12:01 ` [PATCH net v3 2/2] net/x25: re-check x25->neighbour after re-acquiring the socket lock Martin Schiller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox