Linux CAN drivers development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Oliver Hartkopp" <socketcan@hartkopp.net>
Cc: "Vincent Mailhol" <mailhol@kernel.org>,
	linux-can@vger.kernel.org,
	"Oliver Hartkopp" <socketcan@hartkopp.net>,
	"Oleksij Rempel" <o.rempel@pengutronix.de>,
	"Marc Kleine-Budde" <mkl@pengutronix.de>
Subject: Re: [PATCH v3 4/7] can: bcm: fix CAN frame rx/tx statistics
Date: Tue, 07 Jul 2026 10:37:14 +0000	[thread overview]
Message-ID: <20260707103714.9A6AB1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260707-bcm_fixes-v3-4-84600ca6d889@hartkopp.net>

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

New issues:
- [Medium] A Time-Of-Check to Time-Of-Use (TOCTOU) race during statistics reset can permanently break the invariant `frames_filtered <= frames_abs`.

Pre-existing issues:
- [High] Missing RCU publish/subscribe barriers for lockless list operations, risking uninitialized memory reads in procfs.
--

--- Patch [4]: [PATCH v3 4/7] can: bcm: fix CAN frame rx/tx statistics ---
commit faeade145d2ba64627aca2f6af5f2b0b8ad0d933
Author: Oliver Hartkopp <socketcan@hartkopp.net>

can: bcm: fix CAN frame rx/tx statistics

KCSAN detected a data race within the bcm_rx_handler() when two CAN frames
have been simultaneously received and processed in a single rx op by two
different CPUs. Use atomic operations with (signed) long data types to
access the statistics in the hot path.

> diff --git a/net/can/bcm.c b/net/can/bcm.c
> index fd548c6eaa913..0378e31ea10ed 100644
> --- a/net/can/bcm.c
> +++ b/net/can/bcm.c
[ ... ]
> @@ -280,7 +283,8 @@ static int bcm_proc_show(struct seq_file *m, void *v)
>  			seq_printf(m, "t2=%lld ",
>  				   (long long)ktime_to_us(op->kt_ival2));
>  
> -		seq_printf(m, "# sent %ld\n", op->frames_abs);
> +		seq_printf(m, "# sent %ld\n",
> +			   atomic_long_read(&op->frames_abs));
>  	}

[Severity: High]
This is a pre-existing issue, but does this function safely traverse
the lockless tx_ops list?

bcm_proc_show() traverses bo->tx_ops via list_for_each_entry() instead of
list_for_each_entry_rcu(), which lacks the necessary subscribe barriers.

Additionally, neither bcm_tx_setup() nor bcm_rx_setup() use list_add_rcu()
when inserting into these lists:

bcm_tx_setup()
    list_add(&op->list, &bo->tx_ops);

bcm_rx_setup()
    list_add(&op->list, &bo->rx_ops);

Could reading /proc/net/can/bcm/<inode> while another thread concurrently
sets up a BCM message on the same socket result in traversing uninitialized
memory due to missing RCU publish/subscribe barriers?

>  	seq_putc(m, '\n');
>  
> @@ -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]
Could this non-atomic reset allow other threads to interleave their
increments and permanently break the statistic invariant?

If Thread A hits the LONG_MAX / 100 threshold and begins resetting the stats:

Thread A
bcm_update_stats()
    atomic_long_set(&op->frames_filtered, 0);
    atomic_long_set(&op->frames_abs, 0);

And Thread B is concurrently processing bcm_rx_changed():

Thread B
bcm_rx_changed()
    if (atomic_long_read(&op->frames_abs))
        atomic_long_inc(&op->frames_filtered);

If Thread B reads a non-zero frames_abs, then Thread A resets both to 0,
and finally Thread B increments frames_filtered, wouldn't frames_filtered
become 1 while frames_abs is 0?

This would leave frames_filtered greater than frames_abs, causing the procfs
output to display negative reduction percentages.

> +
>  /*
>   * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
>   *              of the given bcm tx op
[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260707-bcm_fixes-v3-0-84600ca6d889@hartkopp.net?part=4

  reply	other threads:[~2026-07-07 10:37 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-07 10:16 [PATCH v3 0/7] can: bcm: collected fixes Oliver Hartkopp via B4 Relay
2026-07-07 10:16 ` [PATCH v3 1/7] can: bcm: defer rx_op deallocation to workqueue to fix thrtimer UAF Oliver Hartkopp via B4 Relay
2026-07-07 10:27   ` sashiko-bot
2026-07-07 11:11     ` Oliver Hartkopp
2026-07-07 10:16 ` [PATCH v3 2/7] can: bcm: fix lockless bound/ifindex race and silent RX_SETUP failure Oliver Hartkopp via B4 Relay
2026-07-07 10:29   ` sashiko-bot
2026-07-07 11:16     ` Oliver Hartkopp
2026-07-07 10:16 ` [PATCH v3 3/7] can: bcm: add locking when updating filter and timer values Oliver Hartkopp via B4 Relay
2026-07-07 10:35   ` sashiko-bot
2026-07-07 11:32     ` Oliver Hartkopp
2026-07-07 10:16 ` [PATCH v3 4/7] can: bcm: fix CAN frame rx/tx statistics Oliver Hartkopp via B4 Relay
2026-07-07 10:37   ` sashiko-bot [this message]
2026-07-07 12:23     ` Oliver Hartkopp
2026-07-07 10:16 ` [PATCH v3 5/7] can: bcm: add missing rcu list annotations and operations Oliver Hartkopp via B4 Relay
2026-07-07 10:42   ` sashiko-bot
2026-07-07 12:26     ` Oliver Hartkopp
2026-07-07 10:16 ` [PATCH v3 6/7] can: bcm: extend bcm_tx_lock usage for data and timer updates Oliver Hartkopp via B4 Relay
2026-07-07 12:33   ` Marc Kleine-Budde
2026-07-07 12:38     ` Oliver Hartkopp
2026-07-07 10:16 ` [PATCH v3 7/7] can: bcm: validate frame length in bcm_rx_setup() for RTR replies Oliver Hartkopp via B4 Relay

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=20260707103714.9A6AB1F000E9@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox