Re: [PATCH net 07/12] can: isotp: reject socket options while a transfer is active

From: Oliver Hartkopp

Date: Wed Nov 25 2026 - 04:31:11 EST




On 25.11.26 09:58, netdev-bot+sashiko@xxxxxxxxxx wrote:
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The commit message says that moving the `ae`/`wait_tx_done`
reads after the claim means "all socket options used by…
- [Medium] The new check in isotp_setsockopt_locked() (net/can/isotp.c,
lines 1737-1739) reads so->tx.state and so->rx.state with READ_ONCE()…



Both findings belong to the scenario that has already been discussed
for v6 of this patch:

https://lore.kernel.org/linux-can/8cf5687f-4dd4-42f9-b459-5e192b2bcdd5@xxxxxxxxxxxx/

--

can: isotp: reject socket options while a transfer is active

This patch makes isotp_setsockopt_locked() return -EAGAIN while
so->tx.state or so->rx.state is not ISOTP_IDLE, mirroring the check in
isotp_bind(). It also moves the CAN_ISOTP_EXTEND_ADDR and
CAN_ISOTP_WAIT_TX_DONE reads in isotp_sendmsg() to after the transfer claim
and the so->bound recheck.

Link: https://patch.msgid.link/20261120-can-bcmlock-memcg-v7-7-f6b84f5257b1@xxxxxxxxxxxx

diff --git a/net/can/isotp.c b/net/can/isotp.c
index 0b7db1ead7a90..642fec500af9d 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c

[ ... ]

@@ -1188,6 +1188,10 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
goto err_out_drop;
}
+ /* read the options after the claim of the transfer */
+ ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
+ wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0;
+

[Severity: Medium]

The commit message says this change means "all socket options used by
isotp_sendmsg() are covered by this check". Is that accurate?


This requires that the caller removes or moves its own CAN device and
changes the socket options while its own sendmsg() is in progress. At
most the first frame of this sendmsg() is sent with the flags byte of
the new options, and only when the caller has moved the CAN device into
another network namespace and set it up again. See below.


Later in isotp_sendmsg(), so->ll.tx_flags is still read after the final
tx.state is published, the timeout is armed and so->rx_lock is dropped:

net/can/isotp.c:isotp_sendmsg() {
...
WRITE_ONCE(so->tx.state, new_state);
hrtimer_start(tx_hrt, ktime_set(hrtimer_sec, 0),
HRTIMER_MODE_REL_SOFT);
spin_unlock_bh(&so->rx_lock);

/* send the first or only CAN frame */
cf->flags = so->ll.tx_flags;
...
}

After that point the transfer can return to ISOTP_IDLE without sendmsg()
doing anything. isotp_tx_timeout() can cmpxchg tx.state to IDLE after the
FC or echo timeout. In WAIT_FIRST_FC, an FC frame that isotp_rcv_fc()
handles (overflow, unknown status, bad padding) can also set IDLE.

Suppose NETDEV_UNREGISTER then clears so->bound in isotp_notify(). Then
isotp_setsockopt_locked() passes both the so->bound check and the new
tx.state/rx.state check. CAN_ISOTP_LL_OPTS can then memcpy() a new so->ll
before the cf->flags assignment runs.

Would the first frame then take its flags byte from the new options, while
its skb size (old ll.mtu), length and payload come from the old ones?

can_send() usually fails with -ENETDOWN here. The frame could still go out
if the device was moved to another netns and brought up again.

Could cf->flags be set while the frame is built, before tx.state is
published and the timer is armed?

[ ... ]

@@ -1726,6 +1730,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]

This check runs under conf_lock only, without so->rx_lock. Can it race with
RX paths that have already set rx.state to ISOTP_IDLE but are still reading
the socket options?


This is the case of a frame in flight that has already been described
in the referenced mail: when the caller moves its own CAN device into
another network namespace and concurrently changes the socket options
and binds the socket, a single, possibly inconsistent, flow control
frame can be sent on the caller's own target CAN device, or the padding
check for the caller's own reception can use the new options. There is
no memory corruption, as alloc_skb() takes the buffer for these small
sizes from skb_small_head_cache, which is larger than CANFD_MTU, so
skb_put_zero() with the new so->ll.mtu stays within the buffer.


For example, isotp_rcv_ff() sets IDLE on entry. On the overflow path it
then calls isotp_send_fc():

net/can/isotp.c:isotp_rcv_ff() {
...
hrtimer_cancel(&so->rxtimer);
WRITE_ONCE(so->rx.state, ISOTP_IDLE);
...
if (so->rx.len > so->rx.buflen) {
/* send FC frame with overflow status */
isotp_send_fc(sk, ae, ISOTP_FC_OVFLW);
return 1;
}
...
}

isotp_send_fc() reads so->ll.mtu twice:

net/can/isotp.c:isotp_send_fc() {
...
nskb = alloc_skb(so->ll.mtu, gfp_any());
...
skb_put_zero(nskb, so->ll.mtu);
...
}

It also reads so->opt, so->rxfc and so->ll.tx_flags.

There are similar cases:

- isotp_rcv_cf() sets IDLE before check_pad() uses so->opt.
- isotp_rcv_sf() sets IDLE and then reads so->opt.
- isotp_rcv() reads so->opt.flags, so->ll.mtu and so->opt.rx_ext_address
before it takes spin_lock(&so->rx_lock) at all.

isotp_notify(NETDEV_UNREGISTER) calls can_rx_unregister() and clears
so->bound without waiting for a grace period. The comment in
isotp_release() already says this happens "without waiting for in-flight
isotp_rcv() callers to finish".

Frames can still arrive in that window. For example, vxcan_xmit() delivers
to the peer via netif_rx() without checking IFF_UP, and can_rcv() does not
check it either.

So setsockopt() could see bound == 0 with both states IDLE, and overwrite
so->opt, so->ll, so->rxfc or so->tx.ll_dl while isotp_rcv() on another CPU
is still using them. Couldn't that produce an FC frame built from mixed
option sets (alloc_skb() with the old ll.mtu, skb_put_zero() with the new
one), or run padding checks against the wrong options?

Would either of these close the window?

- Do the state check and the option update under
spin_lock_bh(&so->rx_lock), and move isotp_rcv()'s pre-lock option reads
under that lock.
- Wait for a grace period after the unregister.


The caller only harms itself: other sockets and other users are not
affected. At most, a single frame with partly updated options is sent
on the caller's own CAN device.

The regular sequence for a CAN_ISOTP socket is setsockopt() -> bind() ->
sendmsg()/recvmsg() -> close(). When the bound CAN device vanishes, the
application gets an ENODEV error and either closes the socket or binds
it again, optionally after setting new socket options. While a transfer
is still pending, this returns -EAGAIN until the transfer has ended. To
reach the described windows, the caller has to move or remove the CAN
device itself and fire setsockopt() and bind() at its own socket without
waiting for this error. This is a pointless misuse of the socket API
without any gain for the caller.

Covering these windows would need so->rx_lock in setsockopt() and for
all option reads in isotp_rcv() for every received CAN frame, or a
grace period in the netdev notifier, which runs under RTNL and would
bring back the kind of RTNL stall that this series removes. Both add
complexity and cost for every regular user without any benefit.
Therefore the code is not changed for these scenarios.

Best regards,
Oliver