msm_pinctrl_probe() allocates the pctrl structure via devm_kzalloc() and unconditionally populates the global variables poweroff_pctrl and pm_power_off. If msm_pinctrl_probe() subsequently fails, devres resources - including the memory allocated for pctrl - are automatically freed. However, the global pointers are never cleared, resulting in a use-after-free during kernel_power_off(). Schedule a devres action clearing the global pointers that runs before the pctrl memory is freed. Cc: stable@vger.kernel.org Closes: https://sashiko.dev/#/bug/linux-0c3a18a4-3de8-489b-abc9-e9e3a8641ed2 Fixes: ad64498762dd ("pinctrl: qcom: Hook pm_power_down for shutdown support") Signed-off-by: Bartosz Golaszewski --- drivers/pinctrl/qcom/pinctrl-msm.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/drivers/pinctrl/qcom/pinctrl-msm.c b/drivers/pinctrl/qcom/pinctrl-msm.c index 32aa7b919e24a0e614078ddc70a248c746e4813b..6ddbec7c27664b5b137ae1bdf668dd88a0f5e224 100644 --- a/drivers/pinctrl/qcom/pinctrl-msm.c +++ b/drivers/pinctrl/qcom/pinctrl-msm.c @@ -1561,9 +1561,15 @@ static void msm_ps_hold_poweroff(void) msm_ps_hold_restart(&data); } +static void msm_pinctrl_clear_poweroff(void *data) +{ + poweroff_pctrl = NULL; + pm_power_off = NULL; +} + static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl) { - int i; + int i, ret; const struct pinfunction *func = pctrl->soc->functions; for (i = 0; i < pctrl->soc->nfunctions; i++) @@ -1577,6 +1583,13 @@ static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl) "failed to setup restart handler.\n"); poweroff_pctrl = pctrl; pm_power_off = msm_ps_hold_poweroff; + + ret = devm_add_action_or_reset(pctrl->dev, + msm_pinctrl_clear_poweroff, NULL); + if (ret) + dev_err(pctrl->dev, + "Failed to register the poweroff cleanup action.\n"); + break; } } --- base-commit: ddfe51b0a0f5dc9e415cc2c8ad8db17532dd426b change-id: 20261105-pinctrl-qcom-uaf-500115513a62 Best regards, -- Bartosz Golaszewski