net/x25: fix invalid lock_sock() usage in x25_kill_by_neigh()
Subsystems (2)
syzbot
syzbotx25_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.
To: "David S. Miller" <[email protected]>
To: "Eric Dumazet" <[email protected]>
To: "Jakub Kicinski" <[email protected]>
To: <[email protected]>
To: "Martin Schiller" <[email protected]>
To: <[email protected]>
To: "Paolo Abeni" <[email protected]>
To: "Duoming Zhou" <[email protected]>
Cc: "Simon Horman" <[email protected]>
Cc: <[email protected]>
---
v4:
- Added comments above x25_release_cb() and x25_kill_by_neigh() explaining locking and device reference handling.
- Updated commit description to clarify softirq frame delivery, device reference lifetime, and socket ownership by tasks holding lock_sock().
v3:
- Check that x25->neighbour is non-NULL in x25_release_cb() before calling x25_disconnect()
- Remove duplicate wrapped Fixes tag from the commit description
https://lore.kernel.org/all/[email protected]/T/
v2:
- Pass a boolean parameter to x25_kill_by_neigh() and x25_link_terminated() to distinguish softirq from process context.
- Retain lock_sock() in process context to preserve synchronous socket teardown for netdevice notifications.
- Retain write_lock_bh() on x25_list_lock instead of manually managing local_bh_disable().
- Shorten the commit description by removing stack traces and per-function details.
- Fix line wrapping in the Fixes tag.
https://lore.kernel.org/all/[email protected]/T/
v1:
https://lore.kernel.org/all/[email protected]/T/
---
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 · 9 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).
See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at [email protected].Quoted 250 lines · syzbot
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 asNETDEV_DOWNinx25_device_event()), but it is also called in softirq context when receiving LAPB frames inx25_lapb_receive_frame()or link control packets inx25_link_control(), as both lapbether (via NAPI) andhdlc_x25(via tasklet) deliver frames in softirq.
x25_kill_by_neigh()acquireslock_sock()aroundx25_disconnect(). Becauselock_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()andx25_link_terminated()to distinguish between process and softirq contexts. Sockets hold neighbour references and reachnet_deviceonly through the neighbour. In process context (netdevice notifiers), continue usinglock_sock()to synchronously disconnect sockets and wait for tasks owning the socket, becausex25_link_device_down()drops the neighbour's netdevice reference onNETDEV_UNREGISTER. In softirq context, usebh_lock_sock()instead; if the socket is currently owned by a task throughlock_sock(), defer the disconnect by settingX25_KILL_BY_NEIGH_FLAGand handling it inx25_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 syzbotReported-by [email protected]To: "David S. Miller" <[email protected]>
To: "Eric Dumazet" <[email protected]>
To: "Jakub Kicinski" <[email protected]>
To: <[email protected]>
To: "Martin Schiller" <[email protected]>
To: <[email protected]>
To: "Paolo Abeni" <[email protected]>
To: "Duoming Zhou" <[email protected]>
Cc: "Simon Horman" <[email protected]>
Cc: <[email protected]>---
v4:
- Added comments abovex25_release_cb()andx25_kill_by_neigh()explaining locking and device reference handling.
- Updated commit description to clarify softirq frame delivery, device reference lifetime, and socket ownership by tasks holdinglock_sock().v3:
- Check thatx25->neighbouris non-NULL inx25_release_cb()before callingx25_disconnect()
- Remove duplicate wrapped Fixes tag from the commit description
https://lore.kernel.org/all/[email protected]/T/v2:
- Pass a boolean parameter tox25_kill_by_neigh()andx25_link_terminated()to distinguish softirq from process context.
- Retainlock_sock()in process context to preserve synchronous socket teardown for netdevice notifications.
- Retainwrite_lock_bh()onx25_list_lockinstead of manually managinglocal_bh_disable().
- Shorten the commit description by removing stack traces and per-function details.
- Fix line wrapping in the Fixes tag.
https://lore.kernel.org/all/[email protected]/T/v1:
https://lore.kernel.org/all/[email protected]/T/
---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@@ -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; }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); }/*
Quoted 1 line
base-commit:
6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
#syz upstream
Signature · 1 line
Krystian Kaniewski