Skip to contentSkip to conversation

dma-buf: heaps: Fix off-by-one in CMA heap fault handler

PATCH In review T.J. Mercier 2 participants 5 messages Sep 16, 2024 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma-buf: heaps: Fix off-by-one in CMA heap fault handler

commit ea5ff5d351b520524019f7ff7f9ce418de2dad87 upstream.

Until VM_DONTEXPAND was added in commit 1c1914d6e8c6 ("dma-buf: heaps:
Don't track CMA dma-buf pages under RssFile") it was possible to obtain a mapping larger than the buffer size via mremap and bypass the overflow check in dma_buf_mmap_internal. When using such a mapping to attempt to fault past the end of the buffer, the CMA heap fault handler also checks the fault offset against the buffer size, but gets the boundary wrong by
1. Fix the boundary check so that we don't read off the end of the pages array and insert an arbitrary page in the mapping.

Fixes a5d2d29e24be ("dma-buf: heaps: Move heap-helper logic into the cma_heap implementation")
Cc [email protected] # Applicable >= 5.10. Needs adjustments only for 5.10.

[TJ: Backport to 5.10. On this kernel the bug is located in dma_heap_vm_fault which is used by both the CMA and system heaps.]

---
 drivers/dma-buf/heaps/heap-helpers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma-buf/heaps/heap-helpers.c +1 -1
index d0696cf937af..a852b5e8122f 100644
@@ -161,7 +161,7 @@ static vm_fault_t dma_heap_vm_fault(struct vm_fault *vmf)
 	struct vm_area_struct *vma = vmf->vma;
 	struct heap_helper_buffer *buffer = vma->vm_private_data;
 
-	if (vmf->pgoff > buffer->pagecount)
+	if (vmf->pgoff >= buffer->pagecount)
 		return VM_FAULT_SIGBUS;
 
 	vmf->page = buffer->pages[vmf->pgoff];
Signature · 1 line
2.46.0.662.g92d0881bb0-goog
maintainer
#1 lore raw
Quoted 11 lines · T.J. Mercier

commit ea5ff5d351b520524019f7ff7f9ce418de2dad87 upstream.

Until VM_DONTEXPAND was added in commit 1c1914d6e8c6 ("dma-buf: heaps:
Don't track CMA dma-buf pages under RssFile") it was possible to obtain a mapping larger than the buffer size via mremap and bypass the overflow check in dma_buf_mmap_internal. When using such a mapping to attempt to fault past the end of the buffer, the CMA heap fault handler also checks the fault offset against the buffer size, but gets the boundary wrong by
1. Fix the boundary check so that we don't read off the end of the pages array and insert an arbitrary page in the mapping.

Quoted 2 lines
Fixes a5d2d29e24be ("dma-buf: heaps: Move heap-helper logic into the cma_heap implementation")

This commit is in 5.11, so why:

Quoted 1 line
Cc [email protected] # Applicable >= 5.10. Needs adjustments only for 5.10.

does this say 5.10?

thanks,

greg k-h

#2 lore raw
Quoted 18 lines · Greg KH
Quoted 11 lines · T.J. Mercier

commit ea5ff5d351b520524019f7ff7f9ce418de2dad87 upstream.

Until VM_DONTEXPAND was added in commit 1c1914d6e8c6 ("dma-buf: heaps:
Don't track CMA dma-buf pages under RssFile") it was possible to obtain a mapping larger than the buffer size via mremap and bypass the overflow check in dma_buf_mmap_internal. When using such a mapping to attempt to fault past the end of the buffer, the CMA heap fault handler also checks the fault offset against the buffer size, but gets the boundary wrong by
1. Fix the boundary check so that we don't read off the end of the pages array and insert an arbitrary page in the mapping.

Quoted 2 lines
Fixes a5d2d29e24be ("dma-buf: heaps: Move heap-helper logic into the cma_heap implementation")

This commit is in 5.11, so why:

Quoted 7 lines
Quoted 1 line
Cc [email protected] # Applicable >= 5.10. Needs adjustments only for 5.10.

does this say 5.10?

thanks,

greg k-h

a5d2d29e24be ("dma-buf: heaps: Move heap-helper logic into the cma_heap implementation") moved the code from this heap-helpers.c file to cma_heap.c in 5.11, which is the only place it lives upstream now. The bug still exists in the original location in this heap-helpers.c file on 5.10.

maintainer
#3 lore raw
Quoted 25 lines · T.J. Mercier
Quoted 24 lines · Greg KH
Quoted 11 lines · T.J. Mercier

commit ea5ff5d351b520524019f7ff7f9ce418de2dad87 upstream.

Until VM_DONTEXPAND was added in commit 1c1914d6e8c6 ("dma-buf: heaps:
Don't track CMA dma-buf pages under RssFile") it was possible to obtain a mapping larger than the buffer size via mremap and bypass the overflow check in dma_buf_mmap_internal. When using such a mapping to attempt to fault past the end of the buffer, the CMA heap fault handler also checks the fault offset against the buffer size, but gets the boundary wrong by
1. Fix the boundary check so that we don't read off the end of the pages array and insert an arbitrary page in the mapping.

Quoted 2 lines
Fixes a5d2d29e24be ("dma-buf: heaps: Move heap-helper logic into the cma_heap implementation")

This commit is in 5.11, so why:

Quoted 1 line
Cc [email protected] # Applicable >= 5.10. Needs adjustments only for 5.10.

does this say 5.10?

thanks,

Quoted 7 lines
Quoted 1 line

greg k-h

a5d2d29e24be ("dma-buf: heaps: Move heap-helper logic into the cma_heap implementation") moved the code from this heap-helpers.c file to cma_heap.c in 5.11, which is the only place it lives upstream now. The bug still exists in the original location in this heap-helpers.c file on 5.10.

Ah, then that was the wrong Fixes: tag :(

thanks, I'll go queue this up now.

greg k-h

#4 lore raw
Quoted 35 lines · Greg KH
Quoted 25 lines · T.J. Mercier
Quoted 24 lines · Greg KH
Quoted 11 lines · T.J. Mercier

commit ea5ff5d351b520524019f7ff7f9ce418de2dad87 upstream.

Until VM_DONTEXPAND was added in commit 1c1914d6e8c6 ("dma-buf: heaps:
Don't track CMA dma-buf pages under RssFile") it was possible to obtain a mapping larger than the buffer size via mremap and bypass the overflow check in dma_buf_mmap_internal. When using such a mapping to attempt to fault past the end of the buffer, the CMA heap fault handler also checks the fault offset against the buffer size, but gets the boundary wrong by
1. Fix the boundary check so that we don't read off the end of the pages array and insert an arbitrary page in the mapping.

Quoted 2 lines
Fixes a5d2d29e24be ("dma-buf: heaps: Move heap-helper logic into the cma_heap implementation")

This commit is in 5.11, so why:

Quoted 1 line
Cc [email protected] # Applicable >= 5.10. Needs adjustments only for 5.10.

does this say 5.10?

thanks,

Quoted 7 lines
Quoted 1 line

greg k-h

a5d2d29e24be ("dma-buf: heaps: Move heap-helper logic into the cma_heap implementation") moved the code from this heap-helpers.c file to cma_heap.c in 5.11, which is the only place it lives upstream now. The bug still exists in the original location in this heap-helpers.c file on 5.10.

Quoted 5 lines

Ah, then that was the wrong Fixes: tag :(

thanks, I'll go queue this up now.

greg k-h

Ok sorry about that. Thanks.