linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 9/9] dmapool: debug: prevent endless loop in case of corruption
@ 2018-11-12 15:46 Tony Battersby
  2018-11-13  6:36 ` Matthew Wilcox
  0 siblings, 1 reply; 10+ messages in thread
From: Tony Battersby @ 2018-11-12 15:46 UTC (permalink / raw)
  To: Matthew Wilcox, Christoph Hellwig, Marek Szyprowski, iommu, linux-mm
  Cc: linux-scsi

Prevent a possible endless loop with DMAPOOL_DEBUG enabled if a buggy
driver corrupts DMA pool memory.

Signed-off-by: Tony Battersby <tonyb@cybernetics.com>
---
--- linux/mm/dmapool.c.orig	2018-08-06 17:52:53.000000000 -0400
+++ linux/mm/dmapool.c	2018-08-06 17:53:31.000000000 -0400
@@ -454,17 +454,39 @@ void dma_pool_free(struct dma_pool *pool
 	{
 		void *page_vaddr = vaddr - offset;
 		unsigned int chain = page->dma_free_off;
+		unsigned int free_blks = 0;
+
 		while (chain < pool->allocation) {
-			if (chain != offset) {
-				chain = *(int *)(page_vaddr + chain);
-				continue;
+			if (unlikely(chain == offset)) {
+				spin_unlock_irqrestore(&pool->lock, flags);
+				dev_err(pool->dev,
+					"dma_pool_free %s, dma %pad already free\n",
+					pool->name, &dma);
+				return;
+			}
+
+			/*
+			 * A buggy driver could corrupt the freelist by
+			 * use-after-free, buffer overflow, etc.  Besides
+			 * checking for corruption, this also prevents an
+			 * endless loop in case corruption causes a circular
+			 * loop in the freelist.
+			 */
+			if (unlikely(++free_blks + page->dma_in_use >
+				     pool->blks_per_alloc)) {
+ freelist_corrupt:
+				spin_unlock_irqrestore(&pool->lock, flags);
+				dev_err(pool->dev,
+					"dma_pool_free %s, freelist corrupted\n",
+					pool->name);
+				return;
 			}
-			spin_unlock_irqrestore(&pool->lock, flags);
-			dev_err(pool->dev,
-				"dma_pool_free %s, dma %pad already free\n",
-				pool->name, &dma);
-			return;
+
+			chain = *(int *)(page_vaddr + chain);
 		}
+		if (unlikely(free_blks + page->dma_in_use !=
+			     pool->blks_per_alloc))
+			goto freelist_corrupt;
 	}
 	memset(vaddr, POOL_POISON_FREED, pool->size);
 #endif

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2018-12-04 22:05 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-12 15:46 [PATCH v4 9/9] dmapool: debug: prevent endless loop in case of corruption Tony Battersby
2018-11-13  6:36 ` Matthew Wilcox
2018-12-04 16:22   ` Tony Battersby
2018-12-04 20:14     ` Andrew Morton
2018-12-04 20:18       ` Matthew Wilcox
2018-12-04 20:28         ` Andrew Morton
2018-12-04 20:35           ` Matthew Wilcox
2018-12-04 20:30         ` Andy Shevchenko
2018-12-04 21:26           ` Tony Battersby
2018-12-04 22:05             ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox