wifi: mac80211: Avoid address calculations via out of bounds array indexing
authorKenton Groombridge <concord@gentoo.org>
Wed, 5 Jun 2024 15:22:18 +0000 (11:22 -0400)
committerJohannes Berg <johannes.berg@intel.com>
Fri, 14 Jun 2024 07:08:41 +0000 (09:08 +0200)
req->n_channels must be set before req->channels[] can be used.

This patch fixes one of the issues encountered in [1].

[   83.964255] UBSAN: array-index-out-of-bounds in net/mac80211/scan.c:364:4
[   83.964258] index 0 is out of range for type 'struct ieee80211_channel *[]'
[...]
[   83.964264] Call Trace:
[   83.964267]  <TASK>
[   83.964269]  dump_stack_lvl+0x3f/0xc0
[   83.964274]  __ubsan_handle_out_of_bounds+0xec/0x110
[   83.964278]  ieee80211_prep_hw_scan+0x2db/0x4b0
[   83.964281]  __ieee80211_start_scan+0x601/0x990
[   83.964291]  nl80211_trigger_scan+0x874/0x980
[   83.964295]  genl_family_rcv_msg_doit+0xe8/0x160
[   83.964298]  genl_rcv_msg+0x240/0x270
[...]

[1] https://bugzilla.kernel.org/show_bug.cgi?id=218810

Co-authored-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Kees Cook <kees@kernel.org>
Signed-off-by: Kenton Groombridge <concord@gentoo.org>
Link: https://msgid.link/20240605152218.236061-1-concord@gentoo.org
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
net/mac80211/scan.c

index 8ecc4b710b0e64449c2a03339b3d3093ec939b7b..b5f2df61c7f6716ebac832073687257e13c65e85 100644 (file)
@@ -358,7 +358,8 @@ static bool ieee80211_prep_hw_scan(struct ieee80211_sub_if_data *sdata)
        struct cfg80211_scan_request *req;
        struct cfg80211_chan_def chandef;
        u8 bands_used = 0;
-       int i, ielen, n_chans;
+       int i, ielen;
+       u32 *n_chans;
        u32 flags = 0;
 
        req = rcu_dereference_protected(local->scan_req,
@@ -368,34 +369,34 @@ static bool ieee80211_prep_hw_scan(struct ieee80211_sub_if_data *sdata)
                return false;
 
        if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
+               local->hw_scan_req->req.n_channels = req->n_channels;
+
                for (i = 0; i < req->n_channels; i++) {
                        local->hw_scan_req->req.channels[i] = req->channels[i];
                        bands_used |= BIT(req->channels[i]->band);
                }
-
-               n_chans = req->n_channels;
        } else {
                do {
                        if (local->hw_scan_band == NUM_NL80211_BANDS)
                                return false;
 
-                       n_chans = 0;
+                       n_chans = &local->hw_scan_req->req.n_channels;
+                       *n_chans = 0;
 
                        for (i = 0; i < req->n_channels; i++) {
                                if (req->channels[i]->band !=
                                    local->hw_scan_band)
                                        continue;
-                               local->hw_scan_req->req.channels[n_chans] =
+                               local->hw_scan_req->req.channels[(*n_chans)++] =
                                                        req->channels[i];
-                               n_chans++;
+
                                bands_used |= BIT(req->channels[i]->band);
                        }
 
                        local->hw_scan_band++;
-               } while (!n_chans);
+               } while (!*n_chans);
        }
 
-       local->hw_scan_req->req.n_channels = n_chans;
        ieee80211_prepare_scan_chandef(&chandef);
 
        if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)