af_unix: fix listen() succeeding on sockets in the wrong state
Subsystems (4)
PATCH v2 1/3 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 v2 2/3 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, re-listen()ing a socket already in TCP_LISTEN is allowed, and an unbound socket fails with EINVAL.
--- .../testing/selftests/net/af_unix/.gitignore | 1 + tools/testing/selftests/net/af_unix/Makefile | 1 + .../selftests/net/af_unix/unix_listen.c | 126 ++++++++++++++++++ 3 files changed, 128 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 +126 -0
126 lines not on this page · raw message
Signature · 1 line
2.54.0
Re: [PATCH net v2 2/3] selftests/net/af_unix: test listen() rejects wrong socket states
Quoted 7 lines
Add a regression test for the
unix_listen()state check. The key case islisten()on a bound socket that has already been connected: it is no longer inTCP_CLOSEorTCP_LISTEN, so it must fail withEINVAL. Aprepare_peercred()call slipped in ahead of that check once lefterrat 0 and madelisten()silently succeed there instead; this guards against a repeat.
Quoted 7 lines
The neighbouring outcomes are covered too so they cannot regress the same way: a bound socket in
TCP_CLOSElistens fine, re-listen()ing a socket already inTCP_LISTENis allowed, and an unbound socket fails withEINVAL.Fixes fd0a109a0f6b ("net, pidfs: prepare for handing out pidfds for reaped sk->sk_peer_pid")Signed-off-by John Ericson <[email protected]>
Signature · 1 line
Christian Brauner <[email protected]>
Kuniyuki Iwashima
maintainerRe: [PATCH net v2 2/3] selftests/net/af_unix: test listen() rejects wrong socket states
In reply to John EricsonQuoted 85 lines · John Ericson
From: John Ericson <[email protected]>
Add a regression test for the
unix_listen()state check. The key case islisten()on a bound socket that has already been connected: it is no longer inTCP_CLOSEorTCP_LISTEN, so it must fail withEINVAL. Aprepare_peercred()call slipped in ahead of that check once lefterrat 0 and madelisten()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_CLOSElistens fine, re-listen()ing a socket already inTCP_LISTENis allowed, and an unbound socket fails withEINVAL.Fixes fd0a109a0f6b ("net, pidfs: prepare for handing out pidfds for reaped sk->sk_peer_pid")Assisted-by Claude:claude-opus-4-8Signed-off-by John Ericson <[email protected]>--- .../testing/selftests/net/af_unix/.gitignore | 1 + tools/testing/selftests/net/af_unix/Makefile | 1 + .../selftests/net/af_unix/unix_listen.c | 126 ++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 tools/testing/selftests/net/af_unix/unix_listen.ctools/testing/selftests/net/af_unix/.gitignore +1
-0@@ -6,3 +6,4 @@ scm_rights so_peek_off unix_connect unix_connreset +unix_listentools/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.mktools/testing/selftests/net/af_unix/unix_listen.c new file +33
-0@@ -0,0 +1,126 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Tests for the state checks in AF_UNIX listen(). +* +* The central case is a regression test: listen() on a bound socket that +* is already connected (i.e. not in TCP_CLOSE or TCP_LISTEN state) must +* fail with EINVAL. A prior change accidentally let it return success +* without doing anything, because a helper called in between reset the +* error code to 0. The neighbouring checks (unbound, already listening) +* are tested too so they cannot silently regress the same way. +*/ +#define _GNU_SOURCE + +#include <errno.h> +#include <stddef.h> +#include <string.h> +#include <unistd.h> + +#include <sys/socket.h> +#include <sys/un.h> + +#include "kselftest_harness.h" + +/* Fill @addr with an abstract-namespace address named @name. */ +static socklen_t unix_abstract (struct sockaddr_un *addr,const char *name) +{ +size_t len =strlen (name); + +memset (addr,0 ,sizeof (*addr)); + addr->sun_family =AF_UNIX ; +/* Leading NUL selects the abstract namespace (no filesystem entry). */ +memcpy (addr->sun_path +1 , name, len); +
tools/testing/selftests/net/af_unix/unix_listen.c line 43
Quoted 10 lines
tools/testing/selftests/net/af_unix/unix_listen.c new file +10
-0+return offsetof(struct sockaddr_un , sun_path) +1 + len; +} + +FIXTURE (unix_listen) +{ +int sk;/* socket under test */ +int server;/* a listening peer, when a test needs one */ +struct sockaddr_un addr, srv_addr; +socklen_t addrlen, srv_addrlen; +};
Could you add test cases for SOCK_SEQPACKET and pathname sockets ? so it will be 4-patterns. see unix_connect.c
Quoted 15 lines
tools/testing/selftests/net/af_unix/unix_listen.c new file +15
-0+ +FIXTURE_SETUP (unix_listen) +{ + self->sk =-1 ; + self->server =-1 ; + self->addrlen =unix_abstract (&self->addr,"unix_listen.sk" ); + self->srv_addrlen =unix_abstract (&self->srv_addr,"unix_listen.srv" ); +} + +FIXTURE_TEARDOWN (unix_listen) +{ +if (self->sk >=0 ) +close (self->sk); +if (self->server >=0 ) +close (self->server);
tools/testing/selftests/net/af_unix/unix_listen.c line 68
Quoted 10 lines
tools/testing/selftests/net/af_unix/unix_listen.c new file +10
-0+/* Abstract addresses are released automatically on close. */ +} + +/* A bound socket in TCP_CLOSE is the normal, allowed case. */ +TEST_F(unix_listen, bound_is_ok) +{ + self->sk =socket (AF_UNIX ,SOCK_STREAM ,0 ); +ASSERT_LE (0 , self->sk); +ASSERT_EQ (0 ,bind (self->sk, (struct sockaddr *)&self->addr, + self->addrlen));
Personally I think this style is more readable:
err = func(...)ASSERT_XX(val, err)
Quoted 61 lines
tools/testing/selftests/net/af_unix/unix_listen.c new file +58
-1+ +EXPECT_EQ (0 ,listen (self->sk,8 )); +} + +/* Listening again on an already-listening socket (TCP_LISTEN) is allowed. */ +TEST_F(unix_listen, relisten_is_ok) +{ + self->sk =socket (AF_UNIX ,SOCK_STREAM ,0 ); +ASSERT_LE (0 , self->sk); +ASSERT_EQ (0 ,bind (self->sk, (struct sockaddr *)&self->addr, + self->addrlen)); +ASSERT_EQ (0 ,listen (self->sk,8 )); + +EXPECT_EQ (0 ,listen (self->sk,16 )); +} + +/* listen() on an unbound socket fails: there is nothing to listen on. */ +TEST_F(unix_listen, unbound_is_einval) +{ +int ret; + + self->sk =socket (AF_UNIX ,SOCK_STREAM ,0 ); +ASSERT_LE (0 , self->sk); + + ret =listen (self->sk,8 ); +EXPECT_EQ (-1 , ret); +EXPECT_EQ (EINVAL , errno); +} + +/* +* The regression: a bound socket that has already been connected is not in +* TCP_CLOSE or TCP_LISTEN, so listen() must reject it with EINVAL rather +* than quietly succeeding. +*/ +TEST_F(unix_listen, connected_is_einval) +{ +int ret; + + self->server =socket (AF_UNIX ,SOCK_STREAM ,0 ); +ASSERT_LE (0 , self->server); +ASSERT_EQ (0 ,bind (self->server, (struct sockaddr *)&self->srv_addr, + self->srv_addrlen)); +ASSERT_EQ (0 ,listen (self->server,8 )); + + self->sk =socket (AF_UNIX ,SOCK_STREAM ,0 ); +ASSERT_LE (0 , self->sk); +/* Bind first so the unbound check does not mask the state check. */ +ASSERT_EQ (0 ,bind (self->sk, (struct sockaddr *)&self->addr, + self->addrlen)); +ASSERT_EQ (0 ,connect (self->sk, (struct sockaddr *)&self->srv_addr, + self->srv_addrlen)); + + ret =listen (self->sk,8 ); +EXPECT_EQ (-1 , ret); +EXPECT_EQ (EINVAL , errno); +} + +TEST_HARNESS_MAIN --2.54.0
PATCH v2 3/3 af_unix: Clean up error handling in unix_listen()
In reply to John EricsonFrom: John Ericson <[email protected]>
To avoid the sort of bug that was just fixed going forward, switch to a style where err = -E...; instead happens right before the goto. (err = other_function(...); is not changed.) Then there is no spooky-action-at-a-distance between the err initialization and the goto, something which is easier to slip by code review.
--- net/unix/af_unix.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
net/unix/af_unix.c +9 -6
@@ -813,19 +813,22 @@ static int unix_listen(struct socket *sock, int backlog)struct unix_sock *u =unix_sk (sk);struct unix_peercred peercred = {}; - err = -EOPNOTSUPP ; -if (sock->type !=SOCK_STREAM && sock->type !=SOCK_SEQPACKET ) +if (sock->type !=SOCK_STREAM && sock->type !=SOCK_SEQPACKET ) { + err = -EOPNOTSUPP ; goto out;/* Only stream/seqpacket sockets accept */ - err = -EINVAL ; -if (!READ_ONCE (u->addr)) + } +if (!READ_ONCE (u->addr)) { + err = -EINVAL ; goto out;/* No listens on an unbound socket */ + } err =prepare_peercred (&peercred);if (err) goto out;unix_state_lock (sk); - err = -EINVAL ; -if (sk->sk_state !=TCP_CLOSE && sk->sk_state !=TCP_LISTEN ) +if (sk->sk_state !=TCP_CLOSE && sk->sk_state !=TCP_LISTEN ) { + err = -EINVAL ; goto out_unlock; + }if (backlog > sk->sk_max_ack_backlog)wake_up_interruptible_all (&u->peer_wait); sk->sk_max_ack_backlog = backlog;
Signature · 1 line
2.54.0
Re: [PATCH net-next v2 3/3] af_unix: Clean up error handling in unix_listen()
Quoted 7 lines
To avoid the sort of bug that was just fixed going forward, switch to a style where
err = -E...;instead happens right before thegoto. (err = other_function(...);is not changed.) Then there is no spooky-action-at-a-distance between theerrinitialization and thegoto, something which is easier to slip by code review.Signed-off-by John Ericson <[email protected]>
Signature · 1 line
Christian Brauner <[email protected]>
Kuniyuki Iwashima
maintainerRe: [PATCH net-next v2 3/3] af_unix: Clean up error handling in unix_listen()
In reply to John EricsonQuoted 8 lines · John Ericson
From: John Ericson <[email protected]>
To avoid the sort of bug that was just fixed going forward, switch to a style where
err = -E...;instead happens right before thegoto. (err = other_function(...);is not changed.) Then there is no spooky-action-at-a-distance between theerrinitialization and thegoto, something which is easier to slip by code review.
Sorry, I meant posting this separately to net-next.git after the patch 1 is merged to net.git and lands net-next.git.
Also could you remove markdown `` ?
Quoted 43 lines
Assisted-by Claude:claude-fable-5Signed-off-by John Ericson <[email protected]>--- net/unix/af_unix.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)net/unix/af_unix.c +9
-7@@ -813,19 +813,22 @@ static int unix_listen(struct socket *sock, int backlog)struct unix_sock *u =unix_sk (sk);struct unix_peercred peercred = {}; - err = -EOPNOTSUPP ; -if (sock->type !=SOCK_STREAM && sock->type !=SOCK_SEQPACKET ) +if (sock->type !=SOCK_STREAM && sock->type !=SOCK_SEQPACKET ) { + err = -EOPNOTSUPP ; goto out;/* Only stream/seqpacket sockets accept */ - err = -EINVAL ; -if (!READ_ONCE (u->addr)) + } +if (!READ_ONCE (u->addr)) { + err = -EINVAL ; goto out;/* No listens on an unbound socket */ + } err =prepare_peercred (&peercred);if (err) goto out;unix_state_lock (sk); - err = -EINVAL ; -if (sk->sk_state !=TCP_CLOSE && sk->sk_state !=TCP_LISTEN ) +if (sk->sk_state !=TCP_CLOSE && sk->sk_state !=TCP_LISTEN ) { + err = -EINVAL ; goto out_unlock; + }if (backlog > sk->sk_max_ack_backlog)wake_up_interruptible_all (&u->peer_wait); sk->sk_max_ack_backlog = backlog; --2.54.0
Quoted 10 lines
Commit
fd0a109a0f6b("net, pidfs: prepare for handing out pidfds for reapedsk->sk_peer_pid") inserted aprepare_peercred()call betweenerr = -EINVALand the socket-state check inunix_listen(). Sinceprepare_peercred()leaveserrat 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 withEINVALas it did before.Fixes fd0a109a0f6b ("net, pidfs: prepare for handing out pidfds for reaped sk->sk_peer_pid")Signed-off-by John Ericson <[email protected]>
Signature · 1 line
Christian Brauner <[email protected]>