SUNRPC: rpc_switch_client_transport() removes the client pipedir without pipefs_sb_lock, racing the RPC_PIPEFS_UMOUNT notification chain into a double removal (UAF)
From: 徐晗翔
Date: Thu Nov 26 2026 - 07:55:21 EST
------------------------------------------------------------------------
rpc_switch_client_transport() (net/sunrpc/clnt.c) is the NFSv4
migration/referral transport-replacement path (nfs4_update_server(),
fs/nfs/nfs4client.c). After switching transports it calls
rpc_unregister_client() and then __rpc_clnt_remove_pipedir() -- and
that second call is the only place in the file that touches the client
pipedir WITHOUT holding sn->pipefs_sb_lock. Every other mutator --
rpc_clnt_remove_pipedir(), the RPC_PIPEFS_MOUNT/RPC_PIPEFS_UMOUNT
notification handlers, and the killers rpc_fill_super()/rpc_kill_sb()
-- runs under that mutex. When a umount of rpc_pipefs is in flight,
rpc_kill_sb() holds pipefs_sb_lock and walks sn->all_clients through
the notification chain, handing out bare client pointers; the umount
side and the switch side can therefore both read the same non-NULL
clnt->pipefs_sb, cl_pipedir_objects.pdh_dentry and pipe->dentry before
either clears them, and both execute the same removal sequence:
double rpc_remove_client_dir()/simple_recursive_removal() on the same
dentries, a second rpc_close_pipes() on a pipe whose
RPC_I(inode)->pipe has already been cleared (spin_lock(&pipe->lock) on
a NULL pointer), and unsynchronised accesses to clnt->pipefs_sb and
pdh_dentry. Verified on mainline 7.3 with a choreographed reproducer:
the resumed umount-side removal oopses with a NULL-pointer write to
inode->i_rwsem (address 0x98) inside __simple_recursive_removal(),
followed by slab-use-after-free reports on the corrupted pipefs_sb_lock
state; the VM does not survive the resulting lockup.
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/clnt.c and
net/sunrpc/rpc_pipe.c are byte-identical between the audited tree
and 7.3, line numbers below are from 7.3. Tested with KASAN enabled
in QEMU/KVM x86_64.
* Mechanism
The unlocked call site (clnt.c:765-766 in
rpc_switch_client_transport()):
rpc_unregister_client(clnt);
__rpc_clnt_remove_pipedir(clnt); /* NO pipefs_sb_lock */
__rpc_clnt_remove_pipedir() (clnt.c:97-103) reads clnt->pipefs_sb
without synchronisation and, if non-NULL, calls
rpc_remove_client_dir() (rpc_pipe.c:890-900), which reads
pdh_dentry (a borrowed dentry reference, no lock, no refcount),
runs rpc_destroy_pipe_dir_objects() over the pdh_entries (for each
pipe_dir_object: rpc_unlink(pipe), i.e.
simple_recursive_removal(pipe->dentry, rpc_close_pipes) followed by
the unlocked "pipe->dentry = NULL"), then sets pdh_dentry = NULL and
calls simple_recursive_removal(D, NULL) on the pipedir dentry
itself, and finally clears clnt->pipefs_sb.
The concurrent side is rpc_kill_sb() (rpc_pipe.c:1188-1208), which
holds pipefs_sb_lock, sets sn->pipefs_sb = NULL and synchronously
dispatches RPC_PIPEFS_UMOUNT through the blocking notifier chain;
the handler walks sn->all_clients via
rpc_get_client_for_event() (bare pointer, no reference;
a client with a pipedir is not skipped for UMOUNT,
clnt.c:174-176) and executes the identical removal sequence in
__rpc_clnt_handle_event() (clnt.c:194-199):
"if (clnt->pipefs_sb == sb) { __rpc_clnt_remove_pipedir(clnt);
clnt->pipefs_sb = NULL; }".
Both sequences start from the same unsynchronised reads. When they
overlap (the umount side's scan happens before the switch side's
rpc_unregister_client() removes the client from the list, or
concurrently with it), both read pdh_dentry == D and pipe->dentry ==
pd before either clears them, and then:
1. Both run rpc_unlink() on the same pipe: both see a non-NULL
pipe->dentry and both call simple_recursive_removal(pd,
rpc_close_pipes). The first invocation frees pd; the second
resumes on the freed/discarded dentry. Depending on where the
two threads interleave inside rpc_close_pipes() (rpc_pipe.c:
170-195), the second invocation either reads
RPC_I(inode)->pipe after the first one cleared it and does
spin_lock(&pipe->lock) on a NULL pointer -- a kernel oops --
or, if it read it earlier, runs pipe->ops->release_pipe(inode)
a second time (gss_pipe_release() twice ->
put_pipe_version() twice -> sn->pipe_users underflow and a
corrupted pipe_version state machine, i.e. every RPCSEC_GSS
upcall in that netns stalls with -EAGAIN).
2. Both run simple_recursive_removal(D, NULL) on the pipedir
dentry, and both run the plain (unsynchronised) writes to
clnt->pipefs_sb and pdh_dentry -- KCSAN-visible data races
even when the interleaving stays benign.
* Trigger model
Both knobs are root-domain operations: umount/mount of rpc_pipefs
requires CAP_SYS_ADMIN in the initial user namespace (the rpc_pipefs
fs_type has no FS_USERNS_MOUNT), and the transport switch is reached
through NFSv4 migration/referral recovery --
nfs4_replace_transport() -> nfs4_try_replacing_one_location()
(fs/nfs/nfs4namespace.c) -> nfs4_update_server() ->
rpc_switch_client_transport() (fs/nfs/nfs4client.c:1183). The
client's pipedir entries are plain in-tree dentries
(d_make_persistent) and do not pin the mount, so nothing structurally
prevents the umount from proceeding while a transport switch is
running; the rpc_kill_sb() notifier only needs the client's
clnt->pipefs_sb to still equal the dying superblock, which is the
normal state before the switch side clears it. Consequence: kernel
oops (NULL-pointer write), dentry/inode corruption, and a
permanently wedged pipefs_sb_lock for everything else in that netns.
* Reproducer (attached: rpcrace.c) and results on 7.3
rpcrace.c builds the exact preconditions with exported APIs (no
kernel-source changes): it creates a client whose pipedir carries a
child pipe object (rpc_mkpipe_data() + rpc_add_pipe_dir_object(),
mirroring what a sec=krb5 client gets from its gss pipes), then a
kprobe stalls the umount task inside
simple_recursive_removal() -- filtered to calls with callback ==
rpc_close_pipes (address resolved at runtime from /proc/kallsyms)
and comm == "umount" -- while a module thread drives
rpc_switch_client_transport() against the stalled umount. A
release timer lets the umount resume after the switch side's
unlocked removal has completed. This widens the natural
microseconds window deterministically, following the
controlled-window methodology; it does not by itself measure natural
hit rates.
Results (7.3, KASAN, QEMU 8 vCPU): the resumed umount-side removal
reports "BUG: KASAN: null-ptr-deref in down_write -- Write of size 8
at address 0x98 (= inode->i_rwsem) by task umount" with the stack
umount -> rpc_kill_sb -> blocking_notifier_call_chain ->
rpc_pipefs_event -> rpc_remove_client_dir -> rpc_unlink ->
__simple_recursive_removal, followed by "BUG: KASAN:
slab-use-after-free in mutex_spin_on_owner" on the corrupted lock
state; the virtual machine then hangs and is torn down by the
harness. A note on the manifestation shape: dcache frees dentries
via RCU and dentry_unlink_inode() clears d_inode synchronously
during the first removal, so the resumed access hits a still
allocated-but-detached dentry and surfaces as the NULL-pointer write
above rather than a dentry slab-use-after-free report; the follow-up
mutex owner read does report a slab-use-after-free. Full serial log
and report excerpts are available on request.
* Fix
The patch below replaces the unlocked __rpc_clnt_remove_pipedir()
with the locked helper rpc_clnt_remove_pipedir(), which takes
pipefs_sb_lock, only removes the pipedir when clnt->pipefs_sb still
matches the mounted superblock, and releases the lock -- making the
switch path identical to every other pipedir mutator in the file and
serialising it against both notification chains. One-line change;
reviewed against the audit interleaving but not runtime-tested (the
race itself is dynamically verified as described above). Happy to
test.
Please keep me in Cc; I am not subscribed to the lists.
Regards,
Hanxiang Xu <xuhx@xxxxxxxxxxx>
------------------------------------------------------------------------
The patch (text/plain; apply with patch -p1 or git am):
From: Hanxiang Xu <xuhx@xxxxxxxxxxx>
Subject: [PATCH] SUNRPC: remove the client pipedir under pipefs_sb_lock
in rpc_switch_client_transport()
rpc_switch_client_transport() calls __rpc_clnt_remove_pipedir()
without holding sn->pipefs_sb_lock. It is the only pipedir mutator in
the file that does so; every other one (rpc_clnt_remove_pipedir() and
the RPC_PIPEFS_MOUNT/UMOUNT notification handlers, dispatched from
rpc_fill_super()/rpc_kill_sb()) runs under that mutex.
Because the notification chains hand out bare client pointers and both
sides read the same clnt->pipefs_sb, cl_pipedir_objects.pdh_dentry and
pipe->dentry without synchronisation, a concurrent umount of rpc_pipefs
and a transport switch (NFSv4 migration/referral recovery,
nfs4_update_server()) can both enter the removal sequence for the same
pipedir: double rpc_remove_client_dir()/simple_recursive_removal() on
the same dentries, a second rpc_close_pipes() whose
RPC_I(inode)->pipe has already been cleared (NULL-pointer
spin_lock(&pipe->lock) oops), a double gss_pipe_release() (pipe_users
underflow corrupting the RPCSEC_GSS pipe-version state machine), and
unsynchronised clnt->pipefs_sb / pdh_dentry accesses.
Use the locked rpc_clnt_remove_pipedir() helper -- it takes
pipefs_sb_lock, only removes the pipedir when clnt->pipefs_sb still
matches the mounted superblock, and releases the lock -- making the
switch path identical to the rest of the file. Verified by a
choreographed reproducer (kprobe-stalled umount inside
simple_recursive_removal() while rpc_switch_client_transport() runs
its unlocked removal): before the patch the resumed umount oopses with
a NULL-pointer write to inode->i_rwsem followed by slab-use-after-free
reports on the corrupted lock state.
Signed-off-by: Hanxiang Xu <xuhx@xxxxxxxxxxx>
---
net/sunrpc/clnt.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
--- a/net/sunrpc/clnt.c
+++ b/net/sunrpc/clnt.c
@@ -763,7 +763,19 @@
oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
rpc_unregister_client(clnt);
- __rpc_clnt_remove_pipedir(clnt);
+ /*
+ * Remove the pipedir under pipefs_sb_lock. The unlocked
+ * __rpc_clnt_remove_pipedir() races the RPC_PIPEFS_UMOUNT and
+ * RPC_PIPEFS_MOUNT notification chains (rpc_kill_sb() and
+ * rpc_fill_super() run them while holding pipefs_sb_lock): both
+ * sides can read the same non-NULL clnt->pipefs_sb /
+ * cl_pipedir_objects.pdh_dentry / pipe->dentry before either
+ * clears them, producing double rpc_remove_client_dir()/
+ * simple_recursive_removal() on the same dentries, a second
+ * rpc_close_pipes() on a pipe whose RPC_I(inode)->pipe was
+ * already cleared, and unsynchronised clnt->pipefs_sb accesses.
+ */
+ rpc_clnt_remove_pipedir(clnt);
rpc_sysfs_client_destroy(clnt);
rpc_clnt_debugfs_unregister(clnt);
net/sunrpc/clnt.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletions(-)
------------------------------------------------------------------------------------------------------------------------------------------------
// SPDX-License-Identifier: GPL-2.0
/*
* rpcrace.c - controlled-window verification instruments for the net/sunrpc
* conditional-race audit cases SUNRPC-MR-01..04
* (docs/manual-review-net-sunrpc-2026-11-25.md, v4).
*
* Methodology follows this environment's established controlled-window
* precedent (r4_stall.c, r1stall.c, fx*): zero kernel-source changes; a
* loadable module drives exported core APIs (rpc_create, rpcauth_register,
* rpc_switch_client_transport, rpc_mkpipe_data/rpc_add_pipe_dir_object,
* cache_create_net/sunrpc_cache_upcall) and kprobe pre-handlers widen the
* natural race windows into deterministic choreographies.
*
* Modes (module param "mode"):
* mr03pfs - SUNRPC-MR-03 pipefs flavor. Custom authops->create() holds the
* rpc_client_register() failure window open until a userspace
* helper has open(2)'d the failing client's <pipefs>/nfs/clntXX/info
* file (open takes a cl_count reference via rpc_info_open). After
* the window closes rpc_new_client() kfree()s the client anyway
* and the helper read()s/closes the fd -> use-after-free in
* rpc_show_info()/rpc_info_release().
* mr03dbg - same choreography against the debugfs "tasks" file
* (/sys/kernel/debug/sunrpc/rpc_clnt/<clid>/tasks, tasks_open
* takes the cl_count reference; full_proxy_release forwards
* tasks_release unconditionally). Only meaningful on a kernel
* built with CONFIG_SUNRPC_DEBUG (debugfs.c compiled in).
* mr01 - SUNRPC-MR-01. Double-kprobe choreography:
* kp1 = rpc_unregister_client entry, claimed by our failing
* client thread at err_auth (after rpc_get_sb_net() already saw
* pipefs unmounted and released pipefs_sb_lock);
* kp2 = rpc_create_client_dir entry, claimed by the concurrent
* "mount -t rpc_pipefs" notifier while it holds pipefs_sb_lock
* and has already written clnt->pipefs_sb = sb.
* Release order (A resumes -> unregister/remove/kfree, then B
* resumes) reproduces the audited interleaving: B writes
* pdh_dentry into the freed rpc_clnt (UAF write) and leaves an
* orphan pipedir whose info file private points at freed memory.
* mr04 - SUNRPC-MR-04. kprobe stall on simple_recursive_removal() when
* called with callback == rpc_close_pipes from the umount task,
* while a module thread drives rpc_switch_client_transport()
* (which calls __rpc_clnt_remove_pipedir() WITHOUT
* pipefs_sb_lock). Both threads read the same pdh_dentry /
* pipe->dentry before either clears it -> the umount thread
* resumes simple_recursive_removal() on the already-freed pipe
* child dentry (use-after-free).
* mr02 - SUNRPC-MR-02 (KCSAN kernel). Module-owned cache_detail
* ("racetest", /proc/net/rpc/racetest/channel) + an upcall
* feeder; userspace runs two concurrent readers and a
* FIONREAD ioctl loop against the shared cache_request so that
* cache_read()/cache_read()/cache_ioctl() race on rq->buf and
* rq->len (plain, unsynchronised accesses).
*
* Handshake with userspace (see scripts/init-rpcrace):
* /sys/module/rpcrace/parameters/event - readonly state machine
* /sys/module/rpcrace/parameters/helper_ready - write 1 to complete the
* window (mr03*); mr01/mr04 choreograph internally.
*/
#define pr_fmt(fmt) "RPCRACE: " fmt
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/kprobes.h>
#include <linux/kthread.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/in.h>
#include <linux/kref.h>
#include <linux/jiffies.h>
#include <linux/timer.h>
#include <linux/atomic.h>
#include <linux/sunrpc/clnt.h>
#include <linux/sunrpc/auth.h>
#include <linux/sunrpc/msg_prot.h>
#include <linux/sunrpc/rpc_pipe_fs.h>
#include <linux/sunrpc/cache.h>
#include <linux/sunrpc/xprt.h>
#include <linux/sunrpc/stats.h>
static char *mode = "mr03pfs";
static int hold_ms = 8000;
static int iters = 2;
static int stall_ms = 4000;
static int secs = 30;
static int auth_ok;
static unsigned long cb_addr;
static bool do_hold;
module_param(mode, charp, 0400);
module_param(hold_ms, int, 0400);
module_param(iters, int, 0400);
module_param(stall_ms, int, 0400);
module_param(secs, int, 0400);
module_param(auth_ok, int, 0400);
module_param(cb_addr, ulong, 0400);
MODULE_PARM_DESC(mode, "mr03pfs | mr03dbg | mr01 | mr04 | mr02");
MODULE_PARM_DESC(cb_addr, "runtime address of rpc_close_pipes (mr04)");
/* ------------------------------------------------------------------ */
/* state machine */
/* event==1 : A stalled / window open (helper may act) */
/* event==2 : B stalled (mr01: mount inside create_client_dir; */
/* mr04: umount inside simple_recursive_removal) */
/* event==3 : failing client freed / done */
/* ------------------------------------------------------------------ */
static atomic_t race_state = ATOMIC_INIT(0);
static DECLARE_COMPLETION(helper_compl);
static DECLARE_COMPLETION(go_compl);
static struct task_struct *worker;
static int kp_unreg_hit, kp_ccd_hit, kp_srr_hit;
/* ------------------------- fake rpc program ------------------------ */
static u32 race_counts[1];
static const struct rpc_procinfo race_procs[1] = { { 0 } };
static const struct rpc_version race_v1 = {
.number = 1,
.nrprocs = 1,
.procs = race_procs,
.counts = race_counts,
};
static const struct rpc_version *race_version[1] = { &race_v1 };
static struct rpc_stat race_stat;
static const struct rpc_program race_program = {
.name = "rpcrace",
.number = 200000,
.nrvers = 1,
.version = race_version,
.stats = &race_stat,
.pipe_dir_name = "nfs", /* exists in rpc_pipefs root */
};
/* ------------------------- fake rpc auth --------------------------- */
static void race_auth_destroy(struct rpc_auth *auth) { }
static struct rpc_auth race_fake_auth;
/*
* create() is called from rpcauth_create() inside rpc_client_register()
* (clnt.c L322) - i.e. AFTER the client was published to sn->all_clients
* (L318) and after the pipefs pipedir was created (L313-315). Holding this
* callback open holds the audited publication window open (mr03*).
*/
static struct rpc_auth *
race_auth_create(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
{
if (auth_ok) {
refcount_set(&race_fake_auth.au_count, 1);
return &race_fake_auth;
}
if (do_hold) {
pr_info("window OPEN (hold_ms=%d), clnt=%p clid=%d\n",
hold_ms, clnt, clnt->cl_clid);
atomic_set(&race_state, 1);
wait_for_completion_timeout(&helper_compl,
msecs_to_jiffies(hold_ms));
}
return ERR_PTR(-EINVAL);
}
static const struct rpc_authops race_authops = {
.owner = THIS_MODULE,
.au_flavor = RPC_AUTH_KRB, /* 4, unregistered in-kernel */
.au_name = "rpcrace",
.create = race_auth_create,
.destroy = race_auth_destroy,
};
/* ------------------------- kprobe machinery ------------------------ */
static atomic_t arm = ATOMIC_INIT(0);
static atomic_t release_flag = ATOMIC_INIT(0);
static void spin_until(int target)
{
unsigned long until = jiffies + msecs_to_jiffies(stall_ms);
while (atomic_read(&race_state) < target && time_before(jiffies, until))
cpu_relax();
}
/* mr01 kp1: rpc_unregister_client entry on the failing-client thread */
static int mr01_kp_unreg_pre(struct kprobe *p, struct pt_regs *regs)
{
if (!atomic_read(&arm) || kp_unreg_hit || current != worker)
return 0;
kp_unreg_hit = 1;
atomic_set(&race_state, 1);
pr_info("kp1: failing thread stalled at err_auth unregister "
"(rpc_get_sb_net already returned NULL)\n");
spin_until(2);
pr_info("kp1: mount side stalled -> releasing failing thread\n");
return 0;
}
/* mr01 kp2: rpc_create_client_dir entry on the mount task */
static int mr01_kp_ccd_pre(struct kprobe *p, struct pt_regs *regs)
{
if (!atomic_read(&arm) || kp_ccd_hit || !kp_unreg_hit)
return 0;
if (strncmp(current->comm, "mount", 5) != 0)
return 0;
kp_ccd_hit = 1;
atomic_set(&race_state, 2);
pr_info("kp2: mount notifier stalled inside rpc_create_client_dir "
"(holds pipefs_sb_lock, clnt->pipefs_sb already written)\n");
spin_until(3);
pr_info("kp2: failing client freed -> resuming create_client_dir "
"on freed rpc_clnt\n");
return 0;
}
/* mr04 kp: simple_recursive_removal(dentry, callback==rpc_close_pipes) */
static int mr04_kp_srr_pre(struct kprobe *p, struct pt_regs *regs)
{
if (!atomic_read(&arm) || kp_srr_hit)
return 0;
#ifdef CONFIG_X86_64
if (!cb_addr || regs->si != cb_addr)
return 0;
#else
return 0;
#endif
if (strncmp(current->comm, "umount", 6) != 0)
return 0;
kp_srr_hit = 1;
atomic_set(&race_state, 2);
pr_info("kp: umount stalled inside simple_recursive_removal(pipe child)\n");
{
unsigned long until = jiffies + msecs_to_jiffies(stall_ms * 2);
while (!atomic_read(&release_flag) &&
time_before(jiffies, until))
cpu_relax();
}
pr_info("kp: switch-side removal done -> resuming removal on freed "
"pipe dentry\n");
return 0;
}
static struct kprobe kp_unreg = { .symbol_name = "rpc_unregister_client",
.pre_handler = mr01_kp_unreg_pre };
static struct kprobe kp_ccd = { .symbol_name = "rpc_create_client_dir",
.pre_handler = mr01_kp_ccd_pre };
static struct kprobe kp_srr = { .symbol_name = "simple_recursive_removal",
.pre_handler = mr04_kp_srr_pre };
/*
* mr04: the switch thread blocks on pipefs_sb_lock inside the re-register
* (the stalled umount holds the mutex), so the release is driven by a timer
* armed just before rpc_switch_client_transport(): the switch's unlocked
* __rpc_clnt_remove_pipedir() completes in microseconds, well within the
* 2s window; the umount thread then resumes on the freed pipe dentry.
*/
static struct timer_list mr04_rel_timer;
static void mr04_rel_timer_fn(struct timer_list *t)
{
atomic_set(&release_flag, 1);
pr_info("mr04: release timer fired\n");
}
/* ------------------------- client helpers -------------------------- */
static struct rpc_clnt *race_make_client(bool want_ok)
{
struct sockaddr_in sin = {
.sin_family = AF_INET,
.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
.sin_port = htons(2049),
};
struct rpc_create_args args = {
.net = current->nsproxy->net_ns,
.protocol = XPRT_TRANSPORT_TCP,
.address = (struct sockaddr *)&sin,
.addrsize = sizeof(sin),
.servername = "racetest",
.program = &race_program,
.version = 0,
.authflavor = RPC_AUTH_KRB,
.cred = current_cred(),
.flags = RPC_CLNT_CREATE_NOPING,
};
auth_ok = want_ok ? 1 : 0;
return rpc_create(&args);
}
/* ------------------------- mr03 worker ----------------------------- */
static int mr03_threadfn(void *unused)
{
int i;
wait_for_completion(&go_compl);
for (i = 0; i < iters; i++) {
struct rpc_clnt *clnt;
reinit_completion(&helper_compl);
clnt = race_make_client(false);
if (!IS_ERR(clnt)) {
pr_err("unexpected success, aborting\n");
rpc_shutdown_client(clnt);
break;
}
pr_info("iteration %d: rpc_create failed (%ld); client freed "
"while file references were held\n", i, PTR_ERR(clnt));
atomic_set(&race_state, 3);
msleep(300);
}
atomic_set(&race_state, 3);
pr_info("mr03 done\n");
return 0;
}
/* ------------------------- mr01 worker ----------------------------- */
static int mr01_threadfn(void *unused)
{
struct rpc_clnt *clnt;
wait_for_completion(&go_compl);
kp_unreg_hit = 0;
kp_ccd_hit = 0;
/* pipefs must be unmounted at create time; kp1 stalls the err_auth
* unregister; the helper's concurrent mount stalls inside
* rpc_create_client_dir via kp2; release order reproduces the
* audited interleaving. */
atomic_set(&arm, 1);
clnt = race_make_client(false);
atomic_set(&arm, 0);
if (!IS_ERR(clnt)) {
pr_err("mr01: unexpected success\n");
rpc_shutdown_client(clnt);
} else {
pr_info("mr01: rpc_create failed (%ld)\n", PTR_ERR(clnt));
}
atomic_set(&race_state, 3);
return 0;
}
/* ------------------------- mr04 machinery -------------------------- */
struct race_pdo {
struct rpc_pipe_dir_object pdo;
struct rpc_pipe *pipe;
};
static struct rpc_clnt *mr04_clnt;
static struct race_pdo mr04_rp;
static ssize_t race_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
char __user *dst, size_t buflen)
{
return rpc_pipe_generic_upcall(filp, msg, dst, buflen);
}
static const struct rpc_pipe_ops race_pipe_ops = {
.upcall = race_pipe_upcall,
};
static int race_pdo_create(struct dentry *dir, struct rpc_pipe_dir_object *pdo)
{
struct race_pdo *rp = pdo->pdo_data;
return rpc_mkpipe_dentry(dir, "racetest", NULL, rp->pipe);
}
static void race_pdo_destroy(struct dentry *dir, struct rpc_pipe_dir_object *pdo)
{
struct race_pdo *rp = pdo->pdo_data;
rpc_unlink(rp->pipe);
}
static const struct rpc_pipe_dir_object_ops race_pdo_ops = {
.create = race_pdo_create,
.destroy = race_pdo_destroy,
};
static int mr04_setup(void)
{
int ret;
mr04_clnt = race_make_client(true); /* want_ok: fake valid auth */
if (IS_ERR(mr04_clnt))
return PTR_ERR(mr04_clnt);
mr04_rp.pipe = rpc_mkpipe_data(&race_pipe_ops, 0);
ret = PTR_ERR_OR_ZERO(mr04_rp.pipe);
if (ret)
goto out_clnt;
mr04_rp.pdo.pdo_data = &mr04_rp;
rpc_init_pipe_dir_object(&mr04_rp.pdo, &race_pdo_ops, &mr04_rp);
ret = rpc_add_pipe_dir_object(rpc_net_ns(mr04_clnt),
&mr04_clnt->cl_pipedir_objects,
&mr04_rp.pdo);
if (ret)
goto out_pipe;
pr_info("mr04: client clid=%d armed with pipedir + child pipe\n",
mr04_clnt->cl_clid);
return 0;
out_pipe:
rpc_destroy_pipe_data(mr04_rp.pipe);
out_clnt:
rpc_shutdown_client(mr04_clnt);
mr04_clnt = NULL;
return ret;
}
static void mr04_cleanup(void)
{
if (mr04_clnt)
rpc_shutdown_client(mr04_clnt);
mr04_clnt = NULL;
}
static int mr04_threadfn(void *unused)
{
struct sockaddr_in sin = {
.sin_family = AF_INET,
.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
.sin_port = htons(2049),
};
struct xprt_create xargs = {
.ident = XPRT_TRANSPORT_TCP,
.net = rpc_net_ns(mr04_clnt),
.dstaddr = (struct sockaddr *)&sin,
.addrlen = sizeof(sin),
.servername = "racetest",
};
int ret, i;
/* wait for the umount thread to stall inside simple_recursive_removal */
{
unsigned long until = jiffies + msecs_to_jiffies(stall_ms * 4);
while (atomic_read(&race_state) < 2 &&
time_before(jiffies, until))
msleep(1);
}
if (atomic_read(&race_state) < 2) {
pr_err("mr04: umount side never stalled, aborting\n");
return 0;
}
pr_info("mr04: driving rpc_switch_client_transport() against the "
"stalled umount\n");
timer_setup(&mr04_rel_timer, mr04_rel_timer_fn, 0);
/* 300ms: the switch-side unlocked removal completes in microseconds,
* while the freed dentries are still inside the KASAN quarantine when
* the umount thread resumes (clean slab-use-after-free instead of a
* post-reuse crash). */
mod_timer(&mr04_rel_timer, jiffies + msecs_to_jiffies(300));
for (i = 0; i < 3; i++) {
ret = rpc_switch_client_transport(mr04_clnt, &xargs, NULL);
pr_info("mr04: switch attempt %d -> %d\n", i, ret);
if (ret == 0)
break;
msleep(50);
}
timer_delete_sync(&mr04_rel_timer);
atomic_set(&release_flag, 1);
atomic_set(&race_state, 3);
return 0;
}
/* ------------------------- mr02 machinery -------------------------- */
static struct cache_detail *race_cd;
static struct task_struct *feeder;
static bool feeder_run = true;
static void race_cache_put(struct kref *ref) { }
static void race_cache_request(struct cache_detail *cd, struct cache_head *h,
char **bpp, int *blen)
{
/* deterministic, comfortably-sized upcall text */
qword_add(bpp, blen, (char *)"racetest");
qword_add(bpp, blen, (char *)"filler-line-for-concurrent-formatting-race");
qword_add(bpp, blen, (char *)"0123456789012345678901234567890123456789");
qword_add(bpp, blen, (char *)"0123456789012345678901234567890123456789");
if (*blen > 0)
(*bpp)[-1] = '\n';
}
static struct cache_head *race_alloc(void)
{
return kzalloc_obj(struct cache_head, GFP_KERNEL);
}
static int race_match(struct cache_head *a, struct cache_head *b) { return 1; }
static void race_init_h(struct cache_head *a, struct cache_head *b) { }
static void race_update(struct cache_head *a, struct cache_head *b) { }
static struct cache_detail race_cd_template = {
.owner = THIS_MODULE,
.hash_size = 16,
.name = "racetest",
.cache_put = race_cache_put,
.cache_request = race_cache_request,
.alloc = race_alloc,
.match = race_match,
.init = race_init_h,
.update = race_update,
};
static int mr02_setup(void)
{
int ret;
race_cd = cache_create_net(&race_cd_template, current->nsproxy->net_ns);
if (IS_ERR(race_cd))
return PTR_ERR(race_cd);
ret = cache_register_net(race_cd, current->nsproxy->net_ns);
if (ret)
goto out_free;
pr_info("mr02: channel at /proc/net/rpc/racetest/channel\n");
return 0;
out_free:
cache_destroy_net(race_cd, current->nsproxy->net_ns);
race_cd = NULL;
return ret;
}
static void mr02_cleanup(void)
{
if (!race_cd)
return;
feeder_run = false;
if (feeder && !IS_ERR(feeder)) {
kthread_stop(feeder);
feeder = NULL;
}
cache_unregister_net(race_cd, current->nsproxy->net_ns);
cache_destroy_net(race_cd, current->nsproxy->net_ns);
race_cd = NULL;
}
static int mr02_threadfn(void *unused)
{
unsigned long until = jiffies + secs * HZ;
while (feeder_run && time_before(jiffies, until)) {
struct cache_head *h = race_alloc();
if (h) {
memset(h, 0, sizeof(*h));
INIT_HLIST_NODE(&h->cache_list);
kref_init(&h->ref);
h->expiry_time = seconds_since_boot() + 3600;
h->last_refresh = seconds_since_boot();
sunrpc_cache_upcall(race_cd, h);
/* h intentionally left pending: the rq stays on
* cd->requests for the readers to race on; the
* per-item leak is bounded by the run duration. */
}
usleep_range(1000, 2000);
}
pr_info("mr02: feeder done\n");
return 0;
}
/* ------------------------- module glue ----------------------------- */
static int helper_ready_get(char *buf, const struct kernel_param *kp)
{
return sprintf(buf, "%d\n", atomic_read(&race_state) >= 1 ? 1 : 0);
}
static int helper_ready_set(const char *val, const struct kernel_param *kp)
{
int v;
if (kstrtoint(val, 0, &v))
return -EINVAL;
if (v) {
pr_info("helper_ready -> window closing\n");
complete(&helper_compl);
}
return 0;
}
static const struct kernel_param_ops helper_ready_ops = {
.flags = KERNEL_PARAM_OPS_FL_NOARG,
.get = helper_ready_get,
.set = helper_ready_set,
};
module_param_cb(helper_ready, &helper_ready_ops, NULL, 0600);
static int event_get(char *buf, const struct kernel_param *kp)
{
return sprintf(buf, "%d\n", atomic_read(&race_state));
}
static const struct kernel_param_ops event_ops = {
.flags = KERNEL_PARAM_OPS_FL_NOARG,
.get = event_get,
};
module_param_cb(event, &event_ops, NULL, 0444);
static int __init rpcrace_init(void)
{
int ret;
do_hold = !strcmp(mode, "mr03pfs") || !strcmp(mode, "mr03dbg");
pr_info("mode=%s hold_ms=%d iters=%d stall_ms=%d secs=%d cb_addr=0x%llx\n",
mode, hold_ms, iters, stall_ms, secs, (unsigned long long)cb_addr);
if (!strcmp(mode, "mr03pfs") || !strcmp(mode, "mr03dbg")) {
ret = rpcauth_register(&race_authops);
if (ret)
return ret;
worker = kthread_run(mr03_threadfn, NULL, "rpcraceA");
ret = PTR_ERR_OR_ZERO(worker);
if (!ret)
complete(&go_compl);
return ret;
}
if (!strcmp(mode, "mr01")) {
ret = register_kprobe(&kp_unreg);
if (ret)
return ret;
ret = register_kprobe(&kp_ccd);
if (ret) {
unregister_kprobe(&kp_unreg);
return ret;
}
worker = kthread_run(mr01_threadfn, NULL, "rpcraceA");
ret = PTR_ERR_OR_ZERO(worker);
if (ret) {
unregister_kprobe(&kp_ccd);
unregister_kprobe(&kp_unreg);
return ret;
}
/* thread blocks on go_compl; release after worker assigned */
complete(&go_compl);
return 0;
}
if (!strcmp(mode, "mr04")) {
if (!cb_addr) {
pr_err("mr04 requires cb_addr=<rpc_close_pipes>\n");
return -EINVAL;
}
ret = rpcauth_register(&race_authops);
if (ret)
return ret;
ret = mr04_setup();
if (ret) {
rpcauth_unregister(&race_authops);
return ret;
}
ret = register_kprobe(&kp_srr);
if (ret) {
mr04_cleanup();
return ret;
}
atomic_set(&arm, 1);
worker = kthread_run(mr04_threadfn, NULL, "rpcraceA");
if (IS_ERR(worker)) {
unregister_kprobe(&kp_srr);
atomic_set(&arm, 0);
mr04_cleanup();
return PTR_ERR(worker);
}
return 0;
}
if (!strcmp(mode, "mr02")) {
ret = mr02_setup();
if (ret)
return ret;
feeder = kthread_run(mr02_threadfn, NULL, "rpcraceF");
if (IS_ERR(feeder)) {
mr02_cleanup();
return PTR_ERR(feeder);
}
return 0;
}
return -EINVAL;
}
static void __exit rpcrace_exit(void)
{
if (!strcmp(mode, "mr03pfs") || !strcmp(mode, "mr03dbg")) {
if (worker && !IS_ERR(worker))
kthread_stop(worker);
rpcauth_unregister(&race_authops);
} else if (!strcmp(mode, "mr01")) {
atomic_set(&arm, 0);
if (worker && !IS_ERR(worker))
kthread_stop(worker);
unregister_kprobe(&kp_ccd);
unregister_kprobe(&kp_unreg);
} else if (!strcmp(mode, "mr04")) {
atomic_set(&arm, 0);
atomic_set(&release_flag, 1);
if (worker && !IS_ERR(worker))
kthread_stop(worker);
unregister_kprobe(&kp_srr);
mr04_cleanup();
rpcauth_unregister(&race_authops);
} else if (!strcmp(mode, "mr02")) {
mr02_cleanup();
}
pr_info("exit\n");
}
module_init(rpcrace_init);
module_exit(rpcrace_exit);
MODULE_LICENSE("GPL");