[PATCH] kernfs: fix potential NULL dereference in mmap handler
From: Wentao Liang
Date: Tue Apr 01 2025 - 11:19:55 EST
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@xxxxxxxxxxxxxxx # 3.14+
Signed-off-by: Wentao Liang <vulab@xxxxxxxxxxx>
---
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