qmp_cooling_devices_register() frees qmp->cooling_devs when no cooling devices are registered or registration fails, but leaves the pointer non-NULL. qmp_probe() still returns success in both cases, so qmp_remove() later walks the freed array. The error path also unregisters the failed slot because count is incremented before registration is checked. Moreover, cooling devices are registered with devres even though qmp_remove() unregisters them manually before closing the QMP channel. Clear qmp->cooling_devs after freeing it, increment count only after successful registration, and use unmanaged registration so manual unregistration remains the only release path. Skip removal when the array is NULL. Found by LLM-assisted static analysis. Build tested only; no runtime test. Fixes: a89f355e469d ("soc: qcom: aoss: Fix the out of bound usage of cooling_devs") Cc: stable@vger.kernel.org Signed-off-by: Ziheng Wei --- drivers/soc/qcom/qcom_aoss.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/drivers/soc/qcom/qcom_aoss.c b/drivers/soc/qcom/qcom_aoss.c index 2d248c7be..b9ae12685 100644 --- a/drivers/soc/qcom/qcom_aoss.c +++ b/drivers/soc/qcom/qcom_aoss.c @@ -381,10 +381,9 @@ static int qmp_cooling_device_add(struct qmp *qmp, qmp_cdev->qmp = qmp; qmp_cdev->state = !qmp_cdev_max_state; qmp_cdev->name = cdev_name; - qmp_cdev->cdev = devm_thermal_of_child_cooling_device_register - (qmp->dev, node, - cdev_name, - qmp_cdev, &qmp_cooling_device_ops); + qmp_cdev->cdev = thermal_of_cooling_device_register(node, 0, cdev_name, + qmp_cdev, + &qmp_cooling_device_ops); if (IS_ERR(qmp_cdev->cdev)) dev_err(qmp->dev, "unable to register %s cooling device\n", @@ -410,22 +409,25 @@ static int qmp_cooling_devices_register(struct qmp *qmp) for_each_available_child_of_node_scoped(np, child) { if (!of_property_present(child, "#cooling-cells")) continue; - ret = qmp_cooling_device_add(qmp, &qmp->cooling_devs[count++], + ret = qmp_cooling_device_add(qmp, &qmp->cooling_devs[count], child); if (ret) goto unroll; + count++; } - if (!count) + if (!count) { devm_kfree(qmp->dev, qmp->cooling_devs); + qmp->cooling_devs = NULL; + } return 0; unroll: while (--count >= 0) - thermal_cooling_device_unregister - (qmp->cooling_devs[count].cdev); + thermal_cooling_device_unregister(qmp->cooling_devs[count].cdev); devm_kfree(qmp->dev, qmp->cooling_devs); + qmp->cooling_devs = NULL; return ret; } @@ -434,6 +436,9 @@ static void qmp_cooling_devices_remove(struct qmp *qmp) { int i; + if (!qmp->cooling_devs) + return; + for (i = 0; i < QMP_NUM_COOLING_RESOURCES; i++) thermal_cooling_device_unregister(qmp->cooling_devs[i].cdev); } base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa -- 2.43.0