mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yu Zhang <yuz08559@gmail.com>
To: Srinivas Kandagatla <srini@kernel.org>
Cc: Bartosz Golaszewski <brgl@kernel.org>, linux-kernel@vger.kernel.org
Subject: [BUG] nvmem: use-after-free of nvmem->layout in nvmem_layout_module_put() on provider teardown
Date: Mon, 26 Oct 2026 22:58:56 +1000	[thread overview]
Message-ID: <20261026125856.73564-1-yuz08559@gmail.com> (raw)

Hi,

On nvmem provider teardown while a consumer still holds a cell handle,
nvmem_cell_put() dereferences a freed nvmem->layout - a use-after-free.
This looks like a regression from 87341c4dd5de ("nvmem: remove
duplicated reference counting").

Mechanism (drivers/nvmem/core.c + layouts.c, torvalds/master):

nvmem_unregister() frees the layout sub-device immediately:

    nvmem_unregister()                              core.c:1075
      ...
      nvmem_destroy_layout(nvmem);                  core.c:1088
        device_unregister(&nvmem->layout->dev);     layouts.c:205
      device_unregister(&nvmem->dev);               core.c:1091

nvmem_destroy_layout() does not clear nvmem->layout. The layout device's
only reference is its own and its parent is nvmem->dev (layouts.c:110),
so device_unregister() drops it to zero and nvmem_layout_release_device()
kfree()s the layout (layouts.c:84). nvmem->layout now dangles.

A consumer that still holds an nvmem_cell keeps nvmem->dev alive (the
cell took a device reference via nvmem_device_match()), so the nvmem
struct and its cell entries survive this. When the consumer later
releases the cell:

    nvmem_cell_put()                                core.c:1591
      nvmem_layout_module_put(nvmem);               core.c:1599
        if (nvmem->layout && nvmem->layout->dev.driver)   <-- UAF read
            module_put(nvmem->layout->dev.driver->owner);

nvmem->layout is non-NULL but freed, so nvmem->layout->dev.driver is read
from freed memory, followed by module_put() on a garbage owner.

Regression:
Before 87341c4dd5de, nvmem_destroy_layout() ran from the device-release
callback, i.e. only after the last reference (including consumers' cell
references) was dropped. That commit removed the kref and moved
nvmem_destroy_layout() into nvmem_unregister(), where it now runs
immediately regardless of outstanding cells. The companion SRCU change
gave nvmem->ops and the cell entries deferred/guarded teardown, but
nvmem->layout was left dereferenceable after free.

Reproduction (deterministic; needs a DT nvmem provider with a layout,
e.g. "fixed-layout", and a consumer holding a cell across provider
removal. From code inspection; no KASAN splat captured yet):
  1. provider binds and instantiates the layout device;
  2. consumer does devm_nvmem_cell_get() and keeps the cell;
  3. echo <provider> > .../unbind  -> nvmem_unregister() frees the
     layout; nvmem->layout dangles;
  4. echo <consumer> > .../unbind  -> devm release -> nvmem_cell_put()
     -> nvmem_layout_module_put() reads the freed nvmem->layout.

Note on fixing it:
The obvious "move nvmem_destroy_layout() into nvmem_release()" does not
work: the layout device's parent is nvmem->dev, so it holds a reference
on nvmem->dev, and nvmem_release() (the nvmem->dev release callback)
cannot run until the layout device is gone. A correct fix probably needs
the layout module reference decoupled from the layout device's lifetime
(e.g. caching the owner, or keeping the layout alive until all cells are
put). As this area was just reworked, I'm reporting rather than guessing
at the fix.

This is distinct from the recently discussed nvmem_cell_put() /
__nvmem_device_put() ordering (5b6b6fc49189 and the follow-up that was
rejected as a duplicate): those address the consumer's own put dropping
the last device reference, whereas here the layout is freed by the
provider's nvmem_unregister() while a consumer still holds a cell.

Thanks,
Yu Zhang

                 reply	other threads:[~2026-10-26 13:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261026125856.73564-1-yuz08559@gmail.com \
    --to=yuz08559@gmail.com \
    --cc=brgl@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=srini@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 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®