Skip to contentSkip to conversation

staging: greybus: fix a missing-check bug in gb_lights_light_config()

PATCH In review Chen Zhou 3 participants 6 messages Apr 1, 2020 – Apr 2, 2020

Subsystems (3)
lore
Share & tools
lore

Chen Zhou

author
#0 lore raw

PATCH 1/1 staging: greybus: fix a missing-check bug in gb_lights_light_config()

In gb_lights_light_config(), 'light->name' is allocated by kstrndup(). It returns NULL when fails, add check for it.

---
 drivers/staging/greybus/light.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/greybus/light.c +2 -1
index d6ba25f..d2672b6 100644
@@ -1026,7 +1026,8 @@ static int gb_lights_light_config(struct gb_lights *glights, u8 id)
 
 	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,
 				  sizeof(struct gb_channel), GFP_KERNEL);
 	if (!light->channels)
Signature · 1 line
2.7.4
maintainer Acked-by on v1 1/1
#1 lore raw

Hi Chen Zhou,
Thanks for the patch.

Quoted 4 lines · Chen Zhou

In gb_lights_light_config(), 'light->name' is allocated by kstrndup(). It returns NULL when fails, add check for it.

------
Cheers,
Rui

Quoted 21 lines
---
 drivers/staging/greybus/light.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/greybus/light.c +2 -1
index d6ba25f..d2672b6 100644
@@ -1026,7 +1026,8 @@ static int gb_lights_light_config(struct gb_lights *glights, u8 id)
 
 	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,
 				  sizeof(struct gb_channel), GFP_KERNEL);
 	if (!light->channels)
Signature · 1 line
2.7.4
#2 lore raw
In reply to Chen Zhou
Quoted 13 lines · Chen Zhou

In gb_lights_light_config(), 'light->name' is allocated by kstrndup(). It returns NULL when fails, add check for it.

---
 drivers/staging/greybus/light.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/greybus/light.c +0 -0
index d6ba25f..d2672b6 100644

drivers/staging/greybus/light.c static int gb_lights_light_config(struct gb_lights *glights, u8 id) · line 1033

Quoted 9 lines
drivers/staging/greybus/light.c +2 -1
@@ -1026,7 +1026,8 @@ static int gb_lights_light_config(struct gb_lights *glights, u8 id)
 
 	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,
 				  sizeof(struct gb_channel), GFP_KERNEL);
 	if (!light->channels)

The clean up in this function is non-existant. :(

regards,
dan carpenter

maintainer
#3 lore raw

Hi Dan,

Quoted 15 lines · Dan Carpenter
Quoted 14 lines · Chen Zhou

In gb_lights_light_config(), 'light->name' is allocated by kstrndup(). It returns NULL when fails, add check for it.

---
 drivers/staging/greybus/light.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/greybus/light.c +0 -0
index d6ba25f..d2672b6 100644
@@ -1026,7 +1026,8 @@ static int gb_lights_light_config(struct gb_lights *glights, u8 id)
 
Quoted 10 lines

drivers/staging/greybus/light.c static int gb_lights_light_config(struct gb_lights *glights, u8 id) · line 1033

Quoted 8 lines
drivers/staging/greybus/light.c +2 -1
 	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,
 				  sizeof(struct gb_channel), GFP_KERNEL);
 	if (!light->channels)

The clean up in this function is non-existant. :(

Yeah, this have a central point to do the cleanups, gb_lights_release, since we may have other lights already configured at this point, we could cleanup this specific one here, but than would need to make sure all other already configure got clean also.

------
Cheers,
Rui

#4 lore raw
Quoted 15 lines · Rui Miguel Silva
Quoted 4 lines
Quoted 4 lines
drivers/staging/greybus/light.c +0 -0
@@ -1026,7 +1026,8 @@ static int gb_lights_light_config(struct gb_lights *glights, u8 id)
 
Quoted 10 lines

drivers/staging/greybus/light.c static int gb_lights_light_config(struct gb_lights *glights, u8 id) · line 1033

Quoted 8 lines
drivers/staging/greybus/light.c +2 -1
 	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,
 				  sizeof(struct gb_channel), GFP_KERNEL);
 	if (!light->channels)

The clean up in this function is non-existant. :(

Quoted 4 lines

Yeah, this have a central point to do the cleanups, gb_lights_release, since we may have other lights already configured at this point, we could cleanup this specific one here, but than would need to make sure all other already configure got clean also.

Central clean up functions never work correctly.

For example, we allocate "cdev->name" in gb_lights_channel_config() before we register the channel later in gb_lights_register_all(glights);. Now imagine that the register fails. Then when we're freeing it in __gb_lights_led_unregister() we see that the ->is_registered is false so we don't kfree(cdev->name).

That's just a small memory leak. But there are going to be tons of little bugs like that.

Anyway it doesn't affect this patch so it's fine.

regards,
dan carpenter

maintainer
#5 lore raw

Hi,

Quoted 16 lines · Dan Carpenter
Quoted 15 lines · Rui Miguel Silva
Quoted 14 lines

drivers/staging/greybus/light.c static int gb_lights_light_config(struct gb_lights *glights, u8 id) · line 1033

Quoted 12 lines
drivers/staging/greybus/light.c +2 -1
@@ -1026,7 +1026,8 @@ static int gb_lights_light_config(struct gb_lights *glights, u8 id)
 
 	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,
 				  sizeof(struct gb_channel), GFP_KERNEL);
 	if (!light->channels)

The clean up in this function is non-existant. :(

Quoted 6 lines
Quoted 4 lines

Yeah, this have a central point to do the cleanups, gb_lights_release, since we may have other lights already configured at this point, we could cleanup this specific one here, but than would need to make sure all other already configure got clean also.

Central clean up functions never work correctly.

I agree.

Quoted 9 lines

For example, we allocate "cdev->name" in gb_lights_channel_config() before we register the channel later in gb_lights_register_all(glights);. Now imagine that the register fails. Then when we're freeing it in __gb_lights_led_unregister() we see that the ->is_registered is false so we don't kfree(cdev->name).

That's just a small memory leak. But there are going to be tons of little bugs like that.

Yeah, when I have some cycles I'll go over that error codes paths and mitigate this kind of issues.

Quoted 2 lines

Anyway it doesn't affect this patch so it's fine.

Yeah, thanks.

------
Cheers,
Rui