linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][next][V2] gpio: sim: Fix dereference of free'd pointer config
@ 2021-04-27 11:49 Colin King
  2021-04-27 12:11 ` Andy Shevchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Colin King @ 2021-04-27 11:49 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Andy Shevchenko, linux-gpio
  Cc: kernel-janitors, linux-kernel

From: Colin Ian King <colin.king@canonical.com>

The error return of config->id dereferences the kfree'd object config.
Fix this by using a temporary variable for the id to avoid this issue.

Addresses-Coverity: ("Read from pointer aftyer free")
Fixes: a49d14276ac4 ("gpio: sim: allocate IDA numbers earlier")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
V2: Don't make id local to the if statement to improve coding style.
    Thanks to Bartosz Golaszewski for this improvement suggestion.
---
 drivers/gpio/gpio-sim.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c
index 2e2e6399e453..b21541c0b700 100644
--- a/drivers/gpio/gpio-sim.c
+++ b/drivers/gpio/gpio-sim.c
@@ -744,20 +744,22 @@ static struct config_item *
 gpio_sim_config_make_item(struct config_group *group, const char *name)
 {
 	struct gpio_sim_chip_config *config;
+	int id;
 
 	config = kzalloc(sizeof(*config), GFP_KERNEL);
 	if (!config)
 		return ERR_PTR(-ENOMEM);
 
-	config->id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
-	if (config->id < 0) {
+	id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
+	if (id < 0) {
 		kfree(config);
-		return ERR_PTR(config->id);
+		return ERR_PTR(id);
 	}
 
 	config_item_init_type_name(&config->item, name,
 				   &gpio_sim_chip_config_type);
 	config->num_lines = 1;
+	config->id = id;
 	mutex_init(&config->lock);
 
 	return &config->item;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH][next][V2] gpio: sim: Fix dereference of free'd pointer config
  2021-04-27 11:49 [PATCH][next][V2] gpio: sim: Fix dereference of free'd pointer config Colin King
@ 2021-04-27 12:11 ` Andy Shevchenko
  2021-04-27 12:20   ` Colin Ian King
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2021-04-27 12:11 UTC (permalink / raw)
  To: Colin King
  Cc: Linus Walleij, Bartosz Golaszewski, open list:GPIO SUBSYSTEM,
	kernel-janitors, Linux Kernel Mailing List

On Tue, Apr 27, 2021 at 2:49 PM Colin King <colin.king@canonical.com> wrote:
>
> From: Colin Ian King <colin.king@canonical.com>
>
> The error return of config->id dereferences the kfree'd object config.
> Fix this by using a temporary variable for the id to avoid this issue.

Thanks!
I'm wondering how I missed this... Nevertheless

Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>

> Addresses-Coverity: ("Read from pointer aftyer free")

after

> Fixes: a49d14276ac4 ("gpio: sim: allocate IDA numbers earlier")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
> V2: Don't make id local to the if statement to improve coding style.
>     Thanks to Bartosz Golaszewski for this improvement suggestion.
> ---
>  drivers/gpio/gpio-sim.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c
> index 2e2e6399e453..b21541c0b700 100644
> --- a/drivers/gpio/gpio-sim.c
> +++ b/drivers/gpio/gpio-sim.c
> @@ -744,20 +744,22 @@ static struct config_item *
>  gpio_sim_config_make_item(struct config_group *group, const char *name)
>  {
>         struct gpio_sim_chip_config *config;
> +       int id;
>
>         config = kzalloc(sizeof(*config), GFP_KERNEL);
>         if (!config)
>                 return ERR_PTR(-ENOMEM);
>
> -       config->id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
> -       if (config->id < 0) {
> +       id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
> +       if (id < 0) {
>                 kfree(config);
> -               return ERR_PTR(config->id);
> +               return ERR_PTR(id);
>         }
>
>         config_item_init_type_name(&config->item, name,
>                                    &gpio_sim_chip_config_type);
>         config->num_lines = 1;
> +       config->id = id;
>         mutex_init(&config->lock);
>
>         return &config->item;
> --
> 2.30.2
>


-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH][next][V2] gpio: sim: Fix dereference of free'd pointer config
  2021-04-27 12:11 ` Andy Shevchenko
@ 2021-04-27 12:20   ` Colin Ian King
  2021-04-27 12:57     ` Bartosz Golaszewski
  0 siblings, 1 reply; 4+ messages in thread
From: Colin Ian King @ 2021-04-27 12:20 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Linus Walleij, Bartosz Golaszewski, open list:GPIO SUBSYSTEM,
	kernel-janitors, Linux Kernel Mailing List

On 27/04/2021 13:11, Andy Shevchenko wrote:
> On Tue, Apr 27, 2021 at 2:49 PM Colin King <colin.king@canonical.com> wrote:
>>
>> From: Colin Ian King <colin.king@canonical.com>
>>
>> The error return of config->id dereferences the kfree'd object config.
>> Fix this by using a temporary variable for the id to avoid this issue.
> 
> Thanks!
> I'm wondering how I missed this... Nevertheless
> 
> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> 
>> Addresses-Coverity: ("Read from pointer aftyer free")
> 
> after
> 

