Skip to contentSkip to conversation

tcp: Fix bugs of OOB handling in TCP.

PATCH NAK Takamitsu Iwai 2 participants 5 messages Aug 20, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover tcp: Fix bugs of OOB handling in TCP.

These patches fix bugs of OOB handling in TCP.

Patch 1 fixes an issue where a head OOB is dropped when queuing new OOB, and Patch 2 fixes an issue where OOB is recv()ed twice.

Takamitsu Iwai (2):
tcp: Don't drop head OOB when queuing new OOB.
tcp: Don't recv() OOB twice.

 include/linux/tcp.h                           |  1 +
 include/net/tcp.h                             |  3 +-
 net/ipv4/tcp.c                                | 15 +++--
 net/ipv4/tcp_input.c                          | 30 ++-------
 tools/testing/selftests/net/af_unix/msg_oob.c | 65 +++++++------------
 5 files changed, 42 insertions(+), 72 deletions(-)
Signature · 1 line
2.39.3 (Apple Git-145)
#1 lore raw

PATCH 1/2 tcp: Don't drop head OOB when queuing new OOB.

If OOB is not at the head of recvq, it's not dropped when a new OOB is queued.

OTOH, as commit f5ea0768a255 ("selftest: af_unix: Add non-TCP-compliant test cases in msg_oob.c.") points out, TCP drops OOB data at the head of recvq when queuing a new OOB data subsequently.

This behavior has been introduced in tcp_check_urg() by deleting preceding skb when next MSG_OOB data is received. This process is weird OOB handling, and the comment also says this is wrong.

We remove this code block for appropriate OOB handling.

Now TCP works exactly the same way as AF_UNIX, so this patch enables kernel to pass the test when removing tcp_incompliant braces.

# RUN msg_oob.no_peek.inline_ex_oob_drop ...
# OK msg_oob.no_peek.inline_ex_oob_drop
ok 18 msg_oob.no_peek.inline_ex_oob_drop
# RUN msg_oob.peek.ex_oob_drop ...
# OK msg_oob.peek.ex_oob_drop
ok 28 msg_oob.peek.ex_oob_drop
# RUN msg_oob.peek.ex_oob_drop_2 ...
# OK msg_oob.peek.ex_oob_drop_2
ok 29 msg_oob.peek.ex_oob_drop_2

Fixes tag refers to the commit of Linux-2.6.12-rc2, but this code was written at v2.4.4 which is older than this version.

This is a long-standing bug since then, and technically the patch slightly changes uAPI, but RFC 6091, published in 2011, suggests TCP urgent mechanism should not be used for newer applications in 2011.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 net/ipv4/tcp_input.c                          | 25 ---------
 tools/testing/selftests/net/af_unix/msg_oob.c | 55 ++++++++-----------
 2 files changed, 22 insertions(+), 58 deletions(-)
net/ipv4/tcp_input.c +0 -25
index e37488d3453f..648d0f3ade78 100644
@@ -5830,31 +5830,6 @@ static void tcp_check_urg(struct sock *sk, const struct tcphdr *th)
 	/* Tell the world about our new urgent pointer. */
 	sk_send_sigurg(sk);
 
-	/* We may be adding urgent data when the last byte read was
-	 * urgent. To do this requires some care. We cannot just ignore
-	 * tp->copied_seq since we would read the last urgent byte again
-	 * as data, nor can we alter copied_seq until this data arrives
-	 * or we break the semantics of SIOCATMARK (and thus sockatmark())
-	 *
-	 * NOTE. Double Dutch. Rendering to plain English: author of comment
-	 * above did something sort of 	send("A", MSG_OOB); send("B", MSG_OOB);
-	 * and expect that both A and B disappear from stream. This is _wrong_.
-	 * Though this happens in BSD with high probability, this is occasional.
-	 * Any application relying on this is buggy. Note also, that fix "works"
-	 * only in this artificial test. Insert some normal data between A and B and we will
-	 * decline of BSD again. Verdict: it is better to remove to trap
-	 * buggy users.
-	 */
-	if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
-	    !sock_flag(sk, SOCK_URGINLINE) && tp->copied_seq != tp->rcv_nxt) {
-		struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
-		tp->copied_seq++;
-		if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) {
-			__skb_unlink(skb, &sk->sk_receive_queue);
-			__kfree_skb(skb);
-		}
-	}
-
 	WRITE_ONCE(tp->urg_data, TCP_URG_NOTYET);
 	WRITE_ONCE(tp->urg_seq, ptr);
 
