[PATCH AUTOSEL 6.17-6.12] wifi: mt76: mt7996: fix memory leak on mt7996_mcu_sta_key_tlv error

From: Sasha Levin
Date: Sat Oct 25 2025 - 12:31:16 EST


From: Felix Fietkau <nbd@xxxxxxxx>

[ Upstream commit 7c0f63fe37a5da2c13fc35c89053b31be8ead895 ]

Free the allocated skb on error

Link: https://patch.msgid.link/20250915075910.47558-5-nbd@xxxxxxxx
Signed-off-by: Felix Fietkau <nbd@xxxxxxxx>
Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
---

LLM Generated explanations, may be completely bogus:

## BACKPORT RECOMMENDATION: **YES**

Based on my comprehensive analysis, this commit **SHOULD be backported**
to stable kernel trees.

---

## DETAILED ANALYSIS

### **Summary of the Bug**

The commit fixes a memory leak in the mt7996 MediaTek WiFi 7 driver. In
the function `mt7996_mcu_add_key()`
(drivers/net/wireless/mediatek/mt76/mt7996/mcu.c:2590), when
`mt7996_mcu_sta_key_tlv()` returns an error, the function was returning
immediately without freeing the previously allocated socket buffer
(`skb`).

### **Code Analysis**

**Before the fix (lines 2603-2605):**
```c
ret = mt7996_mcu_sta_key_tlv(wcid, skb, key, cmd);
if (ret)
return ret; // MEMORY LEAK: skb is not freed
```

**After the fix (lines 2603-2607):**
```c
ret = mt7996_mcu_sta_key_tlv(wcid, skb, key, cmd);
if (ret) {
dev_kfree_skb(skb); // Properly free the allocated memory
return ret;
}
```

### **When the Leak Occurs**

The `mt7996_mcu_sta_key_tlv()` function returns errors in two specific
scenarios:

1. **Line 2552**: Returns `-EOPNOTSUPP` when `cipher == MCU_CIPHER_NONE`
(unsupported cipher type)
2. **Line 2582**: Returns `-EOPNOTSUPP` for beacon protection keys
(keyidx 6 or 7) using unsupported cipher suites (anything other than
AES-CMAC, BIP-GMAC-128, or BIP-GMAC-256)

Each leak would be of size `MT7996_STA_UPDATE_MAX_SIZE` (approximately
several hundred bytes to a few KB, depending on the sum of multiple
structure sizes).

### **Impact Assessment**

**Severity: MODERATE to HIGH**

1. **User Impact**: Memory leaks can gradually degrade system stability,
especially on systems with limited memory or long uptimes. Each
failed key configuration leaks memory that cannot be reclaimed until
reboot.

2. **Trigger Conditions**: The leak occurs during WiFi key configuration
operations, which happen:
- During station association with access points
- During key rotation operations
- When unsupported cipher suites are requested (could be
configuration errors or attack attempts)
- When beacon protection keys use unsupported ciphers

3. **Frequency**: While the error conditions are relatively uncommon in
normal operation, they could be triggered:
- By misconfigured wireless networks
- During compatibility issues with certain access points
- Potentially by malicious actors attempting to exhaust system memory
- In enterprise environments with frequent key rotations

4. **Security Implications**: While no CVE has been assigned, kernel-
level memory leaks in WiFi drivers are security-relevant because:
- They operate at kernel privilege level
- They can lead to denial-of-service through memory exhaustion
- WiFi drivers process unauthenticated network frames
- The mt76 driver family has had other security-related memory leak
fixes

### **Historical Context**

- **Bug Age**: This bug has existed since the mt7996 driver was first
introduced in commit `98686cd21624c` (November 22, 2022, merged in
v6.2-rc1)
- **Affected Versions**: All kernel versions from v6.2 onwards
(approximately 2.5 years)
- **Fix Date**: September 15, 2025 (approximately 1 month ago)
- **Related Fixes**: Part of a series of key management improvements by
Felix Fietkau, including other key-related fixes around the same
timeframe

### **Backporting Assessment**

**Positive Factors for Backporting:**

