* [PATCH v15 0/3] can: j1939: make module more robust
@ 2026-11-17 15:56 Tetsuo Handa
2026-11-17 15:56 ` [PATCH v15 1/3] can: j1939: fix possibility of UAF Tetsuo Handa
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Tetsuo Handa @ 2026-11-17 15:56 UTC (permalink / raw)
To: linux-can, Marc Kleine-Budde, Oleksij Rempel,
Robin van der Gracht, kernel, Oliver Hartkopp
This series is intended to address the following problems.
# Problem 1
Since module refcount is currently associated with lifecycle of j1939
sockets, we need to make sure that all resources associated with socket
("struct j1939_priv", "struct j1939_ecu", "struct j1939_session") are
synchronously destructed before socket is destructed. Otherwise,
j1939_module_exit() can complete and memory for this module is unmapped
and cause UAF.
# Problem 2
This module takes a refcount upon arming a hrtimer and drops that refcount
when the hrtimer is fired or canceled, but nothing guarantees that the
callback is no longer running when the last refcount is dropped. We need
to make sure that hrtimer callbacks are no longer running before calling
kfree(). But we can't use hrtimer_cancel() from atomic context.
# Problem 3
Since j1939_ac_recv() and j1939_ac_fixup() are calling j1939_ecu_put()
without priv->lock held for write, list corruption might happen if the
last user called j1939_ecu_put() without priv->lock held for write. But
requiring all j1939_ecu_put() callers to hold priv->lock for write might
be a difficult change.
# Problem 4
Since
j1939_ecu_get(ecu);
priv->ents[ecu->addr] = ecu;
in j1939_ecu_map_locked() from j1939_ecu_timer_handler() can succeed
even after
priv->ents[ecu->addr] = NULL;
j1939_ecu_put(ecu);
in j1939_ecu_unmap_locked() from j1939_ecu_unmap_all() from
__j1939_rx_release() from j1939_netdev_stop() has completed.
# Problem 5
Since j1939_ecu_timer_cancel() is calling hrtimer_cancel() with priv->lock
held for write while j1939_ecu_timer_handler() takes priv->lock, a hard-
lockup (if CONFIG_PREEMPT_RT=n && CONFIG_SMP=y) or a deadlock between
priv->lock and cpu_base->softirq_expiry_lock (if CONFIG_PREEMPT_RT=y) will
happen when hrtimer_cancel() calls hrtimer_cancel_wait_running().
[PATCH v15 1/3] can: j1939: fix possibility of UAF
[PATCH v15 2/3] can: j1939: fix "struct j1939_ecu" refcount leak
[PATCH v15 3/3] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel()
---
Changes since v14:
- Reorganized series, based on review comments by netdev-bot+sashiko in v14.
- Re-added "can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel()"
using a different approach.
Changes since v13:
- Dropped "can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel()" for now.
Although sashiko will mention that this problem is not addressed, this problem is too complicated
to handle within this series.
- Added "can: j1939: defer j1939_session_destroy() to WQ context", per findings by sashiko in v13.
Changes since v12:
- Added a cover letter and split the series into 6 logical patches (1 patch per problem).
- Deferred __j1939_priv_release() to WQ context to simplify "struct j1939_ecu" refcount leak fix
- Fixed findings by sashiko in v12.
net/can/j1939/address-claim.c | 4 +++
net/can/j1939/bus.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
net/can/j1939/j1939-priv.h | 11 ++++++++++
net/can/j1939/main.c | 48 ++++++++++++++++++++++++++++++++++++++++-------
net/can/j1939/transport.c | 23 +++++++++++++++++++---
5 files changed, 137 insertions(+), 30 deletions(-)
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v15 1/3] can: j1939: fix possibility of UAF 2026-11-17 15:56 [PATCH v15 0/3] can: j1939: make module more robust Tetsuo Handa @ 2026-11-17 15:56 ` Tetsuo Handa 2026-11-17 15:57 ` [PATCH v15 2/3] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa ` (2 subsequent siblings) 3 siblings, 0 replies; 8+ messages in thread From: Tetsuo Handa @ 2026-11-17 15:56 UTC (permalink / raw) To: linux-can, Marc Kleine-Budde, Oleksij Rempel, Robin van der Gracht, kernel, Oliver Hartkopp ## Problems # Problem 1 Since module refcount is currently associated with lifecycle of j1939 sockets, we need to make sure that all resources associated with socket ("struct j1939_priv", "struct j1939_ecu", "struct j1939_session") are synchronously destructed before socket is destructed. Otherwise, j1939_module_exit() can complete and memory for this module is unmapped and cause UAF. # Problem 2 This module takes a refcount upon arming an hrtimer and drops that refcount when the hrtimer is fired or canceled, but nothing guarantees that the callback is no longer running when the last refcount is dropped. We need to make sure that hrtimer callbacks are no longer running before calling kfree(). But we can't use hrtimer_cancel() from atomic context. # Problem 3 Since j1939_ac_recv() and j1939_ac_fixup() are calling j1939_ecu_put() without priv->lock held for write, list corruption might happen if the last user called j1939_ecu_put() without priv->lock held for write. But requiring all j1939_ecu_put() callers to hold priv->lock for write might be a difficult change. ## Approach Calling module_put(THIS_MODULE) from a module function whose completion cannot be waited for from that module's exit function is a source of UAF. But calling module_put(THIS_MODULE) from a WQ function whose completion can be waited for from that module's exit function using flush_workqueue() etc. is fine. Fix problem 1 by deferring release steps of "struct j1939_priv" to WQ context where we can call module_put(THIS_MODULE) while allowing j1939_module_exit() to wait for completion using destroy_workqueue(). Fix problem 2 by deferring timer cancellation and final freeing of "struct j1939_ecu" and "struct j1939_session" to WQ context where we can call hrtimer_cancel() before calling kfree(). Fix problem 3 by deferring release steps of "struct j1939_ecu" to WQ context where we can unconditionally hold priv->lock for write. By deferring to WQ context, readers might find ECUs which have already reached __j1939_ecu_release(). Therefore, allow using an ECU only when __j1939_ecu_release() is not yet called (indicated by ecu->kref > 0). Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol") Cc: stable@vger.kernel.org Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> --- net/can/j1939/bus.c | 66 +++++++++++++++++++++++++++----------- net/can/j1939/j1939-priv.h | 8 +++++ net/can/j1939/main.c | 47 +++++++++++++++++++++++---- net/can/j1939/transport.c | 23 +++++++++++-- 4 files changed, 116 insertions(+), 28 deletions(-) diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c index cdc3c0a71937..3924404152a2 100644 --- a/net/can/j1939/bus.c +++ b/net/can/j1939/bus.c @@ -14,25 +14,52 @@ #include "j1939-priv.h" -static void __j1939_ecu_release(struct kref *kref) +static void __j1939_ecu_release_work(struct work_struct *work) { - struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref); + struct j1939_ecu *ecu = container_of(work, struct j1939_ecu, release_work); struct j1939_priv *priv = ecu->priv; + write_lock_bh(&priv->lock); list_del(&ecu->list); + write_unlock_bh(&priv->lock); + /* There can't be any ref on this ECU because __j1939_ecu_release() + * was already called. But we need to make sure that + * j1939_ecu_timer_handler() is no longer running before kfree(). + */ + WARN_ON(hrtimer_cancel(&ecu->ac_timer)); netdev_put(priv->ndev, &ecu->priv_dev_tracker); kfree(ecu); j1939_priv_put(priv); } +static void __j1939_ecu_release(struct kref *kref) +{ + struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref); + + /* Since not all callers are calling j1939_ecu_put() with priv->lock + * held for write, defer actual release to WQ context. By deferring to + * WQ context, readers might find ECUs with ecu->kref == 0. Therefore, + * readers must validate with j1939_ecu_valid() or j1939_ecu_get(). + */ + INIT_WORK(&ecu->release_work, __j1939_ecu_release_work); + queue_work(j1939_wq, &ecu->release_work); +} + void j1939_ecu_put(struct j1939_ecu *ecu) { kref_put(&ecu->kref, __j1939_ecu_release); } -static void j1939_ecu_get(struct j1939_ecu *ecu) +static bool __must_check j1939_ecu_get(struct j1939_ecu *ecu) { - kref_get(&ecu->kref); + /* Don't get an ECU ref if __j1939_ecu_release() was called. */ + return kref_get_unless_zero(&ecu->kref); +} + +static bool __must_check j1939_ecu_valid(struct j1939_ecu *ecu) +{ + /* Treat an ECU as invalid if __j1939_ecu_release() was called. */ + return kref_read(&ecu->kref); } static bool j1939_ecu_is_mapped_locked(struct j1939_ecu *ecu) @@ -56,15 +83,17 @@ static void j1939_ecu_map_locked(struct j1939_ecu *ecu) if (!j1939_address_is_unicast(ecu->addr)) return; + if (!j1939_ecu_get(ecu)) + return; + ent = &priv->ents[ecu->addr]; if (ent->ecu) { netdev_warn(priv->ndev, "Trying to map already mapped ECU, addr: 0x%02x, name: 0x%016llx. Skip it.\n", ecu->addr, ecu->name); + j1939_ecu_put(ecu); return; } - - j1939_ecu_get(ecu); ent->ecu = ecu; ent->nusers += ecu->nusers; } @@ -109,10 +138,11 @@ void j1939_ecu_unmap_all(struct j1939_priv *priv) void j1939_ecu_timer_start(struct j1939_ecu *ecu) { - /* The ECU is held here and released in the + /* The corresponding j1939_ecu_put() is in * j1939_ecu_timer_handler() or j1939_ecu_timer_cancel(). */ - j1939_ecu_get(ecu); + if (!j1939_ecu_get(ecu)) + return; /* Schedule timer in 250 msec to commit address change. */ hrtimer_start(&ecu->ac_timer, ms_to_ktime(250), @@ -122,6 +152,7 @@ void j1939_ecu_timer_start(struct j1939_ecu *ecu) void j1939_ecu_timer_cancel(struct j1939_ecu *ecu) { if (hrtimer_cancel(&ecu->ac_timer)) + /* Drop the ref from j1939_ecu_timer_start(). */ j1939_ecu_put(ecu); } @@ -137,9 +168,7 @@ static enum hrtimer_restart j1939_ecu_timer_handler(struct hrtimer *hrtimer) */ j1939_ecu_map_locked(ecu); - /* The corresponding j1939_ecu_get() is in - * j1939_ecu_timer_start(). - */ + /* Drop the ref from j1939_ecu_timer_start(). */ j1939_ecu_put(ecu); write_unlock_bh(&priv->lock); @@ -189,8 +218,8 @@ struct j1939_ecu *j1939_ecu_get_by_addr_locked(struct j1939_priv *priv, u8 addr) return NULL; ecu = j1939_ecu_find_by_addr_locked(priv, addr); - if (ecu) - j1939_ecu_get(ecu); + if (ecu && !j1939_ecu_get(ecu)) + ecu = NULL; return ecu; } @@ -215,7 +244,7 @@ static struct j1939_ecu *j1939_ecu_find_by_name_locked(struct j1939_priv *priv, lockdep_assert_held(&priv->lock); list_for_each_entry(ecu, &priv->ecus, list) { - if (ecu->name == name) + if (ecu->name == name && j1939_ecu_valid(ecu)) return ecu; } @@ -232,11 +261,12 @@ struct j1939_ecu *j1939_ecu_get_by_name_locked(struct j1939_priv *priv, if (!name) return NULL; - ecu = j1939_ecu_find_by_name_locked(priv, name); - if (ecu) - j1939_ecu_get(ecu); + list_for_each_entry(ecu, &priv->ecus, list) { + if (ecu->name == name && j1939_ecu_get(ecu)) + return ecu; + } - return ecu; + return NULL; } struct j1939_ecu *j1939_ecu_get_by_name(struct j1939_priv *priv, name_t name) diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h index cf26352d1d8c..af179eb44b15 100644 --- a/net/can/j1939/j1939-priv.h +++ b/net/can/j1939/j1939-priv.h @@ -42,6 +42,8 @@ struct j1939_ecu { /* count users, to help transport protocol decide for interaction */ int nusers; + + struct work_struct release_work; }; struct j1939_priv { @@ -93,6 +95,8 @@ struct j1939_priv { struct kref rx_kref; u32 rx_tskey; + + struct work_struct release_work; }; void j1939_ecu_put(struct j1939_ecu *ecu); @@ -290,6 +294,8 @@ struct j1939_session { unsigned int dpo; } pkt; struct hrtimer txtimer, rxtimer; + + struct work_struct release_work; }; struct j1939_sock { @@ -345,4 +351,6 @@ void j1939_session_timers_cancel(struct j1939_session *session); /* CAN protocol */ extern const struct can_proto j1939_can_proto; +extern struct workqueue_struct *j1939_wq; + #endif /* _J1939_PRIV_H_ */ diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c index d9384cf0e356..b76037f89a80 100644 --- a/net/can/j1939/main.c +++ b/net/can/j1939/main.c @@ -20,6 +20,8 @@ #include "j1939-priv.h" +struct workqueue_struct *j1939_wq __ro_after_init; + MODULE_DESCRIPTION("PF_CAN SAE J1939"); MODULE_LICENSE("GPL v2"); MODULE_AUTHOR("EIA Electronics (Kurt Van Dijck & Pieter Beyens)"); @@ -132,6 +134,11 @@ static struct j1939_priv *j1939_priv_create(struct net_device *ndev) if (!priv) return NULL; + if (!try_module_get(THIS_MODULE)) { + kfree(priv); + return NULL; + } + rwlock_init(&priv->lock); INIT_LIST_HEAD(&priv->ecus); priv->ndev = ndev; @@ -152,9 +159,9 @@ static inline void j1939_priv_set(struct net_device *ndev, can_ml->j1939_priv = priv; } -static void __j1939_priv_release(struct kref *kref) +static void __j1939_priv_release_work(struct work_struct *work) { - struct j1939_priv *priv = container_of(kref, struct j1939_priv, kref); + struct j1939_priv *priv = container_of(work, struct j1939_priv, release_work); struct net_device *ndev = priv->ndev; netdev_dbg(priv->ndev, "%s: 0x%p\n", __func__, priv); @@ -165,6 +172,21 @@ static void __j1939_priv_release(struct kref *kref) netdev_put(ndev, &priv->dev_tracker); kfree(priv); + /* This is safe, for j1939_module_exit() waits for completion of + * this callback using destroy_workqueue(). + */ + module_put(THIS_MODULE); +} + +static void __j1939_priv_release(struct kref *kref) +{ + struct j1939_priv *priv = container_of(kref, struct j1939_priv, kref); + + /* Since synchronously dropping module refcount of self is not safe, + * defer actual release to WQ context. + */ + INIT_WORK(&priv->release_work, __j1939_priv_release_work); + queue_work(j1939_wq, &priv->release_work); } void j1939_priv_put(struct j1939_priv *priv) @@ -281,8 +303,7 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev) */ kref_get(&priv_new->rx_kref); mutex_unlock(&j1939_netdev_lock); - netdev_put(ndev, &priv->dev_tracker); - kfree(priv); + j1939_priv_put(priv); return priv_new; } j1939_priv_set(ndev, priv); @@ -297,9 +318,7 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev) out_priv_put: j1939_priv_set(ndev, NULL); mutex_unlock(&j1939_netdev_lock); - - netdev_put(ndev, &priv->dev_tracker); - kfree(priv); + j1939_priv_put(priv); return ERR_PTR(ret); } @@ -401,6 +420,12 @@ static __init int j1939_module_init(void) pr_info("can: SAE J1939\n"); + j1939_wq = alloc_workqueue("j1939_wq", WQ_HIGHPRI | WQ_PERCPU, 0); + if (!j1939_wq) { + ret = -ENOMEM; + goto fail_wq; + } + ret = register_netdevice_notifier(&j1939_netdev_notifier); if (ret) goto fail_notifier; @@ -416,6 +441,8 @@ static __init int j1939_module_init(void) fail_sk: unregister_netdevice_notifier(&j1939_netdev_notifier); fail_notifier: + destroy_workqueue(j1939_wq); + fail_wq: return ret; } @@ -424,6 +451,12 @@ static __exit void j1939_module_exit(void) can_proto_unregister(&j1939_can_proto); unregister_netdevice_notifier(&j1939_netdev_notifier); + + /* We need to make sure that __j1939_priv_release_work() and + * __j1939_ecu_release_work() and j1939_session_destroy_work() + * are no longer running before unmapping memory for this module. + */ + destroy_workqueue(j1939_wq); } module_init(j1939_module_init); diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c index 8fcfd13e5e6f..60cb6b602b09 100644 --- a/net/can/j1939/transport.c +++ b/net/can/j1939/transport.c @@ -259,6 +259,24 @@ static void __j1939_session_drop(struct j1939_session *session) sock_put(session->sk); } +static void j1939_session_destroy_work(struct work_struct *work) +{ + struct j1939_session *session = container_of(work, struct j1939_session, + release_work); + struct j1939_priv *priv = session->priv; + + /* There can't be any ref on this session because __j1939_session_release() + * was already called. But we need to make sure that j1939_tp_txtimer() and + * j1939_tp_rxtimer() are no longer running before kfree(). + */ + WARN_ON(hrtimer_cancel(&session->txtimer)); + WARN_ON(hrtimer_cancel(&session->rxtimer)); + + netdev_put(priv->ndev, &session->priv_dev_tracker); + kfree(session); + j1939_priv_put(priv); +} + static void j1939_session_destroy(struct j1939_session *session) { struct sk_buff *skb; @@ -283,9 +301,8 @@ static void j1939_session_destroy(struct j1939_session *session) kfree_skb(skb); } __j1939_session_drop(session); - netdev_put(session->priv->ndev, &session->priv_dev_tracker); - j1939_priv_put(session->priv); - kfree(session); + INIT_WORK(&session->release_work, j1939_session_destroy_work); + queue_work(j1939_wq, &session->release_work); } static void __j1939_session_release(struct kref *kref) -- 2.56.1 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v15 2/3] can: j1939: fix "struct j1939_ecu" refcount leak 2026-11-17 15:56 [PATCH v15 0/3] can: j1939: make module more robust Tetsuo Handa 2026-11-17 15:56 ` [PATCH v15 1/3] can: j1939: fix possibility of UAF Tetsuo Handa @ 2026-11-17 15:57 ` Tetsuo Handa 2026-11-27 11:12 ` Dmitry Antipov 2026-11-17 15:57 ` [PATCH v15 3/3] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel() Tetsuo Handa 2026-11-20 11:35 ` [PATCH v15 0/3] can: j1939: make module more robust Tetsuo Handa 3 siblings, 1 reply; 8+ messages in thread From: Tetsuo Handa @ 2026-11-17 15:57 UTC (permalink / raw) To: linux-can, Marc Kleine-Budde, Oleksij Rempel, Robin van der Gracht, kernel, Oliver Hartkopp syzbot is reporting "struct j1939_ecu" refcount leak, for j1939_ecu_get(ecu); priv->ents[ecu->addr] = ecu; in j1939_ecu_map_locked() from j1939_ecu_timer_handler() can succeed even after priv->ents[ecu->addr] = NULL; j1939_ecu_put(ecu); in j1939_ecu_unmap_locked() from j1939_ecu_unmap_all() from __j1939_rx_release() from j1939_netdev_stop() has completed. Fix this problem by (1) preventing ecu->ac_timer from re-arming in j1939_ecu_timer_start() (2) making j1939_ecu_map_locked() no-op when a netdev which is associated with j1939_priv is no longer registered (indicated by ecu->priv->ndev->reg_state != NETREG_REGISTERED) or __j1939_rx_release() was called (indicated by ecu->priv->rx_kref == 0). Reported-by: syzbot+e2af46126e0644cbebdd@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=e2af46126e0644cbebdd Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol") Cc: stable@vger.kernel.org Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> --- net/can/j1939/bus.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c index 3924404152a2..9b40f3754623 100644 --- a/net/can/j1939/bus.c +++ b/net/can/j1939/bus.c @@ -56,6 +56,17 @@ static bool __must_check j1939_ecu_get(struct j1939_ecu *ecu) return kref_get_unless_zero(&ecu->kref); } +static bool __must_check j1939_ecu_verify_get(struct j1939_ecu *ecu) +{ + struct j1939_priv *priv = ecu->priv; + + /* Don't get an ECU ref if netdev is no longer registered or + * __j1939_rx_release() was called or __j1939_ecu_release() was called. + */ + return READ_ONCE(priv->ndev->reg_state) == NETREG_REGISTERED && + kref_read(&priv->rx_kref) && j1939_ecu_get(ecu); +} + static bool __must_check j1939_ecu_valid(struct j1939_ecu *ecu) { /* Treat an ECU as invalid if __j1939_ecu_release() was called. */ @@ -83,7 +94,7 @@ static void j1939_ecu_map_locked(struct j1939_ecu *ecu) if (!j1939_address_is_unicast(ecu->addr)) return; - if (!j1939_ecu_get(ecu)) + if (!j1939_ecu_verify_get(ecu)) return; ent = &priv->ents[ecu->addr]; @@ -141,7 +152,7 @@ void j1939_ecu_timer_start(struct j1939_ecu *ecu) /* The corresponding j1939_ecu_put() is in * j1939_ecu_timer_handler() or j1939_ecu_timer_cancel(). */ - if (!j1939_ecu_get(ecu)) + if (!j1939_ecu_verify_get(ecu)) return; /* Schedule timer in 250 msec to commit address change. */ -- 2.56.1 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v15 2/3] can: j1939: fix "struct j1939_ecu" refcount leak 2026-11-17 15:57 ` [PATCH v15 2/3] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa @ 2026-11-27 11:12 ` Dmitry Antipov 0 siblings, 0 replies; 8+ messages in thread From: Dmitry Antipov @ 2026-11-27 11:12 UTC (permalink / raw) To: Tetsuo Handa, linux-can, Marc Kleine-Budde, Oleksij Rempel, Robin van der Gracht, kernel, Oliver Hartkopp On 11/17/26 6:57 PM, Tetsuo Handa wrote: > syzbot is reporting "struct j1939_ecu" refcount leak, for > > j1939_ecu_get(ecu); > priv->ents[ecu->addr] = ecu; > > in j1939_ecu_map_locked() from j1939_ecu_timer_handler() can succeed > even after > > priv->ents[ecu->addr] = NULL; > j1939_ecu_put(ecu); > > in j1939_ecu_unmap_locked() from j1939_ecu_unmap_all() from > __j1939_rx_release() from j1939_netdev_stop() has completed. > > Fix this problem by > > (1) preventing ecu->ac_timer from re-arming in j1939_ecu_timer_start() > (2) making j1939_ecu_map_locked() no-op > > when a netdev which is associated with j1939_priv is no longer registered > (indicated by ecu->priv->ndev->reg_state != NETREG_REGISTERED) or > __j1939_rx_release() was called (indicated by ecu->priv->rx_kref == 0). > > Reported-by: syzbot+e2af46126e0644cbebdd@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=e2af46126e0644cbebdd > Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol") > Cc: stable@vger.kernel.org > Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Tested-by: Dmitry Antipov <dmantipov@yandex.ru> Closes: https://syzkaller.appspot.com/bug?extid=489e907b2a026a6f5fa0 ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v15 3/3] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel() 2026-11-17 15:56 [PATCH v15 0/3] can: j1939: make module more robust Tetsuo Handa 2026-11-17 15:56 ` [PATCH v15 1/3] can: j1939: fix possibility of UAF Tetsuo Handa 2026-11-17 15:57 ` [PATCH v15 2/3] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa @ 2026-11-17 15:57 ` Tetsuo Handa 2026-11-20 11:35 ` [PATCH v15 0/3] can: j1939: make module more robust Tetsuo Handa 3 siblings, 0 replies; 8+ messages in thread From: Tetsuo Handa @ 2026-11-17 15:57 UTC (permalink / raw) To: linux-can, Marc Kleine-Budde, Oleksij Rempel, Robin van der Gracht, kernel, Oliver Hartkopp Since j1939_ecu_timer_cancel() is calling hrtimer_cancel() with priv->lock held for write while j1939_ecu_timer_handler() takes priv->lock, a hard- lockup (if CONFIG_PREEMPT_RT=n && CONFIG_SMP=y) or a deadlock between priv->lock and cpu_base->softirq_expiry_lock (if CONFIG_PREEMPT_RT=y) will happen when hrtimer_cancel() calls hrtimer_cancel_wait_running(). The comment for "struct j1939_priv"->lock says that priv->lock is used for protecting "struct list_head ecus" and "struct j1939_addr_ent ents[256]". But j1939_ac_process() is using priv->lock for also serializing timer management across multiple frames. This makes it impossible to temporarily drop priv->lock in order to safely call hrtimer_cancel(). Therefore, fix this problem by introducing ac_process_lock that works as an outer lock for temporarily dropping priv->lock in order to safely call hrtimer_cancel(). Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol") Cc: stable@vger.kernel.org Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> --- net/can/j1939/address-claim.c | 4 ++++ net/can/j1939/j1939-priv.h | 3 +++ net/can/j1939/main.c | 1 + 3 files changed, 8 insertions(+) diff --git a/net/can/j1939/address-claim.c b/net/can/j1939/address-claim.c index ca4ad6cdd5cb..594d2849c9f8 100644 --- a/net/can/j1939/address-claim.c +++ b/net/can/j1939/address-claim.c @@ -147,6 +147,7 @@ static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb) return; } + spin_lock_bh(&priv->ac_process_lock); write_lock_bh(&priv->lock); /* Few words on the ECU ref counting: @@ -212,7 +213,9 @@ static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb) goto out_unlock_bh; /* cancel pending (previous) address claim */ + write_unlock_bh(&priv->lock); j1939_ecu_timer_cancel(ecu); + write_lock_bh(&priv->lock); if (j1939_address_is_idle(skcb->addr.sa)) { j1939_ecu_unmap_locked(ecu); @@ -242,6 +245,7 @@ static void j1939_ac_process(struct j1939_priv *priv, struct sk_buff *skb) j1939_ecu_put(ecu); out_unlock_bh: write_unlock_bh(&priv->lock); + spin_unlock_bh(&priv->ac_process_lock); } void j1939_ac_recv(struct j1939_priv *priv, struct sk_buff *skb) diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h index af179eb44b15..28f6fa6029ec 100644 --- a/net/can/j1939/j1939-priv.h +++ b/net/can/j1939/j1939-priv.h @@ -62,6 +62,9 @@ struct j1939_priv { /* segments need a lock to protect the above list */ rwlock_t lock; + /* serializes j1939_ac_process() processing */ + spinlock_t ac_process_lock; + struct net_device *ndev; netdevice_tracker dev_tracker; diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c index b76037f89a80..4bae9402c4c0 100644 --- a/net/can/j1939/main.c +++ b/net/can/j1939/main.c @@ -140,6 +140,7 @@ static struct j1939_priv *j1939_priv_create(struct net_device *ndev) } rwlock_init(&priv->lock); + spin_lock_init(&priv->ac_process_lock); INIT_LIST_HEAD(&priv->ecus); priv->ndev = ndev; kref_init(&priv->kref); -- 2.56.1 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v15 0/3] can: j1939: make module more robust 2026-11-17 15:56 [PATCH v15 0/3] can: j1939: make module more robust Tetsuo Handa ` (2 preceding siblings ...) 2026-11-17 15:57 ` [PATCH v15 3/3] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel() Tetsuo Handa @ 2026-11-20 11:35 ` Tetsuo Handa 2026-11-27 8:32 ` Oleksij Rempel 3 siblings, 1 reply; 8+ messages in thread From: Tetsuo Handa @ 2026-11-20 11:35 UTC (permalink / raw) To: linux-can, Marc Kleine-Budde, Oleksij Rempel, Robin van der Gracht, kernel, Oliver Hartkopp Hello, j1939 maintainers. On 2026/11/18 0:56, Tetsuo Handa wrote: > This series is intended to address the following problems. Both sashiko-bot@kernel.org and netdev-bot+sashiko@kernel.org are almost pleased with this v15 patchset. https://sashiko.dev/#/patchset/98c16a74-5ea7-476e-ab36-c98fb77b6be4%40I-love.SAKURA.ne.jp https://netdev-ai.bots.linux.dev/sashiko/#/patchset/98c16a74-5ea7-476e-ab36-c98fb77b6be4@I-love.SAKURA.ne.jp Although netdev-bot+sashiko suggested appending below diff to [PATCH v15 3/3], it is not worth reposting as v16 without waiting for your response. diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h index 28f6fa6029ec..f33f40a4c23d 100644 --- a/net/can/j1939/j1939-priv.h +++ b/net/can/j1939/j1939-priv.h @@ -69,7 +69,7 @@ struct j1939_priv { netdevice_tracker dev_tracker; /* list of 256 ecu ptrs, that cache the claimed addresses. - * also protected by the above lock + * also protected by the "rwlock_t lock" above */ struct j1939_addr_ent { struct j1939_ecu *ecu; Regarding the deadlock problem in transport.c , I need some trial-and-error for making a workable patch. Therefore, I want to avoid needlessly re-posting the whole patchset. Will you apply at least [PATCH v15 1/3] and [PATCH v15 2/3] now? (If you don't mind lack of above diff, [PATCH v15 3/3] together please.) Regards. ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v15 0/3] can: j1939: make module more robust 2026-11-20 11:35 ` [PATCH v15 0/3] can: j1939: make module more robust Tetsuo Handa @ 2026-11-27 8:32 ` Oleksij Rempel 2026-11-27 12:01 ` Tetsuo Handa 0 siblings, 1 reply; 8+ messages in thread From: Oleksij Rempel @ 2026-11-27 8:32 UTC (permalink / raw) To: Tetsuo Handa Cc: linux-can, Marc Kleine-Budde, Robin van der Gracht, kernel, Oliver Hartkopp Hi Tetsuo, Thank you for your work! On Fri, Nov 20, 2026 at 08:35:22PM +0900, Tetsuo Handa wrote: > Hello, j1939 maintainers. > > On 2026/11/18 0:56, Tetsuo Handa wrote: > > This series is intended to address the following problems. > > Both sashiko-bot@kernel.org and netdev-bot+sashiko@kernel.org are almost > pleased with this v15 patchset. > > https://sashiko.dev/#/patchset/98c16a74-5ea7-476e-ab36-c98fb77b6be4%40I-love.SAKURA.ne.jp > https://netdev-ai.bots.linux.dev/sashiko/#/patchset/98c16a74-5ea7-476e-ab36-c98fb77b6be4@I-love.SAKURA.ne.jp > > Although netdev-bot+sashiko suggested appending below diff to [PATCH v15 3/3], > it is not worth reposting as v16 without waiting for your response. > > diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h > index 28f6fa6029ec..f33f40a4c23d 100644 > --- a/net/can/j1939/j1939-priv.h > +++ b/net/can/j1939/j1939-priv.h > @@ -69,7 +69,7 @@ struct j1939_priv { > netdevice_tracker dev_tracker; > > /* list of 256 ecu ptrs, that cache the claimed addresses. > - * also protected by the above lock > + * also protected by the "rwlock_t lock" above > */ > struct j1939_addr_ent { > struct j1939_ecu *ecu; > > Regarding the deadlock problem in transport.c , I need some trial-and-error > for making a workable patch. Therefore, I want to avoid needlessly re-posting > the whole patchset. Agreed. A separate patch for the j1939_session_cancel() deadlock is the right way. You can keep working on it, but please do not post it until I have answered on this series. > Will you apply at least [PATCH v15 1/3] and [PATCH v15 2/3] now? > (If you don't mind lack of above diff, [PATCH v15 3/3] together please.) Sorry for the slow answer. The patches are not ready to be applied yet. The bots found no real bug in v15. They only found the old j1939_session_cancel() deadlock and one small comment issue in 3/3, which your diff already fixes. So for v15 nothing is left from the bots. What is left is my own review: I am going through the patches in detail now, and I need a little time for it. Please do not send a new version of this series until I have answered. About the delay: my mailbox is full of Sashiko reviews and new versions of other patch sets, from many developers. I do not have the capacity to follow all of it, and I miss things. If you think I am stuck on your series, please ping me on the list, with a subject like "ping for Oleksij". Then the other maintainers can see it too, in case I am not available. Some general background on why the bar is high. As you have seen with this series and with your earlier patches, Sashiko and netdev Sashiko review CAN patches twice. The first time is when the author posts them. When a patch has no open findings, Marc collects it and sends it to the net or net-next tree in a pull request. That pull request goes through all the checks again, and also through NIPA. If one patch gets a valid finding there, the whole pull request is sent back, and Marc has to send it again without that patch. We ask for your understanding: a patch can only go in when every finding is either fixed or answered on the list, for example by showing that it is pre-existing. Also, the number of patches has grown a lot, mostly because of advanced tooling. This tooling helps to find bugs, but it does not yet help as much to fix them or to write good patches. Most reviewers now look at a patch only after it passes the checks. With this many patches, they could not do anything else. In general, small patches and small series that each fix only one problem work best at the moment. They are easier to review, and they can be applied one by one. v15 1/3 is not like that yet. It fixes three problems in one patch. Regards, Oleksij ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v15 0/3] can: j1939: make module more robust 2026-11-27 8:32 ` Oleksij Rempel @ 2026-11-27 12:01 ` Tetsuo Handa 0 siblings, 0 replies; 8+ messages in thread From: Tetsuo Handa @ 2026-11-27 12:01 UTC (permalink / raw) To: Oleksij Rempel Cc: linux-can, Marc Kleine-Budde, Robin van der Gracht, kernel, Oliver Hartkopp On 2026/11/27 17:32, Oleksij Rempel wrote: > About the delay: my mailbox is full of Sashiko reviews and new versions > of other patch sets, from many developers. Yes, that's why I don't want to try sending to relevant MLs before sashiko-bot@kernel.org gets pleased. I was using LKML with dot versioning. I appreciate if there is a ML which sashiko-bot@kernel.org reviews but automated tooling (e.g. build bot) do not run. ;-) ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-27 12:01 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-17 15:56 [PATCH v15 0/3] can: j1939: make module more robust Tetsuo Handa 2026-11-17 15:56 ` [PATCH v15 1/3] can: j1939: fix possibility of UAF Tetsuo Handa 2026-11-17 15:57 ` [PATCH v15 2/3] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa 2026-11-27 11:12 ` Dmitry Antipov 2026-11-17 15:57 ` [PATCH v15 3/3] can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel() Tetsuo Handa 2026-11-20 11:35 ` [PATCH v15 0/3] can: j1939: make module more robust Tetsuo Handa 2026-11-27 8:32 ` Oleksij Rempel 2026-11-27 12:01 ` Tetsuo Handa
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox