[PATCH v3 0/2] mtd: spi-nor: fix use-after-free on unbind with open handles
NG, TZE YEE
tze.yee.ng at altera.com
Thu Nov 19 18:21:41 PST 2026
Hi Michael,
Thanks for taking a look.
On 18/11/2026 5:36 pm, Michael Walle wrote:
> Hi,
>
> On Wed Nov 18, 2026 at 9:07 AM CET, Tze Yee Ng wrote:
>> Gentle ping on this patch. Any chance for a review when you have time?
>
> I had a quick glance, and honestly, I really don't like all the
> reference and resource handling splattered all over the place.
I agree it is spread across spi-nor. It comes from one thing, which the
MTD core still uses the driver object after unbind, so spi-nor has to
keep that object and its data alive and fence its own operations.
> Also do we really need the low level kref_ handling?
A core-owned lifetime would avoid it. v2 tried that, with a release hook
and a removed flag in the core. Review found problems in that design:
the master can be freed inside __put_mtd_device(), the driver object
leaked with CONFIG_MTD_PARTITIONED_MASTER off, and a lockless removed
check raced with remove(). The kref in spi-nor is what works without a
larger change in the core; patch 1 is the only core change.
> And what about spi-nand and all the other MTD subsystems? They have the same
> problem, no?
>
> -michael
Yes. spinand allocates its object with devm_kzalloc() and frees it after
mtd_device_unregister(), and many other drivers that embed mtd_info do
the same. A generic fix belongs in the core, but it is a much larger
change that touches every MTD driver's lifetime rules.
I would prefer to keep this series as a spi-nor-only fix, since it
solves the problem for spi-nor today. James Hilliard's raw NAND series
[1] adds a generic opt-in lifetime to the core (mtd_set_release()). I
would be happy to look at moving spi-nor onto it if it lands, but maybe
in another series. Are you okay with keeping this series as is?
Thanks,
Tze Yee
[1]
https://lore.kernel.org/all/20261115-submit-rawnand-retained-handle-lifetime-v1-v5-0-cbc49c5c5e74@gmail.com/
More information about the linux-mtd
mailing list