wifi: carl9170: take the URB reference before queueing the TX frame
Subsystems (2)
PATCH 1/1 wifi: carl9170: take the URB reference before queueing the TX frame
carl9170_tx() puts a frame on ar->tx_status[] while it holds only its initial reference and takes the reference for the URB afterwards. A TX status for the frame's cookie that is processed on another CPU in between drops the only reference, and mac80211 frees the frame. The driver then increments the dead refcount and submits the freed skb as URB buffer and context.
A device can hit this window by reporting TX status for the small, predictable cookies while the host transmits, which gives a KASAN slab-use-after-free in carl9170_usb_tx() and carl9170_tx_callback().
Take the reference before the frame is queued, as the driver did before commit caf1eae20668 ("carl9170: improve unicast PS buffering").
The bug was found, and the reproducer and this fix were written, by AI agents (Claude Mythos and Claude Opus models). The analysis and the patch were reviewed by a human.
---
An emulated AR9170 that answers TX status for cookies 1..4 while the host sends traffic (an unprivileged UDP socket is enough) triggers "refcount_t: addition on 0" and KASAN use-after-free on v7.3.1 and "kernel BUG at lib/refcount.c:34" on master 666539eecdf4. With the fix:
no report in 68 patched trigger boots (9 on xHCI, 9 on master); traffic with a well-behaved device (also HT/A-MPDU) is unchanged. Reproducer available on request.
Applies to master 4405c18c95d2 (tx.c unchanged since the tests).
drivers/net/wireless/ath/carl9170/tx.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/net/wireless/ath/carl9170/tx.c +9 -7
@@ -1364,13 +1364,6 @@ static void carl9170_tx(struct ar9170 *ar)atomic_inc (&ar->tx_total_pending); - q =__carl9170_get_queue (ar, i); -/* -* NB: tx_status[i] vs. tx_status[q], -* TODO: Move into pick_skb or alloc_dev_space. -*/ -skb_queue_tail (&ar->tx_status[q], skb); -/* * increase ref count to "2". * Ref counting is the easiest way to solve the
@@ -1378,9 +1371,18 @@ static void carl9170_tx(struct ar9170 *ar)* carl9170_tx_callback * and wlan tx status functions: * carl9170_tx_status/janitor. +* It must be taken before the frame is queued on +* tx_status[], where the status path can find it. */ carl9170_tx_get_skb (skb); + q =__carl9170_get_queue (ar, i); +/* +* NB: tx_status[i] vs. tx_status[q], +* TODO: Move into pick_skb or alloc_dev_space. +*/ +skb_queue_tail (&ar->tx_status[q], skb); +carl9170_usb_tx (ar, skb); schedule_garbagecollector = true; }
Signature · 1 line
2.53.0
Quoted 6 lines · Alexandre Becholey
carl9170_tx()puts a frame on ar->tx_status[] while it holds only its initial reference and takes the reference for the URB afterwards. A TX status for the frame's cookie that is processed on another CPU in between drops the only reference, and mac80211 frees the frame. The driver then increments the dead refcount and submits the freed skb as URB buffer and context.
Ok, so you have virtual AR9170 (as in USB-emulated device or did you make a custom firmware) that is injecting/generating tx_statuses with potential "future" cookies to arrive even before the driver gets to fully assign the cookie?
Yeah, I think I can see what you are doing there and why.
Quoted 7 lines
A device can hit this window by reporting TX status for the small, predictable cookies while the host transmits, which gives a KASAN slab-use-after-free in
carl9170_usb_tx()andcarl9170_tx_callback().Take the reference before the frame is queued, as the driver did before commit
caf1eae20668("carl9170: improve unicast PS buffering").
Quoted 6 lines
The bug was found, and the reproducer and this fix were written, by AI agents (Claude Mythos and Claude Opus models). The analysis and the patch were reviewed by a human.
Fixes caf1eae20668 ("carl9170: improve unicast PS buffering")
Quoted 3 lines
Signed-off-by Alexandre Becholey <[email protected]>---
An emulated AR9170 that answers TX status for cookies 1..4 while the
Do you really have emulated AR9170 like something similar to mac80211_hwsim? Cool! I'm not interested in the reproducer, but a even a semi-working simulator for the
AR9170: Now that would be sick!
Quoted 5 lines
host sends traffic (an unprivileged UDP socket is enough) triggers "
refcount_t: addition on 0" and KASAN use-after-free on v7.3.1 and "kernel BUG atlib/refcount.c:34" on master666539eecdf4. With the fix:
no report in 68 patched trigger boots (9 on xHCI, 9 on master); traffic with a well-behaved device (also HT/A-MPDU) is unchanged.
Am I reading this correctly? You have access to a real AR9170 hardware device too and this patch was tested there as well? If that's the case, why did you not add this to the commit message above? Because I think this belongs into the commit message. This is the "best" way to show everyone that you really did "dotted the i's and crossed the t's".
Question: What do you mean with (9 on xHCI, 9 on master)?
Do you mean 9 tests where successfully performed on real hardware (connected to a xHCI controller) And "9 on master" means that a patched linux on top of 666539eecdf4 [1] survived without triggering a use-after-free?
Cheers,
Christian
Quoted 43 lines
Reproducer available on request.
Applies to master4405c18c95d2(tx.cunchanged since the tests).drivers/net/wireless/ath/carl9170/tx.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)drivers/net/wireless/ath/carl9170/tx.c +9
-7@@ -1364,13 +1364,6 @@ static void carl9170_tx(struct ar9170 *ar)atomic_inc (&ar->tx_total_pending); - q =__carl9170_get_queue (ar, i); -/* -* NB: tx_status[i] vs. tx_status[q], -* TODO: Move into pick_skb or alloc_dev_space. -*/ -skb_queue_tail (&ar->tx_status[q], skb); -/* * increase ref count to "2". * Ref counting is the easiest way to solve the @@ -1378,9 +1371,18 @@ static void carl9170_tx(struct ar9170 *ar)* carl9170_tx_callback * and wlan tx status functions: * carl9170_tx_status/janitor. +* It must be taken before the frame is queued on +* tx_status[], where the status path can find it. */ carl9170_tx_get_skb (skb); + q =__carl9170_get_queue (ar, i); +/* +* NB: tx_status[i] vs. tx_status[q], +* TODO: Move into pick_skb or alloc_dev_space. +*/ +skb_queue_tail (&ar->tx_status[q], skb); +carl9170_usb_tx (ar, skb); schedule_garbagecollector = true; }
Quoted 8 lines · Christian Lamparter
Quoted 6 lines · Alexandre Becholey
carl9170_tx()puts a frame on ar->tx_status[] while it holds only its initial reference and takes the reference for the URB afterwards. A TX status for the frame's cookie that is processed on another CPU in between drops the only reference, and mac80211 frees the frame. The driver then increments the dead refcount and submits the freed skb as URB buffer and context.
Quoted 6 lines
Ok, so you have virtual AR9170 (as in USB-emulated device or did you make a custom firmware) that is injecting/generating
tx_statuseswith potential "future" cookies to arrive even before the driver gets to fully assign the cookie?Yeah, I think I can see what you are doing there and why.
Yes, a USB-emulated device, no custom firmware. It is a userspace program on raw-gadget with dummy_hcd, or attached to QEMU's xHCI through usb-redir. It accepts the firmware download and then answers the way the carl9170 firmware does: BOOT trap, command responses, the EEPROM through register reads, and a TXCOMP trap for each uploaded frame. For this bug it also sends TXCOMP for cookies 1..4 while the host is transmitting.
Quoted 16 lines
Quoted 8 lines
A device can hit this window by reporting TX status for the small, predictable cookies while the host transmits, which gives a KASAN slab-use-after-free in
carl9170_usb_tx()andcarl9170_tx_callback().Take the reference before the frame is queued, as the driver did before commit
caf1eae20668("carl9170: improve unicast PS buffering").Quoted 6 lines
The bug was found, and the reproducer and this fix were written, by AI agents (Claude Mythos and Claude Opus models). The analysis and the patch were reviewed by a human.
Fixes caf1eae20668 ("carl9170: improve unicast PS buffering")
Quoted 7 lines
Quoted 3 lines
Signed-off-by Alexandre Becholey <[email protected]>---
An emulated AR9170 that answers TX status for cookies 1..4 while theDo you really have emulated AR9170 like something similar to
mac80211_hwsim? Cool! I'm not interested in the reproducer, but a even a semi-working simulator for the
AR9170: Now that would be sick!
Not that far. It plays an open access point, so the host can bring wlan0 up, associate and send traffic, including ADDBA and A-MPDU, but there is no PHY behind it and it only covers what the tests need. It is about 1500 lines of C; I can send it to you off-list if it is useful.
Quoted 7 lines
Quoted 6 lines
host sends traffic (an unprivileged UDP socket is enough) triggers "
refcount_t: addition on 0" and KASAN use-after-free on v7.3.1 and "kernel BUG atlib/refcount.c:34" on master666539eecdf4. With the fix:
no report in 68 patched trigger boots (9 on xHCI, 9 on master); traffic with a well-behaved device (also HT/A-MPDU) is unchanged.
Quoted 5 lines
Am I reading this correctly? You have access to a real AR9170 hardware device too and this patch was tested there as well? If that's the case, why did you not add this to the commit message above? Because I think this belongs into the commit message. This is the "best" way to show everyone that you really did "dotted the i's and crossed the t's".
No, sorry, that was unclear. The "well-behaved device" is the same emulator in a mode where it reports TX status only after the frame was uploaded, as the firmware does. The patch has not been tested on real hardware.
Quoted 2 lines
Question: What do you mean with (9 on xHCI, 9 on master)?
All 68 boots were QEMU virtual machines (arm64) running a kernel with the patch. 9 of them attached the emulated device to QEMU's xHCI controller instead of dummy_hcd, and 9 ran torvalds/master 666539eecdf4 with the patch instead of v7.3.1. None of them showed the refcount warning or a KASAN report, while the same reproducer triggers it in most boots without the patch. On an instrumented build we also checked that the early TX status really arrives in the window between queueing and carl9170_usb_tx() with the patch applied.
Thanks for the review and the ack.
Alexandre
Quoted 56 lines
Do you mean 9 tests where successfully performed on real hardware (connected to a xHCI controller) And "9 on master" means that a patched linux on top of
666539eecdf4[1] survived without triggering a use-after-free?Cheers,
ChristianQuoted 43 lines
Reproducer available on request.
Applies to master4405c18c95d2(tx.cunchanged since the tests).drivers/net/wireless/ath/carl9170/tx.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)drivers/net/wireless/ath/carl9170/tx.c +9
-7@@ -1364,13 +1364,6 @@ static void carl9170_tx(struct ar9170 *ar)atomic_inc (&ar->tx_total_pending); - q =__carl9170_get_queue (ar, i); -/* -* NB: tx_status[i] vs. tx_status[q], -* TODO: Move into pick_skb or alloc_dev_space. -*/ -skb_queue_tail (&ar->tx_status[q], skb); -/* * increase ref count to "2". * Ref counting is the easiest way to solve the @@ -1378,9 +1371,18 @@ static void carl9170_tx(struct ar9170 *ar)* carl9170_tx_callback * and wlan tx status functions: * carl9170_tx_status/janitor. +* It must be taken before the frame is queued on +* tx_status[], where the status path can find it. */ carl9170_tx_get_skb (skb); + q =__carl9170_get_queue (ar, i); +/* +* NB: tx_status[i] vs. tx_status[q], +* TODO: Move into pick_skb or alloc_dev_space. +*/ +skb_queue_tail (&ar->tx_status[q], skb); +carl9170_usb_tx (ar, skb); schedule_garbagecollector = true; }