Re: [PATCH] nvmem: core: Fix memleak in nvmem_add_cells_from_dt error path

From: Markus Schneider-Pargmann
Date: Wed Sep 11 2024 - 15:46:34 EST


On Wed, Sep 11, 2024 at 08:06:55PM GMT, Ruihai Zhou wrote:
> In the nvmem_add_cells_from_dt(), if the devicetree 'bits' property
> is outside of the valid range, the info.name alloc by kasprintf will
> cause memleak. Just free before return from nvmem_add_cells_from_dt
> in the error path.

Thanks for spotting and fixing this.

Alternative to addding a kfree() I think the info.name allocation can be
moved down behind the if-block so it can't leak when returning inside of
the nested if block.

Both is fine for me.

Reviewed-by: Markus Schneider-Pargmann <msp@xxxxxxxxxxxx>

Best
Markus

>
> Fixes: def3173d4f17 ("nvmem: core: Print error on wrong bits DT property")
> Signed-off-by: Ruihai Zhou <zhou.ruihai@xxxxxx>
> ---
> drivers/nvmem/core.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 516dfd861b9f..ce71bee5e1a9 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -826,6 +826,7 @@ static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_nod
> info.nbits = be32_to_cpup(addr);
> if (info.bit_offset >= BITS_PER_BYTE || info.nbits < 1) {
> dev_err(dev, "nvmem: invalid bits on %pOF\n", child);
> + kfree(info.name);
> of_node_put(child);
> return -EINVAL;
> }
> --
> 2.31.1
>