Skip to content

Commit

Permalink
block: fix use-after-free in disk_part_iter_next
Browse files Browse the repository at this point in the history
Make sure that bdgrab() is done on the 'block_device' instance before
referring to it for avoiding use-after-free.

Cc: <[email protected]>
Reported-by: [email protected]
Signed-off-by: Ming Lei <[email protected]>
Reviewed-by: Christoph Hellwig <[email protected]>
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
Ming Lei authored and axboe committed Jan 5, 2021
1 parent 6d4d273 commit aebf5db
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions block/genhd.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,18 @@ struct block_device *disk_part_iter_next(struct disk_part_iter *piter)
part = rcu_dereference(ptbl->part[piter->idx]);
if (!part)
continue;
piter->part = bdgrab(part);
if (!piter->part)
continue;
if (!bdev_nr_sectors(part) &&
!(piter->flags & DISK_PITER_INCL_EMPTY) &&
!(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
piter->idx == 0))
piter->idx == 0)) {
bdput(piter->part);
piter->part = NULL;
continue;
}

piter->part = bdgrab(part);
if (!piter->part)
continue;
piter->idx += inc;
break;
}
Expand Down

0 comments on commit aebf5db

Please sign in to comment.