* [PATCH v6 1/8] can: bcm: copy the CAN frames outside of lock_sock()
2026-11-17 16:35 [PATCH v6 0/8] can: avoid RTNL stalls and fix CAN_ISOTP and netlink issues Oliver Hartkopp
@ 2026-11-17 16:35 ` Oliver Hartkopp
2026-11-17 16:35 ` [PATCH v6 2/8] can: bcm: do not remove the procfs entry in bcm_notify() Oliver Hartkopp
` (6 subsequent siblings)
7 siblings, 0 replies; 11+ messages in thread
From: Oliver Hartkopp @ 2026-11-17 16:35 UTC (permalink / raw)
To: linux-can
Cc: Marc Kleine-Budde, Oleksij Rempel, Vincent Mailhol, Zhu Ling,
Oliver Hartkopp
bcm_sendmsg() copies the CAN frames of TX_SETUP, RX_SETUP and TX_SEND
from user space while holding lock_sock(). A copy that faults or
stalls, e.g. on a FUSE backed mapping, holds lock_sock() for its whole
duration.
bcm_notify() takes lock_sock() of every CAN_BCM socket in the namespace
of a vanishing CAN device while holding RTNL, independent of the
binding of the socket. So a stalled copy on any CAN_BCM socket can
block RTNL on the unregistration or namespace change of a CAN device in
this namespace, e.g. of a vcan device in an unprivileged user and
network namespace.
Copy the CAN frames that the opcode handler reads to kernel memory
before lock_sock() and hand them to the handlers with a kvec based
msghdr. bcm_tx_setup(), bcm_rx_setup() and bcm_tx_send() keep their
checks and read the frames with memcpy_from_msg() as before. Up to
sizeof(struct canfd_frame) bytes, i.e. a single CAN FD frame or up to
four CAN CC frames, are copied to a buffer on the stack, more frames to
a kvmalloc() buffer of up to MAX_NFRAMES + 1 CAN FD frames.
As the frames are copied before lock_sock() now, a failing copy is
reported before the error conditions that are checked under
lock_sock(). This is in line with POSIX, which does not define an
order in which multiple error conditions are detected and reported.
The generic SOL_SOCKET options in sk_setsockopt() and sk_getsockopt(),
e.g. SO_ATTACH_FILTER, still access user space under the socket lock.
This is not specific to CAN_BCM and not addressed by this patch.
Fixes: ffd980f976e7 ("[CAN]: Add broadcast manager (bcm) protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
Changes in v2:
- the stack buffer holds up to sizeof(struct canfd_frame) bytes and not
a single CAN frame: fixed comment and commit message (Sashiko review)
- commit message: describe the remaining generic SOL_SOCKET paths
(Sashiko review)
- v1: https://lore.kernel.org/linux-can/20261107-can-bcmlock-memcg-v1-1-482d9f5be1fd@hartkopp.net/
---
net/can/bcm.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 52 insertions(+), 3 deletions(-)
diff --git a/net/can/bcm.c b/net/can/bcm.c
index 7bd2aa39777a..3f4c8d828337 100644
--- a/net/can/bcm.c
+++ b/net/can/bcm.c
@@ -1641,10 +1641,15 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
{
struct sock *sk = sock->sk;
struct bcm_sock *bo = bcm_sk(sk);
int ifindex;
struct bcm_msg_head msg_head;
+ struct msghdr fmsg = {}; /* CAN frames copied to kernel memory */
+ struct kvec kv;
+ struct canfd_frame sframe; /* stack buffer for up to CANFD_MTU bytes */
+ void *frames = &sframe;
+ size_t flen = 0;
int cfsiz;
int ret; /* read bytes or error codes as return value */
/* Lockless fast-path check for bound socket */
if (!READ_ONCE(bo->bound))
@@ -1661,10 +1666,51 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
cfsiz = CFSIZ(msg_head.flags);
if ((size - MHSIZ) % cfsiz)
return -EINVAL;
+ /* Copy the CAN frames from user space before lock_sock(), so a
+ * stalled copy can not block bcm_notify() waiting for lock_sock()
+ * under RTNL. Only the frames the opcode handler reads are copied.
+ * The handlers keep their checks and read the frames from fmsg.
+ */
+ switch (msg_head.opcode) {
+ case TX_SETUP:
+ if (msg_head.nframes >= 1 && msg_head.nframes <= MAX_NFRAMES)
+ flen = msg_head.nframes * cfsiz;
+ break;
+
+ case RX_SETUP:
+ if (!(msg_head.flags & RX_FILTER_ID) &&
+ msg_head.nframes <= MAX_NFRAMES + 1)
+ flen = msg_head.nframes * cfsiz;
+ break;
+
+ case TX_SEND:
+ if (msg_head.nframes == 1 && size == cfsiz + MHSIZ)
+ flen = cfsiz;
+ break;
+ }
+
+ /* a message with missing frames still fails in the handler */
+ flen = min(flen, msg_data_left(msg));
+
+ if (flen > sizeof(sframe)) {
+ frames = kvmalloc(flen, GFP_KERNEL);
+ if (!frames)
+ return -ENOMEM;
+ }
+
+ if (!copy_from_iter_full(frames, flen, &msg->msg_iter)) {
+ ret = -EFAULT;
+ goto out_free;
+ }
+
+ kv.iov_base = frames;
+ kv.iov_len = flen;
+ iov_iter_kvec(&fmsg.msg_iter, ITER_SOURCE, &kv, 1, flen);
+
lock_sock(sk);
/* Re-validate under the socket lock: a concurrent bcm_notify()
* may have unbound this socket (device removal) after the
* lockless fast-path check above. bo->ifindex is only ever
@@ -1719,15 +1765,15 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
}
switch (msg_head.opcode) {
case TX_SETUP:
- ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
+ ret = bcm_tx_setup(&msg_head, &fmsg, ifindex, sk);
break;
case RX_SETUP:
- ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
+ ret = bcm_rx_setup(&msg_head, &fmsg, ifindex, sk);
break;
case TX_DELETE:
if (bcm_delete_tx_op(&bo->tx_ops, &msg_head, ifindex))
ret = MHSIZ;
@@ -1757,20 +1803,23 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
case TX_SEND:
/* we need exactly one CAN frame behind the msg head */
if ((msg_head.nframes != 1) || (size != cfsiz + MHSIZ))
ret = -EINVAL;
else
- ret = bcm_tx_send(msg, ifindex, sk, cfsiz);
+ ret = bcm_tx_send(&fmsg, ifindex, sk, cfsiz);
break;
default:
ret = -EINVAL;
break;
}
out_release:
release_sock(sk);
+out_free:
+ if (frames != &sframe)
+ kvfree(frames);
return ret;
}
/*
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 2/8] can: bcm: do not remove the procfs entry in bcm_notify()
2026-11-17 16:35 [PATCH v6 0/8] can: avoid RTNL stalls and fix CAN_ISOTP and netlink issues Oliver Hartkopp
2026-11-17 16:35 ` [PATCH v6 1/8] can: bcm: copy the CAN frames outside of lock_sock() Oliver Hartkopp
@ 2026-11-17 16:35 ` Oliver Hartkopp
2026-11-17 16:35 ` [PATCH v6 3/8] can: raw: use a private mutex instead of lock_sock() Oliver Hartkopp
` (5 subsequent siblings)
7 siblings, 0 replies; 11+ messages in thread
From: Oliver Hartkopp @ 2026-11-17 16:35 UTC (permalink / raw)
To: linux-can
Cc: Marc Kleine-Budde, Oleksij Rempel, Vincent Mailhol, Zhu Ling,
Oliver Hartkopp
bcm_notify() removes the procfs entry of a CAN_BCM socket that is bound
to a vanishing CAN device with remove_proc_entry() while holding RTNL.
remove_proc_entry() waits for all running readers of this entry. A
reader of /proc/net/can-bcm/<inode> that stalls in its copy to user
space, e.g. on a FUSE backed mapping, therefore blocks RTNL on the
unregistration or namespace change of the bound CAN device.
The entry has been removed in bcm_notify() so that a later
bcm_connect() of the unbound socket can register it again without a
"proc_dir_entry already registered" warning.
The CAN_BCM socket itself stays usable after a NETDEV_UNREGISTER:
bcm_notify() only unbinds it and reports ENODEV, so the application can
connect() the socket again, e.g. to a re-created CAN device. The name
of the procfs entry is the inode number of the socket, which does not
change until the socket is closed.
Keep the procfs entry until bcm_release() instead, which removes it
after the socket has left the notifier list and without holding RTNL.
bcm_connect() only creates the entry, if it does not exist yet.
bcm_proc_show() reports an unbound socket as "unbound" instead of
showing the cleared ifindex as "any".
As bcm_proc_show() can now run while the socket is bound or unbound, it
reads bo->ifindex locklessly with READ_ONCE() and the writers use
WRITE_ONCE(). The NETDEV_DOWN handling in bcm_notify(), which runs
without lock_sock(), reads bo->bound and bo->ifindex with READ_ONCE()
as well. bo->ifindex is still only modified under lock_sock(). A
concurrent bind or unbind may show an intermediate state in the procfs
output, which is acceptable for this debug information.
Fixes: 76fe372ccb81 ("can: bcm: Remove proc entry when dev is unregistered.")
Cc: stable@vger.kernel.org
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
Changes in v3:
- read bo->bound and bo->ifindex with READ_ONCE() for NETDEV_DOWN and
shortened the bcm_notify() comment (Sashiko review)
- v2: https://lore.kernel.org/linux-can/20261108-can-bcmlock-memcg-v2-2-19fafdfc9d12@hartkopp.net/
Changes in v2:
- read bo->ifindex with READ_ONCE() in bcm_proc_show() and write it
with WRITE_ONCE(), updated the bcm_notify() comment (Sashiko review)
- v1: https://lore.kernel.org/linux-can/20261107-can-bcmlock-memcg-v1-2-482d9f5be1fd@hartkopp.net/
---
net/can/bcm.c | 33 +++++++++++++++------------------
1 file changed, 15 insertions(+), 18 deletions(-)
diff --git a/net/can/bcm.c b/net/can/bcm.c
index 3f4c8d828337..1595b7720c98 100644
--- a/net/can/bcm.c
+++ b/net/can/bcm.c
@@ -217,15 +217,20 @@ static int bcm_proc_show(struct seq_file *m, void *v)
{
char ifname[IFNAMSIZ];
struct net *net = m->private;
struct sock *sk = (struct sock *)pde_data(m->file->f_inode);
struct bcm_sock *bo = bcm_sk(sk);
+ int ifindex = READ_ONCE(bo->ifindex);
struct bcm_op *op;
seq_printf(m, ">>> sock inode %s", bo->procname);
seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
- seq_printf(m, " / bound %s", bcm_proc_getifname(net, ifname, bo->ifindex));
+ if (READ_ONCE(bo->bound))
+ seq_printf(m, " / bound %s",
+ bcm_proc_getifname(net, ifname, ifindex));
+ else
+ seq_puts(m, " / unbound");
seq_printf(m, " <<<\n");
rcu_read_lock();
list_for_each_entry_rcu(op, &bo->rx_ops, list) {
@@ -1864,23 +1869,13 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg,
if (op->ifindex == dev->ifindex)
hrtimer_cancel(&op->timer);
/* remove device reference, if this is our bound device */
if (bo->bound && bo->ifindex == dev->ifindex) {
-#if IS_ENABLED(CONFIG_PROC_FS)
- if (sock_net(sk)->can.bcmproc_dir && bo->bcm_proc_read) {
- remove_proc_entry(bo->procname, sock_net(sk)->can.bcmproc_dir);
- bo->bcm_proc_read = NULL;
- }
-#endif
- /* Paired with the lockless fast-path check in
- * bcm_sendmsg(); bo->ifindex itself is only ever
- * accessed under lock_sock() so it needs no
- * annotation.
- */
+ /* pairs with the lockless check in bcm_sendmsg() */
WRITE_ONCE(bo->bound, 0);
- bo->ifindex = 0;
+ WRITE_ONCE(bo->ifindex, 0);
notify_enodev = 1;
}
release_sock(sk);
@@ -1890,11 +1885,12 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg,
sk_error_report(sk);
}
break;
case NETDEV_DOWN:
- if (bo->bound && bo->ifindex == dev->ifindex) {
+ if (READ_ONCE(bo->bound) &&
+ READ_ONCE(bo->ifindex) == dev->ifindex) {
sk->sk_err = ENETDOWN;
if (!sock_flag(sk, SOCK_DEAD))
sk_error_report(sk);
}
}
@@ -2011,11 +2007,11 @@ static int bcm_release(struct socket *sock)
}
/* remove device reference */
if (bo->bound) {
WRITE_ONCE(bo->bound, 0);
- bo->ifindex = 0;
+ WRITE_ONCE(bo->ifindex, 0);
}
sock_orphan(sk);
sock->sk = NULL;
@@ -2058,20 +2054,21 @@ static int bcm_connect(struct socket *sock, struct sockaddr_unsized *uaddr, int
dev_put(dev);
ret = -ENODEV;
goto fail;
}
- bo->ifindex = dev->ifindex;
+ WRITE_ONCE(bo->ifindex, dev->ifindex);
dev_put(dev);
} else {
/* no interface reference for ifindex = 0 ('any' CAN device) */
- bo->ifindex = 0;
+ WRITE_ONCE(bo->ifindex, 0);
}
#if IS_ENABLED(CONFIG_PROC_FS)
- if (net->can.bcmproc_dir) {
+ /* create the procfs entry once, it is removed in bcm_release() */
+ if (net->can.bcmproc_dir && !bo->bcm_proc_read) {
/* use unique socket inode number as filename */
snprintf(bo->procname, sizeof(bo->procname),
"%llu", sock_i_ino(sk));
bo->bcm_proc_read = proc_create_net_single(bo->procname, 0644,
net->can.bcmproc_dir,
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 3/8] can: raw: use a private mutex instead of lock_sock()
2026-11-17 16:35 [PATCH v6 0/8] can: avoid RTNL stalls and fix CAN_ISOTP and netlink issues Oliver Hartkopp
2026-11-17 16:35 ` [PATCH v6 1/8] can: bcm: copy the CAN frames outside of lock_sock() Oliver Hartkopp
2026-11-17 16:35 ` [PATCH v6 2/8] can: bcm: do not remove the procfs entry in bcm_notify() Oliver Hartkopp
@ 2026-11-17 16:35 ` Oliver Hartkopp
2026-11-17 16:35 ` [PATCH v6 4/8] can: isotp: " Oliver Hartkopp
` (4 subsequent siblings)
7 siblings, 0 replies; 11+ messages in thread
From: Oliver Hartkopp @ 2026-11-17 16:35 UTC (permalink / raw)
To: linux-can
Cc: Marc Kleine-Budde, Oleksij Rempel, Vincent Mailhol, Zhu Ling,
Oliver Hartkopp
raw_notify() takes lock_sock() of the sockets that are bound to a
vanishing CAN device while holding RTNL. Any path that holds the socket
lock while accessing user space can therefore block RTNL. Since
commit 43c973a0887d ("can: raw, isotp: copy socket options outside of
lock_sock()") the CAN_RAW specific socket options copy their values
outside of the socket lock, but the generic SOL_SOCKET options in
sk_setsockopt() and sk_getsockopt() still access user space under
sockopt_lock_sock(), e.g. for SO_ATTACH_FILTER, SO_LINGER or SO_TXTIME.
A stalled copy, e.g. on a FUSE backed mapping, then blocks raw_notify()
and RTNL on the unregistration or namespace change of the bound CAN
device.
CAN_RAW only uses lock_sock() to serialize its own state: the binding,
the filters and the CAN_RAW socket options. It does not use the socket
backlog, as raw_rcv() queues the frames directly to the socket.
Replace lock_sock() with the mutex conf_lock in struct raw_sock. The
generic socket options do not take this mutex, so they can not block
raw_notify() anymore. No user space access happens while the mutex is
held. The memory allocations for the CAN receivers in can_rx_register()
are still done under the mutex, as they have been under lock_sock()
before.
Sashiko suggested to hold not only the filter copy in raw_getsockopt()
under the new conf_lock mutex but to also protect ro->raw_vcid_opts.
This change will be addressed in a follow-up patch as it goes beyond a
simple lock replacement intended for stable.
Fixes: c18ce101f2e4 ("[CAN]: Add raw protocol")
Cc: stable@vger.kernel.org # depends on "can: raw, isotp: copy socket options outside of lock_sock()"
Tested-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
net/can/raw.c | 41 ++++++++++++++++++++++-------------------
1 file changed, 22 insertions(+), 19 deletions(-)
diff --git a/net/can/raw.c b/net/can/raw.c
index f14270b56f52..b69eaadc5e93 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -101,10 +101,11 @@ struct raw_sock {
can_err_mask_t err_mask;
int count; /* number of active filters */
struct can_filter dfilter; /* default/single filter */
struct can_filter *filter; /* pointer to filter(s) */
struct uniqframe __percpu *uniq;
+ struct mutex conf_lock; /* protects bind, filters and setsockopt */
};
static LIST_HEAD(raw_notifier_list);
static DEFINE_SPINLOCK(raw_notifier_lock);
static struct raw_sock *raw_busy_notifier;
@@ -317,22 +318,22 @@ static void raw_notify(struct raw_sock *ro, unsigned long msg,
if (msg == NETDEV_DOWN)
smp_mb(); /* IFF_UP is cleared before, pairs with raw_bind() */
/* Lockless fast path, ro->bind_dev covers a concurrent bind().
* Read it before ro->dev, pairs with smp_store_release() in
- * raw_bind(). A stale ro->bind_dev only costs a lock_sock().
+ * raw_bind(). A stale ro->bind_dev only costs a mutex_lock().
*/
bind_dev = smp_load_acquire(&ro->bind_dev);
if (READ_ONCE(ro->dev) != dev &&
(msg != NETDEV_UNREGISTER || bind_dev != dev))
return;
switch (msg) {
case NETDEV_UNREGISTER:
- lock_sock(sk);
+ mutex_lock(&ro->conf_lock);
if (ro->dev != dev) {
- release_sock(sk);
+ mutex_unlock(&ro->conf_lock);
break;
}
/* remove current filters & unregister */
if (ro->bound) {
@@ -349,11 +350,11 @@ static void raw_notify(struct raw_sock *ro, unsigned long msg,
ro->count = 0;
sk->sk_err = ENODEV;
if (!sock_flag(sk, SOCK_DEAD))
sk_error_report(sk);
- release_sock(sk);
+ mutex_unlock(&ro->conf_lock);
break;
case NETDEV_DOWN:
sk->sk_err = ENETDOWN;
if (!sock_flag(sk, SOCK_DEAD))
@@ -399,10 +400,11 @@ static int raw_init(struct sock *sk)
ro->bound = 0;
ro->ifindex = 0;
ro->dev = NULL;
ro->bind_dev = NULL;
+ mutex_init(&ro->conf_lock);
/* set default filter to single entry dfilter */
ro->dfilter.can_id = 0;
ro->dfilter.can_mask = MASK_ALL;
ro->filter = &ro->dfilter;
@@ -449,11 +451,11 @@ static int raw_release(struct socket *sock)
spin_lock(&raw_notifier_lock);
}
list_del(&ro->notifier);
spin_unlock(&raw_notifier_lock);
- lock_sock(sk);
+ mutex_lock(&ro->conf_lock);
/* remove current filters & unregister
* the tracked reference ro->dev keeps the device's receive lists
* accessible, even after a namespace change
*/
@@ -472,15 +474,15 @@ static int raw_release(struct socket *sock)
ro->ifindex = 0;
ro->bound = 0;
ro->dev = NULL;
ro->count = 0;
+ mutex_unlock(&ro->conf_lock);
+
sock_orphan(sk);
sock->sk = NULL;
- release_sock(sk);
-
sock_prot_inuse_add(net, sk->sk_prot, -1);
sock_put(sk);
return 0;
}
@@ -498,12 +500,12 @@ static int raw_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int len
if (len < RAW_MIN_NAMELEN)
return -EINVAL;
if (addr->can_family != AF_CAN)
return -EINVAL;
- /* lock_sock() serializes the binding with raw_notify() */
- lock_sock(sk);
+ /* ro->conf_lock serializes the binding with raw_notify() */
+ mutex_lock(&ro->conf_lock);
if (ro->bound && addr->can_ifindex == ro->ifindex)
goto out;
if (addr->can_ifindex) {
@@ -579,11 +581,11 @@ static int raw_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int len
sk->sk_err = ENETDOWN;
if (!sock_flag(sk, SOCK_DEAD))
sk_error_report(sk);
}
- release_sock(sk);
+ mutex_unlock(&ro->conf_lock);
return err;
}
static int raw_getname(struct socket *sock, struct sockaddr *uaddr,
@@ -619,11 +621,11 @@ static int raw_setsockopt_locked(struct socket *sock, int optname,
/* @filter has been copied by raw_setsockopt(). It is linked
* to the socket on success and freed by the caller on error.
*
* The reg_state check is only a shortcut: On a concurrent
* unregistration or namespace change raw_notify() waits for
- * lock_sock() and removes the filters set here.
+ * ro->conf_lock and removes the filters set here.
*/
dev = ro->dev;
if (ro->bound && dev &&
READ_ONCE(dev->reg_state) != NETREG_REGISTERED)
return -ENODEV;
@@ -728,10 +730,11 @@ static int raw_setsockopt_locked(struct socket *sock, int optname,
static int raw_setsockopt(struct socket *sock, int level, int optname,
sockptr_t optval, unsigned int optlen)
{
struct sock *sk = sock->sk;
+ struct raw_sock *ro = raw_sk(sk);
struct can_filter *filter = NULL; /* dyn. alloc'ed filters */
struct can_filter sfilter; /* single filter */
union {
can_err_mask_t err_mask;
int flag;
@@ -742,13 +745,13 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
int err;
if (level != SOL_CAN_RAW)
return -EINVAL;
- /* Copy the option values from user space before lock_sock(), so a
- * stalled copy can not block raw_notify() waiting for lock_sock()
- * under RTNL.
+ /* Copy the option values from user space before taking ro->conf_lock,
+ * so a stalled copy can not block raw_notify() waiting for
+ * ro->conf_lock under RTNL.
*/
switch (optname) {
case CAN_RAW_FILTER:
if (optlen % sizeof(struct can_filter) != 0)
return -EINVAL;
@@ -796,15 +799,15 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
if (copy_from_sockptr(&val, optval, size))
return -EFAULT;
}
- lock_sock(sk);
+ mutex_lock(&ro->conf_lock);
err = raw_setsockopt_locked(sock, optname, &val, filter, count);
- release_sock(sk);
+ mutex_unlock(&ro->conf_lock);
/* on error free the filters allocated for CAN_RAW_FILTER above */
if (err && count > 1)
kfree(filter);
@@ -829,11 +832,11 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
switch (optname) {
case CAN_RAW_FILTER: {
struct can_filter *filter = NULL;
int err = 0;
- lock_sock(sk);
+ mutex_lock(&ro->conf_lock);
if (ro->count > 0) {
int fsize = ro->count * sizeof(struct can_filter);
/* user space buffer to small for filter list? */
if (len < fsize) {
@@ -841,19 +844,19 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
err = -ERANGE;
opt->optlen = fsize;
} else {
if (len > fsize)
len = fsize;
- /* copy to user space after release_sock() */
+ /* copy to user space after mutex_unlock() */
filter = kmemdup(ro->filter, len, GFP_KERNEL);
if (!filter)
err = -ENOMEM;
}
} else {
len = 0;
}
- release_sock(sk);
+ mutex_unlock(&ro->conf_lock);
if (filter) {
if (copy_to_iter(filter, len, &opt->iter_out) != len)
err = -EFAULT;
kfree(filter);
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 4/8] can: isotp: use a private mutex instead of lock_sock()
2026-11-17 16:35 [PATCH v6 0/8] can: avoid RTNL stalls and fix CAN_ISOTP and netlink issues Oliver Hartkopp
` (2 preceding siblings ...)
2026-11-17 16:35 ` [PATCH v6 3/8] can: raw: use a private mutex instead of lock_sock() Oliver Hartkopp
@ 2026-11-17 16:35 ` Oliver Hartkopp
2026-11-17 16:35 ` [PATCH v6 5/8] can: bcm: " Oliver Hartkopp
` (3 subsequent siblings)
7 siblings, 0 replies; 11+ messages in thread
From: Oliver Hartkopp @ 2026-11-17 16:35 UTC (permalink / raw)
To: linux-can
Cc: Marc Kleine-Budde, Oleksij Rempel, Vincent Mailhol, Zhu Ling,
Oliver Hartkopp
isotp_notify() takes lock_sock() of a socket that is bound to a
vanishing CAN device while holding RTNL. Any path that holds the socket
lock while accessing user space can therefore block RTNL. Since
commit 43c973a0887d ("can: raw, isotp: copy socket options outside of
lock_sock()") the CAN_ISOTP specific socket options copy their values
outside of the socket lock, but the generic SOL_SOCKET options in
sk_setsockopt() and sk_getsockopt() still access user space under
sockopt_lock_sock(), e.g. for SO_ATTACH_FILTER, SO_LINGER or SO_TXTIME.
CAN_ISOTP only uses lock_sock() to serialize the binding and the
CAN_ISOTP socket options. It does not use the socket backlog, as the
received PDUs are queued directly to the socket.
Replace lock_sock() with the mutex conf_lock in struct isotp_sock. The
generic socket options do not take this mutex, so they can not block
isotp_notify() anymore. No user space access happens while the mutex is
held. The memory allocations for the CAN receivers in isotp_bind() are
still done under the mutex, as they have been under lock_sock() before.
isotp_release() drops the mutex before synchronize_rcu() and the
hrtimer cancellation, as the socket has already left the notifier list
at this point.
Following a Sashiko feedback for raw.c it would make sense to copy the
content in isotp_getsockopt() under the new conf_lock mutex too.
This change will be addressed in a follow-up patch as it goes beyond a
simple lock replacement intended for stable.
Fixes: e057dd3fc20f ("can: add ISO 15765-2:2016 transport protocol")
Cc: stable@vger.kernel.org # depends on "can: raw, isotp: copy socket options outside of lock_sock()"
Tested-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
net/can/isotp.c | 28 ++++++++++++++++------------
1 file changed, 16 insertions(+), 12 deletions(-)
diff --git a/net/can/isotp.c b/net/can/isotp.c
index 7a90ba3f1cf1..8229b6a69ecd 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -180,10 +180,11 @@ struct isotp_sock {
u32 tx_result[ISOTP_TX_RESULT_SLOTS]; /* per-generation result slots */
struct tpcon rx, tx;
struct list_head notifier;
wait_queue_head_t wait;
spinlock_t rx_lock; /* protect single thread state machine */
+ struct mutex conf_lock; /* protects bind and setsockopt */
};
static LIST_HEAD(isotp_notifier_list);
static DEFINE_SPINLOCK(isotp_notifier_lock);
static struct isotp_sock *isotp_busy_notifier;
@@ -1495,11 +1496,11 @@ static int isotp_release(struct socket *sock)
spin_lock(&isotp_notifier_lock);
}
list_del(&so->notifier);
spin_unlock(&isotp_notifier_lock);
- lock_sock(sk);
+ mutex_lock(&so->conf_lock);
/* remove current filters & unregister
* the tracked reference so->dev keeps the device's receive lists
* accessible, even after a namespace change
*/
@@ -1517,10 +1518,12 @@ static int isotp_release(struct socket *sock)
so->ifindex = 0;
WRITE_ONCE(so->bound, 0);
so->dev = NULL;
+ mutex_unlock(&so->conf_lock);
+
/* Always wait for a grace period before touching the timers below.
* A concurrent NETDEV_UNREGISTER may have already unregistered our
* filters and cleared so->bound in isotp_notify() without waiting
* for in-flight isotp_rcv() callers to finish, so this call must not
* be skipped just because so->bound is already 0 here.
@@ -1533,11 +1536,10 @@ static int isotp_release(struct socket *sock)
hrtimer_cancel(&so->rxtimer);
sock_orphan(sk);
sock->sk = NULL;
- release_sock(sk);
sock_prot_inuse_add(net, sk->sk_prot, -1);
sock_put(sk);
return 0;
}
@@ -1583,22 +1585,22 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l
}
if (!addr->can_ifindex)
return -ENODEV;
- /* lock_sock() serializes the binding with isotp_notify() */
- lock_sock(sk);
+ /* so->conf_lock serializes the binding with isotp_notify() */
+ mutex_lock(&so->conf_lock);
if (so->bound) {
err = -EINVAL;
goto out;
}
/* A transmission or reception that outlived a previous binding
* (unbound by NETDEV_UNREGISTER) may still be draining; the FC/echo
* and RX watchdog timers bound how long this takes. Checked together
- * with so->bound in the same lock_sock() section above, so there is
+ * with so->bound in the same so->conf_lock section above, so there is
* no window in which a concurrent isotp_notify() could be missed.
*/
if (READ_ONCE(so->tx.state) != ISOTP_IDLE ||
READ_ONCE(so->rx.state) != ISOTP_IDLE) {
err = -EAGAIN;
@@ -1684,11 +1686,11 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l
sk->sk_err = ENETDOWN;
if (!sock_flag(sk, SOCK_DEAD))
sk_error_report(sk);
}
- release_sock(sk);
+ mutex_unlock(&so->conf_lock);
return err;
}
static int isotp_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
@@ -1788,10 +1790,11 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
static int isotp_setsockopt(struct socket *sock, int level, int optname,
sockptr_t optval, unsigned int optlen)
{
struct sock *sk = sock->sk;
+ struct isotp_sock *so = isotp_sk(sk);
union {
struct can_isotp_options opt;
struct can_isotp_fc_options rxfc;
struct can_isotp_ll_options ll;
u32 stmin;
@@ -1800,11 +1803,11 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname,
int ret;
if (level != SOL_CAN_ISOTP)
return -EINVAL;
- /* copy the option values from user space before lock_sock() */
+ /* copy the option values from user space before taking so->conf_lock */
switch (optname) {
case CAN_ISOTP_OPTS:
size = sizeof(val.opt);
break;
@@ -1829,13 +1832,13 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname,
return -EINVAL;
if (copy_from_sockptr(&val, optval, size))
return -EFAULT;
- lock_sock(sk);
+ mutex_lock(&so->conf_lock);
ret = isotp_setsockopt_locked(sock, optname, &val);
- release_sock(sk);
+ mutex_unlock(&so->conf_lock);
return ret;
}
static int isotp_getsockopt(struct socket *sock, int level, int optname,
char __user *optval, int __user *optlen)
@@ -1904,13 +1907,13 @@ static void isotp_notify(struct isotp_sock *so, unsigned long msg,
if (READ_ONCE(so->dev) != dev)
return;
switch (msg) {
case NETDEV_UNREGISTER:
- lock_sock(sk);
+ mutex_lock(&so->conf_lock);
if (so->dev != dev) {
- release_sock(sk);
+ mutex_unlock(&so->conf_lock);
break;
}
/* remove current filters & unregister */
if (so->bound) {
@@ -1930,11 +1933,11 @@ static void isotp_notify(struct isotp_sock *so, unsigned long msg,
WRITE_ONCE(so->dev, NULL);
sk->sk_err = ENODEV;
if (!sock_flag(sk, SOCK_DEAD))
sk_error_report(sk);
- release_sock(sk);
+ mutex_unlock(&so->conf_lock);
break;
case NETDEV_DOWN:
if (!READ_ONCE(so->bound))
break;
@@ -1989,10 +1992,11 @@ static int isotp_init(struct sock *sk)
struct isotp_sock *so = isotp_sk(sk);
so->ifindex = 0;
so->bound = 0;
so->dev = NULL;
+ mutex_init(&so->conf_lock);
so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
so->opt.rx_ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
so->opt.rxpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 5/8] can: bcm: use a private mutex instead of lock_sock()
2026-11-17 16:35 [PATCH v6 0/8] can: avoid RTNL stalls and fix CAN_ISOTP and netlink issues Oliver Hartkopp
` (3 preceding siblings ...)
2026-11-17 16:35 ` [PATCH v6 4/8] can: isotp: " Oliver Hartkopp
@ 2026-11-17 16:35 ` Oliver Hartkopp
2026-11-17 16:35 ` [PATCH v6 6/8] can: isotp: always start the rx timeout in ISOTP_WAIT_DATA state Oliver Hartkopp
` (2 subsequent siblings)
7 siblings, 0 replies; 11+ messages in thread
From: Oliver Hartkopp @ 2026-11-17 16:35 UTC (permalink / raw)
To: linux-can
Cc: Marc Kleine-Budde, Oleksij Rempel, Vincent Mailhol, Zhu Ling,
Oliver Hartkopp
bcm_notify() takes lock_sock() of every CAN_BCM socket in the namespace
of a vanishing CAN device while holding RTNL. Any path that holds the
socket lock while accessing user space can therefore block RTNL. With
"can: bcm: copy the CAN frames outside of lock_sock()" the CAN frames
of bcm_sendmsg() are copied outside of the socket lock, but the generic
SOL_SOCKET options in sk_setsockopt() and sk_getsockopt() still access
user space under sockopt_lock_sock(), e.g. for SO_ATTACH_FILTER,
SO_LINGER or SO_TXTIME.
CAN_BCM only uses lock_sock() to serialize the binding and the bcm_op
lists. It does not use the socket backlog, as the BCM messages are
queued directly to the socket.
Replace lock_sock() with the mutex conf_lock in struct bcm_sock. The
generic socket options do not take this mutex, so they can not block
bcm_notify() anymore. No user space access happens while the mutex is
held. The memory allocations for the bcm_ops and the CAN receivers are
still done under the mutex, as they have been under lock_sock() before.
bcm_release() removes the procfs entry before taking the mutex, as
remove_proc_entry() waits for readers that may stall in their copy to
user space. The socket has already left the notifier list at this
point.
Fixes: ffd980f976e7 ("[CAN]: Add broadcast manager (bcm) protocol")
Cc: stable@vger.kernel.org # depends on "can: bcm: copy the CAN frames outside of lock_sock()"
Tested-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
net/can/bcm.c | 30 ++++++++++++++++--------------
1 file changed, 16 insertions(+), 14 deletions(-)
diff --git a/net/can/bcm.c b/net/can/bcm.c
index 1595b7720c98..dc9e2890490a 100644
--- a/net/can/bcm.c
+++ b/net/can/bcm.c
@@ -143,10 +143,11 @@ struct bcm_sock {
struct list_head rx_ops;
struct list_head tx_ops;
unsigned long dropped_usr_msgs;
struct proc_dir_entry *bcm_proc_read;
char procname [32]; /* inode number in decimal with \0 */
+ struct mutex conf_lock; /* protects bind and the bcm_op lists */
};
static LIST_HEAD(bcm_notifier_list);
static DEFINE_SPINLOCK(bcm_notifier_lock);
static struct bcm_sock *bcm_busy_notifier;
@@ -1671,12 +1672,12 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
cfsiz = CFSIZ(msg_head.flags);
if ((size - MHSIZ) % cfsiz)
return -EINVAL;
- /* Copy the CAN frames from user space before lock_sock(), so a
- * stalled copy can not block bcm_notify() waiting for lock_sock()
+ /* Copy the CAN frames from user space before taking bo->conf_lock, so a
+ * stalled copy can not block bcm_notify() waiting for bo->conf_lock
* under RTNL. Only the frames the opcode handler reads are copied.
* The handlers keep their checks and read the frames from fmsg.
*/
switch (msg_head.opcode) {
case TX_SETUP:
@@ -1712,16 +1713,16 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
kv.iov_base = frames;
kv.iov_len = flen;
iov_iter_kvec(&fmsg.msg_iter, ITER_SOURCE, &kv, 1, flen);
- lock_sock(sk);
+ mutex_lock(&bo->conf_lock);
- /* Re-validate under the socket lock: a concurrent bcm_notify()
+ /* Re-validate under bo->conf_lock: a concurrent bcm_notify()
* may have unbound this socket (device removal) after the
* lockless fast-path check above. bo->ifindex is only ever
- * mutated under lock_sock(), so reading it here - instead of
+ * mutated under bo->conf_lock, so reading it here - instead of
* before taking the lock - guarantees it can't be observed
* torn against bo->bound.
*/
if (!bo->bound) {
ret = -ENOTCONN;
@@ -1817,11 +1818,11 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
ret = -EINVAL;
break;
}
out_release:
- release_sock(sk);
+ mutex_unlock(&bo->conf_lock);
out_free:
if (frames != &sframe)
kvfree(frames);
return ret;
@@ -1841,11 +1842,11 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg,
return;
switch (msg) {
case NETDEV_UNREGISTER:
- lock_sock(sk);
+ mutex_lock(&bo->conf_lock);
/* rx_ops: remove device specific receive entries */
list_for_each_entry(op, &bo->rx_ops, list) {
if (op->rx_reg_dev == dev)
bcm_rx_unreg(dev, op);
@@ -1875,11 +1876,11 @@ static void bcm_notify(struct bcm_sock *bo, unsigned long msg,
WRITE_ONCE(bo->bound, 0);
WRITE_ONCE(bo->ifindex, 0);
notify_enodev = 1;
}
- release_sock(sk);
+ mutex_unlock(&bo->conf_lock);
if (notify_enodev) {
sk->sk_err = ENODEV;
if (!sock_flag(sk, SOCK_DEAD))
sk_error_report(sk);
@@ -1928,10 +1929,11 @@ static int bcm_init(struct sock *sk)
bo->bound = 0;
bo->ifindex = 0;
bo->dropped_usr_msgs = 0;
bo->bcm_proc_read = NULL;
+ mutex_init(&bo->conf_lock);
INIT_LIST_HEAD(&bo->tx_ops);
INIT_LIST_HEAD(&bo->rx_ops);
/* set notifier */
@@ -1967,19 +1969,18 @@ static int bcm_release(struct socket *sock)
spin_lock(&bcm_notifier_lock);
}
list_del(&bo->notifier);
spin_unlock(&bcm_notifier_lock);
- /* bcm_notify() can not access this socket anymore */
- lock_sock(sk);
-
#if IS_ENABLED(CONFIG_PROC_FS)
/* remove procfs entry */
if (net->can.bcmproc_dir && bo->bcm_proc_read)
remove_proc_entry(bo->procname, net->can.bcmproc_dir);
#endif /* CONFIG_PROC_FS */
+ mutex_lock(&bo->conf_lock);
+
list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
list_del_rcu(&op->list);
bcm_remove_op(op);
}
@@ -2010,14 +2011,15 @@ static int bcm_release(struct socket *sock)
if (bo->bound) {
WRITE_ONCE(bo->bound, 0);
WRITE_ONCE(bo->ifindex, 0);
}
+ mutex_unlock(&bo->conf_lock);
+
sock_orphan(sk);
sock->sk = NULL;
- release_sock(sk);
sock_prot_inuse_add(net, sk->sk_prot, -1);
sock_put(sk);
return 0;
}
@@ -2032,11 +2034,11 @@ static int bcm_connect(struct socket *sock, struct sockaddr_unsized *uaddr, int
int ret = 0;
if (len < BCM_MIN_NAMELEN)
return -EINVAL;
- lock_sock(sk);
+ mutex_lock(&bo->conf_lock);
if (bo->bound) {
ret = -EISCONN;
goto fail;
}
@@ -2084,11 +2086,11 @@ static int bcm_connect(struct socket *sock, struct sockaddr_unsized *uaddr, int
* with the lockless fast-path check in bcm_sendmsg()
*/
WRITE_ONCE(bo->bound, 1);
fail:
- release_sock(sk);
+ mutex_unlock(&bo->conf_lock);
return ret;
}
static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 6/8] can: isotp: always start the rx timeout in ISOTP_WAIT_DATA state
2026-11-17 16:35 [PATCH v6 0/8] can: avoid RTNL stalls and fix CAN_ISOTP and netlink issues Oliver Hartkopp
` (4 preceding siblings ...)
2026-11-17 16:35 ` [PATCH v6 5/8] can: bcm: " Oliver Hartkopp
@ 2026-11-17 16:35 ` Oliver Hartkopp
2026-11-17 16:35 ` [PATCH v6 7/8] can: isotp: reject socket options while a transfer is active Oliver Hartkopp
2026-11-17 16:35 ` [PATCH v6 8/8] can: netlink: fix static FD-NON-ISO mode validation Oliver Hartkopp
7 siblings, 0 replies; 11+ messages in thread
From: Oliver Hartkopp @ 2026-11-17 16:35 UTC (permalink / raw)
To: linux-can
Cc: Marc Kleine-Budde, Oleksij Rempel, Vincent Mailhol, Zhu Ling,
Oliver Hartkopp
The rx timeout is only started at the end of isotp_send_fc() and for
consecutive frames inside a block in isotp_rcv_cf(). So so->rx.state
can stay in ISOTP_WAIT_DATA without a running rx timeout:
- isotp_send_fc() returns early, when it can not allocate the skb or
does not find the CAN device anymore
- isotp_rcv_ff() does not send a flow control frame in listen mode
- isotp_rcv_cf() has stopped the rx timeout and does not restart it at
the end of a block, when the flow control frame is not sent
On a bound socket the next first frame or single frame resets the
state. But when the CAN device has been removed, no further CAN frame
is received and so->rx.state remains in ISOTP_WAIT_DATA. isotp_bind()
then rejects a new binding with -EAGAIN until the socket is closed.
Start the rx timeout in isotp_rcv_ff() and isotp_rcv_cf() whenever the
reception continues in ISOTP_WAIT_DATA state, before a flow control
frame is sent. isotp_send_fc() restarts the rx timeout as before. All
this runs under so->rx_lock, so the order of the rx timeout start and
the transmission of the flow control frame does not matter.
In listen mode a first frame that is not followed by consecutive
frames now leads to ETIMEDOUT, like a missing consecutive frame inside
a block already does. So a listening socket reports a missing
consecutive frame consistently, independent of its position in the
PDU.
Fixes: e057dd3fc20f ("can: add ISO 15765-2:2016 transport protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
net/can/isotp.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/net/can/isotp.c b/net/can/isotp.c
index 8229b6a69ecd..0293d590186c 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -632,10 +632,14 @@ static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae)
/* initial setup for this pdu reception */
so->rx.sn = 1;
WRITE_ONCE(so->rx.state, ISOTP_WAIT_DATA);
+ /* start rx timeout, isotp_send_fc() restarts it */
+ hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
+ HRTIMER_MODE_REL_SOFT);
+
/* no creation of flow control frames */
if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
return 0;
/* send our first FC frame */
@@ -724,17 +728,17 @@ static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae,
nskb->dev = skb->dev;
isotp_rcv_skb(nskb, sk);
return 0;
}
+ /* start rx timeout, isotp_send_fc() restarts it */
+ hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
+ HRTIMER_MODE_REL_SOFT);
+
/* perform blocksize handling, if enabled */
- if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
- /* start rx timeout watchdog */
- hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
- HRTIMER_MODE_REL_SOFT);
+ if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs)
return 0;
- }
/* no creation of flow control frames */
if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
return 0;
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH v6 7/8] can: isotp: reject socket options while a transfer is active
2026-11-17 16:35 [PATCH v6 0/8] can: avoid RTNL stalls and fix CAN_ISOTP and netlink issues Oliver Hartkopp
` (5 preceding siblings ...)
2026-11-17 16:35 ` [PATCH v6 6/8] can: isotp: always start the rx timeout in ISOTP_WAIT_DATA state Oliver Hartkopp
@ 2026-11-17 16:35 ` Oliver Hartkopp
2026-11-17 17:17 ` sashiko-bot
2026-11-17 16:35 ` [PATCH v6 8/8] can: netlink: fix static FD-NON-ISO mode validation Oliver Hartkopp
7 siblings, 1 reply; 11+ messages in thread
From: Oliver Hartkopp @ 2026-11-17 16:35 UTC (permalink / raw)
To: linux-can
Cc: Marc Kleine-Budde, Oleksij Rempel, Vincent Mailhol, Zhu Ling,
Sashiko, Oliver Hartkopp
isotp_setsockopt_locked() only checks so->bound before it changes the
CAN_ISOTP socket options. When the bound CAN device is unregistered,
isotp_notify() clears so->bound, but a transmission or reception that
is already in progress is not stopped and may still be draining.
isotp_sendmsg() claims so->tx.state and rechecks so->bound. After that
it can block in sock_alloc_send_skb(), while the device is unregistered
and so->bound is cleared. A concurrent setsockopt() can then change
so->opt, so->ll and so->tx.ll_dl, before isotp_sendmsg() reads so->ll.mtu
and so->tx.ll_dl again to build the CAN frame for the skb that has been
allocated with the former so->ll.mtu.
isotp_bind() already rejects a new binding while so->tx.state or
so->rx.state are not ISOTP_IDLE for the same reason. Add this check to
isotp_setsockopt_locked() and return -EAGAIN, so the socket options can
only be changed when no transfer is active. As the transmission and
the reception always end by their timeouts at the latest, this state
is only temporary.
Fixes: e057dd3fc20f ("can: add ISO 15765-2:2016 transport protocol")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-d1d3ef49-c24e-4f3a-91ce-20f8fb222684
Cc: stable@vger.kernel.org
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
net/can/isotp.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/net/can/isotp.c b/net/can/isotp.c
index 0293d590186c..32c88749d75e 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -1723,10 +1723,18 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
int ret = 0;
if (so->bound)
return -EISCONN;
+ /* A transmission or reception that outlived a previous binding
+ * (unbound by NETDEV_UNREGISTER) may still be draining and uses the
+ * socket options, see isotp_bind().
+ */
+ if (READ_ONCE(so->tx.state) != ISOTP_IDLE ||
+ READ_ONCE(so->rx.state) != ISOTP_IDLE)
+ return -EAGAIN;
+
switch (optname) {
case CAN_ISOTP_OPTS:
memcpy(&so->opt, val, sizeof(so->opt));
/* no separate rx_ext_address is given => use ext_address */
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [PATCH v6 7/8] can: isotp: reject socket options while a transfer is active
2026-11-17 16:35 ` [PATCH v6 7/8] can: isotp: reject socket options while a transfer is active Oliver Hartkopp
@ 2026-11-17 17:17 ` sashiko-bot
2026-11-17 18:58 ` Oliver Hartkopp
0 siblings, 1 reply; 11+ messages in thread
From: sashiko-bot @ 2026-11-17 17:17 UTC (permalink / raw)
To: Oliver Hartkopp, linux-can
Cc: Marc Kleine-Budde, Vincent Mailhol, Oleksij Rempel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] can: isotp: race in isotp_setsockopt_locked() while rx.state
is ISOTP_IDLE
--
commit 2bbcd6c26df0af71086da24b238251d946bf05f7
Author: Oliver Hartkopp <socketcan@hartkopp.net>
can: isotp: reject socket options while a transfer is active
This commit rejects changing CAN_ISOTP socket options in
isotp_setsockopt_locked() when so->tx.state or so->rx.state is not
ISOTP_IDLE.
> diff --git a/net/can/isotp.c b/net/can/isotp.c
> index 0293d590186c4..32c88749d75e7 100644
> --- a/net/can/isotp.c
> +++ b/net/can/isotp.c
> @@ -1725,6 +1725,14 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
> if (so->bound)
> return -EISCONN;
>
> + /* A transmission or reception that outlived a previous binding
> + * (unbound by NETDEV_UNREGISTER) may still be draining and uses the
> + * socket options, see isotp_bind().
> + */
> + if (READ_ONCE(so->tx.state) != ISOTP_IDLE ||
> + READ_ONCE(so->rx.state) != ISOTP_IDLE)
> + return -EAGAIN;
[Severity: Medium]
Can isotp_setsockopt_locked() still race with an in-flight isotp_rcv()
handler while so->rx.state is ISOTP_IDLE?
isotp_notify() clears so->bound on NETDEV_UNREGISTER without waiting for
an RCU grace period or acquiring so->rx_lock. Meanwhile, isotp_rcv_ff()
resets so->rx.state to ISOTP_IDLE before sending an overflow Flow Control
frame or transitioning to ISOTP_WAIT_DATA, and isotp_rcv_cf() sets
so->rx.state to ISOTP_IDLE before checking padding options. Because
isotp_setsockopt_locked() and isotp_bind() check so->rx.state without
holding so->rx_lock, a concurrent setsockopt() can still modify so->ll.mtu
between alloc_skb() and skb_put_zero() in isotp_send_fc():
CPU0 (<softirq>) CPU1
isotp_rcv()
spin_lock(&so->rx_lock)
isotp_rcv_ff()
WRITE_ONCE(so->rx.state, ISOTP_IDLE)
isotp_notify(NETDEV_UNREGISTER)
so->bound = 0
isotp_send_fc(..., ISOTP_FC_OVFLW)
alloc_skb(so->ll.mtu, ...)
/* allocated with CAN_MTU (16) */
isotp_setsockopt_locked(CAN_ISOTP_LL_OPTS)
/* rx.state is ISOTP_IDLE */
so->ll.mtu = CANFD_MTU (72)
isotp_bind()
so->ifindex = ...
dev = dev_get_by_index(...)
skb_put_zero(nskb, so->ll.mtu)
<-- skb_over_panic (72 > 16)
Acquiring so->rx_lock around the state check and option update (or
waiting for in-flight RCU readers on unbind) would close this window.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-can-bcmlock-memcg-v6-0-6826a6227d36@hartkopp.net?part=7
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH v6 7/8] can: isotp: reject socket options while a transfer is active
2026-11-17 17:17 ` sashiko-bot
@ 2026-11-17 18:58 ` Oliver Hartkopp
0 siblings, 0 replies; 11+ messages in thread
From: Oliver Hartkopp @ 2026-11-17 18:58 UTC (permalink / raw)
To: sashiko-reviews, linux-can
Cc: Marc Kleine-Budde, Vincent Mailhol, Oleksij Rempel
On 17.11.26 18:17, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] can: isotp: race in isotp_setsockopt_locked() while rx.state
> is ISOTP_IDLE
> --
>
> commit 2bbcd6c26df0af71086da24b238251d946bf05f7
> Author: Oliver Hartkopp <socketcan@hartkopp.net>
>
> can: isotp: reject socket options while a transfer is active
>
> This commit rejects changing CAN_ISOTP socket options in
> isotp_setsockopt_locked() when so->tx.state or so->rx.state is not
> ISOTP_IDLE.
>
>> diff --git a/net/can/isotp.c b/net/can/isotp.c
>> index 0293d590186c4..32c88749d75e7 100644
>> --- a/net/can/isotp.c
>> +++ b/net/can/isotp.c
>> @@ -1725,6 +1725,14 @@ static int isotp_setsockopt_locked(struct socket *sock, int optname,
>> if (so->bound)
>> return -EISCONN;
>>
>> + /* A transmission or reception that outlived a previous binding
>> + * (unbound by NETDEV_UNREGISTER) may still be draining and uses the
>> + * socket options, see isotp_bind().
>> + */
>> + if (READ_ONCE(so->tx.state) != ISOTP_IDLE ||
>> + READ_ONCE(so->rx.state) != ISOTP_IDLE)
>> + return -EAGAIN;
>
> [Severity: Medium]
> Can isotp_setsockopt_locked() still race with an in-flight isotp_rcv()
> handler while so->rx.state is ISOTP_IDLE?
>
> isotp_notify() clears so->bound on NETDEV_UNREGISTER without waiting for
> an RCU grace period or acquiring so->rx_lock. Meanwhile, isotp_rcv_ff()
> resets so->rx.state to ISOTP_IDLE before sending an overflow Flow Control
> frame or transitioning to ISOTP_WAIT_DATA, and isotp_rcv_cf() sets
> so->rx.state to ISOTP_IDLE before checking padding options. Because
> isotp_setsockopt_locked() and isotp_bind() check so->rx.state without
> holding so->rx_lock, a concurrent setsockopt() can still modify so->ll.mtu
> between alloc_skb() and skb_put_zero() in isotp_send_fc():
>
> CPU0 (<softirq>) CPU1
> isotp_rcv()
> spin_lock(&so->rx_lock)
> isotp_rcv_ff()
> WRITE_ONCE(so->rx.state, ISOTP_IDLE)
> isotp_notify(NETDEV_UNREGISTER)
> so->bound = 0
> isotp_send_fc(..., ISOTP_FC_OVFLW)
> alloc_skb(so->ll.mtu, ...)
> /* allocated with CAN_MTU (16) */
> isotp_setsockopt_locked(CAN_ISOTP_LL_OPTS)
> /* rx.state is ISOTP_IDLE */
> so->ll.mtu = CANFD_MTU (72)
> isotp_bind()
> so->ifindex = ...
> dev = dev_get_by_index(...)
> skb_put_zero(nskb, so->ll.mtu)
> <-- skb_over_panic (72 > 16)
Nope.
The normal sequence for a CAN_ISOTP socket is setsockopt() -> bind() ->
release(). Only an attacker would change the socket options while the
bound CAN device vanishes. The attacker needs CAP_NET_ADMIN for the CAN
device, e.g. a vcan device in an own user and network namespace.
What the attacker gets at most, for the four (in-flight) frame types:
- Single Frame: the PDU is delivered to the own socket or dropped with
EBADMSG, depending on partly updated padding options.
- Consecutive Frame: only handled in ISOTP_WAIT_DATA state, where
setsockopt() is rejected with -EAGAIN. No mixed socket options.
- Flow Control: only handled while a transmission waits for it, where
setsockopt() is rejected with -EAGAIN. No mixed socket options.
- First Frame: the rx state goes to ISOTP_WAIT_DATA and the rx timeout
resets it with ETIMEDOUT after one second. The flow control frame is
not sent as so->ifindex is 0. Only with a concurrent bind() to another
device a single flow control frame with partly updated options is sent
on this device. With an increased max_pdu_size the rx buffer may be
allocated, which is freed on close().
On unregistration the netdev core runs flush_all_backlogs() and
synchronize_net() before calling the NETDEV_UNREGISTER notifiers, so an
isotp_rcv() call can only be in flight for a frame from the backlog on a
namespace change. In the First Frame case the following sequence can
then happen:
CPU0 (softirq) CPU1 (attacker)
isotp_rcv() ip link set vcanX netns ...
(frame from the backlog) NETDEV_UNREGISTER
spin_lock(&so->rx_lock) isotp_notify()
isotp_rcv_ff() so->bound = 0, so->ifindex = 0
rx.state = ISOTP_IDLE setsockopt(CAN_ISOTP_LL_OPTS)
rx/tx state is ISOTP_IDLE
so->ll is updated
[optional] bind() to another device
so->ifindex = new device
isotp_send_fc()
alloc_skb(old so->ll.mtu)
dev_get_by_index(so->ifindex)
ifindex 0: free skb, return
new device: skb_put_zero(new so->ll.mtu)
send the FC frame
CAN_MTU is 16 bytes and CANFD_MTU is 72 bytes. The skb_put_zero() with a
bigger new MTU does not overflow the skb, as alloc_skb() does not limit
skb->end to the requested size: for the GFP flags used in
isotp_send_fc() small skb heads are taken from the skb_small_head_cache,
which provides at least MAX_TCP_HEADER bytes. MAX_TCP_HEADER is
L1_CACHE_ALIGN(128 + MAX_HEADER), which is at least 160 bytes in every
configuration. __finalize_skb_around() sets skb->end to the size that
has really been allocated, so a skb that has been allocated for CAN_MTU
also has room for CANFD_MTU.
There is no memory corruption, no persistent state and no effect on
other sockets or on RTNL. Everything stays on the attacker's own socket
and CAN device.
TL;DR The worst case is a single flow control frame on the newly bound
CAN device that is built with partly updated socket options. There is
no memory corruption and the rx timeout resets the rx state afterwards.
I do not see a reason to add more locking to the reception path for
this attacker-only scenario.
Best regards,
Oliver
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v6 8/8] can: netlink: fix static FD-NON-ISO mode validation
2026-11-17 16:35 [PATCH v6 0/8] can: avoid RTNL stalls and fix CAN_ISOTP and netlink issues Oliver Hartkopp
` (6 preceding siblings ...)
2026-11-17 16:35 ` [PATCH v6 7/8] can: isotp: reject socket options while a transfer is active Oliver Hartkopp
@ 2026-11-17 16:35 ` Oliver Hartkopp
7 siblings, 0 replies; 11+ messages in thread
From: Oliver Hartkopp @ 2026-11-17 16:35 UTC (permalink / raw)
To: linux-can
Cc: Marc Kleine-Budde, Oleksij Rempel, Vincent Mailhol, Zhu Ling,
Oliver Hartkopp
From: Zhu Ling <zhuling0805@qq.com>
Controllers with a static CAN_CTRLMODE_FD_NON_ISO mode (M_CAN v3.0.x and
v3.1.x, PEAK pcan_usb_fd with firmware < 2.x) do not require the
'fd-non-iso' option to be provided when CAN FD is not switched on by the
IFLA_CAN_CTRLMODE request.
Since commit 6742ca18cb41 ("can: netlink: add userland error messages")
ctrlstatic_missing is calculated before this exception is applied to
ctrlstatic, which is not evaluated afterwards. As a result every
IFLA_CAN_CTRLMODE request on such controllers that does not contain
'fd-non-iso on' is rejected with -EOPNOTSUPP and "missing required
FD-NON-ISO static control mode", e.g. 'fd off', 'loopback on' or
'one-shot on', regardless of the current CAN FD state.
Apply the exception to ctrlstatic_missing instead.
The exception must only relax the requirement to *provide* the static
FD-NON-ISO mode. It must not allow to *switch off* a static mode: before
the above mentioned commit a request like 'fd-non-iso off' passed the
check and removed the static CAN_CTRLMODE_FD_NON_ISO bit from
priv->ctrlmode. As can_get_static_ctrlmode() derives the static modes
from priv->ctrlmode, this information was lost permanently. Therefore
reject any request that explicitly switches off a static control mode
with a dedicated error message. With this check in place a static mode
can never be cleared by the 'priv->ctrlmode &= ~cm->mask' operation as
every static bit in cm->mask is also set in maskedflags.
The request is rejected independent of the resulting CAN FD state, as
accepting it while CAN FD is switched off would remove the static mode
from priv->ctrlmode as well.
Fixes: 6742ca18cb41 ("can: netlink: add userland error messages")
Cc: stable@vger.kernel.org
Signed-off-by: Zhu Ling <zhuling0805@qq.com>
Link: https://lore.kernel.org/all/tencent_3A964832542399A780E646D6A6246335C409@qq.com/
[Hartkopp: reject switching off static control modes instead of restoring
them, extend the commit message (Sashiko review)]
Link: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261115095746.709371-1-mkl%40pengutronix.de
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
drivers/net/can/dev/netlink.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/drivers/net/can/dev/netlink.c b/drivers/net/can/dev/netlink.c
index 081dfddda000..521e2496e2cc 100644
--- a/drivers/net/can/dev/netlink.c
+++ b/drivers/net/can/dev/netlink.c
@@ -328,13 +328,21 @@ static int can_ctrlmode_changelink(struct net_device *dev,
"requested control mode %s not supported",
can_get_ctrlmode_str(notsupp));
return -EOPNOTSUPP;
}
+ /* static control modes can not be switched off */
+ if (deactivated & ctrlstatic) {
+ NL_SET_ERR_MSG_FMT(extack,
+ "static control mode %s can not be switched off",
+ can_get_ctrlmode_str(deactivated & ctrlstatic));
+ return -EOPNOTSUPP;
+ }
+
/* do not check for static fd-non-iso if 'fd' is disabled */
if (!(maskedflags & CAN_CTRLMODE_FD))
- ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
+ ctrlstatic_missing &= ~CAN_CTRLMODE_FD_NON_ISO;
if (ctrlstatic_missing) {
NL_SET_ERR_MSG_FMT(extack,
"missing required %s static control mode",
can_get_ctrlmode_str(ctrlstatic_missing));
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread