Skip to contentSkip to conversation

mac80211_hwsim: fix divide error in mac80211_hwsim_link_info_changed

PATCH In review Deepakkumar Karn 2 participants 7 messages Dec 5, 2025 – Dec 10, 2025

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

PATCH 1/1 mac80211_hwsim: fix divide error in mac80211_hwsim_link_info_changed

Prevent a division by zero crash when userspace provides a zero beacon interval value. The crash occurs in the beacon timer setup code path when info->beacon_int is 0, causing bcn_int to be 0 and triggering a divide error in the do_div() macro.The issue can be triggered from userspace via nl80211/cfg80211 when configuring a virtual interface in AP mode with an invalid beacon interval.

Fixes e57f8a489c29 ("wifi: mac80211_hwsim: send a beacon per link")
Reported-by [email protected]
---
 drivers/net/wireless/virtual/mac80211_hwsim.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/net/wireless/virtual/mac80211_hwsim.c +7 -4
index 5903d82e1ab1..e67e2c989ea6 100644
@@ -2595,11 +2595,14 @@ static void mac80211_hwsim_link_info_changed(struct ieee80211_hw *hw,
 			link_data->beacon_int = info->beacon_int * 1024;
 			tsf = mac80211_hwsim_get_tsf(hw, vif);
 			bcn_int = link_data->beacon_int;
-			until_tbtt = bcn_int - do_div(tsf, bcn_int);
+			/* Proceed only when bcn_int != 0 */
+			if (bcn_int) {
+				until_tbtt = bcn_int - do_div(tsf, bcn_int);
 
-			hrtimer_start(&link_data->beacon_timer,
-				      ns_to_ktime(until_tbtt * NSEC_PER_USEC),
-				      HRTIMER_MODE_REL_SOFT);
+				hrtimer_start(&link_data->beacon_timer,
+					      ns_to_ktime(until_tbtt * NSEC_PER_USEC),
+					      HRTIMER_MODE_REL_SOFT);
+			}
 		} else if (!info->enable_beacon) {
 			unsigned int count = 0;
 			ieee80211_iterate_active_interfaces_atomic(
Signature · 1 line
2.51.1
maintainer
#1 lore raw
Quoted 6 lines · Deepakkumar Karn

Prevent a division by zero crash when userspace provides a zero beacon interval value. The crash occurs in the beacon timer setup code path when info->beacon_int is 0, causing bcn_int to be 0 and triggering a divide error in the do_div() macro.The issue can be triggered from userspace via nl80211/cfg80211 when configuring a virtual interface in AP mode with an invalid beacon interval.

Seems like we should not let userspace do that, to protect all other drivers too, not just hwsim.

johannes

#2 lore raw
Quoted 2 lines · Johannes Berg

Seems like we should not let userspace do that, to protect all other drivers too, not just hwsim.

As suggested, we should provide a zero-value division check for other drivers as well. I will investigate other places where divide errors can occur due to edge cases.

Please let me know if you meant something different. In the meantime, I will analyze other drivers for similar cases.

Thanks,
Deepakkumar Karn

maintainer
#3 lore raw
Quoted 7 lines · Deepakkumar Karn
Quoted 2 lines · Johannes Berg

Seems like we should not let userspace do that, to protect all other drivers too, not just hwsim.

As suggested, we should provide a zero-value division check for other drivers as well. I will investigate other places where divide errors can occur due to edge cases.

What, no no.

Quoted 2 lines

Please let me know if you meant something different. In the meantime, I will analyze other drivers for similar cases.

I did. My point is we shouldn't _have_ to check any drivers for this at all, it's nonsense and higher layers (here cfg80211) should reject it.

johannes

#4 lore raw
Quoted 9 lines · Deepakkumar Karn
Quoted 7 lines · Johannes Berg
Quoted 2 lines

Seems like we should not let userspace do that, to protect all other drivers too, not just hwsim.

As suggested, we should provide a zero-value division check for other drivers as well. I will investigate other places where divide errors can occur due to edge cases.

Quoted 7 lines

What, no no.

Quoted 2 lines

Please let me know if you meant something different. In the meantime, I will analyze other drivers for similar cases.

I did. My point is we shouldn't _have_ to check any drivers for this at all, it's nonsense and higher layers (here cfg80211) should reject it.

Thank you for your response Johannes. cfg80211 already have validation in cfg80211_validate_beacon_int(). The problem seems to occur in case of interface shutdown which calls ieee80211_do_stop() that makes beacon_int = 0 or set_tsf which causes divides by zero.

What if we:
1. Handle off-channel operation:

net/mac80211/offchannel.c +6 -4
index ae82533e3c02..14a103d320e3 100644
@@ -156,10 +156,12 @@ void ieee80211_offchannel_return(struct ieee80211_local *local)
 
                if (test_and_clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED,
                                       &sdata->state)) {
-                       sdata->vif.bss_conf.enable_beacon = true;
-                       ieee80211_link_info_change_notify(
-                               sdata, &sdata->deflink,
-                               BSS_CHANGED_BEACON_ENABLED);
+                       if (sdata->vif.bss_conf.beacon_int) {
+                               sdata->vif.bss_conf.enable_beacon = true;
+                               ieee80211_link_info_change_notify(
+                                       sdata, &sdata->deflink,
+                                       BSS_CHANGED_BEACON_ENABLED);
+                       }
                }
        }
 

2. Handle case where debugfs is written after shutdown or any race condition during disable beaconing:

