Re: [PATCH v2] powerpc/mm: Fix null-pointer dereference in pgtable_cache_add

From: Michael Ellerman
Date: Fri Dec 01 2023 - 05:17:34 EST


Kunwu Chan <chentao@xxxxxxxxxx> writes:
> kasprintf() returns a pointer to dynamically allocated memory
> which can be NULL upon failure. Ensure the allocation was successful
> by checking the pointer validity.
>
> Suggested-by: Christophe Leroy <christophe.leroy@xxxxxxxxxx>
> Suggested-by: Michael Ellerman <mpe@xxxxxxxxxxxxxx>
> Signed-off-by: Kunwu Chan <chentao@xxxxxxxxxx>
> ---
> v2: Use "panic" instead of "return"
> ---
> arch/powerpc/mm/init-common.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/arch/powerpc/mm/init-common.c b/arch/powerpc/mm/init-common.c
> index 119ef491f797..9788950b33f5 100644
> --- a/arch/powerpc/mm/init-common.c
> +++ b/arch/powerpc/mm/init-common.c
> @@ -139,6 +139,8 @@ void pgtable_cache_add(unsigned int shift)
>
> align = max_t(unsigned long, align, minalign);
> name = kasprintf(GFP_KERNEL, "pgtable-2^%d", shift);
> + if (!name)
> + panic("Failed to allocate memory for order %d", shift);
> new = kmem_cache_create(name, table_size, align, 0, ctor(shift));
> if (!new)
> panic("Could not allocate pgtable cache for order %d", shift);

It would be nice to avoid two calls to panic. Can you reorganise the
logic so that there's only one? Initialising new to NULL might help.

cheers