Skip to contentSkip to conversation

fs: btrfs: fix potential overflow

PATCH Superseded Brian Norris 2 participants 4 messages Aug 24, 2014 – Aug 26, 2014 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: btrfs: fix potential overflow

It looks like this intended to be 64-bit arithmetic, but it's actually performed as 32-bit. Fix that. (Note that 'increment' was being initialized twice, so this patch removes one of those.)

Caught by Coverity Scan (CID 1201422).

---
Untested

 fs/btrfs/scrub.c |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
fs/btrfs/scrub.c +6 -6
index b6d198f5181e..b1a8127737b4 100644
@@ -2320,26 +2320,26 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
 	do_div(nstripes, map->stripe_len);
 	if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
 		offset = map->stripe_len * num;
-		increment = map->stripe_len * map->num_stripes;
+		increment *= map->num_stripes;
 		mirror_num = 1;
 	} else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
 		int factor = map->num_stripes / map->sub_stripes;
 		offset = map->stripe_len * (num / map->sub_stripes);
-		increment = map->stripe_len * factor;
+		increment *= factor;
 		mirror_num = num % map->sub_stripes + 1;
 	} else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
-		increment = map->stripe_len;
+		increment *= 1;
 		mirror_num = num % map->num_stripes + 1;
 	} else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
-		increment = map->stripe_len;
+		increment *= 1;
 		mirror_num = num % map->num_stripes + 1;
 	} else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
 				BTRFS_BLOCK_GROUP_RAID6)) {
 		get_raid56_logic_offset(physical, num, map, &offset);
-		increment = map->stripe_len * nr_data_stripes(map);
+		increment *= nr_data_stripes(map);
 		mirror_num = 1;
 	} else {
-		increment = map->stripe_len;
+		increment *= 1;
 		mirror_num = 1;
 	}
 
Signature · 1 line
1.7.9.5
#1 lore raw

2014-08-24 8:41 GMT+03:00 Brian Norris <[email protected]>:

Quoted 23 lines

It looks like this intended to be 64-bit arithmetic, but it's actually performed as 32-bit. Fix that. (Note that 'increment' was being initialized twice, so this patch removes one of those.)

Caught by Coverity Scan (CID 1201422).

---
Untested

 fs/btrfs/scrub.c |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
fs/btrfs/scrub.c +1 -1
index b6d198f5181e..b1a8127737b4 100644
@@ -2320,26 +2320,26 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
        do_div(nstripes, map->stripe_len);
        if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
                offset = map->stripe_len * num;
-               increment = map->stripe_len * map->num_stripes;
+               increment *= map->num_stripes;

fs/btrfs/scrub.c static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, · line 2331

Quoted 10 lines
fs/btrfs/scrub.c +2 -2
                mirror_num = 1;
        } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
                int factor = map->num_stripes / map->sub_stripes;
                offset = map->stripe_len * (num / map->sub_stripes);
-               increment = map->stripe_len * factor;
+               increment *= factor;
                mirror_num = num % map->sub_stripes + 1;
        } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
-               increment = map->stripe_len;
+               increment *= 1;

Why? I think multiple by one is useless or i miss something?

fs/btrfs/scrub.c static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, · line 2334

Quoted 4 lines
fs/btrfs/scrub.c +1 -1
                mirror_num = num % map->num_stripes + 1;
        } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
-               increment = map->stripe_len;
+               increment *= 1;

Same as above.

fs/btrfs/scrub.c static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, · line 2342

Quoted 10 lines
fs/btrfs/scrub.c +2 -2
                mirror_num = num % map->num_stripes + 1;
        } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
                                BTRFS_BLOCK_GROUP_RAID6)) {
                get_raid56_logic_offset(physical, num, map, &offset);
-               increment = map->stripe_len * nr_data_stripes(map);
+               increment *= nr_data_stripes(map);
                mirror_num = 1;
        } else {
-               increment = map->stripe_len;
+               increment *= 1;

Same.

Quoted 10 lines
fs/btrfs/scrub.c +0 -1
                mirror_num = 1;
        }
 
--

1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Have a nice day,
Timofey.
#2 lore raw
Quoted 12 lines · Timofey Titovets

2014-08-24 8:41 GMT+03:00 Brian Norris <[email protected]>:

Quoted 11 lines
fs/btrfs/scrub.c +1 -1
@@ -2320,26 +2320,26 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
        do_div(nstripes, map->stripe_len);
        if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
                offset = map->stripe_len * num;
-               increment = map->stripe_len * map->num_stripes;
+               increment *= map->num_stripes;
                mirror_num = 1;
        } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
                int factor = map->num_stripes / map->sub_stripes;
Quoted 8 lines

fs/btrfs/scrub.c static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, · line 2331

Quoted 7 lines
fs/btrfs/scrub.c +2 -2
                offset = map->stripe_len * (num / map->sub_stripes);
-               increment = map->stripe_len * factor;
+               increment *= factor;
                mirror_num = num % map->sub_stripes + 1;
        } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
-               increment = map->stripe_len;
+               increment *= 1;

Why? I think multiple by one is useless or i miss something?

A misguided attempt to keep the code structure intact. I'll drop these for v2.

[snip]

Thanks,
Brian

#3 lore raw

PATCH v2 1/1 fs: btrfs: fix potential overflow

In reply to Brian Norris

It looks like this intended to be 64-bit arithmetic, but it's actually performed as 32-bit. Fix that. (Note that 'increment' was being initialized twice, so this patch removes one of those.)

Caught by Coverity Scan (CID 1201422).

---
v2: remove useless multiplication-by-one

Untested

 fs/btrfs/scrub.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
fs/btrfs/scrub.c +3 -6
index b6d198f5181e..e38933ebf97f 100644
@@ -2320,26 +2320,23 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
 	do_div(nstripes, map->stripe_len);
 	if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
 		offset = map->stripe_len * num;
-		increment = map->stripe_len * map->num_stripes;
+		increment *= map->num_stripes;
 		mirror_num = 1;
 	} else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
 		int factor = map->num_stripes / map->sub_stripes;
 		offset = map->stripe_len * (num / map->sub_stripes);
-		increment = map->stripe_len * factor;
+		increment *= factor;
 		mirror_num = num % map->sub_stripes + 1;
 	} else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
-		increment = map->stripe_len;
 		mirror_num = num % map->num_stripes + 1;
 	} else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
-		increment = map->stripe_len;
 		mirror_num = num % map->num_stripes + 1;
 	} else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
 				BTRFS_BLOCK_GROUP_RAID6)) {
 		get_raid56_logic_offset(physical, num, map, &offset);
-		increment = map->stripe_len * nr_data_stripes(map);
+		increment *= nr_data_stripes(map);
 		mirror_num = 1;
 	} else {
-		increment = map->stripe_len;
 		mirror_num = 1;
 	}
 
Signature · 1 line
1.9.1