Skip to contentSkip to conversation

af_unix: fix listen() succeeding on sockets in the wrong state

PATCH Applied John Ericson 4 participants 7 messages Jul 18 – Jul 25

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

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.

Fixes fd0a109a0f6b ("net, pidfs: prepare for handing out pidfds for reaped sk->sk_peer_pid")
Assisted-by Claude:claude-fable-5
---
 net/unix/af_unix.c | 1 +
 1 file changed, 1 insertion(+)
net/unix/af_unix.c +1 -0
index f7a9d55eee8a..10ed9421e43a 100644
@@ -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
#1 lore raw

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.

Fixes fd0a109a0f6b ("net, pidfs: prepare for handing out pidfds for reaped sk->sk_peer_pid")
Assisted-by Claude:claude-opus-4-8
---
 .../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
index 240b26740c9e..973176644103 100644
@@ -6,3 +6,4 @@ scm_rights
 so_peek_off
 unix_connect
 unix_connreset
+unix_listen
tools/testing/selftests/net/af_unix/Makefile +1 -0
index 4c0375e28bbe..57d159803a3a 100644
@@ -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
maintainer
#2 lore raw
In reply to John Ericson
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 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.

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
#3 lore raw
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 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.

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

[1]: https://lore.kernel.org/all/[email protected]/

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

#5 lore raw

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

#6 lore raw
In reply to John Ericson
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 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.

[...]

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