All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "AngeloGioacchino Del Regno" <angelogioacchino.delregno@collabora.com>
Cc: linux-phy@lists.infradead.org, olteanv@gmail.com,
	neil.armstrong@linaro.org, vkoul@kernel.org
Subject: Re: [PATCH v10 01/11] spmi: Fix potential use-after-free by grabbing of_node reference
Date: Tue, 07 Jul 2026 08:53:02 +0000	[thread overview]
Message-ID: <20260707085302.BDB221F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260707083730.33977-2-angelogioacchino.delregno@collabora.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] Premature release of the OF node reference in `spmi_device_remove()` causes a use-after-free during device unregistration.

Pre-existing issues:
- [High] If `ida_alloc()` fails in `spmi_controller_alloc()`, the error path erroneously frees ID 0.
--

--- Patch [1]: [PATCH v10 01/11] spmi: Fix potential use-after-free by grabbing of_node reference ---
commit 32b4e0545e66d81a2191ec48ea0cb9fc1e026b1c
Author: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>

spmi: Fix potential use-after-free by grabbing of_node reference

This patch increases the of_node refcount when assigning in
of_spmi_register_devices() and releases it in spmi_device_remove() to fix a
potential use-after-free during dynamic overlay unloading.

Link: https://sashiko.dev/#/patchset/20260608100949.36309-1-angelogioacchino.delregno@collabora.com?part=2

> diff --git a/drivers/spmi/spmi.c b/drivers/spmi/spmi.c
> index 57b7c0cb42407..6e701aff60454 100644
> --- a/drivers/spmi/spmi.c
> +++ b/drivers/spmi/spmi.c
> @@ -86,6 +86,9 @@ EXPORT_SYMBOL_GPL(spmi_device_add);
>   */
>  void spmi_device_remove(struct spmi_device *sdev)
>  {
> +	if (IS_ENABLED(CONFIG_OF))
> +		of_node_put(sdev->dev.of_node);
> +
>  	device_unregister(&sdev->dev);
>  }

[Severity: High]
Could this prematurely release the OF node reference?

By dropping the reference before device_unregister() completes, this seems to
risk a use-after-free. During device_unregister(), device_del() is called and
the driver core emits a KOBJ_REMOVE uevent. This triggers the bus callback
spmi_drv_uevent(), which calls of_device_uevent_modalias() where it directly
dereferences the freed dev->of_node to read the compatible string.

Would it be better to drop the OF node reference in spmi_dev_release() so it
is bound to the device's lifetime? (If moved there, the manual of_node_put()
in the spmi_device_add() error path might need to be removed to prevent a
double-free).

[ ... ]

[Severity: High]
This isn't a bug introduced by this patch, but I noticed a potential problem
in the error handling for spmi_controller_alloc().

drivers/spmi/spmi.c:spmi_controller_alloc() {
    ...
	ctrl = kzalloc_flex(*ctrl, priv, size);
    ...
	id = ida_alloc(&ctrl_ida, GFP_KERNEL);
	if (id < 0) {
		dev_err(parent,
			"unable to allocate SPMI controller identifier.\n");
		spmi_controller_put(ctrl);
		return ERR_PTR(id);
	}
    ...
}

If ida_alloc() fails, wouldn't this error path inadvertently free ID 0?

Since kzalloc_flex() zero-initializes ctrl->nr, an ida_alloc() failure will
call spmi_controller_put(ctrl) while ctrl->nr is still 0. This triggers the
spmi_ctrl_release() callback:

drivers/spmi/spmi.c:spmi_ctrl_release() {
    ...
	ida_free(&ctrl_ida, ctrl->nr);
	kfree(ctrl);
}

Because it unconditionally frees ctrl->nr, ID 0 is released. If ID 0 was
already assigned to another SPMI controller, this could cause its ID to be
erroneously marked as available, potentially leading to a sysfs naming
collision ('spmi-0') during the next controller allocation.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260707083730.33977-1-angelogioacchino.delregno@collabora.com?part=1

-- 
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy

  reply	other threads:[~2026-07-07  8:53 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-07  8:37 [PATCH v10 00/11] SPMI: Implement sub-devices and migrate drivers AngeloGioacchino Del Regno
2026-07-07  8:37 ` AngeloGioacchino Del Regno
2026-07-07  8:37 ` [PATCH v10 01/11] spmi: Fix potential use-after-free by grabbing of_node reference AngeloGioacchino Del Regno
2026-07-07  8:37   ` AngeloGioacchino Del Regno
2026-07-07  8:53   ` sashiko-bot [this message]
2026-07-07  9:07   ` Andy Shevchenko
2026-07-07  9:07     ` Andy Shevchenko
2026-07-07  8:37 ` [PATCH v10 02/11] spmi: Remove redundant dev_name() print in spmi_device_add() AngeloGioacchino Del Regno
2026-07-07  8:37   ` AngeloGioacchino Del Regno
2026-07-07  8:37 ` [PATCH v10 03/11] spmi: Print error status with %pe format AngeloGioacchino Del Regno
2026-07-07  8:37   ` AngeloGioacchino Del Regno
2026-07-07  8:37 ` [PATCH v10 04/11] spmi: Remove unneeded goto in spmi_device_add() error path AngeloGioacchino Del Regno
2026-07-07  8:37   ` AngeloGioacchino Del Regno
2026-07-07  8:48   ` sashiko-bot
2026-07-07  8:37 ` [PATCH v10 05/11] spmi: Implement spmi_subdevice_alloc_and_add() and devm variant AngeloGioacchino Del Regno
2026-07-07  8:37   ` AngeloGioacchino Del Regno
2026-07-07  8:48   ` sashiko-bot
2026-07-07  8:37 ` [PATCH v10 06/11] nvmem: qcom-spmi-sdam: Migrate to devm_spmi_subdevice_alloc_and_add() AngeloGioacchino Del Regno
2026-07-07  8:37   ` AngeloGioacchino Del Regno
2026-07-07  8:50   ` sashiko-bot
2026-07-07  8:37 ` [PATCH v10 07/11] power: reset: qcom-pon: " AngeloGioacchino Del Regno
2026-07-07  8:37   ` AngeloGioacchino Del Regno
2026-07-07  8:49   ` sashiko-bot
2026-07-07  8:37 ` [PATCH v10 08/11] phy: qualcomm: eusb2-repeater: " AngeloGioacchino Del Regno
2026-07-07  8:37   ` AngeloGioacchino Del Regno
2026-07-07  8:51   ` sashiko-bot
2026-07-07  8:37 ` [PATCH v10 09/11] misc: qcom-coincell: " AngeloGioacchino Del Regno
2026-07-07  8:37   ` AngeloGioacchino Del Regno
2026-07-07  8:56   ` sashiko-bot
2026-07-07  8:37 ` [PATCH v10 10/11] iio: adc: qcom-spmi-iadc: " AngeloGioacchino Del Regno
2026-07-07  8:37   ` AngeloGioacchino Del Regno
2026-07-07  9:03   ` sashiko-bot
2026-07-07  8:37 ` [PATCH v10 11/11] iio: adc: qcom-spmi-iadc: Remove regmap R/W wrapper functions AngeloGioacchino Del Regno
2026-07-07  8:37   ` AngeloGioacchino Del Regno
2026-07-07  8:55   ` sashiko-bot

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=20260707085302.BDB221F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=linux-phy@lists.infradead.org \
    --cc=neil.armstrong@linaro.org \
    --cc=olteanv@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vkoul@kernel.org \
    /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.