]> git.proxmox.com Git - mirror_ubuntu-kernels.git/commitdiff
block: fix use-after-free in disk_part_iter_next
authorMing Lei <ming.lei@redhat.com>
Mon, 21 Dec 2020 04:33:35 +0000 (12:33 +0800)
committerJens Axboe <axboe@kernel.dk>
Tue, 5 Jan 2021 18:35:17 +0000 (11:35 -0700)
Make sure that bdgrab() is done on the 'block_device' instance before
referring to it for avoiding use-after-free.

Cc: <stable@vger.kernel.org>
Reported-by: syzbot+825f0f9657d4e528046e@syzkaller.appspotmail.com
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/genhd.c

index 73faec438e49a88d308bb438c9e170dcacdc986e..419548e92d82f6216995293928b1c0e2f63f1d4c 100644 (file)
@@ -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;
        }