🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
2 messages
2 total messages Started by Martin Schiller Tue, 01 Dec 2026 12:01
[PATCH net v3 0/2] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh()
#25211
Author: Martin Schiller
Date: Tue, 01 Dec 2026 12:01
105 lines
15215 bytes
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>
[PATCH net v3 1/2] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh()
#25212
Author: Martin Schiller
Date: Tue, 01 Dec 2026 12:01
373 lines
22630 bytes
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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads