Re: [PATCH] ASoC: meson: Keep link pointers valid on realloc failure
From: Jerome Brunet
Date: Thu Jul 16 2026 - 11:32:39 EST
On jeu. 16 juil. 2026 at 18:09, Linmao Li <lilinmao@xxxxxxxxxx> wrote:
> meson_card_reallocate_links() grows the DAI link and private data
> arrays with two consecutive krealloc() calls and updates the owner
> pointers only after both calls have succeeded.
>
> A successful krealloc() may move the data: it frees the old block and
> returns a new one. When that happens for the link array and the second
> krealloc() then fails, card->dai_link still points to the block that
> krealloc() already freed, and the error path frees the new block too.
> The probe error path then calls meson_card_clean_references(), which
> dereferences card->dai_link and kfree()s it again, resulting in a
> use-after-free and a double free.
>
> Set card->dai_link right after the first krealloc() succeeds and keep
> the resized array when the second call fails, so the pointer always
> refers to a valid allocation that the normal cleanup path can free.
> card->num_links is still updated only on full success, which keeps the
> cleanup limited to initialized links.
>
> Fixes: 7864a79f37b5 ("ASoC: meson: add axg sound card support")
> Signed-off-by: Linmao Li <lilinmao@xxxxxxxxxx>
Thanks for this
With this fix there is not much point in keeping the labels doing the
goto error handling. Please remove those (and the err print) and just return
-ENOMEM.
> ---
> sound/soc/meson/meson-card-utils.c | 7 +++----
> 1 file changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/sound/soc/meson/meson-card-utils.c b/sound/soc/meson/meson-card-utils.c
> index cdb759b466ad..38c1b2ae227f 100644
> --- a/sound/soc/meson/meson-card-utils.c
> +++ b/sound/soc/meson/meson-card-utils.c
> @@ -52,19 +52,18 @@ int meson_card_reallocate_links(struct snd_soc_card *card,
> if (!links)
> goto err_links;
>
> + priv->card.dai_link = links;
You must assign num_links too for this to be consistent.
> +
> ldata = krealloc(priv->link_data,
> num_links * sizeof(*priv->link_data),
> GFP_KERNEL | __GFP_ZERO);
> if (!ldata)
Adding a comment here saying that meson_card_clean_references() will
take care of the above on the exist path would be helpful
> - goto err_ldata;
> + goto err_links;
>
> - priv->card.dai_link = links;
> priv->link_data = ldata;
> priv->card.num_links = num_links;
> return 0;
>
> -err_ldata:
> - kfree(links);
> err_links:
> dev_err(priv->card.dev, "failed to allocate links\n");
> return -ENOMEM;
With this
Reviewed-by: Jerome Brunet <jbrunet@xxxxxxxxxxxx>
--
Jerome