All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Oliver Hartkopp" <socketcan@hartkopp.net>
Cc: "Oleksij Rempel" <o.rempel@pengutronix.de>,
	linux-can@vger.kernel.org,
	"Marc Kleine-Budde" <mkl@pengutronix.de>,
	"Vincent Mailhol" <mailhol@kernel.org>,
	"Oliver Hartkopp" <socketcan@hartkopp.net>
Subject: Re: [PATCH v7 04/10] can: bcm: fix CAN frame rx/tx statistics
Date: Wed, 08 Jul 2026 15:34:59 +0000	[thread overview]
Message-ID: <20260708153500.66EBF1F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260708-bcm_fixes-v7-4-c78896b342e4@hartkopp.net>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Medium] The statistics reset logic introduces a race condition for RX operations handling RTR frames, directly contradicting the commit's claim of preventing races.

Pre-existing issues:
- [Medium] Concurrent lockless updates to `op->rx_stamp` and `op->rx_ifindex` cause data races and metadata corruption.
--

--- Patch [4]: [PATCH v7 04/10] can: bcm: fix CAN frame rx/tx statistics ---
commit df517d421b8dc860bfb7f92f0c455c27cf5d743e
Author: Oliver Hartkopp <socketcan@hartkopp.net>

can: bcm: fix CAN frame rx/tx statistics

This patch attempts to fix data races in the CAN broadcast manager's receive
and transmit statistics by using atomic operations. It introduces a helper
to update and reset statistics to prevent overflow.

> diff --git a/net/can/bcm.c b/net/can/bcm.c
> index af4bb4182bc29..340b7fc89744e 100644
> --- a/net/can/bcm.c
> +++ b/net/can/bcm.c

[ ... ]

> @@ -290,6 +294,15 @@ static int bcm_proc_show(struct seq_file *m, void *v)
>  }
>  #endif /* CONFIG_PROC_FS */
>  
> +static void bcm_update_stats(struct bcm_op *op)
> +{
> +	/* prevent statistics overflow */
> +	if (atomic_long_inc_return(&op->frames_abs) > LONG_MAX / 100) {
> +		atomic_long_set(&op->frames_filtered, 0);
> +		atomic_long_set(&op->frames_abs, 0);
> +	}
> +}
> +

[Severity: Medium]
If an application sets up an RX op with RX_RTR_FRAME enabled, and multiple
RTR frames arrive concurrently, could there be a race condition here?

Because bcm_update_stats() is called under bcm_tx_lock in bcm_can_tx() and
under bcm_rx_update_lock in bcm_rx_handler(), both functions can execute
for the same RX op when processing an RTR frame.

If multiple CPUs handle RTR frames concurrently, they might execute
bcm_update_stats() using different locks, causing a race on this reset
logic where valid increments are overwritten or frames_filtered temporarily
exceeds frames_abs.

>  /*
>   * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
>   *              of the given bcm tx op
> @@ -344,7 +357,7 @@ static void bcm_can_tx(struct bcm_op *op, struct canfd_frame *cf)
>  	spin_lock_bh(&op->bcm_tx_lock);
>  
>  	if (!err)
> -		op->frames_abs++;
> +		bcm_update_stats(op);
>  
>  	op->currframe++;
>  

[ ... ]

> @@ -748,16 +758,19 @@ static void bcm_rx_handler(struct sk_buff *skb, void *data)
>  	op->rx_stamp = skb->tstamp;
>  	/* save originator for recvfrom() */
>  	op->rx_ifindex = skb->dev->ifindex;

[Severity: Medium]
This isn't a bug introduced by this patch, but does this lockless update of
rx_stamp and rx_ifindex cause data races?

When multiple CAN frames are received simultaneously on different CPUs (e.g.,
listening on all interfaces), these fields are updated outside the protection
of bcm_rx_update_lock.

This can lead to torn reads of the 64-bit timestamp on 32-bit systems, or
metadata mismatches where a CAN frame is sent to userspace with a timestamp
from a different frame.

