x25_kill_by_neigh() walks all sockets bound to a neighbour and calls lock_sock() and x25_disconnect() on each one. It is invoked from x25_link_terminated() and x25_link_control(), which both run in softirq context when the LAPB link goes down or a restart frame arrives, e.g. from the lapbether NAPI poll path. lock_sock() takes a mutex in its slow path, so calling it from softirq sleeps in atomic context: BUG: sleeping function called from invalid context at net/core/sock.c:3839 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 14, name: ksoftirqd/0 ... lock_sock_nested+0x66/0x70 x25_kill_by_neigh+0x90/0x110 x25_link_terminated+0x18/0x50 x25_lapb_receive_frame+0x1c2/0x1f0 and lockdep flags the sk_lock-AF_X25 usage violation because the same lock is registered in process context by x25_bind(): WARNING: inconsistent lock state inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} usage. ksoftirqd/0/14 [HC0[0]:SC1[1]:HE1:SE0] takes: ... x25_kill_by_neigh+0x90/0x110 x25_link_terminated+0x18/0x50 x25_lapb_receive_frame+0x1c2/0x1f0 Defer the neighbour socket cleanup to a per-neighbour work item so it always runs in process context. The work item holds a reference on the neighbour and on its net_device, so the deferred cleanup cannot race with neighbour or device teardown; both references are dropped again if queueing fails because the work is already pending. A dedicated workqueue is drained in x25_exit() so that the module stays loaded until pending work has run, and NETDEV_DOWN flushes the work to keep device teardown synchronous as before. Fixes: 7781607938c8 ("net/x25: Fix null-ptr-deref caused by x25_disconnect") Reported-by: syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=300375dfd44665f063a6 Cc: stable@vger.kernel.org Signed-off-by: Shuai Liu <847807867@qq.com> --- Changes in v2: - Use a dedicated workqueue and drain it in x25_exit() so the module stays loaded until pending work has run (Sashiko) - Hold a reference on the neighbour's net_device for the deferred work, and drop it again when queueing fails because the work is already pending (Sashiko) - Flush the work in NETDEV_DOWN to keep device teardown synchronous (Sashiko) - Explicitly request WQ_PERCPU to satisfy the workqueue API - Add Fixes tag and Cc stable Changes in v3: - Flush kill_work in NETDEV_CHANGE as well, restoring synchronous socket cleanup for both process-context notifier events (Sashiko) include/net/x25.h | 3 +++ net/x25/af_x25.c | 12 +++++++++++- net/x25/x25_link.c | 28 +++++++++++++++++++++++++--- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/include/net/x25.h b/include/net/x25.h index 414f3fd993454..125803d4f0ee7 100644 --- a/include/net/x25.h +++ b/include/net/x25.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #define X25_ADDR_LEN 16 @@ -143,6 +144,7 @@ struct x25_neigh { struct sk_buff_head queue; unsigned long t20; struct timer_list t20timer; + struct work_struct kill_work; unsigned long global_facil_mask; refcount_t refcnt; }; @@ -317,6 +319,7 @@ extern struct list_head x25_forward_list; extern rwlock_t x25_forward_list_lock; extern struct list_head x25_neigh_list; extern rwlock_t x25_neigh_list_lock; +extern struct workqueue_struct *x25_wq; int x25_proc_init(void); void x25_proc_exit(void); diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c index f45c9ec7d2c68..3696414617db1 100644 --- a/net/x25/af_x25.c +++ b/net/x25/af_x25.c @@ -222,6 +222,7 @@ static int x25_device_event(struct notifier_block *this, unsigned long event, nb = x25_get_neigh(dev); if (nb) { x25_link_terminated(nb); + flush_work(&nb->kill_work); x25_neigh_put(nb); } x25_route_device_down(dev); @@ -235,6 +236,7 @@ static int x25_device_event(struct notifier_block *this, unsigned long event, nb = x25_get_neigh(dev); if (nb) { x25_link_terminated(nb); + flush_work(&nb->kill_work); x25_neigh_put(nb); } } @@ -1790,9 +1792,13 @@ static int __init x25_init(void) { int rc; + x25_wq = alloc_workqueue("x25", WQ_PERCPU, 0); + if (!x25_wq) + return -ENOMEM; + rc = proto_register(&x25_proto, 0); if (rc) - goto out; + goto out_wq; rc = sock_register(&x25_family_ops); if (rc) @@ -1825,6 +1831,8 @@ static int __init x25_init(void) sock_unregister(AF_X25); out_proto: proto_unregister(&x25_proto); +out_wq: + destroy_workqueue(x25_wq); goto out; } module_init(x25_init); @@ -1843,6 +1851,8 @@ static void __exit x25_exit(void) sock_unregister(AF_X25); proto_unregister(&x25_proto); + + destroy_workqueue(x25_wq); } module_exit(x25_exit); diff --git a/net/x25/x25_link.c b/net/x25/x25_link.c index d1e8cd81c2143..424044c1cf974 100644 --- a/net/x25/x25_link.c +++ b/net/x25/x25_link.c @@ -31,6 +31,8 @@ LIST_HEAD(x25_neigh_list); DEFINE_RWLOCK(x25_neigh_list_lock); +struct workqueue_struct *x25_wq; + static void x25_t20timer_expiry(struct timer_list *); static void x25_transmit_restart_confirmation(struct x25_neigh *nb); @@ -58,6 +60,25 @@ static inline void x25_stop_t20timer(struct x25_neigh *nb) timer_delete(&nb->t20timer); } +static void x25_kill_by_neigh_work(struct work_struct *work) +{ + struct x25_neigh *nb = container_of(work, struct x25_neigh, kill_work); + + x25_kill_by_neigh(nb); + dev_put(nb->dev); + x25_neigh_put(nb); +} + +static void x25_defer_kill_by_neigh(struct x25_neigh *nb) +{ + x25_neigh_hold(nb); + dev_hold(nb->dev); + if (!queue_work(x25_wq, &nb->kill_work)) { + dev_put(nb->dev); + x25_neigh_put(nb); + } +} + /* * This handles all restart and diagnostic frames. */ @@ -82,7 +103,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_defer_kill_by_neigh(nb); x25_transmit_restart_confirmation(nb); break; @@ -97,7 +118,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_defer_kill_by_neigh(nb); x25_transmit_restart_request(nb); nb->state = X25_LINK_STATE_2; @@ -254,7 +275,7 @@ void x25_link_terminated(struct x25_neigh *nb) x25_stop_t20timer(nb); /* Out of order: clear existing virtual calls (X.25 03/93 4.6.3) */ - x25_kill_by_neigh(nb); + x25_defer_kill_by_neigh(nb); } /* @@ -269,6 +290,7 @@ void x25_link_device_up(struct net_device *dev) skb_queue_head_init(&nb->queue); timer_setup(&nb->t20timer, x25_t20timer_expiry, 0); + INIT_WORK(&nb->kill_work, x25_kill_by_neigh_work); dev_hold(dev); nb->dev = dev; -- 2.43.0