* [PATCH] btrfs: Avoid potential integer overflow when left-shifting 32-bit int
@ 2023-04-06 19:24 Nur Hussein
2023-04-07 0:35 ` Qu Wenruo
0 siblings, 1 reply; 4+ messages in thread
From: Nur Hussein @ 2023-04-06 19:24 UTC (permalink / raw)
To: clm, josef, dsterba, linux-btrfs, linux-kernel; +Cc: Nur Hussein
In scrub_stripe(), the 32-bit signed value returned by the
nr_data_stripes(map) function call should be cast to u64
before being shifted left by BTRFS_STRIPE_LEN_SHIFT (16),
as a cautionary measure to avoid potential overflows. We
then assign it to a u64 value anyway, so a cast before a
shift seems prudent.
Signed-off-by: Nur Hussein <hussein@unixcat.org>
---
fs/btrfs/scrub.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index ccb4f58ae307..4de1665fcd52 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -2187,7 +2187,7 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
/* Initialize @offset in case we need to go to out: label */
get_raid56_logic_offset(physical, stripe_index, map, &offset, NULL);
- increment = nr_data_stripes(map) << BTRFS_STRIPE_LEN_SHIFT;
+ increment = (u64)nr_data_stripes(map) << BTRFS_STRIPE_LEN_SHIFT;
/*
* Due to the rotation, for RAID56 it's better to iterate each stripe
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] btrfs: Avoid potential integer overflow when left-shifting 32-bit int
2023-04-06 19:24 [PATCH] btrfs: Avoid potential integer overflow when left-shifting 32-bit int Nur Hussein
@ 2023-04-07 0:35 ` Qu Wenruo
2023-04-07 13:51 ` Nur Hussein
2023-05-02 16:11 ` David Sterba
0 siblings, 2 replies; 4+ messages in thread
From: Qu Wenruo @ 2023-04-07 0:35 UTC (permalink / raw)
To: Nur Hussein, clm, josef, dsterba, linux-btrfs, linux-kernel
On 2023/4/7 03:24, Nur Hussein wrote:
> In scrub_stripe(), the 32-bit signed value returned by the
> nr_data_stripes(map) function call should be cast to u64
> before being shifted left by BTRFS_STRIPE_LEN_SHIFT (16),
> as a cautionary measure to avoid potential overflows. We
> then assign it to a u64 value anyway, so a cast before a
> shift seems prudent.
I'd say it's a little overkilled.
For nr_data_stripes(), it's at most hundreds of stripes (which is
already insane).
Even with 16 bits left shift, we need to get 2 ** 16 stripes to overflow
32bits.
Thanks,
Qu
>
> Signed-off-by: Nur Hussein <hussein@unixcat.org>
> ---
> fs/btrfs/scrub.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
> index ccb4f58ae307..4de1665fcd52 100644
> --- a/fs/btrfs/scrub.c
> +++ b/fs/btrfs/scrub.c
> @@ -2187,7 +2187,7 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
>
> /* Initialize @offset in case we need to go to out: label */
> get_raid56_logic_offset(physical, stripe_index, map, &offset, NULL);
> - increment = nr_data_stripes(map) << BTRFS_STRIPE_LEN_SHIFT;
> + increment = (u64)nr_data_stripes(map) << BTRFS_STRIPE_LEN_SHIFT;
>
> /*
> * Due to the rotation, for RAID56 it's better to iterate each stripe
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] btrfs: Avoid potential integer overflow when left-shifting 32-bit int
2023-04-07 0:35 ` Qu Wenruo
@ 2023-04-07 13:51 ` Nur Hussein
2023-05-02 16:11 ` David Sterba
1 sibling, 0 replies; 4+ messages in thread
From: Nur Hussein @ 2023-04-07 13:51 UTC (permalink / raw)
To: Qu Wenruo; +Cc: clm, josef, dsterba, linux-btrfs, linux-kernel
On Fri, Apr 07, 2023 at 08:35:40AM +0800, Qu Wenruo wrote:
>
>
> On 2023/4/7 03:24, Nur Hussein wrote:
> > In scrub_stripe(), the 32-bit signed value returned by the
> > nr_data_stripes(map) function call should be cast to u64
> > before being shifted left by BTRFS_STRIPE_LEN_SHIFT (16),
> > as a cautionary measure to avoid potential overflows. We
> > then assign it to a u64 value anyway, so a cast before a
> > shift seems prudent.
>
> I'd say it's a little overkilled.
>
> For nr_data_stripes(), it's at most hundreds of stripes (which is already
> insane).
> Even with 16 bits left shift, we need to get 2 ** 16 stripes to overflow
> 32bits.
Perhaps so, but it was flagged by Coverity, and it's a little safer with
the cast, with no cost. It's up to y'all if you want it though.
- Nur
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] btrfs: Avoid potential integer overflow when left-shifting 32-bit int
2023-04-07 0:35 ` Qu Wenruo
2023-04-07 13:51 ` Nur Hussein
@ 2023-05-02 16:11 ` David Sterba
1 sibling, 0 replies; 4+ messages in thread
From: David Sterba @ 2023-05-02 16:11 UTC (permalink / raw)
To: Qu Wenruo; +Cc: Nur Hussein, clm, josef, dsterba, linux-btrfs, linux-kernel
On Fri, Apr 07, 2023 at 08:35:40AM +0800, Qu Wenruo wrote:
> On 2023/4/7 03:24, Nur Hussein wrote:
> > In scrub_stripe(), the 32-bit signed value returned by the
> > nr_data_stripes(map) function call should be cast to u64
> > before being shifted left by BTRFS_STRIPE_LEN_SHIFT (16),
> > as a cautionary measure to avoid potential overflows. We
> > then assign it to a u64 value anyway, so a cast before a
> > shift seems prudent.
>
> I'd say it's a little overkilled.
>
> For nr_data_stripes(), it's at most hundreds of stripes (which is
> already insane).
> Even with 16 bits left shift, we need to get 2 ** 16 stripes to overflow
> 32bits.
I don't like adding casts unless really necessary. That the values won't
overflow is what we know because there are other constraints.
In this case I'd rather switch the return type of nr_data_stripes to u32
as the return value from the helper is used for shifts by
BTRFS_STRIPE_LEN_SHIFT in several places.
For the struct map_lookup we should use u32 for all values that simply
count something and there's no semantic value for -1 (like uninitialzed
or invalid). I did a quick grep and the values are assigned from
unsigned types in most if not all cases.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-05-02 16:17 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-06 19:24 [PATCH] btrfs: Avoid potential integer overflow when left-shifting 32-bit int Nur Hussein
2023-04-07 0:35 ` Qu Wenruo
2023-04-07 13:51 ` Nur Hussein
2023-05-02 16:11 ` David Sterba
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox