[PATCH v2 1/3] SUNRPC: name a listener socket before publishing its transport
From: Piyus Kedia
Date: Sun Nov 29 2026 - 12:50:58 EST
svc_addsock() publishes the new transport 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 pick it up immediately. The socket need
not be connected: an unbound AF_INET SOCK_STREAM descriptor passes every
check here, 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 set before the transport is visible, so
the dequeuing thread deletes it at once and drops the list reference -
the only one a new transport holds. The svc_sock is freed before
svc_add_new_perm_xprt() has returned, and svc_one_sock_name() reads
svsk->sk_sk out of it:
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
vfs_write+0x218/0x810
Freed by task 9512:
kfree+0x1c9/0x660
svc_xprt_put+0x398/0x540
svc_addsock+0x37b/0x3a0
Same task and the same write(2): the free is at svc_addsock+0x37b and
the read at +0x38d. It is reached by writing a descriptor number to
/proc/fs/nfsd/portlist.
Format the name before publishing, while this thread still owns the
transport. 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 them accepted, this fired 29 times. With the
patch, 490 accepted attempts produced no report.
We found this with a syzkaller-based fuzzing harness driven by an AI tool.
Fixes: b41b66d63c73 ("[PATCH] knfsd: allow sockets to be passed to nfsd via 'portlist'")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---
net/sunrpc/svcsock.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c
index 498c5096eef9..17116b188db1 100644
--- a/net/sunrpc/svcsock.c
+++ b/net/sunrpc/svcsock.c
@@ -1660,8 +1660,10 @@ 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);
+ /* svc_add_new_perm_xprt() drops this thread's only reference */
+ 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;
base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48