Skip to content

Commit 66d7780

Browse files
author
Christoph Hellwig
committed
dma-mapping: check pfn validity in dma_common_{mmap,get_sgtable}
Check that the pfn returned from arch_dma_coherent_to_pfn refers to a valid page and reject the mmap / get_sgtable requests otherwise. Based on the arm implementation of the mmap and get_sgtable methods. Signed-off-by: Christoph Hellwig <hch@lst.de> Tested-by: Vignesh Raghavendra <vigneshr@ti.com>
1 parent 0653275 commit 66d7780

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

‎kernel/dma/mapping.c‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,16 @@ int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
116116
int ret;
117117

118118
if (!dev_is_dma_coherent(dev)) {
119+
unsigned long pfn;
120+
119121
if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN))
120122
return -ENXIO;
121123

122-
page = pfn_to_page(arch_dma_coherent_to_pfn(dev, cpu_addr,
123-
dma_addr));
124+
/* If the PFN is not valid, we do not have a struct page */
125+
pfn = arch_dma_coherent_to_pfn(dev, cpu_addr, dma_addr);
126+
if (!pfn_valid(pfn))
127+
return -ENXIO;
128+
page = pfn_to_page(pfn);
124129
} else {
125130
page = virt_to_page(cpu_addr);
126131
}
@@ -170,7 +175,11 @@ int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
170175
if (!dev_is_dma_coherent(dev)) {
171176
if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN))
172177
return -ENXIO;
178+
179+
/* If the PFN is not valid, we do not have a struct page */
173180
pfn = arch_dma_coherent_to_pfn(dev, cpu_addr, dma_addr);
181+
if (!pfn_valid(pfn))
182+
return -ENXIO;
174183
} else {
175184
pfn = page_to_pfn(virt_to_page(cpu_addr));
176185
}

0 commit comments

Comments
 (0)