Skip to contentSkip to conversation

ieee802154: hwsim: serialize pib updates to fix double-free

PATCH Applied David Carlier 4 participants 4 messages Jul 9 – Sep 2

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ieee802154: hwsim: serialize pib updates to fix double-free

hwsim_update_pib() does an unserialized read-swap-free of phy->pib:

	pib_old = rtnl_dereference(phy->pib);
	...
	rcu_assign_pointer(phy->pib, pib);
	kfree_rcu(pib_old, rcu);

It assumes the RTNL is held, but ->set_channel is not always called under it: the mac802154 scan worker changes channels via drv_set_channel() without the RTNL. Such an update can race an RTNL-held one on the same phy; both read the same pib_old and both kfree_rcu() it, double-freeing the object. With SLUB percpu sheaves batching kfree_rcu(), this surfaces as a KASAN invalid-free in rcu_free_sheaf().

struct hwsim_phy has no lock for pib. Add one and make the swap atomic with rcu_replace_pointer() under it, dropping the misleading rtnl_dereference().

Reported-by [email protected]
Fixes f25da51fdc38 ("ieee802154: hwsim: add replacement for fakelb")
---
 drivers/net/ieee802154/mac802154_hwsim.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/net/ieee802154/mac802154_hwsim.c +7 -3
index 6daa0f198b9f..a9bd1555d2dc 100644
@@ -72,6 +72,8 @@ struct hwsim_phy {
 	struct ieee802154_hw *hw;
 	u32 idx;
 
+	/* Serializes phy->pib_updates. */
+	spinlock_t pib_lock;
 	struct hwsim_pib __rcu *pib;
 
 	bool suspended;
@@ -102,8 +104,6 @@ static int hwsim_update_pib(struct ieee802154_hw *hw, u8 page, u8 channel,
 	if (!pib)
 		return -ENOMEM;
 
-	pib_old = rtnl_dereference(phy->pib);
-
 	pib->page = page;
 	pib->channel = channel;
 	pib->filt.short_addr = filt->short_addr;
@@ -112,7 +112,10 @@ static int hwsim_update_pib(struct ieee802154_hw *hw, u8 page, u8 channel,
 	pib->filt.pan_coord = filt->pan_coord;
 	pib->filt_level = filt_level;
 
-	rcu_assign_pointer(phy->pib, pib);
+	spin_lock_bh(&phy->pib_lock);
+	pib_old = rcu_replace_pointer(phy->pib, pib,
+				      lockdep_is_held(&phy->pib_lock));
+	spin_unlock_bh(&phy->pib_lock);
 	kfree_rcu(pib_old, rcu);
 	return 0;
 }
@@ -952,6 +955,7 @@ static int hwsim_add_one(struct genl_info *info, struct device *dev,
 		goto err_pib;
 	}
 
+	spin_lock_init(&phy->pib_lock);
 	pib->channel = 13;
 	pib->filt.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
 	pib->filt.pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
Signature · 1 line
2.53.0
maintainer
#1 lore raw

Hello David,

Quoted 19 lines · David Carlier

hwsim_update_pib() does an unserialized read-swap-free of phy->pib:

	pib_old = rtnl_dereference(phy->pib);
	...
	rcu_assign_pointer(phy->pib, pib);
	kfree_rcu(pib_old, rcu);

It assumes the RTNL is held, but ->set_channel is not always called under it: the mac802154 scan worker changes channels via drv_set_channel() without the RTNL. Such an update can race an RTNL-held one on the same phy; both read the same pib_old and both kfree_rcu() it, double-freeing the object. With SLUB percpu sheaves batching kfree_rcu(), this surfaces as a KASAN invalid-free in rcu_free_sheaf().

struct hwsim_phy has no lock for pib. Add one and make the swap atomic with rcu_replace_pointer() under it, dropping the misleading rtnl_dereference().

Quoted 5 lines
Reported-by [email protected]
Fixes f25da51fdc38 ("ieee802154: hwsim: add replacement for fakelb")

Thank you for the patch, but I think Yousef already provided a similar patch:

https://lore.kernel.org/all/[email protected]/

Yousef, can you confirm you will send v2 soon?

Thanks,
Miquèl

#2 lore raw

Yes. I'll send v2 of my teardown patch with rcu_access_pointer(). It is separate from David's patch: mine moves the final PIB free after unregister, while David's serializes concurrent runtime PIB replacements. I don't think his patch is superseded by mine.

Thanks,
Yousef

Quoted 38 lines · Miquel Raynal

Hello David,

Quoted 19 lines · David Carlier

hwsim_update_pib() does an unserialized read-swap-free of phy->pib:

pib_old = rtnl_dereference(phy->pib);
...
rcu_assign_pointer(phy->pib, pib);
kfree_rcu(pib_old, rcu);

It assumes the RTNL is held, but ->set_channel is not always called under it: the mac802154 scan worker changes channels via drv_set_channel() without the RTNL. Such an update can race an RTNL-held one on the same phy; both read the same pib_old and both kfree_rcu() it, double-freeing the object. With SLUB percpu sheaves batching kfree_rcu(), this surfaces as a KASAN invalid-free in rcu_free_sheaf().

struct hwsim_phy has no lock for pib. Add one and make the swap atomic with rcu_replace_pointer() under it, dropping the misleading rtnl_dereference().

Quoted 5 lines
Reported-by [email protected]
Fixes f25da51fdc38 ("ieee802154: hwsim: add replacement for fakelb")

Thank you for the patch, but I think Yousef already provided a similar patch:

https://lore.kernel.org/all/[email protected]/

Yousef, can you confirm you will send v2 soon?

Thanks,
Miquèl

maintainer Applied
#3 lore raw
In reply to David Carlier

Hello David Carlier.

Quoted 8 lines · David Carlier

hwsim_update_pib() does an unserialized read-swap-free of phy->pib:

	pib_old = rtnl_dereference(phy->pib);
	...
	rcu_assign_pointer(phy->pib, pib);
	kfree_rcu(pib_old, rcu);

[...]

Applied to wpan/wpan.git, thanks!

[1/1] ieee802154: hwsim: serialize pib updates to fix double-free

      https://git.kernel.org/wpan/wpan/c/979d5b8de8ed

regards,
Stefan Schmidt