The core routine get_unmapped_area will call a filesystem specific version
of get_unmapped_area if it exists in file operations.  If a file is on a
union/overlay, overlayfs does not contain a get_unmapped_area f_op and the
underlying filesystem routine may be ignored.  Add an overlayfs f_op to call
the underlying f_op if it exists.

The routine is_file_hugetlbfs() is used to determine if a file is on
hugetlbfs.  This is determined by f_mode & FMODE_HUGETLBFS.  Copy the mode
to the overlayfs file during open so that is_file_hugetlbfs() will work as
intended.

These two issues can result in the BUG as shown in [1].

[1] https://lore.kernel.org/linux-mm/000000000000b4684e05a2968...@google.com/

Reported-by: syzbot+d6ec23007e951dadf...@syzkaller.appspotmail.com
Signed-off-by: Miklos Szeredi <mik...@szeredi.hu>
Signed-off-by: Mike Kravetz <mike.krav...@oracle.com>
---
 fs/overlayfs/file.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c
index 87c362f65448..41e5746ba3c6 100644
--- a/fs/overlayfs/file.c
+++ b/fs/overlayfs/file.c
@@ -124,6 +124,8 @@ static int ovl_real_fdget(const struct file *file, struct 
fd *real)
        return ovl_real_fdget_meta(file, real, false);
 }
 
+#define OVL_F_MODE_TO_UPPER    (FMODE_HUGETLBFS)
+
 static int ovl_open(struct inode *inode, struct file *file)
 {
        struct file *realfile;
@@ -140,6 +142,9 @@ static int ovl_open(struct inode *inode, struct file *file)
        if (IS_ERR(realfile))
                return PTR_ERR(realfile);
 
+       /* Copy modes from underlying file */
+       file->f_mode |= (realfile->f_mode & OVL_F_MODE_TO_UPPER);
+
        file->private_data = realfile;
 
        return 0;
@@ -757,6 +762,21 @@ static loff_t ovl_remap_file_range(struct file *file_in, 
loff_t pos_in,
                            remap_flags, op);
 }
 
+#ifdef CONFIG_MMU
+static unsigned long ovl_get_unmapped_area(struct file *file,
+                               unsigned long uaddr, unsigned long len,
+                               unsigned long pgoff, unsigned long flags)
+{
+       struct file *realfile = file->private_data;
+
+       return (realfile->f_op->get_unmapped_area ?:
+               current->mm->get_unmapped_area)(realfile,
+                                               uaddr, len, pgoff, flags);
+}
+#else
+#define ovl_get_unmapped_area NULL
+#endif
+
 const struct file_operations ovl_file_operations = {
        .open           = ovl_open,
        .release        = ovl_release,
@@ -774,6 +794,7 @@ const struct file_operations ovl_file_operations = {
 
        .copy_file_range        = ovl_copy_file_range,
        .remap_file_range       = ovl_remap_file_range,
+       .get_unmapped_area      = ovl_get_unmapped_area,
 };
 
 int __init ovl_aio_request_cache_init(void)
-- 
2.25.4

Reply via email to