Skip to content

Commit 326b74d

Browse files
Lizhi Xugregkh
authored andcommitted
loop: Add sanity check for read/write_iter
[ Upstream commit f5c84ef ] Some file systems do not support read_iter/write_iter, such as selinuxfs in this issue. So before calling them, first confirm that the interface is supported and then call it. It is releavant in that vfs_iter_read/write have the check, and removal of their used caused szybot to be able to hit this issue. Fixes: f2fed44 ("loop: stop using vfs_iter__{read,write} for buffered I/O") Reported-by: syzbot+6af973a3b8dfd2faefdc@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=6af973a3b8dfd2faefdc Signed-off-by: Lizhi Xu <lizhi.xu@windriver.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20250428143626.3318717-1-lizhi.xu@windriver.com Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 85d5098 commit 326b74d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

drivers/block/loop.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,17 @@ static void loop_assign_backing_file(struct loop_device *lo, struct file *file)
504504
lo->old_gfp_mask & ~(__GFP_IO | __GFP_FS));
505505
}
506506

507+
static int loop_check_backing_file(struct file *file)
508+
{
509+
if (!file->f_op->read_iter)
510+
return -EINVAL;
511+
512+
if ((file->f_mode & FMODE_WRITE) && !file->f_op->write_iter)
513+
return -EINVAL;
514+
515+
return 0;
516+
}
517+
507518
/*
508519
* loop_change_fd switched the backing store of a loopback device to
509520
* a new file. This is useful for operating system installers to free up
@@ -525,6 +536,10 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
525536
if (!file)
526537
return -EBADF;
527538

539+
error = loop_check_backing_file(file);
540+
if (error)
541+
return error;
542+
528543
/* suppress uevents while reconfiguring the device */
529544
dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 1);
530545

@@ -956,6 +971,14 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
956971

957972
if (!file)
958973
return -EBADF;
974+
975+
if ((mode & BLK_OPEN_WRITE) && !file->f_op->write_iter)
976+
return -EINVAL;
977+
978+
error = loop_check_backing_file(file);
979+
if (error)
980+
return error;
981+
959982
is_loop = is_loop_device(file);
960983

961984
/* This is safe, since we have a reference from open(). */

0 commit comments

Comments
 (0)