mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] vfio/mlx5: Fix an unwind issue in mlx5vf_add_pages()
@ 2026-11-16  3:01 Hungyu Lin
  2026-11-23  9:55 ` Yishai Hadas
  0 siblings, 1 reply; 2+ messages in thread
From: Hungyu Lin @ 2026-11-16  3:01 UTC (permalink / raw)
  To: yishaih, alex
  Cc: jgg, skolothumtho, kevin.tian, leon, kvm, linux-kernel, dennylin0707

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")
Cc: stable@vger.kernel.org
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
 drivers/vfio/pci/mlx5/cmd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/vfio/pci/mlx5/cmd.c b/drivers/vfio/pci/mlx5/cmd.c
index 04180035a7fe..8e8514f2660f 100644
--- a/drivers/vfio/pci/mlx5/cmd.c
+++ b/drivers/vfio/pci/mlx5/cmd.c
@@ -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;
-- 
2.43.0


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

* Re: [PATCH] vfio/mlx5: Fix an unwind issue in mlx5vf_add_pages()
  2026-11-16  3:01 [PATCH] vfio/mlx5: Fix an unwind issue in mlx5vf_add_pages() Hungyu Lin
@ 2026-11-23  9:55 ` Yishai Hadas
  0 siblings, 0 replies; 2+ messages in thread
From: Yishai Hadas @ 2026-11-23  9:55 UTC (permalink / raw)
  To: Hungyu Lin, alex; +Cc: jgg, skolothumtho, kevin.tian, leon, kvm, linux-kernel

On 16/11/2026 5:01, Hungyu Lin 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")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
> ---
>   drivers/vfio/pci/mlx5/cmd.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/vfio/pci/mlx5/cmd.c b/drivers/vfio/pci/mlx5/cmd.c
> index 04180035a7fe..8e8514f2660f 100644
> --- a/drivers/vfio/pci/mlx5/cmd.c
> +++ b/drivers/vfio/pci/mlx5/cmd.c
> @@ -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;

Reviewed-by: Yishai Hadas <yishaih@nvidia.com>

Yishai

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

end of thread, other threads:[~2026-11-23  9:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16  3:01 [PATCH] vfio/mlx5: Fix an unwind issue in mlx5vf_add_pages() Hungyu Lin
2026-11-23  9:55 ` Yishai Hadas

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

all inboxes | Powered by JetHome®