gpio: sim: Fix dereference of free'd pointer config
Subsystems (2)
PATCH 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")
--- drivers/gpio/gpio-sim.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpio/gpio-sim.c +3 -1
@@ -751,8 +751,10 @@ gpio_sim_config_make_item(struct config_group *group, const char *name) config->id =ida_alloc (&gpio_sim_ida,GFP_KERNEL );if (config->id <0 ) { +int id = config->id; +kfree (config); -return ERR_PTR (config->id); +return ERR_PTR (id); } config_item_init_type_name(&config->item, name,
Signature · 1 line
2.30.2
Quoted 30 lines · Colin King
From: Colin Ian King <[email protected]>
The error return of
config->iddereferences 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 <[email protected]>--- drivers/gpio/gpio-sim.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/gpio/gpio-sim.c +3
-2@@ -751,8 +751,10 @@ gpio_sim_config_make_item(struct config_group *group, const char *name) config->id =ida_alloc (&gpio_sim_ida,GFP_KERNEL );if (config->id <0 ) { +int id = config->id; +kfree (config); -return ERR_PTR (config->id); +return ERR_PTR (id); } config_item_init_type_name(&config->item, name, --
Quoted 2 lines
2.30.2
Thanks! Can you do something like this:
drivers/gpio/gpio-sim.c +5 -3
@@ -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;
I think this looks more elegant without the local variable inside the if.
Bart
Quoted 68 lines · Bartosz Golaszewski
Quoted 30 lines · Colin King
From: Colin Ian King <[email protected]>
The error return of
config->iddereferences 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 <[email protected]>--- drivers/gpio/gpio-sim.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/gpio/gpio-sim.c +3
-2@@ -751,8 +751,10 @@ gpio_sim_config_make_item(struct config_group *group, const char *name) config->id =ida_alloc (&gpio_sim_ida,GFP_KERNEL );if (config->id <0 ) { +int id = config->id; +kfree (config); -return ERR_PTR (config->id); +return ERR_PTR (id); } config_item_init_type_name(&config->item, name, --Quoted 2 lines
2.30.2
Thanks! Can you do something like this:
drivers/gpio/gpio-sim.c +5
-3@@ -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;
Quoted 1 line
I think this looks more elegant without the local variable inside the if.
OK, good idea, V2 sent.
Quoted 3 lines
Bart