Netdev List
 help / color / mirror / Atom feed
From: Oliver Hartkopp <socketcan@hartkopp.net>
To: netdev-bot+sashiko@kernel.org, mkl@pengutronix.de
Cc: netdev@vger.kernel.org, davem@davemloft.net, kuba@kernel.org,
	linux-can@vger.kernel.org, kernel@pengutronix.de,
	zhuling0805@qq.com, stable@vger.kernel.org, mailhol@kernel.org,
	o.rempel@pengutronix.de
Subject: Re: [PATCH net 07/11] can: isotp: reject socket options while a transfer is active
Date: Thu, 19 Nov 2026 09:04:24 +0100	[thread overview]
Message-ID: <8cf5687f-4dd4-42f9-b459-5e192b2bcdd5@hartkopp.net> (raw)
In-Reply-To: <179504036031.3136.7813193141510099541@kernel.org>

In general, a CAN_ISOTP socket whose bound CAN device has been removed
or moved to another network namespace is unbound by isotp_notify() and
reports ENODEV. A regular application sets the socket options once
before bind() and closes the socket in this case. The scenarios with a
concurrent setsockopt() and bind() on such a socket can only be created
on purpose. They do not lead to memory corruption and affect at most
the socket of the attacker and a single, possibly inconsistent, flow
control frame on the attacker's own target CAN device.

On 18.11.26 23:19, netdev-bot+sashiko@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issue(s) to consider.
> 
> Critical: 0 · High: 1 · Medium: 1 · 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).
> 
> - [High] The new -EAGAIN check in isotp_setsockopt_locked()
>    (net/can/isotp.c:1732-1734) assumes, as the commit message says, that…
> - [Medium] isotp_sendmsg() reads so->opt.flags into 'ae'
>    (CAN_ISOTP_EXTEND_ADDR) and 'wait_tx_done' (CAN_ISOTP_WAIT_TX_DONE) at…
> 
> --
> 
> can: isotp: reject socket options while a transfer is active
> 
> This commit adds a check to isotp_setsockopt_locked() that returns -EAGAIN
> while so->tx.state or so->rx.state is not ISOTP_IDLE. This matches the check
> already done in isotp_bind(). The goal is to stop a concurrent setsockopt()
> from changing so->opt, so->ll and so->tx.ll_dl while isotp_sendmsg() is
> still building frames after the device was unregistered.
> 
> Link: https://patch.msgid.link/20261117-can-bcmlock-memcg-v6-7-6826a6227d36@hartkopp.net
> 
>> 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: High]
> 
> The commit message says:
> 
>    "As the transmission and the reception always end by their timeouts at
>    the latest, this state is only temporary."
> 
> Is this true for the rx side?

This is the same finding as for "can: isotp: always start the rx timeout
in ISOTP_WAIT_DATA state". I will send a separate patch that moves the
length checks in isotp_rcv_cf() in front of hrtimer_cancel(), so that
the rx state can not remain in ISOTP_WAIT_DATA without a running rx
timeout. With this patch the statement in the commit message holds for
the rx side as well.

> 
> In isotp_rcv_cf(), the rx watchdog is cancelled before the length checks.
> Two of the early exits that follow return without resetting so->rx.state
> and without re-arming so->rxtimer:
> 
> net/can/isotp.c:isotp_rcv_cf() {
>      ...
> 	hrtimer_cancel(&so->rxtimer);
>      ...
> 	/* CFs are never longer than the FF */
> 	if (cf->len > so->rx.ll_dl)
> 		return 1;
> 
> 	/* CFs have usually the LL_DL length */
> 	if (cf->len < so->rx.ll_dl) {
> 		/* this is only allowed for the last CF */
> 		if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ)
> 			return 1;
> 	}
>      ...
> }
> 
> This leaves the socket in ISOTP_WAIT_DATA with no timer armed, so
> isotp_rx_timer_handler() never runs to set it back to ISOTP_IDLE. While
> the socket is bound, the next SF or FF fixes the state through
> isotp_rcv_sf() or isotp_rcv_ff().
> 
> On NETDEV_UNREGISTER, isotp_notify() unregisters the isotp_rcv filter and
> clears so->bound, but it does not touch so->rx.state:
> 
> 	so->ifindex = 0;
> 	WRITE_ONCE(so->bound, 0);
> 	WRITE_ONCE(so->dev, NULL);
> 
> After that, only isotp_release() can bring rx.state back to IDLE.
> 
> One possible sequence:
> 
>    1) a node on the bus sends a valid FF; isotp_rcv_ff() sets WAIT_DATA
>       and arms rxtimer
>    2) the same sender sends a malformed CF (classic CAN: a 3 byte CF
>       while more than 7 bytes are still missing; CAN FD: an FF with len 8
>       followed by a 12 byte CF)
>    3) isotp_rcv_cf() cancels rxtimer and returns early, still in WAIT_DATA
>    4) the CAN device is unregistered (USB unplug, vcan/vxcan deletion,
>       netns teardown)
> 
> Would every later setsockopt() on this socket then return -EAGAIN until
> the socket is closed?
> 
> The matching check in isotp_bind() would also keep returning -EAGAIN. The
> socket could then be neither reconfigured nor rebound.
> 
> An earlier commit in this series, "can: isotp: always start the rx timeout
> in ISOTP_WAIT_DATA state", seems meant to close this class of stuck
> states. It only adds hrtimer_start() on the success paths of
> isotp_rcv_ff() and isotp_rcv_cf(), and these two early exits are not
> covered.
> 
> The isotp_bind() comment that this check points to makes the same
> assumption:
> 
> 	 * (unbound by NETDEV_UNREGISTER) may still be draining; the FC/echo
> 	 * and RX watchdog timers bound how long this takes. Checked together
> 
> Could these exits re-arm rxtimer or reset rx.state to ISOTP_IDLE? Another
> option would be for isotp_notify() to cancel rxtimer and reset rx.state on
> NETDEV_UNREGISTER.
> 
> [Severity: Medium]
> 
> Does this check fully cover isotp_sendmsg()? The locals derived from the
> options are computed at function entry, before so->tx.state is claimed:
> 
> net/can/isotp.c:isotp_sendmsg() {
>      ...
> 	int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
> 	int wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0;
>      ...
> 	/* so->bound is only checked once above - a wakeup may have
> 	 * unbound/rebound the socket meanwhile => recheck
> 	 */
> 	if (!READ_ONCE(so->bound)) {
>      ...
> }
> 
> The new check only rejects setsockopt() once tx.state != ISOTP_IDLE, so
> the window between function entry and the claim is not covered.
> 
> Here is how that could play out. isotp_sendmsg() is waiting in the claim
> loop (or is preempted) when the device is unregistered. Then
> setsockopt(CAN_ISOTP_OPTS) toggles CAN_ISOTP_EXTEND_ADDR or
> CAN_ISOTP_WAIT_TX_DONE, and bind() succeeds. In that case the bound
> recheck above passes.
> 
> The SF/FF size checks, isotp_fill_dataframe() and isotp_create_fframe()
> would then use the stale ae with the new so->tx.ll_dl and
> so->opt.ext_address. Meanwhile isotp_send_cframe() recomputes ae from the
> new flags:
> 
> 	int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
> 
> Could the FF and its CFs then use different extended addressing layouts
> within one PDU? And would the WAIT_TX_DONE blocking for that sendmsg()
> call still follow the old options?

The observation is correct, but it does not add a new kind of problem.
It requires a concurrent setsockopt() and bind() on a socket whose CAN
device vanished, while another thread waits in isotp_sendmsg(). As you
already noted, there is no out-of-bounds access, and the result is at
most one PDU with an inconsistent extended addressing on the attacker's
own CAN device, which is the same class of effect as for a frame in
flight described above.

Reading ae and wait_tx_done after the claim and the so->bound recheck
would be a cleanup, which I will consider for can-next.

Best regards,
Oliver

> 
> This does not look like an out-of-bounds access. The indices stay bounded
> by so->tx.ll_dl, and the skb is sized from so->ll.mtu, which is read after
> the claim. It does not quite match the commit message's statement that
> "the socket options can only be changed when no transfer is active".
> 
> Would moving the ae and wait_tx_done computation after the claim, next to
> the bound recheck, close this window?
> 
>> +
>>   	switch (optname) {
>>   	case CAN_ISOTP_OPTS:
>>   		memcpy(&so->opt, val, sizeof(so->opt));
> 


  reply	other threads:[~2026-11-19  8:04 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 22:03 [PATCH net 0/11] pull-request: can 2026-11-17 Marc Kleine-Budde
2026-11-17 22:03 ` [PATCH net 01/11] can: bcm: copy the CAN frames outside of lock_sock() Marc Kleine-Budde
2026-11-17 22:12   ` netdev-bot+sinfo
2026-11-18  7:47     ` Oliver Hartkopp
2026-11-17 22:03 ` [PATCH net 02/11] can: bcm: do not remove the procfs entry in bcm_notify() Marc Kleine-Budde
2026-11-17 22:03 ` [PATCH net 03/11] can: raw: use a private mutex instead of lock_sock() Marc Kleine-Budde
2026-11-17 22:03 ` [PATCH net 04/11] can: isotp: " Marc Kleine-Budde
2026-11-17 22:03 ` [PATCH net 05/11] can: bcm: " Marc Kleine-Budde
2026-11-17 22:03 ` [PATCH net 06/11] can: isotp: always start the rx timeout in ISOTP_WAIT_DATA state Marc Kleine-Budde
2026-11-18 22:19   ` netdev-bot+sashiko
2026-11-19  7:53     ` Oliver Hartkopp
2026-11-19 17:38       ` Oliver Hartkopp
2026-11-17 22:03 ` [PATCH net 07/11] can: isotp: reject socket options while a transfer is active Marc Kleine-Budde
2026-11-18 22:19   ` netdev-bot+sashiko
2026-11-19  8:04     ` Oliver Hartkopp [this message]
2026-11-17 22:03 ` [PATCH net 08/11] can: netlink: fix static FD-NON-ISO mode validation Marc Kleine-Budde
2026-11-18 22:19   ` netdev-bot+sashiko
2026-11-19  8:13     ` Oliver Hartkopp
2026-11-17 22:03 ` [PATCH net 09/11] can: netlink: fix IFLA_CAN_BITRATE_MAX attribute sizing Marc Kleine-Budde
2026-11-17 22:03 ` [PATCH net 10/11] can: netlink: fix CAN XL PWM " Marc Kleine-Budde
2026-11-17 22:03 ` [PATCH net 11/11] can: gs_usb: gs_can_start_xmit(): don't leak uninitialized memory over USB Marc Kleine-Budde
2026-11-19 16:53 ` [PATCH net 0/11] pull-request: can 2026-11-17 Oliver Hartkopp
2026-11-19 17:32   ` 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=8cf5687f-4dd4-42f9-b459-5e192b2bcdd5@hartkopp.net \
    --to=socketcan@hartkopp.net \
    --cc=davem@davemloft.net \
    --cc=kernel@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=netdev-bot+sashiko@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=o.rempel@pengutronix.de \
    --cc=stable@vger.kernel.org \
    --cc=zhuling0805@qq.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