Skip to contentSkip to conversation

block: fix use-after-free in disk_part_iter_next

PATCH Applied Ming Lei 3 participants 3 messages Dec 21, 2020 – Jan 5, 2021

Subsystems (2)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 block: fix use-after-free in disk_part_iter_next

Make sure that bdgrab() is done on the 'block_device' instance before referring to it for avoiding use-after-free.

---
 block/genhd.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
block/genhd.c +7 -4
index b84b8671e627..2df3c5b1c9c8 100644
@@ -244,15 +244,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;
 	}
Signature · 1 line
2.28.0
maintainer Applied
#2 lore raw
In reply to Ming Lei
Quoted 2 lines · Ming Lei

Make sure that bdgrab() is done on the 'block_device' instance before referring to it for avoiding use-after-free.

Applied, thanks.

Signature · 1 line
Jens Axboe