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

From: Chuck Lever

Date: Sat Nov 28 2026 - 13:20:34 EST


On Sat, Nov 28, 2026, Piyus Kedia wrote:
> 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);

The reported issue is confirmed, but a few things need to change before
I can apply the fix.


> 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.

ed2849d3ecfa did not introduce this bug. At that commit, svc_addsock()
already called svc_xprt_received() before svc_one_sock_name(). The
svc_addsock() ordering goes back to b41b66d63c73 ("[PATCH] knfsd:
allow sockets to be passed to nfsd via 'portlist'"). Let's use that
commit for the Fixes: tag.

The fix to svc_create_xprt() in ed2849d3ecfa also did not survive.
39b553013719 ("svcrpc: share some setup of listening sockets") moved
the svc_xprt_local_port() call back after publication, and
_svc_xprt_create() still reads newxprt after svc_add_new_perm_xprt()
returns. Let's drop that part of the patch description, and add a
second patch that moves the svc_xprt_local_port() call in
_svc_xprt_create() ahead of svc_add_new_perm_xprt(), with Fixes:
39b553013719.


> + /*
> + * 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.
> + */

One line is enough here, for example:

/* svc_add_new_perm_xprt() drops this thread's only reference */


Lastly, svc_addsock() should not accept a stream socket that is not
listening. Can you add a third patch that fails such a socket with
-EINVAL, without a Fixes: tag?


--
Chuck Lever