tools/testing/selftests/net/af_unix/msg_oob.c +22 -33

87 lines not on this page · raw message

Signature · 1 line
2.39.3 (Apple Git-145)
#2 lore raw

Re: [PATCH v1 net 1/2] tcp: Don't drop head OOB when queuing new OOB.

Quoted 64 lines · Takamitsu Iwai

If OOB is not at the head of recvq, it's not dropped when a new OOB is queued.

OTOH, as commit f5ea0768a255 ("selftest: af_unix: Add non-TCP-compliant test cases in msg_oob.c.") points out, TCP drops OOB data at the head of recvq when queuing a new OOB data subsequently.

This behavior has been introduced in tcp_check_urg() by deleting preceding skb when next MSG_OOB data is received. This process is weird OOB handling, and the comment also says this is wrong.

We remove this code block for appropriate OOB handling.

Now TCP works exactly the same way as AF_UNIX, so this patch enables kernel to pass the test when removing tcp_incompliant braces.

# RUN msg_oob.no_peek.inline_ex_oob_drop ...
# OK msg_oob.no_peek.inline_ex_oob_drop
ok 18 msg_oob.no_peek.inline_ex_oob_drop
# RUN msg_oob.peek.ex_oob_drop ...
# OK msg_oob.peek.ex_oob_drop
ok 28 msg_oob.peek.ex_oob_drop
# RUN msg_oob.peek.ex_oob_drop_2 ...
# OK msg_oob.peek.ex_oob_drop_2
ok 29 msg_oob.peek.ex_oob_drop_2

Fixes tag refers to the commit of Linux-2.6.12-rc2, but this code was written at v2.4.4 which is older than this version.

This is a long-standing bug since then, and technically the patch slightly changes uAPI, but RFC 6091, published in 2011, suggests TCP urgent mechanism should not be used for newer applications in 2011.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 net/ipv4/tcp_input.c                          | 25 ---------
 tools/testing/selftests/net/af_unix/msg_oob.c | 55 ++++++++-----------
 2 files changed, 22 insertions(+), 58 deletions(-)
net/ipv4/tcp_input.c +0 -15
index e37488d3453f..648d0f3ade78 100644
@@ -5830,31 +5830,6 @@ static void tcp_check_urg(struct sock *sk, const struct tcphdr *th)
        /* Tell the world about our new urgent pointer. */
        sk_send_sigurg(sk);
 
-       /* We may be adding urgent data when the last byte read was
-        * urgent. To do this requires some care. We cannot just ignore
-        * tp->copied_seq since we would read the last urgent byte again
-        * as data, nor can we alter copied_seq until this data arrives
-        * or we break the semantics of SIOCATMARK (and thus sockatmark())
-        *
-        * NOTE. Double Dutch. Rendering to plain English: author of comment
-        * above did something sort of  send("A", MSG_OOB); send("B", MSG_OOB);
-        * and expect that both A and B disappear from stream. This is _wrong_.
-        * Though this happens in BSD with high probability, this is occasional.
-        * Any application relying on this is buggy. Note also, that fix "works"
-        * only in this artificial test. Insert some normal data between A and B and we will
-        * decline of BSD again. Verdict: it is better to remove to trap
-        * buggy users.
-        */

net/ipv4/tcp_input.c static void tcp_check_urg(struct sock *sk, const struct tcphdr *th) · line 5857

Quoted 10 lines
net/ipv4/tcp_input.c +0 -10
-       if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
-           !sock_flag(sk, SOCK_URGINLINE) && tp->copied_seq != tp->rcv_nxt) {
-               struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
-               tp->copied_seq++;
-               if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) {
-                       __skb_unlink(skb, &sk->sk_receive_queue);
-                       __kfree_skb(skb);
-               }
-       }
-

My opinion is that we should not touch this code.

No one sane uses OOB and TCP, otherwise this issue would have been discussed a long time ago.

If anyone is using it, then your change will change the behavior.

Fact that you changed tools/testing/selftests/net/af_unix/msg_oob.c should speak by itself.

Note that OOB has been added to AF_UNIX recently, it should have followed TCP behavior.
Honestly we should not have accepted OOB on AF_UNIX, this was a real mistake.

#3 lore raw

PATCH 2/2 tcp: Don't recv() OOB twice.

In reply to Takamitsu Iwai

commit 36893ef0b661 ("af_unix: Don't stop recv() at consumed ex-OOB skb.") finds a bug that TCP reads OOB which has been already recv()ed.

This bug is caused because current TCP code does not have a process to check and skip consumed OOB data. So OOB exists until it is recv()ed even if it is already consumed through recv() with MSG_OOB option.

We add code to check and skip consumed OOB when reading skbs.

In this patch, we introduce urg_skb in tcp_sock to keep track of skbs containing consumed OOB. We make tcp_try_coalesce() avoid coalescing skb to urg_skb to locate OOB data at the last byte of urg_skb.

I tried not to modify tcp_try_coalesce() by decrementing end_seq when OOB data is recv()ed. But this hack does not work when OOB data is at the middle of skb by coalescing OOB and normal skbs. Also, when the next OOB data comes in, we’ll lose the seq# of the consumed OOB to skip during the normal recv().

Consequently, the code to prevent coalescing is now located within tcp_try_coalesce().

This patch enables TCP to pass msg_oob selftests when removing tcp_incompliant braces in inline_oob_ahead_break and ex_oob_ahead_break tests.

# RUN msg_oob.no_peek.ex_oob_ahead_break ...
# OK msg_oob.no_peek.ex_oob_ahead_break
ok 11 msg_oob.no_peek.ex_oob_ahead_break
# RUN msg_oob.no_peek.inline_oob_ahead_break ...
# OK msg_oob.no_peek.inline_oob_ahead_break
ok 15 msg_oob.no_peek.inline_oob_ahead_break

We will rewrite existing other code to use urg_skb and remove urg_data and urg_seq, which have the same functionality as urg_skb

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 include/linux/tcp.h                           |  1 +
 include/net/tcp.h                             |  3 ++-
 net/ipv4/tcp.c                                | 15 ++++++++++-----
 net/ipv4/tcp_input.c                          |  5 +++++
 tools/testing/selftests/net/af_unix/msg_oob.c | 10 ++--------
 5 files changed, 20 insertions(+), 14 deletions(-)
