Skip to contentSkip to conversation

wifi: ath12k: protect DP peer key cache with RCU

PATCH New Aishwarya R 1 participant 1 message Dec 2 Qualcomm ATH12K wireless driver

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 wifi: ath12k: protect DP peer key cache with RCU

dp_peer->keys[] caches ieee80211_key_conf pointers for use in the data path. These pointers are updated by ath12k_mac_set_key() and ath12k_mac_clear_peer_keys(), while RX and TX processing access them concurrently.

RX processing dereferences cached keys during TKIP MIC verification, and TX processing uses the cached multicast key to determine the cipher type for transmitted frames. The existing peer lookup locking only protects retrieval of dp_peer and provides no synchronization for subsequent accesses to dp_peer->keys[].

This creates a potential use-after-free window. During key replacement or removal, RX or TX processing may continue using a cached ieee80211_key_conf pointer concurrently with the cache update, resulting in the dereference of a stale pointer.

Protect dp_peer->keys[] with RCU. Use rcu_assign_pointer() when publishing or removing cached key entries and use rcu_dereference() when accessing them from RX and TX paths. This provides proper synchronization between readers and updaters of the cached key references.

Also remove the cached sec_type and sec_type_grp fields from dp_peer. These fields duplicate information already available through the cached key configuration and may become inconsistent with dp_peer->keys[]. Derive the encryption type directly from the key configuration so that the cached key entry remains the single source of truth.

Move cache invalidation to sta_pre_rcu_remove(), which fires before the RCU grace period in station teardown, ensuring no stale pointer remains visible to the data path when mac80211 frees the keys in __sta_info_destroy_part2().

Tested-on: QCN9274 hw2.0 PCI WLAN.WBE.1.6-01243-QCAHKSWPL_SILICONZ-1

---
Changes in v2:
- Rebased the changes

---
 drivers/net/wireless/ath/ath12k/dp_peer.c     |  53 ++++-
 drivers/net/wireless/ath/ath12k/dp_peer.h     |  10 +-
 drivers/net/wireless/ath/ath12k/mac.c         | 192 ++++++++----------
 drivers/net/wireless/ath/ath12k/mac.h         |   3 +
 drivers/net/wireless/ath/ath12k/wifi7/dp_rx.c |  23 ++-
 drivers/net/wireless/ath/ath12k/wifi7/hw.c    |   7 +-
 drivers/net/wireless/ath/ath12k/wmi.h         |   5 +-
 7 files changed, 170 insertions(+), 123 deletions(-)
drivers/net/wireless/ath/ath12k/dp_peer.c +51 -2
index 5dc7f08e6292..76462731e60c 100644
@@ -394,6 +394,55 @@ struct ath12k_dp_peer *ath12k_dp_peer_find_by_addr(struct ath12k_dp_hw *dp_hw, u
 }
 EXPORT_SYMBOL(ath12k_dp_peer_find_by_addr);
 
