mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chris Down <chris@chrisdown.name>
To: Ian Abbott <abbotti@mev.co.uk>,
	H Hartley Sweeten <hsweeten@visionengravers.com>
Cc: linux-kernel@vger.kernel.org,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: [PATCH 2/2] comedi: Reject MAP_PRIVATE mappings of the buffer
Date: Sun, 29 Nov 2026 11:33:42 -0800	[thread overview]
Message-ID: <d49050ef05ee5b92add1685d7cecad3634efdedd.1795963468.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1795963468.git.chris@chrisdown.name>

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@chrisdown.name>
---
 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


      parent reply	other threads:[~2026-11-29 19:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 19:33 [PATCH 0/2] comedi: honour VMA protections in comedi_mmap() Chris Down
2026-11-29 19:33 ` [PATCH 1/2] comedi: Prevent writes to the buffer through read-only mappings Chris Down
2026-11-29 19:33 ` Chris Down [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d49050ef05ee5b92add1685d7cecad3634efdedd.1795963468.git.chris@chrisdown.name \
    --to=chris@chrisdown.name \
    --cc=abbotti@mev.co.uk \
    --cc=gregkh@linuxfoundation.org \
    --cc=hsweeten@visionengravers.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®