Skip to contentSkip to conversation

thermal/drivers/rcar_thermal: fix list_first_entry on possibly empty sensor list in suspend/resume

PATCH NAK Li Ximo 2 participants 2 messages Oct 26

Subsystems (3)
lore
Share & tools
lore

Li Ximo

author
#0 lore raw

PATCH 1/1 thermal/drivers/rcar_thermal: fix list_first_entry on possibly empty sensor list in suspend/resume

From: Wang Haoran <[email protected]>

rcar_thermal_suspend() and rcar_thermal_resume() call list_first_entry(&common->head, ...) without checking whether the list is empty. list_first_entry() does not return NULL for an empty list; it returns the container_of() of the list head pointer, i.e. a wild pointer into struct rcar_thermal_common. Dereferencing it at priv->chip->needs_suspend_resume (and the MMIO accesses through priv->base that follow) is undefined behavior and can corrupt memory or crash the kernel.

The list can indeed be empty: rcar_thermal_probe() only adds a sensor to common->head after platform_get_resource() succeeds, and it returns success even when zero sensors were probed (no i == 0 check before "return 0"). With a malformed DT node (no MEM resource) the driver binds successfully with an empty list, and the PM core then unconditionally invokes the suspend/resume callbacks. This was found by code audit; we do not have R-Car hardware to reproduce it at runtime.

Fix both callbacks by switching to list_first_entry_or_null() and returning 0 early when the sensor list is empty. This matches how the rest of the driver already tolerates an empty list via the list_for_each_entry()-based rcar_thermal_for_each_priv() iterator used in remove and IRQ handling, and does not change behavior on the normal non-empty path.

---
 drivers/thermal/renesas/rcar_thermal.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/thermal/renesas/rcar_thermal.c +10 -4
index 9a9038671..65dba6354 100644
@@ -530,8 +530,11 @@ static int rcar_thermal_probe(struct platform_device *pdev)
 static int rcar_thermal_suspend(struct device *dev)
 {
 	struct rcar_thermal_common *common = dev_get_drvdata(dev);
-	struct rcar_thermal_priv *priv = list_first_entry(&common->head,
-							  typeof(*priv), list);
+	struct rcar_thermal_priv *priv = list_first_entry_or_null(&common->head,
+								  typeof(*priv), list);
+
+	if (!priv)
+		return 0;
 
 	if (priv->chip->needs_suspend_resume) {
 		rcar_thermal_common_write(common, ENR, 0);
@@ -545,10 +548,13 @@ static int rcar_thermal_suspend(struct device *dev)
 static int rcar_thermal_resume(struct device *dev)
 {
 	struct rcar_thermal_common *common = dev_get_drvdata(dev);
-	struct rcar_thermal_priv *priv = list_first_entry(&common->head,
-							  typeof(*priv), list);
+	struct rcar_thermal_priv *priv = list_first_entry_or_null(&common->head,
+								  typeof(*priv), list);
 	int ret;
 
+	if (!priv)
+		return 0;
+
 	if (priv->chip->needs_suspend_resume) {
 		ret = rcar_thermal_update_temp(priv);
 		if (ret < 0)
Signature · 1 line
2.30.2
maintainer Nacked-by on v1 1/1 NAK
#1 lore raw

Hello Li,

Thanks for your work.

Quoted 11 lines · Li Ximo

From: Wang Haoran <[email protected]>

rcar_thermal_suspend() and rcar_thermal_resume() call list_first_entry(&common->head, ...) without checking whether the list is empty. list_first_entry() does not return NULL for an empty list; it returns the container_of() of the list head pointer, i.e. a wild pointer into struct rcar_thermal_common. Dereferencing it at priv->chip->needs_suspend_resume (and the MMIO accesses through priv->base that follow) is undefined behavior and can corrupt memory or crash the kernel.

Quoted 7 lines

The list can indeed be empty: rcar_thermal_probe() only adds a sensor to common->head after platform_get_resource() succeeds, and it returns success even when zero sensors were probed (no i == 0 check before "return 0"). With a malformed DT node (no MEM resource) the driver binds successfully with an empty list, and the PM core then unconditionally invokes the suspend/resume callbacks. This was found by code audit; we do not have R-Car hardware to reproduce it at runtime.

Could you please audit the code some more, preferably without an LLM. While I agree the issue should be solved this is not the fix.

Does it make sens to allow probe to succeed if we haver a malformed DT node? Would a nicer fix not be to fail probe with -ENODEV in that case?

Quoted 52 lines

Fix both callbacks by switching to list_first_entry_or_null() and returning 0 early when the sensor list is empty. This matches how the rest of the driver already tolerates an empty list via the list_for_each_entry()-based rcar_thermal_for_each_priv() iterator used in remove and IRQ handling, and does not change behavior on the normal non-empty path.

---
 drivers/thermal/renesas/rcar_thermal.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/thermal/renesas/rcar_thermal.c +10 -4
index 9a9038671..65dba6354 100644
@@ -530,8 +530,11 @@ static int rcar_thermal_probe(struct platform_device *pdev)
 static int rcar_thermal_suspend(struct device *dev)
 {
 	struct rcar_thermal_common *common = dev_get_drvdata(dev);
-	struct rcar_thermal_priv *priv = list_first_entry(&common->head,
-							  typeof(*priv), list);
+	struct rcar_thermal_priv *priv = list_first_entry_or_null(&common->head,
+								  typeof(*priv), list);
+
+	if (!priv)
+		return 0;
 
 	if (priv->chip->needs_suspend_resume) {
 		rcar_thermal_common_write(common, ENR, 0);
@@ -545,10 +548,13 @@ static int rcar_thermal_suspend(struct device *dev)
 static int rcar_thermal_resume(struct device *dev)
 {
 	struct rcar_thermal_common *common = dev_get_drvdata(dev);
-	struct rcar_thermal_priv *priv = list_first_entry(&common->head,
-							  typeof(*priv), list);
+	struct rcar_thermal_priv *priv = list_first_entry_or_null(&common->head,
+								  typeof(*priv), list);
 	int ret;
 
+	if (!priv)
+		return 0;
+
 	if (priv->chip->needs_suspend_resume) {
 		ret = rcar_thermal_update_temp(priv);
 		if (ret < 0)
Signature · 1 line
2.30.2
Signature · 2 lines
Kind Regards,
Niklas Söderlund