+int ath12k_dp_peer_set_key_config(struct ath12k_dp_hw *dp_hw,
+				  const u8 *peer_addr,
+				  enum set_key_cmd cmd,
+				  struct ieee80211_key_conf *key,
+				  struct ieee80211_sta *sta)
+{
+	struct ath12k_dp_peer *dp_peer;
+
+	guard(spinlock_bh)(&dp_hw->peer_lock);
+
+	if (sta)
+		dp_peer = ath12k_dp_peer_find_by_addr_and_sta(dp_hw, (u8 *)peer_addr,
+							      sta);
+	else
+		dp_peer = ath12k_dp_peer_find_by_addr(dp_hw, (u8 *)peer_addr);
+
+	if (!dp_peer) {
+		/* No peer means no key to disable. */
+		if (cmd == DISABLE_KEY)
+			return 0;
+
+		return -ENOENT;
+	}
+
+	switch (cmd) {
+	case SET_KEY:
+		rcu_assign_pointer(dp_peer->keys[key->keyidx], key);
+		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
+			WRITE_ONCE(dp_peer->ucast_keyidx, key->keyidx);
+		else
+			WRITE_ONCE(dp_peer->mcast_keyidx, key->keyidx);
+		return 0;
+	case DISABLE_KEY:
+		rcu_assign_pointer(dp_peer->keys[key->keyidx], NULL);
+		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
+			if (READ_ONCE(dp_peer->ucast_keyidx) == key->keyidx)
+				WRITE_ONCE(dp_peer->ucast_keyidx,
+					   WMI_INVALID_KEY_INDEX);
+		} else {
+			if (READ_ONCE(dp_peer->mcast_keyidx) == key->keyidx)
+				WRITE_ONCE(dp_peer->mcast_keyidx,
+					   WMI_INVALID_KEY_INDEX);
+		}
+		return 0;
+	default:
+		return -EINVAL;
+	}
+}
+
 static void ath12k_dp_peer_hash_table_add(struct ath12k_dp_hw *dp_hw,
 					  struct ath12k_dp_peer *dp_peer)
 {
@@ -541,8 +590,8 @@ int ath12k_dp_peer_create(struct ath12k_dp_hw *dp_hw, u8 *addr,
 	dp_peer->peer_id = params->is_mlo ? params->peer_id : ATH12K_DP_PEER_ID_INVALID;
 	dp_peer->ucast_ra_only = params->ucast_ra_only;
 
-	dp_peer->sec_type = HAL_ENCRYPT_TYPE_OPEN;
-	dp_peer->sec_type_grp = HAL_ENCRYPT_TYPE_OPEN;
+	dp_peer->ucast_keyidx = WMI_INVALID_KEY_INDEX;
+	dp_peer->mcast_keyidx = WMI_INVALID_KEY_INDEX;
 
 	dp_peer->free_slot_map = U16_MAX;
 
drivers/net/wireless/ath/ath12k/dp_peer.h +6 -4
index 0a2b9d1814a5..d861d08c8108 100644
@@ -153,11 +153,8 @@ struct ath12k_dp_peer {
 	 */
 	u16 free_slot_map;
 
-	u16 sec_type_grp;
-	u16 sec_type;
-
 	/* Info used in MMIC verification of * RX fragments */
-	struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1];
+	struct ieee80211_key_conf __rcu *keys[WMI_MAX_KEY_INDEX + 1];
 	struct ath12k_dp_link_peer __rcu *link_peers[ATH12K_NUM_MAX_LINKS];
 	struct ath12k_reoq_buf reoq_bufs[IEEE80211_NUM_TIDS + 1];
 	struct ath12k_dp_rx_tid rx_tid[IEEE80211_NUM_TIDS + 1];
@@ -217,4 +214,9 @@ ath12k_dp_link_peer_get_sta(const struct ath12k_dp_link_peer *link_peer)
 
 void ath12k_dp_peer_hash_table_delete(struct ath12k_dp_hw *dp_hw,
 				      struct ath12k_dp_peer *dp_peer);
+int ath12k_dp_peer_set_key_config(struct ath12k_dp_hw *dp_hw,
+				  const u8 *peer_addr,
+				  enum set_key_cmd cmd,
+				  struct ieee80211_key_conf *key,
+				  struct ieee80211_sta *sta);
 #endif
drivers/net/wireless/ath/ath12k/mac.c +85 -107

266 lines not on this page · raw message

drivers/net/wireless/ath/ath12k/mac.h +3 -0
index 61c2ec8865a3..4bb845284838 100644
@@ -251,6 +251,9 @@ int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
 			    struct ieee80211_sta *sta,
 			    enum ieee80211_sta_state old_state,
 			    enum ieee80211_sta_state new_state);
+void ath12k_mac_op_sta_pre_rcu_remove(struct ieee80211_hw *hw,
+				      struct ieee80211_vif *vif,
+				      struct ieee80211_sta *sta);
 int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
 				struct ieee80211_vif *vif,
 				struct ieee80211_sta *sta);
drivers/net/wireless/ath/ath12k/wifi7/dp_rx.c +16 -7
index 473887ed4bb1..9f6eb5c64498 100644
@@ -357,8 +357,10 @@ static int ath12k_wifi7_dp_rx_h_mpdu(struct ath12k_pdev_dp *dp_pdev,
 	bool is_decrypted = false;
 	struct ieee80211_hdr *hdr;
 	struct ath12k_dp_peer *peer;
+	struct ieee80211_key_conf *key_conf = NULL;
 	struct ieee80211_rx_status *rx_status = rx_info->rx_status;
 	u32 err_bitmap = rx_info->err_bitmap;
+	u8 key_idx;
 
 	RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
 			 "dp_rx_h_mpdu called without rcu lock");
@@ -389,10 +391,12 @@ static int ath12k_wifi7_dp_rx_h_mpdu(struct ath12k_pdev_dp *dp_pdev,
 		 */
 		rxcb->is_mcbc = rxcb->is_mcbc && !peer->ucast_ra_only;
 
-		if (rxcb->is_mcbc)
-			enctype = peer->sec_type_grp;
-		else
-			enctype = peer->sec_type;
+		key_idx = rxcb->is_mcbc ? READ_ONCE(peer->mcast_keyidx) :
+				 READ_ONCE(peer->ucast_keyidx);
+		if (key_idx <= WMI_MAX_KEY_INDEX)
+			key_conf = rcu_dereference(peer->keys[key_idx]);
+		enctype = key_conf ? ath12k_dp_tx_get_encrypt_type(key_conf->cipher)
+				: HAL_ENCRYPT_TYPE_OPEN;
 	} else {
 		enctype = HAL_ENCRYPT_TYPE_OPEN;
 	}
@@ -1053,11 +1057,16 @@ static int ath12k_wifi7_dp_rx_h_verify_tkip_mic(struct ath12k_pdev_dp *dp_pdev,
 	tail_len = IEEE80211_CCMP_MIC_LEN + IEEE80211_TKIP_ICV_LEN + FCS_LEN;
 
 	if (!is_multicast_ether_addr(hdr->addr1))
-		key_idx = peer->ucast_keyidx;
+		key_idx = READ_ONCE(peer->ucast_keyidx);
 	else
-		key_idx = peer->mcast_keyidx;
+		key_idx = READ_ONCE(peer->mcast_keyidx);
 
-	key_conf = peer->keys[key_idx];
+	if (key_idx > WMI_MAX_KEY_INDEX)
+		goto mic_fail;
+
+	key_conf = rcu_dereference(peer->keys[key_idx]);
+	if (!key_conf)
+		goto mic_fail;
 
 	data = msdu->data + head_len;
 	data_len = msdu->len - head_len - tail_len;
drivers/net/wireless/ath/ath12k/wifi7/hw.c +6 -1
index 2c036ac503e5..0547f404b460 100644
@@ -1096,6 +1096,7 @@ static void ath12k_wifi7_mac_op_tx(struct ieee80211_hw *hw,
 	u8 link_id;
 	int ret;
 	struct ath12k_dp *tmp_dp;
+	u8 keyidx;
 
 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
 		ieee80211_free_txskb(hw, skb);
@@ -1286,7 +1287,10 @@ static void ath12k_wifi7_mac_op_tx(struct ieee80211_hw *hw,
 				continue;
 			}
 
-			key = peer->dp_peer->keys[peer->dp_peer->mcast_keyidx];
+			keyidx = READ_ONCE(peer->dp_peer->mcast_keyidx);
+			key = (keyidx <= WMI_MAX_KEY_INDEX) ?
+				rcu_dereference(peer->dp_peer->keys[keyidx]) :
+				NULL;
 			if (key) {
 				skb_cb->cipher = key->cipher;
 				skb_cb->flags |= ATH12K_SKB_CIPHER_SET;
@@ -1346,6 +1350,7 @@ static const struct ieee80211_ops ath12k_ops_wifi7 = {
 	.set_key                        = ath12k_mac_op_set_key,
 	.set_rekey_data	                = ath12k_mac_op_set_rekey_data,
 	.sta_state                      = ath12k_mac_op_sta_state,
+	.sta_pre_rcu_remove             = ath12k_mac_op_sta_pre_rcu_remove,
 	.sta_set_txpwr			= ath12k_mac_op_sta_set_txpwr,
 	.link_sta_rc_update		= ath12k_mac_op_link_sta_rc_update,
 	.sta_set_4addr                  = ath12k_mac_op_sta_set_4addr,
drivers/net/wireless/ath/ath12k/wmi.h +3 -2
index 843f1d11f9c8..c0345d439854 100644
@@ -4950,8 +4950,9 @@ enum wmi_ap_ps_peer_param {
 
 #define DISABLE_SIFS_RESPONSE_TRIGGER 0
 
-#define WMI_MAX_KEY_INDEX   7
-#define WMI_MAX_KEY_LEN     32
+#define WMI_MAX_KEY_INDEX     7
+#define WMI_INVALID_KEY_INDEX (WMI_MAX_KEY_INDEX + 1)
+#define WMI_MAX_KEY_LEN       32
 
 enum wmi_key_type {
 	WMI_KEY_PAIRWISE = 0,

base-commit: 851c0117ee50b0e5c417cb9f85f14b43c369fb70

Signature · 1 line
2.34.1