linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] staging: greybus: light: fix a couple double frees
@ 2019-08-29 12:28 Dan Carpenter
  2019-08-29 14:53 ` Rui Miguel Silva
  0 siblings, 1 reply; 2+ messages in thread
From: Dan Carpenter @ 2019-08-29 12:28 UTC (permalink / raw)
  To: Rui Miguel Silva
  Cc: Johan Hovold, Alex Elder, Greg Kroah-Hartman, greybus-dev, devel,
	linux-kernel, kernel-janitors

The problem is in gb_lights_request_handler().  If we get a request to
change the config then we release the light with gb_lights_light_release()
and re-allocated it.  However, if the allocation fails part way through
then we call gb_lights_light_release() again.  This can lead to a couple
different double frees where we haven't cleared out the original values:

	gb_lights_light_v4l2_unregister(light);
	...
	kfree(light->channels);
	kfree(light->name);

I also made a small change to how we set "light->channels_count = 0;".
The original code handled this part fine and did not cause a use after
free but it was sort of complicated to read.

Fixes: 2870b52bae4c ("greybus: lights: add lights implementation")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
 drivers/staging/greybus/light.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/staging/greybus/light.c b/drivers/staging/greybus/light.c
index 010ae1e9c7fb..40680eaf3974 100644
--- a/drivers/staging/greybus/light.c
+++ b/drivers/staging/greybus/light.c
@@ -1098,21 +1098,21 @@ static void gb_lights_channel_release(struct gb_channel *channel)
 static void gb_lights_light_release(struct gb_light *light)
 {
 	int i;
-	int count;
 
 	light->ready = false;
 
-	count = light->channels_count;
-
 	if (light->has_flash)
 		gb_lights_light_v4l2_unregister(light);
+	light->has_flash = false;
 
-	for (i = 0; i < count; i++) {
+	for (i = 0; i < light->channels_count; i++)
 		gb_lights_channel_release(&light->channels[i]);
-		light->channels_count--;
-	}
+	light->channels_count = 0;
+
 	kfree(light->channels);
+	light->channels = NULL;
 	kfree(light->name);
+	light->name = NULL;
 }
 
 static void gb_lights_release(struct gb_lights *glights)
-- 
2.20.1


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

* Re: [PATCH] staging: greybus: light: fix a couple double frees
  2019-08-29 12:28 [PATCH] staging: greybus: light: fix a couple double frees Dan Carpenter
@ 2019-08-29 14:53 ` Rui Miguel Silva
  0 siblings, 0 replies; 2+ messages in thread
From: Rui Miguel Silva @ 2019-08-29 14:53 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Johan Hovold, Alex Elder, Greg Kroah-Hartman, greybus-dev, devel,
	linux-kernel, kernel-janitors

Hi Dan,
On Thu 29 Aug 2019 at 13:28, Dan Carpenter wrote:
> The problem is in gb_lights_request_handler().  If we get a request to
> change the config then we release the light with gb_lights_light_release()
> and re-allocated it.  However, if the allocation fails part way through
> then we call gb_lights_light_release() again.  This can lead to a couple
> different double frees where we haven't cleared out the original values:
>
> 	gb_lights_light_v4l2_unregister(light);
> 	...
> 	kfree(light->channels);
> 	kfree(light->name);
>
> I also made a small change to how we set "light->channels_count = 0;".
> The original code handled this part fine and did not cause a use after
> free but it was sort of complicated to read.
>
> Fixes: 2870b52bae4c ("greybus: lights: add lights implementation")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
>

Thanks so much for this, I was looking for some time at this and
was half way to a much less elegant fix then yours.

Acked-by: Rui Miguel Silva <rmfrfs@gmail.com>

Cheers,
    Rui

> ---
>  drivers/staging/greybus/light.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/staging/greybus/light.c b/drivers/staging/greybus/light.c
> index 010ae1e9c7fb..40680eaf3974 100644
> --- a/drivers/staging/greybus/light.c
> +++ b/drivers/staging/greybus/light.c
> @@ -1098,21 +1098,21 @@ static void gb_lights_channel_release(struct gb_channel *channel)
>  static void gb_lights_light_release(struct gb_light *light)
>  {
>  	int i;
> -	int count;
>
>  	light->ready = false;
>
> -	count = light->channels_count;
> -
>  	if (light->has_flash)
>  		gb_lights_light_v4l2_unregister(light);
> +	light->has_flash = false;
>
> -	for (i = 0; i < count; i++) {
> +	for (i = 0; i < light->channels_count; i++)
>  		gb_lights_channel_release(&light->channels[i]);
> -		light->channels_count--;
> -	}
> +	light->channels_count = 0;
> +
>  	kfree(light->channels);
> +	light->channels = NULL;
>  	kfree(light->name);
> +	light->name = NULL;
>  }
>
>  static void gb_lights_release(struct gb_lights *glights)


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

end of thread, other threads:[~2019-08-29 14:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-29 12:28 [PATCH] staging: greybus: light: fix a couple double frees Dan Carpenter
2019-08-29 14:53 ` Rui Miguel Silva

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