Skip to contentSkip to conversation

wifi: ath12k: fix M3 buffer reuse in my CVE-2024-40979 backport

PATCH Applied Artem Dinaburg 3 participants 6 messages Oct 12 – Oct 15

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

cover wifi: ath12k: fix M3 buffer reuse in my CVE-2024-40979 backport

Hi Greg, Sasha, and ath12k maintainers,

My backport of 05090ae82f44 ("wifi: ath12k: check M3 buffer size as well whey trying to reuse it"), which landed in 6.6.158 as 9a38ba5561d9, does not match upstream. When the existing M3 buffer is large enough, it returns early without copying the new image or updating its length. Upstream copies the current image into the reused buffer every time. In 6.6.y, recovery or resume therefore keeps the old M3 image unless the new one is larger.

Restoring the copy needs one more upstream fix first. Once a smaller image can be copied into the buffer, m3_mem->size can shrink below the allocation size, and that shrunken size is later passed to dma_free_coherent(). Upstream fixed this in 00575bb44b2c ("wifi: ath12k:
fix reusing m3 memory") by tracking the allocation size separately.

1/2 backports 00575bb44b2c.
2/2 fixes my backport by restoring the upstream copy on reuse. It has
no upstream equivalent; upstream never had the early return.

6.12.y already copies on reuse but lacked 00575bb44b2c; my backport of it is now queued for 6.12.y. 6.18.y and 7.2.y already have the fix, and 6.1.y has no ath12k driver.

Testing: both patches apply to v6.6.158 and on top of the current 6.6 stable queue. An x86_64 allmodconfig build with CONFIG_WERROR=y is clean, and ath12k also builds cleanly with W=1. I tested ath12k_qmi_m3_load() and ath12k_qmi_m3_free() in userspace with firmware and DMA stubs under ASan/UBSan, covering same-size, smaller, and larger replacement images, plus allocation and firmware-request failures. I did not test on ath12k hardware.

Previous series: https://lore.kernel.org/all/[email protected]/

An LLM helped compare the backport with upstream, prepare these patches, and build and test them. I reviewed the results.

Thanks,
Artem Dinaburg

Artem Dinaburg (1):
wifi: ath12k: copy M3 firmware when reusing the buffer

Baochen Qiang (1):
wifi: ath12k: fix reusing m3 memory

#1 lore raw

PATCH 1/2 wifi: ath12k: fix reusing m3 memory

From: Baochen Qiang <[email protected]>

[ Upstream commit 00575bb44b2c2aa53d0a768de2b80c9c1af0174d ]

During firmware recovery or suspend/resume, m3 memory could be reused if the size of the new m3 binary is equal to or less than that of the existing memory. There will be issues for the latter case, since m3_mem->size will be updated with a smaller value and this value is eventually used in the free path, where the original total size should be used instead.

To fix it, add a new member in m3_mem_region structure to track the original memory size and use it in free path.

Tested-on: WCN7850 hw2.0 PCI WLAN.HMT.1.1.c5-00302-QCAHMTSWPL_V1.0_V2.0_SILICONZ-1.115823.3

[ Backport to 6.6.y: 6.6.y only loads the separate m3.bin, so use
fw->size where upstream uses m3_len. The upstream change to the allocation-failure message only matters for the firmware-N.bin path, which 6.6.y lacks, so it is dropped. The copyright lines are left as they are.
On its own this changes nothing in 6.6.y, because the reuse path currently returns before m3_mem->size is updated. It is needed by the following fix to my 6.6.y backport of 05090ae82f44, which copies a possibly smaller image into the reused buffer as upstream does. ]

Fixes 05090ae82f44 ("wifi: ath12k: check M3 buffer size as well whey trying to reuse it")
Assisted-by LLM
---
 drivers/net/wireless/ath/ath12k/qmi.c | 7 +++++--
 drivers/net/wireless/ath/ath12k/qmi.h | 3 +++
 2 files changed, 8 insertions(+), 2 deletions(-)
drivers/net/wireless/ath/ath12k/qmi.c +5 -2
index 48d0043fcee9..86cc61dba094 100644
@@ -2518,9 +2518,10 @@ static void ath12k_qmi_m3_free(struct ath12k_base *ab)
 	if (!m3_mem->vaddr)
 		return;
 
-	dma_free_coherent(ab->dev, m3_mem->size,
+	dma_free_coherent(ab->dev, m3_mem->total_size,
 			  m3_mem->vaddr, m3_mem->paddr);
 	m3_mem->vaddr = NULL;
+	m3_mem->total_size = 0;
 	m3_mem->size = 0;
 }
 
@@ -2542,7 +2543,7 @@ static int ath12k_qmi_m3_load(struct ath12k_base *ab)
 
 	/* In recovery/resume cases, M3 buffer is not freed, try to reuse that */
 	if (m3_mem->vaddr) {
-		if (m3_mem->size >= fw->size) {
+		if (m3_mem->total_size >= fw->size) {
 			release_firmware(fw);
 			return 0;
 		}
@@ -2561,6 +2562,8 @@ static int ath12k_qmi_m3_load(struct ath12k_base *ab)
 		return -ENOMEM;
 	}
 
+	m3_mem->total_size = fw->size;
+
 	memcpy(m3_mem->vaddr, fw->data, fw->size);
 	m3_mem->size = fw->size;
 	release_firmware(fw);
drivers/net/wireless/ath/ath12k/qmi.h +3 -0
index a996c6d57aa7..62c7a526f111 100644
@@ -119,6 +119,9 @@ struct target_info {
 };
 
 struct m3_mem_region {
+	/* total memory allocated */
+	u32 total_size;
+	/* actual memory being used */
 	u32 size;
 	dma_addr_t paddr;
 	void *vaddr;
#2 lore raw

PATCH 2/2 wifi: ath12k: copy M3 firmware when reusing the buffer

In reply to Artem Dinaburg

This fixes my 6.6.y backport of upstream commit 05090ae82f44 ("wifi:
ath12k: check M3 buffer size as well whey trying to reuse it"). There is no corresponding upstream commit, because upstream never had this bug.

When the existing M3 buffer is large enough for the new m3.bin, my backport releases the firmware and returns early. The new image is never copied, and m3_mem->size keeps the old length. Upstream instead jumps to skip_m3_alloc, which copies the current image into the existing buffer and records its length. As a result, after recovery or resume, 6.6.y gives the firmware the previous M3 image whenever the new one is the same size or smaller. A larger image is reallocated and loaded.

Replace the early return with the upstream goto skip_m3_alloc. The previous patch, a backport of upstream commit 00575bb44b2c ("wifi:
ath12k: fix reusing m3 memory"), keeps the allocation size in total_size. So copying a smaller image no longer changes the size passed to dma_free_coherent().

Fixes 9a38ba5561d9 ("wifi: ath12k: check M3 buffer size as well whey trying to reuse it")
Assisted-by LLM
---
 drivers/net/wireless/ath/ath12k/qmi.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/net/wireless/ath/ath12k/qmi.c +3 -4
index 86cc61dba094..8dc1c8e0c7b1 100644
@@ -2543,10 +2543,8 @@ static int ath12k_qmi_m3_load(struct ath12k_base *ab)
 
 	/* In recovery/resume cases, M3 buffer is not freed, try to reuse that */
 	if (m3_mem->vaddr) {
-		if (m3_mem->total_size >= fw->size) {
-			release_firmware(fw);
-			return 0;
-		}
+		if (m3_mem->total_size >= fw->size)
+			goto skip_m3_alloc;
 
 		/* Old buffer is too small, free and reallocate */
 		ath12k_qmi_m3_free(ab);
@@ -2564,6 +2562,7 @@ static int ath12k_qmi_m3_load(struct ath12k_base *ab)
 
 	m3_mem->total_size = fw->size;
 
+skip_m3_alloc:
 	memcpy(m3_mem->vaddr, fw->data, fw->size);
 	m3_mem->size = fw->size;
 	release_firmware(fw);
maintainer
#3 lore raw

Re: [PATCH 6.6.y 2/2] wifi: ath12k: copy M3 firmware when reusing the buffer

Quoted 3 lines

This fixes my 6.6.y backport of upstream commit 05090ae82f44 ("wifi:
ath12k: check M3 buffer size as well whey trying to reuse it"). There is no corresponding upstream commit, because upstream never had this bug.

Holding this series until we have an Acked-by or Reviewed-by on 2/2 from Baochen Qiang or Jeff Johnson. It is a stable-only fix with no upstream equivalent, so I'd like an ath12k maintainer to look at it first.

Signature · 2 lines
Thanks,
Sasha
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 6.6.y 2/2] wifi: ath12k: copy M3 firmware when reusing the buffer

In reply to Artem Dinaburg
Quoted 18 lines · Artem Dinaburg

This fixes my 6.6.y backport of upstream commit 05090ae82f44 ("wifi:
ath12k: check M3 buffer size as well whey trying to reuse it"). There is no corresponding upstream commit, because upstream never had this bug.

When the existing M3 buffer is large enough for the new m3.bin, my backport releases the firmware and returns early. The new image is never copied, and m3_mem->size keeps the old length. Upstream instead jumps to skip_m3_alloc, which copies the current image into the existing buffer and records its length. As a result, after recovery or resume, 6.6.y gives the firmware the previous M3 image whenever the new one is the same size or smaller. A larger image is reallocated and loaded.

Replace the early return with the upstream goto skip_m3_alloc. The previous patch, a backport of upstream commit 00575bb44b2c ("wifi:
ath12k: fix reusing m3 memory"), keeps the allocation size in total_size. So copying a smaller image no longer changes the size passed to dma_free_coherent().

Quoted 3 lines
Fixes 9a38ba5561d9 ("wifi: ath12k: check M3 buffer size as well whey trying to reuse it")
Assisted-by LLM
maintainer Applied
#5 lore raw

Re: [PATCH 6.6.y 2/2] wifi: ath12k: copy M3 firmware when reusing the buffer

Quoted 1 line

Queued the series for 6.6, thanks.

Signature · 2 lines
Thanks,
Sasha