> -	/* update statistics */
> -	op->frames_abs++;
>  
> -	/* snapshot the flag under lock: op->flags/op->frames may be updated
> -	 * concurrently by bcm_rx_setup().
> +	/* snapshot RTR content under lock: op->flags/op->frames may be
> +	 * updated concurrently by bcm_rx_setup().
>  	 */
>  	spin_lock_bh(&op->bcm_rx_update_lock);
> +
> +	/* update statistics with lock analogue to bcm_rx_changed() callers */
> +	bcm_update_stats(op);
> +
>  	rtr_frame = op->flags & RX_RTR_FRAME;
>  	if (rtr_frame)
>  		memcpy(&rtrframe, op->frames, op->cfsiz);
> +
>  	spin_unlock_bh(&op->bcm_rx_update_lock);
>  
>  	if (rtr_frame) {

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260708-bcm_fixes-v7-0-c78896b342e4@hartkopp.net?part=4

  reply	other threads:[~2026-07-08 15:35 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-08 15:21 [PATCH v7 00/10] can: bcm: collected fixes - final2 :-D Oliver Hartkopp
2026-07-08 15:21 ` Oliver Hartkopp via B4 Relay
2026-07-08 15:21 ` [PATCH v7 01/10] can: bcm: defer rx_op deallocation to workqueue to fix thrtimer UAF Oliver Hartkopp
2026-07-08 15:21   ` Oliver Hartkopp via B4 Relay
2026-07-08 15:36   ` sashiko-bot
2026-07-08 15:21 ` [PATCH v7 02/10] can: bcm: fix lockless bound/ifindex race and silent RX_SETUP failure Oliver Hartkopp
2026-07-08 15:21   ` Oliver Hartkopp via B4 Relay
2026-07-08 15:36   ` sashiko-bot
2026-07-08 15:21 ` [PATCH v7 03/10] can: bcm: add locking when updating filter and timer values Oliver Hartkopp
2026-07-08 15:21   ` Oliver Hartkopp via B4 Relay
2026-07-08 15:21 ` [PATCH v7 04/10] can: bcm: fix CAN frame rx/tx statistics Oliver Hartkopp
2026-07-08 15:21   ` Oliver Hartkopp via B4 Relay
2026-07-08 15:34   ` sashiko-bot [this message]
2026-07-08 15:21 ` [PATCH v7 05/10] can: bcm: add missing rcu list annotations and operations Oliver Hartkopp
2026-07-08 15:21   ` Oliver Hartkopp via B4 Relay
2026-07-08 15:35   ` sashiko-bot
2026-07-08 15:21 ` [PATCH v7 06/10] can: bcm: extend bcm_tx_lock usage for data and timer updates Oliver Hartkopp
2026-07-08 15:21   ` Oliver Hartkopp via B4 Relay
2026-07-08 15:34   ` sashiko-bot
2026-07-08 15:21 ` [PATCH v7 07/10] can: bcm: validate frame length in bcm_rx_setup() for RTR replies Oliver Hartkopp
2026-07-08 15:21   ` Oliver Hartkopp via B4 Relay
2026-07-08 15:21 ` [PATCH v7 08/10] can: bcm: add missing device refcount for CAN filter removal Oliver Hartkopp
2026-07-08 15:21   ` Oliver Hartkopp via B4 Relay
2026-07-08 15:21 ` [PATCH v7 09/10] can: bcm: fix stale rx/tx ops after device removal Oliver Hartkopp
2026-07-08 15:21   ` Oliver Hartkopp via B4 Relay
2026-07-08 15:40   ` sashiko-bot
2026-07-08 15:21 ` [PATCH v7 10/10] can: bcm: fix data race on rx_stamp/rx_ifindex in bcm_rx_handler() Oliver Hartkopp
2026-07-08 15:21   ` Oliver Hartkopp via B4 Relay
2026-07-08 15:48   ` sashiko-bot

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=20260708153500.66EBF1F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=o.rempel@pengutronix.de \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=socketcan@hartkopp.net \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.