mac80211: fix calling ieee80211_free_txskb with NULL skb
PATCH 1/2 mac80211: fix calling ieee80211_free_txskb with NULL skb
As we check sta->ps_tx_buf[ac] length without lock, we can get not updated value. In the worst scenario queue could be currently empty and we can see length bigger than STA_MAX_TX_BUFFER. When this happen skb_dequeue() will return NULL and we will crash trying to free NULL skb:
[ 6335.697678] BUG: unable to handle kernel NULL pointer dereference at 000000ac [ 6335.697858] IP: [<fefcdc70>] ieee80211_report_used_skb+0x10/0x1e0 [mac80211] ... [ 6335.700223] Call Trace: [ 6335.700599] [<fefcde55>] ieee80211_free_txskb+0x15/0x20 [mac80211] [ 6335.700697] [<fefef242>] invoke_tx_handlers+0x1322/0x1370 [mac80211] [ 6335.700787] [<c13551b6>] ? dev_hard_start_xmit+0x2b6/0x510 [ 6335.700879] [<fefef3fb>] ? ieee80211_tx_prepare+0x16b/0x330 [mac80211] [ 6335.700981] [<fefef626>] ieee80211_tx+0x66/0xe0 [mac80211] [ 6335.701072] [<fefeff43>] ieee80211_xmit+0x93/0xf0 [mac80211] [ 6335.701163] [<feff0d45>] ieee80211_subif_start_xmit+0xab5/0xbc0 [mac80211] [ 6335.701258] [<c13551b6>] dev_hard_start_xmit+0x2b6/0x510
To fix this problem, recheck queue length with lock taken.
Bug report:
http://bugzilla.kernel.org/show_bug.cgi?id=70551#c11
--- net/mac80211/tx.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
net/mac80211/tx.c +10 -2
@@ -472,17 +472,25 @@ ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx) test_sta_flag(sta, WLAN_STA_PS_DRIVER)) && !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {int ac =skb_get_queue_mapping (tx->skb); +struct sk_buff *old_skb =NULL ; +unsigned long flags;ps_dbg (sta->sdata,"STA %pM aid %d: PS buffer for AC %d\n" , sta->sta.addr, sta->sta.aid, ac);if (tx->local->total_ps_buffered >=TOTAL_MAX_TX_BUFFER )purge_old_ps_buffers (tx->local);if (skb_queue_len (&sta->ps_tx_buf[ac]) >=STA_MAX_TX_BUFFER ) { -struct sk_buff *old =skb_dequeue (&sta->ps_tx_buf[ac]); +spin_lock_irqsave (&sta->ps_tx_buf[ac].lock, flags); +/* queue could be modified, recheck length with lock taken */ +if (skb_queue_len (&sta->ps_tx_buf[ac]) >=STA_MAX_TX_BUFFER ) + old_skb =__skb_dequeue (&sta->ps_tx_buf[ac]); +spin_unlock_irqrestore (&sta->ps_tx_buf[ac].lock, flags); + } +if (old_skb) {ps_dbg (tx->sdata,"STA %pM TX buffer for AC %d full - dropping oldest frame\n" , sta->sta.addr, ac); -ieee80211_free_txskb (&local->hw, old); +ieee80211_free_txskb (&local->hw, old_skb); }else tx->local->total_ps_buffered++;
Signature · 1 line
1.7.11.7
PATCH 2/2 mac80211: protect skb_queue_len(&ps->bc_buf) by lock
Similar change as on current patch "mac80211: fix calling ieee80211_free_txskb with NULL skb", but for multicast queue. Patch does not prevent crash, as dev_kfree_skb() checks against NULL skb, but it help to prevent not necessary frame drop, when bc_buf queue was partially flushed and no longer exceeds AP_MAX_BC_BUFFER .
--- net/mac80211/tx.c | 9 +++++++++ 1 file changed, 9 insertions(+)
net/mac80211/tx.c +9 -0
@@ -387,6 +387,8 @@ ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)struct ieee80211_tx_info *info =IEEE80211_SKB_CB (tx->skb);struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;struct ps_data *ps; +struct sk_buff *old_skb =NULL ; +unsigned long flags;/* * broadcast/multicast frame
@@ -432,6 +434,13 @@ ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)purge_old_ps_buffers (tx->local);if (skb_queue_len (&ps->bc_buf) >=AP_MAX_BC_BUFFER ) { +spin_lock_irqsave (&ps->bc_buf.lock, flags); +/* queue could be modified, recheck length with lock taken */ +if (skb_queue_len (&ps->bc_buf) >=AP_MAX_BC_BUFFER ) + old_skb =__skb_dequeue (&ps->bc_buf); +spin_unlock_irqrestore (&ps->bc_buf.lock, flags); + } +if (old_skb) {ps_dbg (tx->sdata,"BC TX buffer full - dropping the oldest frame\n" );dev_kfree_skb (skb_dequeue (&ps->bc_buf));
Signature · 1 line
1.7.11.7
Johannes Berg
maintainerRe: [PATCH 2/2] mac80211: protect skb_queue_len(&ps->bc_buf) by lock
Quoted 5 lines · Stanislaw Gruszka
Similar change as on current patch "mac80211: fix calling
ieee80211_free_txskbwith NULL skb", but for multicast queue. Patch does not prevent crash, asdev_kfree_skb()checks against NULL skb, but it help to prevent not necessary frame drop, whenbc_bufqueue was partially flushed and no longer exceedsAP_MAX_BC_BUFFER.
I don't think this makes sense. It doesn't really change anything, holding a spinlock isn't something magic that makes other things go away, so instead of
* check queue length, is >= limit
* free frame from queue, even if somebody else is dequeuing as well
as before, you'd just have
* check queue length, is >= limit
* take lock
* check queue length, is >= limit
* drop frame
* unlock
* somebody else who was dequeuing now wakes up from waiting on the lock
and
finds no frame there
It ultimately makes no difference at all, it just makes this code more difficult to read and understand.
johannes
Re: [PATCH 2/2] mac80211: protect skb_queue_len(&ps->bc_buf) by lock
Quoted 24 lines · Johannes Berg
Quoted 5 lines · Stanislaw Gruszka
Similar change as on current patch "mac80211: fix calling
ieee80211_free_txskbwith NULL skb", but for multicast queue. Patch does not prevent crash, asdev_kfree_skb()checks against NULL skb, but it help to prevent not necessary frame drop, whenbc_bufqueue was partially flushed and no longer exceedsAP_MAX_BC_BUFFER.I don't think this makes sense. It doesn't really change anything, holding a spinlock isn't something magic that makes other things go away, so instead of
* check queue length, is >= limit
* free frame from queue, even if somebody else is dequeuing as wellas before, you'd just have
* check queue length, is >= limit
* take lock
* check queue length, is >= limit
* drop frame
* unlock
* somebody else who was dequeuing now wakes up from waiting on the lock
and
finds no frame there
Quoted 2 lines
It ultimately makes no difference at all, it just makes this code more difficult to read and understand.
It make difference when queue length value is modified on different CPU and read on different CPU. Without lock you can 'see' old length value on CPU that run ieee80211_tx_h_multicast_ps_buf() for undefined period of time (ok maybe not undefined on x86), and current oldest frame can be not necessarily dropped.
I can remove first call skb_queue_len(&ps->bc_buf) and take spinlock unconditionally, will that help with code readability ?
Stanislaw
Johannes Berg
maintainerRe: [PATCH 2/2] mac80211: protect skb_queue_len(&ps->bc_buf) by lock
Quoted 8 lines · Stanislaw Gruszka
Quoted 2 lines
It ultimately makes no difference at all, it just makes this code more difficult to read and understand.
It make difference when queue length value is modified on different CPU and read on different CPU. Without lock you can 'see' old length value on CPU that run
ieee80211_tx_h_multicast_ps_buf()for undefined period of time (ok maybe not undefined on x86), and current oldest frame can be not necessarily dropped.
I don't see how that can be true, since the modifications of the queue length are under spinlock with the implied memory barriers.
johannes
Re: [PATCH 2/2] mac80211: protect skb_queue_len(&ps->bc_buf) by lock
Quoted 5 lines · Johannes Berg
Quoted 3 lines · Stanislaw Gruszka
Quoted 2 lines
It ultimately makes no difference at all, it just makes this code more difficult to read and understand.
Quoted 8 lines
Quoted 5 lines
It make difference when queue length value is modified on different CPU and read on different CPU. Without lock you can 'see' old length value on CPU that run
ieee80211_tx_h_multicast_ps_buf()for undefined period of time (ok maybe not undefined on x86), and current oldest frame can be not necessarily dropped.I don't see how that can be true, since the modifications of the queue length are under spinlock with the implied memory barriers.
On processor that read value there is no memory barrier. If you do:
CPU1 CPU2
b = a;
spin_lock(a_lock)
a = 1; ...spin_unlock(a_lock)
b = a;
There is nothing that guarantee that on CPU2 b will be 1.
Stanislaw
Johannes Berg
maintainerRe: [PATCH 2/2] mac80211: protect skb_queue_len(&ps->bc_buf) by lock
Quoted 19 lines · Stanislaw Gruszka
Quoted 5 lines · Johannes Berg
Quoted 3 lines · Stanislaw Gruszka
Quoted 2 lines
It ultimately makes no difference at all, it just makes this code more difficult to read and understand.
Quoted 8 lines
Quoted 5 lines
It make difference when queue length value is modified on different CPU and read on different CPU. Without lock you can 'see' old length value on CPU that run
ieee80211_tx_h_multicast_ps_buf()for undefined period of time (ok maybe not undefined on x86), and current oldest frame can be not necessarily dropped.I don't see how that can be true, since the modifications of the queue length are under spinlock with the implied memory barriers.
On processor that read value there is no memory barrier. If you do:
CPU1 CPU2
Quoted 8 lines
b = a;
spin_lock(a_lock)
a = 1; ...spin_unlock(a_lock)b = a;There is nothing that guarantee that on CPU2 b will be 1.
Well, OK, so there's some small window of time where the drop code isn't perfect. Does it matter? I'd argue it doesn't - if you're getting close to the drop case then the client is probably misbehaving anyway.
johannes
Re: [PATCH 2/2] mac80211: protect skb_queue_len(&ps->bc_buf) by lock
Quoted 3 lines · Johannes Berg
Well, OK, so there's some small window of time where the drop code isn't perfect. Does it matter? I'd argue it doesn't - if you're getting close to the drop case then the client is probably misbehaving anyway.
Ok, let drop those patches. Do you want me to repost first patch with just if(old) check to fix kaboom, or you prefer to problem be solved properly at once ?
Stanislaw
Johannes Berg
maintainerRe: [PATCH 2/2] mac80211: protect skb_queue_len(&ps->bc_buf) by lock
Quoted 8 lines · Stanislaw Gruszka
Quoted 3 lines · Johannes Berg
Well, OK, so there's some small window of time where the drop code isn't perfect. Does it matter? I'd argue it doesn't - if you're getting close to the drop case then the client is probably misbehaving anyway.
Ok, let drop those patches. Do you want me to repost first patch with just if(old) check to fix kaboom, or you prefer to problem be solved properly at once ?
Emmanuel posted his patch, so I think I'll just pick that up. Care to review it?
johannes
Re: [PATCH 2/2] mac80211: protect skb_queue_len(&ps->bc_buf) by lock
In reply to Stanislaw GruszkaQuoted 8 lines · Stanislaw Gruszka
Quoted 3 lines · Johannes Berg
Well, OK, so there's some small window of time where the drop code isn't perfect. Does it matter? I'd argue it doesn't - if you're getting close to the drop case then the client is probably misbehaving anyway.
Ok, let drop those patches. Do you want me to repost first patch with just if(old) check to fix kaboom, or you prefer to problem be solved properly at once ?
Nevermind, I just saw Emmanuel posted proper patch.
Stanislaw
Johannes Berg
maintainer+Emmanuel
Interesting. We just ran into the same issue as well.
Quoted 6 lines
net/mac80211/tx.c +2
-0+struct sk_buff *old_skb =NULL ; +unsigned long flags;ps_dbg (sta->sdata,"STA %pM aid %d: PS buffer for AC %d\n" , sta->sta.addr, sta->sta.aid, ac);if (tx->local->total_ps_buffered >=TOTAL_MAX_TX_BUFFER )
net/mac80211/tx.c ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx) · line 489
Quoted 10 lines
net/mac80211/tx.c +7
-1purge_old_ps_buffers (tx->local);if (skb_queue_len (&sta->ps_tx_buf[ac]) >=STA_MAX_TX_BUFFER ) { -struct sk_buff *old =skb_dequeue (&sta->ps_tx_buf[ac]); +spin_lock_irqsave (&sta->ps_tx_buf[ac].lock, flags); +/* queue could be modified, recheck length with lock taken */ +if (skb_queue_len (&sta->ps_tx_buf[ac]) >=STA_MAX_TX_BUFFER ) + old_skb =__skb_dequeue (&sta->ps_tx_buf[ac]); +spin_unlock_irqrestore (&sta->ps_tx_buf[ac].lock, flags); + } +if (old_skb) {
I think that's pointless, you can just say "if (old)" instead as the dequeue would return NULL.
In any case, while this solves the crash which is a good thing, it still leaves the code buggy. This crash seems to occur in the following racy scenario:
* station is sleeping
* frame TX to station begins
* station wakes up
* frame TX goes into the queue length check, finds long queue
* pending frames are transmitted
* queue is now empty
* old = skb_dequeue() returns NULL
* *kaboom*
The problem is that you're just fixing the "*kaboom*" part, so the code will continue like this:
* old is NULL
* no kaboom
* new frame is queued on ps_tx_buf queue
* frame never gets transmitted
johannes
PiANCj4gK0VtbWFudWVsDQo+IA0KPiBJbnRlcmVzdGluZy4gV2UganVzdCByYW4gaW50byB0aGUg c2FtZSBpc3N1ZSBhcyB3ZWxsLg0KPiANCj4gPiArCQlzdHJ1Y3Qgc2tfYnVmZiAqb2xkX3NrYiA9 IE5VTEw7DQo+ID4gKwkJdW5zaWduZWQgbG9uZyBmbGFnczsNCj4gPg0KPiA+ICAJCXBzX2RiZyhz dGEtPnNkYXRhLCAiU1RBICVwTSBhaWQgJWQ6IFBTIGJ1ZmZlciBmb3IgQUMNCj4gJWRcbiIsDQo+ ID4gIAkJICAgICAgIHN0YS0+c3RhLmF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iAgKiBmcmFtZSBuZXZlciBnZXRzIHRyYW5zbWl0dGVk DQo+ID4NCj4gPiBqb2hhbm5lcw0KDQo=
Quoted 7 lines · Johannes Berg
+Emmanuel
Interesting. We just ran into the same issue as well.
Quoted 3 lines
net/mac80211/tx.c +2
-0+struct sk_buff *old_skb =NULL ; +unsigned long flags;
Quoted 16 lines
net/mac80211/tx.c ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx) · line 489
Quoted 13 lines
net/mac80211/tx.c +7
-1ps_dbg (sta->sdata,"STA %pM aid %d: PS buffer for AC %d\n" , sta->sta.addr, sta->sta.aid, ac);if (tx->local->total_ps_buffered >=TOTAL_MAX_TX_BUFFER )purge_old_ps_buffers (tx->local);if (skb_queue_len (&sta->ps_tx_buf[ac]) >=STA_MAX_TX_BUFFER ) { -struct sk_buff *old =skb_dequeue (&sta->ps_tx_buf[ac]); +spin_lock_irqsave (&sta->ps_tx_buf[ac].lock, flags); +/* queue could be modified, recheck length with lock taken */ +if (skb_queue_len (&sta->ps_tx_buf[ac]) >=STA_MAX_TX_BUFFER ) + old_skb =__skb_dequeue (&sta->ps_tx_buf[ac]); +spin_unlock_irqrestore (&sta->ps_tx_buf[ac].lock, flags); + } +if (old_skb) {I think that's pointless, you can just say "if (old)" instead as the dequeue would return NULL.
I think it helps if queue was only partially empted, then we do not drop oldest frame.
Quoted 13 lines
In any case, while this solves the crash which is a good thing, it still leaves the code buggy. This crash seems to occur in the following racy scenario:
* station is sleeping
* frame TX to station begins
* station wakes up
* frame TX goes into the queue length check, finds long queue
* pending frames are transmitted
* queue is now empty
* old =skb_dequeue()returns NULL
* *kaboom*
Quoted 7 lines
The problem is that you're just fixing the "*kaboom*" part, so the code will continue like this:
* old is NULL
* no kaboom
* new frame is queued onps_tx_bufqueue
* frame never gets transmitted
When started to look at that code I found at least 3 bugs, but miss this one :-)
Why frame will not be transmitted, we are disabling PS, but buffers stays not empty ?
Stanislaw
Quoted 21 lines · Stanislaw Gruszka
Quoted 13 lines
In any case, while this solves the crash which is a good thing, it still leaves the code buggy. This crash seems to occur in the following racy scenario:
* station is sleeping
* frame TX to station begins
* station wakes up
* frame TX goes into the queue length check, finds long queue
* pending frames are transmitted
* queue is now empty
* old =skb_dequeue()returns NULL
* *kaboom*Quoted 7 lines
The problem is that you're just fixing the "*kaboom*" part, so the code will continue like this:
* old is NULL
* no kaboom
* new frame is queued onps_tx_bufqueue
* frame never gets transmitted
Quoted 5 lines
When started to look at that code I found at least 3 bugs, but miss this one :-)
Why frame will not be transmitted, we are disabling PS, but buffers stays not empty ?
Ok, I think I see this, it seems to be race condition in ieee80211_sta_ps_deliver_wakeup().
Perhaps it could be solved by modifying ieee80211_add_pending_skbs_fn() to take list of queues as argument, that function seems properly stop queues, add buffered frames to pending queue, clear WLAN_STA_PS_STA and then wake up queues. Or just stop using ieee80211_add_pending_skbs_fn() and do the same sequence directly on ieee80211_sta_ps_deliver_wakeup() .
Stanislaw
Johannes Berg
maintainerQuoted 9 lines · Stanislaw Gruszka
Ok, I think I see this, it seems to be race condition in
ieee80211_sta_ps_deliver_wakeup().Perhaps it could be solved by modifying
ieee80211_add_pending_skbs_fn()to take list of queues as argument, that function seems properly stop queues, add buffered frames to pending queue, clearWLAN_STA_PS_STAand then wake up queues. Or just stop usingieee80211_add_pending_skbs_fn()and do the same sequence directly onieee80211_sta_ps_deliver_wakeup().
That might work, but we were considering just locking the sta->lock around the whole "pending" thing inside deliver_wakeup() and in the "is station sleeping" part. Then you also need to re-check ("is it still sleeping?") inside the lock.
johannes
Quoted 12 lines · Johannes Berg
Quoted 9 lines · Stanislaw Gruszka
Ok, I think I see this, it seems to be race condition in
ieee80211_sta_ps_deliver_wakeup().Perhaps it could be solved by modifying
ieee80211_add_pending_skbs_fn()to take list of queues as argument, that function seems properly stop queues, add buffered frames to pending queue, clearWLAN_STA_PS_STAand then wake up queues. Or just stop usingieee80211_add_pending_skbs_fn()and do the same sequence directly onieee80211_sta_ps_deliver_wakeup().
Quoted 4 lines
That might work, but we were considering just locking the
sta->lockaround the whole "pending" thing insidedeliver_wakeup()and in the "is station sleeping" part. Then you also need to re-check ("is it still sleeping?") inside the lock.
Looks good to me, feel free to post that.
Stanislaw