[PATCH 2/2] comedi: Reject MAP_PRIVATE mappings of the buffer
From: Chris Down
Date: Sun Nov 29 2026 - 14:33:59 EST
comedi_mmap() maps a buffer that wasn't allocated for DMA one page at a
time with remap_pfn_range(). For a CoW mapping, remap_pfn_range() only
works when a single call covers the whole VMA, since it records the pfn
in vm_pgoff so that vm_normal_page() can tell the original pages from
CoW copies.
So today a MAP_PRIVATE mapping of more than one page fails with -EINVAL,
while a mapping of a single page succeeds and gets its vm_pgoff replaced
with the pfn. comedi_vm_access() then computes the buffer offset from
that pfn, which is well past the end of the buffer, so reading the
mapping through /proc/pid/mem or ptrace fails.
A private copy of a buffer that the driver keeps writing to isn't useful
anyway, and comedilib only ever maps it with MAP_SHARED. Let's reject
CoW mappings up front, as other drivers mapping pfns do, so the result
no longer depends on the size of the mapping.
Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
drivers/comedi/comedi_fops.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/drivers/comedi/comedi_fops.c b/drivers/comedi/comedi_fops.c
index d31214f1a8c1..862590dc7fcf 100644
--- a/drivers/comedi/comedi_fops.c
+++ b/drivers/comedi/comedi_fops.c
@@ -2527,6 +2527,16 @@ static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
goto done;
}
+ /*
+ * remap_pfn_range() can only set up a CoW mapping in a single call
+ * covering the whole VMA, but we map the buffer page by page.
+ */
+ if (vma_is_cow_mapping(vma)) {
+ dev_dbg(dev->class_dev, "mmap() must use MAP_SHARED.\n");
+ retval = -EINVAL;
+ goto done;
+ }
+
size = vma->vm_end - vma->vm_start;
if (size > async->prealloc_bufsz) {
retval = -EFAULT;
--
2.56.1