net/x25: fix invalid lock_sock() usage in x25_kill_by_neigh()
Subsystems (3)
syzbot
syzbotFrom: Krystian Kaniewski <[email protected]>
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.
---
include/net/x25.h +3 -2
@@ -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 *);
net/x25/af_x25.c +44 -8
94 lines not on this page · raw message
net/x25/x25_dev.c +1 -1
@@ -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);
net/x25/x25_link.c +4 -4
@@ -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
Signature · 4 lines
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 [email protected].
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
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.
Quoted 4 lines
net/x25/af_x25.c +0
-0
[ ... ]
Quoted 10 lines
net/x25/af_x25.c +3
-1@@ -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);
net/x25/af_x25.c void x25_kill_by_neigh(struct x25_neigh *nb) · line 1803
Quoted 10 lines
net/x25/af_x25.c +6
-4-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.
net/x25/af_x25.c void x25_kill_by_neigh(struct x25_neigh *nb) · line 1806
Quoted 3 lines
net/x25/af_x25.c +3
-0+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()?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/00f667e3-a158-4fb5-84a3-9ecda4718ce8%40mail.kernel.org
Quoted 2 lines · On Tue, 24 Nov 2026 09:58:44 +0000, [email protected] 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.
Quoted 3 lines
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/[email protected]/
pw-bot: cr
Signature · 1 line
Krystian Kaniewski
Martin Schiller
maintainerHi Krystian,
Quoted 2 lines · Krystian Kaniewski
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.
Quoted 2 lines
Could you also add the Reported-by and Closes tags for
300375dfd44665f063a6to the series, so that syzbot closes this one too?
Yes, the v2 will carry the tags for both reports.
Quoted 2 lines
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.