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 v4 6/6] can: bittiming: register_candev(): reject unusable bit-timing constants
Date: Fri, 04 Dec 2026 03:51:46 +0000	[thread overview]
Message-ID: <179635630671.3142.14619902516001059445@kernel.org> (raw)
In-Reply-To: <20261202-bittiming-fix-conversion-v4-6-fae7aaaf3191@pengutronix.de>

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] can_pwm_const_valid() (drivers/net/can/dev/dev.c:643-659) checks
  `pwms_min + pwml_min` for overflow but puts no bound on `pwms_max +…

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch
  hardens can_calc_pwm() against bad registered constants and leaves a…

--

can: bittiming: register_candev(): reject unusable bit-timing constants

This commit tightens can_bittiming_const_valid() so it rejects zero or
inverted tseg and brp ranges, a zero brp_inc, bit times above
CAN_BIT_TIME_TQ_MAX and a brp_max above CAN_BITTIMING_CONST_BRP_MAX. It also
adds can_pwm_const_valid() for the CAN XL PWM constants. register_candev()
now rejects a zero clock frequency and validates the constants along the
CC -> FD -> XL -> PWM chain.

> diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c
> index 3207c0e08712c..50fd17035d8f6 100644
> --- a/drivers/net/can/dev/dev.c
> +++ b/drivers/net/can/dev/dev.c

[ ... ]

> @@ -595,15 +596,65 @@ static int can_get_termination(struct net_device *ndev)

[ ... ]

> +static bool
> +can_pwm_const_valid(const struct can_pwm_const *pwm_const)
> +{
> +	u32 shortest_symbol;
> +
> +	if (pwm_const->pwms_min > pwm_const->pwms_max ||
> +	    pwm_const->pwml_min > pwm_const->pwml_max ||
> +	    pwm_const->pwmo_min > pwm_const->pwmo_max)
> +		return false;

[Severity: Low]
Should the maxima be bounded here as well? Only pwms_min + pwml_min is
checked for overflow. can_pwm_changelink() limits the PWMS and PWML values
from user space only to [pwms_min, pwms_max] and [pwml_min, pwml_max], and
then passes them to can_validate_pwm_bittiming():

drivers/net/can/dev/bittiming.c:can_validate_pwm_bittiming() {
    ...
	if (xl_bit_time_tqmin % (pwm->pwms + pwm->pwml) != 0) {
    ...
}

That sum is computed in u32. Take clock.freq = 1 GHz, pwms = 10 and
pwml = 2^32 - 10. The earlier checks all pass:

  pwms_ns + pwml_ns wraps to 0, so it passes the CAN_PWM_NS_MAX check
  pwms_ns >= CAN_PWM_DECODE_NS
  pwms < pwml
  pwml_ns - pwms_ns = 2^32 - 20 >= 2 * CAN_PWM_DECODE_NS

Then pwm->pwms + pwm->pwml wraps to 0.

If a driver registers a pwm_const with pwms_max + pwml_max >= 2^32, can
this divide by zero?

No in-tree driver has such constants today. dummy_can is the only
pwm_const user, and its values are small and static.

Would it be enough to reject a pwms_max + pwml_max that overflows, or to
cap each maximum at CAN_BIT_TIME_TQ_MAX * CAN_BITTIMING_CONST_BRP_MAX?

> +
> +	/* can_calc_pwm() divides by the shortest PWM symbol. */
> +	if (check_add_overflow(pwm_const->pwms_min, pwm_const->pwml_min,
> +			       &shortest_symbol) || !shortest_symbol)
> +		return false;

[Severity: High]
This is a pre-existing issue, but this check is meant to protect the
divisions in can_calc_pwm(), and that function has a second divisor that
can still be zero:

drivers/net/can/dev/calc_bittiming.c:can_calc_pwm() {
	u32 xl_tqmin = can_bit_time_tqmin(&priv->xl.data_bittiming);
	u32 xl_ns = can_tqmin_to_ns(xl_tqmin, priv->clock.freq);
    ...
	int pwm_per_bit_max = xl_tqmin / (pwm_const->pwms_min + pwm_const->pwml_min);
    ...
	for (pwm_per_bit = DIV_ROUND_UP(xl_ns, CAN_PWM_NS_MAX);
	     pwm_per_bit <= pwm_per_bit_max; pwm_per_bit++)
		if (xl_tqmin % pwm_per_bit == 0)
			break;
    ...
}

Suppose priv->xl.data_bittiming is all zero while CAN_CTRLMODE_XL_TMS is
set. Then xl_tqmin and xl_ns are both 0, so pwm_per_bit and
pwm_per_bit_max both start at 0. The loop body then computes 0 % 0.

Can that state be reached? With the interface down,
can_ctrlmode_changelink() commits the new flags before any bit timing is
validated:

	priv->ctrlmode &= ~cm->mask;
	priv->ctrlmode |= maskedflags;

Take a changelink request with mask = flags = XL | XL_TMS, plus
IFLA_CAN_BITTIMING and IFLA_CAN_XL_DATA_BITTIMING. If it later fails in
can_get_bittiming() or in the XL can_dbt_changelink(), can_changelink()
returns the error without rolling back ctrlmode. XL_TMS stays set and
xl.data_bittiming stays zeroed.

A second changelink can then carry an IFLA_INFO_DATA with no CTRLMODE,
BITTIMING or XL_PWM attributes. That request reaches:

can_changelink()
  can_pwm_changelink()
    can_calc_pwm()

can_pwm_changelink() only checks XL_TMS and pwm_const before calling it:

	if (!(priv->ctrlmode & CAN_CTRLMODE_XL_TMS))
		return 0;
    ...
	if (!pwm_nla)
		return can_calc_pwm(dev, extack);

Triggering this needs CAP_NET_ADMIN and a driver that sets xl.pwm_const
and supports XL_TMS, such as dummy_can in-tree. The divide error happens
while RTNL is held.

Should can_pwm_changelink() or can_calc_pwm() check that the XL data bit
timing has been configured? Alternatively, should can_changelink() restore
ctrlmode when a later step fails?

> +
> +	return true;
>  }

[ ... ]

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

      reply	other threads:[~2026-12-04  3:51 UTC|newest]

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

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=179635630671.3142.14619902516001059445@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