[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <12DB1EC7-28CA-4792-B411-E09918B9C828@suse.de>
Date: Wed, 6 Sep 2023 11:39:59 +0800
From: Coly Li <colyli@...e.de>
To: Rand Deeb <rand.sec96@...il.com>
Cc: Kent Overstreet <kent.overstreet@...il.com>,
Bcache Linux <linux-bcache@...r.kernel.org>,
linux-kernel@...r.kernel.org, deeb.rand@...fident.ru,
lvc-project@...uxtesting.org, voskresenski.stanislav@...fident.ru
Subject: Re: [PATCH] bcache: prevent potential division by zero error
> 2023年9月6日 09:22,Rand Deeb <rand.sec96@...il.com> 写道:
>
> In SHOW(), the variable 'n' is of type 'size_t.' While there is a
> conditional check to verify that 'n' is not equal to zero before
> executing the 'do_div' macro, concerns arise regarding potential
> division by zero error in 64-bit environments.
>
> The concern arises when 'n' is 64 bits in size, greater than zero, and
> the lower 32 bits of it are zeros. In such cases, the conditional check
> passes because 'n' is non-zero, but the 'do_div' macro casts 'n' to
> 'uint32_t,' effectively truncating it to its lower 32 bits.
> Consequently, the 'n' value becomes zero.
>
> To fix this potential division by zero error and ensure precise
> division handling, this commit replaces the 'do_div' macro with
> div64_u64(). div64_u64() is designed to work with 64-bit operands,
> guaranteeing that division is performed correctly.
>
> This change enhances the robustness of the code, ensuring that division
> operations yield accurate results in all scenarios, eliminating the
> possibility of division by zero, and improving compatibility across
> different 64-bit environments.
>
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>
> Signed-off-by: Rand Deeb <rand.sec96@...il.com>
Thanks, added into my for-next queue.
Coly Li
> ---
> drivers/md/bcache/sysfs.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
> index 554e3afc9b68..ca3e2f000cd4 100644
> --- a/drivers/md/bcache/sysfs.c
> +++ b/drivers/md/bcache/sysfs.c
> @@ -1078,7 +1078,7 @@ SHOW(__bch_cache)
> sum += INITIAL_PRIO - cached[i];
>
> if (n)
> - do_div(sum, n);
> + sum = div64_u64(sum, n);
>
> for (i = 0; i < ARRAY_SIZE(q); i++)
> q[i] = INITIAL_PRIO - cached[n * (i + 1) /
> --
> 2.34.1
>
Powered by blists - more mailing lists