* [PATCH v16 0/5] can: j1939: make module more robust
@ 2026-12-04 16:03 Tetsuo Handa
2026-12-04 16:03 ` [PATCH v16 1/5] can: j1939: fix possibility of UAF on j1939_priv upon module unload Tetsuo Handa
` (4 more replies)
0 siblings, 5 replies; 10+ messages in thread
From: Tetsuo Handa @ 2026-12-04 16:03 UTC (permalink / raw)
To: linux-can
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_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 4
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.
[PATCH v16 1/5] can: j1939: fix possibility of UAF on j1939_priv upon module unload
[PATCH v16 2/5] can: j1939: fix possibility of UAF on j1939_session upon module unload
[PATCH v16 3/5] can: j1939: fix possibility of UAF on j1939_ecu upon module unload
[PATCH v16 4/5] can: j1939: fix "struct j1939_ecu" refcount leak
[PATCH v16 5/5] can: j1939: fix possibility of list corruption in __j1939_ecu_release()
---
Changes since v15:
- Break "can: j1939: fix possibility of UAF" into 4 smaller patches.
- Updated "can: j1939: fix possibility of UAF" to use plain counter instead of module ref.
- Updated "can: j1939: fix "struct j1939_ecu" refcount leak" not to depend on
"can: j1939: fix possibility of list corruption in __j1939_ecu_release()".
- Dropped "can: j1939: fix possibility of hard-lockup/deadlock in j1939_ecu_timer_cancel()"
because 32ef7b4de77b ("can: j1939: avoid address-claim timer deadlock") was accepted.
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/bus.c | 60 ++++++++++++++++++++++++++++++++++++++++++++----------------
net/can/j1939/j1939-priv.h | 8 ++++++++
net/can/j1939/main.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++-------
net/can/j1939/transport.c | 23 ++++++++++++++++++++---
4 files changed, 118 insertions(+), 26 deletions(-)
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v16 1/5] can: j1939: fix possibility of UAF on j1939_priv upon module unload
2026-12-04 16:03 [PATCH v16 0/5] can: j1939: make module more robust Tetsuo Handa
@ 2026-12-04 16:03 ` Tetsuo Handa
2026-12-04 16:34 ` sashiko-bot
2026-12-04 16:04 ` [PATCH v16 2/5] can: j1939: fix possibility of UAF on j1939_session " Tetsuo Handa
` (3 subsequent siblings)
4 siblings, 1 reply; 10+ messages in thread
From: Tetsuo Handa @ 2026-12-04 16:03 UTC (permalink / raw)
To: linux-can
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.
Fix this problem by making the following changes.
(1) introduce a counter which tracks "struct j1939_priv" instances and
works like __module_get(THIS_MODULE) and module_put(THIS_MODULE) but
works in a way that prevents "rmmod can_j1939" from returning EBUSY
(2) defer release steps of "struct j1939_priv" to WQ context using
a module-local WQ
(3) introduce a wait queue that waits at the beginning of
j1939_module_exit() until the counter for (1) drops to 0
(this will hang up when any j1939_priv reference leaked)
(4) wait for completion of (2) from j1939_module_exit() using
destroy_workqueue()
This patch calls alloc_workqueue() with WQ_HIGHPRI flag when creating
j1939_wq, for we don't want to delay memory reclaim and completion of (3)
for too long. Subsequent patches will defer release steps of "struct
j1939_ecu" and "struct j1939_session" to j1939_wq context, for (4) is
the gatekeeper for avoiding UAF upon "rmmod can_j1939".
This patch calls alloc_workqueue() with WQ_PERCPU flag when creating
j1939_wq due to commit 21c05ca88a54 ("workqueue: Add warnings and ensure
one among WQ_PERCPU or WQ_UNBOUND is present"). You can drop WQ_PERCPU flag
when backporting to older kernels.
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/j1939-priv.h | 4 +++
net/can/j1939/main.c | 53 +++++++++++++++++++++++++++++++++-----
2 files changed, 50 insertions(+), 7 deletions(-)
diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
index 5ed4f4b592e3..68431534c57a 100644
--- a/net/can/j1939/j1939-priv.h
+++ b/net/can/j1939/j1939-priv.h
@@ -94,6 +94,8 @@ struct j1939_priv {
struct kref rx_kref;
u32 rx_tskey;
+
+ struct work_struct release_work;
};
void j1939_ecu_put(struct j1939_ecu *ecu);
@@ -346,4 +348,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..858f9fa1a02f 100644
--- a/net/can/j1939/main.c
+++ b/net/can/j1939/main.c
@@ -20,6 +20,12 @@
#include "j1939-priv.h"
+struct workqueue_struct *j1939_wq __ro_after_init;
+#if defined(MODULE) && defined(CONFIG_MODULE_UNLOAD)
+static atomic_t j1939_unload_blocker_count;
+static DECLARE_WAIT_QUEUE_HEAD(j1939_unload_wait);
+#endif
+
MODULE_DESCRIPTION("PF_CAN SAE J1939");
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("EIA Electronics (Kurt Van Dijck & Pieter Beyens)");
@@ -132,6 +138,10 @@ static struct j1939_priv *j1939_priv_create(struct net_device *ndev)
if (!priv)
return NULL;
+#if defined(MODULE) && defined(CONFIG_MODULE_UNLOAD)
+ atomic_inc(&j1939_unload_blocker_count);
+#endif
+
rwlock_init(&priv->lock);
INIT_LIST_HEAD(&priv->ecus);
priv->ndev = ndev;
@@ -152,9 +162,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 +175,18 @@ static void __j1939_priv_release(struct kref *kref)
netdev_put(ndev, &priv->dev_tracker);
kfree(priv);
+#if defined(MODULE) && defined(CONFIG_MODULE_UNLOAD)
+ if (atomic_dec_and_test(&j1939_unload_blocker_count))
+ wake_up(&j1939_unload_wait);
+#endif
+}
+
+static void __j1939_priv_release(struct kref *kref)
+{
+ struct j1939_priv *priv = container_of(kref, struct j1939_priv, kref);
+
+ 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,14 +441,28 @@ 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;
}
static __exit void j1939_module_exit(void)
{
+#if defined(MODULE) && defined(CONFIG_MODULE_UNLOAD)
+ /* Wait until all "struct j1939_priv", "struct j1939_ecu" and
+ * "struct j1939_session" are released.
+ */
+ wait_event(j1939_unload_wait, !atomic_read(&j1939_unload_blocker_count));
+#endif
+
can_proto_unregister(&j1939_can_proto);
unregister_netdevice_notifier(&j1939_netdev_notifier);
+
+ /* Wait until __j1939_priv_release_work() completes
+ * before unmapping memory for this module.
+ */
+ destroy_workqueue(j1939_wq);
}
module_init(j1939_module_init);
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v16 2/5] can: j1939: fix possibility of UAF on j1939_session upon module unload
2026-12-04 16:03 [PATCH v16 0/5] can: j1939: make module more robust Tetsuo Handa
2026-12-04 16:03 ` [PATCH v16 1/5] can: j1939: fix possibility of UAF on j1939_priv upon module unload Tetsuo Handa
@ 2026-12-04 16:04 ` Tetsuo Handa
2026-12-04 16:29 ` sashiko-bot
2026-12-04 16:04 ` [PATCH v16 3/5] can: j1939: fix possibility of UAF on j1939_ecu " Tetsuo Handa
` (2 subsequent siblings)
4 siblings, 1 reply; 10+ messages in thread
From: Tetsuo Handa @ 2026-12-04 16:04 UTC (permalink / raw)
To: linux-can
There is a possibility of UAF that j1939_priv_put() from
j1939_session_destroy() allows j1939_module_exit() to complete before
j1939_session_destroy() completes, for destroy_workqueue() from
j1939_module_exit() does not wait for completion of j1939_session_destroy()
after j1939_unload_blocker_count reached 0.
Fix this problem by deferring release steps of "struct j1939_session"
to WQ context. As a side effect of this change, we obtain a chance to call
hrtimer_cancel() before kfree(), which is important for avoiding UAF on
an hrtimer object.
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/j1939-priv.h | 2 ++
net/can/j1939/transport.c | 23 ++++++++++++++++++++---
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
index 68431534c57a..a770c82820c6 100644
--- a/net/can/j1939/j1939-priv.h
+++ b/net/can/j1939/j1939-priv.h
@@ -293,6 +293,8 @@ struct j1939_session {
unsigned int dpo;
} pkt;
struct hrtimer txtimer, rxtimer;
+
+ struct work_struct release_work;
};
struct j1939_sock {
diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
index fa30273df326..4345921ede07 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] 10+ messages in thread
* [PATCH v16 3/5] can: j1939: fix possibility of UAF on j1939_ecu upon module unload
2026-12-04 16:03 [PATCH v16 0/5] can: j1939: make module more robust Tetsuo Handa
2026-12-04 16:03 ` [PATCH v16 1/5] can: j1939: fix possibility of UAF on j1939_priv upon module unload Tetsuo Handa
2026-12-04 16:04 ` [PATCH v16 2/5] can: j1939: fix possibility of UAF on j1939_session " Tetsuo Handa
@ 2026-12-04 16:04 ` Tetsuo Handa
2026-12-04 16:04 ` [PATCH v16 4/5] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
2026-12-04 16:05 ` [PATCH v16 5/5] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
4 siblings, 0 replies; 10+ messages in thread
From: Tetsuo Handa @ 2026-12-04 16:04 UTC (permalink / raw)
To: linux-can
There is a possibility of UAF that j1939_priv_put() from
__j1939_ecu_release() allows j1939_module_exit() to complete before
__j1939_ecu_release() completes, for destroy_workqueue() from
j1939_module_exit() does not wait for completion of __j1939_ecu_release()
after j1939_unload_blocker_count reached 0.
Fix this problem by deferring release steps of "struct j1939_ecu" to WQ
context. As a side effect of this change, we obtain a chance to call
hrtimer_cancel() before kfree(), which is important for avoiding UAF on
an hrtimer object.
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 | 19 ++++++++++++++++---
net/can/j1939/j1939-priv.h | 2 ++
2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
index eb9a3fd847f4..7e22a7f7ecad 100644
--- a/net/can/j1939/bus.c
+++ b/net/can/j1939/bus.c
@@ -23,17 +23,30 @@
*/
#define J1939_ECU_TIMER_RETRY_WARN 5
-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;
- list_del(&ecu->list);
+ /* 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);
+
+ list_del(&ecu->list);
+ 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);
diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
index a770c82820c6..c925443d2a08 100644
--- a/net/can/j1939/j1939-priv.h
+++ b/net/can/j1939/j1939-priv.h
@@ -43,6 +43,8 @@ struct j1939_ecu {
/* count users, to help transport protocol decide for interaction */
int nusers;
+
+ struct work_struct release_work;
};
struct j1939_priv {
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v16 4/5] can: j1939: fix "struct j1939_ecu" refcount leak
2026-12-04 16:03 [PATCH v16 0/5] can: j1939: make module more robust Tetsuo Handa
` (2 preceding siblings ...)
2026-12-04 16:04 ` [PATCH v16 3/5] can: j1939: fix possibility of UAF on j1939_ecu " Tetsuo Handa
@ 2026-12-04 16:04 ` Tetsuo Handa
2026-12-04 16:23 ` sashiko-bot
2026-12-04 16:05 ` [PATCH v16 5/5] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
4 siblings, 1 reply; 10+ messages in thread
From: Tetsuo Handa @ 2026-12-04 16:04 UTC (permalink / raw)
To: linux-can
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 making j1939_ecu_map_locked() no-op
when __j1939_rx_release() was already 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 | 2 ++
1 file changed, 2 insertions(+)
diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
index 7e22a7f7ecad..b8e517c6ad1c 100644
--- a/net/can/j1939/bus.c
+++ b/net/can/j1939/bus.c
@@ -74,6 +74,8 @@ static void j1939_ecu_map_locked(struct j1939_ecu *ecu)
struct j1939_addr_ent *ent;
lockdep_assert_held(&priv->lock);
+ if (!kref_read(&priv->rx_kref))
+ return;
if (!j1939_address_is_unicast(ecu->addr))
return;
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v16 5/5] can: j1939: fix possibility of list corruption in __j1939_ecu_release()
2026-12-04 16:03 [PATCH v16 0/5] can: j1939: make module more robust Tetsuo Handa
` (3 preceding siblings ...)
2026-12-04 16:04 ` [PATCH v16 4/5] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
@ 2026-12-04 16:05 ` Tetsuo Handa
2026-12-04 16:23 ` sashiko-bot
4 siblings, 1 reply; 10+ messages in thread
From: Tetsuo Handa @ 2026-12-04 16:05 UTC (permalink / raw)
To: linux-can
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.
Fix this problem by deferring list_del() 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 | 39 ++++++++++++++++++++++++++-------------
1 file changed, 26 insertions(+), 13 deletions(-)
diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
index b8e517c6ad1c..a9dc565e34c2 100644
--- a/net/can/j1939/bus.c
+++ b/net/can/j1939/bus.c
@@ -28,6 +28,9 @@ static void __j1939_ecu_release_work(struct work_struct *work)
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().
@@ -42,7 +45,6 @@ static void __j1939_ecu_release(struct kref *kref)
{
struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
- list_del(&ecu->list);
INIT_WORK(&ecu->release_work, __j1939_ecu_release_work);
queue_work(j1939_wq, &ecu->release_work);
}
@@ -52,9 +54,16 @@ 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)
@@ -80,15 +89,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;
}
@@ -138,7 +149,8 @@ void j1939_ecu_timer_start(struct j1939_ecu *ecu)
/* The ECU is held here and released in the
* 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),
@@ -234,8 +246,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;
}
@@ -260,7 +272,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;
}
@@ -277,11 +289,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)
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v16 4/5] can: j1939: fix "struct j1939_ecu" refcount leak
2026-12-04 16:04 ` [PATCH v16 4/5] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
@ 2026-12-04 16:23 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-12-04 16:23 UTC (permalink / raw)
To: Tetsuo Handa, linux-can
Cc: Marc Kleine-Budde, Oliver Hartkopp, Vincent Mailhol,
Oleksij Rempel
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] can: j1939: undocumented stable dependency in
j1939_ecu_map_locked() fix
- [Low] can: j1939: commit message omits testing description required by
netdev policy
--
commit 37da636ba3209e2bc000b307361a6b7796d12a7f
Author: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
> can: j1939: fix "struct j1939_ecu" refcount leak
>
> 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 making j1939_ecu_map_locked() no-op
> when __j1939_rx_release() was already 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>
[Severity: Low]
As reported by Oleksij Rempel on v15 [1], the commit message still omits how
the fix was tested. Documentation/process/maintainer-netdev.rst requires
every bug fix to state how it was tested.
> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index 7e22a7f7ecad5..b8e517c6ad1ce 100644
> --- a/net/can/j1939/bus.c
> +++ b/net/can/j1939/bus.c
> @@ -74,6 +74,8 @@ static void j1939_ecu_map_locked(struct j1939_ecu *ecu)
> struct j1939_addr_ent *ent;
>
> lockdep_assert_held(&priv->lock);
> + if (!kref_read(&priv->rx_kref))
> + return;
>
> if (!j1939_address_is_unicast(ecu->addr))
> return;
[Severity: Medium]
As reported by Oleksij Rempel on v15 [1], can this patch cause a
use-after-free in j1939_ecu_timer_handler() if backported to stable without
earlier commits from this series?
This two-line diff carries Cc: stable@vger.kernel.org and applies cleanly on
older trees. Without commit "can: j1939: fix possibility of UAF on
j1939_ecu upon module unload", skipping the map lets j1939_ecu_put(ecu) call
kfree(priv) before write_unlock(&priv->lock), causing a use-after-free.
Preceding this fix with kfree_rcu() for j1939_priv (as suggested on v15)
would keep it safe for standalone backporting; otherwise the commit message
should note the dependency on the earlier workqueue release commits.
[1] https://lore.kernel.org/r/awv_tkJZbviq3RBT@pengutronix.de
--
Sashiko AI review · https://sashiko.dev/#/patchset/02d9f333-d774-4cec-9d12-cb181395fd8a@I-love.SAKURA.ne.jp?part=4
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v16 5/5] can: j1939: fix possibility of list corruption in __j1939_ecu_release()
2026-12-04 16:05 ` [PATCH v16 5/5] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
@ 2026-12-04 16:23 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-12-04 16:23 UTC (permalink / raw)
To: Tetsuo Handa, linux-can
Cc: Marc Kleine-Budde, Oliver Hartkopp, Vincent Mailhol,
Oleksij Rempel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] can: j1939: O(N^2) zombie scan and lock fan-out from deferred
ECU list_del()
--
commit 75cc65689af4612193f82d163f25817aea7da880
Author: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
can: j1939: fix possibility of list corruption in __j1939_ecu_release()
This commit defers list_del(&ecu->list) from __j1939_ecu_release() to
__j1939_ecu_release_work() under write_lock_bh(&priv->lock), and updates
j1939_ecu lookup and reference-taking helpers to skip zero-refcount entries.
> diff --git a/net/can/j1939/bus.c b/net/can/j1939/bus.c
> index b8e517c6ad1ce..a9dc565e34c22 100644
> --- a/net/can/j1939/bus.c
> +++ b/net/can/j1939/bus.c
> @@ -28,6 +28,9 @@ static void __j1939_ecu_release_work(struct work_struct *work)
> 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().
> @@ -42,7 +45,6 @@ static void __j1939_ecu_release(struct kref *kref)
> {
> struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
>
> - list_del(&ecu->list);
> INIT_WORK(&ecu->release_work, __j1939_ecu_release_work);
> queue_work(j1939_wq, &ecu->release_work);
> }
[Severity: Medium]
Can deferring list_del(&ecu->list) to j1939_wq cause O(n^2) scans over
priv->ecus under priv->lock?
j1939_ecu_create_locked() appends new entries with list_add_tail(), so
zero-refcount entries accumulate at the head of priv->ecus ahead of live
ones whenever create/release churn outpaces the workqueue.
During an address-claim burst in softirq, j1939_ac_process() alternating
unicast and idle 0xfe claims preempts j1939_wq. Lookup paths such as
j1939_ecu_get_by_name_locked() and j1939_ecu_find_by_name_locked() then
scan past all dead entries under priv->lock, starving the cleanup worker.
Separately, j1939_ecu_unmap_all() previously unlinked up to 254 mapped
entries inside one write_lock_bh(&priv->lock) critical section, but now
schedules up to 254 work items that each re-acquire write_lock_bh(),
increasing contention with j1939_can_recv() readers.
--
Sashiko AI review · https://sashiko.dev/#/patchset/02d9f333-d774-4cec-9d12-cb181395fd8a@I-love.SAKURA.ne.jp?part=5
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v16 2/5] can: j1939: fix possibility of UAF on j1939_session upon module unload
2026-12-04 16:04 ` [PATCH v16 2/5] can: j1939: fix possibility of UAF on j1939_session " Tetsuo Handa
@ 2026-12-04 16:29 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-12-04 16:29 UTC (permalink / raw)
To: Tetsuo Handa, linux-can
Cc: Marc Kleine-Budde, Oliver Hartkopp, Vincent Mailhol,
Oleksij Rempel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] can: j1939: unconditional WQ_HIGHPRI offload in
j1939_session_destroy()
--
commit ff8109ff98ecd01431b08fa480c06d1f155822f3
Author: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
can: j1939: fix possibility of UAF on j1939_session upon module unload
This commit defers the final teardown of struct j1939_session (hrtimer
cancellation, netdev_put(), kfree(), and j1939_priv_put()) to j1939_wq so
that module unload waits for session destruction to complete and neither
hrtimer callback is still running before kfree().
> diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c
> index fa30273df3263..4345921ede073 100644
> --- a/net/can/j1939/transport.c
> +++ b/net/can/j1939/transport.c
[ ... ]
> @@ -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);
> }
[Severity: Medium]
Does unconditionally queuing j1939_session_destroy_work() onto j1939_wq
introduce a per-frame scheduling regression on the transmit path?
Every j1939_sk_sendmsg() allocates a struct j1939_session via
j1939_sk_send_loop() -> j1939_tp_send(), including single-frame
J1939_SIMPLE messages. On loopback completion, j1939_can_recv() ->
j1939_simple_recv() cancels the timers and calls j1939_session_put().
Because j1939_wq is allocated with WQ_HIGHPRI | WQ_PERCPU, every
transmitted frame now wakes a nice -20 kworker on the local CPU, which
can preempt normal tasks, adds a context switch, and defers kfree() out
of softirq.
In the common path, both timers are already inactive and callers like
j1939_can_recv() still hold a priv reference. Could this check
!hrtimer_active() on both timers and refcount_dec_not_one() on
priv->kref.refcount to free inline, queueing work only otherwise?
--
Sashiko AI review · https://sashiko.dev/#/patchset/02d9f333-d774-4cec-9d12-cb181395fd8a@I-love.SAKURA.ne.jp?part=2
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v16 1/5] can: j1939: fix possibility of UAF on j1939_priv upon module unload
2026-12-04 16:03 ` [PATCH v16 1/5] can: j1939: fix possibility of UAF on j1939_priv upon module unload Tetsuo Handa
@ 2026-12-04 16:34 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-12-04 16:34 UTC (permalink / raw)
To: Tetsuo Handa, linux-can
Cc: Marc Kleine-Budde, Oliver Hartkopp, Vincent Mailhol,
Oleksij Rempel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] can: j1939: unbounded wait_event() in j1939_module_exit() hangs
rmmod
--
commit acd386211cb084e36e0fd335311a953078fa035b
Author: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
can: j1939: fix possibility of UAF on j1939_priv upon module unload
This commit tracks active struct j1939_priv instances with an atomic
counter, defers __j1939_priv_release() to a module-local workqueue, and
waits in j1939_module_exit() for all instances and release work to
finish before unloading.
> diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
> index d9384cf0e356f..858f9fa1a02ff 100644
> --- a/net/can/j1939/main.c
> +++ b/net/can/j1939/main.c
[ ... ]
> @@ -416,14 +441,28 @@ 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;
> }
>
> static __exit void j1939_module_exit(void)
> {
> +#if defined(MODULE) && defined(CONFIG_MODULE_UNLOAD)
> + /* Wait until all "struct j1939_priv", "struct j1939_ecu" and
> + * "struct j1939_session" are released.
> + */
> + wait_event(j1939_unload_wait, !atomic_read(&j1939_unload_blocker_count));
> +#endif
[Severity: High]
Can this unbounded wait_event() permanently hang rmmod and wedge the
module?
delete_module() transitions the module to MODULE_STATE_GOING before
calling j1939_module_exit(). If a struct j1939_priv reference leaks,
rmmod can_j1939 hangs forever in uninterruptible sleep and later module
loads fail with -EBUSY until reboot.
Such a leak is reachable via j1939_xtp_rx_dat_one(), which calls
j1939_tp_schedule_txtimer(session, 0) without canceling a pending
txtimer. Two back-to-back block-boundary frames before the timer fires
take two j1939_session_get() refs while j1939_tp_txtimer() drops one.
That leaks session->kref and its reference on priv->kref, keeping
j1939_unload_blocker_count non-zero. Even without a leak,
__j1939_rx_release() leaves active RX sessions running when the last
socket detaches, stalling rmmod until they time out and send aborts.
Pinning the module with try_module_get()/module_put() so rmmod returns
-EBUSY while j1939_priv instances exist, or synchronously canceling
active sessions and timers in __j1939_rx_release(), would avoid blocking
indefinitely in module_exit().
--
Sashiko AI review · https://sashiko.dev/#/patchset/02d9f333-d774-4cec-9d12-cb181395fd8a@I-love.SAKURA.ne.jp?part=1
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-12-04 16:34 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 16:03 [PATCH v16 0/5] can: j1939: make module more robust Tetsuo Handa
2026-12-04 16:03 ` [PATCH v16 1/5] can: j1939: fix possibility of UAF on j1939_priv upon module unload Tetsuo Handa
2026-12-04 16:34 ` sashiko-bot
2026-12-04 16:04 ` [PATCH v16 2/5] can: j1939: fix possibility of UAF on j1939_session " Tetsuo Handa
2026-12-04 16:29 ` sashiko-bot
2026-12-04 16:04 ` [PATCH v16 3/5] can: j1939: fix possibility of UAF on j1939_ecu " Tetsuo Handa
2026-12-04 16:04 ` [PATCH v16 4/5] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
2026-12-04 16:23 ` sashiko-bot
2026-12-04 16:05 ` [PATCH v16 5/5] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
2026-12-04 16:23 ` sashiko-bot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.