From: tze.yee.ng@altera.com
To: linux-kernel@vger.kernel.org,
Pratyush Yadav <pratyush@kernel.org>,
Michael Walle <mwalle@kernel.org>,
Takahiro Kuwano <takahiro.kuwano@infineon.com>,
linux-mtd@lists.infradead.org
Subject: [PATCH v3 0/2] mtd: spi-nor: fix use-after-free on unbind with open handles
Date: Thu, 22 Oct 2026 11:53:50 +0800 [thread overview]
Message-ID: <20261022035352.52129-1-tze.yee.ng@altera.com> (raw)
From: Tze Yee Ng <tze.yee.ng@altera.com>
An open /dev/mtdX can outlive driver unbind. The MTD core keeps mtd_info
registered until the last close, but a spi_nor allocated with devm_*() on
the SPI device is already freed, so a later close, ioctl, read or
mtdblock flush uses freed memory.
v2 moved the lifetime into the MTD core (mtd->_free and mtd->removed).
Review pointed out that this creates new problems in the core: the master
can be freed inside __put_mtd_device() while it is still used, the driver
object leaks when CONFIG_MTD_PARTITIONED_MASTER is off, and a lockless
removed check races with remove(). Only spi-nor needs the fix, so v3
goes back to a spi-nor-local kref and reduces the core change to one
reorder:
- Patch 1: in __put_mtd_device(), call ->_put_device() first and drop the
module reference last, so the owner module stays pinned while
->_put_device() frees the driver object.
- Patch 2: on the spi-mem path, allocate spi_nor, params, erase regions
and the bounce buffer with kmalloc and refcount spi_nor (one
reference for registration plus one per open handle). remove()
unregisters debugfs and the MTD device, then sets nor->removed under
the nor lock; prep_and_lock*() re-check it under the lock and return
-ENODEV. Legacy controllers keep the devres lifetime.
Not covered:
- A sysfs file held open across unbind is a core lifetime question for
all MTD drivers and is left out of this series.
---
Changes in v3:
- Drop the core _free/removed hooks. Patch 1 is only the module_put()
reorder.
- Patch 2: spi-nor kref again, removed re-checked under the lock,
controller module cached at probe, debugfs and MTD unregistered at the
start of remove() so mtdblock can flush, eraseregions leak and probe
error paths fixed.
Changes in v2:
- Drop the unrelated RWW self-deadlock fix.
- New patch 1: MTD-core _free / removed hooks.
- Patch 2: drop the spi-nor kref / refcounted / controller_module
and the prep_and_lock*() removed checks; use the core hooks.
Also keep eraseregions on that lifetime, free on the probe
error path, and unregister debugfs from _free.
v2: https://lore.kernel.org/all/cover.1791512623.git.tze.yee.ng@altera.com/
Tze Yee Ng (2):
mtd: core: drop the module reference after ->_put_device()
mtd: spi-nor: fix use-after-free on unbind with open handles
drivers/mtd/mtdcore.c | 9 +-
drivers/mtd/spi-nor/core.c | 173 +++++++++++++++++++++++++++-------
drivers/mtd/spi-nor/core.h | 2 +
drivers/mtd/spi-nor/debugfs.c | 9 +-
include/linux/mtd/spi-nor.h | 9 ++
5 files changed, 159 insertions(+), 43 deletions(-)
--
2.43.7
next reply other threads:[~2026-10-22 3:54 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-22 3:53 tze.yee.ng [this message]
2026-10-22 3:53 ` [PATCH v3 1/2] mtd: core: drop the module reference after ->_put_device() tze.yee.ng
2026-10-22 3:53 ` [PATCH v3 2/2] mtd: spi-nor: fix use-after-free on unbind with open handles tze.yee.ng
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=20261022035352.52129-1-tze.yee.ng@altera.com \
--to=tze.yee.ng@altera.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=mwalle@kernel.org \
--cc=pratyush@kernel.org \
--cc=takahiro.kuwano@infineon.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®