Linux CAN drivers development
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: mkl@pengutronix.de
Cc: mailhol@kernel.org, socketcan@hartkopp.net, wg@grandegger.com,
	davem@davemloft.net, kernel@pengutronix.de,
	linux-can@vger.kernel.org, 25031212351@stu.xidian.edu.cn,
	stable@vger.kernel.org, byu@xidian.edu.cn,
	wencheng@xidian.edu.cn, kuba@kernel.org
Subject: Re: [PATCH can-next v3 2/6] can: bittiming: can_calc_bittiming(): fix bitrate error calculation on unsigned operands
Date: Wed, 02 Dec 2026 02:07:54 +0000	[thread overview]
Message-ID: <179617727420.3142.17006311535952509360@kernel.org> (raw)
In-Reply-To: <20261130-bittiming-fix-conversion-v3-2-e61f92dc76d0@pengutronix.de>

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

Critical: 0 · High: 0 · 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).

- [Medium] The commit message of "can: bittiming: can_calc_bittiming():
  fix bitrate error calculation on unsigned operands" describes a user-…

--

can: bittiming: can_calc_bittiming(): fix bitrate error calculation on unsigned operands

This commit replaces abs(bt->bitrate - bitrate) with
abs_diff(bt->bitrate, bitrate) in can_calc_bittiming(). The error between
the requested and the computed bitrate is then calculated using only
unsigned arithmetic.

> Both operands are unsigned int, so the subtraction wraps instead of
> becoming negative, and abs() resolves an unsigned int argument to its
> int branch. A wrapped difference is therefore reinterpreted as a small
> positive value instead of the large error it actually represents.
>
> A requested bitrate far above anything the controller can reach then
> passes the CAN_CALC_MAX_ERROR gate. On a dummy_can device with a 160 MHz
> clock, requesting 4294967294 bps reports an error of 0.01%, configures
> 415584 bps and returns success to userspace, where -EINVAL is expected.

[Severity: Medium]
Does this reproducer still work on top of the previous patch in the series,
"can: bittiming: can_calc_bittiming(): fix potential divide-by-zero"?

That patch added this check in can_calc_bittiming(). It runs before brp,
bitrate and bitrate_error are computed:

drivers/net/can/dev/calc_bittiming.c:can_calc_bittiming() {
    ...
		if (check_mul_overflow(tsegall, bt->bitrate, &div))
			continue;
    ...
}

dummy_can sets tseg1_min = 2 and tseg2_min = 2. The loop therefore only
runs while tseg >= 8, so tsegall is always >= 5. Since 5 * 4294967294
overflows u32, every candidate takes the continue.

best_bitrate_error then stays at UINT_MAX and the computed error is about
10000 (100%). That is above CAN_CALC_MAX_ERROR, so the function returns
-EINVAL.

The 415584 bps result (tsegall = 385, brp = 1) only seems reachable when
the multiplication is allowed to wrap, which means without the previous
patch.

More generally, any iteration that reaches the abs_diff() line has
tsegall >= 2, so bt->bitrate <= UINT_MAX / 2 = INT_MAX. The computed
bitrate = freq / (brp * tsegall) is also <= INT_MAX.

So the difference always lies within [-INT_MAX, INT_MAX]. In that range,
abs() through its int branch already returns the exact magnitude, and
abs_diff() gives the same result.

The description of the mechanism also looks inverted. When the subtraction
wraps (bitrate > bt->bitrate), abs() through int recovers the correct
value.

The failing case in the example (4294967294 vs 415584) is a difference
that does not wrap. It is larger than INT_MAX, so it reads as a negative
int, and abs() of that gives 415586.

Could the commit message either say that the issue is only reachable
without the overflow check from the previous patch, or describe this
change as hardening on top of it? The Fixes: tag together with the
current reproducer suggests a live userspace-visible bug in this tree.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130-bittiming-fix-conversion-v3-0-e61f92dc76d0%40pengutronix.de

  reply	other threads:[~2026-12-02  2:07 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 13:06 [PATCH can-next v3 0/6] can: bittiming: fix defects in the userspace bit rate conversion and reject invalid timing constants Marc Kleine-Budde
2026-11-30 13:06 ` [PATCH can-next v3 1/6] can: bittiming: can_calc_bittiming(): fix potential divide-by-zero Marc Kleine-Budde
2026-12-02  2:07   ` netdev-bot+sashiko
2026-11-30 13:06 ` [PATCH can-next v3 2/6] can: bittiming: can_calc_bittiming(): fix bitrate error calculation on unsigned operands Marc Kleine-Budde
2026-12-02  2:07   ` netdev-bot+sashiko [this message]
2026-11-30 13:06 ` [PATCH can-next v3 3/6] can: bittiming: can_update_sample_point(): fix sample point " Marc Kleine-Budde
2026-12-02  2:07   ` netdev-bot+sashiko
2026-11-30 13:06 ` [PATCH can-next v3 4/6] can: bittiming: register_candev(): reject unusable bit-timing constants Marc Kleine-Budde
2026-12-02  2:07   ` netdev-bot+sashiko
2026-11-30 13:06 ` [PATCH can-next v3 5/6] can: bittiming: can_bittiming_const_valid(): reject unusable BRP bit timing constant Marc Kleine-Budde
2026-12-02  2:07   ` netdev-bot+sashiko
2026-11-30 13:06 ` [PATCH can-next v3 6/6] can: bittiming: can_fixup_bittiming(): reject too high values for prop-seg/phase-seg1 Marc Kleine-Budde
2026-12-02  2:07   ` netdev-bot+sashiko

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=179617727420.3142.17006311535952509360@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=25031212351@stu.xidian.edu.cn \
    --cc=byu@xidian.edu.cn \
    --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=socketcan@hartkopp.net \
    --cc=stable@vger.kernel.org \
    --cc=wencheng@xidian.edu.cn \
    --cc=wg@grandegger.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