* [PATCH] net/x25: fix invalid lock_sock() usage in x25_kill_by_neigh()
@ 2026-11-20 9:44 syzbot
2026-11-24 9:58 ` netdev-bot+sashiko
0 siblings, 1 reply; 4+ messages in thread
From: syzbot @ 2026-11-20 9:44 UTC (permalink / raw)
To: syzkaller-bugs, Krystian Kaniewski, David S. Miller, Eric Dumazet,
Jakub Kicinski, linux-x25, Martin Schiller, netdev, Paolo Abeni,
Duoming Zhou
Cc: horms, linux-kernel, syzbot
From: Krystian Kaniewski <krystianmkaniewski@gmail.com>
x25_kill_by_neigh() is invoked to tear down X.25 sockets associated with a
terminating neighbour. This function is called in process context from
netdevice notifier handlers (such as NETDEV_DOWN in x25_device_event()),
but it is also called in softirq context when receiving LAPB frames in
x25_lapb_receive_frame() or link control packets in x25_link_control(), as
both lapbether (via NAPI) and hdlc_x25 (via tasklet) deliver frames in
softirq.
x25_kill_by_neigh() acquires lock_sock() around x25_disconnect(). Because
lock_sock() is a sleeping lock, calling it from softirq context triggers a
"BUG: sleeping function called from invalid context" and a lockdep
inconsistent lock state warning ({SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W}) on
sk_lock-AF_X25.
Fix this by passing a boolean parameter to x25_kill_by_neigh() and
x25_link_terminated() to distinguish between process and softirq contexts.
Sockets hold neighbour references and reach net_device only through the
neighbour. In process context (netdevice notifiers), continue using
lock_sock() to synchronously disconnect sockets and wait for tasks owning
the socket, because x25_link_device_down() drops the neighbour's netdevice
reference on NETDEV_UNREGISTER. In softirq context, use bh_lock_sock()
instead; if the socket is currently owned by a task through lock_sock(),
defer the disconnect by setting X25_KILL_BY_NEIGH_FLAG and handling it in
x25_release_cb() when the socket is released.
Fixes: 7781607938c8 ("net/x25: Fix null-ptr-deref caused by x25_disconnect")
Assisted-by: Gemini:gemini-4-carbon Gemini:gemini-3.8-flash syzbot
Reported-by: syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=300375dfd44665f063a6
Link: https://syzkaller.appspot.com/ai_job?id=98a38af7-51e0-4126-afa7-b8246507da37
Signed-off-by: Krystian Kaniewski <krystianmkaniewski@gmail.com>
---
diff --git a/include/net/x25.h b/include/net/x25.h
index 414f3fd99..ec4c9ced1 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_BY_NEIGH_FLAG 3
/**
* struct x25_route - x25 routing entry
@@ -196,7 +197,7 @@ int x25_addr_aton(unsigned char *, struct x25_address *, struct x25_address *);
struct sock *x25_find_socket(unsigned int, struct x25_neigh *);
void x25_destroy_socket_from_timer(struct sock *);
int x25_rx_call_request(struct sk_buff *, struct x25_neigh *, unsigned int);
-void x25_kill_by_neigh(struct x25_neigh *);
+void x25_kill_by_neigh(struct x25_neigh *, bool);
/* x25_dev.c */
void x25_send_frame(struct sk_buff *, struct x25_neigh *);
@@ -230,7 +231,7 @@ void x25_link_control(struct sk_buff *, struct x25_neigh *, unsigned short);
void x25_link_device_up(struct net_device *);
void x25_link_device_down(struct net_device *);
void x25_link_established(struct x25_neigh *);
-void x25_link_terminated(struct x25_neigh *);
+void x25_link_terminated(struct x25_neigh *, bool);
void x25_transmit_clear_request(struct x25_neigh *, unsigned int,
unsigned char);
void x25_transmit_link(struct sk_buff *, struct x25_neigh *);
diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
index f45c9ec7d..26a8deb82 100644
--- a/net/x25/af_x25.c
+++ b/net/x25/af_x25.c
@@ -221,7 +221,7 @@ static int x25_device_event(struct notifier_block *this, unsigned long event,
case NETDEV_DOWN:
nb = x25_get_neigh(dev);
if (nb) {
- x25_link_terminated(nb);
+ x25_link_terminated(nb, false);
x25_neigh_put(nb);
}
x25_route_device_down(dev);
@@ -234,7 +234,7 @@ static int x25_device_event(struct notifier_block *this, unsigned long event,
if (!netif_carrier_ok(dev)) {
nb = x25_get_neigh(dev);
if (nb) {
- x25_link_terminated(nb);
+ x25_link_terminated(nb, false);
x25_neigh_put(nb);
}
}
@@ -492,10 +492,24 @@ static int x25_listen(struct socket *sock, int backlog)
return rc;
}
+/*
+ * release_sock() calls this with the socket spinlock held while the task
+ * still owns the socket, so x25->neighbour cannot change during the check.
+ */
+static void x25_release_cb(struct sock *sk)
+{
+ struct x25_sock *x25 = x25_sk(sk);
+
+ if (test_and_clear_bit(X25_KILL_BY_NEIGH_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 +624,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_BY_NEIGH_FLAG, &x25->flags);
x25_init_timers(sk);
out:
return sk;
@@ -1761,7 +1776,13 @@ static struct notifier_block x25_dev_notifier = {
.notifier_call = x25_device_event,
};
-void x25_kill_by_neigh(struct x25_neigh *nb)
+/*
+ * The receive path (bh == true) runs in softirq and must not wait for a task
+ * that owns the socket. The netdevice notifier (bh == false) must wait for
+ * every owner because x25_link_device_down() drops the device reference of the
+ * neighbour on NETDEV_UNREGISTER.
+ */
+void x25_kill_by_neigh(struct x25_neigh *nb, bool bh)
{
struct sock *s;
@@ -1769,13 +1790,28 @@ 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 &&
+ (!bh ||
+ !test_bit(X25_KILL_BY_NEIGH_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);
+ if (bh) {
+ bh_lock_sock(s);
+ if (x25_sk(s)->neighbour == nb) {
+ if (sock_owned_by_user(s))
+ set_bit(X25_KILL_BY_NEIGH_FLAG,
+ &x25_sk(s)->flags);
+ else
+ x25_disconnect(s, ENETUNREACH,
+ 0, 0);
+ }
+ bh_unlock_sock(s);
+ } else {
+ lock_sock(s);
+ if (x25_sk(s)->neighbour == nb)
+ x25_disconnect(s, ENETUNREACH, 0, 0);
+ release_sock(s);
+ }
sock_put(s);
goto again;
}
diff --git a/net/x25/x25_dev.c b/net/x25/x25_dev.c
index fb8ac1aa5..be5eb01de 100644
--- a/net/x25/x25_dev.c
+++ b/net/x25/x25_dev.c
@@ -135,7 +135,7 @@ int x25_lapb_receive_frame(struct sk_buff *skb, struct net_device *dev,
break;
case X25_IFACE_DISCONNECT:
- x25_link_terminated(nb);
+ x25_link_terminated(nb, true);
break;
}
x25_neigh_put(nb);
diff --git a/net/x25/x25_link.c b/net/x25/x25_link.c
index d1e8cd81c..d3e90b402 100644
--- a/net/x25/x25_link.c
+++ b/net/x25/x25_link.c
@@ -82,7 +82,7 @@ void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
break;
case X25_LINK_STATE_3:
/* clear existing virtual calls */
- x25_kill_by_neigh(nb);
+ x25_kill_by_neigh(nb, true);
x25_transmit_restart_confirmation(nb);
break;
@@ -97,7 +97,7 @@ void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
break;
case X25_LINK_STATE_3:
/* clear existing virtual calls */
- x25_kill_by_neigh(nb);
+ x25_kill_by_neigh(nb, true);
x25_transmit_restart_request(nb);
nb->state = X25_LINK_STATE_2;
@@ -247,14 +247,14 @@ void x25_link_established(struct x25_neigh *nb)
* request has failed.
*/
-void x25_link_terminated(struct x25_neigh *nb)
+void x25_link_terminated(struct x25_neigh *nb, bool bh)
{
nb->state = X25_LINK_STATE_0;
skb_queue_purge(&nb->queue);
x25_stop_t20timer(nb);
/* Out of order: clear existing virtual calls (X.25 03/93 4.6.3) */
- x25_kill_by_neigh(nb);
+ x25_kill_by_neigh(nb, bh);
}
/*
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
--
See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
The person who has signed off on the patch is responsible for
addressing comments.
syzbot engineers can be reached at syzkaller@googlegroups.com.
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] net/x25: fix invalid lock_sock() usage in x25_kill_by_neigh()
2026-11-20 9:44 [PATCH] net/x25: fix invalid lock_sock() usage in x25_kill_by_neigh() syzbot
@ 2026-11-24 9:58 ` netdev-bot+sashiko
2026-11-24 12:31 ` Krystian Kaniewski
0 siblings, 1 reply; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 9:58 UTC (permalink / raw)
To: syzbot
Cc: syzkaller-bugs, krystianmkaniewski, davem, edumazet, kuba,
linux-x25, ms, netdev, pabeni, duoming, horms, linux-kernel,
syzbot
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 0 (1) · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] When the new softirq path finds a socket owned by a task, it
only sets a flag and leaves the dead call usable until release_sock().
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it is still
reachable after it.
--
net/x25: fix invalid lock_sock() usage in x25_kill_by_neigh()
This patch adds a bh parameter to x25_kill_by_neigh() and
x25_link_terminated(), so the softirq receive paths no longer call
lock_sock(). In softirq context, an unowned socket is disconnected under
bh_lock_sock(). If a task owns the socket, X25_KILL_BY_NEIGH_FLAG is set
and the disconnect is deferred to a new x25_release_cb() hook.
Closes: https://syzkaller.appspot.com/bug?extid=300375dfd44665f063a6
Link: https://syzkaller.appspot.com/ai_job?id=98a38af7-51e0-4126-afa7-b8246507da37
> diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
> index f45c9ec7d2c68..26a8deb82452b 100644
> --- a/net/x25/af_x25.c
> +++ b/net/x25/af_x25.c
[ ... ]
> @@ -1769,13 +1790,28 @@ 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 &&
> + (!bh ||
> + !test_bit(X25_KILL_BY_NEIGH_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);
> + if (bh) {
> + bh_lock_sock(s);
> + if (x25_sk(s)->neighbour == nb) {
> + if (sock_owned_by_user(s))
> + set_bit(X25_KILL_BY_NEIGH_FLAG,
> + &x25_sk(s)->flags);
[Severity: Medium]
When a task owns the socket, this branch only sets the flag. It leaves
x25->lci, x25->neighbour, x25->state and sk_state unchanged. Can the
cleared call still be used until release_sock()?
__x25_find_socket() matches only on lci and neighbour. Frames that arrive
on the old LCI after the restart or link loss still find this socket, and
x25_receive_data() queues them on its backlog:
queued = !sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf));
release_sock() drains the backlog before it calls the new hook:
net/core/sock.c:release_sock() {
...
if (unlikely(sk->sk_backlog.tail))
__release_sock(sk);
if (sk->sk_prot->release_cb) {
if (!tcp_release_cb_cond(sk))
sk->sk_prot->release_cb(sk);
...
}
So those frames go through x25_backlog_rcv()->x25_process_rx_frame() in
the call's old state. Only after that does x25_release_cb() run
x25_disconnect().
For example, a CALL_REQUEST from the peer on a reused LCI right after a
restart would be dropped by the stale socket in state 3. It would not
reach x25_rx_call_request(). DATA or INTERRUPT on that LCI would go to
the dead call, and x25_process_rx_frame() can call x25_kick() and
transmit on the stale LCI.
x25_link_control() also sends the restart confirmation or request right
after x25_kill_by_neigh() returns. At that point the deferred sockets
still hold their LCIs on nb.
The transmit side has a similar gap. x25_link_terminated() purges
nb->queue and resets nb->state to X25_LINK_STATE_0. An owner that is
still in sendmsg can then reach
x25_kick()->x25_send_iframe()->x25_transmit_link():
net/x25/x25_link.c:x25_transmit_link() {
...
case X25_LINK_STATE_0:
skb_queue_tail(&nb->queue, skb);
nb->state = X25_LINK_STATE_1;
x25_establish_link(nb);
...
}
Would that re-establish a link that the peer just took down?
The RESTART_CONFIRMATION path in x25_link_control() sets nb->state to
X25_LINK_STATE_2. There, the queued frames would be flushed once state 3
is reached, on an LCI the restart already cleared.
Before 7781607938c8, the socket was disconnected right away (lci = 0,
neighbour = NULL). The unowned branch here does the same. With either,
none of these paths can happen.
> + else
> + x25_disconnect(s, ENETUNREACH,
> + 0, 0);
[Severity: High]
This is a pre-existing issue and this patch did not introduce it, but the
new unowned branch adds another way to reach it. Can x25_sendmsg() and
x25_output() dereference a NULL x25->neighbour?
x25_sendmsg() checks x25->neighbour under the socket lock, then drops the
lock to allocate:
net/x25/af_x25.c:x25_sendmsg() {
...
rc = -ENETUNREACH;
if (!x25->neighbour)
goto out;
...
release_sock(sk);
skb = sock_alloc_send_skb(sk, size, noblock, &rc);
lock_sock(sk);
...
if (msg->msg_flags & MSG_OOB) {
if (x25->neighbour->extended) {
...
} else {
if (x25->neighbour->extended) {
...
}
x25_output() follows the same pattern in its fragmentation loop:
net/x25/x25_out.c:x25_output() {
...
release_sock(sk);
skbn = sock_alloc_send_skb(sk, frontlen + max_len,
noblock, &err);
lock_sock(sk);
...
if (skb->len > 0) {
if (x25->neighbour->extended)
...
}
sock_alloc_send_pskb() checks sock_error() and SEND_SHUTDOWN only before
it allocates. Suppose x25_disconnect() runs after those checks and before
lock_sock() returns. Then x25->neighbour is NULL, and the next
x25->neighbour->extended faults.
Several softirq paths can do this while the socket is unowned:
- a CLEAR_REQUEST from the peer in state 3/4/5, via
x25_receive_data()->x25_process_rx_frame()->x25_disconnect()
- T23 timer expiry
- with this patch, this branch, reached from x25_lapb_receive_frame() on
X25_IFACE_DISCONNECT, or from x25_link_control() on a
RESTART_REQUEST/RESTART_CONFIRMATION in X25_LINK_STATE_3
The window gets wider if another thread holds lock_sock() while sendmsg
waits in lock_sock(). Peer frames are then backlogged and processed by
x25_backlog_rcv() in that thread's release_sock().
7781607938c8, the commit in the Fixes: tag, was meant to close this class
of neighbour NULL dereference in sendmsg/recvmsg. Should x25_sendmsg()
and x25_output() check x25->neighbour (or sk_state) again after they
retake lock_sock()?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/00f667e3-a158-4fb5-84a3-9ecda4718ce8%40mail.kernel.org
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] net/x25: fix invalid lock_sock() usage in x25_kill_by_neigh()
2026-11-24 9:58 ` netdev-bot+sashiko
@ 2026-11-24 12:31 ` Krystian Kaniewski
2026-11-24 12:55 ` Martin Schiller
0 siblings, 1 reply; 4+ messages in thread
From: Krystian Kaniewski @ 2026-11-24 12:31 UTC (permalink / raw)
To: netdev-bot+sashiko, syzbot
Cc: syzkaller-bugs, davem, edumazet, kuba, linux-x25, ms, netdev,
pabeni, duoming, horms, linux-kernel, syzbot, ngocthang2710.1999
On Tue, 24 Nov 2026 09:58:44 +0000, netdev-bot+sashiko@kernel.org wrote:
> - [Medium] When the new softirq path finds a socket owned by a task, it
> only sets a flag and leaves the dead call usable until release_sock().
Agreed. While a task owns the socket, the flagged call keeps its LCI
binding until release_sock(). __x25_find_socket() still returns it, so
frames that arrive after the restart go to its backlog and are processed
in the old state before x25_release_cb() runs. The owner can also still
transmit on the old LCI, and after a LAPB disconnect this brings the
link back up.
This patch follows the same design as Thang's patch from October [1].
In that thread Martin confirmed both effects in a test on 23 November
and announced a v2 which skips flagged sockets in __x25_find_socket()
and returns early from x25_kick() and x25_write_internal() for them.
That covers this finding, so please drop this patch in favour of his
series. I will not send a v2 of it.
> Pre-existing issues:
> - [High] This problem wasn't introduced by this patch, but it is still
> reachable after it.
Agreed as well. x25_disconnect() clears x25->neighbour since commit
4becb7ee5b3d ("net/x25: Fix x25_neigh refcnt leak when x25
disconnect"), and x25_sendmsg() and x25_output() dereference it after
they take the socket lock again. Martin wrote that he has a separate
fix for this.
x25_recvmsg() has the same kind of window, and the review did not
report it. If x25_disconnect() runs after skb_recv_datagram() has
dequeued a packet and before lock_sock() returns, x25_recvmsg() then
calls x25_check_rbuf() on a socket without a neighbour.
x25_disconnect() does not clear x25->condition, so with
X25_COND_OWN_RX_BUSY still set and sk_rmem_alloc below half of
sk_rcvbuf, x25_check_rbuf() sends an RR through x25_write_internal(),
which dereferences x25->neighbour. I found this by reading the code and
have not reproduced it, but it may be worth covering in the same fix.
Martin, syzbot tracks this report separately from the one in Thang's
patch (extid 9faa82ae2e94c5c7d2ef). Could you also add the Reported-by
and Closes tags for 300375dfd44665f063a6 to the series, so that syzbot
closes this one too?
I am happy to test the series with the syzbot reproducer once it is
posted.
[1] https://lore.kernel.org/all/20261001150118.128470-1-ngocthang2710.1999@gmail.com/
pw-bot: cr
--
Krystian Kaniewski
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] net/x25: fix invalid lock_sock() usage in x25_kill_by_neigh()
2026-11-24 12:31 ` Krystian Kaniewski
@ 2026-11-24 12:55 ` Martin Schiller
0 siblings, 0 replies; 4+ messages in thread
From: Martin Schiller @ 2026-11-24 12:55 UTC (permalink / raw)
To: Krystian Kaniewski
Cc: netdev-bot+sashiko, syzbot, syzkaller-bugs, davem, edumazet, kuba,
linux-x25, netdev, pabeni, duoming, horms, linux-kernel, syzbot,
ngocthang2710.1999
Hi Krystian,
On Tue, 24 Nov 2026 13:31:55 +0100, Krystian Kaniewski wrote:
> x25_recvmsg() has the same kind of window, and the review did not
> report it.
Good catch, the code agrees: x25_disconnect() leaves x25->condition
alone, so x25_check_rbuf() can still send an RR through
x25_write_internal(). I'll cover x25_recvmsg() in the separate fix for
x25_sendmsg() and x25_output(), with your Reported-by.
> Could you also add the Reported-by and Closes tags for
> 300375dfd44665f063a6 to the series, so that syzbot closes this one too?
Yes, the v2 will carry the tags for both reports.
> I am happy to test the series with the syzbot reproducer once it is
> posted.
Thanks, that would be great. Thang can still send the v2 himself this
week; otherwise I'll post it. Either way, you'll be on Cc.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-24 12:55 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 9:44 [PATCH] net/x25: fix invalid lock_sock() usage in x25_kill_by_neigh() syzbot
2026-11-24 9:58 ` netdev-bot+sashiko
2026-11-24 12:31 ` Krystian Kaniewski
2026-11-24 12:55 ` Martin Schiller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox