fs/kernfs/file.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
The kernfs_fop_mmap() invokes the '->mmap' callback without verifying its
existence. This leads to a NULL pointer dereference when the kernfs node
does not define the operation, resulting in an invalid memory access.
Add a check to ensure the '->mmap' callback is present before invocation.
Return -EINVAL when uninitialized to prevent the invalid access.
Fixes: 324a56e16e44 ("kernfs: s/sysfs_dirent/kernfs_node/ and rename its friends accordingly")
Cc: stable@vger.kernel.org # 3.14+
Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
---
fs/kernfs/file.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c
index 8502ef68459b..7d8434a97487 100644
--- a/fs/kernfs/file.c
+++ b/fs/kernfs/file.c
@@ -459,9 +459,14 @@ static int kernfs_fop_mmap(struct file *file, struct vm_area_struct *vma)
goto out_unlock;
ops = kernfs_ops(of->kn);
- rc = ops->mmap(of, vma);
- if (rc)
+ if (ops->mmap) {
+ rc = ops->mmap(of, vma);
+ if (rc)
+ goto out_put;
+ } else {
+ rc = -EINVAL;
goto out_put;
+ }
/*
* PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
--
2.42.0.windows.2
On Tue, Apr 01, 2025 at 11:18:59PM +0800, Wentao Liang wrote: > The kernfs_fop_mmap() invokes the '->mmap' callback without verifying its > existence. This leads to a NULL pointer dereference when the kernfs node > does not define the operation, resulting in an invalid memory access. How can that happen with any in-kernel user of kernfs? If you try to mmap any sysfs file today does this trigger? thanks, greg k-h
On Tue, Apr 01, 2025 at 11:18:59PM +0800, Wentao Liang wrote: > The kernfs_fop_mmap() invokes the '->mmap' callback without verifying its > existence. This leads to a NULL pointer dereference when the kernfs node > does not define the operation, resulting in an invalid memory access. > > Add a check to ensure the '->mmap' callback is present before invocation. > Return -EINVAL when uninitialized to prevent the invalid access. I think that's already checked through KERNFS_HAS_MMAP. Thanks. -- tejun
© 2016 - 2025 Red Hat, Inc.