[PATCH] lockd: serialize host->h_rpcclnt across rebind, bind, and shutdown

From: Ziheng Wei

Date: Mon Nov 30 2026 - 05:35:58 EST


lockd_down() -> lockd_down_net() calls nlm_shutdown_hosts_net(net) while
the lockd kthread is still running. nlm_shutdown_hosts_net() shuts down
and clears host->h_rpcclnt without holding host->h_mutex, racing with
nlmsvc_grant_blocked():
- nlm_rebind_host() reads host->h_rpcclnt without h_mutex and passes a
freed or cleared pointer to rpc_force_rebind().
- nlm_async_call() -> nlm_bind_host() drops h_mutex before dereferencing
clnt->cl_procinfo and calling rpc_run_task(), allowing a concurrent
shutdown to free clnt or nlm_bind_host() to recreate a client on a
shutting-down host.

Export rpc_hold_client(), mark hosts h_shutdown and detach h_rpcclnt
under h_mutex in nlm_shutdown_hosts_net() before rpc_shutdown_client(),
return a held rpc_clnt reference from nlm_bind_host() (failing if
h_shutdown), serialize nlm_rebind_host() in nlmsvc_grant_blocked() with
h_mutex, and release the reference in nlm_bind_host() callers.

Found by LLM-assisted static analysis; the original rebind/shutdown race
was reproduced in QEMU+KASAN (slab-use-after-free in rpc_force_rebind())
and the fix was compile-tested on x86_64.

Signed-off-by: Ziheng Wei <zihengw03@xxxxxxx>
---
fs/lockd/clntlock.c | 9 +++++++--
fs/lockd/clntproc.c | 11 ++++++++---
fs/lockd/host.c | 24 +++++++++++++++++++-----
fs/lockd/lockd.h | 1 +
fs/lockd/svclock.c | 2 ++
net/sunrpc/clnt.c | 1 +
6 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/fs/lockd/clntlock.c b/fs/lockd/clntlock.c
index f797cc99f..fa0070772 100644
--- a/fs/lockd/clntlock.c
+++ b/fs/lockd/clntlock.c
@@ -58,8 +58,13 @@ struct nlm_host *nlmclnt_init(const struct nlmclnt_initdata *nlm_init)
nlm_init->net, nlm_init->cred);
if (host == NULL)
goto out_nohost;
- if (host->h_rpcclnt == NULL && nlm_bind_host(host) == NULL)
- goto out_nobind;
+ if (!host->h_rpcclnt) {
+ struct rpc_clnt *clnt = nlm_bind_host(host);
+
+ if (!clnt)
+ goto out_nobind;
+ rpc_release_client(clnt);
+ }

host->h_nlmclnt_ops = nlm_init->nlmclnt_ops;
return host;
diff --git a/fs/lockd/clntproc.c b/fs/lockd/clntproc.c
index f8018bfe9..dcc89f8ee 100644
--- a/fs/lockd/clntproc.c
+++ b/fs/lockd/clntproc.c
@@ -288,7 +288,9 @@ nlmclnt_call(const struct cred *cred, struct nlm_rqst *req, u32 proc)
msg.rpc_proc = &clnt->cl_procinfo[proc];

