Re: [PATCH v4] sfc: potential dereference null pointer of rx_queue->page_ring

From: Martin Habets
Date: Mon Dec 20 2021 - 06:20:23 EST


On Mon, Dec 20, 2021 at 10:37:15AM +0800, Jiasheng Jiang wrote:
> The return value of kcalloc() needs to be checked.

Maybe my previous reply against v2 crossed with your later versions.

Your predicate is wrong. The code that uses rx_queue->page_ring
can deal with it being NULL.
The only thing you might want to do is set rx_queue->page_ptr_mask
to 0.
It is a mask, never use a signed value for it.

Martin

> To avoid dereference of null pointer in case of the failure of alloc,
> such as efx_fini_rx_recycle_ring().
> Therefore, it should be better to change the definition of page_ptr_mask
> to signed int and then assign the page_ptr_mask to -1 when page_ring is
> NULL, in order to avoid the use in the loop.
>
> Fixes: 5a6681e22c14 ("sfc: separate out SFC4000 ("Falcon") support into new sfc-falcon driver")
> Signed-off-by: Jiasheng Jiang <jiasheng@xxxxxxxxxxx>
> ---
> Changelog:
>
> v3 -> v4
>
> *Change 1. Casade return -ENOMEM when alloc fails and deal with the
> error.
> *Change 2. Set size to -1 instead of return error.
> *Change 3. Change the Fixes tag.
> ---
> drivers/net/ethernet/sfc/net_driver.h | 2 +-
> drivers/net/ethernet/sfc/rx_common.c | 5 ++++-
> 2 files changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/net/ethernet/sfc/net_driver.h b/drivers/net/ethernet/sfc/net_driver.h
> index 9b4b25704271..beba3e0a6027 100644
> --- a/drivers/net/ethernet/sfc/net_driver.h
> +++ b/drivers/net/ethernet/sfc/net_driver.h
> @@ -407,7 +407,7 @@ struct efx_rx_queue {
> unsigned int page_recycle_count;
> unsigned int page_recycle_failed;
> unsigned int page_recycle_full;
> - unsigned int page_ptr_mask;
> + int page_ptr_mask;
> unsigned int max_fill;
> unsigned int fast_fill_trigger;
> unsigned int min_fill;
> diff --git a/drivers/net/ethernet/sfc/rx_common.c b/drivers/net/ethernet/sfc/rx_common.c
> index 68fc7d317693..d9d0a5805f1c 100644
> --- a/drivers/net/ethernet/sfc/rx_common.c
> +++ b/drivers/net/ethernet/sfc/rx_common.c
> @@ -150,7 +150,10 @@ static void efx_init_rx_recycle_ring(struct efx_rx_queue *rx_queue)
> efx->rx_bufs_per_page);
> rx_queue->page_ring = kcalloc(page_ring_size,
> sizeof(*rx_queue->page_ring), GFP_KERNEL);
> - rx_queue->page_ptr_mask = page_ring_size - 1;
> + if (!rx_queue->page_ring)
> + rx_queue->page_ptr_mask = -1;
> + else
> + rx_queue->page_ptr_mask = page_ring_size - 1;
> }
>
> static void efx_fini_rx_recycle_ring(struct efx_rx_queue *rx_queue)
> --
> 2.25.1