drivers/net/wireless/virtual/mac80211_hwsim.c +1 -1
index 551f5eb4e747..8363cdd17a97 100644
@@ -1242,7 +1242,7 @@ static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
        struct ieee80211_bss_conf *conf;
 
        conf = link_conf_dereference_protected(vif, data->link_data[0].link_id);
-       if (conf && !conf->enable_beacon)
+       if ((conf && !conf->enable_beacon) || !bcn_int)
                return;
 
        /* adjust after beaconing with new timestamp at old TBTT */

3. As other drivers already have beacon_int 0 value validation, consider earlier patch along with above 2 points?

maintainer
#5 lore raw
Quoted 4 lines · Deepakkumar Karn

Thank you for your response Johannes. cfg80211 already have validation in cfg80211_validate_beacon_int().

Oh OK.

Quoted 3 lines

The problem seems to occur in
case of interface shutdown which calls ieee80211_do_stop() that makes beacon_int = 0 or set_tsf which causes divides by zero.

Why would that call set_tsf()?

And yeah that should make beacon_int=0, but not have beaconing enabled.

Quoted 5 lines

What if we:
1. Handle off-channel operation:

                if (test_and_clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state)) {

net/mac80211/offchannel.c void ieee80211_offchannel_return(struct ieee80211_local *local) · line 164

Quoted 10 lines
net/mac80211/offchannel.c +6 -4
-                       sdata->vif.bss_conf.enable_beacon = true;
-                       ieee80211_link_info_change_notify(
-                               sdata, &sdata->deflink,
-                               BSS_CHANGED_BEACON_ENABLED);
+                       if (sdata->vif.bss_conf.beacon_int) {
+                               sdata->vif.bss_conf.enable_beacon = true;
+                               ieee80211_link_info_change_notify(
+                                       sdata, &sdata->deflink,
+                                       BSS_CHANGED_BEACON_ENABLED);
+                       }

don't follow, that only does it when beacon was actually stopped ... maybe do_stop() needs to clear that bit if there's a possibility of this happening in the wrong order, but I don't see how there could be a race since off-channel must also be stopped at that point anyway, unless that is in the wrong order.

drivers/net/wireless/virtual/mac80211_hwsim.c

Quoted 5 lines

2. Handle case where debugfs is written after shutdown or any race condition during disable beaconing:

drivers/net/wireless/virtual/mac80211_hwsim.c +0 -1
index 551f5eb4e747..8363cdd17a97 100644
--- a/drivers/net/wireless/virtual/mac80211_hwsim.c

first of all, I really do think you should *not* (need to) modify hwsim at all

Quoted 4 lines

        conf = link_conf_dereference_protected(vif, data->link_data[0].link_id);

drivers/net/wireless/virtual/mac80211_hwsim.c +1 -1
-       if (conf && !conf->enable_beacon)
+       if ((conf && !conf->enable_beacon) || !bcn_int)

                return;

But this also makes no sense, it shouldn't be possible to have beacon enabled and beacon interval == 0.

johannes

#6 lore raw
Quoted 48 lines · Johannes Berg
Quoted 4 lines · Deepakkumar Karn

Thank you for your response Johannes. cfg80211 already have validation in cfg80211_validate_beacon_int().

Oh OK.

Quoted 3 lines

The problem seems to occur in
case of interface shutdown which calls ieee80211_do_stop() that makes beacon_int = 0 or set_tsf which causes divides by zero.

Why would that call set_tsf()?

And yeah that should make beacon_int=0, but not have beaconing enabled.

Quoted 5 lines

What if we:
1. Handle off-channel operation:

                if (test_and_clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED,
                                       &sdata->state)) {

net/mac80211/offchannel.c void ieee80211_offchannel_return(struct ieee80211_local *local) · line 164

Quoted 10 lines
net/mac80211/offchannel.c +6 -4
-                       sdata->vif.bss_conf.enable_beacon = true;
-                       ieee80211_link_info_change_notify(
-                               sdata, &sdata->deflink,
-                               BSS_CHANGED_BEACON_ENABLED);
+                       if (sdata->vif.bss_conf.beacon_int) {
+                               sdata->vif.bss_conf.enable_beacon = true;
+                               ieee80211_link_info_change_notify(
+                                       sdata, &sdata->deflink,
+                                       BSS_CHANGED_BEACON_ENABLED);
+                       }

don't follow, that only does it when beacon was actually stopped ... maybe do_stop() needs to clear that bit if there's a possibility of this happening in the wrong order, but I don't see how there could be a race since off-channel must also be stopped at that point anyway, unless that is in the wrong order.

drivers/net/wireless/virtual/mac80211_hwsim.c

Quoted 5 lines

2. Handle case where debugfs is written after shutdown or any race condition during disable beaconing:

drivers/net/wireless/virtual/mac80211_hwsim.c +0 -1
index 551f5eb4e747..8363cdd17a97 100644
--- a/drivers/net/wireless/virtual/mac80211_hwsim.c

first of all, I really do think you should *not* (need to) modify hwsim at all

Quoted 8 lines

drivers/net/wireless/virtual/mac80211_hwsim.c static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw, · line 1246

Quoted 4 lines
        conf = link_conf_dereference_protected(vif, data->link_data[0].link_id);
drivers/net/wireless/virtual/mac80211_hwsim.c +1 -1
-       if (conf && !conf->enable_beacon)
+       if ((conf && !conf->enable_beacon) || !bcn_int)
                return;

But this also makes no sense, it shouldn't be possible to have beacon enabled and beacon interval == 0.

Thank you Johannes. As you highlighted I searched for a code path if there is any place that has the
invariant condition of beacon being enabled and beacon int is 0 but none seems to be having that.

Regards,
Deepakkumar Karn