[PATCH] SUNRPC: name a listener socket before publishing its transport

From: Piyus Kedia

Date: Sat Nov 28 2026 - 05:29:59 EST


svc_addsock() hands the new transport to the service and then reads it
back:

svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
return svc_one_sock_name(svsk, name_return, len);

svc_add_new_perm_xprt() puts the transport on sv_permsocks and clears
XPT_BUSY, so an nfsd thread can dequeue it straight away. The socket
handed in need not be connected: an unbound AF_INET SOCK_STREAM
descriptor passes every check in svc_addsock(), and for such a socket
svc_tcp_init() has already called svc_xprt_deferred_close(), because
sk_state is neither TCP_SYN_RECV nor TCP_ESTABLISHED. XPT_CLOSE is
therefore set before the transport is ever visible, the dequeuing thread
runs svc_delete_xprt() at once, and that drops the list reference - the
only reference a new transport holds. The svc_xprt_put() at the end of
svc_xprt_received() is then the last one, so the svc_sock is freed
before svc_add_new_perm_xprt() has returned, and svc_one_sock_name()
reads svsk->sk_sk out of the freed object:

BUG: KASAN: slab-use-after-free in svc_one_sock_name+0x23/0x150
Read of size 8 at addr ffff888026d502c0 by task repro/9512
svc_one_sock_name+0x23/0x150
svc_addsock+0x38d/0x3a0
write_ports+0x608/0x9e0
nfsctl_transaction_write+0xcc/0x110
vfs_write+0x218/0x810
ksys_write+0x123/0x200

Allocated by task 9512:
__kmalloc_noprof+0x36a/0x720
svc_setup_socket+0xf4/0xa30
svc_addsock+0x29b/0x3a0

Freed by task 9512:
kfree+0x1c9/0x660
svc_xprt_put+0x398/0x540
svc_addsock+0x37b/0x3a0

The free is at svc_addsock+0x37b and the read at svc_addsock+0x38d, in
the same task and the same write(2). It is reached by writing a
descriptor number to /proc/fs/nfsd/portlist, which
__write_ports_addfd() passes straight to svc_addsock().

Commit ed2849d3ecfa ("sunrpc: prevent use-after-free on clearing
XPT_BUSY") stated the rule - "when a creator clears XPT_BUSY it is
dropping its only reference and so must not touch the xprt again" - and
fixed svc_create_xprt() by reading the port it wanted to return before
publishing the transport. It only touched svc_xprt.c, so svc_addsock()
kept its dereference-after-publication.

Do the same here. Before publication the calling thread owns the
transport exclusively, so format the name there. Nothing between the
two statements changes what svc_one_sock_name() prints, and the
transport is still published when the name does not fit, as before.

Over 500 attempts on v7.4-rc4-00571-g9437ee8b6cf6 with KASAN and
kasan_multi_shot, 483 of which were accepted, this fired 29 times. With
this patch, 490 accepted attempts produced no KASAN report at all.

We found this with a syzkaller-based fuzzing harness driven by an AI tool.

Fixes: ed2849d3ecfa ("sunrpc: prevent use-after-free on clearing XPT_BUSY")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---
net/sunrpc/svcsock.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c
index 498c5096eef9..3482ae59d8a0 100644
--- a/net/sunrpc/svcsock.c
+++ b/net/sunrpc/svcsock.c
@@ -1660,8 +1660,16 @@ int svc_addsock(struct svc_serv *serv, struct net *net, const int fd,
if (salen >= 0)
svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
svsk->sk_xprt.xpt_cred = get_cred(cred);
+
+ /*
+ * Format the name while this thread still owns the transport
+ * exclusively. svc_add_new_perm_xprt() clears XPT_BUSY, and an
+ * nfsd thread can then delete and free the transport before
+ * svc_add_new_perm_xprt() has returned.
+ */
+ err = svc_one_sock_name(svsk, name_return, len);
svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
- return svc_one_sock_name(svsk, name_return, len);
+ return err;
out:
sockfd_put(so);
return err;
--
2.34.1