* [PATCH] driver: base: fix UAF when driver_attach failed
@ 2022-05-11 12:43 Schspa Shi
2022-05-11 12:57 ` Greg KH
0 siblings, 1 reply; 3+ messages in thread
From: Schspa Shi @ 2022-05-11 12:43 UTC (permalink / raw)
To: gregkh, rafael; +Cc: ming.lei, linux-kernel, Schspa Shi
When driver_attach(drv); failed, the driver_private will be freed.
But it has been added to the bus, which caused a UAF.
To fix it, we need to delete it from the bus when failed.
Fixes: 190888ac01d0 ("driver core: fix possible missing of device probe")
Signed-off-by: Schspa Shi <schspa@gmail.com>
---
drivers/base/bus.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index 97936ec49bde..7ca47e5b3c1f 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -617,7 +617,7 @@ int bus_add_driver(struct device_driver *drv)
if (drv->bus->p->drivers_autoprobe) {
error = driver_attach(drv);
if (error)
- goto out_unregister;
+ goto out_del_list;
}
module_add_driver(drv->owner, drv);
@@ -644,6 +644,8 @@ int bus_add_driver(struct device_driver *drv)
return 0;
+out_del_list:
+ klist_del(&priv->knode_bus);
out_unregister:
kobject_put(&priv->kobj);
/* drv->p is freed in driver_release() */
--
2.29.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] driver: base: fix UAF when driver_attach failed
2022-05-11 12:43 [PATCH] driver: base: fix UAF when driver_attach failed Schspa Shi
@ 2022-05-11 12:57 ` Greg KH
2022-05-11 13:25 ` Schspa Shi
0 siblings, 1 reply; 3+ messages in thread
From: Greg KH @ 2022-05-11 12:57 UTC (permalink / raw)
To: Schspa Shi; +Cc: rafael, ming.lei, linux-kernel
On Wed, May 11, 2022 at 08:43:36PM +0800, Schspa Shi wrote:
> When driver_attach(drv); failed, the driver_private will be freed.
> But it has been added to the bus, which caused a UAF.
>
> To fix it, we need to delete it from the bus when failed.
>
> Fixes: 190888ac01d0 ("driver core: fix possible missing of device probe")
>
> Signed-off-by: Schspa Shi <schspa@gmail.com>
No blank line needed after fixes:
> ---
> drivers/base/bus.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/base/bus.c b/drivers/base/bus.c
> index 97936ec49bde..7ca47e5b3c1f 100644
> --- a/drivers/base/bus.c
> +++ b/drivers/base/bus.c
> @@ -617,7 +617,7 @@ int bus_add_driver(struct device_driver *drv)
> if (drv->bus->p->drivers_autoprobe) {
> error = driver_attach(drv);
> if (error)
> - goto out_unregister;
> + goto out_del_list;
> }
> module_add_driver(drv->owner, drv);
>
> @@ -644,6 +644,8 @@ int bus_add_driver(struct device_driver *drv)
>
> return 0;
>
> +out_del_list:
> + klist_del(&priv->knode_bus);
Odd, how did you find this? Has this ever been triggered by any
real-world situations?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] driver: base: fix UAF when driver_attach failed
2022-05-11 12:57 ` Greg KH
@ 2022-05-11 13:25 ` Schspa Shi
0 siblings, 0 replies; 3+ messages in thread
From: Schspa Shi @ 2022-05-11 13:25 UTC (permalink / raw)
To: Greg KH; +Cc: Rafael J. Wysocki, ming.lei, Linux Kernel Mailing List
Greg KH <gregkh@linuxfoundation.org> writes:
> On Wed, May 11, 2022 at 08:43:36PM +0800, Schspa Shi wrote:
>> When driver_attach(drv); failed, the driver_private will be freed.
>> But it has been added to the bus, which caused a UAF.
>>
>> To fix it, we need to delete it from the bus when failed.
>>
>> Fixes: 190888ac01d0 ("driver core: fix possible missing of device probe")
>>
>> Signed-off-by: Schspa Shi <schspa@gmail.com>
>
> No blank line needed after fixes:
>
Do I need a new patch version for this ?
>> ---
>> drivers/base/bus.c | 4 +++-
>> 1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/base/bus.c b/drivers/base/bus.c
>> index 97936ec49bde..7ca47e5b3c1f 100644
>> --- a/drivers/base/bus.c
>> +++ b/drivers/base/bus.c
>> @@ -617,7 +617,7 @@ int bus_add_driver(struct device_driver *drv)
>> if (drv->bus->p->drivers_autoprobe) {
>> error = driver_attach(drv);
>> if (error)
>> - goto out_unregister;
>> + goto out_del_list;
>> }
>> module_add_driver(drv->owner, drv);
>>
>> @@ -644,6 +644,8 @@ int bus_add_driver(struct device_driver *drv)
>>
>> return 0;
>>
>> +out_del_list:
>> + klist_del(&priv->knode_bus);
>
> Odd, how did you find this? Has this ever been triggered by any
> real-world situations?
>
I found this when fixing this issue via code review.
Link: https://lore.kernel.org/all/20220508150247.38204-1-schspa@gmail.com/
It shouldn't not happen, unless the bus driver's match() return value < 0.
Which never happens with the existing bus drivers.
> thanks,
>
> greg k-h
---
BRs
Schspa Shi
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-05-11 13:25 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-05-11 12:43 [PATCH] driver: base: fix UAF when driver_attach failed Schspa Shi
2022-05-11 12:57 ` Greg KH
2022-05-11 13:25 ` Schspa Shi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox