* [PATCH 0/2] comedi: honour VMA protections in comedi_mmap() @ 2026-11-29 19:33 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 ` [PATCH 2/2] comedi: Reject MAP_PRIVATE mappings of the buffer Chris Down 0 siblings, 2 replies; 5+ messages in thread From: Chris Down @ 2026-11-29 19:33 UTC (permalink / raw) To: Ian Abbott, H Hartley Sweeten; +Cc: linux-kernel, Greg Kroah-Hartman comedi_mmap() maps buffers that weren't allocated for DMA with PAGE_SHARED, so the pages are writable no matter how they were mapped. That means anyone who can open a comedi device read-only can map its buffer with PROT_READ and overwrite the samples in it. Patch 1 fixes that by passing vma->vm_page_prot to remap_pfn_range(), as the DMA path already does through dma_mmap_coherent(). Patch 2 rejects MAP_PRIVATE mappings, which comedi_mmap() can't set up properly anyway: a mapping of more than one page already fails with -EINVAL, and a single page one ends up with a vm_pgoff that comedi_vm_access() can't use. Chris Down (2): comedi: Prevent writes to the buffer through read-only mappings comedi: Reject MAP_PRIVATE mappings of the buffer drivers/comedi/comedi_fops.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) base-commit: 89f33682b2c34b12b3a7a549ba94a808f0c7c98d -- 2.56.1 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] comedi: Prevent writes to the buffer through read-only mappings 2026-11-29 19:33 [PATCH 0/2] comedi: honour VMA protections in comedi_mmap() Chris Down @ 2026-11-29 19:33 ` Chris Down 2026-11-30 11:23 ` Ian Abbott 2026-11-29 19:33 ` [PATCH 2/2] comedi: Reject MAP_PRIVATE mappings of the buffer Chris Down 1 sibling, 1 reply; 5+ messages in thread From: Chris Down @ 2026-11-29 19:33 UTC (permalink / raw) To: Ian Abbott, H Hartley Sweeten; +Cc: linux-kernel, Greg Kroah-Hartman 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 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] comedi: Prevent writes to the buffer through read-only mappings 2026-11-29 19:33 ` [PATCH 1/2] comedi: Prevent writes to the buffer through read-only mappings Chris Down @ 2026-11-30 11:23 ` Ian Abbott 0 siblings, 0 replies; 5+ messages in thread From: Ian Abbott @ 2026-11-30 11:23 UTC (permalink / raw) To: Chris Down, H Hartley Sweeten; +Cc: linux-kernel, Greg Kroah-Hartman On 29/11/2026 19:33, Chris Down wrote: > 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; > Looks good, thanks! Reviewed-by: Ian Abbott <abbotti@mev.co.uk> -- -=( Ian Abbott <abbotti@mev.co.uk> || MEV Ltd. is a company )=- -=( registered in England & Wales. Regd. number: 02862268. )=- -=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=- -=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=- ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] comedi: Reject MAP_PRIVATE mappings of the buffer 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 2026-11-30 11:23 ` Ian Abbott 1 sibling, 1 reply; 5+ messages in thread From: Chris Down @ 2026-11-29 19:33 UTC (permalink / raw) To: Ian Abbott, H Hartley Sweeten; +Cc: linux-kernel, Greg Kroah-Hartman 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 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] comedi: Reject MAP_PRIVATE mappings of the buffer 2026-11-29 19:33 ` [PATCH 2/2] comedi: Reject MAP_PRIVATE mappings of the buffer Chris Down @ 2026-11-30 11:23 ` Ian Abbott 0 siblings, 0 replies; 5+ messages in thread From: Ian Abbott @ 2026-11-30 11:23 UTC (permalink / raw) To: Chris Down, H Hartley Sweeten; +Cc: linux-kernel, Greg Kroah-Hartman On 29/11/2026 19:33, Chris Down wrote: > 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; Looks good, thanks! Reviewed-by: Ian Abbott <abbotti@mev.co.uk> -- -=( Ian Abbott <abbotti@mev.co.uk> || MEV Ltd. is a company )=- -=( registered in England & Wales. Regd. number: 02862268. )=- -=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=- -=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=- ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-30 11:24 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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-30 11:23 ` Ian Abbott 2026-11-29 19:33 ` [PATCH 2/2] comedi: Reject MAP_PRIVATE mappings of the buffer Chris Down 2026-11-30 11:23 ` Ian Abbott
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.