Skip to content

software node: fix the use-after-free when a shared managed node is removed (backport + fix + KUnit) - #2914

Merged
RyanTheTide merged 3 commits into
integration/aurorafrom
fix/swnode-owner-release-backport
Nov 23, 2026
Merged

RyanTheTide merged 3 commits into
integration/aurorafrom
fix/swnode-owner-release-backport

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

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 whose gdev->dev inherits 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:

  • sharer removed first: our tree drops the owner's managed reference for every device that carries the node (double put, refcount underflow). Upstream fixed this in v7.4-rc3 with f8623ce59649 ("software node: only let the owner release a managed node", Cc stable); our tip does not have it.
  • owner removed first: still broken upstream (v7.4-rc4). The sharer's put is the last one and frees the node, then the function reads swnode->owner from the freed node.

Change

  1. Cherry-pick of upstream f8623ce59649 (author Bartosz Golaszewski), unchanged.
  2. software node: check the owner before dropping the device's reference. Same change as the upstream patch prepared for submission (not sent yet).
  3. New KUnit suite 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), suites software-node-*:

  • tip without this PR: 6/8, both new cases report KASAN UAFs (plus a refcount underflow)
  • tip + cherry-pick only: 7/8, owner-first UAF
  • this PR: 8/8, no KASAN report
  • Linux v7.4-rc4 + test only: 7/8 (the owner-first bug is live upstream); v7.4-rc4 + fix + test: 8/8

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
  • The fix moves the swnode->owner == dev check, and the managed put that depends on it, before the device's own swnode_put(). The owner still holds both of its references at that point. A non-owner never touches the node after its put.
  • Fixes tag on 2: 151f6ff. The read-after-put existed with the old managed bit too.
  • Not changed: after the owner goes first, f8623ce leaves swnode->owner pointing at the freed owner. It is only compared, never dereferenced. Johan dropped the clearing during upstream review.
  • Logs (7 rows) are in the lab trace dir 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)

brgl and others added 3 commits November 23, 2026 13:36
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]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

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 (cherry picked from commit ...). Patch 2 is correct and should go upstream as it is (Fixes 151f6ff, Cc stable).

  1. drivers/base/swnode.c:1214-1219: the refcounts work in both orders. Start: 3 refs (creation ref, the owner's notify ref, the sharer's notify ref). Owner first: the owner branch drops the creation ref, the owner's own put leaves 1, and the sharer's put frees the node as the very last statement. Sharer first: one put, 3 -> 2, and the node is not read after it. software_node_release() does not touch any device, so nothing else depends on this order.
  2. Optional, swnode.c:1214: once the owner has gone first, swnode->owner still points at the freed device. It is only ever compared, but a new device allocated at the same address and given this fwnode would pass as the owner and drop the creation ref a second time. With the new order, swnode->owner = NULL; in the owner branch, before the puts, is safe and removes that case. It is worth raising when this goes upstream, even though Johan dropped the clearing from f8623ce.
  3. drivers/base/test/swnode-managed-test.c: both cases only fail with KASAN on. Without it the owner-first case reads freed memory without any report, and the sharer-first double put shows up only as a refcount_t WARN, which does not fail the test. drivers/base/test/.kunitconfig turns on neither DRIVER_SWNODE_KUNIT_TEST nor KASAN, so kunit.py run --kunitconfig drivers/base/test does not run the suite at all. Please add both, or say in the file header that the suite needs a KASAN kunitconfig to be useful.
  4. Nit, swnode-managed-test.c:72-76: if the sharer add or the is_software_node() assert aborts, the owner is never unregistered and leaks. kunit_add_action() for device_unregister would cover it.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() and software_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 == sharer is false → single swnode_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 == owner is true → set_secondary_fwnode(owner, NULL) + managed swnode_put() (3 → 2), followed by owner's notify swnode_put() (2 → 1). When sharer is later removed, swnode->owner == sharer is evaluated before the final swnode_put() (1 → 0), eliminating the slab-use-after-free.
  • §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's Signed-off-by:; commits 2–3 carry human author/committer (Ryan Murray <[email protected]>) and human Signed-off-by: only (zero AI trailers in commits); PR body includes Assisted-by:.
  • §5 (Evidence): Waiting on CI — driver-core change with thorough arm64 QEMU KASAN KUnit evidence (6/8 → 7/8 → 8/8 on this tree, plus upstream v7.4-rc4 repro 7/8 → 8/8). GitHub CI currently stands at 2/7 green (host-tests, source) and 5/7 QUEUED (rust-build, kunit, DWC3/ATC ×3). Cannot approve until CI finishes green.
  • §6 (Naming): Pass.

Non-blocking Notes

  • Stale swnode->owner pointer reuse (worth raising upstream): When the owner is unregistered first, swnode->owner retains the freed struct device * address. If slab reuses that address for a newly allocated sharer of the still-live swnode, swnode->owner == dev would falsely match and double-put. With the new ordering, clearing swnode->owner = NULL; inside the if (swnode->owner == dev) block before the puts is safe and closes that ABA window.
  • swnode-managed-test.c:162-164: If KUNIT_ASSERT_TRUE(test, is_software_node(fwnode)) or swnode_managed_dev_add(sharer) aborts, *owner leaks without device_unregister(); registering a kunit_add_action() cleanup would cover abort paths. Also note drivers/base/test/.kunitconfig does not enable CONFIG_DRIVER_SWNODE_KUNIT_TEST or CONFIG_KASAN, though the test file header clearly documents the KASAN requirement.

Assisted-by: LLM (Gemini 4 Neon)

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Merge-train review at 47e2324 (non-author pass): no blockers.

Checked the refcount walk in software_node_notify_remove() for both removal orders. The managed node holds one reference from creation, plus one per device that carries the fwnode. Owner removed first: the owner's two puts leave the sharer's reference, and the sharer's put frees the node. Sharer removed first: the sharer drops only its own reference, then the owner's two puts free the node. swnode->owner is read before any put, so the node can't be freed under the check (that was the UAF in the old managed flag ordering). No gates, no params. The tests cover both orders.

Nit (optional): after the owner goes, swnode->owner points at a freed device. It is only ever compared and never dereferenced. A device still holding the node was alive before the owner was freed, so it can't share that address. Clearing it to NULL in the owner branch would make that obvious, but it isn't needed.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

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 swnode->owner == dev check and drop the creation reference a second time. My "can't share that address" only holds for devices that already existed. The owner field and the comparison come from the upstream patch (f8623ce59649) unchanged, so this isn't a merge blocker for the backport. Clearing swnode->owner = NULL in the owner branch, before the puts, is still the right follow-up, and it's worth raising upstream with patch 2.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide
RyanTheTide merged commit 2c52cad into integration/aurora Nov 23, 2026
7 checks passed
@RyanTheTide
RyanTheTide deleted the fix/swnode-owner-release-backport branch November 27, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants