Skip to contentSkip to conversation

ataflop: potential out of bounds in do_format()

PATCH Applied Dan Carpenter 3 participants 5 messages Apr 21, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ataflop: potential out of bounds in do_format()

The function uses "type" as an array index:

	q = unit[drive].disk[type]->queue;

Unfortunately the bounds check on "type" isn't done until later in the function. Fix this by moving the bounds check to the start.

Fixes bf9c0538e485 ("ataflop: use a separate gendisk for each media format")
---
 drivers/block/ataflop.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/block/ataflop.c +5 -6
index 104b713f4055..aed2c2a4f4ea 100644
@@ -729,8 +729,12 @@ static int do_format(int drive, int type, struct atari_format_descr *desc)
 	unsigned long	flags;
 	int ret;
 
-	if (type)
+	if (type) {
 		type--;
+		if (type >= NUM_DISK_MINORS ||
+		    minor2disktype[type].drive_types > DriveType)
+			return -EINVAL;
+	}
 
 	q = unit[drive].disk[type]->queue;
 	blk_mq_freeze_queue(q);
@@ -742,11 +746,6 @@ static int do_format(int drive, int type, struct atari_format_descr *desc)
 	local_irq_restore(flags);
 
 	if (type) {
-		if (type >= NUM_DISK_MINORS ||
-		    minor2disktype[type].drive_types > DriveType) {
-			ret = -EINVAL;
-			goto out;
-		}
 		type = minor2disktype[type].index;
 		UDT = &atari_disk_type[type];
 	}
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/2 ataflop: fix off by one in ataflop_probe()

Smatch complains that the "type > NUM_DISK_MINORS" should be >= instead of >. We also need to subtract one from "type" at the start.

Fixes bf9c0538e485 ("ataflop: use a separate gendisk for each media format")
---
 drivers/block/ataflop.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/block/ataflop.c +4 -1
index aed2c2a4f4ea..d601e49f80e0 100644
@@ -2001,7 +2001,10 @@ static void ataflop_probe(dev_t dev)
 	int drive = MINOR(dev) & 3;
 	int type  = MINOR(dev) >> 2;
 
-	if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
+	if (type)
+		type--;
+
+	if (drive >= FD_MAX_UNITS || type >= NUM_DISK_MINORS)
 		return;
 	mutex_lock(&ataflop_probe_lock);
 	if (!unit[drive].disk[type]) {
Signature · 1 line
2.30.2
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] ataflop: fix off by one in ataflop_probe()

Quoted 6 lines · Dan Carpenter

Smatch complains that the "type > NUM_DISK_MINORS" should be >= instead of >. We also need to subtract one from "type" at the start.

Fixes bf9c0538e485 ("ataflop: use a separate gendisk for each media format")

Looks good,

maintainer Applied
#4 lore raw
In reply to Dan Carpenter
Quoted 6 lines · Dan Carpenter

The function uses "type" as an array index:

	q = unit[drive].disk[type]->queue;

Unfortunately the bounds check on "type" isn't done until later in the function. Fix this by moving the bounds check to the start.

Applied this and 2/2, thanks Dan.

Signature · 1 line
Jens Axboe