Skip to content

Commit 868c9dd

Browse files
martinetdkonradwilk
authored andcommitted
swiotlb: add overflow checks to swiotlb_bounce
This is a follow-up on 5f89468 ("swiotlb: manipulate orig_addr when tlb_addr has offset") which fixed unaligned dma mappings, making sure the following overflows are caught: - offset of the start of the slot within the device bigger than requested address' offset, in other words if the base address given in swiotlb_tbl_map_single to create the mapping (orig_addr) was after the requested address for the sync (tlb_offset) in the same block: |------------------------------------------| block <----------------------------> mapped part of the block ^ orig_addr ^ invalid tlb_addr for sync - if the resulting offset was bigger than the allocation size this one could happen if the mapping was not until the end. e.g. |------------------------------------------| block <---------------------> mapped part of the block ^ ^ orig_addr invalid tlb_addr Both should never happen so print a warning and bail out without trying to adjust the sizes/offsets: the first one could try to sync from orig_addr to whatever is left of the requested size, but the later really has nothing to sync there... Signed-off-by: Dominique Martinet <[email protected]> Cc: Konrad Rzeszutek Wilk <[email protected]> Reviewed-by: Bumyong Lee <[email protected] Cc: Chanho Park <[email protected]> Cc: Christoph Hellwig <[email protected]> Signed-off-by: Konrad Rzeszutek Wilk <[email protected]>
1 parent 09a4a79 commit 868c9dd

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

kernel/dma/swiotlb.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,27 @@ static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size
365365
size_t alloc_size = mem->slots[index].alloc_size;
366366
unsigned long pfn = PFN_DOWN(orig_addr);
367367
unsigned char *vaddr = phys_to_virt(tlb_addr);
368-
unsigned int tlb_offset;
368+
unsigned int tlb_offset, orig_addr_offset;
369369

370370
if (orig_addr == INVALID_PHYS_ADDR)
371371
return;
372372

373-
tlb_offset = (tlb_addr & (IO_TLB_SIZE - 1)) -
374-
swiotlb_align_offset(dev, orig_addr);
373+
tlb_offset = tlb_addr & (IO_TLB_SIZE - 1);
374+
orig_addr_offset = swiotlb_align_offset(dev, orig_addr);
375+
if (tlb_offset < orig_addr_offset) {
376+
dev_WARN_ONCE(dev, 1,
377+
"Access before mapping start detected. orig offset %u, requested offset %u.\n",
378+
orig_addr_offset, tlb_offset);
379+
return;
380+
}
381+
382+
tlb_offset -= orig_addr_offset;
383+
if (tlb_offset > alloc_size) {
384+
dev_WARN_ONCE(dev, 1,
385+
"Buffer overflow detected. Allocation size: %zu. Mapping size: %zu+%u.\n",
386+
alloc_size, size, tlb_offset);
387+
return;
388+
}
375389

376390
orig_addr += tlb_offset;
377391
alloc_size -= tlb_offset;

0 commit comments

Comments
 (0)