------------------------------------------------------------------------ cache_read() (net/sunrpc/cache.c) guards the shared upcall-request state of a sunrpc cache channel with inode_lock(file_inode(filp)), commented "protect against multiple concurrent readers on this file". But the state it needs to protect -- the cache_request's rq->buf and rq->len -- is shared per struct cache_detail, not per inode, and on procfs every open() of the channel gets a brand-new inode (/proc/net/rpc entries force revalidation on every lookup and proc_get_inode() allocates a fresh inode each time), so the lock serialises nothing across readers: two concurrent readers of the same channel both pass the "rq->len == 0" check and both run cache_request(), concurrently formatting the same shared PAGE_SIZE buffer, and both store rq->len with plain, unsynchronised stores -- while cache_ioctl(FIONREAD) reads rq->len under cd->queue_lock, a lock the formatting write does not hold. A reader's copy_to_user() can also run concurrently with the other reader's formatting, delivering torn upcall text to the userspace cache daemons (gssd, nfsd's export/idmap caches). This is a true data race on plain C accesses, confirmed with KCSAN on mainline 7.3; the request lifetime itself is correctly protected by rq->readers, so this is data corruption (wrong/torn upcall text, failed daemon parsing), not memory unsafety. The detailed description: * Test environment / affected version Linux version 7.3.0 (gcc 13.3.0, GNU ld 2.42) #1 SMP PREEMPT_DYNAMIC -- vanilla mainline 7.3 tree; net/sunrpc/cache.c is byte-identical between the audited tree and 7.3, line numbers below are from 7.3. Data race confirmed with a KCSAN-enabled build of the same tree in QEMU/KVM x86_64. * Mechanism Every cache_detail exposes an upcall channel (channel file, created in create_cache_proc_entries(), cache.c:1670-1704, with private data pointing at the shared struct cache_detail) and serves reads through cache_read() (cache.c:841-910): cache.c:852 inode_lock(inode); /* "protect against multiple concurrent readers on this file" */ cache.c:855 spin_lock(&cd->queue_lock); cache.c:865 rq->readers++; /* under queue_lock -- safe */ cache.c:866 spin_unlock(&cd->queue_lock); cache.c:868 if (rq->len == 0) { cache.c:869 err = cache_request(cd, rq); /* formats rq->buf */ cache.c:872 rq->len = err; /* plain store */ cache.c:873 } cache.c:879 count = rq->len - rp->offset; /* plain read */ cache.c:882 copy_to_user(buf, rq->buf + rp->offset, count); cache.c:894 spin_lock(&cd->queue_lock); /* readers--, free */ Two things break this: 1. The lock is per inode, the data is per cache_detail. The /proc/net/rpc hierarchy is created with pde_force_lookup() (fs/proc/proc_net.c:393, propagated to children in fs/proc/generic.c:482-483) and proc_net_d_revalidate() (fs/proc/generic.c:352-356) returns 0 unconditionally, so every path-walk lookup invalidates the cached dentry and proc_get_inode() (fs/proc/inode.c:628-635) allocates a NEW inode via new_inode(). Any two open()s of the channel therefore hold two different inodes and two different inode_lock()s; the "concurrent readers of this file" that the comment worries about are never actually serialised. The pipefs channel (cache_read_pipefs(), cache.c:1759) reaches the same code through a different filesystem with the same outcome. 2. The critical state is touched outside every lock. After cache.c:866 the reader holds only its own inode_lock, while rq->buf and rq->len belong to the cache_request that sits on the shared cd->requests list. Concurrent readers both observe rq->len == 0 and both run cache_request() over the same buffer (word-level write/write race; each formatter starts from offset 0), both store rq->len, and a reader that got past the check can copy_to_user() from the buffer while the other is still formatting it. cache_ioctl(FIONREAD) (cache.c:990-1012) reads "rq->len - rp->offset" under cd->queue_lock (cache.c:1008) -- a lock the rq->len store at cache.c:872 does not hold, so that read/write pair is a plain data race as well. What is NOT broken: the lifetime of rq. rq->readers is incremented and decremented under cd->queue_lock and a request is only freed when its reader count drops to zero while not CACHE_PENDING (cache.c:894-905, cache_dequeue(), cache_release()), so the two kfree()-adjacent races that a static analysis flags on this function are genuinely refuted. This report is about the content/length consistency of the shared buffer, which no lock currently covers. * Trigger model The channel files are mode 0600, so this needs root or the identity of the legitimate cache daemon. Two concurrent readers of the same channel (e.g. two gssd instances, or a daemon racing a manual inspection) is the natural trigger; racing a read() with ioctl(FIONREAD) on one fd needs no second opener at all. A cache miss must have queued an upcall (cache_do_upcall()) for a request to exist. Consequence: torn/garbled upcall text delivered to userspace (daemon parse failures for RPCSEC_GSS and nfsd caches), and a formal data race under the C memory model. No memory-unsafety: the formatters are bounded by the remaining-length convention (qword_add()/qword_addhex(), cache.c:1122-1170) and cache_request() rejects overflow with -E2BIG, and copy_to_user()'s count is derived from a length that is always <= PAGE_SIZE. * Reproducer (attached: rpcrace.c + rpcrd.c) and results on 7.3 Because the reporting environment has no nfsd running (no pre-existing auth caches with live traffic), the reproducer registers its own cache_detail ("racetest", exposed at /proc/net/rpc/racetest/channel via the exported cache_create_net()/cache_register_net()) whose cache_request formats a deterministic text line, and a kernel feeder thread calls sunrpc_cache_upcall() continuously so requests keep appearing on cd->requests. rpcrd.c is a small userspace racer: concurrent read() loops (mode r) and an FIONREAD loop (mode c). # build (module + static userspace racer) make -C M=$PWD modules clang -O2 -static -o rpcrd rpcrd.c # run on a KCSAN kernel (kcsan runtime enabled for the window): insmod rpcrace.ko mode=mr02 secs=40 /rpcrd r 40 & /rpcrd r 40 & /rpcrd r 40 & /rpcrd c 40 & Results (7.3 KCSAN build, 8 vCPU, one 40s window): 25 cache-related reports, two shapes: - "BUG: KCSAN: data-race in cache_read / cache_read": write of 4 bytes at cache_read+0x1d3 (the rq->len = err store) by one task vs read of 4 bytes at cache_read+0x127 (the rq->len == 0 check) by another task on another CPU -- the concurrent formatting decision itself; - "BUG: KCSAN: data-race in _copy_to_user / string_escape_mem" x11: read of 134 bytes (_copy_to_user <- cache_read, the copy_to_user() of rq->buf) vs write of 1 byte (string_escape_mem <- qword_add <- cache_request <- cache_read) at the same buffer -- the torn upcall text delivered to userspace while the sibling reader is still formatting it. A manual sanity pass also shows the functional effect: with two readers started together the channel output of one reader comes back interleaved/truncated. Serial logs and report excerpts are available on request. * Fix The patch below formats the upcall text while holding cd->queue_lock. cache_request() is pure formatting into the preallocated page (the formatters only respect the remaining-length convention and never sleep), so the lock hold is bounded and short; it makes the "rq->len == 0 -> formatted" transition single-stepped, puts the rq->len store under the same lock cache_ioctl() reads it under, and guarantees every copy_to_user() is strictly ordered after the formatting of the same request (a reader can only reach the copy once rq->len is set, and from that moment nobody writes rq->buf again). A negative length (formatting error) is made sticky for the request so subsequent readers get the error instead of a negative copy count. The patch has been reviewed but not runtime-tested (the race itself is KCSAN-verified as above); happy to test alternatives, e.g. a per-cache_request state bit with a waitqueue if the serialisation is considered too coarse. Please keep me in Cc; I am not subscribed to the lists. Regards, Hanxiang Xu ------------------------------------------------------------------------ The patch (text/plain; apply with patch -p1 or git am): From: Hanxiang Xu Subject: [PATCH] SUNRPC: format the cache upcall text under queue_lock cache_read() serialises concurrent readers with inode_lock(inode), but the state it protects -- the cache_request's rq->buf and rq->len -- is shared per cache_detail, not per inode: /proc/net/rpc entries force dentry revalidation on every lookup (proc_net_d_revalidate() returns 0) and proc_get_inode() allocates a fresh inode each time, so every open() of a channel file gets its own inode and the VFS lock never actually serialises two readers. Concurrent readers both observe rq->len == 0 and both run cache_request() over the same shared buffer (word-level write/write race), a reader's copy_to_user() of rq->buf can overlap the sibling's formatting (torn upcall text delivered to gssd/nfsd cache daemons), and the rq->len store races the cache_ioctl(FIONREAD) read, which is done under cd->queue_lock -- a lock the store does not hold. All three were confirmed with KCSAN on 7.3 against a private cache_detail with concurrent readers and an FIONREAD loop ("data-race in cache_read / cache_read" for the rq->len store vs check, "data-race in _copy_to_user / string_escape_mem" for the buffer content). Format the upcall text while holding cd->queue_lock: cache_request() is pure formatting into the preallocated page and cannot sleep, the hold is bounded, and it makes the rq->len transition single-stepped and ordered against both the sibling readers and cache_ioctl(). The copy_to_user() can then only run once rq->len is set, after which nobody writes rq->buf again. A negative cache_request() result is stored and made sticky for the request so later readers receive the error rather than computing a negative copy count. The request lifetime itself is unaffected: rq->readers under queue_lock already protects rq from premature free. Signed-off-by: Hanxiang Xu --- net/sunrpc/cache.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) --- a/net/sunrpc/cache.c +++ b/net/sunrpc/cache.c @@ -863,13 +863,26 @@ } if (rp->offset == 0) rq->readers++; + /* + * Format the upcall text while holding queue_lock. rq is shared + * by every reader of this cache: each open() of the channel gets a + * fresh inode (procfs revalidates /proc/net/rpc entries on every + * lookup and allocates a new inode), so the VFS inode_lock() above + * serialises nothing across readers, and cache_ioctl(FIONREAD) + * reads rq->len under this same lock. cache_request() is pure + * formatting into the preallocated page and cannot sleep. Doing + * it under the lock makes the "rq->len == 0 -> formatted" + * transition single-stepped and keeps the copy_to_user() below + * strictly ordered after the formatting of the same request. + */ + if (rq->len == 0) + rq->len = cache_request(cd, rq); spin_unlock(&cd->queue_lock); - if (rq->len == 0) { - err = cache_request(cd, rq); - if (err < 0) - goto out; - rq->len = err; + /* a formatting error is sticky for this request */ + if (rq->len < 0) { + err = rq->len; + goto out; } if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) { net/sunrpc/cache.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) ------------------------------------------------------------------------