Linux CAN drivers development
 help / color / mirror / Atom feed
* [PATCH can-next v3 0/4] can: protect the receive lists with a per-device lock
@ 2026-10-19  7:42 Oliver Hartkopp
  2026-10-19  7:42 ` [PATCH can-next v3 1/4] can: proc: reset pkg_stats atomics individually Oliver Hartkopp
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Oliver Hartkopp @ 2026-10-19  7:42 UTC (permalink / raw)
  To: linux-can; +Cc: Oliver Hartkopp

This series fixes a use-after-free in CAN_ISOTP and CAN_BCM that can be
triggered when a CAN device is moved to another network namespace while
a socket bound to this device is released [1].

Only patch 4 is new. Patches 1-3 are already on their way to net-next
with the linux-can-next pull request [2]. They are included unchanged
only to provide a sequence that applies on top of mainline, so that the
sashiko bot is able to build and review patch 4. Please do not apply
patches 1-3 from this series.

The idea behind patch 4:

The receive lists of a CAN device are located in the device's
can_ml_priv and travel with the device when it is moved to another
network namespace. But they are protected by the per-netns
rcvlists_lock, so the lock that protects a device's lists depends on
the namespace the caller passes to can_rx_register() and
can_rx_unregister(). can_rx_unregister() silently returns when this
namespace does not match dev_net(dev) anymore.

isotp_release() and bcm_release() leave the netdev notifier list before
they can serialize against the notifier. When the device changes its
namespace in this window, their filters are not removed and the
remaining receivers point to the freed socket or bcm op.

Taking RTNL in these release paths is no option, as CAN_RAW already
holds RTNL while adding and removing up to CAN_RAW_FILTER_MAX filters
per socket, which unprivileged users can trigger repeatedly.

Instead, patch 4 protects the receive lists where they are located: A
spinlock in struct can_dev_rcv_lists protects the lists of a CAN device
(initialized in can_set_ml_priv()) or the 'any' CAN device lists of a
namespace (initialized in can_pernet_init()). can_rx_unregister() then
finds the subscription independently of the current namespace of the
device, which is kept accessible by the tracked device references the
protocols already hold. The per-netns rcvlists_lock only protects the
receive list statistics, for which the callers pass the namespace that
has been used for can_rx_register().

As a consequence, CAN_RAW and CAN_ISOTP do not need RTNL in bind(),
setsockopt() and release() anymore. bind() is serialized against
NETDEV_UNREGISTER by publishing the device inside the RCU section of
its lookup, as unlist_netdevice() and synchronize_net() precede
NETDEV_UNREGISTER both on unregistration and on a namespace change.
The notifiers recheck the binding under lock_sock().

[1] https://lore.kernel.org/linux-can/CEA6A38A-2646-4ADA-95B4-CBAE2F301A8E@doyensec.com/
[2] https://lore.kernel.org/linux-can/20261018164417.1540449-1-mkl@pengutronix.de/

Previous versions of patch 4:
v2: https://lore.kernel.org/linux-can/20261018180739.689750-1-socketcan@hartkopp.net/
v1: https://lore.kernel.org/linux-can/20261017201935.399233-1-socketcan@hartkopp.net/

Oliver Hartkopp (3):
  can: proc: remove pointers from CAN specific proc output
  can: replace ARPHRD_CAN type checks with can_get_ml_priv()
  can: protect the receive lists with a per-device lock

Runyu Xiao (1):
  can: proc: reset pkg_stats atomics individually

 Documentation/networking/can.rst |  16 ++--
 include/linux/can/can-ml.h       |   3 +
 include/linux/can/core.h         |   2 +-
 include/net/netns/can.h          |   2 +-
 net/can/af_can.c                 |  59 ++++++------
 net/can/af_can.h                 |   2 +-
 net/can/bcm.c                    |  64 +++++--------
 net/can/gw.c                     |   9 +-
 net/can/isotp.c                  |  49 ++++++----
 net/can/j1939/main.c             |   2 +-
 net/can/proc.c                   |  65 +++++++++-----
 net/can/raw.c                    | 150 ++++++++++++++++++++-----------
 12 files changed, 252 insertions(+), 171 deletions(-)


base-commit: 32c52ec87d6245ee2d12ed5dd509e40062939b50
-- 
2.43.0


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

end of thread, other threads:[~2026-10-19 15:29 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-19  7:42 [PATCH can-next v3 0/4] can: protect the receive lists with a per-device lock Oliver Hartkopp
2026-10-19  7:42 ` [PATCH can-next v3 1/4] can: proc: reset pkg_stats atomics individually Oliver Hartkopp
2026-10-19  7:53   ` sashiko-bot
2026-10-19  8:08     ` Oliver Hartkopp
2026-10-19 11:54       ` Oliver Hartkopp
2026-10-19  7:42 ` [PATCH can-next v3 2/4] can: proc: remove pointers from CAN specific proc output Oliver Hartkopp
2026-10-19  7:42 ` [PATCH can-next v3 3/4] can: replace ARPHRD_CAN type checks with can_get_ml_priv() Oliver Hartkopp
2026-10-19  7:42 ` [PATCH can-next v3 4/4] can: protect the receive lists with a per-device lock Oliver Hartkopp
2026-10-19 14:12 ` [PATCH can-next v3 0/4] " Marc Kleine-Budde
2026-10-19 14:46   ` Oliver Hartkopp
2026-10-19 14:49     ` Marc Kleine-Budde
2026-10-19 14:52       ` Marc Kleine-Budde
2026-10-19 15:28         ` Oliver Hartkopp

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