Skip to contentSkip to conversation

vfio/mlx5: Fix an unwind issue in mlx5vf_add_pages()

PATCH Applied Hungyu Lin 3 participants 3 messages Nov 16 – Nov 26

Subsystems (4)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH 1/1 vfio/mlx5: Fix an unwind issue in mlx5vf_add_pages()

mlx5vf_add_pages() gets a pointer to the page list of its caller. If alloc_pages_bulk() fails after some of the pages were allocated, the unwind flow frees them with

	__free_page(*page_list[i]);

The index is applied to page_list before it is dereferenced, so only the first page is freed. For the other pages the code reads the memory that follows the page list pointer of the caller and dereferences it. In both callers this is a dma_iova_state that is still zeroed, which ends in a NULL pointer dereference, and the pages are leaked.

The pages are allocated with GFP_KERNEL_ACCOUNT, so the allocation can fail, for example when the memory cgroup of the task is out of memory.

Dereference page_list before indexing it.

Fixes 089803c40193 ("vfio/mlx5: Enable the DMA link API")
---
 drivers/vfio/pci/mlx5/cmd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/pci/mlx5/cmd.c +1 -1
index 04180035a7fe..8e8514f2660f 100644
@@ -518,7 +518,7 @@ static int mlx5vf_add_pages(struct page ***page_list, unsigned int npages)
 
 err:
 	for (i = 0; i < done; i++)
-		__free_page(*page_list[i]);
+		__free_page((*page_list)[i]);
 
 	kvfree(*page_list);
 	*page_list = NULL;
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 29 lines · Hungyu Lin

mlx5vf_add_pages() gets a pointer to the page list of its caller. If alloc_pages_bulk() fails after some of the pages were allocated, the unwind flow frees them with

	__free_page(*page_list[i]);

The index is applied to page_list before it is dereferenced, so only the first page is freed. For the other pages the code reads the memory that follows the page list pointer of the caller and dereferences it. In both callers this is a dma_iova_state that is still zeroed, which ends in a NULL pointer dereference, and the pages are leaked.

The pages are allocated with GFP_KERNEL_ACCOUNT, so the allocation can fail, for example when the memory cgroup of the task is out of memory.

Dereference page_list before indexing it.

Fixes 089803c40193 ("vfio/mlx5: Enable the DMA link API")
---
  drivers/vfio/pci/mlx5/cmd.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/pci/mlx5/cmd.c +0 -0
index 04180035a7fe..8e8514f2660f 100644

drivers/vfio/pci/mlx5/cmd.c static int mlx5vf_add_pages(struct page ***page_list, unsigned int npages) · line 524

Quoted 8 lines
drivers/vfio/pci/mlx5/cmd.c +1 -1
@@ -518,7 +518,7 @@ static int mlx5vf_add_pages(struct page ***page_list, unsigned int npages)
 
  err:
  	for (i = 0; i < done; i++)
-		__free_page(*page_list[i]);
+		__free_page((*page_list)[i]);
 
  	kvfree(*page_list);
  	*page_list = NULL;

Yishai

maintainer Applied
#2 lore raw
In reply to Hungyu Lin
Quoted 29 lines · On Mon, 16 Nov 2026 03:01:30 +0000 Hungyu Lin <[email protected]> wrote:

mlx5vf_add_pages() gets a pointer to the page list of its caller. If alloc_pages_bulk() fails after some of the pages were allocated, the unwind flow frees them with

	__free_page(*page_list[i]);

The index is applied to page_list before it is dereferenced, so only the first page is freed. For the other pages the code reads the memory that follows the page list pointer of the caller and dereferences it. In both callers this is a dma_iova_state that is still zeroed, which ends in a NULL pointer dereference, and the pages are leaked.

The pages are allocated with GFP_KERNEL_ACCOUNT, so the allocation can fail, for example when the memory cgroup of the task is out of memory.

Dereference page_list before indexing it.

Fixes 089803c40193 ("vfio/mlx5: Enable the DMA link API")
---
 drivers/vfio/pci/mlx5/cmd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/pci/mlx5/cmd.c +0 -0
index 04180035a7fe..8e8514f2660f 100644

drivers/vfio/pci/mlx5/cmd.c static int mlx5vf_add_pages(struct page ***page_list, unsigned int npages) · line 524

Quoted 8 lines
drivers/vfio/pci/mlx5/cmd.c +1 -1
@@ -518,7 +518,7 @@ static int mlx5vf_add_pages(struct page ***page_list, unsigned int npages)
 
 err:
 	for (i = 0; i < done; i++)
-		__free_page(*page_list[i]);
+		__free_page((*page_list)[i]);
 
 	kvfree(*page_list);
 	*page_list = NULL;

Applied to vfio next branch for v7.5. Thanks,

Alex