Can that be fixed before applying rather me sending a V3?

>> Fixes: a49d14276ac4 ("gpio: sim: allocate IDA numbers earlier")
>> Signed-off-by: Colin Ian King <colin.king@canonical.com>
>> ---
>> V2: Don't make id local to the if statement to improve coding style.
>>     Thanks to Bartosz Golaszewski for this improvement suggestion.
>> ---
>>  drivers/gpio/gpio-sim.c | 8 +++++---
>>  1 file changed, 5 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c
>> index 2e2e6399e453..b21541c0b700 100644
>> --- a/drivers/gpio/gpio-sim.c
>> +++ b/drivers/gpio/gpio-sim.c
>> @@ -744,20 +744,22 @@ static struct config_item *
>>  gpio_sim_config_make_item(struct config_group *group, const char *name)
>>  {
>>         struct gpio_sim_chip_config *config;
>> +       int id;
>>
>>         config = kzalloc(sizeof(*config), GFP_KERNEL);
>>         if (!config)
>>                 return ERR_PTR(-ENOMEM);
>>
>> -       config->id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
>> -       if (config->id < 0) {
>> +       id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
>> +       if (id < 0) {
>>                 kfree(config);
>> -               return ERR_PTR(config->id);
>> +               return ERR_PTR(id);
>>         }
>>
>>         config_item_init_type_name(&config->item, name,
>>                                    &gpio_sim_chip_config_type);
>>         config->num_lines = 1;
>> +       config->id = id;
>>         mutex_init(&config->lock);
>>
>>         return &config->item;
>> --
>> 2.30.2
>>
> 
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH][next][V2] gpio: sim: Fix dereference of free'd pointer config
  2021-04-27 12:20   ` Colin Ian King
@ 2021-04-27 12:57     ` Bartosz Golaszewski
  0 siblings, 0 replies; 4+ messages in thread
From: Bartosz Golaszewski @ 2021-04-27 12:57 UTC (permalink / raw)
  To: Colin Ian King
  Cc: Andy Shevchenko, Linus Walleij, open list:GPIO SUBSYSTEM,
	kernel-janitors, Linux Kernel Mailing List

On Tue, Apr 27, 2021 at 2:20 PM Colin Ian King <colin.king@canonical.com> wrote:
>
> On 27/04/2021 13:11, Andy Shevchenko wrote:
> > On Tue, Apr 27, 2021 at 2:49 PM Colin King <colin.king@canonical.com> wrote:
> >>
> >> From: Colin Ian King <colin.king@canonical.com>
> >>
> >> The error return of config->id dereferences the kfree'd object config.
> >> Fix this by using a temporary variable for the id to avoid this issue.
> >
> > Thanks!
> > I'm wondering how I missed this... Nevertheless
> >
> > Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> >
> >> Addresses-Coverity: ("Read from pointer aftyer free")
> >
> > after
> >
>
> Can that be fixed before applying rather me sending a V3?
>

No need, I'll add it and apply right away.

Bartosz

> >> Fixes: a49d14276ac4 ("gpio: sim: allocate IDA numbers earlier")
> >> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> >> ---
> >> V2: Don't make id local to the if statement to improve coding style.
> >>     Thanks to Bartosz Golaszewski for this improvement suggestion.
> >> ---
> >>  drivers/gpio/gpio-sim.c | 8 +++++---
> >>  1 file changed, 5 insertions(+), 3 deletions(-)
> >>
> >> diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c
> >> index 2e2e6399e453..b21541c0b700 100644
> >> --- a/drivers/gpio/gpio-sim.c
> >> +++ b/drivers/gpio/gpio-sim.c
> >> @@ -744,20 +744,22 @@ static struct config_item *
> >>  gpio_sim_config_make_item(struct config_group *group, const char *name)
> >>  {
> >>         struct gpio_sim_chip_config *config;
> >> +       int id;
> >>
> >>         config = kzalloc(sizeof(*config), GFP_KERNEL);
> >>         if (!config)
> >>                 return ERR_PTR(-ENOMEM);
> >>
> >> -       config->id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
> >> -       if (config->id < 0) {
> >> +       id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
> >> +       if (id < 0) {
> >>                 kfree(config);
> >> -               return ERR_PTR(config->id);
> >> +               return ERR_PTR(id);
> >>         }
> >>
> >>         config_item_init_type_name(&config->item, name,
> >>                                    &gpio_sim_chip_config_type);
> >>         config->num_lines = 1;
> >> +       config->id = id;
> >>         mutex_init(&config->lock);
> >>
> >>         return &config->item;
> >> --
> >> 2.30.2
> >>
> >
> >
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-04-27 12:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-27 11:49 [PATCH][next][V2] gpio: sim: Fix dereference of free'd pointer config Colin King
2021-04-27 12:11 ` Andy Shevchenko
2021-04-27 12:20   ` Colin Ian King
2021-04-27 12:57     ` Bartosz Golaszewski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).