From: Petr Tesarik Date: Wed, 8 Nov 2023 11:12:49 +0000 (+0100) Subject: swiotlb: fix out-of-bounds TLB allocations with CONFIG_SWIOTLB_DYNAMIC X-Git-Tag: v6.6.7~557 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ce7612496a4ba6068bc68aa1fa9d947dadb4ad9b;p=platform%2Fkernel%2Flinux-starfive.git swiotlb: fix out-of-bounds TLB allocations with CONFIG_SWIOTLB_DYNAMIC commit 53c87e846e335e3c18044c397cc35178163d7827 upstream. Limit the free list length to the size of the IO TLB. Transient pool can be smaller than IO_TLB_SEGSIZE, but the free list is initialized with the assumption that the total number of slots is a multiple of IO_TLB_SEGSIZE. As a result, swiotlb_area_find_slots() may allocate slots past the end of a transient IO TLB buffer. Reported-by: Niklas Schnelle Closes: https://lore.kernel.org/linux-iommu/104a8c8fedffd1ff8a2890983e2ec1c26bff6810.camel@linux.ibm.com/ Fixes: 79636caad361 ("swiotlb: if swiotlb is full, fall back to a transient memory pool") Cc: stable@vger.kernel.org Signed-off-by: Petr Tesarik Reviewed-by: Halil Pasic Signed-off-by: Christoph Hellwig Signed-off-by: Greg Kroah-Hartman --- diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 0e1632f..2048194 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -283,7 +283,8 @@ static void swiotlb_init_io_tlb_pool(struct io_tlb_pool *mem, phys_addr_t start, } for (i = 0; i < mem->nslabs; i++) { - mem->slots[i].list = IO_TLB_SEGSIZE - io_tlb_offset(i); + mem->slots[i].list = min(IO_TLB_SEGSIZE - io_tlb_offset(i), + mem->nslabs - i); mem->slots[i].orig_addr = INVALID_PHYS_ADDR; mem->slots[i].alloc_size = 0; }