Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mac802154: Fix potential RCU dereference issue in mac802154_scan_worker

In the `mac802154_scan_worker` function, the `scan_req->type` field was
accessed after the RCU read-side critical section was unlocked. According
to RCU usage rules, this is illegal and can lead to unpredictable
behavior, such as accessing memory that has been updated or causing
use-after-free issues.

This possible bug was identified using a static analysis tool developed
by myself, specifically designed to detect RCU-related issues.

To address this, the `scan_req->type` value is now stored in a local
variable `scan_req_type` while still within the RCU read-side critical
section. The `scan_req_type` is then used after the RCU lock is released,
ensuring that the type value is safely accessed without violating RCU
rules.

Fixes: e2c3e6f53a7a ("mac802154: Handle active scanning")
Cc: stable@vger.kernel.org
Signed-off-by: Jiawei Ye <jiawei.ye@foxmail.com>
Acked-by: Miquel Raynal <miquel.raynal@bootlin.com>
Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Link: https://lore.kernel.org/tencent_3B2F4F2B4DA30FAE2F51A9634A16B3AD4908@qq.com
Signed-off-by: Stefan Schmidt <stefan@datenfreihafen.org>

authored by

Jiawei Ye and committed by
Stefan Schmidt
bff1709b addf8977

+3 -1
+3 -1
net/mac802154/scan.c
··· 176 176 struct ieee802154_local *local = 177 177 container_of(work, struct ieee802154_local, scan_work.work); 178 178 struct cfg802154_scan_request *scan_req; 179 + enum nl802154_scan_types scan_req_type; 179 180 struct ieee802154_sub_if_data *sdata; 180 181 unsigned int scan_duration = 0; 181 182 struct wpan_phy *wpan_phy; ··· 210 209 } 211 210 212 211 wpan_phy = scan_req->wpan_phy; 212 + scan_req_type = scan_req->type; 213 213 scan_req_duration = scan_req->duration; 214 214 215 215 /* Look for the next valid chan */ ··· 248 246 goto end_scan; 249 247 } 250 248 251 - if (scan_req->type == NL802154_SCAN_ACTIVE) { 249 + if (scan_req_type == NL802154_SCAN_ACTIVE) { 252 250 ret = mac802154_transmit_beacon_req(local, sdata); 253 251 if (ret) 254 252 dev_err(&sdata->dev->dev,