Skip to contentSkip to conversation

wifi: mwifiex: fix UAF and ext scan TLV bounds checks

PATCH New Elkin Cruz 1 participant 4 messages Nov 25

Subsystems (2)
lore
Share & tools
lore

Elkin Cruz

author
#0 lore raw

cover wifi: mwifiex: fix UAF and ext scan TLV bounds checks

I am trying out new Clang static analysis checks for kernel bug classes, and they flagged this code as possibly buggy. The findings were triaged/fixed and commit descriptions were drafted with help of AI, then checked against linux-next (not runtime-tested on hardware).

Thanks,
Elkin

Elkin Cruz (3):
wifi: mwifiex: fix UAF after skb_realloc_headroom() in bridged RX
wifi: mwifiex: check TLV header size in ext scan report
wifi: mwifiex: validate ext scan event envelope and TLV lengths

 drivers/net/wireless/marvell/mwifiex/scan.c   | 30 ++++++++++++-------
 .../net/wireless/marvell/mwifiex/uap_txrx.c   | 13 +++++---
 2 files changed, 29 insertions(+), 14 deletions(-)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#1 lore raw

PATCH 1/3 wifi: mwifiex: fix UAF after skb_realloc_headroom() in bridged RX

In mwifiex_uap_queue_bridged_pkt(), uap_rx_pd and rx_pkt_hdr point into the initial skb->data buffer. When skb_headroom(skb) is smaller than MWIFIEX_MIN_DATA_HEADER_LEN, skb_realloc_headroom() allocates a new skb, kfree_skb() frees the original skb, and skb is updated to new_skb, leaving uap_rx_pd and rx_pkt_hdr dangling. Subsequent reads of rx_pkt_hdr->eth803_hdr.h_source, uap_rx_pd->rx_rate, uap_rx_pd->ht_info, and rx_pkt_hdr->eth803_hdr.h_dest dereference the freed skb (and in the RFC1042/SNAP conversion branch, rx_pkt_hdr->eth803_hdr.h_source was also partially overwritten in-place when constructing the Ethernet II header).

Save rx_rate and ht_info from uap_rx_pd after the packet length check and re-derive p_ethhdr from the live skb->data after any headroom reallocation, since skb_pull() already positioned skb->data at the Ethernet header in both header-conversion branches.

Fixes f7b598532f3d ("mwifiex: update statistics for bridged packets on AP interface")
Fixes 442f6f9b49c8 ("mwifiex: maintain station statistic in uap mode")
---
 drivers/net/wireless/marvell/mwifiex/uap_txrx.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/net/wireless/marvell/mwifiex/uap_txrx.c +9 -4
index 318bd4ed8399..d2512b985014 100644
@@ -89,6 +89,7 @@ static void mwifiex_uap_queue_bridged_pkt(struct mwifiex_private *priv,
 	int hdr_chop;
 	struct ethhdr *p_ethhdr;
 	struct mwifiex_sta_node *src_node;
+	u8 rx_rate, ht_info;
 	int index;
 
 	uap_rx_pd = (struct uap_rxpd *)(skb->data);
@@ -113,6 +114,9 @@ static void mwifiex_uap_queue_bridged_pkt(struct mwifiex_private *priv,
 		return;
 	}
 
+	rx_rate = uap_rx_pd->rx_rate;
+	ht_info = uap_rx_pd->ht_info;
+
 	if ((!memcmp(&rx_pkt_hdr->rfc1042_hdr, bridge_tunnel_header,
 		     sizeof(bridge_tunnel_header))) ||
 	    (!memcmp(&rx_pkt_hdr->rfc1042_hdr, rfc1042_header,
@@ -183,16 +187,17 @@ static void mwifiex_uap_queue_bridged_pkt(struct mwifiex_private *priv,
 	tx_info->bss_type = priv->bss_type;
 	tx_info->flags |= MWIFIEX_BUF_FLAG_BRIDGED_PKT;
 
-	src_node = mwifiex_get_sta_entry(priv, rx_pkt_hdr->eth803_hdr.h_source);
+	p_ethhdr = (void *)skb->data;
+	src_node = mwifiex_get_sta_entry(priv, p_ethhdr->h_source);
 	if (src_node) {
 		src_node->stats.last_rx = jiffies;
 		src_node->stats.rx_bytes += skb->len;
 		src_node->stats.rx_packets++;
-		src_node->stats.last_tx_rate = uap_rx_pd->rx_rate;
-		src_node->stats.last_tx_htinfo = uap_rx_pd->ht_info;
+		src_node->stats.last_tx_rate = rx_rate;
+		src_node->stats.last_tx_htinfo = ht_info;
 	}
 
-	if (is_unicast_ether_addr(rx_pkt_hdr->eth803_hdr.h_dest)) {
+	if (is_unicast_ether_addr(p_ethhdr->h_dest)) {
 		/* Update bridge packet statistics as the
 		 * packet is not going to kernel/upper layer.
 		 */
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#2 lore raw

PATCH 2/3 wifi: mwifiex: check TLV header size in ext scan report

In reply to Elkin Cruz

mwifiex_handle_event_ext_scan_report() loops while bytes_left is non-zero and immediately dereferences tlv->header.type and tlv->header.len (4 bytes) before checking whether bytes_left can hold a complete struct mwifiex_ie_types_header. When 1 <= bytes_left < 4, this reads past the end of the firmware event buffer.

Require bytes_left >= sizeof(struct mwifiex_ie_types_header) in the outer loop condition, matching the inner TLV loop directly below.

Fixes 21f58d200388 ("mwifiex: implement extended scan feature")
---
 drivers/net/wireless/marvell/mwifiex/scan.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/wireless/marvell/mwifiex/scan.c +2 -1
index bdd4b8465863..53aa16c1df33 100644
@@ -2649,7 +2649,8 @@ int mwifiex_handle_event_ext_scan_report(struct mwifiex_private *priv,
 
 	tlv = (struct mwifiex_ie_types_data *)scan_resp;
 
-	for (idx = 0; idx < num_of_set && bytes_left; idx++) {
+	for (idx = 0; idx < num_of_set &&
+	     bytes_left >= sizeof(struct mwifiex_ie_types_header); idx++) {
 		type = le16_to_cpu(tlv->header.type);
 		len = le16_to_cpu(tlv->header.len);
 		if (bytes_left < sizeof(struct mwifiex_ie_types_header) + len) {
Signature · 1 line
2.56.1.839.g22a8290db9-goog
author
#3 lore raw

PATCH 3/3 wifi: mwifiex: validate ext scan event envelope and TLV lengths

In reply to Elkin Cruz

In mwifiex_handle_event_ext_scan_report(), event_scan is cast from buf without first verifying that adapter->event_skb contains at least sizeof(*event_scan) bytes, and scan_resp_size is not validated against the remaining event buffer length before dumping memory.

Additionally, under TLV_TYPE_BSS_SCAN_INFO, scan_info_tlv was assigned before checking whether the TLV length matches the expected structure size, causing invalid pointers to be retained on short TLVs.

Validate the event_skb length and scan_resp_size upfront, and verify the TLV size before assigning scan_info_tlv. Break out of the switch on a malformed TLV_TYPE_BSS_SCAN_INFO so the loop skips that TLV while keeping bytes_left accounting consistent.

Fixes 21f58d200388 ("mwifiex: implement extended scan feature")
---
 drivers/net/wireless/marvell/mwifiex/scan.c | 27 ++++++++++++++-------
 1 file changed, 18 insertions(+), 9 deletions(-)
drivers/net/wireless/marvell/mwifiex/scan.c +18 -9
index 53aa16c1df33..52543532367b 100644
@@ -2627,9 +2627,21 @@ int mwifiex_handle_event_ext_scan_report(struct mwifiex_private *priv,
 	u64 fw_tsf = 0;
 	s32 rssi = 0;
 	struct mwifiex_event_scan_result *event_scan = buf;
-	u8 num_of_set = event_scan->num_of_set;
-	u8 *scan_resp = buf + sizeof(struct mwifiex_event_scan_result);
-	u16 scan_resp_size = le16_to_cpu(event_scan->buf_size);
+	u8 num_of_set;
+	u8 *scan_resp;
+	u16 scan_resp_size;
+
+	if (adapter->event_skb->len < sizeof(*event_scan))
+		return -EINVAL;
+
+	num_of_set = event_scan->num_of_set;
+	scan_resp = buf + sizeof(struct mwifiex_event_scan_result);
+	scan_resp_size = le16_to_cpu(event_scan->buf_size);
+
+	if (scan_resp_size > adapter->event_skb->len - sizeof(*event_scan)) {
+		ret = -EINVAL;
+		goto check_next_scan;
+	}
 
 	if (num_of_set > MWIFIEX_MAX_AP) {
 		mwifiex_dbg(adapter, ERROR,
@@ -2690,14 +2702,11 @@ int mwifiex_handle_event_ext_scan_report(struct mwifiex_private *priv,
 			}
 			switch (type) {
 			case TLV_TYPE_BSS_SCAN_INFO:
+				if (len != sizeof(struct mwifiex_ie_types_bss_scan_info) -
+					   sizeof(struct mwifiex_ie_types_header))
+					break;
 				scan_info_tlv =
 				  (struct mwifiex_ie_types_bss_scan_info *)tlv;
-				if (len !=
-				 sizeof(struct mwifiex_ie_types_bss_scan_info) -
-				 sizeof(struct mwifiex_ie_types_header)) {
-					bytes_left_for_tlv = 0;
-					continue;
-				}
 				break;
 			default:
 				break;
Signature · 1 line
2.56.1.839.g22a8290db9-goog