All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Manikandan Muralidharan" <manikandan.m@microchip.com>,
	pratyush@kernel.org, mwalle@kernel.org,
	takahiro.kuwano@infineon.com, miquel.raynal@bootlin.com,
	richard@nod.at, vigneshr@ti.com, robh@kernel.org,
	krzk+dt@kernel.org, conor+dt@kernel.org, srini@kernel.org,
	nicolas.ferre@microchip.com, alexandre.belloni@bootlin.com,
	claudiu.beznea@tuxon.dev, linux@armlinux.org.uk,
	richardcochran@gmail.com, linusw@kernel.org, arnd@arndb.de,
	aaro.koskinen@iki.fi, gregkh@linuxfoundation.org,
	mathieu.dubois-briand@bootlin.com, michael@walle.cc,
	linux-mtd@lists.infradead.org, devicetree@vger.kernel.org
Subject: Re: [PATCH v8 4/8] mtd: spi-nor: sfdp: expose the SFDP as a read-only NVMEM device
Date: Thu, 15 Oct 2026 06:42:03 +0000	[thread overview]
Message-ID: <sashiko-outbox-172348@kernel.org> (raw)
In-Reply-To: <093672d183a06dc2f37366b2f9ff144316850863.1790847519.git.manikandan.m@microchip.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mtd: spi-nor: UAF of SFDP cached data via NVMEM cell consumer after unbind
--

commit 27ebe83329cdf6f2e062cc50041bbf3a877fcaba
Author: Manikandan Muralidharan <manikandan.m@microchip.com>

mtd: spi-nor: sfdp: expose the SFDP as a read-only NVMEM device

This commit exposes the SFDP tables read from the SPI NOR flash as a
read-only NVMEM device. The device is conditionally registered when a
jedec,sfdp child node is present in the device tree, allowing consumers
to reference any SFDP data.

> diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c
> --- a/drivers/mtd/spi-nor/core.c
> +++ b/drivers/mtd/spi-nor/core.c

[ ... ]

> +static int spi_nor_sfdp_reg_read(void *priv, unsigned int offset,
> +				 void *val, size_t bytes)
> +{
> +	struct spi_nor *nor = priv;
> +	struct sfdp *sfdp = nor->sfdp;
> +	size_t sfdp_size = sfdp->num_dwords * sizeof(*sfdp->dwords);
> +
> +	if (offset >= sfdp_size || bytes > sfdp_size - offset)
> +		return -EINVAL;
> +
> +	/* The cached SFDP is kept in on-flash (little-endian) byte order. */
> +	memcpy(val, (u8 *)sfdp->dwords + offset, bytes);

[Severity: High]
Does this code introduce a potential use-after-free if the SPI NOR device is
unbound while an NVMEM cell consumer still holds a reference?

When an in-kernel consumer driver obtains an NVMEM cell reference to the
SFDP device, it increments the nvmem_device reference count. Later, if the
SPI NOR device is unbound via sysfs, the devres framework runs
devm_nvmem_unregister.

Looking at nvmem_unregister in drivers/nvmem/core.c, it only drops the
provider's reference count. It does not explicitly block future reads, clear
the operations pointer, or disconnect active consumers. Because the consumer
still holds a cell reference, the nvmem_device object and its reg_read ops
remain active.

Devres then proceeds to free the nor struct and nor->sfdp->dwords buffers
which were allocated earlier in the probe sequence.

If the consumer subsequently calls nvmem_cell_read, the read is forwarded
to spi_nor_sfdp_reg_read, which dereferences the already freed nor and sfdp
memory.

> +
> +	return 0;
> +}

[ ... ]

