Skip to contentSkip to conversation

wifi: mac80211: Set n_channels after allocating struct cfg80211_scan_request

PATCH In review Kees Cook 2 participants 2 messages May 9, 2025 mac80211

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 wifi: mac80211: Set n_channels after allocating struct cfg80211_scan_request

Make sure that n_channels is set after allocating the struct cfg80211_registered_device::int_scan_req member. Seen with syzkaller:

UBSAN: array-index-out-of-bounds in net/mac80211/scan.c:1208:5

index 0 is out of range for type 'struct ieee80211_channel *[] __counted_by(n_channels)' (aka 'struct ieee80211_channel *[]')

This was missed in the initial conversions because I failed to locate the allocation likely due to the "sizeof(void *)" not matching the "channels" array type.

Reported-by [email protected]
Fixes e3eac9f32ec0 ("wifi: cfg80211: Annotate struct cfg80211_scan_request with __counted_by")

---
Cc: Edward Adam Davis <[email protected]>
Cc: Johannes Berg <[email protected]>
Cc: <[email protected]>

---
 net/mac80211/main.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/mac80211/main.c +4 -2
index 741e6c7edcb7..6b6de43d9420 100644
@@ -1354,10 +1354,12 @@ int ieee80211_register_hw(struct ieee80211_hw *hw)
 	hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_MONITOR);
 
 
-	local->int_scan_req = kzalloc(sizeof(*local->int_scan_req) +
-				      sizeof(void *) * channels, GFP_KERNEL);
+	local->int_scan_req = kzalloc(struct_size(local->int_scan_req,
+						  channels, channels),
+				      GFP_KERNEL);
 	if (!local->int_scan_req)
 		return -ENOMEM;
+	local->int_scan_req->n_channels = channels;
 
 	eth_broadcast_addr(local->int_scan_req->bssid);
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Kees Cook

Make sure that n_channels is set after allocating the struct cfg80211_registered_device::int_scan_req member. Seen with syzkaller:

UBSAN: array-index-out-of-bounds in net/mac80211/scan.c:1208:5

index 0 is out of range for type 'struct ieee80211_channel *[] __counted_by(n_channels)' (aka 'struct ieee80211_channel *[]')

Quoted 8 lines

This was missed in the initial conversions because I failed to locate the allocation likely due to the "sizeof(void *)" not matching the "channels" array type.

Reported-by [email protected]
Fixes e3eac9f32ec0 ("wifi: cfg80211: Annotate struct cfg80211_scan_request with __counted_by")

Thanks!
-Gustavo

Quoted 27 lines

---
Cc: Edward Adam Davis <[email protected]>
Cc: Johannes Berg <[email protected]>
Cc: <[email protected]>

---
  net/mac80211/main.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
net/mac80211/main.c +4 -2
index 741e6c7edcb7..6b6de43d9420 100644
@@ -1354,10 +1354,12 @@ int ieee80211_register_hw(struct ieee80211_hw *hw)
  	hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_MONITOR);
 
 
-	local->int_scan_req = kzalloc(sizeof(*local->int_scan_req) +
-				      sizeof(void *) * channels, GFP_KERNEL);
+	local->int_scan_req = kzalloc(struct_size(local->int_scan_req,
+						  channels, channels),
+				      GFP_KERNEL);
  	if (!local->int_scan_req)
  		return -ENOMEM;
+	local->int_scan_req->n_channels = channels;
 
  	eth_broadcast_addr(local->int_scan_req->bssid);