linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] firmware: arm_scmi: avoid double free in error flow
@ 2019-11-25 15:54 Wen Yang
  2019-11-25 16:13 ` Sudeep Holla
  0 siblings, 1 reply; 4+ messages in thread
From: Wen Yang @ 2019-11-25 15:54 UTC (permalink / raw)
  To: Sudeep Holla; +Cc: Arnd Bergmann, Wen Yang, linux-arm-kernel, linux-kernel

If device_register() fails, both put_device() and kfree()
are called, ending with a double free of the scmi_dev.

Calling kfree() is needed only when a failure happens between the
allocation of the scmi_dev and its registration, so move it to
there and remove it from the error flow.

Fixes: 46edb8d1322c ("firmware: arm_scmi: provide the mandatory device release callback")
Signed-off-by: Wen Yang <wenyang@linux.alibaba.com>
Cc: Sudeep Holla <sudeep.holla@arm.com>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
---
 drivers/firmware/arm_scmi/bus.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/firmware/arm_scmi/bus.c b/drivers/firmware/arm_scmi/bus.c
index 92f843ea..7a30952 100644
--- a/drivers/firmware/arm_scmi/bus.c
+++ b/drivers/firmware/arm_scmi/bus.c
@@ -135,8 +135,10 @@ struct scmi_device *
 		return NULL;
 
 	id = ida_simple_get(&scmi_bus_id, 1, 0, GFP_KERNEL);
-	if (id < 0)
-		goto free_mem;
+	if (id < 0) {
+		kfree(scmi_dev);
+		return NULL;
+	}
 
 	scmi_dev->id = id;
 	scmi_dev->protocol_id = protocol;
@@ -154,8 +156,6 @@ struct scmi_device *
 put_dev:
 	put_device(&scmi_dev->dev);
 	ida_simple_remove(&scmi_bus_id, id);
-free_mem:
-	kfree(scmi_dev);
 	return NULL;
 }
 
-- 
1.8.3.1


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

* Re: [PATCH] firmware: arm_scmi: avoid double free in error flow
  2019-11-25 15:54 [PATCH] firmware: arm_scmi: avoid double free in error flow Wen Yang
@ 2019-11-25 16:13 ` Sudeep Holla
  2019-11-26 10:24   ` Wen Yang
  0 siblings, 1 reply; 4+ messages in thread
From: Sudeep Holla @ 2019-11-25 16:13 UTC (permalink / raw)
  To: Wen Yang; +Cc: Arnd Bergmann, linux-arm-kernel, linux-kernel, Sudeep Holla

On Mon, Nov 25, 2019 at 11:54:09PM +0800, Wen Yang wrote:
> If device_register() fails, both put_device() and kfree()
> are called, ending with a double free of the scmi_dev.
>

Correct.

> Calling kfree() is needed only when a failure happens between the
> allocation of the scmi_dev and its registration, so move it to
> there and remove it from the error flow.
>

kstrdup_const can fail and in that case device is not yet registered,
so we need to free. Since device_register() calls put_device() on failure
too, I would just drop it as it's unnecessary, not sure why I have added
it in the first place. Can you re-spin the patch dropping put_device
and renaming put_dev label to something like free_const.

--
Regards,
Sudeep


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

* Re: [PATCH] firmware: arm_scmi: avoid double free in error flow
  2019-11-25 16:13 ` Sudeep Holla
@ 2019-11-26 10:24   ` Wen Yang
  2019-11-26 10:48     ` Sudeep Holla
  0 siblings, 1 reply; 4+ messages in thread
From: Wen Yang @ 2019-11-26 10:24 UTC (permalink / raw)
  To: Sudeep Holla; +Cc: Arnd Bergmann, linux-arm-kernel, linux-kernel



On 2019/11/26 12:13 上午, Sudeep Holla wrote:
> On Mon, Nov 25, 2019 at 11:54:09PM +0800, Wen Yang wrote:
>> If device_register() fails, both put_device() and kfree()
>> are called, ending with a double free of the scmi_dev.
>>
> 
> Correct.
> 
>> Calling kfree() is needed only when a failure happens between the
>> allocation of the scmi_dev and its registration, so move it to
>> there and remove it from the error flow.
>>
> 
> kstrdup_const can fail and in that case device is not yet registered,
> so we need to free. Since device_register() calls put_device() on failure
> too, I would just drop it as it's unnecessary, not sure why I have added
> it in the first place. Can you re-spin the patch dropping put_device
> and renaming put_dev label to something like free_const.
> 
> --
> Regards,
> Sudeep
> 

Hi Sudeep,
Thanks for your comments.
Let's check the code like this:

int device_register(struct device *dev)
{
         device_initialize(dev);   --> Initialize kobj-> kref to 1
         return device_add(dev);
}

int device_add(struct device *dev)
{
...
         dev = get_device(dev);  --> kobj-> kref increases by 1
...
done:
         put_device(dev);  --> kobj-> kref decreases by 1 and is now 1
         return error;
...
}

So we also need to call put_device (),
and the original patch should be fine.
Please kindly help to check again, thank you.

--
Regards,
Wen









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

* Re: [PATCH] firmware: arm_scmi: avoid double free in error flow
  2019-11-26 10:24   ` Wen Yang
@ 2019-11-26 10:48     ` Sudeep Holla
  0 siblings, 0 replies; 4+ messages in thread
From: Sudeep Holla @ 2019-11-26 10:48 UTC (permalink / raw)
  To: Wen Yang; +Cc: Arnd Bergmann, linux-arm-kernel, linux-kernel, Sudeep Holla

On Tue, Nov 26, 2019 at 06:24:47PM +0800, Wen Yang wrote:
>
>
> On 2019/11/26 12:13 上午, Sudeep Holla wrote:
> > On Mon, Nov 25, 2019 at 11:54:09PM +0800, Wen Yang wrote:
> > > If device_register() fails, both put_device() and kfree()
> > > are called, ending with a double free of the scmi_dev.
> > >
> >
> > Correct.
> >
> > > Calling kfree() is needed only when a failure happens between the
> > > allocation of the scmi_dev and its registration, so move it to
> > > there and remove it from the error flow.
> > >
> >
> > kstrdup_const can fail and in that case device is not yet registered,
> > so we need to free. Since device_register() calls put_device() on failure
> > too, I would just drop it as it's unnecessary, not sure why I have added
> > it in the first place. Can you re-spin the patch dropping put_device
> > and renaming put_dev label to something like free_const.
> >

Please ignore the above completely. I have made some changes locally and
got completely confused when I looked at your patch and compared with
the modified context locally.

>
> Hi Sudeep,
> Thanks for your comments.
> Let's check the code like this:
>
> int device_register(struct device *dev)
> {
>         device_initialize(dev);   --> Initialize kobj-> kref to 1
>         return device_add(dev);
> }
>
> int device_add(struct device *dev)
> {
> ...
>         dev = get_device(dev);  --> kobj-> kref increases by 1
> ...
> done:
>         put_device(dev);  --> kobj-> kref decreases by 1 and is now 1
>         return error;
> ...
> }
>
> So we also need to call put_device (),
> and the original patch should be fine.
> Please kindly help to check again, thank you.
>

You are right, sorry for the confusion. I will apply your original patch.
Thanks again for the patch.

--
Regards,
Sudeep

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

end of thread, other threads:[~2019-11-26 10:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-25 15:54 [PATCH] firmware: arm_scmi: avoid double free in error flow Wen Yang
2019-11-25 16:13 ` Sudeep Holla
2019-11-26 10:24   ` Wen Yang
2019-11-26 10:48     ` Sudeep Holla

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