ataflop: potential out of bounds in do_format()
authorDan Carpenter <dan.carpenter@oracle.com>
Wed, 21 Apr 2021 10:18:35 +0000 (13:18 +0300)
committerJens Axboe <axboe@kernel.dk>
Wed, 21 Apr 2021 15:15:25 +0000 (09:15 -0600)
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")
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/block/ataflop.c

index 104b713f4055af3d4e481550aac8553d7e7b66ff..aed2c2a4f4ea3f95de0849e43683c79a77ebf6e6 100644 (file)
@@ -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];
        }