From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796173661; b=VqLkfWBmqh4v7DkpG6bZYTt8DoCMPTYVjhyplao5Jo3I7bgWdD4Y3F+LOBV9wDDffEAz seY0RTKytB7qB/fs2Ach0z2XNO+pr9pAWhBU1M+FPjJdxfIgmsdWhC672DGqfAr8AeMKJ I1peSpIKCy3VmTqibtC6o4rV+AjYA6/CtA5egyni4Wn+Dthjnbc4Q+785qSU+rTANf//f hjnJVaGFvTHSbxCU5hYT81C9EA48IszBkHf78bAvbT9c2I0mhOnnH5o39dpdmP88TQ0fI qIqU0d4tQqdXLBjBtaPogctcRJDtFalmtdLK6D3TyGjsvITlqUqu35YYuFb7HOyWZxg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796173661; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=CAzmJSrCgybnVMLrHugHI6nxUDaxTnL/YA8CvfUXWKE=; b=E9MaJRkz1qNr6SM/ONxaoc/9eXAJAJCEuNvzZ5ZmlZ7KvLFaxB19ojvNPripi4K42vbD tMV7vwI5YalstNK2jSoQhDZZ2AYJ0+JJROplCne3BAbZiEVUMbPs2+JY8KEyUBfkDQS7L O0MgGO2S95ehANJeae//dBLemQTFi3F1LsHOV07mzIQjf0eH7N8F6w5HRpZDJtK9Vn3lR Zu3xlmsKHRzqIfvWMR7aAidrpjefv7/ALRrVfdKMJ5r+D9ov4B7CYXQrPKh86Nk5SBQx6 GJPEfEpV0nvPWNunUGIVyinGNG8dMWJyFcQY7ml4ENa7/97/1lsYXa8m/u2DT3MULsg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="QKXm1/qo"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B4AD3606A323; Wed, 2 Dec 2026 01:07:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=QKXm1/qo Received: by smtp.kernel.org (Postfix) with ESMTPSA id 68A431F000C1; Wed, 2 Dec 2026 01:07:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796173661; bh=CAzmJSrCgybnVMLrHugHI6nxUDaxTnL/YA8CvfUXWKE=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=QKXm1/qoqpYe71EVxSQ80fyOjIM2/rki0bKB9YP8AR8gvxlqm5iqrxkMEVKLrKiar 1VuuAyd1esVm7shkH/MEMusvgGAeINPg6PiqKpsklIId32eLbuwtP+uc/ziKCYDSx2 ueVM2f8uxB/8lwuSSH74zauMvTz9hxf+a23WkhslT5rFgul/wmDeL6M84jWLIQf1mx RHT6BokJMVOQTdrf7sgRrunVONhxW9iIIEktRRiLpqQyzoaPhdWunWhQKYZ/jZ3QQp E+ORdzHTirElMo1M4+1PI6KnSHWysCIVGKUkJgrzPivi0CUosbNKlsbnFmgAmu16ZX BA41J/2N7Zzwg== Subject: Re: [PATCH can-next v2 4/4] can: dev: reject unusable bit-timing constants 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, ssureshmsd7@gmail.com, 25031212351@stu.xidian.edu.cn, stable@vger.kernel.org, byu@xidian.edu.cn, wencheng@xidian.edu.cn, kuba@kernel.org Date: Wed, 02 Dec 2026 01:07:39 +0000 Message-ID: <179617365997.3142.9105066348742797062@kernel.org> In-Reply-To: <20261130-bittiming-fix-conversion-v2-4-36e8a0275923@pengutronix.de> References: <20261130-bittiming-fix-conversion-v2-4-36e8a0275923@pengutronix.de> X-sashiko-severity: High Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: linux-can@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider. Critical: 0 · High: 1 · Medium: 0 · 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 patch says it rejects device-supplied bit-timing constants so that a bad USB device "fails to probe instead of taking the kernel… -- can: dev: reject unusable bit-timing constants This commit extends can_bittiming_const_valid() so it also checks the tseg, brp and brp_inc constants, and it limits the bit time to CAN_MAX_BIT_TIME_TQ. It adds can_pwm_const_valid(). register_candev() now rejects a zero clock frequency and validates the FD, XL and PWM constants along a dependency chain, so a USB device that reports unusable values fails to probe. > diff --git a/drivers/net/can/dev/dev.c b/drivers/net/can/dev/dev.c > index 3207c0e08712c..67cd0d29c5f23 100644 > --- a/drivers/net/can/dev/dev.c > +++ b/drivers/net/can/dev/dev.c [ ... ] > @@ -595,15 +596,55 @@ static int can_get_termination(struct net_device *ndev) > return 0; > } > > +/* Upper bound for the bit time in time quanta. The bit times of the > + * currently supported CAN controllers are well below it, the limit keeps the > + * time segment scan in can_calc_bittiming() bounded. > + */ > +#define CAN_MAX_BIT_TIME_TQ 1024 > + > static bool > can_bittiming_const_valid(const struct can_bittiming_const *btc) > { > - if (!btc) > - return true; > + u32 bit_time; > + > + /* Controllers may read the bit timing constants via USB, check the > + * minimum/maximum values and avoid divide by zero in the calculation. > + */ > + if (!btc->tseg1_min || btc->tseg1_min > btc->tseg1_max) > + return false; > + > + if (!btc->tseg2_min || btc->tseg2_min > btc->tseg2_max) > + return false; > > if (!btc->sjw_max) > return false; > > + if (!btc->brp_min || btc->brp_min > btc->brp_max || !btc->brp_inc) > + return false; > + > + if (check_add_overflow(btc->tseg1_max, btc->tseg2_max, &bit_time) || > + check_add_overflow(bit_time, CAN_SYNC_SEG, &bit_time) || > + bit_time > CAN_MAX_BIT_TIME_TQ) > + return false; [Severity: High] Can the divide by zero still be reached with constants that pass these checks? brp_max is only compared against brp_min and has no upper bound. bit_time can still be a power of two, up to 1024. gs_usb takes both brp_max and the clock frequency from the device in gs_usb_probe(): dev->bt_const.brp_max = le32_to_cpu(bt_const.brp_max); ... dev->can.clock.freq = le32_to_cpu(bt_const.fclk_can); The FD dbrp_max in gs_usb and brp_max in ucan also come from the device. The bitrate calculations multiply brp by the bit time as a 32-bit unsigned int and then divide by that product. One example is can_fixup_bittiming(): bt->bitrate = priv->clock.freq / (bt->brp * can_bit_time(bt)); Take fclk_can = 48 MHz, tseg1_max = 767, tseg2_max = 256 (bit time 1024, which passes the new check), brp_max = U32_MAX, brp_inc = 1 and sjw_max >= 128. A user then sets: tq 87381333 prop-seg 383 phase-seg1 384 phase-seg2 256 This rounds to brp = 2^22, and 2^22 * 1024 wraps to 0. can_calc_bittiming() has the same problem: bitrate = priv->clock.freq / (brp * tsegall); Assume bitrate = 1 (bitrate_max is only checked after can_get_bittiming() returns) and clock.freq is between 2^32 - 1024 and 2^32 - 1. On the first loop iteration tsegall is 1024 and brp is 2^22, so brp * tsegall wraps to 0. The final real bitrate calculation in the same function has the same 32-bit product: bt->bitrate = priv->clock.freq / (bt->brp * can_bit_time(bt)); can_bit_time_tqmin(), which can_calc_pwm() uses, computes the same product. Today only dummy_can uses CAN XL, and its constants are fixed. Both paths run from can_changelink() with RTNL held. A bad USB adapter plus a bitrate change by a CAP_NET_ADMIN user would still oops the kernel. The commit message says this case is covered: "Reject unusable values during registration, so that a device or driver handing out such values fails to probe instead of taking the kernel down later." Could this also reject constants where brp_max * (tseg1_max + tseg2_max + CAN_SYNC_SEG) overflows u32, for example with check_mul_overflow()? Alternatively, these products could be computed in 64-bit. > + > + return true; > +} [ ... ] -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130-bittiming-fix-conversion-v2-0-36e8a0275923%40pengutronix.de