Skip to contentSkip to conversation

wifi: nxpwifi: validate firmware response sizes

PATCH In review Tristan Madani 2 participants 5 messages Nov 2 – Nov 18

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover wifi: nxpwifi: validate firmware response sizes

From: Tristan Madani <[email protected]>

nxpwifi inherits several patterns from mwifiex where firmware-provided lengths and counts are used to bound loops and copies without validation against the actual response buffer size. A malformed firmware response can cause reads past the buffer.

This series adds bounds checks to three such paths:

1/3 - HE capability TLV length in 11ax config response 2/3 - ext_scan buf_size against event buffer length 3/3 - command response size against response skb length

All three were introduced with the driver in 73b01e57ed3e ("wifi: nxp:
add nxpwifi driver for IW61x").

Tristan Madani (3):
wifi: nxpwifi: validate HE capability TLV length in firmware response
wifi: nxpwifi: validate ext_scan buf_size against event buffer length
wifi: nxpwifi: validate command response size against buffer length

 drivers/net/wireless/nxp/nxpwifi/11ax.c   |  3 +++
 drivers/net/wireless/nxp/nxpwifi/scan.c   |  9 +++++++++
 drivers/net/wireless/nxp/nxpwifi/cmdevt.c | 14 ++++++++++++++
 3 files changed, 26 insertions(+)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/3 wifi: nxpwifi: validate HE capability TLV length in firmware response

From: Tristan Madani <[email protected]>

nxpwifi_ret_11ax_cfg() parses TLV data from firmware without checking that each TLV's declared length fits within the remaining buffer. A firmware response with an oversized TLV length causes reads past the response buffer.

Add a bounds check that the TLV header plus its declared length does not exceed the remaining data before processing the TLV contents.

Fixes 73b01e57ed3e ("wifi: nxp: add nxpwifi driver for IW61x")
---
 drivers/net/wireless/nxp/nxpwifi/11ax.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/wireless/nxp/nxpwifi/11ax.c +3 -0
index dc1e747383eca..1006b4a84de73 100644
@@ -215,6 +215,9 @@ int nxpwifi_ret_11ax_cfg(struct nxpwifi_private *priv,
 		tlv_type = le16_to_cpu(header->type);
 		tlv_len = le16_to_cpu(header->len);
 
+		if (sizeof(*header) + tlv_len > left_len)
+			break;
+
 		if (tlv_type == TLV_TYPE_EXTENSION_ID) {
 			ext_id = *((u8 *)header + sizeof(*header) + 1);
 			if (ext_id == WLAN_EID_EXT_HE_CAPABILITY) {
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/3 wifi: nxpwifi: validate ext_scan buf_size against event buffer length

In reply to Tristan Madani

From: Tristan Madani <[email protected]>

nxpwifi_handle_event_ext_scan_report() uses the firmware-provided buf_size to determine how much scan response data to parse, but never checks it against the actual event buffer length. A firmware response with an inflated buf_size causes reads past the event buffer.

Add a check that the declared buf_size plus the event header does not exceed the event skb length before parsing scan results.

Fixes 73b01e57ed3e ("wifi: nxp: add nxpwifi driver for IW61x")
---
 drivers/net/wireless/nxp/nxpwifi/scan.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/net/wireless/nxp/nxpwifi/scan.c +9 -0
index f3becafb9c23c..e7d98463ad18b 100644
@@ -2427,6 +2427,15 @@ int nxpwifi_handle_event_ext_scan_report(struct nxpwifi_private *priv,
 	u8 *scan_resp = buf + sizeof(struct nxpwifi_event_scan_result);
 	u16 scan_resp_size = le16_to_cpu(event_scan->buf_size);
 
+	if (sizeof(struct nxpwifi_event_scan_result) + scan_resp_size >
+	    adapter->event_skb->len) {
+		nxpwifi_dbg(adapter, ERROR,
+			    "EXT_SCAN: buf_size %u exceeds event length %u\n",
+			    scan_resp_size, adapter->event_skb->len);
+		ret = -EINVAL;
+		goto check_next_scan;
+	}
+
 	if (num_of_set > NXPWIFI_MAX_AP) {
 		nxpwifi_dbg(adapter, ERROR,
 			    "EXT_SCAN: Invalid number of AP returned (%d)!!\n",
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/3 wifi: nxpwifi: validate command response size against buffer length

In reply to Tristan Madani

From: Tristan Madani <[email protected]>

nxpwifi_process_cmdresp() reads the firmware-provided resp->size field and passes it to individual command handlers that use it to determine how much data to process, but never validates it against the actual response buffer length. A firmware response with an inflated size field causes subsequent handlers to read past the buffer.

Add a check early in command response processing that validates the declared response size fits within the response skb.

Fixes 73b01e57ed3e ("wifi: nxp: add nxpwifi driver for IW61x")
---
 drivers/net/wireless/nxp/nxpwifi/cmdevt.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/net/wireless/nxp/nxpwifi/cmdevt.c +14 -0
index fc1950a125b65..50aabf1a72b2e 100644
@@ -722,6 +722,20 @@ int nxpwifi_process_cmdresp(struct nxpwifi_adapter *adapter)
 	orig_cmdresp_no = le16_to_cpu(resp->command);
 	cmdresp_no = (orig_cmdresp_no & HOST_CMD_ID_MASK);
 
+	if (le16_to_cpu(resp->size) > adapter->curr_cmd->resp_skb->len) {
+		nxpwifi_dbg(adapter, ERROR,
+			    "CMD_RESP: resp size %u exceeds buffer %u\n",
+			    le16_to_cpu(resp->size),
+			    adapter->curr_cmd->resp_skb->len);
+		if (adapter->curr_cmd->wait_q_enabled)
+			adapter->cmd_wait_q.status = -EINVAL;
+		nxpwifi_recycle_cmd_node(adapter, adapter->curr_cmd);
+		spin_lock_bh(&adapter->nxpwifi_cmd_lock);
+		adapter->curr_cmd = NULL;
+		spin_unlock_bh(&adapter->nxpwifi_cmd_lock);
+		return -EINVAL;
+	}
+
 	if (adapter->curr_cmd->cmd_no != cmdresp_no) {
 		nxpwifi_dbg(adapter, ERROR,
 			    "cmdresp error: cmd=0x%x cmd_resp=0x%x\n",
Signature · 1 line
2.47.3
#4 lore raw

Re: [PATCH wireless 3/3] wifi: nxpwifi: validate command response size against buffer length

From: Jeff Chen <[email protected]>

drivers/net/wireless/nxp/nxpwifi/cmdevt.c int nxpwifi_process_cmdresp(struct nxpwifi_adapter *adapter) · line 737

Quoted 13 lines · Tristan Madani
drivers/net/wireless/nxp/nxpwifi/cmdevt.c +13 -0
+	if (le16_to_cpu(resp->size) > adapter->curr_cmd->resp_skb->len) {
+		nxpwifi_dbg(adapter, ERROR,
+			    "CMD_RESP: resp size %u exceeds buffer %u\n",
+			    le16_to_cpu(resp->size),
+			    adapter->curr_cmd->resp_skb->len);
+		if (adapter->curr_cmd->wait_q_enabled)
+			adapter->cmd_wait_q.status = -EINVAL;
+		nxpwifi_recycle_cmd_node(adapter, adapter->curr_cmd);
+		spin_lock_bh(&adapter->nxpwifi_cmd_lock);
+		adapter->curr_cmd = NULL;
+		spin_unlock_bh(&adapter->nxpwifi_cmd_lock);
+		return -EINVAL;
+	}

This returns before timer_delete_sync(&adapter->cmd_timer), so the command timer stays armed after curr_cmd is cleared. It will fire and set NXPWIFI_IS_CMD_TIMEDOUT with no command pending.

Please move the check after the timer is cancelled.