Skip to contentSkip to conversation

af_unix: Read with MSG_PEEK loops if the first unread byte is OOB

PATCH Superseded Rao Shoaib 2 participants 5 messages Apr 22, 2024 – Apr 30, 2024

Subsystems (3)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Rao Shoaib

author
#0 lore raw

PATCH v3 1/1 af_unix: Read with MSG_PEEK loops if the first unread byte is OOB

Read with MSG_PEEK flag loops if the first byte to read is an OOB byte. commit 22dd70eb2c3d ("af_unix: Don't peek OOB data without MSG_OOB.") addresses the loop issue but does not address the issue that no data beyond OOB byte can be read.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

from socket import *
c1, c2 = socketpair(AF_UNIX, SOCK_STREAM)
c1.send(b'a', MSG_OOB)

1

Quoted 1 line
Quoted 1 line
Quoted 1 line

c1.send(b'b')

1

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(1, MSG_PEEK | MSG_DONTWAIT)

b'b'

Fixes 314001f0bf92 ("af_unix: Add OOB support")
---
 net/unix/af_unix.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
net/unix/af_unix.c +14 -12
index 9a6ad5974dff..ed5f70735435 100644
@@ -2658,19 +2658,19 @@ static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,
 		if (skb == u->oob_skb) {
 			if (copied) {
 				skb = NULL;
-			} else if (sock_flag(sk, SOCK_URGINLINE)) {
-				if (!(flags & MSG_PEEK)) {
+			} else if (!(flags & MSG_PEEK)) {
+				if (sock_flag(sk, SOCK_URGINLINE)) {
 					WRITE_ONCE(u->oob_skb, NULL);
 					consume_skb(skb);
+				} else {
+					skb_unlink(skb, &sk->sk_receive_queue);
+					WRITE_ONCE(u->oob_skb, NULL);
+					if (!WARN_ON_ONCE(skb_unref(skb)))
+						kfree_skb(skb);
+					skb = skb_peek(&sk->sk_receive_queue);
 				}
-			} else if (flags & MSG_PEEK) {
-				skb = NULL;
-			} else {
-				skb_unlink(skb, &sk->sk_receive_queue);
-				WRITE_ONCE(u->oob_skb, NULL);
-				if (!WARN_ON_ONCE(skb_unref(skb)))
-					kfree_skb(skb);
-				skb = skb_peek(&sk->sk_receive_queue);
+			} else if (!sock_flag(sk, SOCK_URGINLINE)) {
+				skb = skb_peek_next(skb, &sk->sk_receive_queue);
 			}
 		}
 	}
