Linux IEEE 802.15.4 and 6LoWPAN development
 help / color / mirror / Atom feed
* [PATCH net] ieee802154: hwsim: free PIB after hardware unregister
@ 2026-11-13 13:41 Yichong Chen
  2026-11-17 10:17 ` Miquel Raynal
  2026-11-17 13:55 ` netdev-bot+sashiko
  0 siblings, 2 replies; 3+ messages in thread
From: Yichong Chen @ 2026-11-13 13:41 UTC (permalink / raw)
  To: Alexander Aring, Stefan Schmidt, Miquel Raynal, linux-wpan
  Cc: netdev, linux-kernel, Zhan Jun, syzbot+8a8482d4c33cc58babb4,
	stable, Yichong Chen

From: Zhan Jun <zhanjun@uniontech.com>

hwsim_del() queued the current PIB for RCU freeing before calling
ieee802154_unregister_hw(). Unregistering the hardware closes its
interfaces and aborts an in-progress scan; scan cleanup calls
set_channel(), which replaces the same phy->pib and queues the old object
for RCU freeing again. KASAN then reports an invalid free in
hwsim_update_pib().

Unregister the hardware first, then atomically detach the final PIB under
pib_lock and queue only that current object for RCU freeing. This lets all
driver callbacks finish before the PIB is reclaimed and avoids freeing a
PIB that was already replaced during teardown.

Reported-by: syzbot+8a8482d4c33cc58babb4@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/6af5f575.b350cda4.2b6a63.0000.GAE@google.com/T/
Fixes: f25da51fdc38 ("ieee802154: hwsim: add replacement for fakelb")
Cc: stable@vger.kernel.org
Signed-off-by: Zhan Jun <zhanjun@uniontech.com>
Signed-off-by: Yichong Chen <chenyichong@uniontech.com>
---
 drivers/net/ieee802154/mac802154_hwsim.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ieee802154/mac802154_hwsim.c b/drivers/net/ieee802154/mac802154_hwsim.c
index a9bd1555d2dc..a65ae2afb789 100644
--- a/drivers/net/ieee802154/mac802154_hwsim.c
+++ b/drivers/net/ieee802154/mac802154_hwsim.c
@@ -1008,12 +1008,16 @@ static void hwsim_del(struct hwsim_phy *phy)
 		list_del_rcu(&e->list);
 		hwsim_free_edge(e);
 	}
-	pib = rcu_dereference(phy->pib);
 	rcu_read_unlock();
 
+	ieee802154_unregister_hw(phy->hw);
+
+	spin_lock_bh(&phy->pib_lock);
+	pib = rcu_replace_pointer(phy->pib, NULL,
+				  lockdep_is_held(&phy->pib_lock));
+	spin_unlock_bh(&phy->pib_lock);
 	kfree_rcu(pib, rcu);
 
-	ieee802154_unregister_hw(phy->hw);
 	ieee802154_free_hw(phy->hw);
 }
 
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net] ieee802154: hwsim: free PIB after hardware unregister
  2026-11-13 13:41 [PATCH net] ieee802154: hwsim: free PIB after hardware unregister Yichong Chen
@ 2026-11-17 10:17 ` Miquel Raynal
  2026-11-17 13:55 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: Miquel Raynal @ 2026-11-17 10:17 UTC (permalink / raw)
  To: Yichong Chen
  Cc: Alexander Aring, Stefan Schmidt, linux-wpan, netdev, linux-kernel,
	Zhan Jun, syzbot+8a8482d4c33cc58babb4, stable

On 13/11/2026 at 21:41:41 +08, Yichong Chen <chenyichong@uniontech.com> wrote:

> From: Zhan Jun <zhanjun@uniontech.com>
>
> hwsim_del() queued the current PIB for RCU freeing before calling
> ieee802154_unregister_hw(). Unregistering the hardware closes its
> interfaces and aborts an in-progress scan; scan cleanup calls
> set_channel(), which replaces the same phy->pib and queues the old object
> for RCU freeing again. KASAN then reports an invalid free in
> hwsim_update_pib().
>
> Unregister the hardware first, then atomically detach the final PIB under
> pib_lock and queue only that current object for RCU freeing. This lets all
> driver callbacks finish before the PIB is reclaimed and avoids freeing a
> PIB that was already replaced during teardown.
>
> Reported-by: syzbot+8a8482d4c33cc58babb4@syzkaller.appspotmail.com
> Closes: https://lore.kernel.org/all/6af5f575.b350cda4.2b6a63.0000.GAE@google.com/T/
> Fixes: f25da51fdc38 ("ieee802154: hwsim: add replacement for fakelb")
> Cc: stable@vger.kernel.org
> Signed-off-by: Zhan Jun <zhanjun@uniontech.com>
> Signed-off-by: Yichong Chen <chenyichong@uniontech.com>

This is a duplicate of:
https://lore.kernel.org/all/20260627235805.17310-1-alhouseenyousef@gmail.com/
https://lore.kernel.org/all/20260709221858.158063-1-devnexen@gmail.com/
https://lore.kernel.org/all/20260919223434.3882824-1-benquike@gmail.com/

Miquèl

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net] ieee802154: hwsim: free PIB after hardware unregister
  2026-11-13 13:41 [PATCH net] ieee802154: hwsim: free PIB after hardware unregister Yichong Chen
  2026-11-17 10:17 ` Miquel Raynal
