Skip to contentSkip to conversation

block: not create too many partitions

PATCH Applied Ming Lei 3 participants 3 messages Mar 27, 2021 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH v2 1/1 block: not create too many partitions

Commit a33df75c6328 ("block: use an xarray for disk->part_tbl") drops check on max supported partitions number, and allows partition with bigger partition number to be added. However, ->bd_partno is defined as u8, so partition index of xarray table may not match with ->bd_partno. Then delete_partition() may delete one unmatched partition, and caused use-after-free.

Cc Bart Van Assche <[email protected]>
Reported-by [email protected]
Fixes a33df75c6328 ("block: use an xarray for disk->part_tbl")

---
V2:
- don't check disk_max_parts() which is supposed to not zero

 block/partitions/core.c | 7 +++++++
 1 file changed, 7 insertions(+)
block/partitions/core.c +7 -0
index 1a7558917c47..46f055bc7ecb 100644
@@ -322,6 +322,13 @@ static struct block_device *add_partition(struct gendisk *disk, int partno,
 	const char *dname;
 	int err;
 
+	/*
+	 * disk_max_parts() won't be zero, either GENHD_FL_EXT_DEVT is set
+	 * or 'minors' is passed to alloc_disk().
+	 */
+	if (partno >= disk_max_parts(disk))
+		return ERR_PTR(-EINVAL);
+
 	/*
 	 * Partitions are not supported on zoned block devices that are used as
 	 * such.
Signature · 1 line
2.29.2
maintainer Applied
#1 lore raw
Quoted 6 lines · Ming Lei

Commit a33df75c6328 ("block: use an xarray for disk->part_tbl") drops check on max supported partitions number, and allows partition with bigger partition number to be added. However, ->bd_partno is defined as u8, so partition index of xarray table may not match with ->bd_partno. Then delete_partition() may delete one unmatched partition, and caused use-after-free.

Applied, thanks for nailing this one down, Ming.

Signature · 1 line
Jens Axboe
Reviewed-by on v2 1/1
#2 lore raw
In reply to Ming Lei
Quoted 27 lines · Ming Lei

Commit a33df75c6328 ("block: use an xarray for disk->part_tbl") drops check on max supported partitions number, and allows partition with bigger partition number to be added. However, ->bd_partno is defined as u8, so partition index of xarray table may not match with ->bd_partno. Then delete_partition() may delete one unmatched partition, and caused use-after-free.

Cc Bart Van Assche <[email protected]>
Reported-by [email protected]
Fixes a33df75c6328 ("block: use an xarray for disk->part_tbl")

---
V2:
- don't check disk_max_parts() which is supposed to not zero

 block/partitions/core.c | 7 +++++++
 1 file changed, 7 insertions(+)
block/partitions/core.c +0 -0
index 1a7558917c47..46f055bc7ecb 100644
@@ -322,6 +322,13 @@ static struct block_device *add_partition(struct gendisk *disk, int partno,
 	const char *dname;
 	int err;
 

block/partitions/core.c static struct block_device *add_partition(struct gendisk *disk, int partno, · line 334

Quoted 10 lines
block/partitions/core.c +7 -0
+	/*
+	 * disk_max_parts() won't be zero, either GENHD_FL_EXT_DEVT is set
+	 * or 'minors' is passed to alloc_disk().
+	 */
+	if (partno >= disk_max_parts(disk))
+		return ERR_PTR(-EINVAL);
+
 	/*
 	 * Partitions are not supported on zoned block devices that are used as
 	 * such.

Thanks!

Bart.