udp: on peeking bad csum, drop packets even if not at head
Subsystems (4)
Willem de Bruijn
maintainerPATCH 1/1 udp: on peeking bad csum, drop packets even if not at head
From: Willem de Bruijn <[email protected]>
When peeking, if a bad csum is discovered, the skb is unlinked from the queue with __sk_queue_drop_skb and the peek operation restarted.
__sk_queue_drop_skb only drops packets that match the queue head. With sk_peek_off, the skb need not be at head, causing the call to fail and the same skb to be found again on restart.
Walk the queue to find the correct skb. Limit the walk to sk_peek_off, to bound cycle cost to at most twice the original skb_queue_walk in __skb_try_recv_from_queue.
The operation may race with updates to sk_peek_off. As the operation is retried, it will eventually succeed.
---
Simpler would be to check (skb->csum_complete_sw && !sbk->csum_valid) in __skb_try_recv_from_queue to ignore skbs with bad checksum. But __udp_lib_checksum_complete does not update those fields if called while peeking, because the skb is shared. I found no way around that.
--- net/core/datagram.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
net/core/datagram.c +9 -1
@@ -360,9 +360,17 @@ int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue,int err =0 ;if (flags &MSG_PEEK ) { +struct sk_buff *lskb; +int off =sk_peek_offset (sk, flags); + err = -ENOENT ;spin_lock_bh (&sk_queue->lock); -if (skb ==skb_peek (sk_queue)) { + lskb =skb_peek (sk_queue); +while (lskb != skb && lskb && off >= lskb->len) { + off -= lskb->len; + lskb =skb_peek_next (lskb, sk_queue); + } +if (lskb == skb) {__skb_unlink (skb, sk_queue);refcount_dec (&skb->users);if (destructor)
Signature · 1 line
2.14.1.480.gb18f417b89-goog
Willem de Bruijn
maintainerQuoted 9 lines · Willem de Bruijn
From: Willem de Bruijn <[email protected]>
When peeking, if a bad csum is discovered, the skb is unlinked from the queue with
__sk_queue_drop_skband the peek operation restarted.
__sk_queue_drop_skbonly drops packets that match the queue head. Withsk_peek_off, the skb need not be at head, causing the call to fail and the same skb to be found again on restart.
Quoted 8 lines
Walk the queue to find the correct skb. Limit the walk to
sk_peek_off, to bound cycle cost to at most twice the originalskb_queue_walkin__skb_try_recv_from_queue.The operation may race with updates to
sk_peek_off. As the operation is retried, it will eventually succeed.Signed-off-by Willem de Bruijn <[email protected]>
Eric just suggested an alternative that does not require looping, which is much nicer.
Quoted 9 lines · Willem de Bruijn
From: Willem de Bruijn <[email protected]>
When peeking, if a bad csum is discovered, the skb is unlinked from the queue with
__sk_queue_drop_skband the peek operation restarted.
__sk_queue_drop_skbonly drops packets that match the queue head. Withsk_peek_off, the skb need not be at head, causing the call to fail and the same skb to be found again on restart.
Quoted 8 lines
Walk the queue to find the correct skb. Limit the walk to
sk_peek_off, to bound cycle cost to at most twice the originalskb_queue_walkin__skb_try_recv_from_queue.The operation may race with updates to
sk_peek_off. As the operation is retried, it will eventually succeed.Signed-off-by Willem de Bruijn <[email protected]>
You forgot the Fixes: tag, that such a bug fix deserves.
I am not a big fan of your patch and would prefer a solution without the loop.
skb already have ->next and ->prev pointer telling us its position in the receive queue.
We only need to make sure we are the last owner of this skb before doing the check (ie cancel the kfree_skb() that usually follows the call to v__sk_queue_drop_skb() of skb->next being NULL or not.
Something like :
include/net/sock.h | 2 +- net/core/datagram.c | 22 ++++++++++++++-------- net/ipv4/udp.c | 2 +- net/ipv6/udp.c | 2 +- 4 files changed, 17 insertions(+), 11 deletions(-)
include/net/sock.h +1 -1
@@ -2030,7 +2030,7 @@ void sk_reset_timer(struct sock *sk, struct timer_list *timer,void sk_stop_timer (struct sock *sk,struct timer_list *timer);int __sk_queue_drop_skb (struct sock *sk,struct sk_buff_head *sk_queue, -struct sk_buff *skb,unsigned int flags, +struct sk_buff **pskb,unsigned int flags,void (*destructor)(struct sock *sk,struct sk_buff *skb));int __sock_queue_rcv_skb (struct sock *sk,struct sk_buff *skb);
net/core/datagram.c +14 -8
@@ -353,21 +353,27 @@ void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len)EXPORT_SYMBOL (__skb_free_datagram_locked);int __sk_queue_drop_skb (struct sock *sk,struct sk_buff_head *sk_queue, -struct sk_buff *skb,unsigned int flags, +struct sk_buff **pskb,unsigned int flags,void (*destructor)(struct sock *sk,struct sk_buff *skb)) {int err =0 ;if (flags &MSG_PEEK ) { +struct sk_buff *skb = *pskb; + err = -ENOENT ;spin_lock_bh (&sk_queue->lock); -if (skb ==skb_peek (sk_queue)) { -__skb_unlink (skb, sk_queue); -refcount_dec (&skb->users); -if (destructor) -destructor (sk, skb); - err =0 ; +refcount_dec (&skb->users); + *pskb =NULL ; +if (refcount_dec_if_one (&skb->users)) { +if (skb->next) { +__skb_unlink (skb, sk_queue); +if (destructor) +destructor (sk, skb); + err =0 ; + } +__kfree_skb (skb); }spin_unlock_bh (&sk_queue->lock); }
@@ -400,7 +406,7 @@ EXPORT_SYMBOL(__sk_queue_drop_skb);int skb_kill_datagram (struct sock *sk,struct sk_buff *skb,unsigned int flags) { -int err =__sk_queue_drop_skb (sk, &sk->sk_receive_queue, skb, flags, +int err =__sk_queue_drop_skb (sk, &sk->sk_receive_queue, &skb, flags,NULL );kfree_skb (skb);
net/ipv4/udp.c +1 -1
@@ -1648,7 +1648,7 @@ int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,return err; csum_copy_err: -if (!__sk_queue_drop_skb (sk, &udp_sk (sk)->reader_queue, skb, flags, +if (!__sk_queue_drop_skb (sk, &udp_sk (sk)->reader_queue, &skb, flags, udp_skb_destructor)) {UDP_INC_STATS (sock_net (sk),UDP_MIB_CSUMERRORS , is_udplite);UDP_INC_STATS (sock_net (sk),UDP_MIB_INERRORS , is_udplite);
net/ipv6/udp.c +1 -1
@@ -465,7 +465,7 @@ int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,return err; csum_copy_err: -if (!__sk_queue_drop_skb (sk, &udp_sk (sk)->reader_queue, skb, flags, +if (!__sk_queue_drop_skb (sk, &udp_sk (sk)->reader_queue, &skb, flags, udp_skb_destructor)) {if (is_udp4) { UDP_INC_STATS(sock_net(sk),
Willem de Bruijn
maintainerQuoted 14 lines · Eric Dumazet
Quoted 13 lines · Willem de Bruijn
From: Willem de Bruijn <[email protected]>
When peeking, if a bad csum is discovered, the skb is unlinked from the queue with
__sk_queue_drop_skband the peek operation restarted.
__sk_queue_drop_skbonly drops packets that match the queue head. Withsk_peek_off, the skb need not be at head, causing the call to fail and the same skb to be found again on restart.Walk the queue to find the correct skb. Limit the walk to
sk_peek_off, to bound cycle cost to at most twice the originalskb_queue_walkin__skb_try_recv_from_queue.
Quoted 6 lines
Quoted 4 lines
The operation may race with updates to
sk_peek_off. As the operation is retried, it will eventually succeed.Signed-off-by Willem de Bruijn <[email protected]>You forgot the Fixes: tag, that such a bug fix deserves.
Indeed, sorry. I'm looking into that now. It should be the patch that introduced peeking at offset, but need to verify.
I should also add that this bug was discovered by syzkaller.
Quoted 2 lines
I am not a big fan of your patch and would prefer a solution without the loop.
Agreed.
Willem de Bruijn
maintainerQuoted 18 lines · Willem de Bruijn
Quoted 17 lines · Eric Dumazet
Quoted 16 lines · Willem de Bruijn
From: Willem de Bruijn <[email protected]>
When peeking, if a bad csum is discovered, the skb is unlinked from the queue with
__sk_queue_drop_skband the peek operation restarted.
__sk_queue_drop_skbonly drops packets that match the queue head. Withsk_peek_off, the skb need not be at head, causing the call to fail and the same skb to be found again on restart.Walk the queue to find the correct skb. Limit the walk to
sk_peek_off, to bound cycle cost to at most twice the originalskb_queue_walkin__skb_try_recv_from_queue.The operation may race with updates to
sk_peek_off. As the operation is retried, it will eventually succeed.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Willem de Bruijn <[email protected]>You forgot the Fixes: tag, that such a bug fix deserves.
Indeed, sorry. I'm looking into that now. It should be the patch that introduced peeking at offset, but need to verify.
It is. Fixes: 627d2d6b5500 ("udp: enable MSG_PEEK at non-zero offset")
PATCH v2 1/1 udp: on peeking bad csum, drop packets even if not at head
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
When peeking, if a bad csum is discovered, the skb is unlinked from the queue with __sk_queue_drop_skb and the peek operation restarted.
__sk_queue_drop_skb only drops packets that match the queue head.
This fails if the skb was found after the head, using SO_PEEK_OFF socket option. This causes an infinite loop.
We MUST drop this problematic skb, and we can simply check if skb was already removed by another thread, by looking at skb->next :
This pointer is set to NULL by the __skb_unlink() operation, that might have happened only under the spinlock protection.
Many thanks to syzkaller team (and particularly Dmitry Vyukov who provided us nice C reproducers exhibiting the lockup) and Willem de Bruijn who provided first version for this patch and a test program.
--- net/core/datagram.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/datagram.c +1 -1
@@ -362,7 +362,7 @@ int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue,if (flags &MSG_PEEK ) { err = -ENOENT ;spin_lock_bh (&sk_queue->lock); -if (skb ==skb_peek (sk_queue)) { +if (skb->next) {__skb_unlink (skb, sk_queue);refcount_dec (&skb->users);if (destructor)
Quoted 34 lines · Eric Dumazet
From: Eric Dumazet <[email protected]>
When peeking, if a bad csum is discovered, the skb is unlinked from the queue with
__sk_queue_drop_skband the peek operation restarted.
__sk_queue_drop_skbonly drops packets that match the queue head.This fails if the skb was found after the head, using
SO_PEEK_OFFsocket option. This causes an infinite loop.We MUST drop this problematic skb, and we can simply check if skb was already removed by another thread, by looking at
skb->next:This pointer is set to NULL by the
__skb_unlink()operation, that might have happened only under the spinlock protection.Many thanks to syzkaller team (and particularly Dmitry Vyukov who provided us nice C reproducers exhibiting the lockup) and Willem de Bruijn who provided first version for this patch and a test program.
Fixes 627d2d6b5500 ("udp: enable MSG_PEEK at non-zero offset")Signed-off-by Eric Dumazet <[email protected]>Reported-by Dmitry Vyukov <[email protected]>Cc Willem de Bruijn <[email protected]>--- net/core/datagram.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/core/datagram.c +0
-0
net/core/datagram.c int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue, · line 368
Quoted 8 lines
net/core/datagram.c +1
-1@@ -362,7 +362,7 @@ int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue,if (flags &MSG_PEEK ) { err = -ENOENT ;spin_lock_bh (&sk_queue->lock); -if (skb ==skb_peek (sk_queue)) { +if (skb->next) {__skb_unlink (skb, sk_queue);refcount_dec (&skb->users);if (destructor)
This version is really nice!
Quoted 29 lines · Paolo Abeni
Quoted 28 lines · Eric Dumazet
From: Eric Dumazet <[email protected]>
When peeking, if a bad csum is discovered, the skb is unlinked from the queue with
__sk_queue_drop_skband the peek operation restarted.
__sk_queue_drop_skbonly drops packets that match the queue head.This fails if the skb was found after the head, using
SO_PEEK_OFFsocket option. This causes an infinite loop.We MUST drop this problematic skb, and we can simply check if skb was already removed by another thread, by looking at
skb->next:This pointer is set to NULL by the
__skb_unlink()operation, that might have happened only under the spinlock protection.Many thanks to syzkaller team (and particularly Dmitry Vyukov who provided us nice C reproducers exhibiting the lockup) and Willem de Bruijn who provided first version for this patch and a test program.
Fixes 627d2d6b5500 ("udp: enable MSG_PEEK at non-zero offset")Signed-off-by Eric Dumazet <[email protected]>Reported-by Dmitry Vyukov <[email protected]>Cc Willem de Bruijn <[email protected]>--- net/core/datagram.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines
Quoted 6 lines
net/core/datagram.c +0
-0net/core/datagram.c int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue, · line 368
Quoted 8 lines
net/core/datagram.c +1
-1@@ -362,7 +362,7 @@ int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue,if (flags &MSG_PEEK ) { err = -ENOENT ;spin_lock_bh (&sk_queue->lock); -if (skb ==skb_peek (sk_queue)) { +if (skb->next) {__skb_unlink (skb, sk_queue);refcount_dec (&skb->users);if (destructor)This version is really nice!
It is :) Thanks, Eric!
David Miller
maintainerFrom: Eric Dumazet <[email protected]>
Date: Tue, 22 Aug 2017 09:39:28 -0700
Quoted 16 lines
From: Eric Dumazet <[email protected]>
When peeking, if a bad csum is discovered, the skb is unlinked from the queue with
__sk_queue_drop_skband the peek operation restarted.
__sk_queue_drop_skbonly drops packets that match the queue head.This fails if the skb was found after the head, using
SO_PEEK_OFFsocket option. This causes an infinite loop.We MUST drop this problematic skb, and we can simply check if skb was already removed by another thread, by looking at
skb->next:This pointer is set to NULL by the
__skb_unlink()operation, that might have happened only under the spinlock protection.
Quoted 7 lines
Many thanks to syzkaller team (and particularly Dmitry Vyukov who provided us nice C reproducers exhibiting the lockup) and Willem de Bruijn who provided first version for this patch and a test program.
Fixes 627d2d6b5500 ("udp: enable MSG_PEEK at non-zero offset")Signed-off-by Eric Dumazet <[email protected]>Reported-by Dmitry Vyukov <[email protected]>
Applied and queued up for -stable.