diff mbox series

[08/10] pinctrl: mediatek: Clean up partially initialised hw->eint on error

Message ID 20261103213002.1673560-9-lee@kernel.org
State New
Headers show
Series pinctrl: Fix out-of-bounds accesses, divide-by-zero, and error handling | expand

Commit Message

Lee Jones Nov. 3, 2026, 9:29 p.m. UTC
In mtk_build_eint(), if of_property_count_strings() fails or
hw->eint->nbase is non-positive, the function returns -EINVAL directly
after allocating hw->eint without jumping to err_free_base.  Because
callers such as mtk_paris_pinctrl_probe() and mtk_moore_pinctrl_probe()
log a warning and continue probing when mtk_build_eint() fails, leaving
hw->eint non-NULL with hw->eint->hw unset causes subsequent GPIO or
suspend/resume callbacks to dereference a NULL pointer inside
mtk_eint_do_suspend() or mtk_gpio_to_irq().

Jump to err_free_base on both error paths so hw->eint is freed and
reset to NULL.

Fixes: 3ef9f710efcb ("pinctrl: mediatek: Add EINT support for multiple addresses")
Closes: https://sashiko.dev/#/bug/linux-0689c07e-ef74-4bc2-b5de-bd2f361124bd
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

Comments

sashiko-bot@kernel.org Nov. 3, 2026, 9:40 p.m. UTC | #1
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] pinctrl: mediatek: NULL deref in mtk_eint_do_suspend() on EINT init error
--

commit 8d040caade190db874ef6d84f338e06e2679ef73
Author: Lee Jones <lee@kernel.org>

pinctrl: mediatek: Clean up partially initialised hw->eint on error

This commit updates mtk_build_eint() to jump to err_free_base when
of_property_count_strings() fails or hw->eint->nbase is non-positive,
freeing hw->eint and resetting it to NULL.

> diff --git a/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c b/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c
> index faa6dbf0f6356..aa15c9e047d89 100644
> --- a/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c
> +++ b/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c
> @@ -382,12 +382,16 @@ int mtk_build_eint(struct mtk_pinctrl *hw, struct platform_device *pdev)
>  		return -ENOMEM;
>  
>  	count_reg_names = of_property_count_strings(np, "reg-names");
> -	if (count_reg_names < 0)
> -		return -EINVAL;
> +	if (count_reg_names < 0) {
> +		ret = -EINVAL;
> +		goto err_free_base;
> +	}
>  
>  	hw->eint->nbase = count_reg_names - (int)hw->soc->nbase_names;
> -	if (hw->eint->nbase <= 0)
> -		return -EINVAL;
> +	if (hw->eint->nbase <= 0) {
> +		ret = -EINVAL;
> +		goto err_free_base;
> +	}

[Severity: High]
The commit message notes that leaving hw->eint non-NULL on these error
paths causes subsequent suspend/resume callbacks to dereference a NULL
pointer inside mtk_eint_do_suspend(), and that resetting hw->eint to NULL
avoids this.

However, when mtk_build_eint() fails during mtk_paris_pinctrl_probe(), the
driver logs a warning and finishes probing with pctl->eint set to NULL. On
system suspend or resume, mtk_paris_pinctrl_suspend() and
mtk_paris_pinctrl_resume() pass pctl->eint directly to
mtk_eint_do_suspend() and mtk_eint_do_resume():

drivers/pinctrl/mediatek/pinctrl-paris.c:
static int mtk_paris_pinctrl_suspend(struct device *device)
{
	struct mtk_pinctrl *pctl = dev_get_drvdata(device);

	return mtk_eint_do_suspend(pctl->eint);
}

Neither those callers nor mtk_eint_do_suspend() and mtk_eint_do_resume()
check whether eint is NULL before dereferencing it:

drivers/pinctrl/mediatek/mtk-eint.c:
int mtk_eint_do_suspend(struct mtk_eint *eint)
{
	mtk_eint_chip_write_mask(eint, eint->wake_mask);

	return 0;
}

Previously on these two error paths, hw->eint remained non-NULL with
hw->eint->nbase <= 0, so the loop in mtk_eint_chip_write_mask() over
inst < eint->nbase did zero iterations. With hw->eint reset to NULL here
(as well as on the existing error paths in mtk_build_eint()), won't
suspending or resuming trigger a NULL pointer dereference in
mtk_eint_do_suspend() and mtk_eint_do_resume() unless a NULL check is also
added?
diff mbox series

Patch

diff --git a/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c b/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c
index faa6dbf0f635..aa15c9e047d8 100644
--- a/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c
+++ b/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c
@@ -382,12 +382,16 @@  int mtk_build_eint(struct mtk_pinctrl *hw, struct platform_device *pdev)
 		return -ENOMEM;
 
 	count_reg_names = of_property_count_strings(np, "reg-names");
-	if (count_reg_names < 0)
-		return -EINVAL;
+	if (count_reg_names < 0) {
+		ret = -EINVAL;
+		goto err_free_base;
+	}
 
 	hw->eint->nbase = count_reg_names - (int)hw->soc->nbase_names;
-	if (hw->eint->nbase <= 0)
-		return -EINVAL;
+	if (hw->eint->nbase <= 0) {
+		ret = -EINVAL;
+		goto err_free_base;
+	}
 
 	hw->eint->base = devm_kmalloc_array(&pdev->dev, hw->eint->nbase,
 					    sizeof(*hw->eint->base), GFP_KERNEL | __GFP_ZERO);