1. ✅ **Fixes Important Bug**: Memory leaks affect system stability and
can lead to DoS
2. ✅ **Small, Contained Change**: Only 3 lines added (+2, -0, modified
braces)
3. ✅ **Clear, Straightforward Fix**: Classic error path resource cleanup
pattern
4. ✅ **No Architectural Changes**: Pure bug fix with no design changes
5. ✅ **Minimal Regression Risk**: Adding cleanup on error path is safe
6. ✅ **Long-Standing Bug**: Has affected users for 2.5 years across many
kernel versions
7. ✅ **Isolated to Subsystem**: Confined to mt7996 driver, no impact on
other components
8. ✅ **Clean Application**: Function signature hasn't changed, should
apply cleanly to older kernels
9. ✅ **Part of Quality Effort**: Matches pattern of ongoing memory leak
fixes in mt76 driver family

**Potential Concerns (All Low Risk):**

1. ⚠️ **No Fixes Tag**: The commit lacks a "Fixes:" tag, which is
typically expected for backports (but this is a documentation issue,
not a technical one)
2. ⚠️ **No Stable CC**: No "Cc: stable@xxxxxxxxxxxxxxx" in commit
message (again, documentation issue)
3. ⚠️ **Limited Test Coverage**: As a relatively new WiFi 7 driver,
there may be fewer users testing this specific hardware

### **Risk Analysis**

**Risk of Backporting: VERY LOW**

- The change only affects an error path that was previously incorrect
- Adding `dev_kfree_skb()` on error is the standard kernel pattern and
cannot introduce new bugs
- The fix doesn't change any logic, just adds missing cleanup
- If the error path was not being hit before, this change has zero
effect
- If the error path was being hit, this fix only helps (prevents memory
leak)

**Risk of NOT Backporting: MODERATE**

- Continued memory leaks on affected systems
- Potential stability issues in production environments
- Possible security implications if leak can be triggered remotely
- Users on stable kernels won't benefit from this fix for years

### **Stable Tree Applicability**

This commit should be backported to all stable trees that include the
mt7996 driver:
- ✅ v6.17.x (current)
- ✅ v6.16.x
- ✅ v6.15.x
- ✅ v6.14.x
- ✅ v6.13.x
- ✅ v6.12.x (LTS)
- ✅ v6.11.x
- ✅ v6.10.x
- ✅ v6.9.x
- ✅ v6.8.x
- ✅ v6.7.x
- ✅ v6.6.x (LTS)
- ✅ v6.5.x
- ✅ v6.4.x
- ✅ v6.3.x
- ✅ v6.2.x (where mt7996 was introduced)

**Priority: HIGH** for LTS kernels (6.12.x, 6.6.x) as these will be
maintained for years and have the most users.

### **Similar Precedents**

The Linux kernel has a strong history of backporting similar memory leak
fixes:
- Multiple mt76 driver memory leak fixes have been backported previously
- Other MediaTek driver leak fixes (mt7663u, mt76x02u) were backported
- Kernel memory leaks in drivers are routinely fixed in stable trees

### **Conclusion**

This is a textbook example of a commit that **should be backported**:
- Important bug (memory leak affecting stability)
- Minimal change (3 lines)
- No architectural modifications
- Extremely low risk
- Affects multiple stable kernel versions
- Clear benefit to users

**RECOMMENDATION: Backport to all stable trees from v6.2 onwards, with
priority for LTS releases.**

drivers/net/wireless/mediatek/mt76/mt7996/mcu.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/mcu.c b/drivers/net/wireless/mediatek/mt76/mt7996/mcu.c
index aad58f7831c7b..0d688ec5a8163 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7996/mcu.c
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/mcu.c
@@ -2535,8 +2535,10 @@ int mt7996_mcu_add_key(struct mt76_dev *dev, struct ieee80211_vif *vif,
return PTR_ERR(skb);

ret = mt7996_mcu_sta_key_tlv(wcid, skb, key, cmd);
- if (ret)
+ if (ret) {
+ dev_kfree_skb(skb);
return ret;
+ }

return mt76_mcu_skb_send_msg(dev, skb, mcu_cmd, true);
}
--
2.51.0