Skip to contentSkip to conversation

udp: on peeking bad csum, drop packets even if not at head

PATCH In review Willem de Bruijn 4 participants 9 messages Aug 21, 2017 – Aug 22, 2017

Subsystems (4)
lore
Share & tools
lore

Willem de Bruijn

author maintainer
#0 lore raw

PATCH 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
index a21ca8dee5ea..5cf32b2372d3 100644
@@ -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
author maintainer
#1 lore raw
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_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.

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 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.

Eric just suggested an alternative that does not require looping, which is much nicer.

#2 lore raw
In reply to Willem de Bruijn
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_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.

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 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.

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
index aeeec62992ca..6e43bab92d95 100644
@@ -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
index a21ca8dee5ea..7e129f91af89 100644
@@ -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
index cd1d044a7fa5..b5f90b845a6f 100644
@@ -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
index 20039c8501eb..214a973571fd 100644
@@ -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),
author maintainer
#3 lore raw
Quoted 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_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.

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.

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.

author maintainer
#4 lore raw
Quoted 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_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.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

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")

#5 lore raw

PATCH v2 1/1 udp: on peeking bad csum, drop packets even if not at head

In reply to 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_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.

Fixes 627d2d6b5500 ("udp: enable MSG_PEEK at non-zero offset")
Cc Willem de Bruijn <[email protected]>
---
 net/core/datagram.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/datagram.c +1 -1
index a21ca8dee5ea..8c2f4489ff8f 100644
@@ -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)
maintainer Acked-by on v2 1/1
#6 lore raw
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_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.

Fixes 627d2d6b5500 ("udp: enable MSG_PEEK at non-zero offset")
Cc Willem de Bruijn <[email protected]>
---
 net/core/datagram.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/datagram.c +0 -0
index a21ca8dee5ea..8c2f4489ff8f 100644

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!

author maintainer Acked-by
#7 lore raw
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_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.

Fixes 627d2d6b5500 ("udp: enable MSG_PEEK at non-zero offset")
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 -0
index a21ca8dee5ea..8c2f4489ff8f 100644

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!

It is :) Thanks, Eric!

maintainer
#8 lore raw
In reply to Eric Dumazet

From: 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_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.

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")

Applied and queued up for -stable.