Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: mkl@pengutronix.de
Cc: netdev@vger.kernel.org, davem@davemloft.net, kuba@kernel.org,
	linux-can@vger.kernel.org, kernel@pengutronix.de,
	u.kleine-koenig@baylibre.com, stable@vger.kernel.org,
	mailhol@kernel.org, robh@kernel.org,
	linux-kernel@vger.kernel.org, jeanmichel.hautbois@yoseli.org
Subject: Re: [PATCH net 07/12] can: isotp: reject socket options while a transfer is active
Date: Wed, 25 Nov 2026 08:58:35 +0000	[thread overview]
Message-ID: <179559711531.3135.15295307789383806315@kernel.org> (raw)
In-Reply-To: <20261124085757.1093632-8-mkl@pengutronix.de>

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()…

--

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@hartkopp.net

> 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?

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?

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.

If so, the commit message claim that "the socket options can only be
changed when no transfer is active" would also hold on the RX side.

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124085757.1093632-2-mkl%40pengutronix.de

  reply	other threads:[~2026-11-25  8:58 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  8:51 [PATCH net 0/12] pull-request: can 2026-11-24 Marc Kleine-Budde
2026-11-24  8:51 ` [PATCH net 01/12] can: bcm: copy the CAN frames outside of lock_sock() Marc Kleine-Budde
2026-11-24  9:04   ` netdev-bot+sinfo
2026-11-24  8:51 ` [PATCH net 02/12] can: bcm: do not remove the procfs entry in bcm_notify() Marc Kleine-Budde
2026-11-24  8:51 ` [PATCH net 03/12] can: raw: use a private mutex instead of lock_sock() Marc Kleine-Budde
2026-11-24  8:51 ` [PATCH net 04/12] can: isotp: " Marc Kleine-Budde
2026-11-24  8:51 ` [PATCH net 05/12] can: bcm: " Marc Kleine-Budde
2026-11-24  8:51 ` [PATCH net 06/12] can: isotp: always start the rx timeout in ISOTP_WAIT_DATA state Marc Kleine-Budde
2026-11-24  8:51 ` [PATCH net 07/12] can: isotp: reject socket options while a transfer is active Marc Kleine-Budde
2026-11-25  8:58   ` netdev-bot+sashiko [this message]
2026-11-24  8:51 ` [PATCH net 08/12] can: netlink: fix static FD-NON-ISO mode validation Marc Kleine-Budde
2026-11-24  8:51 ` [PATCH net 09/12] can: netlink: fix IFLA_CAN_BITRATE_MAX attribute sizing Marc Kleine-Budde
2026-11-24  8:51 ` [PATCH net 10/12] can: netlink: fix CAN XL PWM " Marc Kleine-Budde
2026-11-24  8:51 ` [PATCH net 11/12] can: gs_usb: gs_can_start_xmit(): don't leak uninitialized memory over USB Marc Kleine-Budde
2026-11-24  8:51 ` [PATCH net 12/12] can: flexcan: Fix probing for m68k/coldfire Marc Kleine-Budde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179559711531.3135.15295307789383806315@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=davem@davemloft.net \
    --cc=jeanmichel.hautbois@yoseli.org \
    --cc=kernel@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=netdev@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=u.kleine-koenig@baylibre.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox