Skip to contentSkip to conversation

powerpc/powernv/memtrace: Fix out of bounds issue in memtrace mmap

PATCH New Madhavan Srinivasan 1 participant 2 messages Jun 10, 2025 PowerPC

lore
Share & tools
lore

Madhavan Srinivasan

author maintainer
#0 lore raw

PATCH 1/2 powerpc/powernv/memtrace: Fix out of bounds issue in memtrace mmap

From: "Ritesh Harjani (IBM)" <[email protected]>

memtrace mmap issue has an out of bounds issue. This patch fixes the by checking that the requested mapping region size should stay within the allocated region size.

Fixes 08a022ad3dfa ("powerpc/powernv/memtrace: Allow mmaping trace buffers")
---
 arch/powerpc/platforms/powernv/memtrace.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/powerpc/platforms/powernv/memtrace.c +6 -2
index 4ac9808e55a4..2ea30b343354 100644
@@ -48,11 +48,15 @@ static ssize_t memtrace_read(struct file *filp, char __user *ubuf,
 static int memtrace_mmap(struct file *filp, struct vm_area_struct *vma)
 {
 	struct memtrace_entry *ent = filp->private_data;
+	unsigned long ent_nrpages = ent->size >> PAGE_SHIFT;
+	unsigned long vma_nrpages = vma_pages(vma);
 
-	if (ent->size < vma->vm_end - vma->vm_start)
+	/* The requested page offset should be within object's page count */
+	if (vma->vm_pgoff >= ent_nrpages)
 		return -EINVAL;
 
-	if (vma->vm_pgoff << PAGE_SHIFT >= ent->size)
+	/* The requested mapping range should remain within the bounds */
+	if (vma_nrpages > ent_nrpages - vma->vm_pgoff)
 		return -EINVAL;
 
 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Signature · 1 line
2.46.0
author maintainer
#1 lore raw

PATCH 2/2 powerpc/vas: Return -EINVAL if the offset is non-zero in mmap()

The user space calls mmap() to map VAS window paste address and the kernel returns the complete mapped page for each window. So return -EINVAL if non-zero is passed for offset parameter to mmap().

See Documentation/arch/powerpc/vas-api.rst for mmap() restrictions.

Fixes dda44eb29c23 ("powerpc/vas: Add VAS user space API")
---
 arch/powerpc/platforms/book3s/vas-api.c | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/powerpc/platforms/book3s/vas-api.c +9 -0
index 0b6365d85d11..dc6f75d3ac6e 100644
@@ -521,6 +521,15 @@ static int coproc_mmap(struct file *fp, struct vm_area_struct *vma)
 		return -EINVAL;
 	}
 
+	/*
+	 * Map complete page to the paste address. So the user
+	 * space should pass 0ULL to the offset parameter.
+	 */
+	if (vma->vm_pgoff) {
+		pr_debug("Page offset unsupported to map paste address\n");
+		return -EINVAL;
+	}
+
 	/* Ensure instance has an open send window */
 	if (!txwin) {
 		pr_err("No send window open?\n");
Signature · 1 line
2.46.0