Skip to content

Commit ae66501

Browse files
torvaldsaxboe
authored andcommitted
loop: fix concurrent lo_open/lo_release
范龙飞 reports that KASAN can report a use-after-free in __lock_acquire. The reason is due to insufficient serialization in lo_release(), which will continue to use the loop device even after it has decremented the lo_refcnt to zero. In the meantime, another process can come in, open the loop device again as it is being shut down. Confusion ensues. Reported-by: 范龙飞 <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent cbf3a95 commit ae66501

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

drivers/block/loop.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,9 +1581,8 @@ static int lo_open(struct block_device *bdev, fmode_t mode)
15811581
return err;
15821582
}
15831583

1584-
static void lo_release(struct gendisk *disk, fmode_t mode)
1584+
static void __lo_release(struct loop_device *lo)
15851585
{
1586-
struct loop_device *lo = disk->private_data;
15871586
int err;
15881587

15891588
if (atomic_dec_return(&lo->lo_refcnt))
@@ -1610,6 +1609,13 @@ static void lo_release(struct gendisk *disk, fmode_t mode)
16101609
mutex_unlock(&lo->lo_ctl_mutex);
16111610
}
16121611

1612+
static void lo_release(struct gendisk *disk, fmode_t mode)
1613+
{
1614+
mutex_lock(&loop_index_mutex);
1615+
__lo_release(disk->private_data);
1616+
mutex_unlock(&loop_index_mutex);
1617+
}
1618+
16131619
static const struct block_device_operations lo_fops = {
16141620
.owner = THIS_MODULE,
16151621
.open = lo_open,

0 commit comments

Comments
 (0)