Skip to content

Commit

Permalink
md/raid1,10: Remove use-after-free bug in make_request.
Browse files Browse the repository at this point in the history
A single request to RAID1 or RAID10 might result in multiple
requests if there are known bad blocks that need to be avoided.

To detect if we need to submit another write request we test:
 	if (sectors_handled < (bio->bi_size >> 9)) {

However this is after we call **_write_done() so the 'bio' no longer
belongs to us - the writes could have completed and the bio freed.

So move the **_write_done call until after the test against
bio->bi_size.

This addresses https://bugzilla.kernel.org/show_bug.cgi?id=41862

Reported-by: Bruno Wolff III <[email protected]>
Tested-by: Bruno Wolff III <[email protected]>
Signed-off-by: NeilBrown <[email protected]>
  • Loading branch information
neilbrown committed Sep 10, 2011
1 parent 19d5f83 commit 079fa16
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
14 changes: 9 additions & 5 deletions drivers/md/raid1.c
Original file line number Diff line number Diff line change
Expand Up @@ -1099,12 +1099,11 @@ static int make_request(mddev_t *mddev, struct bio * bio)
bio_list_add(&conf->pending_bio_list, mbio);
spin_unlock_irqrestore(&conf->device_lock, flags);
}
r1_bio_write_done(r1_bio);

/* In case raid1d snuck in to freeze_array */
wake_up(&conf->wait_barrier);

/* Mustn't call r1_bio_write_done before this next test,
* as it could result in the bio being freed.
*/
if (sectors_handled < (bio->bi_size >> 9)) {
r1_bio_write_done(r1_bio);
/* We need another r1_bio. It has already been counted
* in bio->bi_phys_segments
*/
Expand All @@ -1117,6 +1116,11 @@ static int make_request(mddev_t *mddev, struct bio * bio)
goto retry_write;
}

r1_bio_write_done(r1_bio);

/* In case raid1d snuck in to freeze_array */
wake_up(&conf->wait_barrier);

if (do_sync || !bitmap || !plugged)
md_wakeup_thread(mddev->thread);

Expand Down
13 changes: 8 additions & 5 deletions drivers/md/raid10.c
Original file line number Diff line number Diff line change
Expand Up @@ -1132,13 +1132,12 @@ static int make_request(mddev_t *mddev, struct bio * bio)
spin_unlock_irqrestore(&conf->device_lock, flags);
}

/* Remove the bias on 'remaining' */
one_write_done(r10_bio);

/* In case raid10d snuck in to freeze_array */
wake_up(&conf->wait_barrier);
/* Don't remove the bias on 'remaining' (one_write_done) until
* after checking if we need to go around again.
*/

if (sectors_handled < (bio->bi_size >> 9)) {
one_write_done(r10_bio);
/* We need another r10_bio. It has already been counted
* in bio->bi_phys_segments.
*/
Expand All @@ -1152,6 +1151,10 @@ static int make_request(mddev_t *mddev, struct bio * bio)
r10_bio->state = 0;
goto retry_write;
}
one_write_done(r10_bio);

/* In case raid10d snuck in to freeze_array */
wake_up(&conf->wait_barrier);

if (do_sync || !mddev->bitmap || !plugged)
md_wakeup_thread(mddev->thread);
Expand Down

0 comments on commit 079fa16

Please sign in to comment.