diff mbox series

xen/grants: prevent integer overflow in gnttab_dma_alloc_pages()

Message ID YxDROJqu/RPvR0bi@kili (mailing list archive)
State Accepted
Commit e9ea0b30ada008f4e65933f449db6894832cb242
Headers show
Series xen/grants: prevent integer overflow in gnttab_dma_alloc_pages() | expand

Commit Message

Dan Carpenter Sept. 1, 2022, 3:35 p.m. UTC
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()")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
 drivers/xen/grant-table.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Jürgen Groß Sept. 2, 2022, 7:01 a.m. UTC | #1
On 01.09.22 17:35, Dan Carpenter wrote:
> 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()")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

Reviewed-by: Juergen Gross <jgross@suse.com>


Juergen
diff mbox series

Patch

diff --git a/drivers/xen/grant-table.c b/drivers/xen/grant-table.c
index 738029de3c67..e1ec725c2819 100644
--- a/drivers/xen/grant-table.c
+++ b/drivers/xen/grant-table.c
@@ -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,