Skip to contentSkip to conversation

ieee802154: hwsim: Fix memory leak in hwsim_add_one

PATCH In review Dongliang Mu 3 participants 3 messages Jun 16, 2021 – Jun 22, 2021

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

PATCH v2 1/1 ieee802154: hwsim: Fix memory leak in hwsim_add_one

No matter from hwsim_remove or hwsim_del_radio_nl, hwsim_del fails to remove the entry in the edges list. Take the example below, phy0, phy1 and e0 will be deleted, resulting in e1 not freed and accessed in the future.

              hwsim_phys
                  |
    ------------------------------
    |                            |

phy0 (edges) phy1 (edges)
----> e1 (idx = 1) ----> e0 (idx = 0)

Fix this by deleting and freeing all the entries in the edges list between hwsim_edge_unsubscribe_me and list_del(&phy->list).

Reported-by [email protected]
Fixes 1c9f4a3fce77 ("ieee802154: hwsim: fix rcu handling")

---
v1->v2: add rcu_read_lock for the deletion operation according to Pavel Skripkin

 drivers/net/ieee802154/mac802154_hwsim.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/net/ieee802154/mac802154_hwsim.c +5 -0
index da9135231c07..cf659361a3fb 100644
@@ -824,12 +824,17 @@ static int hwsim_add_one(struct genl_info *info, struct device *dev,
 static void hwsim_del(struct hwsim_phy *phy)
 {
 	struct hwsim_pib *pib;
+	struct hwsim_edge *e;
 
 	hwsim_edge_unsubscribe_me(phy);
 
 	list_del(&phy->list);
 
 	rcu_read_lock();
+	list_for_each_entry_rcu(e, &phy->edges, list) {
+		list_del_rcu(&e->list);
+		hwsim_free_edge(e);
+	}
 	pib = rcu_dereference(phy->pib);
 	rcu_read_unlock();
 
Signature · 1 line
2.25.1
maintainer Acked-by on v2 1/1
#1 lore raw

Hi,

Quoted 13 lines · Dongliang Mu

No matter from hwsim_remove or hwsim_del_radio_nl, hwsim_del fails to remove the entry in the edges list. Take the example below, phy0, phy1 and e0 will be deleted, resulting in e1 not freed and accessed in the future.

              hwsim_phys
                  |
    ------------------------------
    |                            |

phy0 (edges) phy1 (edges)
----> e1 (idx = 1) ----> e0 (idx = 0)

Quoted 6 lines

Fix this by deleting and freeing all the entries in the edges list between hwsim_edge_unsubscribe_me and list_del(&phy->list).

Reported-by [email protected]
Fixes 1c9f4a3fce77 ("ieee802154: hwsim: fix rcu handling")

Thanks!

maintainer
#2 lore raw

Hello.

Quoted 19 lines · Alexander Aring

Hi,

Quoted 16 lines · Dongliang Mu

No matter from hwsim_remove or hwsim_del_radio_nl, hwsim_del fails to remove the entry in the edges list. Take the example below, phy0, phy1 and e0 will be deleted, resulting in e1 not freed and accessed in the future.

               hwsim_phys
                   |
     ------------------------------
     |                            |
phy0 (edges)                 phy1 (edges)
    ----> e1 (idx = 1)             ----> e0 (idx = 0)

Fix this by deleting and freeing all the entries in the edges list between hwsim_edge_unsubscribe_me and list_del(&phy->list).

Quoted 7 lines
Quoted 3 lines
Reported-by [email protected]
Fixes 1c9f4a3fce77 ("ieee802154: hwsim: fix rcu handling")

Thanks!

This patch has been applied to the wpan tree and will be part of the next pull request to net. Thanks!

regards
Stefan Schmidt