[PATCH v3] ovl: add f_real file_operation and supporting code for overlayfs

From: Mike Kravetz
Date: Wed Jun 10 2020 - 19:23:46 EST


If a file is on a union/overlay, then the 'struct file *' will have
overlayfs file operations. The routine is_file_hugepages() compares
f->f_op to hugetlbfs_file_operations to determine if it is a hugetlbfs
file. If a hugetlbfs file is on a union/overlay, this comparison is
false and is_file_hugepages() incorrectly indicates the underlying
file is not hugetlbfs. One result of this is a BUG as shown in [1].

mmap uses is_file_hugepages() to identify hugetlbfs file and potentially
round up length because hugetlbfs files have different alignment
restrictions. This is defined/expected behavior. In addition, mmap
code would like to use the filesystem specific get_unmapped_area()
routine if one is defined.

To address this issue,
- Add a new file operation f_real while will return the underlying file.
Only overlayfs provides a function for this operation.
- Add a new routine real_file() which can be used by core code get an
underlying file.
- Update is_file_hugepages to get the real file.
- Add get_unmapped_area f_op to oerrlayfs to call underlying routine.

[1] https://lore.kernel.org/linux-mm/000000000000b4684e05a2968ca6@xxxxxxxxxx/

Reported-by: syzbot+d6ec23007e951dadf3de@xxxxxxxxxxxxxxxxxxxxxxxxx
Signed-off-by: Miklos Szeredi <miklos@xxxxxxxxxx>
Signed-off-by: Mike Kravetz <mike.kravetz@xxxxxxxxxx>
---
fs/overlayfs/file.c | 23 +++++++++++++++++++++++
include/linux/fs.h | 8 ++++++++
include/linux/hugetlb.h | 2 ++
3 files changed, 33 insertions(+)

diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c
index 87c362f65448..eb870fc3912f 100644
--- a/fs/overlayfs/file.c
+++ b/fs/overlayfs/file.c
@@ -757,6 +757,27 @@ 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)
+{
+ file = real_file(file);
+
+ return (file->f_op->get_unmapped_area ?:
+ current->mm->get_unmapped_area)(file, uaddr, len, pgoff, flags);
+}
+#else
+#define ovl_get_unmapped_area NULL
+#endif
+
+static struct file *ovl_f_real(struct file *file)
+{
+ while (file->f_op == &ovl_file_operations)
+ file = file->private_data;
+ return file;
+}
+
const struct file_operations ovl_file_operations = {
.open = ovl_open,
.release = ovl_release,
@@ -771,9 +792,11 @@ const struct file_operations ovl_file_operations = {
.compat_ioctl = ovl_compat_ioctl,
.splice_read = ovl_splice_read,
.splice_write = ovl_splice_write,
+ .f_real = ovl_f_real,

.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)
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 45cc10cdf6dd..59a969549aec 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1863,6 +1863,7 @@ struct file_operations {
struct file *file_out, loff_t pos_out,
loff_t len, unsigned int remap_flags);
int (*fadvise)(struct file *, loff_t, loff_t, int);
+ struct file * (*f_real)(struct file *file);
} __randomize_layout;

struct inode_operations {
@@ -1895,6 +1896,13 @@ struct inode_operations {
int (*set_acl)(struct inode *, struct posix_acl *, int);
} ____cacheline_aligned;

+static inline struct file *real_file(struct file *file)
+{
+ if (unlikely(file->f_op->f_real))
+ return file->f_op->f_real(file);
+ return file;
+}
+
static inline ssize_t call_read_iter(struct file *file, struct kiocb *kio,
struct iov_iter *iter)
{
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 43a1cef8f0f1..528b07145414 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -437,6 +437,8 @@ struct file *hugetlb_file_setup(const char *name, size_t size, vm_flags_t acct,

static inline bool is_file_hugepages(struct file *file)
{
+ file = real_file(file);
+
if (file->f_op == &hugetlbfs_file_operations)
return true;

--
2.25.4