Skip to contentSkip to conversation

gpio: sim: Fix dereference of free'd pointer config

PATCH In review Colin King 3 participants 4 messages Apr 27, 2021

Subsystems (2)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH v2 1/1 gpio: sim: Fix dereference of free'd pointer config

From: Colin Ian King <[email protected]>

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")

---
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(-)
drivers/gpio/gpio-sim.c +5 -3
index 2e2e6399e453..b21541c0b700 100644
@@ -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;
Signature · 1 line
2.30.2
Reviewed-by on v2 1/1
#1 lore raw
Quoted 5 lines · Colin King

From: Colin Ian King <[email protected]>

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

Quoted 1 line

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

after

Quoted 42 lines
Fixes a49d14276ac4 ("gpio: sim: allocate IDA numbers earlier")

---
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(-)
drivers/gpio/gpio-sim.c +5 -4
index 2e2e6399e453..b21541c0b700 100644
@@ -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

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 7 lines · Andy Shevchenko
Quoted 5 lines · Colin King

From: Colin Ian King <[email protected]>

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.

Quoted 9 lines

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

Quoted 1 line

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

after

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

Quoted 44 lines
Quoted 42 lines
Fixes a49d14276ac4 ("gpio: sim: allocate IDA numbers earlier")

---
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(-)
drivers/gpio/gpio-sim.c +5 -4
index 2e2e6399e453..b21541c0b700 100644
@@ -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

#3 lore raw
Quoted 12 lines · Colin Ian King
Quoted 10 lines · Andy Shevchenko
Quoted 5 lines · Colin King

From: Colin Ian King <[email protected]>

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

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

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

Quoted 45 lines
Quoted 44 lines
Quoted 42 lines
Fixes a49d14276ac4 ("gpio: sim: allocate IDA numbers earlier")

---
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(-)
drivers/gpio/gpio-sim.c +5 -4
index 2e2e6399e453..b21541c0b700 100644
@@ -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