Re: [PATCH 1/2] livepatch: Fix wrong index in funcs cleanup error path

Song Liu <[email protected]>
Newsgroups org.kernel.vger.live-patching
Message-ID <CAPhsuW6G+8TAb72vT3yubH2Rk4DdUbPpnL1jmvCnbYNx4UVfjQ@mail.gmail.com>
On Wed, Aug 12, 2026 at 8:04 PM Yafang Shao <[email protected]> wrote:
>
> In the object allocation loop, when kzalloc() for funcs fails, the
> cleanup loop uses `objs[i].funcs` instead of `objs[j].funcs`. Since
> `objs[i].funcs` is still NULL at that point, it repeatedly calls
> kfree(NULL) and leaks all previously allocated funcs arrays.
>
> Fixes: 59adee07b568 ("livepatch/klp-build: Add stub init code for livepatch modules")
> Signed-off-by: Yafang Shao <[email protected]>

Acked-by: Song Liu <[email protected]>

With a nitpick/comment below:

> ---
>  scripts/livepatch/init.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/livepatch/init.c b/scripts/livepatch/init.c
> index f14d8c8fb35f..77746984aa57 100644
> --- a/scripts/livepatch/init.c
> +++ b/scripts/livepatch/init.c
> @@ -50,8 +50,6 @@ static int __init livepatch_mod_init(void)
>                 funcs = kzalloc(sizeof(struct klp_func) * (nr_funcs + 1), GFP_KERNEL);
>                 if (!funcs) {
>                         ret = -ENOMEM;
> -                       for (int j = 0; j < i; j++)
> -                               kfree(objs[i].funcs);

We really just need to replace objs[i] with obj[j] here, right?
I personally feel it is actually cleaner to keep the kfree() loop here.
Current patch is correct as-is though, so this is really a nitpick.

>                         goto err_free_objs;
>                 }
>
> @@ -81,6 +79,8 @@ static int __init livepatch_mod_init(void)
>         return klp_enable_patch(patch);
>
>  err_free_objs:
> +       for (int i = 0; i < nr_objs; i++)
> +               kfree(objs[i].funcs);
>         kfree(objs);
>  err_free_patch:
>         kfree(patch);
> --
> 2.52.0
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.