Linux CAN drivers development
 help / color / mirror / Atom feed
* [PATCH v14 0/6] can: j1939: make module more robust
@ 2026-11-15 11:35 Tetsuo Handa
  2026-11-15 11:35 ` [PATCH v14 1/6] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: Tetsuo Handa @ 2026-11-15 11:35 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: Possible UAF on module unload.

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
before j1939_ecu_timer_handler() or __j1939_ecu_release() or
j1939_session_destroy() or __j1939_priv_release() completes (i.e.
causing UAF).

Calling module_put(THIS_MODULE) from a module function which is not
possible to wait for completion of that function from that module's exit
function is a source of UAF. But calling module_put(THIS_MODULE) from a WQ
function which is possible to wait for completion 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().

By this change, we no longer 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, simplifying further fixes.

## Problem 2: Possible UAF in __j1939_priv_release().

If a user calls j1939_ecu_put(ecu) with priv->lock held but without knowing
that this "struct j1939_ecu" is the last user of a "struct j1939_priv",
__j1939_priv_release_work() via __j1939_priv_release() from
j1939_priv_put() from __j1939_ecu_release() might call kfree(priv)
before priv->lock is released (i.e. causing UAF).

Fix problem 2 by adding a safeguard that delays kfree() in
__j1939_priv_release_work() until there is no priv->lock holder.

## Problem 3: Possible list corruption in __j1939_ecu_release().

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.

Instead of requiring all j1939_ecu_put() callers to hold priv->lock for
write, 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).

## Problem 4: Possible UAF in j1939_ecu_timer_handler().

Since pending ecu->ac_timer is not canceled during j1939_ecu_unmap_all()
or __j1939_rx_release(), ecu->ac_timer can call j1939_ecu_timer_handler()
after __j1939_ecu_release() or j1939_module_exit() (i.e. causing UAF).

Fix problem 4 by adding a safeguard that explicitly cancels ecu->ac_timer
before kfree(). The fix for problem 3 makes it possible to call
hrtimer_cancel() from __j1939_ecu_release_work().

## Problem 5: Possible "struct j1939_ecu" refcount leak.

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,
"struct j1939_ecu" refcount leaks.

Fix problem 5 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).

## Problem 6: Possible UAF on module unload (part 2).

To close a race window that j1939_module_exit() starts as soon as
j1939_priv_put(session->priv) from j1939_session_destroy() dropped
the module's refcount, defer actual release to WQ context which
j1939_module_exit() can wait for completion.

Also, to make sure that session->txtimer / session->rxtimer are no longer
running when the last user called j1939_session_put(), add a safeguard
that explicitly cancels session->txtimer / session->rxtimer before kfree().

[PATCH v14 1/6] can: j1939: fix possibility of UAF on module unload
[PATCH v14 2/6] can: j1939: add a safeguard to __j1939_priv_release_work()
[PATCH v14 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release()
[PATCH v14 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work()
[PATCH v14 5/6] can: j1939: fix "struct j1939_ecu" refcount leak
[PATCH v14 6/6] can: j1939: defer j1939_session_destroy() to WQ context
---
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        |   81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
 net/can/j1939/j1939-priv.h |    8 ++++++++
 net/can/j1939/main.c       |   79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
 net/can/j1939/socket.c     |   10 +++++++++-
 net/can/j1939/transport.c  |   27 ++++++++++++++++++++++-----
 5 files changed, 168 insertions(+), 37 deletions(-)

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-17  4:02 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-15 11:35 [PATCH v14 0/6] can: j1939: make module more robust Tetsuo Handa
2026-11-15 11:35 ` [PATCH v14 1/6] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
2026-11-15 11:35 ` [PATCH v14 2/6] can: j1939: add a safeguard to __j1939_priv_release_work() Tetsuo Handa
2026-11-17  4:02   ` netdev-bot+sashiko
2026-11-15 11:36 ` [PATCH v14 3/6] can: j1939: fix possibility of list corruption in __j1939_ecu_release() Tetsuo Handa
2026-11-17  4:02   ` netdev-bot+sashiko
2026-11-15 11:36 ` [PATCH v14 4/6] can: j1939: add a safeguard to __j1939_ecu_release_work() Tetsuo Handa
2026-11-15 11:36 ` [PATCH v14 5/6] can: j1939: fix "struct j1939_ecu" refcount leak Tetsuo Handa
2026-11-17  4:02   ` netdev-bot+sashiko
2026-11-15 11:37 ` [PATCH v14 6/6] can: j1939: defer j1939_session_destroy() to WQ context Tetsuo Handa
2026-11-17  4:02   ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox