Re: [PATCH v2] ovl: fix null pointer when filesystem doesn't support direct IO

From: Huang Jianan
Date: Wed Sep 22 2021 - 03:18:50 EST


在 2021/9/22 13:09, Chengguang Xu 写道:
在 2021/9/22 11:47, Huang Jianan 写道:
At present, overlayfs provides overlayfs inode to users. Overlayfs
inode provides ovl_aops with noop_direct_IO to avoid open failure
with O_DIRECT. But some compressed filesystems, such as erofs and
squashfs, don't support direct_IO.

Users who use f_mapping->a_ops->direct_IO to check O_DIRECT support,
will read file through this way. This will cause overlayfs to access
a non-existent direct_IO function and cause panic due to null pointer:

Kernel panic - not syncing: CFI failure (target: 0x0)
CPU: 6 PID: 247 Comm: loop0
Call Trace:
  panic+0x188/0x45c
  __cfi_slowpath+0x0/0x254
  __cfi_slowpath+0x200/0x254
  generic_file_read_iter+0x14c/0x150
  vfs_iocb_iter_read+0xac/0x164
  ovl_read_iter+0x13c/0x2fc
  lo_rw_aio+0x2bc/0x458
  loop_queue_work+0x4a4/0xbc0
  kthread_worker_fn+0xf8/0x1d0
  loop_kthread_worker_fn+0x24/0x38
  kthread+0x29c/0x310
  ret_from_fork+0x10/0x30

The filesystem may only support direct_IO for some file types. For
example, erofs supports direct_IO for uncompressed files. So reset
f_mapping->a_ops to NULL when the file doesn't support direct_IO to
fix this problem.

Fixes: 5b910bd615ba ("ovl: fix GPF in swapfile_activate of file from overlayfs over xfs")
Signed-off-by: Huang Jianan <huangjianan@xxxxxxxx>
---
Change since v1:
  - Return error to user rather than fall back to buffered io. (Chengguang Xu)

  fs/overlayfs/file.c | 4 ++++
  1 file changed, 4 insertions(+)

diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c
index d081faa55e83..38118d3b46f8 100644
--- a/fs/overlayfs/file.c
+++ b/fs/overlayfs/file.c
@@ -157,6 +157,10 @@ static int ovl_open(struct inode *inode, struct file *file)
      if (IS_ERR(realfile))
          return PTR_ERR(realfile);
  +    if ((f->f_flags & O_DIRECT) && (!realfile->f_mapping->a_ops ||
+        !realfile->f_mapping->a_ops->direct_IO))
+        file->f_mapping->a_ops = NULL;


There are many other functions in a_ops and also address_space struct will be shared

between files which belong to same inode. Although overlayfs currently only defines

->direct_IO in a_ops, it will be extended in the future. (like containerized sycnfs [1])


It seems the simplest solution is directly return error to upper layer.


I think that after reset a_ops, do_dentry_open will check f_mapping->a_ops->direct_IO
and return error. But return error directly in ovl_open seems to be a better solution, and
won't affect future extend of ovl_aops. Thanks for pointing this out.

Thanks,
Jianan


Thanks,

Chengguang


[1] https://apc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.spinics.net%2Flists%2Flinux-unionfs%2Fmsg08569.html&amp;data=04%7C01%7Chuangjianan%40oppo.com%7Ce01c8bb9ad4e4ac2670008d97d87321c%7Cf1905eb1c35341c5951662b4a54b5ee6%7C0%7C0%7C637678842352759179%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=Wo2uMfbYhqDOzDPSwHci2AVtM9y9nNstmayb741gspQ%3D&amp;reserved=0



+
      file->private_data = realfile;
        return 0;