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 server-side callers in the lockd kthread: - nlmsvc_grant_blocked() calls nlm_rebind_host() without h_mutex, passing a freed or cleared h_rpcclnt to rpc_force_rebind(). - __nlm_async_call() drops h_mutex inside nlm_bind_host() before reading clnt->cl_procinfo and calling rpc_run_task(), so a concurrent shutdown can free clnt under it. Also, after the walk, a server host in that net can still bind a new h_rpcclnt that no later step shuts down. Add a per-net nlmsvc_shutdown flag to struct lockd_net. nlm_shutdown_hosts_net() sets it under nlm_host_mutex before the walk; lockd_up_net() clears it before make_socks() and sets it again if make_socks() fails. While it is set, nlmsvc_lookup_host() returns NULL and the new nlm_bind_host_locked() refuses to bind a server host. The walk now detaches h_rpcclnt under h_mutex and calls rpc_shutdown_client() after dropping it. For server hosts, __nlm_async_call() holds h_mutex across nlm_bind_host_locked() and rpc_run_task(), so either the bind fails or the task is attached to the client, holding its own rpc_clnt reference, before the walk detaches it and calls rpc_shutdown_client(). A plain refcount_inc() pins the host across that section, because rpc_run_task() may release the request and its host reference before h_mutex is dropped; nlm_get_host() would reset h_expires and let nlm_gc_hosts() skip the host. nlmsvc_grant_blocked() now calls nlm_rebind_host() under h_mutex. Found by LLM-assisted static analysis; the original rebind/shutdown race was reproduced in QEMU+KASAN (slab-use-after-free in rpc_force_rebind()). v3 was compile-tested on x86_64 only. Fixes: 3b64739fb928 ("Lockd: shutdown NLM hosts in network namespace context") Cc: stable@vger.kernel.org Signed-off-by: Ziheng Wei --- v2 -> v3: - Replace the per-host h_shutdown flag and nlm_reopen_hosts_net() with a per-net nlmsvc_shutdown flag in struct lockd_net; the make_socks() error path in lockd_up_net() now only sets the flag again instead of calling nlm_shutdown_hosts_net() (Chuck Lever). - Have nlmsvc_lookup_host() return NULL while the flag is set, so it can no longer find or create a server host in that net after the walk (window reported by Chuck Lever). - Document nlmsvc_shutdown and update the h_mutex comment (Chuck Lever). - Pin the host in __nlm_async_call() with refcount_inc() instead of nlm_get_host(), which reset h_expires and could make the shutdown GC skip the host (Sashiko AI review). - v2: https://lore.kernel.org/r/20261130134320.1412254-1-zihengw03@163.com v1 -> v2: - Do not skip shutdown entries in nlmsvc_lookup_host(), add host->h_mutex serialization across async rpc_run_task(), and add Fixes/Cc: stable. diff --git a/fs/lockd/netns.h b/fs/lockd/netns.h index 88e8e2a97397..4dd1dfc73626 100644 --- a/fs/lockd/netns.h +++ b/fs/lockd/netns.h @@ -8,6 +8,7 @@ struct lockd_net { unsigned int nlmsvc_users; + bool nlmsvc_shutdown; /* refuse server host lookup and bind */ unsigned long next_gc; unsigned long nrhosts; u32 gracetime; diff --git a/fs/lockd/lockd.h b/fs/lockd/lockd.h index 14cc952fe81a..f6e1c664a545 100644 --- a/fs/lockd/lockd.h +++ b/fs/lockd/lockd.h @@ -99,7 +99,7 @@ struct nlm_host { u32 h_nsmstate; /* true remote NSM state */ u32 h_pidcount; /* Pseudopids */ refcount_t h_count; /* reference count */ - struct mutex h_mutex; /* mutex for pmap binding */ + struct mutex h_mutex; /* RPC binding, server h_rpcclnt */ unsigned long h_nextrebind; /* next portmap call */ unsigned long h_expires; /* eligible for GC */ struct list_head h_lockowners; /* Lockowners for the client */ @@ -287,6 +287,7 @@ struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp, const char *hostname, const size_t hostname_len); void nlmsvc_release_host(struct nlm_host *); +struct rpc_clnt *nlm_bind_host_locked(struct nlm_host *host); struct rpc_clnt * nlm_bind_host(struct nlm_host *); void nlm_rebind_host(struct nlm_host *); struct nlm_host * nlm_get_host(struct nlm_host *); diff --git a/fs/lockd/host.c b/fs/lockd/host.c index d572cb27533f..911b3fb57b8b 100644 --- a/fs/lockd/host.c +++ b/fs/lockd/host.c @@ -381,6 +381,9 @@ struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp, mutex_lock(&nlm_host_mutex); + if (READ_ONCE(ln->nlmsvc_shutdown)) + goto out; + if (time_after_eq(jiffies, ln->next_gc)) nlm_gc_hosts(net); @@ -453,15 +456,21 @@ void nlmsvc_release_host(struct nlm_host *host) * Create the NLM RPC client for an NLM peer */ struct rpc_clnt * -nlm_bind_host(struct nlm_host *host) +nlm_bind_host_locked(struct nlm_host *host) { struct rpc_clnt *clnt; + lockdep_assert_held(&host->h_mutex); + dprintk("lockd: nlm_bind_host %s (%s)\n", host->h_name, host->h_addrbuf); - /* Lock host handle */ - mutex_lock(&host->h_mutex); + if (host->h_server) { + struct lockd_net *ln = net_generic(host->net, lockd_net_id); + + if (READ_ONCE(ln->nlmsvc_shutdown)) + return NULL; + } /* If we've already created an RPC client, check whether * RPC rebind is required @@ -513,6 +522,16 @@ nlm_bind_host(struct nlm_host *host) } } + return clnt; +} + +struct rpc_clnt * +nlm_bind_host(struct nlm_host *host) +{ + struct rpc_clnt *clnt; + + mutex_lock(&host->h_mutex); + clnt = nlm_bind_host_locked(host); mutex_unlock(&host->h_mutex); return clnt; } @@ -646,17 +665,27 @@ nlm_shutdown_hosts_net(struct net *net) mutex_lock(&nlm_host_mutex); + if (net) { + struct lockd_net *ln = net_generic(net, lockd_net_id); + + WRITE_ONCE(ln->nlmsvc_shutdown, true); + } + /* First, make all hosts eligible for gc */ 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); + 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/svc.c b/fs/lockd/svc.c index f0e1a58c9106..616ef9d253ec 100644 --- a/fs/lockd/svc.c +++ b/fs/lockd/svc.c @@ -231,13 +231,17 @@ static int lockd_up_net(struct svc_serv *serv, struct net *net, if (error) goto err_bind; + WRITE_ONCE(ln->nlmsvc_shutdown, false); + error = make_socks(serv, net, cred); if (error < 0) - goto err_bind; + goto err_socks; set_grace_period(net); dprintk("%s: per-net data created; net=%x\n", __func__, net->ns.inum); return 0; +err_socks: + WRITE_ONCE(ln->nlmsvc_shutdown, true); err_bind: ln->nlmsvc_users--; return error; diff --git a/fs/lockd/clntproc.c b/fs/lockd/clntproc.c index f8018bfe9c64..6485b547dfee 100644 --- a/fs/lockd/clntproc.c +++ b/fs/lockd/clntproc.c @@ -354,6 +354,31 @@ static struct rpc_task *__nlm_async_call(struct nlm_rqst *req, u32 proc, struct dprintk("lockd: call procedure %d on %s (async)\n", (int)proc, host->h_name); + if (host->h_server) { + struct rpc_task *task; + + /* Serialize with the walk in nlm_shutdown_hosts_net() */ + mutex_lock(&host->h_mutex); + clnt = nlm_bind_host_locked(host); + if (!clnt) { + mutex_unlock(&host->h_mutex); + goto out_err; + } + msg->rpc_proc = &clnt->cl_procinfo[proc]; + task_setup_data.rpc_client = clnt; + + /* + * rpc_run_task() may release @req, and its host reference, + * before h_mutex is dropped. Pin the host without touching + * h_expires, which nlm_get_host() would push into the future. + */ + refcount_inc(&host->h_count); + task = rpc_run_task(&task_setup_data); + mutex_unlock(&host->h_mutex); + nlmsvc_release_host(host); + return task; + } + /* If we have no RPC client yet, create one. */ clnt = nlm_bind_host(host); if (clnt == NULL) diff --git a/fs/lockd/svclock.c b/fs/lockd/svclock.c index 495eacb3264f..1daa82179143 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; } base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa