[PATCH v2 3/3] SUNRPC: refuse a stream socket that is not listening
From: Piyus Kedia
Date: Sun Nov 29 2026 - 12:51:06 EST
svc_addsock() takes any unconnected TCP socket. If it is not listening,
svc_tcp_init() goes down the connected-socket path, finds sk_state is
neither TCP_SYN_RECV nor TCP_ESTABLISHED, and marks the transport
closed - so nfsd builds a transport only to delete it again, while the
write() to portlist reports success.
A socket handed over this way is meant to be a listener. Require that
and return -EINVAL otherwise.
Suggested-by: Chuck Lever <cel@xxxxxxxxxx>
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---
net/sunrpc/svcsock.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c
index 17116b188db1..d14db642f6e1 100644
--- a/net/sunrpc/svcsock.c
+++ b/net/sunrpc/svcsock.c
@@ -1641,6 +1641,10 @@ int svc_addsock(struct svc_serv *serv, struct net *net, const int fd,
if (so->sk->sk_protocol != IPPROTO_TCP &&
so->sk->sk_protocol != IPPROTO_UDP)
goto out;
+ err = -EINVAL;
+ if (so->sk->sk_protocol == IPPROTO_TCP &&
+ so->sk->sk_state != TCP_LISTEN)
+ goto out;
err = -EISCONN;
if (so->state > SS_UNCONNECTED)
goto out;