> +static int spi_nor_register_sfdp_nvmem(struct spi_nor *nor)
> +{
> +	struct device *dev = nor->dev;
> +	struct nvmem_config config = { };
> +	struct nvmem_device *nvmem;
> +	struct device_node *np;
> +
> +	if (!nor->sfdp)
> +		return 0;
> +
> +	np = of_get_compatible_child(dev_of_node(dev), "jedec,sfdp");
> +	if (!np)
> +		return 0;
> +
> +	config.dev = dev;
> +	config.of_node = np;
> +	config.name = "sfdp";
> +	config.id = NVMEM_DEVID_AUTO;
> +	config.owner = THIS_MODULE;
> +	config.read_only = true;
> +	config.word_size = 1;
> +	config.stride = 1;
> +	config.size = (int)(nor->sfdp->num_dwords * sizeof(*nor->sfdp->dwords));
> +	config.reg_read = spi_nor_sfdp_reg_read;
> +	config.priv = nor;
> +
> +	nvmem = devm_nvmem_register(dev, &config);

[Severity: High]
Is there a risk to binding the NVMEM registration to the devres lifecycle
here?

Since devres tears down in reverse order, devm_nvmem_unregister will run
before the SFDP memory is freed. But as noted above, this does not safely
destroy the NVMEM device if a consumer is holding a cell reference, allowing
the operations pointer to outlive the memory it accesses.

> +	of_node_put(np);
> +	if (IS_ERR(nvmem)) {

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1790847519.git.manikandan.m@microchip.com?part=4

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

WARNING: multiple messages have this Message-ID (diff)
From: sashiko-bot@kernel.org
To: "Manikandan Muralidharan" <manikandan.m@microchip.com>,
	pratyush@kernel.org, mwalle@kernel.org,
	takahiro.kuwano@infineon.com, miquel.raynal@bootlin.com,
	richard@nod.at, vigneshr@ti.com, robh@kernel.org,
	krzk+dt@kernel.org, conor+dt@kernel.org, srini@kernel.org,
	nicolas.ferre@microchip.com, alexandre.belloni@bootlin.com,
	claudiu.beznea@tuxon.dev, linux@armlinux.org.uk,
	richardcochran@gmail.com, linusw@kernel.org, arnd@arndb.de,
	aaro.koskinen@iki.fi, gregkh@linuxfoundation.org,
	mathieu.dubois-briand@bootlin.com, michael@walle.cc,
	linux-mtd@lists.infradead.org, devicetree@vger.kernel.org
Subject: Re: [PATCH v8 4/8] mtd: spi-nor: sfdp: expose the SFDP as a read-only NVMEM device
Date: Thu, 15 Oct 2026 06:42:03 +0000	[thread overview]
Message-ID: <sashiko-outbox-172348@kernel.org> (raw)
In-Reply-To: <093672d183a06dc2f37366b2f9ff144316850863.1790847519.git.manikandan.m@microchip.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mtd: spi-nor: UAF of SFDP cached data via NVMEM cell consumer after unbind
--

commit 27ebe83329cdf6f2e062cc50041bbf3a877fcaba
Author: Manikandan Muralidharan <manikandan.m@microchip.com>

mtd: spi-nor: sfdp: expose the SFDP as a read-only NVMEM device

This commit exposes the SFDP tables read from the SPI NOR flash as a
read-only NVMEM device. The device is conditionally registered when a
jedec,sfdp child node is present in the device tree, allowing consumers
to reference any SFDP data.

> diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c
> --- a/drivers/mtd/spi-nor/core.c
> +++ b/drivers/mtd/spi-nor/core.c

[ ... ]

> +static int spi_nor_sfdp_reg_read(void *priv, unsigned int offset,
> +				 void *val, size_t bytes)
> +{
> +	struct spi_nor *nor = priv;
> +	struct sfdp *sfdp = nor->sfdp;
> +	size_t sfdp_size = sfdp->num_dwords * sizeof(*sfdp->dwords);
> +
> +	if (offset >= sfdp_size || bytes > sfdp_size - offset)
> +		return -EINVAL;
> +
> +	/* The cached SFDP is kept in on-flash (little-endian) byte order. */
> +	memcpy(val, (u8 *)sfdp->dwords + offset, bytes);

[Severity: High]
Does this code introduce a potential use-after-free if the SPI NOR device is
unbound while an NVMEM cell consumer still holds a reference?

When an in-kernel consumer driver obtains an NVMEM cell reference to the
SFDP device, it increments the nvmem_device reference count. Later, if the
SPI NOR device is unbound via sysfs, the devres framework runs
devm_nvmem_unregister.

Looking at nvmem_unregister in drivers/nvmem/core.c, it only drops the
provider's reference count. It does not explicitly block future reads, clear
the operations pointer, or disconnect active consumers. Because the consumer
still holds a cell reference, the nvmem_device object and its reg_read ops
remain active.

Devres then proceeds to free the nor struct and nor->sfdp->dwords buffers
which were allocated earlier in the probe sequence.

If the consumer subsequently calls nvmem_cell_read, the read is forwarded
to spi_nor_sfdp_reg_read, which dereferences the already freed nor and sfdp
memory.

> +
> +	return 0;
> +}

[ ... ]

> +static int spi_nor_register_sfdp_nvmem(struct spi_nor *nor)
> +{
> +	struct device *dev = nor->dev;
> +	struct nvmem_config config = { };
> +	struct nvmem_device *nvmem;
> +	struct device_node *np;
> +
> +	if (!nor->sfdp)
> +		return 0;
> +
> +	np = of_get_compatible_child(dev_of_node(dev), "jedec,sfdp");
> +	if (!np)
> +		return 0;
> +
> +	config.dev = dev;
> +	config.of_node = np;
> +	config.name = "sfdp";
> +	config.id = NVMEM_DEVID_AUTO;
> +	config.owner = THIS_MODULE;
> +	config.read_only = true;
> +	config.word_size = 1;
> +	config.stride = 1;
> +	config.size = (int)(nor->sfdp->num_dwords * sizeof(*nor->sfdp->dwords));
> +	config.reg_read = spi_nor_sfdp_reg_read;
> +	config.priv = nor;
> +
> +	nvmem = devm_nvmem_register(dev, &config);

[Severity: High]
Is there a risk to binding the NVMEM registration to the devres lifecycle
here?

Since devres tears down in reverse order, devm_nvmem_unregister will run
before the SFDP memory is freed. But as noted above, this does not safely
destroy the NVMEM device if a consumer is holding a cell reference, allowing
the operations pointer to outlive the memory it accesses.

> +	of_node_put(np);
> +	if (IS_ERR(nvmem)) {

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1790847519.git.manikandan.m@microchip.com?part=4

  parent reply	other threads:[~2026-10-15  6:42 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14  6:40 [PATCH v8 0/8] Read MAC address from SST vendor specific SFDP region Manikandan Muralidharan
2026-10-14  6:40 ` Manikandan Muralidharan
2026-10-14  6:40 ` [PATCH v8 1/8] dt-bindings: mtd: jedec,spi-nor: allow the SFDP to be exposed via NVMEM Manikandan Muralidharan
2026-10-14  6:40   ` Manikandan Muralidharan
2026-10-14  6:40 ` [PATCH v8 2/8] dt-bindings: nvmem: layouts: add Microchip/SST SFDP EUI layout Manikandan Muralidharan
2026-10-14  6:40   ` Manikandan Muralidharan
2026-10-14  6:40 ` [PATCH v8 3/8] nvmem: core: take a reference on dev.of_node in nvmem_register() Manikandan Muralidharan
2026-10-14  6:40   ` Manikandan Muralidharan
2026-10-14  8:36   ` Miquel Raynal
2026-10-14  8:36     ` Miquel Raynal
2026-10-15  9:12   ` Michael Walle
2026-10-15  9:12     ` Michael Walle
2026-10-15 11:26     ` Manikandan.M
2026-10-15 11:26       ` Manikandan.M
2026-10-14  6:40 ` [PATCH v8 4/8] mtd: spi-nor: sfdp: expose the SFDP as a read-only NVMEM device Manikandan Muralidharan
2026-10-14  6:40   ` Manikandan Muralidharan
2026-10-14  8:38   ` Miquel Raynal
2026-10-14  8:38     ` Miquel Raynal
2026-10-15  6:42   ` sashiko-bot [this message]
2026-10-15  6:42     ` sashiko-bot
2026-10-15  9:21   ` Michael Walle
2026-10-15  9:21     ` Michael Walle
2026-10-14  6:40 ` [PATCH v8 5/8] nvmem: layouts: add Microchip/SST SFDP EUI layout driver Manikandan Muralidharan
2026-10-14  6:40   ` Manikandan Muralidharan
2026-10-14  8:52   ` Miquel Raynal
2026-10-14  8:52     ` Miquel Raynal
2026-10-15 11:42     ` Manikandan.M
2026-10-15 11:42       ` Manikandan.M
2026-10-15  6:42   ` sashiko-bot
2026-10-15  6:42     ` sashiko-bot
2026-10-14  6:40 ` [PATCH v8 6/8] ARM: dts: microchip: sama5d27_wlsom1: use fixed-partitions for QSPI flash Manikandan Muralidharan
2026-10-14  6:40   ` Manikandan Muralidharan
2026-10-14  6:40 ` [PATCH v8 7/8] ARM: dts: microchip: sama5d27_wlsom1: read MAC address from QSPI SFDP Manikandan Muralidharan
2026-10-14  6:40   ` Manikandan Muralidharan
2026-10-14  6:40 ` [PATCH v8 8/8] ARM: configs: sama5: enable Microchip/SST SFDP EUI NVMEM layout Manikandan Muralidharan
2026-10-14  6:40   ` Manikandan Muralidharan

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-172348@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=aaro.koskinen@iki.fi \
    --cc=alexandre.belloni@bootlin.com \
    --cc=arnd@arndb.de \
    --cc=claudiu.beznea@tuxon.dev \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=krzk+dt@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=linux@armlinux.org.uk \
    --cc=manikandan.m@microchip.com \
    --cc=mathieu.dubois-briand@bootlin.com \
    --cc=michael@walle.cc \
    --cc=miquel.raynal@bootlin.com \
    --cc=mwalle@kernel.org \
    --cc=nicolas.ferre@microchip.com \
    --cc=pratyush@kernel.org \
    --cc=richard@nod.at \
    --cc=richardcochran@gmail.com \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=srini@kernel.org \
    --cc=takahiro.kuwano@infineon.com \
    --cc=vigneshr@ti.com \
    /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.