Skip to contentSkip to conversation

intel_th: avoid double free in error flow

PATCH In review Wen Yang 2 participants 5 messages Nov 19, 2019 – Nov 20, 2019

Subsystems (2)
lore
Share & tools
lore

Wen Yang

author
#0 lore raw

PATCH 1/1 intel_th: avoid double free in error flow

There is a possible double free issue in intel_th_subdevice_alloc:

651 err = intel_th_device_add_resources(thdev, res, subdev->nres);
652 if (err) {
653 put_device(&thdev->dev);
654 goto fail_put_device; ---> freed
655 }
...
687 fail_put_device:
688 put_device(&thdev->dev); ---> double freed
689

This patch fix it by removing the unnecessary put_device().

Fixes a753bfcfdb1f ("intel_th: Make the switch allocate its subdevices")
Cc Alexander Shishkin <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/hwtracing/intel_th/core.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/hwtracing/intel_th/core.c +1 -3
index d5c1821..98d195c 100644
@@ -649,10 +649,8 @@ static inline void intel_th_request_hub_module_flush(struct intel_th *th)
 	}
 
 	err = intel_th_device_add_resources(thdev, res, subdev->nres);
-	if (err) {
-		put_device(&thdev->dev);
+	if (err)
 		goto fail_put_device;
-	}
 
 	if (subdev->type == INTEL_TH_OUTPUT) {
 		if (subdev->mknode)
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 12 lines · Wen Yang

There is a possible double free issue in intel_th_subdevice_alloc:

651 err = intel_th_device_add_resources(thdev, res, subdev->nres);
652 if (err) {
653 put_device(&thdev->dev);
654 goto fail_put_device; ---> freed
655 }
...
687 fail_put_device:
688 put_device(&thdev->dev); ---> double freed
689

Quoted 1 line

This patch fix it by removing the unnecessary put_device().

Unnecessary is a too generous term here.

Quoted 5 lines
Fixes a753bfcfdb1f ("intel_th: Make the switch allocate its subdevices")
Cc Alexander Shishkin <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc stable@ is missing.
Quoted 9 lines
---
 drivers/hwtracing/intel_th/core.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/hwtracing/intel_th/core.c +0 -0
index d5c1821..98d195c 100644

drivers/hwtracing/intel_th/core.c static inline void intel_th_request_hub_module_flush(struct intel_th *th) · line 655

Quoted 8 lines
drivers/hwtracing/intel_th/core.c +1 -3
@@ -649,10 +649,8 @@ static inline void intel_th_request_hub_module_flush(struct intel_th *th)
 	}
 
 	err = intel_th_device_add_resources(thdev, res, subdev->nres);
-	if (err) {
-		put_device(&thdev->dev);
+	if (err)
 		goto fail_put_device;
-	}

What about the second instance of the same problem a few lines lower?

Thanks,
--
Alex

author
#2 lore raw
Quoted 35 lines · Alexander Shishkin
Quoted 12 lines · Wen Yang

There is a possible double free issue in intel_th_subdevice_alloc:

651 err = intel_th_device_add_resources(thdev, res, subdev->nres);
652 if (err) {
653 put_device(&thdev->dev);
654 goto fail_put_device; ---> freed
655 }
...
687 fail_put_device:
688 put_device(&thdev->dev); ---> double freed
689

Quoted 1 line

This patch fix it by removing the unnecessary put_device().

Unnecessary is a too generous term here.

Quoted 5 lines
Fixes a753bfcfdb1f ("intel_th: Make the switch allocate its subdevices")
Cc Alexander Shishkin <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc stable@ is missing.
Quoted 11 lines
---
  drivers/hwtracing/intel_th/core.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
drivers/hwtracing/intel_th/core.c +0 -0
index d5c1821..98d195c 100644
@@ -649,10 +649,8 @@ static inline void intel_th_request_hub_module_flush(struct intel_th *th)
  	}
 
Quoted 10 lines

drivers/hwtracing/intel_th/core.c static inline void intel_th_request_hub_module_flush(struct intel_th *th) · line 655

Quoted 6 lines
drivers/hwtracing/intel_th/core.c +1 -3
  	err = intel_th_device_add_resources(thdev, res, subdev->nres);
-	if (err) {
-		put_device(&thdev->dev);
+	if (err)
  		goto fail_put_device;
-	}

What about the second instance of the same problem a few lines lower? Thanks,
--
Alex

Hi Alex,

Thank you for your comments.

Another example after a few lines lower:

        err = device_add(&thdev->dev);

        if (err) {
                 put_device(&thdev->dev);
                 goto fail_free_res;

         }

device_add() has increased the reference count,

so when it returns an error, an additional call to put_device()

is needed here to reduce the reference count.

So the code in this place is correct.

--

Regards,

Wen

#3 lore raw
Quoted 10 lines · Wen Yang

Another example after a few lines lower:

        err = device_add(&thdev->dev);

        if (err) {
                 put_device(&thdev->dev);
                 goto fail_free_res;

         }

Quoted 7 lines

device_add() has increased the reference count,

so when it returns an error, an additional call to put_device()

is needed here to reduce the reference count.

So the code in this place is correct.

No, device_add() drops its own extra reference in case of error (as it should), so in "if (err) ..." branch we still only have just one reference before it goes free.

Regards,
--
Alex

author
#4 lore raw
Quoted 18 lines · Alexander Shishkin
Quoted 16 lines · Wen Yang

Another example after a few lines lower:

          err = device_add(&thdev->dev);

          if (err) {
                   put_device(&thdev->dev);
                   goto fail_free_res;

           }

device_add() has increased the reference count,

so when it returns an error, an additional call to put_device()

is needed here to reduce the reference count.

Quoted 8 lines
Quoted 1 line

So the code in this place is correct.

No, device_add() drops its own extra reference in case of error (as it should), so in "if (err) ..." branch we still only have just one reference before it goes free.

Regards,
--
Alex

Well, ok, you are right.

We just checked the code and device_add() does release the reference count.

Thank you.

--

Regards,

Wen