Skip to contentSkip to conversation

SUNRPC: name a listener socket before publishing its transport

PATCH Superseded Piyus Kedia 2 participants 3 messages Nov 28 – Nov 30

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 SUNRPC: name a listener socket before publishing its transport

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")
---
 net/sunrpc/svcsock.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
net/sunrpc/svcsock.c +9 -1
index 498c5096eef9..3482ae59d8a0 100644
@@ -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;
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 5 lines · Piyus Kedia

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.

Quoted 6 lines

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.

net/sunrpc/svcsock.c int svc_addsock(struct svc_serv *serv, struct net *net, const int fd, · line 1669

Quoted 6 lines
net/sunrpc/svcsock.c +6 -0
+	/*
+	 * 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?

Signature · 1 line
Chuck Lever
#2 lore raw

Hi Chuck,

Thanks for the review. We have submitted three patches as suggested. Here are the links to each of them.

the series: https://lore.kernel.org/linux-nfs/[email protected]/
1/3 name a listener socket before publishing its transport:

    https://lore.kernel.org/linux-nfs/[email protected]/
  2/3 read a new listener's port before publishing its transport:
    https://lore.kernel.org/linux-nfs/[email protected]/
  3/3 refuse a stream socket that is not listening:
    https://lore.kernel.org/linux-nfs/[email protected]/

Kindly review and let us know if it is OK.

Thanks and regards,
Piyus Kedia
Kapil Vaswani