af_unix: Fix SO_PEEK_OFF bug in unix_stream_read_generic().
Subsystems (4)
Kuniyuki Iwashima
maintainercover af_unix: Fix SO_PEEK_OFF bug in unix_stream_read_generic().
Miao Wang reported a bug of SO_PEEK_OFF on AF_UNIX SOCK_STREAM socket.
Patch 1 fixes the bug and Patch 2 adds a new selftest to cover the case.
Kuniyuki Iwashima (2):
af_unix: Read sk_peek_offset() again after sleeping in
unix_stream_read_generic().
selftest: af_unix: Add test for SO_PEEK_OFF.
net/unix/af_unix.c | 3 +- tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/af_unix/Makefile | 1 + .../selftests/net/af_unix/so_peek_off.c | 162 ++++++++++++++++++ 4 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/net/af_unix/so_peek_off.c
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Kuniyuki Iwashima
maintainerPATCH 1/2 af_unix: Read sk_peek_offset() again after sleeping in unix_stream_read_generic().
Miao Wang reported a bug of SO_PEEK_OFF on AF_UNIX SOCK_STREAM socket.
The unexpected behaviour is triggered when the peek offset is larger than the recv queue and the thread is unblocked by new data.
Let's assume a socket which has "aaaa" in the recv queue and the peek offset is 4.
First, unix_stream_read_generic() reads the offset 4 and skips the skb(s) of "aaaa" with the code below:
skip = max(sk_peek_offset(sk, flags), 0); /* @skip is 4. */
do {
...
while (skip >= unix_skb_len(skb)) {
skip -= unix_skb_len(skb);
...
skb = skb_peek_next(skb, &sk->sk_receive_queue);
if (!skb)
goto again; /* @skip is 0. */
}The thread jumps to the 'again' label and goes to sleep since new data has not arrived yet.
Later, new data "bbbb" unblocks the thread, and the thread jumps to the 'redo:' label to restart the entire process from the first skb in the recv queue.
do {
...
redo:
...
last = skb = skb_peek(&sk->sk_receive_queue);
...
again:
if (skb == NULL) {
...
timeo = unix_stream_data_wait(sk, timeo, last,
last_len, freezable);
...
goto redo; /* @skip is 0 !! */However, the peek offset is not reset in the path.
If the buffer size is 8, recv() will return "aaaabbbb" without skipping any data, and the final offset will be 12 (the original offset 4 + peeked skbs' length 8).
After sleeping in unix_stream_read_generic(), we have to fetch the peek offset again.
Let's move the redo label before mutex_lock(&u->iolock).
--- net/unix/af_unix.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
net/unix/af_unix.c +1 -2
@@ -2960,6 +2960,7 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state, u =unix_sk (sk); +redo:/* Lock the socket to prevent queue disordering * while sleeps in memcpy_tomsg */
@@ -2971,7 +2972,6 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,struct sk_buff *skb, *last;int chunk; -redo:unix_state_lock (sk);if (sock_flag (sk,SOCK_DEAD )) { err = -ECONNRESET ;
@@ -3021,7 +3021,6 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state, goto out; } -mutex_lock (&u->iolock); goto redo; unlock:unix_state_unlock (sk);
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Kuniyuki Iwashima
maintainerPATCH 2/2 selftest: af_unix: Add test for SO_PEEK_OFF.
In reply to Kuniyuki IwashimaThe test covers various cases to verify SO_PEEK_OFF behaviour for all AF_UNIX socket types.
two_chunks_blocking and two_chunks_overlap_blocking reproduce the issue mentioned in the previous patch.
Without the patch, the two tests fail:
# RUN so_peek_off.stream.two_chunks_blocking ...
# so_peek_off.c:121:two_chunks_blocking:Expected 'bbbb' == 'aaaabbbb'. # two_chunks_blocking: Test terminated by assertion
# FAIL so_peek_off.stream.two_chunks_blocking
not ok 3 so_peek_off.stream.two_chunks_blocking
# RUN so_peek_off.stream.two_chunks_overlap_blocking ...
# so_peek_off.c:159:two_chunks_overlap_blocking:Expected 'bbbb' == 'aaaabbbb'. # two_chunks_overlap_blocking: Test terminated by assertion
# FAIL so_peek_off.stream.two_chunks_overlap_blocking
not ok 5 so_peek_off.stream.two_chunks_overlap_blocking
With the patch, all tests pass:
# PASSED: 15 / 15 tests passed.
# Totals: pass:15 fail:0 xfail:0 xpass:0 skip:0 error:0
--- tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/af_unix/Makefile | 1 + .../selftests/net/af_unix/so_peek_off.c | 162 ++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 tools/testing/selftests/net/af_unix/so_peek_off.c
tools/testing/selftests/net/.gitignore +1 -0
@@ -45,6 +45,7 @@ skf_net_off socket so_incoming_cpu so_netns_cookie +so_peek_off so_txtime so_rcv_listener stress_reuseport_listen
tools/testing/selftests/net/af_unix/Makefile +1 -0
@@ -6,6 +6,7 @@ TEST_GEN_PROGS := \ scm_inq \ scm_pidfd \ scm_rights \ + so_peek_off \ unix_connect \ # end of TEST_GEN_PROGS
tools/testing/selftests/net/af_unix/so_peek_off.c new file +162 -0
162 lines not on this page · raw message
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Re: [PATCH v1 net 2/2] selftest: af_unix: Add test for SO_PEEK_OFF.
Quoted 28 lines
2025年11月18日 01:47,Kuniyuki Iwashima <[email protected]> 写道:
The test covers various cases to verify
SO_PEEK_OFFbehaviour for allAF_UNIXsocket types.
two_chunks_blockingandtwo_chunks_overlap_blockingreproduce the issue mentioned in the previous patch.Without the patch, the two tests fail:
# RUN so_peek_off.stream.two_chunks_blocking ...
# so_peek_off.c:121:two_chunks_blocking:Expected 'bbbb' == 'aaaabbbb'. #two_chunks_blocking: Test terminated by assertion
# FAIL so_peek_off.stream.two_chunks_blocking
not ok 3 so_peek_off.stream.two_chunks_blocking# RUN so_peek_off.stream.two_chunks_overlap_blocking ...
# so_peek_off.c:159:two_chunks_overlap_blocking:Expected 'bbbb' == 'aaaabbbb'. #two_chunks_overlap_blocking: Test terminated by assertion
# FAIL so_peek_off.stream.two_chunks_overlap_blocking
not ok 5 so_peek_off.stream.two_chunks_overlap_blockingWith the patch, all tests pass:
# PASSED: 15 / 15 tests passed.
# Totals: pass:15 fail:0 xfail:0 xpass:0 skip:0 error:0
Quoted 8 lines
Signed-off-by Kuniyuki Iwashima <[email protected]>---
tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/af_unix/Makefile | 1 + .../selftests/net/af_unix/so_peek_off.c | 162 ++++++++++++++++++ 3 files changed, 164 insertions(+)create mode 100644
tools/testing/selftests/net/af_unix/so_peek_off.c
Hi,
Many thanks for your patch. I wonder if at the end of each test, a normal recv() without MGS_PEEK can be called to check if it can receive all the content in the receiving buffer and check if SO_PEEK_OFF becomes back to zero.
Cheers,
Miao Wang
Kuniyuki Iwashima
maintainerRe: [PATCH v1 net 2/2] selftest: af_unix: Add test for SO_PEEK_OFF.
Quoted 38 lines · Miao Wang
Quoted 29 lines
2025年11月18日 01:47,Kuniyuki Iwashima <[email protected]> 写道:
The test covers various cases to verify
SO_PEEK_OFFbehaviour for allAF_UNIXsocket types.
two_chunks_blockingandtwo_chunks_overlap_blockingreproduce the issue mentioned in the previous patch.Without the patch, the two tests fail:
# RUN so_peek_off.stream.two_chunks_blocking ...
# so_peek_off.c:121:two_chunks_blocking:Expected 'bbbb' == 'aaaabbbb'. #two_chunks_blocking: Test terminated by assertion
# FAIL so_peek_off.stream.two_chunks_blocking
not ok 3 so_peek_off.stream.two_chunks_blocking# RUN so_peek_off.stream.two_chunks_overlap_blocking ...
# so_peek_off.c:159:two_chunks_overlap_blocking:Expected 'bbbb' == 'aaaabbbb'. #two_chunks_overlap_blocking: Test terminated by assertion
# FAIL so_peek_off.stream.two_chunks_overlap_blocking
not ok 5 so_peek_off.stream.two_chunks_overlap_blockingWith the patch, all tests pass:
# PASSED: 15 / 15 tests passed.
# Totals: pass:15 fail:0 xfail:0 xpass:0 skip:0 error:0Quoted 8 lines
Signed-off-by Kuniyuki Iwashima <[email protected]>---
tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/af_unix/Makefile | 1 + .../selftests/net/af_unix/so_peek_off.c | 162 ++++++++++++++++++ 3 files changed, 164 insertions(+)create mode 100644
tools/testing/selftests/net/af_unix/so_peek_off.c
Quoted 6 lines
Hi,
Many thanks for your patch. I wonder if at the end of each test, a normal
recv()withoutMGS_PEEKcan be called to check if it can receive all the content in the receiving buffer and check ifSO_PEEK_OFFbecomes back to zero.
I'd keep this for now as it covers the fix in the previous patch. We could add more tests in net-next if we want.
Thanks!
Jakub Kicinski
maintainerRe: [PATCH v1 net 2/2] selftest: af_unix: Add test for SO_PEEK_OFF.
In reply to Kuniyuki Iwashimatools/testing/selftests/net/.gitignore skf_net_off · line 48
Quoted 9 lines · On Mon, 17 Nov 2025 17:47:11 +0000 Kuniyuki Iwashima wrote:
tools/testing/selftests/net/.gitignore +1
-0@@ -45,6 +45,7 @@ skf_net_off socket so_incoming_cpu so_netns_cookie +so_peek_off
NIPA is complaining that we're missing the binary name in gitignore. Probably not worth respinning for this but in the future let's start using af_unix/.gitignore rather than the parent's .gitignore?
Quoted 14 lines
tools/testing/selftests/net/.gitignore +0
-0so_txtime so_rcv_listener stress_reuseport_listentools/testing/selftests/net/af_unix/Makefile +1
-0@@ -6,6 +6,7 @@ TEST_GEN_PROGS := \ scm_inq \ scm_pidfd \ scm_rights \ + so_peek_off \ unix_connect \ # end of TEST_GEN_PROGS
Kuniyuki Iwashima
maintainerRe: [PATCH v1 net 2/2] selftest: af_unix: Add test for SO_PEEK_OFF.
Quoted 15 lines · Jakub Kicinski
tools/testing/selftests/net/.gitignore skf_net_off · line 48
Quoted 9 lines · On Mon, 17 Nov 2025 17:47:11 +0000 Kuniyuki Iwashima wrote:
tools/testing/selftests/net/.gitignore +1
-0@@ -45,6 +45,7 @@ skf_net_off socket so_incoming_cpu so_netns_cookie +so_peek_offNIPA is complaining that we're missing the binary name in gitignore. Probably not worth respinning for this but in the future let's start using af_unix/.gitignore rather than the parent's .gitignore?
Sure, I'll move all of them under af_unix in net-next, including this one.
https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
Thanks!
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 11 lines · On Mon, 17 Nov 2025 17:47:09 +0000 you wrote:
Miao Wang reported a bug of
SO_PEEK_OFFonAF_UNIXSOCK_STREAMsocket.Patch 1 fixes the bug and Patch 2 adds a new selftest to cover the case.
Kuniyuki Iwashima (2):
af_unix: Readsk_peek_offset()again after sleeping in
unix_stream_read_generic().
selftest:af_unix: Add test forSO_PEEK_OFF.[...]
Here is the summary with links:
- [v1,net,1/2] af_unix: Read sk_peek_offset() again after sleeping in unix_stream_read_generic().
https://git.kernel.org/netdev/net/c/7bf3a476ce43 - [v1,net,2/2] selftest: af_unix: Add test for SO_PEEK_OFF. https://git.kernel.org/netdev/net/c/e1bb28bf13f4
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html