Skip to content

Commit 60882d6

Browse files
dmantipovharshimogalapalli
authored andcommitted
wifi: mac80211: fix UBSAN noise in ieee80211_prep_hw_scan()
[ Upstream commit 92ecbb3ac6f3fe8ae9edf3226c76aa17b6800699 ] When testing the previous patch with CONFIG_UBSAN_BOUNDS, I've noticed the following: UBSAN: array-index-out-of-bounds in net/mac80211/scan.c:372:4 index 0 is out of range for type 'struct ieee80211_channel *[]' CPU: 0 PID: 1435 Comm: wpa_supplicant Not tainted 6.9.0+ #1 Hardware name: LENOVO 20UN005QRT/20UN005QRT <...BIOS details...> Call Trace: <TASK> dump_stack_lvl+0x2d/0x90 __ubsan_handle_out_of_bounds+0xe7/0x140 ? timerqueue_add+0x98/0xb0 ieee80211_prep_hw_scan+0x2db/0x480 [mac80211] ? __kmalloc+0xe1/0x470 __ieee80211_start_scan+0x541/0x760 [mac80211] rdev_scan+0x1f/0xe0 [cfg80211] nl80211_trigger_scan+0x9b6/0xae0 [cfg80211] ...<the rest is not too useful...> Since '__ieee80211_start_scan()' leaves 'hw_scan_req->req.n_channels' uninitialized, actual boundaries of 'hw_scan_req->req.channels' can't be checked in 'ieee80211_prep_hw_scan()'. Although an initialization of 'hw_scan_req->req.n_channels' introduces some confusion around allocated vs. used VLA members, this shouldn't be a problem since everything is correctly adjusted soon in 'ieee80211_prep_hw_scan()'. Cleanup 'kmalloc()' math in '__ieee80211_start_scan()' by using the convenient 'struct_size()' as well. Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru> Link: https://msgid.link/20240517153332.18271-2-dmantipov@yandex.ru [improve (imho) indentation a bit] Signed-off-by: Johannes Berg <johannes.berg@intel.com> Signed-off-by: Sasha Levin <sashal@kernel.org> (cherry picked from commit cd3212a9e0209dff7eda30f01ab8590f5e8d92fb) [Harshit: add #include that was pulled in through some other unknown header on 4.19.y] Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
1 parent 2fac830 commit 60882d6

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

net/mac80211/scan.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/slab.h>
2222
#include <linux/export.h>
2323
#include <net/mac80211.h>
24+
#include <linux/overflow.h>
2425

2526
#include "ieee80211_i.h"
2627
#include "driver-ops.h"
@@ -592,15 +593,21 @@ static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
592593
local->hw_scan_ies_bufsize *= n_bands;
593594
}
594595

595-
local->hw_scan_req = kmalloc(
596-
sizeof(*local->hw_scan_req) +
597-
req->n_channels * sizeof(req->channels[0]) +
598-
local->hw_scan_ies_bufsize, GFP_KERNEL);
596+
local->hw_scan_req = kmalloc(struct_size(local->hw_scan_req,
597+
req.channels,
598+
req->n_channels) +
599+
local->hw_scan_ies_bufsize,
600+
GFP_KERNEL);
599601
if (!local->hw_scan_req)
600602
return -ENOMEM;
601603

602604
local->hw_scan_req->req.ssids = req->ssids;
603605
local->hw_scan_req->req.n_ssids = req->n_ssids;
606+
/* None of the channels are actually set
607+
* up but let UBSAN know the boundaries.
608+
*/
609+
local->hw_scan_req->req.n_channels = req->n_channels;
610+
604611
ies = (u8 *)local->hw_scan_req +
605612
sizeof(*local->hw_scan_req) +
606613
req->n_channels * sizeof(req->channels[0]);

0 commit comments

Comments
 (0)