* [PATCH] driver core: bus: Fix a potential double free
@ 2017-08-29 19:23 Christophe JAILLET
2017-08-31 16:57 ` Greg KH
0 siblings, 1 reply; 2+ messages in thread
From: Christophe JAILLET @ 2017-08-29 19:23 UTC (permalink / raw)
To: gregkh; +Cc: linux-kernel, kernel-janitors, Christophe JAILLET
The .release function of driver_ktype is 'driver_release()'.
This function frees the container_of this kobject.
So, this memory must not be freed explicitly in the error handling path of
'bus_add_driver()'. Otherwise a double free will occur.
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
I have left the 'drv->p = NULL' even if it looks useless to me. It does not
hurt. I have also left a comment to explain why we do not free 'drv->p'
explicitly before zeroing this pointer.
---
drivers/base/bus.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index e162c9a789ba..22a64fd3309b 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -698,7 +698,7 @@ int bus_add_driver(struct device_driver *drv)
out_unregister:
kobject_put(&priv->kobj);
- kfree(drv->p);
+ /* drv->p is freed in driver_release() */
drv->p = NULL;
out_put_bus:
bus_put(bus);
--
2.11.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] driver core: bus: Fix a potential double free
2017-08-29 19:23 [PATCH] driver core: bus: Fix a potential double free Christophe JAILLET
@ 2017-08-31 16:57 ` Greg KH
0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2017-08-31 16:57 UTC (permalink / raw)
To: Christophe JAILLET; +Cc: linux-kernel, kernel-janitors
On Tue, Aug 29, 2017 at 09:23:49PM +0200, Christophe JAILLET wrote:
> The .release function of driver_ktype is 'driver_release()'.
> This function frees the container_of this kobject.
>
> So, this memory must not be freed explicitly in the error handling path of
> 'bus_add_driver()'. Otherwise a double free will occur.
Oh, nice catch!
>
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
> I have left the 'drv->p = NULL' even if it looks useless to me. It does not
> hurt. I have also left a comment to explain why we do not free 'drv->p'
> explicitly before zeroing this pointer.
We need that for when people do static structures. Like as is done for
drivers. Well, I think that's why we need it, can't really remember,
might as well be safe, the error handling paths here are crazy at
times...
thnaks,
greg k-h
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2017-08-31 16:57 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-08-29 19:23 [PATCH] driver core: bus: Fix a potential double free Christophe JAILLET
2017-08-31 16:57 ` Greg KH
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).