Repository navigation
software node: fix the use-after-free when a shared managed node is removed (backport + fix + KUnit) - #2914
Conversation
device_create_managed_software_node() creates a node whose lifetime is tied to the device it was created for. software_node_notify_remove() is called for every device that has the node as its fwnode (primary OR secondary) and, if the node is managed, unconditionally detaches it from that device and drops the creation reference. This breaks when the node is shared with another device. Example: the i2c-designware copies the parent's fwnode to the adapter's struct device. When the adapter is deleted, it releases the reference that belongs to the owner and the latter ends up with a dangling pointer. Record the device's "manager" and only perform the actual release when notify is called for that "owner" device. Note that it was never the intention that managed software nodes would be shared, but descendants reusing ancestor firmware nodes is not uncommon and such use is hard to predict when allocating software nodes. Reported-by: Balasubramani Vivekanandan <[email protected]> Closes: https://lore.kernel.org/all/[email protected]/ Fixes: 151f6ff ("software node: Provide replacement for device_add_properties()") Signed-off-by: Bartosz Golaszewski <[email protected]> Fixes: 0f485f8 ("drm/xe/i2c: use device_create_managed_software_node()") Reviewed-by: Balasubramani Vivekanandan <[email protected]> Tested-by: Balasubramani Vivekanandan <[email protected]> Reviewed-by: Heikki Krogerus <[email protected]> Cc: [email protected] Link: https://patch.msgid.link/20261102-swnode-only-owner-release-v1-1-ee4c9022a629@oss.qualcomm.com [ johan: drop temporary variable; skip clearing owner field; mention reuse not intended in commit message ] Signed-off-by: Johan Hovold <[email protected]> (cherry picked from commit f8623ce596492d2e96ca57eeec992938d3d79f00) Signed-off-by: Ryan Murray <[email protected]>
software_node_notify_remove() drops the reference the device took in
software_node_notify() and only then reads swnode->owner to decide
whether to release the managed node as well.
The node does not always survive that put. Other devices may carry the
owner's node; commit f8623ce59649 ("software node: only let the owner release a managed node")
made the sharer's removal leave the owner's reference alone, but when
the owner is unregistered first, the sharer's reference is the last
one. Its put frees the node and the owner check then reads freed
memory:
BUG: KASAN: slab-use-after-free in software_node_notify_remove+0x1c4/0x1ec
Read of size 8 at addr fff000000230e690 by task kunit_try_catch/42
Call trace:
software_node_notify_remove+0x1c4/0x1ec
device_del+0x324/0xa1c
device_unregister+0x14/0x2c
swnode_managed_owner_removed_first+0x10c/0x18c
...
Freed by task 42:
kfree+0x17c/0x3cc
software_node_release+0x128/0x25c
kobject_put+0x140/0x25c
software_node_notify_remove+0x138/0x1ec
device_del+0x324/0xa1c
Check for the owner, and drop the managed reference if it is one,
before dropping the device's own reference. The owner still holds both
of its references at that point, and no other device touches the node
after its put.
Fixes: 151f6ff ("software node: Provide replacement for device_add_properties()")
Cc: [email protected]
Signed-off-by: Ryan Murray <[email protected]>
A managed software node belongs to the device it was created for, but
other devices may carry the same fwnode, a child reusing its parent's
node for instance. Removing those devices must drop only their own
references, in either order, and free the node exactly once.
Add a suite that gives a device a managed node, adds a second device
sharing it and unregisters the two in both orders, checking that the
remaining device can still read the node's properties. With KASAN
enabled, the sharer-first case catches the double put fixed by
commit f8623ce59649 ("software node: only let the owner release a managed node")
and the owner-first case the read after the final put fixed by the
previous patch.
The tests are built with the existing DRIVER_SWNODE_KUNIT_TEST option,
whose prompt now covers software nodes in general.
Signed-off-by: Ryan Murray <[email protected]>
|
Review at 47e2324. Patch 1 (6f78a97) is upstream f8623ce59649 ("software node: only let the owner release a managed node", v7.4-rc3), and the diff matches it exactly. Bartosz keeps authorship, and the original Reported/Reviewed/Tested/Johan SoB trail is kept under
|
DjDeveloperr
left a comment
There was a problem hiding this comment.
Formal adversarial §1–§6 pre-PR checklist review at 47e2324f1b2dd5cee91c7b66153d1ceaf0f48ca2 (COMMENT — 0 code/policy blockers; waiting on CI).
Checklist Summary
- §1 (No model gating): Pass — generic driver-core code (
drivers/base/swnode.c). - §2 (Production-ready, no hacks): Pass — clean upstream backport + targeted ordering fix + KUnit suite.
- §3 (Safety — refcounting & lifetime): Pass — traced
software_node_notify()andsoftware_node_notify_remove()(drivers/base/swnode.c:1159-1220) in both removal orders:- Starting with 3 references (creation ref, owner notify ref, sharer notify ref):
- Sharer removed first:
swnode->owner == shareris false → singleswnode_put()(3 → 2), no access after put; owner retains valid node and drops both remaining refs on its own removal (2 → 1 → 0). - Owner removed first:
swnode->owner == owneris true →set_secondary_fwnode(owner, NULL)+ managedswnode_put()(3 → 2), followed by owner's notifyswnode_put()(2 → 1). When sharer is later removed,swnode->owner == shareris evaluated before the finalswnode_put()(1 → 0), eliminating the slab-use-after-free.
- Sharer removed first:
- Starting with 3 references (creation ref, owner notify ref, sharer notify ref):
- §4 (Clean-room & attribution): Pass — commit 1 (
6f78a97f1117) cleanly preserves Bartosz Golaszewski's authorship and upstream trailers under(cherry picked from commit f8623ce596492d2e96ca57eeec992938d3d79f00)with Ryan'sSigned-off-by:; commits 2–3 carry human author/committer (Ryan Murray <[email protected]>) and humanSigned-off-by:only (zero AI trailers in commits); PR body includesAssisted-by:. - §5 (Evidence): Waiting on CI — driver-core change with thorough arm64 QEMU KASAN KUnit evidence (
6/8→7/8→8/8on this tree, plus upstream v7.4-rc4 repro7/8→8/8). GitHub CI currently stands at 2/7 green (host-tests,source) and 5/7QUEUED(rust-build,kunit,DWC3/ATC×3). Cannot approve until CI finishes green. - §6 (Naming): Pass.
Non-blocking Notes
- Stale
swnode->ownerpointer reuse (worth raising upstream): When the owner is unregistered first,swnode->ownerretains the freedstruct device *address. If slab reuses that address for a newly allocated sharer of the still-liveswnode,swnode->owner == devwould falsely match and double-put. With the new ordering, clearingswnode->owner = NULL;inside theif (swnode->owner == dev)block before the puts is safe and closes that ABA window. swnode-managed-test.c:162-164: IfKUNIT_ASSERT_TRUE(test, is_software_node(fwnode))orswnode_managed_dev_add(sharer)aborts,*ownerleaks withoutdevice_unregister(); registering akunit_add_action()cleanup would cover abort paths. Also notedrivers/base/test/.kunitconfigdoes not enableCONFIG_DRIVER_SWNODE_KUNIT_TESTorCONFIG_KASAN, though the test file header clearly documents the KASAN requirement.
Assisted-by: LLM (Gemini 4 Neon)
DjDeveloperr
left a comment
There was a problem hiding this comment.
All 7/7 CI checks (host-tests, source, kunit, rust-build, and the 3x DWC3/ATC build matrix) are now green on 47e2324f1b2d. Both the upstream swnode UAF cherry-pick and the follow-up fix have 0 code/policy blockers — ready for MERGETRAIN-LINUX4.
Assisted-by: LLM (Gemini 4 Neon)
|
Merge-train review at 47e2324 (non-author pass): no blockers. Checked the refcount walk in Nit (optional): after the owner goes, Assisted-by: LLM (Claude:Opus 5.5) |
|
Correction to my nit above. The earlier review is right that a device added after the owner has gone could be allocated at the owner's old address and given this fwnode. It would then pass the Assisted-by: LLM (Claude:Opus 5.5) |
Problem
Removing a device that shares a managed software node can free the node twice or read it after it is freed (KASAN slab-use-after-free in
software_node_notify_remove()). We hit it while writing KUnit tests for a gpiochip whosegdev->devinherits its parent's managed node; any platform device with properties that registers a gpiochip, i2c adapter or similar child on the same fwnode is exposed on unbind.Two orderings, two bugs:
swnode->ownerfrom the freed node.Change
software-node-managed(drivers/base/test/swnode-managed-test.c): owner plus sharer, unregistered in both orders.Testing
arm64 QEMU KUnit with KASAN (
kasan_multi_shot), suitessoftware-node-*:checkpatch --strict clean on 2 and 3, except one line over 75 chars, which is a
commit <sha> ("title")reference that checkpatch itself wants kept on one line.Still to run
Nothing hardware-specific: this is driver-core code, covered by KUnit. It reaches every Mac through the next fleet kernel.
Reviewer detail
swnode->owner == devcheck, and the managed put that depends on it, before the device's ownswnode_put(). The owner still holds both of its references at that point. A non-owner never touches the node after its put.managedbit too.swnode->ownerpointing at the freed owner. It is only compared, never dereferenced. Johan dropped the clearing during upstream review.traces/kernel/20261123-swnode-managed-uaf/. The upstream series lives in the lab tree (work/upstream/swnode-uaf/patches/v1, base v7.4-rc4). Ryan sends it upstream. Once it lands upstream, the next rebase drops 1 and 2 as already applied.Assisted-by: LLM (Claude:Opus 5.5)