public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kernfs: fix potential NULL dereference in mmap handler
@ 2025-04-01 15:18 Wentao Liang
  2025-04-01 15:50 ` Tejun Heo
  2025-04-01 15:51 ` Greg KH
  0 siblings, 2 replies; 3+ messages in thread
From: Wentao Liang @ 2025-04-01 15:18 UTC (permalink / raw)
  To: gregkh, tj; +Cc: linux-kernel, Wentao Liang, stable

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] kernfs: fix potential NULL dereference in mmap handler
  2025-04-01 15:18 [PATCH] kernfs: fix potential NULL dereference in mmap handler Wentao Liang
@ 2025-04-01 15:50 ` Tejun Heo
  2025-04-01 15:51 ` Greg KH
  1 sibling, 0 replies; 3+ messages in thread
From: Tejun Heo @ 2025-04-01 15:50 UTC (permalink / raw)
  To: Wentao Liang; +Cc: gregkh, linux-kernel, stable

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] kernfs: fix potential NULL dereference in mmap handler
  2025-04-01 15:18 [PATCH] kernfs: fix potential NULL dereference in mmap handler Wentao Liang
  2025-04-01 15:50 ` Tejun Heo
@ 2025-04-01 15:51 ` Greg KH
  1 sibling, 0 replies; 3+ messages in thread
From: Greg KH @ 2025-04-01 15:51 UTC (permalink / raw)
  To: Wentao Liang; +Cc: tj, linux-kernel, stable

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-04-01 15:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-01 15:18 [PATCH] kernfs: fix potential NULL dereference in mmap handler Wentao Liang
2025-04-01 15:50 ` Tejun Heo
2025-04-01 15:51 ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox