Skip to contentSkip to conversation

xen/grants: prevent integer overflow in gnttab_dma_alloc_pages()

PATCH In review Dan Carpenter 2 participants 2 messages Sep 1, 2022 – Sep 2, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/grants: prevent integer overflow in gnttab_dma_alloc_pages()

The change from kcalloc() to kvmalloc() means that arg->nr_pages might now be large enough that the "args->nr_pages << PAGE_SHIFT" can result in an integer overflow.

Fixes b3f7931f5c61 ("xen/gntdev: switch from kcalloc() to kvcalloc()")
---
 drivers/xen/grant-table.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/xen/grant-table.c +3 -0
index 738029de3c67..e1ec725c2819 100644
@@ -1047,6 +1047,9 @@ int gnttab_dma_alloc_pages(struct gnttab_dma_alloc_args *args)
 	size_t size;
 	int i, ret;
 
+	if (args->nr_pages < 0 || args->nr_pages > (INT_MAX >> PAGE_SHIFT))
+		return -ENOMEM;
+
 	size = args->nr_pages << PAGE_SHIFT;
 	if (args->coherent)
 		args->vaddr = dma_alloc_coherent(args->dev, size,
Signature · 1 line
2.35.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Dan Carpenter

The change from kcalloc() to kvmalloc() means that arg->nr_pages might now be large enough that the "args->nr_pages << PAGE_SHIFT" can result in an integer overflow.

Fixes b3f7931f5c61 ("xen/gntdev: switch from kcalloc() to kvcalloc()")

Juergen