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 1/2] comedi: Prevent writes to the buffer through read-only mappings
Date: Sun, 29 Nov 2026 11:33:40 -0800	[thread overview]
Message-ID: <13c8fcd517154be888f62bc76754f4052b3590ff.1795963468.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1795963468.git.chris@chrisdown.name>

comedi_mmap() maps the pages of a buffer that wasn't allocated for DMA
with remap_pfn_range(..., PAGE_SHARED), ignoring vma->vm_page_prot. As a
result, the PTEs it installs are always writable, even for a PROT_READ
mapping, and even when the device was opened O_RDONLY (where VM_MAYWRITE
is clear and mprotect() refuses to add PROT_WRITE).

For example, with comedi_test attached as /dev/comedi0:

    fd = open("/dev/comedi0", O_RDONLY);
    p = mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, 0);
    *p = 0x5a;

The store doesn't fault, and the byte in the read subdevice's buffer
changes from 00 to 5a. So anyone who can open a comedi device read-only
can overwrite the samples in the acquisition buffer that are read by
whoever is running the command. The same goes for a one page
MAP_PRIVATE mapping, where a store should break CoW but goes straight to
the shared buffer instead.

The DMA path is already fine, since dma_mmap_coherent() builds its PTEs
from vma->vm_page_prot.

Let's pass vma->vm_page_prot to remap_pfn_range() as well, so the PTEs
are only writable when the mapping is. Writable MAP_SHARED mappings of a
device opened O_RDWR, as comedilib's demos use, still work as before.

Fixes: ed9eccbe8970 ("Staging: add comedi core")
Cc: stable@vger.kernel.org
Signed-off-by: Chris Down <chris@chrisdown.name>
---
 drivers/comedi/comedi_fops.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/comedi/comedi_fops.c b/drivers/comedi/comedi_fops.c
index 6d71ceeff6fc..d31214f1a8c1 100644
--- a/drivers/comedi/comedi_fops.c
+++ b/drivers/comedi/comedi_fops.c
@@ -2574,7 +2574,7 @@ static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
 			buf = &bm->page_list[i];
 			pfn = page_to_pfn(virt_to_page(buf->virt_addr));
 			retval = remap_pfn_range(vma, start, pfn, PAGE_SIZE,
-						 PAGE_SHARED);
+						 vma->vm_page_prot);
 			if (retval)
 				break;
 
-- 
2.56.1


  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 ` Chris Down [this message]
2026-11-29 19:33 ` [PATCH 2/2] comedi: Reject MAP_PRIVATE mappings of the buffer Chris Down

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=13c8fcd517154be888f62bc76754f4052b3590ff.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®