/* Perform the RPC call. If an error occurs, try again */
- if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
+ status = rpc_call_sync(clnt, &msg, 0);
+ rpc_release_client(clnt);
+ if (status < 0) {
dprintk("lockd: rpc_call returned error %d\n", -status);
switch (status) {
case -EPROTONOSUPPORT:
@@ -344,6 +346,7 @@ static struct rpc_task *__nlm_async_call(struct nlm_rqst *req, u32 proc, struct
{
struct nlm_host *host = req->a_host;
struct rpc_clnt *clnt;
+ struct rpc_task *task;
struct rpc_task_setup task_setup_data = {
.rpc_message = msg,
.callback_ops = tk_ops,
@@ -361,8 +364,10 @@ static struct rpc_task *__nlm_async_call(struct nlm_rqst *req, u32 proc, struct
msg->rpc_proc = &clnt->cl_procinfo[proc];
task_setup_data.rpc_client = clnt;

- /* bootstrap and kick off the async RPC call */
- return rpc_run_task(&task_setup_data);
+ /* bootstrap and kick off the async RPC call */
+ task = rpc_run_task(&task_setup_data);
+ rpc_release_client(clnt);
+ return task;
out_err:
tk_ops->rpc_release(req);
return ERR_PTR(-ENOLCK);
diff --git a/fs/lockd/host.c b/fs/lockd/host.c
index d572cb275..aebeabb04 100644
--- a/fs/lockd/host.c
+++ b/fs/lockd/host.c
@@ -146,6 +146,7 @@ static struct nlm_host *nlm_alloc_host(struct nlm_lookup_host_info *ni,
host->h_server = ni->server;
host->h_noresvport = ni->noresvport;
host->h_inuse = 0;
+ host->h_shutdown = false;
init_waitqueue_head(&host->h_gracewait);
init_rwsem(&host->h_rwsem);
host->h_state = 0;
@@ -386,7 +387,7 @@ struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,

chain = &nlm_server_hosts[nlm_hash_address(ni.sap)];
hlist_for_each_entry(host, chain, h_hash) {
- if (host->net != net)
+ if (host->net != net || host->h_shutdown)
continue;
if (!rpc_cmp_addr(nlm_addr(host), ni.sap))
continue;
@@ -463,6 +464,11 @@ nlm_bind_host(struct nlm_host *host)
/* Lock host handle */
mutex_lock(&host->h_mutex);

+ if (host->h_shutdown) {
+ clnt = NULL;
+ goto out_unlock;
+ }
+
/* If we've already created an RPC client, check whether
* RPC rebind is required
*/
@@ -513,6 +519,9 @@ nlm_bind_host(struct nlm_host *host)
}
}

+ if (clnt)
+ rpc_hold_client(clnt);
+out_unlock:
mutex_unlock(&host->h_mutex);
return clnt;
}
@@ -650,13 +659,18 @@ nlm_shutdown_hosts_net(struct net *net)
dprintk("lockd: nuking all hosts in net %x...\n",
net ? net->ns.inum : 0);
for_each_host(host, chain, nlm_server_hosts) {
+ struct rpc_clnt *clnt;
+
if (net && host->net != net)
continue;
host->h_expires = jiffies - 1;
- if (host->h_rpcclnt) {
- rpc_shutdown_client(host->h_rpcclnt);
- host->h_rpcclnt = NULL;
- }
+ mutex_lock(&host->h_mutex);
+ host->h_shutdown = true;
+ clnt = host->h_rpcclnt;
+ host->h_rpcclnt = NULL;
+ mutex_unlock(&host->h_mutex);
+ if (clnt)
+ rpc_shutdown_client(clnt);
nlmsvc_free_host_resources(host);
}

diff --git a/fs/lockd/lockd.h b/fs/lockd/lockd.h
index 14cc952fe..ca2e03570 100644
--- a/fs/lockd/lockd.h
+++ b/fs/lockd/lockd.h
@@ -93,6 +93,7 @@ struct nlm_host {
h_server : 1, /* server side, not client side */
h_noresvport : 1,
h_inuse : 1;
+ bool h_shutdown;
wait_queue_head_t h_gracewait; /* wait while reclaiming */
struct rw_semaphore h_rwsem; /* Reboot recovery lock */
u32 h_state; /* pseudo-state counter */
diff --git a/fs/lockd/svclock.c b/fs/lockd/svclock.c
index 495eacb32..1daa82179 100644
--- a/fs/lockd/svclock.c
+++ b/fs/lockd/svclock.c
@@ -835,7 +835,9 @@ nlmsvc_grant_blocked(struct nlm_block *block)
* Just retry the grant callback, possibly refreshing the RPC
* binding */
if (block->b_granted) {
+ mutex_lock(&block->b_host->h_mutex);
nlm_rebind_host(block->b_host);
+ mutex_unlock(&block->b_host->h_mutex);
goto callback;
}

diff --git a/net/sunrpc/clnt.c b/net/sunrpc/clnt.c
index 6cedc824c..5e9f2d484 100644
--- a/net/sunrpc/clnt.c
+++ b/net/sunrpc/clnt.c
@@ -1048,6 +1048,7 @@ void rpc_hold_client(struct rpc_clnt *clnt)
{
refcount_inc(&clnt->cl_count);
}
+EXPORT_SYMBOL_GPL(rpc_hold_client);

/**
* rpc_release_client - release a reference on an rpc_clnt

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa
--
2.43.0