[PATCH V2] loop: Add sanity check for read/write_iter

From: Lizhi Xu
Date: Fri Apr 25 2025 - 01:38:43 EST


Some file systems do not support read_iter or write_iter, such as selinuxfs
in this issue.
So before calling them, first confirm that the interface is supported and
then call it.

Reported-by: syzbot+6af973a3b8dfd2faefdc@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=6af973a3b8dfd2faefdc
Signed-off-by: Lizhi Xu <lizhi.xu@xxxxxxxxxxxxx>
---
V1 -> V2: move check to loop_configure and loop_change_fd

drivers/block/loop.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 674527d770dc..d2651e3b5142 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -603,6 +603,12 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
if (!file)
return -EBADF;

+ if (unlikely(!file->f_op->read_iter))
+ return -EINVAL;
+
+ if (file->f_mode & FMODE_WRITE && unlikely(!file->f_op->write_iter))
+ return -EINVAL;
+
/* suppress uevents while reconfiguring the device */
dev_set_uevent_suppress(disk_to_dev(lo->lo_disk), 1);

@@ -1039,6 +1045,14 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,

if (!file)
return -EBADF;
+
+ if (unlikely(!file->f_op->read_iter))
+ return -EINVAL;
+
+ if (((file->f_mode & FMODE_WRITE) || (mode & BLK_OPEN_WRITE)) &&
+ unlikely(!file->f_op->write_iter))
+ return -EINVAL;
+
is_loop = is_loop_device(file);

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