btrfs/raid56: fix and cleanup some error paths
Subsystems (2)
PATCH 1/1 btrfs/raid56: fix and cleanup some error paths
The alloc_rbio() frees "raid_map" and "bbio" on error, so there is a potential double free bug in raid56_parity_write(). The raid56_parity_write() and raid56_parity_recover() functions should still free "raid_map" and "bbio" on error if other errors occur though, so I have added some more calls to kfree().
fs/btrfs/raid56.c +4 -6
@@ -1687,11 +1687,8 @@ int raid56_parity_write(struct btrfs_root *root, struct bio *bio,struct blk_plug_cb *cb; rbio =alloc_rbio (root, bbio, raid_map, stripe_len); -if (IS_ERR (rbio)) { -kfree (raid_map); -kfree (bbio); +if (IS_ERR (rbio))return PTR_ERR (rbio); - }bio_list_add (&rbio->bio_list, bio); rbio->bio_list_bytes = bio->bi_size;
@@ -2041,9 +2038,8 @@ int raid56_parity_recover(struct btrfs_root *root, struct bio *bio,int ret; rbio =alloc_rbio (root, bbio, raid_map, stripe_len); -if (IS_ERR (rbio)) { +if (IS_ERR (rbio))return PTR_ERR (rbio); - } rbio->read_rebuild =1 ;bio_list_add (&rbio->bio_list, bio);
@@ -2052,6 +2048,8 @@ int raid56_parity_recover(struct btrfs_root *root, struct bio *bio, rbio->faila =find_logical_bio_stripe (rbio, bio);if (rbio->faila =-1 ) {BUG (); +kfree (raid_map); +kfree (bbio);kfree (rbio);return -EIO ; }
Quoted 9 lines · Dan Carpenter
The
alloc_rbio()frees "raid_map" and "bbio" on error, so there is a potential double free bug inraid56_parity_write(). Theraid56_parity_write()andraid56_parity_recover()functions should still free "raid_map" and "bbio" on error if other errors occur though, so I have added some more calls tokfree().Signed-off-by Dan Carpenter <[email protected]>From the viewpoint of the readability, it is better to free
raid_mapand bbio
in the caller, I think. But it is up to you.
Quoted 43 lines
fs/btrfs/raid56.c +4
-7@@ -1687,11 +1687,8 @@ int raid56_parity_write(struct btrfs_root *root, struct bio *bio,struct blk_plug_cb *cb; rbio =alloc_rbio (root, bbio, raid_map, stripe_len); -if (IS_ERR (rbio)) { -kfree (raid_map); -kfree (bbio); +if (IS_ERR (rbio))return PTR_ERR (rbio); - }bio_list_add (&rbio->bio_list, bio); rbio->bio_list_bytes = bio->bi_size;@@ -2041,9 +2038,8 @@ int raid56_parity_recover(struct btrfs_root *root, struct bio *bio,int ret; rbio =alloc_rbio (root, bbio, raid_map, stripe_len); -if (IS_ERR (rbio)) { +if (IS_ERR (rbio))return PTR_ERR (rbio); - } rbio->read_rebuild =1 ;bio_list_add (&rbio->bio_list, bio);@@ -2052,6 +2048,8 @@ int raid56_parity_recover(struct btrfs_root *root, struct bio *bio, rbio->faila =find_logical_bio_stripe (rbio, bio);if (rbio->faila ==-1 ) {BUG (); +kfree (raid_map); +kfree (bbio);kfree (rbio);return -EIO ; } --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
Quoted 11 lines · Miao Xie
Quoted 9 lines · Dan Carpenter
The
alloc_rbio()frees "raid_map" and "bbio" on error, so there is a potential double free bug inraid56_parity_write(). Theraid56_parity_write()andraid56_parity_recover()functions should still free "raid_map" and "bbio" on error if other errors occur though, so I have added some more calls tokfree().Signed-off-by Dan Carpenter <[email protected]>From the viewpoint of the readability, it is better to free
raid_mapand bbioin the caller, I think. But it is up to you.
Normally, yes, you are right, but in this case the code makes sense as is. We would have to free it in btrfs_map_bio() but we actually allocate it in __btrfs_map_block(). It would be just as weird as the current code.
The current code is not ugly, it's just that complicated things require complicated code.
regards,
dan carpenter