@ 2026-11-17 13:55 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-17 13:55 UTC (permalink / raw)
  To: chenyichong
  Cc: alex.aring, stefan, miquel.raynal, linux-wpan, netdev,
	linux-kernel, zhanjun, syzbot+8a8482d4c33cc58babb4, stable, kuba

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The patch fixes the stale-PIB double free in hwsim_del(), but
  the error unwind in hwsim_add_one()…

--

ieee802154: hwsim: free PIB after hardware unregister

This patch reorders hwsim_del() so that ieee802154_unregister_hw() runs
before the PIB is freed. The current phy->pib is then detached under
pib_lock with rcu_replace_pointer(), and only that object is passed to
kfree_rcu(). This avoids a double free when a scan abort replaces the PIB
during teardown.

Closes: https://lore.kernel.org/all/6af5f575.b350cda4.2b6a63.0000.GAE@google.com/T/

> diff --git a/drivers/net/ieee802154/mac802154_hwsim.c b/drivers/net/ieee802154/mac802154_hwsim.c
> index a9bd1555d2dc7..a65ae2afb789b 100644
> --- a/drivers/net/ieee802154/mac802154_hwsim.c
> +++ b/drivers/net/ieee802154/mac802154_hwsim.c
> @@ -1008,12 +1008,16 @@ static void hwsim_del(struct hwsim_phy *phy)
>  		list_del_rcu(&e->list);
>  		hwsim_free_edge(e);
>  	}
> -	pib = rcu_dereference(phy->pib);
>  	rcu_read_unlock();
>  
> +	ieee802154_unregister_hw(phy->hw);
> +
> +	spin_lock_bh(&phy->pib_lock);
> +	pib = rcu_replace_pointer(phy->pib, NULL,
> +				  lockdep_is_held(&phy->pib_lock));
> +	spin_unlock_bh(&phy->pib_lock);
>  	kfree_rcu(pib, rcu);

[Severity: High]
This fixes hwsim_del(). Does the error unwind in hwsim_add_one() still
have the same stale PIB problem?

hwsim_add_one() keeps the original PIB in a local variable and frees that
pointer directly on failure:

drivers/net/ieee802154/mac802154_hwsim.c:hwsim_add_one() {
    ...
	rcu_assign_pointer(phy->pib, pib);
    ...
	err = ieee802154_register_hw(hw);
	if (err)
		goto err_reg;
    ...
err_subscribe:
	ieee802154_unregister_hw(phy->hw);
err_reg:
	kfree(pib);
err_pib:
	ieee802154_free_hw(phy->hw);
}

After wpan_phy_register() runs inside ieee802154_register_hw(), driver
callbacks can replace phy->pib through hwsim_update_pib(). That function
calls kfree_rcu(pib_old) on the original object. Some of the paths that
get there:

  NL802154_CMD_SET_CHANNEL -> ieee802154_set_channel() -> drv_set_channel()
    -> hwsim_hw_channel() -> hwsim_update_pib()

  interface open -> drv_start() -> set_promiscuous_mode / set_hw_addr_filt

  scan abort inside ieee802154_unregister_hw(), which is the path the
  commit message describes

On the err_subscribe path, ieee802154_unregister_hw() runs first and then
falls through to kfree(pib). This happens when hwsim_probe() calls with
init set and hwsim_subscribe_all_others() fails with -ENOMEM.

The HWSIM_CMD_NEW_RADIO path can also reach err_reg, when
ieee802154_if_add() fails after wpan_phy_register() has succeeded.
wpan_phy_register() drops rtnl before ieee802154_register_hw() takes it
again. A SET_CHANNEL request can run in that window and replace the PIB:

net/mac802154/main.c:ieee802154_register_hw() {
    ...
	rc = wpan_phy_register(local->phy);
	if (rc < 0)
		goto out_mac_wq;

	rtnl_lock();

	dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
				NL802154_IFTYPE_NODE,
				cpu_to_le64(0x0000000000000000ULL));
	if (IS_ERR(dev)) {
		rtnl_unlock();
		rc = PTR_ERR(dev);
		goto out_phy;
	}
    ...
}

In both cases, wouldn't kfree(pib) free an object that is already queued
for kfree_rcu()? Wouldn't it also leak the PIB that is currently
installed in phy->pib?

The commit message says the change "avoids freeing a PIB that was already
replaced during teardown". Should the hwsim_add_one() unwind follow the
same sequence that hwsim_del() now uses? That would mean detaching
phy->pib under pib_lock with rcu_replace_pointer() after unregistering
the hardware, and passing the result to kfree_rcu(), rather than calling
kfree() on the saved local pointer.

>  
> -	ieee802154_unregister_hw(phy->hw);
>  	ieee802154_free_hw(phy->hw);
>  }

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/0D7C088B45E7283A%2B20261113134141.2626113-1-chenyichong%40uniontech.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-17 13:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-13 13:41 [PATCH net] ieee802154: hwsim: free PIB after hardware unregister Yichong Chen
2026-11-17 10:17 ` Miquel Raynal
2026-11-17 13:55 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox