All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Andrey Derkach" <gabin278@gmail.com>
Cc: linux-gpio@vger.kernel.org
Subject: Re: [PATCH] pinctrl: qcom: lpass-lpi: Don't touch reserved pins
Date: Sat, 05 Dec 2026 12:59:20 +0000	[thread overview]
Message-ID: <sashiko-outbox-251513@kernel.org> (raw)
In-Reply-To: <20261205123913.1063058-1-gabin278@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] pinctrl: qcom: lpass-lpi: UAF of chip.gpiodev on unbind or
  probe failure
- [Medium] pinctrl: qcom: lpass-lpi: probe race before gpiochip
  valid_mask init
- [Low] pinctrl: qcom: lpass-lpi: missing Fixes trailer on bug-fix
  commit
--

commit ef26c5c45ab94e6f1eff8368010383e7945b02b7
Author: Andrey Derkach <gabin278@gmail.com>

> pinctrl: qcom: lpass-lpi: Don't touch reserved pins
>
> Since commit a22d2598a563 ("dt-bindings: qcom: lpass-lpi-common: add
> reserved GPIOs property") an LPI pin controller can have
> gpio-reserved-ranges. On SDM670 phones such as the Pixel 3a these are
> the sensor pins, which the firmware protects: reading their registers
> hangs the bus and freezes the SoC. gpiolib leaves reserved lines alone,
> but the LPI driver still reads them from the GPIO debugfs dump and from
> the pinconf-groups debugfs file, and pinmux does not refuse to mux
> them.
>
> Do what pinctrl-msm does since commit 691bf5d5a7bf ("pinctrl: qcom:
> Don't allow protected pins to be requested") and
> commit 27030ff78772 ("pinctrl: qcom: fail to retrieve configuration
> from invalid pin groups"): refuse pinmux requests for reserved pins,
> fail pinconf reads of them and leave them out of the GPIO debugfs dump.
>
> Tested on the Xiaomi Mi 9 SE (SDM712), which reserves <0 18>, <26 6>:
> without this change reading /sys/kernel/debug/gpio freezes the SoC;
> with it the dump lists only GPIOs 18-25, and pinconf-groups shows the
> reserved groups without configs.
>
> Signed-off-by: Andrey Derkach <gabin278@gmail.com>

[Severity: Low]
This fixes a bus hang when accessing reserved pins.  Should this include:

  Fixes: 6e261d1090d6 ("pinctrl: qcom: Add sm8250 lpass lpi pinctrl driver")

> diff --git a/drivers/pinctrl/qcom/pinctrl-lpass-lpi.c b/drivers/pinctrl/qcom/pinctrl-lpass-lpi.c
> index f693e5101a7bc..de5bab0d28ecd 100644
> --- a/drivers/pinctrl/qcom/pinctrl-lpass-lpi.c
> +++ b/drivers/pinctrl/qcom/pinctrl-lpass-lpi.c
> @@ -171,7 +171,15 @@ static int lpi_gpio_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
>  	return 0;
>  }
>  
> +static int lpi_gpio_request(struct pinctrl_dev *pctldev, unsigned int offset)
> +{
> +	struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
> +
> +	return gpiochip_line_is_valid(&pctrl->chip, offset) ? 0 : -EINVAL;
> +}
> +
>  static const struct pinmux_ops lpi_gpio_pinmux_ops = {
> +	.request		= lpi_gpio_request,
>  	.get_functions_count	= lpi_gpio_get_functions_count,
>  	.get_function_name	= lpi_gpio_get_function_name,
>  	.get_function_groups	= lpi_gpio_get_function_groups,

[Severity: Medium]
Can lpi_gpio_request() allow access to a reserved pin during probe before
pctrl->chip has initialized its valid_mask?

In lpi_pinctrl_probe(), devm_pinctrl_register() and
lpi_build_pin_desc_groups() expose the pin controller and its groups before
devm_gpiochip_add_data() runs:

CPU0                                CPU1
lpi_pinctrl_probe()
  devm_pinctrl_register()
  lpi_build_pin_desc_groups()
                                    pin_request()
                                      lpi_gpio_request()
                                        gpiochip_line_is_valid()
                                          !gc->gpiodev -> true  <-- bypass
  devm_gpiochip_add_data()
    gpiochip_init_valid_mask()

Even after gpiodev is assigned, gpiochip_init_valid_mask() publishes an
all-1s valid_mask via gpiochip_allocate_mask() before
gpiochip_apply_reserved_ranges() clears the reserved bits.  A concurrent
pin_request() or debugfs read via lpi_config_get() in this window can read
or write firmware-protected pin registers and hang the bus.

> @@ -189,6 +197,10 @@ static int lpi_config_get(struct pinctrl_dev *pctldev,
>  	int pull;
>  	int ret;
>  
> +	/* Reserved pins may be protected, reading them can hang the bus */
> +	if (!gpiochip_line_is_valid(&state->chip, pin))
> +		return -EINVAL;
> +
>  	ret = lpi_gpio_read(state, pin, LPI_GPIO_CFG_REG, &ctl_reg);
>  	if (ret)
>  		return ret;

[Severity: Medium]
Can gpiochip_line_is_valid() dereference a freed state->chip.gpiodev on
unbind or probe failure?

Because lpi_pinctrl_probe() calls devm_pinctrl_register() before
devm_gpiochip_add_data(), devres tears them down in reverse order and runs
gpiochip_remove() before pinctrl_unregister().  gpiochip_remove() frees
valid_mask and drops the last reference to gdev without clearing
gc->gpiodev:

CPU0                              CPU1
                                  pin_config_group_get()
                                    mutex_lock(&pctldev->mutex)
devres_release_all()
  gpiochip_remove()
    gpiochip_free_valid_mask()
    gpio_device_put(gdev)
      kfree(gdev)
  pinctrl_unregister()
    mutex_lock(&pctldev->mutex)
                                    lpi_config_get()
                                      gpiochip_line_is_valid()
                                        gdev->valid_mask  <-- use-after-free

If an open gpiochip fd keeps gdev alive after gpiochip_free_valid_mask()
clears valid_mask to NULL, gpiochip_line_is_valid() instead returns true
and accesses the protected register, hanging the bus.  The same lifetime
mismatch applies to lpi_gpio_request(), and to a failing
devm_gpiochip_add_data() that frees gdev without clearing gc->gpiodev while
pctrl->ctrl is still registered.

Storing a driver-owned reserved-pin bitmap parsed before
devm_pinctrl_register() would avoid both this lifetime mismatch and the
probe race above.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205123913.1063058-1-gabin278@gmail.com?part=1

      reply	other threads:[~2026-12-05 12:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 12:39 [PATCH] pinctrl: qcom: lpass-lpi: Don't touch reserved pins Andrey Derkach
2026-12-05 12:59 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-251513@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=gabin278@gmail.com \
    --cc=linux-gpio@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.