mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] driver: core: Prevent NULL pointer dereference in device name functions
@ 2023-02-12 22:04 Alexander Sverdlin
  2023-02-13  7:07 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 4+ messages in thread
From: Alexander Sverdlin @ 2023-02-12 22:04 UTC (permalink / raw)
  To: linux-kernel; +Cc: Alexander Sverdlin, Greg Kroah-Hartman, Rafael J. Wysocki

Prevent similar scenarios:

Unable to handle kernel NULL pointer dereference at virtual address 00000038
...
PC is at dev_driver_string+0x0/0x38

Signed-off-by: Alexander Sverdlin <alexander.sverdlin@gmail.com>
---
 drivers/base/core.c    | 3 +++
 include/linux/device.h | 5 +++++
 2 files changed, 8 insertions(+)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index a3e14143ec0c..4ff2ddea7c9b 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -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.
diff --git a/include/linux/device.h b/include/linux/device.h
index 44e3acae7b36..ff9e19e6d78c 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -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 : "");
 }
 
-- 
2.39.1


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

end of thread, other threads:[~2023-02-13  7:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-12 22:04 [PATCH] driver: core: Prevent NULL pointer dereference in device name functions Alexander Sverdlin
2023-02-13  7:07 ` Greg Kroah-Hartman
2023-02-13  7:12   ` Alexander Sverdlin
2023-02-13  7:16     ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®