Skip to contentSkip to conversation

drm/xe/hwmon: fix uaf on unload

PATCH In review Matthew Auld 3 participants 3 messages Oct 5, 2023 – Oct 6, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/xe/hwmon: fix uaf on unload

It doesn't look like you can mix and match devm_ and drmmm_ for a managed resource. For drmmm the resources are all tracked in drm with its own list, and there is only one devm_ resource for the entire list. If the driver itself also adds some of its own devm resources, then those will be released first. In the case of hwmon the devm_kzalloc will be freed before the drmmm_ action to destroy the mutex allocated within, leading to uaf.

Since hwmon itself wants to use devm, rather use that for the mutex destroy.

---
 drivers/gpu/drm/xe/xe_hwmon.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_hwmon.c +10 -1
index 1deb5007e1e2..9d3e06b96073 100644
@@ -584,6 +584,13 @@ xe_hwmon_get_preregistration_info(struct xe_device *xe)
 		xe_hwmon_energy_get(hwmon, &energy);
 }
 
+static void xe_hwmon_mutex_destroy(void *arg)
+{
+	struct xe_hwmon *hwmon = arg;
+
+	mutex_destroy(&hwmon->hwmon_lock);
+}
+
 void xe_hwmon_register(struct xe_device *xe)
 {
 	struct device *dev = xe->drm.dev;
@@ -599,7 +606,9 @@ void xe_hwmon_register(struct xe_device *xe)
 
 	xe->hwmon = hwmon;
 
-	drmm_mutex_init(&xe->drm, &hwmon->hwmon_lock);
+	mutex_init(&hwmon->hwmon_lock);
+	if (devm_add_action_or_reset(dev, xe_hwmon_mutex_destroy, hwmon))
+		return;
 
 	/* primary GT to access device level properties */
 	hwmon->gt = xe->tiles[0].primary_gt;
Signature · 1 line
2.41.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Matthew Auld

It doesn't look like you can mix and match devm_ and drmmm_ for a managed resource. For drmmm the resources are all tracked in drm with its own list, and there is only one devm_ resource for the entire list. If the driver itself also adds some of its own devm resources, then those will be released first. In the case of hwmon the devm_kzalloc will be freed before the drmmm_ action to destroy the mutex allocated within, leading to uaf.

Quoted 7 lines

Since hwmon itself wants to use devm, rather use that for the mutex destroy.

Quoted 36 lines
---
 drivers/gpu/drm/xe/xe_hwmon.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_hwmon.c +10 -1
index 1deb5007e1e2..9d3e06b96073 100644
@@ -584,6 +584,13 @@ xe_hwmon_get_preregistration_info(struct xe_device *xe)
 		xe_hwmon_energy_get(hwmon, &energy);
 }
 
+static void xe_hwmon_mutex_destroy(void *arg)
+{
+	struct xe_hwmon *hwmon = arg;
+
+	mutex_destroy(&hwmon->hwmon_lock);
+}
+
 void xe_hwmon_register(struct xe_device *xe)
 {
 	struct device *dev = xe->drm.dev;
@@ -599,7 +606,9 @@ void xe_hwmon_register(struct xe_device *xe)
 
 	xe->hwmon = hwmon;
 
-	drmm_mutex_init(&xe->drm, &hwmon->hwmon_lock);
+	mutex_init(&hwmon->hwmon_lock);
+	if (devm_add_action_or_reset(dev, xe_hwmon_mutex_destroy, hwmon))
+		return;
 
 	/* primary GT to access device level properties */
 	hwmon->gt = xe->tiles[0].primary_gt;
Signature · 1 line
2.41.0
Reviewed-by on v1 1/1
#2 lore raw
In reply to Matthew Auld
Quoted 8 lines · Matthew Auld

It doesn't look like you can mix and match devm_ and drmmm_ for a managed resource. For drmmm the resources are all tracked in drm with its own list, and there is only one devm_ resource for the entire list. If the driver itself also adds some of its own devm resources, then those will be released first. In the case of hwmon the devm_kzalloc will be freed before the drmmm_ action to destroy the mutex allocated within, leading to uaf.

Quoted 7 lines

Since hwmon itself wants to use devm, rather use that for the mutex destroy.

Quoted 33 lines
---
  drivers/gpu/drm/xe/xe_hwmon.c | 11 ++++++++++-
  1 file changed, 10 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_hwmon.c +10 -1
index 1deb5007e1e2..9d3e06b96073 100644
@@ -584,6 +584,13 @@ xe_hwmon_get_preregistration_info(struct xe_device *xe)
  		xe_hwmon_energy_get(hwmon, &energy);
  }
 
+static void xe_hwmon_mutex_destroy(void *arg)
+{
+	struct xe_hwmon *hwmon = arg;
+
+	mutex_destroy(&hwmon->hwmon_lock);
+}
+
  void xe_hwmon_register(struct xe_device *xe)
  {
  	struct device *dev = xe->drm.dev;
@@ -599,7 +606,9 @@ void xe_hwmon_register(struct xe_device *xe)
 
  	xe->hwmon = hwmon;
 
-	drmm_mutex_init(&xe->drm, &hwmon->hwmon_lock);
+	mutex_init(&hwmon->hwmon_lock);
+	if (devm_add_action_or_reset(dev, xe_hwmon_mutex_destroy, hwmon))
+		return;
 
  	/* primary GT to access device level properties */
  	hwmon->gt = xe->tiles[0].primary_gt;