Re: [PATCH] drivers/staging/greybus: fix max dup length for kstrndup

From: Rui Miguel Silva
Date: Thu Dec 14 2017 - 05:49:54 EST


Hi Ma,
Thanks for your patch.

Please make sure you use scripts/get_maintainer.pl so that your patches
goes to the right lists and people. I CC's them now. ;)


On Tue 12 Dec 2017 at 09:25, Ma Shimiao wrote:
> If source string longer than max, kstrndup will alloc max+1 space.
> So, we should make sure the result will not over limit.

I think we are good here. kstrndup alloc memory for us the max+1, and
in fact we want to have the 32 chars plus the \0 set by kstrndup.

So, I think the code as is now is ok.

---
Cheers,
Rui

>
> Signed-off-by: Ma Shimiao <mashimiao.fnst@xxxxxxxxxxxxxx>
> ---
> drivers/staging/greybus/light.c | 9 ++++++---
> drivers/staging/greybus/power_supply.c | 10 ++++++----
> 2 files changed, 12 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/staging/greybus/light.c b/drivers/staging/greybus/light.c
> index 010ae1e9c7fb..c7ac2ead5a07 100644
> --- a/drivers/staging/greybus/light.c
> +++ b/drivers/staging/greybus/light.c
> @@ -965,10 +965,12 @@ static int gb_lights_channel_config(struct gb_light *light,
> channel->mode = le32_to_cpu(conf.mode);
> channel->flags = le32_to_cpu(conf.flags);
> channel->color = le32_to_cpu(conf.color);
> - channel->color_name = kstrndup(conf.color_name, NAMES_MAX, GFP_KERNEL);
> + channel->color_name = kstrndup(conf.color_name,
> + NAMES_MAX - 1, GFP_KERNEL);
> if (!channel->color_name)
> return -ENOMEM;
> - channel->mode_name = kstrndup(conf.mode_name, NAMES_MAX, GFP_KERNEL);
> + channel->mode_name = kstrndup(conf.mode_name,
> + NAMES_MAX - 1, GFP_KERNEL);
> if (!channel->mode_name)
> return -ENOMEM;
>
> @@ -1027,7 +1029,8 @@ static int gb_lights_light_config(struct gb_lights *glights, u8 id)
> return -EINVAL;
>
> light->channels_count = conf.channel_count;
> - light->name = kstrndup(conf.name, NAMES_MAX, GFP_KERNEL);
> + light->name = kstrndup(conf.name,
> + NAMES_MAX - 1, GFP_KERNEL);
>
> light->channels = kcalloc(light->channels_count,
> sizeof(struct gb_channel), GFP_KERNEL);
> diff --git a/drivers/staging/greybus/power_supply.c b/drivers/staging/greybus/power_supply.c
> index 0529e5628c24..7bc76633866b 100644
> --- a/drivers/staging/greybus/power_supply.c
> +++ b/drivers/staging/greybus/power_supply.c
> @@ -487,14 +487,16 @@ static int gb_power_supply_description_get(struct gb_power_supply *gbpsy)
> if (ret < 0)
> return ret;
>
> - gbpsy->manufacturer = kstrndup(resp.manufacturer, PROP_MAX, GFP_KERNEL);
> + gbpsy->manufacturer = kstrndup(resp.manufacturer,
> + PROP_MAX - 1, GFP_KERNEL);
> if (!gbpsy->manufacturer)
> return -ENOMEM;
> - gbpsy->model_name = kstrndup(resp.model, PROP_MAX, GFP_KERNEL);
> + gbpsy->model_name = kstrndup(resp.model,
> + PROP_MAX - 1, GFP_KERNEL);
> if (!gbpsy->model_name)
> return -ENOMEM;
> - gbpsy->serial_number = kstrndup(resp.serial_number, PROP_MAX,
> - GFP_KERNEL);
> + gbpsy->serial_number = kstrndup(resp.serial_number,
> + PROP_MAX - 1, GFP_KERNEL);
> if (!gbpsy->serial_number)
> return -ENOMEM;