Skip to contentSkip to conversation

platform/x86: wmi: fix potential null pointer dereferences

PATCH Superseded Mattias Jacobsson 3 participants 6 messages Jan 22, 2019 – Jan 28, 2019 x86 platform drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: wmi: fix potential null pointer dereferences

In the function wmi_dev_match() there are three variables that potentially can result in a null pointer dereference. Namely:
dev/wblock, driver/wmi_driver, and wmi_driver->id_table.

Check for NULL and return that the driver can't handle the device if any of these variables would result in a null pointer dereference.

The NULL checks are performed prior to running container_of() for the variables dev/wblock and driver/wmi_driver.

Fixes 844af950da94 ("platform/x86: wmi: Turn WMI into a bus driver")
---
 drivers/platform/x86/wmi.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/platform/x86/wmi.c +12 -4
index bea35be68706..c596479e8b13 100644
@@ -763,10 +763,18 @@ static void wmi_dev_release(struct device *dev)
 
 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
 {
-	struct wmi_driver *wmi_driver =
-		container_of(driver, struct wmi_driver, driver);
-	struct wmi_block *wblock = dev_to_wblock(dev);
-	const struct wmi_device_id *id = wmi_driver->id_table;
+	const struct wmi_device_id *id;
+	struct wmi_block *wblock;
+	struct wmi_driver *wmi_driver;
+
+	if (dev == NULL || driver == NULL)
+		return 0;
+	wblock = dev_to_wblock(dev);
+	wmi_driver = container_of(driver, struct wmi_driver, driver);
+
+	if (wmi_driver->id_table == NULL)
+		return 0;
+	id = wmi_driver->id_table;
 
 	while (id->guid_string) {
 		uuid_le driver_guid;
Signature · 1 line
2.20.1
#1 lore raw
Quoted 2 lines · Mattias Jacobsson

In the function wmi_dev_match() there are three variables that potentially can result in a null pointer dereference. Namely:

Is this something you have observed? This gets called when a new driver registered for each unassociated device on the bus, so I'm not immediately seeing how dev or driver would end up being NULL here.

See: Documentation/driver-model/bus.txt

Signature · 2 lines
Darren Hart
VMware Open Source Technology Center
#2 lore raw

Hi Darren,

Quoted 7 lines · Darren Hart
Quoted 2 lines · Mattias Jacobsson

In the function wmi_dev_match() there are three variables that potentially can result in a null pointer dereference. Namely:

Is this something you have observed? This gets called when a new driver registered for each unassociated device on the bus, so I'm not immediately seeing how dev or driver would end up being NULL here.

I've observed that wmi_driver->id_table can be NULL, that can happen when a WMI driver registers itself without specifying an id_table.

When adding a NULL check for wmi_driver->id_table, it felt wrong to not do the same check for driver. After all, driver is a pointer given to me from someone else, and can therefore be NULL. The same reasoning applies to dev.

Quoted 6 lines

See: Documentation/driver-model/bus.txt

Signature · 2 lines
Darren Hart
VMware Open Source Technology Center

Thanks,
Mattias

#3 lore raw
In reply to Mattias Jacobsson
Quoted 22 lines · Mattias Jacobsson

In the function wmi_dev_match() there are three variables that potentially can result in a null pointer dereference. Namely:
dev/wblock, driver/wmi_driver, and wmi_driver->id_table.

Check for NULL and return that the driver can't handle the device if any of these variables would result in a null pointer dereference.

The NULL checks are performed prior to running container_of() for the variables dev/wblock and driver/wmi_driver.

Fixes 844af950da94 ("platform/x86: wmi: Turn WMI into a bus driver")
---
 drivers/platform/x86/wmi.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/platform/x86/wmi.c +0 -0
index bea35be68706..c596479e8b13 100644

drivers/platform/x86/wmi.c static void wmi_dev_release(struct device *dev) · line 767

Quoted 5 lines
drivers/platform/x86/wmi.c +0 -2
@@ -763,10 +763,18 @@ static void wmi_dev_release(struct device *dev)
 
 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
 {
-       struct wmi_driver *wmi_driver =
-               container_of(driver, struct wmi_driver, driver);

AFAIU this is just a pointer arithmetics, no need to move it.

drivers/platform/x86/wmi.c static void wmi_dev_release(struct device *dev)

Quoted 10 lines
drivers/platform/x86/wmi.c +6 -2
-       struct wmi_block *wblock = dev_to_wblock(dev);
 
-       const struct wmi_device_id *id = wmi_driver->id_table;
+       const struct wmi_device_id *id;
+       struct wmi_block *wblock;
+       struct wmi_driver *wmi_driver;
+
 
+       if (dev == NULL || driver == NULL)
+               return 0;

On which circumstances this may ever happen?

Quoted 12 lines
drivers/platform/x86/wmi.c +6 -0
+       wblock = dev_to_wblock(dev);
+       wmi_driver = container_of(driver, struct wmi_driver, driver);
+
+       if (wmi_driver->id_table == NULL)
+               return 0;
+       id = wmi_driver->id_table;
 
        while (id->guid_string) {
                uuid_le driver_guid;

--
2.20.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Hi,

Quoted 24 lines · Andy Shevchenko
Quoted 23 lines · Mattias Jacobsson

In the function wmi_dev_match() there are three variables that potentially can result in a null pointer dereference. Namely:
dev/wblock, driver/wmi_driver, and wmi_driver->id_table.

Check for NULL and return that the driver can't handle the device if any of these variables would result in a null pointer dereference.

The NULL checks are performed prior to running container_of() for the variables dev/wblock and driver/wmi_driver.

Fixes 844af950da94 ("platform/x86: wmi: Turn WMI into a bus driver")
---
 drivers/platform/x86/wmi.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/platform/x86/wmi.c +0 -0
index bea35be68706..c596479e8b13 100644
@@ -763,10 +763,18 @@ static void wmi_dev_release(struct device *dev)
 
Quoted 6 lines

drivers/platform/x86/wmi.c static void wmi_dev_release(struct device *dev) · line 767

Quoted 4 lines
drivers/platform/x86/wmi.c +0 -2
 static int wmi_dev_match(struct device *dev, struct device_driver *driver)
 {
-       struct wmi_driver *wmi_driver =
-               container_of(driver, struct wmi_driver, driver);

AFAIU this is just a pointer arithmetics, no need to move it.

That is my understanding too, it seamed backwards to do the NULL check afterwards, but we still have access to dev and driver. So why not...

Quoted 9 lines
Quoted 8 lines
drivers/platform/x86/wmi.c +4 -2
-       struct wmi_block *wblock = dev_to_wblock(dev);
 
-       const struct wmi_device_id *id = wmi_driver->id_table;
+       const struct wmi_device_id *id;
+       struct wmi_block *wblock;
+       struct wmi_driver *wmi_driver;
+
Quoted 4 lines

drivers/platform/x86/wmi.c static void wmi_dev_release(struct device *dev) · line 772

Quoted 2 lines
drivers/platform/x86/wmi.c +2 -0
+       if (dev == NULL || driver == NULL)
+               return 0;

On which circumstances this may ever happen?

Nothing in particular. If there is a bug in the caller of this function, then that is when this will come into play. See my earlier mail to Darren too.

Quoted 8 lines
Quoted 8 lines
drivers/platform/x86/wmi.c +6 -0
+       wblock = dev_to_wblock(dev);
+       wmi_driver = container_of(driver, struct wmi_driver, driver);
+
+       if (wmi_driver->id_table == NULL)
+               return 0;
+       id = wmi_driver->id_table;
 
Quoted 10 lines
Quoted 5 lines
drivers/platform/x86/wmi.c +0 -0
        while (id->guid_string) {
                uuid_le driver_guid;

--
2.20.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko

Thanks,
Mattias

#5 lore raw
Quoted 20 lines · Mattias Jacobsson
Quoted 6 lines · Andy Shevchenko

drivers/platform/x86/wmi.c static void wmi_dev_release(struct device *dev) · line 767

Quoted 2 lines · Mattias Jacobsson
drivers/platform/x86/wmi.c +0 -2
-       struct wmi_driver *wmi_driver =
-               container_of(driver, struct wmi_driver, driver);

AFAIU this is just a pointer arithmetics, no need to move it.

That is my understanding too, it seamed backwards to do the NULL check afterwards, but we still have access to dev and driver. So why not...

Quoted 9 lines
Quoted 8 lines
drivers/platform/x86/wmi.c +4 -2
-       struct wmi_block *wblock = dev_to_wblock(dev);
 
-       const struct wmi_device_id *id = wmi_driver->id_table;
+       const struct wmi_device_id *id;
+       struct wmi_block *wblock;
+       struct wmi_driver *wmi_driver;
+
Quoted 8 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       if (dev == NULL || driver == NULL)
+               return 0;

On which circumstances this may ever happen?

Nothing in particular. If there is a bug in the caller of this function, then that is when this will come into play. See my earlier mail to Darren too.

So, I suggest in these cases do not touch existing till the actual problem will be discovered and proved.
I.o.w. touch only place which you have real problem, and describe this issue in commit message.

Signature · 2 lines
With Best Regards,
Andy Shevchenko