include/linux/tcp.h +1 -0
index 6a5e08b937b3..63234e8680e3 100644
@@ -243,6 +243,7 @@ struct tcp_sock {
 	struct  minmax rtt_min;
 	/* OOO segments go in this rbtree. Socket lock must be held. */
 	struct rb_root	out_of_order_queue;
+	struct sk_buff *urg_skb;
 	u32	snd_ssthresh;	/* Slow start size threshold		*/
 	u8	recvmsg_inq : 1;/* Indicate # of bytes in queue upon recvmsg */
 	__cacheline_group_end(tcp_sock_read_rx);
include/net/tcp.h +2 -1
index 2aac11e7e1cc..4eb0929c0744 100644
@@ -961,7 +961,8 @@ struct tcp_skb_cb {
 	__u8		txstamp_ack:1,	/* Record TX timestamp for ack? */
 			eor:1,		/* Is skb MSG_EOR marked? */
 			has_rxtstamp:1,	/* SKB has a RX timestamp	*/
-			unused:5;
+			consumed_urg:1, /* urg data in SKB has already read */
+			unused:4;
 	__u32		ack_seq;	/* Sequence number ACK'd	*/
 	union {
 		struct {
net/ipv4/tcp.c +10 -5
index e03a342c9162..463e89849d6d 100644
@@ -1402,8 +1402,10 @@ static int tcp_recv_urg(struct sock *sk, struct msghdr *msg, int len, int flags)
 		msg->msg_flags |= MSG_OOB;
 
 		if (len > 0) {
-			if (!(flags & MSG_TRUNC))
+			if (!(flags & MSG_TRUNC)) {
 				err = memcpy_to_msg(msg, &c, 1);
+				TCP_SKB_CB(tp->urg_skb)->consumed_urg = true;
+			}
 			len = 1;
 		} else
 			msg->msg_flags |= MSG_TRUNC;
@@ -2491,11 +2493,13 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
 			used = len;
 
 		/* Do we have urgent data here? */
-		if (unlikely(tp->urg_data)) {
-			u32 urg_offset = tp->urg_seq - *seq;
+		if (unlikely(tp->urg_skb == skb || TCP_SKB_CB(skb)->consumed_urg)) {
+			u32 urg_offset = TCP_SKB_CB(skb)->end_seq - *seq - 1;
+
 			if (urg_offset < used) {
 				if (!urg_offset) {
-					if (!sock_flag(sk, SOCK_URGINLINE)) {
+					if (!sock_flag(sk, SOCK_URGINLINE) ||
+					    TCP_SKB_CB(skb)->consumed_urg) {
 						WRITE_ONCE(*seq, *seq + 1);
 						urg_hole++;
 						offset++;
@@ -2530,6 +2534,7 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
 skip_copy:
 		if (unlikely(tp->urg_data) && after(tp->copied_seq, tp->urg_seq)) {
 			WRITE_ONCE(tp->urg_data, 0);
+			tp->urg_skb = NULL;
 			tcp_fast_path_check(sk);
 		}
 
@@ -4726,7 +4731,7 @@ static void __init tcp_struct_check(void)
 	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_read_rx, rtt_min);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_read_rx, out_of_order_queue);
 	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_read_rx, snd_ssthresh);
-	CACHELINE_ASSERT_GROUP_SIZE(struct tcp_sock, tcp_sock_read_rx, 69);
+	CACHELINE_ASSERT_GROUP_SIZE(struct tcp_sock, tcp_sock_read_rx, 77);
 
 	/* TX read-write hotpath cache lines */
 	CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_write_tx, segs_out);
net/ipv4/tcp_input.c +5 -0
index 648d0f3ade78..47eb7b7f31c4 100644
@@ -4851,6 +4851,10 @@ static bool tcp_try_coalesce(struct sock *sk,
 
 	*fragstolen = false;
 
+	/* avoid coalescing to urgent skb since last byte is OOB data*/
+	if (tcp_sk(sk)->urg_skb)
+		return false;
+
 	/* Its possible this segment overlaps with prior segment in queue */
 	if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq)
 		return false;
@@ -5857,6 +5861,7 @@ static void tcp_urg(struct sock *sk, struct sk_buff *skb, const struct tcphdr *t
 			if (skb_copy_bits(skb, ptr, &tmp, 1))
 				BUG();
 			WRITE_ONCE(tp->urg_data, TCP_URG_VALID | tmp);
+			tp->urg_skb = skb;
 			if (!sock_flag(sk, SOCK_DEAD))
 				sk->sk_data_ready(sk);
 		}
tools/testing/selftests/net/af_unix/msg_oob.c +2 -8
index f3435575dfa5..3eee7930b6ed 100644
@@ -534,10 +534,7 @@ TEST_F(msg_oob, ex_oob_ahead_break)
 	epollpair(true);
 	siocatmarkpair(false);
 
-	tcp_incompliant {
-		recvpair("hellowol", 8, 10, 0);	/* TCP recv()s "helloworl", why "r" ?? */
-	}
-
+	recvpair("hellowol", 8, 10, 0);
 	epollpair(true);
 	siocatmarkpair(true);
 
@@ -623,10 +620,7 @@ TEST_F(msg_oob, inline_oob_ahead_break)
 	epollpair(false);
 	siocatmarkpair(true);
 
-	tcp_incompliant {
-		recvpair("world", 5, 6, 0);	/* TCP recv()s "oworld", ... "o" ??? */
-	}
-
+	recvpair("world", 5, 6, 0);
 	epollpair(false);
 	siocatmarkpair(false);
 }
Signature · 1 line
2.39.3 (Apple Git-145)
#4 lore raw

Re: [PATCH v1 net 2/2] tcp: Don't recv() OOB twice.

Quoted 52 lines · Takamitsu Iwai

commit 36893ef0b661 ("af_unix: Don't stop recv() at consumed ex-OOB skb.") finds a bug that TCP reads OOB which has been already recv()ed.

This bug is caused because current TCP code does not have a process to check and skip consumed OOB data. So OOB exists until it is recv()ed even if it is already consumed through recv() with MSG_OOB option.

We add code to check and skip consumed OOB when reading skbs.

In this patch, we introduce urg_skb in tcp_sock to keep track of skbs containing consumed OOB. We make tcp_try_coalesce() avoid coalescing skb to urg_skb to locate OOB data at the last byte of urg_skb.

I tried not to modify tcp_try_coalesce() by decrementing end_seq when OOB data is recv()ed. But this hack does not work when OOB data is at the middle of skb by coalescing OOB and normal skbs. Also, when the next OOB data comes in, we’ll lose the seq# of the consumed OOB to skip during the normal recv().

Consequently, the code to prevent coalescing is now located within tcp_try_coalesce().

This patch enables TCP to pass msg_oob selftests when removing tcp_incompliant braces in inline_oob_ahead_break and ex_oob_ahead_break tests.

# RUN msg_oob.no_peek.ex_oob_ahead_break ...
# OK msg_oob.no_peek.ex_oob_ahead_break
ok 11 msg_oob.no_peek.ex_oob_ahead_break
# RUN msg_oob.no_peek.inline_oob_ahead_break ...
# OK msg_oob.no_peek.inline_oob_ahead_break
ok 15 msg_oob.no_peek.inline_oob_ahead_break

We will rewrite existing other code to use urg_skb and remove urg_data and urg_seq, which have the same functionality as urg_skb

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 include/linux/tcp.h                           |  1 +
 include/net/tcp.h                             |  3 ++-
 net/ipv4/tcp.c                                | 15 ++++++++++-----
 net/ipv4/tcp_input.c                          |  5 +++++
 tools/testing/selftests/net/af_unix/msg_oob.c | 10 ++--------
 5 files changed, 20 insertions(+), 14 deletions(-)
include/linux/tcp.h +0 -0
index 6a5e08b937b3..63234e8680e3 100644

include/linux/tcp.h struct tcp_sock { · line 249

Quoted 7 lines
include/linux/tcp.h +1 -0
@@ -243,6 +243,7 @@ struct tcp_sock {
        struct  minmax rtt_min;
        /* OOO segments go in this rbtree. Socket lock must be held. */
        struct rb_root  out_of_order_queue;
+       struct sk_buff *urg_skb;
        u32     snd_ssthresh;   /* Slow start size threshold            */
        u8      recvmsg_inq : 1;/* Indicate # of bytes in queue upon recvmsg */
        __cacheline_group_end(tcp_sock_read_rx);

NACK, sorry, we will not change URG behavior, add yet another dangerous dangling pointer.

We should not give users the impression this is maintained, useful, and works as intended on various OS/kernels.