[PATCH] wifi: wilc1000: fix potential RCU dereference issue in wilc_parse_join_bss_param

Jiawei Ye posted 1 patch 1 year, 3 months ago
drivers/net/wireless/microchip/wilc1000/hif.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] wifi: wilc1000: fix potential RCU dereference issue in wilc_parse_join_bss_param
Posted by Jiawei Ye 1 year, 3 months ago
In the `wilc_parse_join_bss_param` function, the TSF field of the `ies`
structure is accessed after the RCU read-side critical section is
unlocked. According to RCU usage rules, this is illegal. Reusing this
pointer can lead to unpredictable behavior, including 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 TSF value is now stored in a local variable
`ies_tsf` before the RCU lock is released. The `param->tsf_lo` field is
then assigned using this local variable, ensuring that the TSF value is
safely accessed.

Fixes: 205c50306acf ("wifi: wilc1000: fix RCU usage in connect path")
Signed-off-by: Jiawei Ye <jiawei.ye@foxmail.com>
---
 drivers/net/wireless/microchip/wilc1000/hif.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/net/wireless/microchip/wilc1000/hif.c b/drivers/net/wireless/microchip/wilc1000/hif.c
index 3c48e1a57b24..bba53307b960 100644
--- a/drivers/net/wireless/microchip/wilc1000/hif.c
+++ b/drivers/net/wireless/microchip/wilc1000/hif.c
@@ -384,6 +384,7 @@ wilc_parse_join_bss_param(struct cfg80211_bss *bss,
 	struct wilc_join_bss_param *param;
 	u8 rates_len = 0;
 	int ies_len;
+	u64 ies_tsf;
 	int ret;
 
 	param = kzalloc(sizeof(*param), GFP_KERNEL);
@@ -399,6 +400,7 @@ wilc_parse_join_bss_param(struct cfg80211_bss *bss,
 		return NULL;
 	}
 	ies_len = ies->len;
+	ies_tsf = ies->tsf;
 	rcu_read_unlock();
 
 	param->beacon_period = cpu_to_le16(bss->beacon_interval);
@@ -454,7 +456,7 @@ wilc_parse_join_bss_param(struct cfg80211_bss *bss,
 				    IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
 				    (u8 *)&noa_attr, sizeof(noa_attr));
 	if (ret > 0) {
-		param->tsf_lo = cpu_to_le32(ies->tsf);
+		param->tsf_lo = cpu_to_le32(ies_tsf);
 		param->noa_enabled = 1;
 		param->idx = noa_attr.index;
 		if (noa_attr.oppps_ctwindow & IEEE80211_P2P_OPPPS_ENABLE_BIT) {
Re: [PATCH] wifi: wilc1000: fix potential RCU dereference issue in wilc_parse_join_bss_param
Posted by Kalle Valo 1 year, 3 months ago
Jiawei Ye <jiawei.ye@foxmail.com> wrote:

> In the `wilc_parse_join_bss_param` function, the TSF field of the `ies`
> structure is accessed after the RCU read-side critical section is
> unlocked. According to RCU usage rules, this is illegal. Reusing this
> pointer can lead to unpredictable behavior, including 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 TSF value is now stored in a local variable
> `ies_tsf` before the RCU lock is released. The `param->tsf_lo` field is
> then assigned using this local variable, ensuring that the TSF value is
> safely accessed.
> 
> Fixes: 205c50306acf ("wifi: wilc1000: fix RCU usage in connect path")
> Signed-off-by: Jiawei Ye <jiawei.ye@foxmail.com>
> Reviewed-by: Alexis Lothoré <alexis.lothore@bootlin.com>

Patch applied to wireless-next.git, thanks.

6d7c6ae1efb1 wifi: wilc1000: fix potential RCU dereference issue in wilc_parse_join_bss_param

-- 
https://patchwork.kernel.org/project/linux-wireless/patch/tencent_466225AA599BA49627FB26F707EE17BC5407@qq.com/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches

Re: [PATCH] wifi: wilc1000: fix potential RCU dereference issue in wilc_parse_join_bss_param
Posted by Alexis Lothoré 1 year, 3 months ago
Hello,

On 8/29/24 10:17, Jiawei Ye wrote:
> In the `wilc_parse_join_bss_param` function, the TSF field of the `ies`
> structure is accessed after the RCU read-side critical section is
> unlocked. According to RCU usage rules, this is illegal. Reusing this
> pointer can lead to unpredictable behavior, including 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 TSF value is now stored in a local variable
> `ies_tsf` before the RCU lock is released. The `param->tsf_lo` field is
> then assigned using this local variable, ensuring that the TSF value is
> safely accessed.
> 
> Fixes: 205c50306acf ("wifi: wilc1000: fix RCU usage in connect path")
> Signed-off-by: Jiawei Ye <jiawei.ye@foxmail.com>

I guess you are right, that indeed looks like a miss from 205c50306acf. And I
guess it needs wilc to receive packets with P2P info in it to trigger a RCU splat.

Reviewed-by: Alexis Lothoré <alexis.lothore@bootlin.com>

-- 
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com