[lvc-project] [PATCH V2 RESEND] media: i2c: ov5640: Fix potential integer overflow in sysclk calculation
Fedor Pchelkin
pchelkin at ispras.ru
Wed Nov 25 21:48:38 MSK 2026
On Mon, 23. Nov 16:41, Daniil Iskhakov wrote:
> From: Daniil Iskhakov <shkiper325 at gmail.com>
>
> The calculation of sysclk uses 32-bit arithmetic because
> sensor->xclk_freq is a 32-bit integer. The intermediate multiplication
> result can overflow 32 bits sysclk variable.
>
> For example, with pll_prediv fixed at 3 (OV5640_PLL_PREDIV), xclk_freq
> set to its maximum of 54MHz (OV5640_XCLK_MAX) and a pll_mult value above
> 238 (the maximum value for pll_mult is 252, defined as
> OV5640_PLL_MULT_MAX), the result exceeds the 32-bit limit.
>
> This overflow causes the 1GHz safety check to fail, as the truncated
> value fits within the 1GHz limit. Consequently, the function returns an
> incorrect frequency, leading to misconfiguration of the sensor.
>
> The expression sysclk / 1000000 > 1000 now also cannot be compiled on
> nios2.
>
> Cast the result of the naturally 32-bit xclk_freq / pll_prediv division
> to u64 instead. This keeps the pre-divider operation 32-bit while the
> potentially overflowing multiplication is performed with 64-bit
> precision. Once the 1GHz safety check has passed, sysclk is guaranteed
> to fit in an unsigned long even on 32-bit systems. Cast it back before
> dividing by sysdiv to avoid another variable 64-bit division. Avoid
> 64-bit division in test.
>
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>
> Fixes: aa2882481cad ("media: ov5640: Adjust the clock based on the expected rate")
> Co-developed-by: Agalakov Daniil <empt1nesss at proton.me>
> Signed-off-by: Agalakov Daniil <empt1nesss at proton.me>
> Signed-off-by: Daniil Iskhakov <shkiper325 at gmail.com>
Вижу на патч был дан Reviewed-by от одного из участников media-сообщества.
При перезасылке патча такие теги можно сохранять, если в патче ничего не
изменилось. [ пересылать ради этого сейчас не надо ]
More information about the lvc-project
mailing list