Skip to contentSkip to conversation

driver: core: Prevent NULL pointer dereference in device name functions

PATCH In review Alexander Sverdlin 2 participants 4 messages Feb 12, 2023 – Feb 13, 2023 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 driver: core: Prevent NULL pointer dereference in device name functions

Prevent similar scenarios:

Unable to handle kernel NULL pointer dereference at virtual address 00000038

...
PC is at dev_driver_string+0x0/0x38

---
 drivers/base/core.c    | 3 +++
 include/linux/device.h | 5 +++++
 2 files changed, 8 insertions(+)
drivers/base/core.c +3 -0
index a3e14143ec0c..4ff2ddea7c9b 100644
@@ -2174,6 +2174,9 @@ const char *dev_driver_string(const struct device *dev)
 {
 	struct device_driver *drv;
 
+	if (!dev)
+		return "<null>";
+
 	/* dev->driver can change to NULL underneath us because of unbinding,
 	 * so be careful about accessing it.  dev->bus and dev->class should
 	 * never change once they are set, so they don't need special care.
include/linux/device.h +5 -0
index 44e3acae7b36..ff9e19e6d78c 100644
@@ -696,6 +696,9 @@ static inline bool device_iommu_mapped(struct device *dev)
 
 static inline const char *dev_name(const struct device *dev)
 {
+	if (!dev)
+		return "<null>";
+
 	/* Use the init name until the kobject becomes available */
 	if (dev->init_name)
 		return dev->init_name;
@@ -712,6 +715,8 @@ static inline const char *dev_name(const struct device *dev)
  */
 static inline const char *dev_bus_name(const struct device *dev)
 {
+	if (!dev)
+		return "<null>";
 	return dev->bus ? dev->bus->name : (dev->class ? dev->class->name : "");
 }
 
Signature · 1 line
2.39.1
#1 lore raw
Quoted 5 lines · Alexander Sverdlin

Prevent similar scenarios:

Unable to handle kernel NULL pointer dereference at virtual address 00000038

...
PC is at dev_driver_string+0x0/0x38

How did this "scenario" happen? What in-tree code caused this?

Quoted 12 lines
---
 drivers/base/core.c    | 3 +++
 include/linux/device.h | 5 +++++
 2 files changed, 8 insertions(+)
drivers/base/core.c +0 -0
index a3e14143ec0c..4ff2ddea7c9b 100644

drivers/base/core.c const char *dev_driver_string(const struct device *dev) · line 2179

Quoted 6 lines
drivers/base/core.c +3 -0
@@ -2174,6 +2174,9 @@ const char *dev_driver_string(const struct device *dev)
 {
 	struct device_driver *drv;
 
+	if (!dev)
+		return "<null>";
+

None of these functions should be called with a NULL pointer as the reference should have been properly gotten on them before calling these functions. So let's fix up the callers please, something is really wrong with them. Again, what in-tree code is causing this to happen?

thanks,

greg k-h

#2 lore raw

Hi Greg,

Quoted 8 lines · Greg Kroah-Hartman
Quoted 5 lines

Prevent similar scenarios:

Unable to handle kernel NULL pointer dereference at virtual address 00000038

...
PC is at dev_driver_string+0x0/0x38

How did this "scenario" happen?  What in-tree code caused this?

such in-tree code is not known to me, I stubled upon this putting dev_info() all over the code in the platform we currently convert to DT (cirrus ep93xx).

Quoted 6 lines
Quoted 6 lines

---
  drivers/base/core.c    | 3 +++
  include/linux/device.h | 5 +++++

  2 files changed, 8 insertions(+)
Quoted 16 lines
Quoted 11 lines
drivers/base/core.c +0 -0
index a3e14143ec0c..4ff2ddea7c9b 100644
@@ -2174,6 +2174,9 @@ const char *dev_driver_string(const struct device *dev)
   {
         struct device_driver *drv;


+       if (!dev)
+               return "<null>";
+

None of these functions should be called with a NULL pointer as the reference should have been properly gotten on them before calling these functions.  So let's fix up the callers please, something is really wrong with them.  Again, what in-tree code is causing this to happen?

Thanks for the explanation!

Signature · 1 line
Alexander Sverdlin.
#3 lore raw
Quoted 9 lines · Alexander Sverdlin

Hi Greg,

Quoted 6 lines · Greg Kroah-Hartman
Quoted 5 lines

Prevent similar scenarios:

Unable to handle kernel NULL pointer dereference at virtual address 00000038

...
PC is at dev_driver_string+0x0/0x38

Quoted 5 lines
Quoted 2 lines

How did this "scenario" happen?  What in-tree code caused this?

such in-tree code is not known to me, I stubled upon this putting dev_info() all over the code in the platform we currently convert to DT (cirrus ep93xx).

Instead of using dev_info() for tracing, use ftrace instead, that's what it is there for! :)

thanks,

greg k-h