@@ -2747,9 +2747,11 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
 #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
 		if (skb) {
 			skb = manage_oob(skb, sk, flags, copied);
-			if (!skb && copied) {
+			if (!skb) {
 				unix_state_unlock(sk);
-				break;
+				if (copied || (flags & MSG_PEEK))
+					break;
+				goto redo;
 			}
 		}
 #endif
Signature · 1 line
2.39.3
#1 lore raw

From: Rao Shoaib <[email protected]>
Date: Mon, 22 Apr 2024 02:25:03 -0700

Quoted 30 lines

Read with MSG_PEEK flag loops if the first byte to read is an OOB byte. commit 22dd70eb2c3d ("af_unix: Don't peek OOB data without MSG_OOB.") addresses the loop issue but does not address the issue that no data beyond OOB byte can be read.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

from socket import *
c1, c2 = socketpair(AF_UNIX, SOCK_STREAM)
c1.send(b'a', MSG_OOB)

1

Quoted 1 line
Quoted 1 line
Quoted 1 line

c1.send(b'b')

1

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(1, MSG_PEEK | MSG_DONTWAIT)

b'b'

Fixes 314001f0bf92 ("af_unix: Add OOB support")
---
 net/unix/af_unix.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
net/unix/af_unix.c +0 -2
index 9a6ad5974dff..ed5f70735435 100644
@@ -2658,19 +2658,19 @@ static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,
 		if (skb == u->oob_skb) {
 			if (copied) {
 				skb = NULL;
-			} else if (sock_flag(sk, SOCK_URGINLINE)) {
-				if (!(flags & MSG_PEEK)) {

net/unix/af_unix.c static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk, · line 2670

Quoted 10 lines
net/unix/af_unix.c +8 -0
+			} else if (!(flags & MSG_PEEK)) {
+				if (sock_flag(sk, SOCK_URGINLINE)) {
 					WRITE_ONCE(u->oob_skb, NULL);
 					consume_skb(skb);
+				} else {
+					skb_unlink(skb, &sk->sk_receive_queue);
+					WRITE_ONCE(u->oob_skb, NULL);
+					if (!WARN_ON_ONCE(skb_unref(skb)))
+						kfree_skb(skb);
+					skb = skb_peek(&sk->sk_receive_queue);

I added a comment about this case.

Quoted 15 lines
net/unix/af_unix.c +2 -8
 				}
-			} else if (flags & MSG_PEEK) {
-				skb = NULL;
-			} else {
-				skb_unlink(skb, &sk->sk_receive_queue);
-				WRITE_ONCE(u->oob_skb, NULL);
-				if (!WARN_ON_ONCE(skb_unref(skb)))
-					kfree_skb(skb);
-				skb = skb_peek(&sk->sk_receive_queue);
+			} else if (!sock_flag(sk, SOCK_URGINLINE)) {
+				skb = skb_peek_next(skb, &sk->sk_receive_queue);
 			}
 		}
 	}

net/unix/af_unix.c static int unix_stream_read_generic(struct unix_stream_read_state *state, · line 2754

Quoted 10 lines
net/unix/af_unix.c +4 -2
@@ -2747,9 +2747,11 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
 #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
 		if (skb) {
 			skb = manage_oob(skb, sk, flags, copied);
-			if (!skb && copied) {
+			if (!skb) {
 				unix_state_unlock(sk);
-				break;
+				if (copied || (flags & MSG_PEEK))
+					break;
+				goto redo;

Here, copied == 0 && !(flags & MSG_PEEK) && skb == NULL, so it means skb_peek(&sk->sk_receive_queue) above returned NULL. Then, we need not jump to the redo label, where we call the same skb_peek().

Instead, we can just fall through the if (!skb) clause below.

Thanks!

Quoted 5 lines
net/unix/af_unix.c +0 -0
 			}
 		}
 #endif
Signature · 1 line
2.39.3
author
#2 lore raw
Quoted 36 lines · Kuniyuki Iwashima

From: Rao Shoaib <[email protected]>
Date: Mon, 22 Apr 2024 02:25:03 -0700

Quoted 34 lines

Read with MSG_PEEK flag loops if the first byte to read is an OOB byte. commit 22dd70eb2c3d ("af_unix: Don't peek OOB data without MSG_OOB.") addresses the loop issue but does not address the issue that no data beyond OOB byte can be read.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

from socket import *
c1, c2 = socketpair(AF_UNIX, SOCK_STREAM)
c1.send(b'a', MSG_OOB)

1

Quoted 1 line
Quoted 1 line
Quoted 1 line

c1.send(b'b')

1

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(1, MSG_PEEK | MSG_DONTWAIT)

b'b'

Fixes 314001f0bf92 ("af_unix: Add OOB support")
---
 net/unix/af_unix.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
net/unix/af_unix.c +2 -2
index 9a6ad5974dff..ed5f70735435 100644
@@ -2658,19 +2658,19 @@ static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,
 		if (skb == u->oob_skb) {
 			if (copied) {
 				skb = NULL;
-			} else if (sock_flag(sk, SOCK_URGINLINE)) {
-				if (!(flags & MSG_PEEK)) {
+			} else if (!(flags & MSG_PEEK)) {
+				if (sock_flag(sk, SOCK_URGINLINE)) {
 					WRITE_ONCE(u->oob_skb, NULL);
 					consume_skb(skb);
Quoted 8 lines

net/unix/af_unix.c static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk, · line 2670

Quoted 6 lines
net/unix/af_unix.c +6 -0
+				} else {
+					skb_unlink(skb, &sk->sk_receive_queue);
+					WRITE_ONCE(u->oob_skb, NULL);
+					if (!WARN_ON_ONCE(skb_unref(skb)))
+						kfree_skb(skb);
+					skb = skb_peek(&sk->sk_receive_queue);

I added a comment about this case.

OK. I will sync up.

Quoted 32 lines
Quoted 17 lines
net/unix/af_unix.c +2 -8
 				}
-			} else if (flags & MSG_PEEK) {
-				skb = NULL;
-			} else {
-				skb_unlink(skb, &sk->sk_receive_queue);
-				WRITE_ONCE(u->oob_skb, NULL);
-				if (!WARN_ON_ONCE(skb_unref(skb)))
-					kfree_skb(skb);
-				skb = skb_peek(&sk->sk_receive_queue);
+			} else if (!sock_flag(sk, SOCK_URGINLINE)) {
+				skb = skb_peek_next(skb, &sk->sk_receive_queue);
 			}
 		}
 	}

net/unix/af_unix.c static int unix_stream_read_generic(struct unix_stream_read_state *state, · line 2754

Quoted 10 lines
net/unix/af_unix.c +4 -2
@@ -2747,9 +2747,11 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
 #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
 		if (skb) {
 			skb = manage_oob(skb, sk, flags, copied);
-			if (!skb && copied) {
+			if (!skb) {
 				unix_state_unlock(sk);
-				break;
+				if (copied || (flags & MSG_PEEK))
+					break;
+				goto redo;

Here, copied == 0 && !(flags & MSG_PEEK) && skb == NULL, so it means skb_peek(&sk->sk_receive_queue) above returned NULL. Then, we need not jump to the redo label, where we call the same skb_peek().

Quoted 3 lines

Instead, we can just fall through the if (!skb) clause below.

Thanks!

Yes that makes sense. I will submit a new version with the jump to redo removed.

Thanks,

Shoaib

Quoted 6 lines
Quoted 6 lines
net/unix/af_unix.c +0 -0
 			}
 		}
 #endif
Signature · 1 line
2.39.3
#3 lore raw

From: Rao Shoaib <[email protected]>
Date: Tue, 23 Apr 2024 18:18:24 -0700

Quoted 79 lines · Kuniyuki Iwashima
Quoted 36 lines

From: Rao Shoaib <[email protected]>
Date: Mon, 22 Apr 2024 02:25:03 -0700

Quoted 34 lines

Read with MSG_PEEK flag loops if the first byte to read is an OOB byte. commit 22dd70eb2c3d ("af_unix: Don't peek OOB data without MSG_OOB.") addresses the loop issue but does not address the issue that no data beyond OOB byte can be read.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

from socket import *
c1, c2 = socketpair(AF_UNIX, SOCK_STREAM)
c1.send(b'a', MSG_OOB)

1

Quoted 1 line
Quoted 1 line
Quoted 1 line

c1.send(b'b')

1

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(1, MSG_PEEK | MSG_DONTWAIT)

b'b'

Fixes 314001f0bf92 ("af_unix: Add OOB support")
---
 net/unix/af_unix.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
net/unix/af_unix.c +2 -2
index 9a6ad5974dff..ed5f70735435 100644
@@ -2658,19 +2658,19 @@ static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,
 		if (skb == u->oob_skb) {
 			if (copied) {
 				skb = NULL;
-			} else if (sock_flag(sk, SOCK_URGINLINE)) {
-				if (!(flags & MSG_PEEK)) {
+			} else if (!(flags & MSG_PEEK)) {
+				if (sock_flag(sk, SOCK_URGINLINE)) {
 					WRITE_ONCE(u->oob_skb, NULL);
 					consume_skb(skb);
Quoted 8 lines

net/unix/af_unix.c static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk, · line 2670

Quoted 6 lines
net/unix/af_unix.c +6 -0
+				} else {
+					skb_unlink(skb, &sk->sk_receive_queue);
+					WRITE_ONCE(u->oob_skb, NULL);
+					if (!WARN_ON_ONCE(skb_unref(skb)))
+						kfree_skb(skb);
+					skb = skb_peek(&sk->sk_receive_queue);

I added a comment about this case.

OK. I will sync up.

Quoted 32 lines
Quoted 17 lines
net/unix/af_unix.c +2 -8
 				}
-			} else if (flags & MSG_PEEK) {
-				skb = NULL;
-			} else {
-				skb_unlink(skb, &sk->sk_receive_queue);
-				WRITE_ONCE(u->oob_skb, NULL);
-				if (!WARN_ON_ONCE(skb_unref(skb)))
-					kfree_skb(skb);
-				skb = skb_peek(&sk->sk_receive_queue);
+			} else if (!sock_flag(sk, SOCK_URGINLINE)) {
+				skb = skb_peek_next(skb, &sk->sk_receive_queue);
 			}
 		}
 	}

net/unix/af_unix.c static int unix_stream_read_generic(struct unix_stream_read_state *state, · line 2754

Quoted 10 lines
net/unix/af_unix.c +4 -2
@@ -2747,9 +2747,11 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
 #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
 		if (skb) {
 			skb = manage_oob(skb, sk, flags, copied);
-			if (!skb && copied) {
+			if (!skb) {
 				unix_state_unlock(sk);
-				break;
+				if (copied || (flags & MSG_PEEK))
+					break;
+				goto redo;

Here, copied == 0 && !(flags & MSG_PEEK) && skb == NULL, so it means skb_peek(&sk->sk_receive_queue) above returned NULL. Then, we need not jump to the redo label, where we call the same skb_peek().

Quoted 6 lines
Quoted 3 lines

Instead, we can just fall through the if (!skb) clause below.

Thanks!

Yes that makes sense. I will submit a new version with the jump to redo removed.

If skb_peek_next() returns NULL, should it also fall down to the !skb case ?

TCP is blocked in the situation.

So, I think this hunk in unix_stream_read_generic() is not needed.

---8<---

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

from socket import *

s = socket()
s.listen()

c1 = socket()
c1.connect(s.getsockname())
c2, _ = s.accept()

c1.send(b'h', MSG_OOB)

1

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(5, MSG_PEEK)

^C
---8<---

author
#4 lore raw
Quoted 88 lines · Kuniyuki Iwashima

From: Rao Shoaib <[email protected]>
Date: Tue, 23 Apr 2024 18:18:24 -0700

Quoted 79 lines · Kuniyuki Iwashima
Quoted 36 lines

From: Rao Shoaib <[email protected]>
Date: Mon, 22 Apr 2024 02:25:03 -0700

Quoted 34 lines

Read with MSG_PEEK flag loops if the first byte to read is an OOB byte. commit 22dd70eb2c3d ("af_unix: Don't peek OOB data without MSG_OOB.") addresses the loop issue but does not address the issue that no data beyond OOB byte can be read.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

from socket import *
c1, c2 = socketpair(AF_UNIX, SOCK_STREAM)
c1.send(b'a', MSG_OOB)

1

Quoted 1 line
Quoted 1 line
Quoted 1 line

c1.send(b'b')

1

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(1, MSG_PEEK | MSG_DONTWAIT)

b'b'

Fixes 314001f0bf92 ("af_unix: Add OOB support")
---
 net/unix/af_unix.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
net/unix/af_unix.c +2 -2
index 9a6ad5974dff..ed5f70735435 100644
@@ -2658,19 +2658,19 @@ static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,
 		if (skb == u->oob_skb) {
 			if (copied) {
 				skb = NULL;
-			} else if (sock_flag(sk, SOCK_URGINLINE)) {
-				if (!(flags & MSG_PEEK)) {
+			} else if (!(flags & MSG_PEEK)) {
+				if (sock_flag(sk, SOCK_URGINLINE)) {
 					WRITE_ONCE(u->oob_skb, NULL);
 					consume_skb(skb);
Quoted 8 lines

net/unix/af_unix.c static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk, · line 2670

Quoted 6 lines
net/unix/af_unix.c +6 -0
+				} else {
+					skb_unlink(skb, &sk->sk_receive_queue);
+					WRITE_ONCE(u->oob_skb, NULL);
+					if (!WARN_ON_ONCE(skb_unref(skb)))
+						kfree_skb(skb);
+					skb = skb_peek(&sk->sk_receive_queue);

I added a comment about this case.

OK. I will sync up.

Quoted 32 lines
Quoted 17 lines
net/unix/af_unix.c +2 -8
 				}
-			} else if (flags & MSG_PEEK) {
-				skb = NULL;
-			} else {
-				skb_unlink(skb, &sk->sk_receive_queue);
-				WRITE_ONCE(u->oob_skb, NULL);
-				if (!WARN_ON_ONCE(skb_unref(skb)))
-					kfree_skb(skb);
-				skb = skb_peek(&sk->sk_receive_queue);
+			} else if (!sock_flag(sk, SOCK_URGINLINE)) {
+				skb = skb_peek_next(skb, &sk->sk_receive_queue);
 			}
 		}
 	}

net/unix/af_unix.c static int unix_stream_read_generic(struct unix_stream_read_state *state, · line 2754

Quoted 10 lines
net/unix/af_unix.c +4 -2
@@ -2747,9 +2747,11 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
 #if IS_ENABLED(CONFIG_AF_UNIX_OOB)
 		if (skb) {
 			skb = manage_oob(skb, sk, flags, copied);
-			if (!skb && copied) {
+			if (!skb) {
 				unix_state_unlock(sk);
-				break;
+				if (copied || (flags & MSG_PEEK))
+					break;
+				goto redo;

Here, copied == 0 && !(flags & MSG_PEEK) && skb == NULL, so it means skb_peek(&sk->sk_receive_queue) above returned NULL. Then, we need not jump to the redo label, where we call the same skb_peek().

Quoted 6 lines
Quoted 3 lines

Instead, we can just fall through the if (!skb) clause below.

Thanks!

Yes that makes sense. I will submit a new version with the jump to redo removed.

Quoted 6 lines

If skb_peek_next() returns NULL, should it also fall down to the !skb case ?

TCP is blocked in the situation.

So, I think this hunk in unix_stream_read_generic() is not needed.

I do not understand can you please explain.

Regards,

Shoaib

Quoted 16 lines

---8<---

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

from socket import *

s = socket()
s.listen()

c1 = socket()
c1.connect(s.getsockname())
c2, _ = s.accept()

c1.send(b'h', MSG_OOB)

1

Quoted 1 line
Quoted 1 line
Quoted 1 line

c2.recv(5, MSG_PEEK)

^C
---8<---