Re: [PATCH v2] staging: greybus: lights: avoid NULL deref

From: Rui Miguel Silva
Date: Thu Jan 08 2026 - 08:08:41 EST


Hey Chaitanya,
Thanks for the patch.

On Thu Jan 8, 2026 at 10:49 AM WET, Chaitanya Mishra wrote:

> gb_lights_light_config() stores channel_count before allocating the
> channels array. If kcalloc() fails, gb_lights_release() iterates the
> non-zero count and dereferences light->channels, which is NULL.
>
> Allocate channels first and only then publish channels_count so the
> cleanup path can't walk a NULL pointer.

Good catch, going through the error path, does looks this fix the issue.

But you need to add the changes between versions bellow -- and maybe a
link to the first version.

Reviewed-by: Rui Miguel Silva <rui.silva@xxxxxxxxxx>

Cheers,
Rui
>
> Fixes: 2870b52bae4c ("greybus: lights: add lights implementation")
> Signed-off-by: Chaitanya Mishra <chaitanyamishra.ai@xxxxxxxxx>
> ---
> drivers/staging/greybus/light.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/staging/greybus/light.c b/drivers/staging/greybus/light.c
> index e509fdc715db..38c233a706c4 100644
> --- a/drivers/staging/greybus/light.c
> +++ b/drivers/staging/greybus/light.c
> @@ -1008,14 +1008,18 @@ static int gb_lights_light_config(struct gb_lights *glights, u8 id)
> if (!strlen(conf.name))
> return -EINVAL;
>
> - light->channels_count = conf.channel_count;
> light->name = kstrndup(conf.name, NAMES_MAX, GFP_KERNEL);
> if (!light->name)
> return -ENOMEM;
> - light->channels = kcalloc(light->channels_count,
> + light->channels = kcalloc(conf.channel_count,
> sizeof(struct gb_channel), GFP_KERNEL);
> if (!light->channels)
> return -ENOMEM;
> + /*
> + * Publish channels_count only after channels allocation so cleanup
> + * doesn't walk a NULL channels pointer on allocation failure.
> + */
> + light->channels_count = conf.channel_count;
>
> /* First we collect all the configurations for all channels */
> for (i = 0; i < light->channels_count; i++) {
> --
> 2.50.1 (Apple Git-155)