af_unix: fix listen() succeeding on sockets in the wrong state
Subsystems (4)
PATCH v3 1/2 af_unix: fix listen() succeeding on sockets in the wrong state
From: John Ericson <[email protected]>
Commit fd0a109a0f6b ("net, pidfs: prepare for handing out pidfds for reaped sk->sk_peer_pid") inserted a prepare_peercred() call between err = -EINVAL and the socket-state check in unix_listen(). Since prepare_peercred() leaves err at 0 on success, listen() on an AF_UNIX socket that is not in TCP_CLOSE or TCP_LISTEN state (e.g. one that is already connected) now silently returns success without doing anything, instead of failing with EINVAL as it did before.
--- net/unix/af_unix.c | 1 + 1 file changed, 1 insertion(+)
net/unix/af_unix.c +1 -0
@@ -823,6 +823,7 @@ static int unix_listen(struct socket *sock, int backlog)if (err) goto out;unix_state_lock (sk); + err = -EINVAL ;if (sk->sk_state !=TCP_CLOSE && sk->sk_state !=TCP_LISTEN ) goto out_unlock;if (backlog > sk->sk_max_ack_backlog)
Signature · 1 line
2.54.0
PATCH v3 2/2 selftests/net/af_unix: test listen() rejects wrong socket states
From: John Ericson <[email protected]>
Add a regression test for the unix_listen() state check. The key case is listen() on a bound socket that has already been connected: it is no longer in TCP_CLOSE or TCP_LISTEN, so it must fail with EINVAL. A prepare_peercred() call slipped in ahead of that check once left err at 0 and made listen() silently succeed there instead; this guards against a repeat.
The neighbouring outcomes are covered too so they cannot regress the same
way: a bound socket in TCP_CLOSE listens fine, calling listen() again on a socket already in TCP_LISTEN is allowed, and an unbound socket fails with EINVAL.
Each case runs for both listenable socket types (SOCK_STREAM and SOCK_SEQPACKET) and both pathname and abstract addresses.
--- .../testing/selftests/net/af_unix/.gitignore | 1 + tools/testing/selftests/net/af_unix/Makefile | 1 + .../selftests/net/af_unix/unix_listen.c | 187 ++++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 tools/testing/selftests/net/af_unix/unix_listen.c
tools/testing/selftests/net/af_unix/.gitignore +1 -0
@@ -6,3 +6,4 @@ scm_rights so_peek_off unix_connect unix_connreset +unix_listen
tools/testing/selftests/net/af_unix/Makefile +1 -0
@@ -14,6 +14,7 @@ TEST_GEN_PROGS := \ so_peek_off \ unix_connect \ unix_connreset \ + unix_listen \ # end of TEST_GEN_PROGS include ../../lib.mk
tools/testing/selftests/net/af_unix/unix_listen.c new file +187 -0
187 lines not on this page · raw message
Signature · 1 line
2.54.0
Jakub Kicinski
maintainerQuoted 7 lines · On Sat, 18 Jul 2026 14:29:01 -0400 John Ericson wrote:
Commit
fd0a109a0f6b("net, pidfs: prepare for handing out pidfds for reapedsk->sk_peer_pid") inserted aprepare_peercred()call between err =-EINVALand the socket-state check inunix_listen(). Sinceprepare_peercred()leaves err at 0 on success,listen()on anAF_UNIXsocket that is not inTCP_CLOSEorTCP_LISTENstate (e.g. one that is already connected) now silently returns success without doing anything, instead of failing with EINVAL as it did before.
As the bug proves this
err = x if (cond) goto bla;
is a fragile pattern. People do this to avoid the extra braces AFAIU but it's not worth it. Can you move the error setting before the jumps please?
if (cond) {
err = x;
goto bla;
}Signature · 1 line
pw-bot: cr
Quoted 15 lines · Jakub Kicinski
Quoted 7 lines · On Sat, 18 Jul 2026 14:29:01 -0400 John Ericson wrote:
Commit
fd0a109a0f6b("net, pidfs: prepare for handing out pidfds for reapedsk->sk_peer_pid") inserted aprepare_peercred()call between err =-EINVALand the socket-state check inunix_listen(). Sinceprepare_peercred()leaves err at 0 on success,listen()on anAF_UNIXsocket that is not inTCP_CLOSEorTCP_LISTENstate (e.g. one that is already connected) now silently returns success without doing anything, instead of failing with EINVAL as it did before.As the bug proves this
err = x
if (cond)
goto bla;
Quoted 11 lines
is a fragile pattern. People do this to avoid the extra braces AFAIU but it's not worth it. Can you move the error setting before the jumps please?
if (cond) {
err = x;
goto bla;
}Signature · 1 line
pw-bot: cr
Ahahaha. I had the exact same thought!
The original revision of this [1] indeed had the error handling reworked like that, but Kuniyuki Iwashima asked me to submit the simple 1-line patch to "net" first, and only submit the cleanup to "net-next" later, so that is what I was doing.
Is that OK with you, too, I hope?
John
Jakub Kicinski
maintainerQuoted 8 lines · On Thu, 23 Jul 2026 16:58:16 -0400 John Ericson wrote:
Ahahaha. I had the exact same thought!
The original revision of this [1] indeed had the error handling reworked like that, but Kuniyuki Iwashima asked me to submit the simple 1-line patch to "net" first, and only submit the cleanup to "net-next" later, so that is what I was doing.
Is that OK with you, too, I hope?
Oh, you should probably mention the net-next follow up under the --- separator or in the cover letter next time.
Yay! I think these are my first kernel contributions to land. Thanks so much!
Quoted 2 lines · Jakub Kicinski
Oh, you should probably mention the net-next follow up under the --- separator or in the cover letter next time.
Ah OK, yes that makes total sense, happy to do that.
I see that the two patches have now landed in net --- yay! Apologies for being a novice with these workflows, but should I submit the follow-up to net-next right away, or should I first wait for some sort of periodic process to occur where net-next gets synced with the latest changes in net?
Cheers,
John
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 Sat, 18 Jul 2026 14:29:01 -0400 you wrote:
From: John Ericson <[email protected]>
Commit
fd0a109a0f6b("net, pidfs: prepare for handing out pidfds for reapedsk->sk_peer_pid") inserted aprepare_peercred()call between err =-EINVALand the socket-state check inunix_listen(). Sinceprepare_peercred()leaves err at 0 on success,listen()on anAF_UNIXsocket that is not inTCP_CLOSEorTCP_LISTENstate (e.g. one that is already connected) now silently returns success without doing anything, instead of failing with EINVAL as it did before.[...]
Here is the summary with links:
- [net,v3,1/2] af_unix: fix listen() succeeding on sockets in the wrong state
https://git.kernel.org/netdev/net/c/f0d9c3ffc2b5 - [net,v3,2/2] selftests/net/af_unix: test listen() rejects wrong socket states https://git.kernel.org/netdev/net/c/7f57c650d08b
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html