From: Martin Schiller <ms@dev.tdt.de>
To: Martin Schiller <ms@dev.tdt.de>,
"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>
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
Subject: [PATCH net v2 1/2] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh()
Date: Fri, 27 Nov 2026 07:28:05 +0100 (CET) [thread overview]
Message-ID: <20261127-x25-kill-by-neigh-v2-1-c7cdcc9f1320@dev.tdt.de> (raw)
In-Reply-To: <20261127-x25-kill-by-neigh-v2-0-c7cdcc9f1320@dev.tdt.de>
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
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 and the peer may reuse that LCI. Skip flagged
sockets in __x25_find_socket(), so that frames for a reused LCI no
longer end up in the old socket's backlog, and keep flagged sockets from
transmitting in x25_kick() and x25_write_internal(). The deferred
x25_disconnect() only changes local state, so 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 from __x25_find_socket() and from
transmission, clear the flag in x25_make_new(), add the current
syzbot report]
Signed-off-by: Martin Schiller <ms@dev.tdt.de>
---
include/net/x25.h | 1 +
net/x25/af_x25.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++-----
net/x25/x25_out.c | 3 ++-
net/x25/x25_subr.c | 4 ++++
4 files changed, 67 insertions(+), 7 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 f45c9ec7d2c68354838b1bfa34ea4ca08120dc8d..a5d3a86918a0f4f337af4566dde4b43a907d1b49 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);
@@ -311,7 +338,8 @@ 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;
}
@@ -492,10 +520,20 @@ 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_and_clear_bit(X25_KILL_FLAG, &x25->flags) && x25->neighbour)
+ x25_disconnect(sk, ENETUNREACH, 0, 0);
+}
+
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)
@@ -610,6 +648,7 @@ 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;
@@ -1761,6 +1800,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;
@@ -1769,13 +1825,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_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
next prev parent reply other threads:[~2026-11-27 6:28 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 6:28 [PATCH net v2 0/2] " Martin Schiller
2026-11-27 6:28 ` Martin Schiller [this message]
2026-11-27 6:28 ` [PATCH net v2 2/2] net/x25: re-check x25->neighbour after re-acquiring the socket lock Martin Schiller
2026-11-27 6:33 ` [PATCH net v2 0/2] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh() netdev-bot+sinfo
2026-11-27 6:43 ` Martin Schiller
2026-11-27 11:48 ` Martin Schiller
2026-11-27 13:30 ` Krystian Kaniewski
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=20261127-x25-kill-by-neigh-v2-1-c7cdcc9f1320@dev.tdt.de \
--to=ms@dev.tdt.de \
--cc=847807867@qq.com \
--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-x25@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=ngocthang2710.1999@gmail.com \
--cc=pabeni@redhat.com \
--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®