cfg80211: free the object allocated in wiphy_apply_custom_regulatory
Subsystems (3)
PATCH 1/1 cfg80211: free the object allocated in wiphy_apply_custom_regulatory
The commit beee24695157 ("cfg80211: Save the regulatory domain when setting custom regulatory") forgets to free the newly allocated regd object.
Fix this by freeing the regd object in the error handling code and deletion function - mac80211_hwsim_del_radio.
--- drivers/net/wireless/mac80211_hwsim.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/net/wireless/mac80211_hwsim.c +4 -0
@@ -3404,6 +3404,8 @@ static int mac80211_hwsim_new_radio(struct genl_info *info,debugfs_remove_recursive (data->debugfs);ieee80211_unregister_hw (data->hw); failed_hw: +if (param->regd) +kfree_rcu (get_wiphy_regdom (data->hw->wiphy));device_release_driver (data->dev); failed_bind:device_unregister (data->dev);
@@ -3454,6 +3456,8 @@ static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data, {hwsim_mcast_del_radio (data->idx, hwname, info);debugfs_remove_recursive (data->debugfs); +if (data->regd) +kfree_rcu (get_wiphy_regdom (data->hw->wiphy));ieee80211_unregister_hw (data->hw);device_release_driver (data->dev);device_unregister (data->dev);
Signature · 1 line
2.25.1
Quoted 20 lines · Dongliang Mu
The commit
beee24695157("cfg80211: Save the regulatory domain when setting custom regulatory") forgets to free the newly allocated regd object.Fix this by freeing the regd object in the error handling code and deletion function -
mac80211_hwsim_del_radio.Reported-by [email protected]Fixes beee24695157 ("cfg80211: Save the regulatory domain when setting custom regulatory")Signed-off-by Dongliang Mu <[email protected]>--- drivers/net/wireless/mac80211_hwsim.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/net/wireless/mac80211_hwsim.c +0
-0
drivers/net/wireless/mac80211_hwsim.c static int mac80211_hwsim_new_radio(struct genl_info *info, · line 3408
Quoted 5 lines
drivers/net/wireless/mac80211_hwsim.c +2
-0@@ -3404,6 +3404,8 @@ static int mac80211_hwsim_new_radio(struct genl_info *info,debugfs_remove_recursive (data->debugfs);ieee80211_unregister_hw (data->hw); failed_hw: +if (param->regd) +kfree_rcu (get_wiphy_regdom (data->hw->wiphy));
Here kfree_rcu supports only 1 parameter. But some example code uses the 2nd parameter: rcu_head or rcu. For example,
static void rcu_free_regdom(const struct ieee80211_regdomain *r)
{
......
kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
}If this patch has any issues, please let me know.
Quoted 15 lines
drivers/net/wireless/mac80211_hwsim.c +2
-1device_release_driver (data->dev); failed_bind:device_unregister (data->dev);@@ -3454,6 +3456,8 @@ static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data, {hwsim_mcast_del_radio (data->idx, hwname, info);debugfs_remove_recursive (data->debugfs); +if (data->regd) +kfree_rcu (get_wiphy_regdom (data->hw->wiphy));ieee80211_unregister_hw (data->hw);device_release_driver (data->dev);device_unregister (data->dev); --2.25.1
Johannes Berg
maintainerQuoted 3 lines · Dongliang Mu
The commit
beee24695157("cfg80211: Save the regulatory domain when setting custom regulatory") forgets to free the newly allocated regd object.
Not really? It's not forgetting it, it just saves it?
+ new_regd = reg_copy_regd(regd);
+ if (IS_ERR(new_regd))
+ return;
+
+ tmp = get_wiphy_regdom(wiphy); + rcu_assign_pointer(wiphy->regd, new_regd); + rcu_free_regdom(tmp);
Quoted 2 lines
Fix this by freeing the regd object in the error handling code and deletion function -
mac80211_hwsim_del_radio.
This can't be right - the same would affect all other users of that function, no?
Perhaps somewhere we have a case where wiphy->regd is leaked, but than that should be fixed more generally in cfg80211?
johannes
Quoted 7 lines · Johannes Berg
Quoted 3 lines · Dongliang Mu
The commit
beee24695157("cfg80211: Save the regulatory domain when setting custom regulatory") forgets to free the newly allocated regd object.Not really? It's not forgetting it, it just saves it?
Yes, it saves the regd object in the function wiphy_apply_custom_regulatory.
But its parent function - mac80211_hwsim_new_radio forgets to free this object when the ieee80211_register_hw fails.
Quoted 9 lines
(quoted diff) +7
-0+ new_regd = reg_copy_regd(regd); + if (IS_ERR(new_regd)) + return; + + tmp = get_wiphy_regdom(wiphy); + rcu_assign_pointer(wiphy->regd, new_regd); + rcu_free_regdom(tmp);
Quoted 5 lines
Quoted 2 lines
Fix this by freeing the regd object in the error handling code and deletion function -
mac80211_hwsim_del_radio.This can't be right - the same would affect all other users of that function, no?
The problem occurs in the error handling code of mac80211_hwsim_new_radio, not wiphy_apply_custom_regulatory. My commit message may be not very clear.
So I think the code in the mac80211_hwsim_del_radio paired with mac80211_hwsim_new_radio should be changed correspondingly. If I miss any problems, please let me know.
I have successfully tested my patch in the syzbot dashboard [1].
[1] https://syzkaller.appspot.com/bug?extid=1638e7c770eef6b6c0d0
Quoted 6 lines
Perhaps somewhere we have a case where
wiphy->regdis leaked, but than that should be fixed more generally in cfg80211?johannes
Johannes Berg
maintainerQuoted 10 lines · Dongliang Mu
Quoted 7 lines · Johannes Berg
Quoted 3 lines · Dongliang Mu
The commit
beee24695157("cfg80211: Save the regulatory domain when setting custom regulatory") forgets to free the newly allocated regd object.Not really? It's not forgetting it, it just saves it?
Yes, it saves the regd object in the function
wiphy_apply_custom_regulatory.
Right.
Quoted 2 lines
But its parent function -
mac80211_hwsim_new_radioforgets to free this object when theieee80211_register_hwfails.
But why is this specific to mac80211-hwsim?
Any other code calling wiphy_apply_custom_regulatory() and then failing the subsequent wiphy_register() or otherwise calling wiphy_free() will run into the same situation.
So why wouldn't we free this in wiphy_free(), if it exists?
johannes
Quoted 18 lines · Johannes Berg
Quoted 10 lines · Dongliang Mu
Quoted 7 lines · Johannes Berg
Quoted 3 lines · Dongliang Mu
The commit
beee24695157("cfg80211: Save the regulatory domain when setting custom regulatory") forgets to free the newly allocated regd object.Not really? It's not forgetting it, it just saves it?
Yes, it saves the regd object in the function
wiphy_apply_custom_regulatory.Right.
Quoted 2 lines
But its parent function -
mac80211_hwsim_new_radioforgets to free this object when theieee80211_register_hwfails.
Quoted 8 lines
But why is this specific to mac80211-hwsim?
Any other code calling
wiphy_apply_custom_regulatory()and then failing the subsequentwiphy_register()or otherwise callingwiphy_free()will run into the same situation.So why wouldn't we free this in
wiphy_free(), if it exists?
Hi Johannes,
if zhao in the thread is right, we don't need to add this free operation to wiphy_free().
What we should do is to only handle regd in the error handling code of mac80211_hwsim_new_radio. This will not affect other users of mac80211-hwsim. Any idea?
Quoted 2 lines
johannes
Johannes Berg
maintainerHi,
Quoted 2 lines · Dongliang Mu
if zhao in the thread is right, we don't need to add this free operation to
wiphy_free().
Actually, no, that statement is not true.
All that zhao claimed was that the free happens correctly during unregister (or later), and that is indeed true, since it happens from
ieee80211_unregister_hw()
-> wiphy_unregister()
-> wiphy_regulatory_deregister()
However, syzbot of course is also correct. Abstracting a bit and ignoring mac80211, the problem is that here we assign it before wiphy_register(), then wiphy_register() doesn't get called or fails, and therefore we don't call wiphy_unregister(), only wiphy_free().
Hence the leak.
But you can also easily see from that description that it's not related to hwsim - we should add a secondary round of cleanups in wiphy_free() or even move the call to wiphy_regulatory_deregister() into wiphy_free(), we need to look what else this does to see if we can move it or not.
johannes
Quoted 7 lines · Johannes Berg
Hi,
Quoted 2 lines · Dongliang Mu
if zhao in the thread is right, we don't need to add this free operation to
wiphy_free().
Quoted 9 lines
Actually, no, that statement is not true.
All that zhao claimed was that the free happens correctly during unregister (or later), and that is indeed true, since it happens from
ieee80211_unregister_hw()
->wiphy_unregister()
->wiphy_regulatory_deregister()
Thanks for your explanation. Now the situation is more clear.
Quoted 5 lines
However, syzbot of course is also correct. Abstracting a bit and ignoring mac80211, the problem is that here we assign it before
wiphy_register(), thenwiphy_register()doesn't get called or fails, and therefore we don't callwiphy_unregister(), onlywiphy_free().
Yes, you're right. In this case, wiphy_register is not called. We should not call wiphy_unregister() to clean up anything.
Quoted 8 lines
Hence the leak.
But you can also easily see from that description that it's not related to hwsim - we should add a secondary round of cleanups in
wiphy_free()or even move the call towiphy_regulatory_deregister()intowiphy_free(), we need to look what else this does to see if we can move it or not.
I agree to move the cleanup operation of regd to wiphy_free API. That's the partial functionability of this function.
Quoted 3 lines
johannes
Quoted 19 lines · Dongliang Mu
The commit
beee24695157("cfg80211: Save the regulatory domain when setting custom regulatory") forgets to free the newly allocated regd object.Fix this by freeing the regd object in the error handling code and deletion function -
mac80211_hwsim_del_radio.Reported-by [email protected]Fixes beee24695157 ("cfg80211: Save the regulatory domain when setting custom regulatory")Signed-off-by Dongliang Mu <[email protected]>--- drivers/net/wireless/mac80211_hwsim.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/net/wireless/mac80211_hwsim.c +0
-0
drivers/net/wireless/mac80211_hwsim.c static int mac80211_hwsim_new_radio(struct genl_info *info, · line 3409
Quoted 6 lines
drivers/net/wireless/mac80211_hwsim.c +2
-0@@ -3404,6 +3404,8 @@ static int mac80211_hwsim_new_radio(struct genl_info *info,debugfs_remove_recursive (data->debugfs);ieee80211_unregister_hw (data->hw); failed_hw: +if (param->regd) +kfree_rcu (get_wiphy_regdom (data->hw->wiphy));device_release_driver (data->dev);
hw->wiphy->regd may be NULL if previous reg_copy_regd failed, so how about:
if (hw->wiphy->regd)
rcu_free_regdom(get_wiphy_regdom(hw->wiphy))
drivers/net/wireless/mac80211_hwsim.c static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data, · line 3460
Quoted 8 lines
drivers/net/wireless/mac80211_hwsim.c +2
-0failed_bind:device_unregister (data->dev);@@ -3454,6 +3456,8 @@ static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data, {hwsim_mcast_del_radio (data->idx, hwname, info);debugfs_remove_recursive (data->debugfs); +if (data->regd) +kfree_rcu (get_wiphy_regdom (data->hw->wiphy));
this is not correct, because ieee80211_unregister_hw below will free data->hw_wiphy->regd
Quoted 4 lines
drivers/net/wireless/mac80211_hwsim.c +0
-0ieee80211_unregister_hw (data->hw);device_release_driver (data->dev);device_unregister (data->dev);
Quoted 32 lines · xiaoqiang zhao
Quoted 19 lines · Dongliang Mu
The commit
beee24695157("cfg80211: Save the regulatory domain when setting custom regulatory") forgets to free the newly allocated regd object.Fix this by freeing the regd object in the error handling code and deletion function -
mac80211_hwsim_del_radio.Reported-by [email protected]Fixes beee24695157 ("cfg80211: Save the regulatory domain when setting custom regulatory")Signed-off-by Dongliang Mu <[email protected]>--- drivers/net/wireless/mac80211_hwsim.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/net/wireless/mac80211_hwsim.c +0
-0drivers/net/wireless/mac80211_hwsim.c static int mac80211_hwsim_new_radio(struct genl_info *info, · line 3409
Quoted 6 lines
drivers/net/wireless/mac80211_hwsim.c +2
-0@@ -3404,6 +3404,8 @@ static int mac80211_hwsim_new_radio(struct genl_info *info,debugfs_remove_recursive (data->debugfs);ieee80211_unregister_hw (data->hw); failed_hw: +if (param->regd) +kfree_rcu (get_wiphy_regdom (data->hw->wiphy));device_release_driver (data->dev);
hw->wiphy->regdmay be NULL if previousreg_copy_regdfailed, so how about:
if (hw->wiphy->regd)
Quoted 1 line
rcu_free_regdom(get_wiphy_regdom(hw->wiphy))
Previously I would like to use this API(rcu_free_regdom), but it is static and located in non-global header file - reg.h.
Quoted 11 lines
drivers/net/wireless/mac80211_hwsim.c static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data, · line 3460
Quoted 9 lines
drivers/net/wireless/mac80211_hwsim.c +2
-0failed_bind:device_unregister (data->dev);@@ -3454,6 +3456,8 @@ static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data, {hwsim_mcast_del_radio (data->idx, hwname, info);debugfs_remove_recursive (data->debugfs); +if (data->regd) +kfree_rcu (get_wiphy_regdom (data->hw->wiphy));this is not correct, because
ieee80211_unregister_hwbelow will freedata->hw_wiphy->regd
Can you point out the concrete code releasing regd? Maybe the link to elixir.
Quoted 4 lines
Quoted 4 lines
drivers/net/wireless/mac80211_hwsim.c +0
-0ieee80211_unregister_hw (data->hw);device_release_driver (data->dev);device_unregister (data->dev);
Quoted 6 lines · Dongliang Mu
Can you point out the concrete code releasing regd? Maybe the link to elixir.
Quoted 4 lines
Quoted 4 lines
ieee80211_unregister_hw(data->hw); device_release_driver(data->dev); device_unregister(data->dev);
call graph seems like this:
ieee80211_unregister_hw
(https://elixir.bootlin.com/linux/v5.14-rc2/source/net/mac80211/main.c#L1368)
wiphy_unregister
(https://elixir.bootlin.com/linux/v5.14-rc2/source/net/wireless/core.c#L1011)
wiphy_regulatory_deregister
(https://elixir.bootlin.com/linux/v5.14-rc2/source/net/wireless/reg.c#L4057)
rcu_free_regdom
Quoted 13 lines · xiaoqiang zhao
Quoted 6 lines · Dongliang Mu
Can you point out the concrete code releasing regd? Maybe the link to elixir.
Quoted 4 lines
Quoted 4 lines
ieee80211_unregister_hw(data->hw); device_release_driver(data->dev); device_unregister(data->dev);call graph seems like this:
Quoted 7 lines
ieee80211_unregister_hw
(https://elixir.bootlin.com/linux/v5.14-rc2/source/net/mac80211/main.c#L1368)
wiphy_unregister
(https://elixir.bootlin.com/linux/v5.14-rc2/source/net/wireless/core.c#L1011)
wiphy_regulatory_deregister
(https://elixir.bootlin.com/linux/v5.14-rc2/source/net/wireless/reg.c#L4057)
rcu_free_regdom
Thanks very much. This is really helpful.