Skip to contentSkip to conversation

SUNRPC: use-after-free in svc_one_sock_name() from svc_addsock()

BUG Piyus Kedia 1 participant 1 message Nov 28

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi,

svc_addsock() publishes the transport it has just created and then reads it back:

net/sunrpc/svcsock.c

	svsk->sk_xprt.xpt_cred = get_cred(cred);
	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 immediately. When the socket that was handed in is unbound, that thread deletes it right away and the svc_sock is freed before svc_add_new_perm_xprt() has even returned:

- an unbound AF_INET SOCK_STREAM descriptor passes every check in svc_addsock() - sockfd_lookup(), the netns, family PF_INET/PF_INET6, protocol TCP/UDP, and so->state > SS_UNCONNECTED;
- its sk_state is TCP_CLOSE, so svc_tcp_init() took the default: arm of its switch and called svc_xprt_deferred_close(). XPT_CLOSE is set before the transport is ever visible;
- the dequeuing nfsd thread therefore runs svc_delete_xprt(), which

    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, and frees the svc_sock.

svc_one_sock_name() then reads svsk->sk_sk out of the freed object.

This is reachable by a single write() of a descriptor number to /proc/fs/nfsd/portlist, which __write_ports_addfd() passes straight to svc_addsock(). It needs CAP_SYS_ADMIN over the nfsd filesystem; the write returns success.

WE HAVE REPRODUCED THIS ON AN UNMODIFIED MAINLINE KERNEL. Not only on the kernel the fuzzer runs: we built 9437ee8b6cf6 (your net-7.4-rc5 merge) straight from Linus' tree with KASAN, carrying no local patches of any kind, and reproduced it there. The kernel says so itself - the baseline boot line below has no "-dirty" suffix, which it would have if the tree had been touched:

7.4.0-rc4-00571-g9437ee8b6cf6 #1 SMP PREEMPT_DYNAMIC

The reproducer is a plain C program that brings nfsd up and then writes the descriptor of a fresh unbound AF_INET SOCK_STREAM socket to /proc/fs/nfsd/portlist:

  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 - the same task, the same write(2), eighteen bytes of text apart. svc_xprt_received() and svc_add_new_perm_xprt() are inlined into svc_addsock() in this build, which is why they do not appear by name.

WHETHER IT FIRES ON ANY GIVEN WRITE IS A RACE, and one that the writer usually wins: the nfsd thread has to dequeue the transport and get through svc_delete_xprt() within the few instructions between svc_xprt_enqueue() and the svc_xprt_put() that follows it. So a single write() normally reads a still-live object and reports nothing. Each write is an independent attempt, though, so the rate is what we measured
- 500 attempts per boot, with kasan_multi_shot so that every occurrence is printed and not just the first:

7.4.0-rc4-00571-g9437ee8b6cf6 #1 483 accepted 29 reports
7.4.0-rc4-00571-g9437ee8b6cf6-dirty #2 490 accepted 0 reports

Same tree, same config, same binary, same number of attempts; the only difference between #1 and #2 is the patch below, which is the whole of the "-dirty". The patched kernel had MORE live attempts than the baseline and produced no KASAN report of any kind.

(The reports carry a "W" taint from an unrelated set_cpu_sibling_map WARNING that this guest's CPU topology trips at boot, long before any of this.)

For completeness, the first sighting, on the 7.3.1-based kernel the fuzzer runs:

  BUG: KASAN: slab-use-after-free in svc_one_sock_name+0x38/0x180 net/sunrpc/svcsock.c:209
  Read of size 8 at addr ffff8880259752c0 by task syz-executor/10445
   svc_one_sock_name+0x38/0x180 net/sunrpc/svcsock.c:209
   svc_addsock+0x34e/0x360 net/sunrpc/svcsock.c:1558
   __write_ports_addfd fs/nfsd/nfsctl.c:749 [inline]
   __write_ports fs/nfsd/nfsctl.c:812 [inline]
   write_ports+0x596/0x960 fs/nfsd/nfsctl.c:868
   nfsctl_transaction_write+0xcc/0x110 fs/nfsd/nfsctl.c:114
   vfs_write+0x218/0x810 fs/read_write.c:685

  Allocated by task 10445:
   svc_setup_socket+0x76/0x320 net/sunrpc/svcsock.c:1454
   svc_addsock+0x27d/0x360 net/sunrpc/svcsock.c:1547

  Freed by task 10445:
   kfree+0x1c4/0x660 mm/slub.c:6808
   svc_sock_free+0xb1/0xd0 net/sunrpc/svcsock.c:1696
   svc_xprt_free+0x155/0x180 net/sunrpc/svc_xprt.c:181
   svc_xprt_put+0x2f/0x50 net/sunrpc/svc_xprt.c:187
   svc_xprt_received+0xce/0xf0 net/sunrpc/svc_xprt.c:250
   svc_add_new_perm_xprt+0xc1/0xf0 net/sunrpc/svc_xprt.c:260
   svc_addsock+0x33c/0x360 net/sunrpc/svcsock.c:1557

There too the allocation, the free and the read are all in the same task and the same write() syscall, with svc_xprt_received() and svc_add_new_perm_xprt() still visible by name in the free stack.

ed2849d3ecfa ("sunrpc: prevent use-after-free on clearing XPT_BUSY") already stated the rule:

So 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. It only touched svc_xprt.c, so svc_addsock() kept its dereference-after-publication. 06f90cf104fc ("SUNRPC: trace an accepted transport before publishing it") fixed the same class in svc_handle_xprt() three months ago, and Chuck's note on that one - "trace before publication instead of taking an extra reference" - is the shape of the patch that follows: format the name while the calling thread still owns the transport exclusively.

A patch is attached separately. We checked mainline as of 9437ee8b6cf6 (net-7.4-rc5 merge); if a fix is already queued in nfsd-next then please ignore this.

We have a reproducer - a short C program, no syzkaller needed - and can send it on request. We have left it out of this mail.

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

Thanks,
Piyus