Fix NFSD post-shutdown use-after-free in the unlock_filesystem paths
Subsystems (2)
Chuck Lever
maintainercover Fix NFSD post-shutdown use-after-free in the unlock_filesystem paths
Musaab Khan reported that a local administrator holding CAP_SYS_ADMIN can trigger a use-after-free in nfsd by writing a filesystem path to /proc/fs/nfsd/unlock_filesystem after the server has been stopped.
write_unlock_fs() calls nfsd4_cancel_copy_by_sb() before it takes nfsd_mutex and before it confirms that nn->nfsd_serv is set. Once nfsd has shut down, nfs4_state_destroy_net() has freed nn->conf_id_hashtbl but left the pointer intact, so the cancel helper walks freed slab memory as an array of struct list_head and then dereferences a bogus nfs4_client when it takes clp->async_lock. KASAN reports a slab-use-after-free read in nfsd4_cancel_copy_by_sb().
nfsd4_revoke_states() walks the same state tables and for that reason already runs only under nfsd_mutex with nn->nfsd_serv confirmed present. The async COPY cancel was added ahead of that protected section, so it escaped the guard. The first two patches move the cancel inside the protected section on both the procfs unlock_filesystem path and the NFSD_CMD_UNLOCK_FILESYSTEM netlink path. Async copies exist only while the server runs, so gating the cancel on nn->nfsd_serv loses nothing.
The netlink command that patch 2 corrects, 327c5168eff2 ("NFSD: Add NFSD_CMD_UNLOCK_FILESYSTEM netlink command"), has not yet reached mainline; it sits in nfsd-next, destined for v7.2. Patch 2 therefore applies to nfsd-next, not to a released kernel, and needs no stable backport.
The last patch adds lockdep_assert_held(&nfsd_mutex) to the state-table walkers and documents the nfsd_mutex / nn->nfsd_serv precondition in a Context: kdoc section, so a future caller added to this path cannot silently reintroduce the same use-after-free.
---
Chuck Lever (3):
NFSD: Prevent post-shutdown use-after-free in unlock_filesystem
NFSD: Prevent post-shutdown use-after-free in NFSD_CMD_UNLOCK_FILESYSTEM
NFSD: Annotate caller preconditions for the state-table walkers
fs/nfsd/nfs4proc.c | 6 ++++++ fs/nfsd/nfs4state.c | 16 +++++++++++++++- fs/nfsd/nfsctl.c | 14 ++++++++------ 3 files changed, 29 insertions(+), 7 deletions(-)
---
base-commit: 56d6b74e37708005739ef00bb59db34bffac15a8
change-id: 20260613-unlock-filesystem-uaf-99a7577caea2
Best regards,
Signature · 1 line
Chuck Lever
Chuck Lever
maintainerPATCH 1/3 NFSD: Prevent post-shutdown use-after-free in unlock_filesystem
Writing a filesystem path to /proc/fs/nfsd/unlock_filesystem runs nfsd4_cancel_copy_by_sb() before nfsd_mutex is held and before the handler confirms that nn->nfsd_serv is set. Once nfsd has shut down, nfs4_state_destroy_net() has freed nn->conf_id_hashtbl but left the pointer intact, so the cancel helper iterates freed slab memory as an array of struct list_head and then dereferences a bogus nfs4_client when it takes clp->async_lock. A local administrator holding CAP_SYS_ADMIN can reach this use-after-free by stopping the server and then writing to unlock_filesystem; KASAN reports a slab-use-after-free read in nfsd4_cancel_copy_by_sb().
nfsd4_revoke_states() walks the same state tables and for that reason already runs only under nfsd_mutex with nn->nfsd_serv confirmed present. Move the async COPY cancel into that protected section so every NFSv4 state-table walker on this path observes a running server. Async copies exist only while the server runs, so gating the cancel on nn->nfsd_serv loses nothing.
--- fs/nfsd/nfsctl.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
fs/nfsd/nfsctl.c +4 -3
@@ -296,14 +296,15 @@ static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)* 2. Is that directory a mount point, or * 3. Is that directory the root of an exported file system? */ -nfsd4_cancel_copy_by_sb (netns (file), path.dentry->d_sb); error =nlmsvc_unlock_all_by_sb (path.dentry->d_sb);mutex_lock (&nfsd_mutex); nn =net_generic (netns (file), nfsd_net_id); -if (nn->nfsd_serv) +if (nn->nfsd_serv) { +nfsd4_cancel_copy_by_sb (netns (file), path.dentry->d_sb);nfsd4_revoke_states (nn, path.dentry->d_sb); -else + }else { error = -EINVAL ; + }mutex_unlock (&nfsd_mutex);path_put (&path);
Signature · 1 line
2.54.0
Re: [PATCH 1/3] NFSD: Prevent post-shutdown use-after-free in unlock_filesystem
Hi Chuck,
I came across a Sashiko AI code review [1] that flagged a remaining issue in write_unlock_fs() — the nn->nfsd_serv check is not sufficient to guarantee that nfsd_startup_net() has run and allocated the state tables.
I was able to trigger it in QEMU with KASAN: creating an NFSD listener via portlist sets nn->nfsd_serv, but without starting any threads, nfsd_startup_net() never runs. Writing to unlock_filesystem then calls nfsd4_cancel_copy_by_sb() which dereferences the uninitialized nfs4_client hashtable → NULL pointer dereference.
Quoted 4 lines · Chuck Lever
Writing a filesystem path to
/proc/fs/nfsd/unlock_filesystemrunsnfsd4_cancel_copy_by_sb()beforenfsd_mutexis held. This commit moves the async COPY cancel into thenfsd_mutexprotected section after checkingnn->nfsd_servto prevent a use-after-free.
This correctly protects against the post-shutdown UAF, but nn->nfsd_serv can be non-NULL while nfsd_startup_net() has not yet been called. The sequence is:
echo 2049 > /proc/fs/nfsd/portlist # sets nn->nfsd_serv
# threads are still 0 — nfsd_startup_net() never ran echo /mnt > /proc/fs/nfsd/unlock_filesystem # passes nn->nfsd_serv check
nfsd4_cancel_copy_by_sb() then iterates nn->nfs4_client_hashtbl, which was allocated by nfsd_startup_net() and is still NULL.
[Reproduction]
Create a listener without starting threads, then write to unlock_filesystem:
echo 2049 > /proc/fs/nfsd/portlist
mount -t nfsd nfsd /proc/fs/nfsd
echo /mnt > /proc/fs/nfsd/unlock_filesystem
[KASAN report — kernel 7.1.0-rc7-next-20260612, CONFIG_KASAN=y]
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] SMP KASAN NOPTI KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] RIP: 0010:nfsd4_cancel_copy_by_sb+0x184/0xa70 Call Trace: <TASK> write_unlock_fs+0x3ab/0x4f0 nfsctl_transaction_write+0xfd/0x180 vfs_write+0x2a5/0x11b0 ksys_write+0x12f/0x250 do_syscall_64+0x129/0x880 entry_SYSCALL_64_after_hwframe+0x77/0x7f
The crash is at offset 0x184 inside nfsd4_cancel_copy_by_sb(), dereferencing a NULL hashtable pointer (RAX: 0x0000000000000000).
[1] https://sashiko.dev/#/patchset/20260613-unlock-filesystem-uaf-v1-0-462b9bec8c84%40kernel.org
(Sashiko AI code review — "Null Pointer Dereference", Severity: High)
Thanks,
XIAO
Chuck Lever
maintainerRe: [PATCH 1/3] NFSD: Prevent post-shutdown use-after-free in unlock_filesystem
Quoted 5 lines · XIAO WU
I was able to trigger it in QEMU with KASAN: creating an NFSD listener via portlist sets
nn->nfsd_serv, but without starting any threads,nfsd_startup_net()never runs. Writing tounlock_filesystemthen callsnfsd4_cancel_copy_by_sb()which dereferences the uninitializednfs4_clienthashtable -> NULL pointer dereference.
Thanks for the careful analysis and the reproducer. I agree the crash is real. nfsd_create_serv() sets nn->nfsd_serv from the portlist and netlink listener paths, none of which run nfsd_startup_net(), so nn->nfsd_serv can indeed be non-NULL while the NFSv4 state tables are still NULL.
One correction: the array the walk dereferences is nn->conf_id_hashtbl, not nn->nfs4_client_hashtbl. Both are allocated together in nfs4_state_create_net(), reached only via nfsd_startup_net(), so your mechanism holds either way; the name in the report is just off.
However, this crasher is a different bug from the one this series addresses, and AFAICT it is not introduced by this patch. Before this change the cancel ran unconditionally, so the same NULL conf_id_hashtbl walk was reachable in the startup window (and more besides). The nn->nfsd_serv guard added here narrows the post-shutdown case but does not cover the startup window, because nn->nfsd_serv is set before the tables exist. The two also differ at KASAN: this patch fixes a slab-use-after-free on a freed-but-not-cleared conf_id_hashtbl; what you hit is a null-ptr-deref on a never-allocated one.
The correct guard for the startup window is NFSD_NET_UP, which is set at the tail of nfsd_startup_net() exactly when the state tables become valid, rather than nn->nfsd_serv. nfsd4_revoke_states() walks the same tables and nfsd_nl_unlock_filesystem_doit() repeats the pattern, so a fix needs to cover both unlock paths. I'll address that in a separate follow-up and add your Reported-by.
Signature · 1 line
Chuck Lever
Chuck Lever
maintainerPATCH 2/3 NFSD: Prevent post-shutdown use-after-free in NFSD_CMD_UNLOCK_FILESYSTEM
In reply to Chuck LeverThe NFSD_CMD_UNLOCK_FILESYSTEM netlink command runs nfsd4_cancel_copy_by_sb() before nfsd_mutex is held and before nn->nfsd_serv is confirmed set, the same pre-mutex ordering the procfs unlock_filesystem path carried. Once nfsd has shut down, nfs4_state_destroy_net() has freed nn->conf_id_hashtbl but left the pointer intact, so the cancel helper iterates freed slab memory as an array of struct list_head and then dereferences a bogus nfs4_client when it takes clp->async_lock. A local administrator holding CAP_SYS_ADMIN can reach this use-after-free by stopping the server and then issuing the command.
Move the async COPY cancel into the nfsd_mutex section, after nn->nfsd_serv is confirmed, so every NFSv4 state-table walker on this path observes a running server. Async copies exist only while the server runs, so gating the cancel on nn->nfsd_serv loses nothing.
--- fs/nfsd/nfsctl.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
fs/nfsd/nfsctl.c +4 -3
@@ -2368,14 +2368,15 @@ int nfsd_nl_unlock_filesystem_doit(struct sk_buff *skb,if (error)return error; -nfsd4_cancel_copy_by_sb (net, path.dentry->d_sb); error =nlmsvc_unlock_all_by_sb (path.dentry->d_sb);mutex_lock (&nfsd_mutex); -if (nn->nfsd_serv) +if (nn->nfsd_serv) { +nfsd4_cancel_copy_by_sb (net, path.dentry->d_sb);nfsd4_revoke_states (nn, path.dentry->d_sb); -else + }else { error = -EINVAL ; + }mutex_unlock (&nfsd_mutex);path_put (&path);
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH 3/3 NFSD: Annotate caller preconditions for the state-table walkers
In reply to Chuck LeverThe state-table walkers now assert nfsd_mutex with lockdep_assert_held() and document the nfsd_mutex / nn->nfsd_serv precondition in a Context: kdoc section, so the next caller added to this path cannot silently reintroduce the same use-after-free.
--- fs/nfsd/nfs4proc.c | 6 ++++++ fs/nfsd/nfs4state.c | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-)
fs/nfsd/nfs4proc.c +6 -0
@@ -1587,6 +1587,11 @@ static bool nfsd4_copy_on_sb(const struct nfsd4_copy *copy,* nfsd4_cancel_copy_by_sb - cancel async copy operations on @sb * @net: net namespace containing the copy operations * @sb: targeted superblock +* +* Context: Caller must hold nfsd_mutex with nn->nfsd_serv confirmed +* non-NULL. nfs4_state_destroy_net() frees conf_id_hashtbl +* at server shutdown without clearing the pointer, so a +* walk without these guarantees iterates freed slab memory. */ void nfsd4_cancel_copy_by_sb (struct net *net,struct super_block *sb) {
@@ -1596,6 +1601,7 @@ void nfsd4_cancel_copy_by_sb(struct net *net, struct super_block *sb)unsigned int idhashval;LIST_HEAD (to_cancel); +lockdep_assert_held (&nfsd_mutex);spin_lock (&nn->client_lock);for (idhashval =0 ; idhashval <CLIENT_HASH_SIZE ; idhashval++) {struct list_head *head = &nn->conf_id_hashtbl[idhashval];
fs/nfsd/nfs4state.c +15 -1
@@ -1873,14 +1873,21 @@ static void revoke_one_stid(struct nfsd_net *nn, struct nfs4_client *clp,* being released. Thus nfsd will no longer prevent the filesystem from being * unmounted. * -* The clients which own the states will subsequently being notified that the +* The clients which own the states will subsequently be notified that the * states have been "admin-revoked". +* +* Context: Caller must hold nfsd_mutex with nn->nfsd_serv confirmed +* non-NULL. nfs4_state_destroy_net() frees conf_id_hashtbl +* at server shutdown without clearing the pointer, so a +* walk without these guarantees iterates freed slab memory. */ void nfsd4_revoke_states (struct nfsd_net *nn,struct super_block *sb) {unsigned int idhashval;unsigned int sc_types; +lockdep_assert_held (&nfsd_mutex); + sc_types =SC_TYPE_OPEN |SC_TYPE_LOCK |SC_TYPE_DELEG |SC_TYPE_LAYOUT ;spin_lock (&nn->client_lock);
@@ -1946,12 +1953,19 @@ static struct nfs4_stid *find_one_export_stid(struct nfs4_client *clp,* * Userspace (exportfs -u) sends this after removing the last client * for a path, enabling the underlying filesystem to be unmounted. +* +* Context: Caller must hold nfsd_mutex with nn->nfsd_serv confirmed +* non-NULL. nfs4_state_destroy_net() frees conf_id_hashtbl +* at server shutdown without clearing the pointer, so a +* walk without these guarantees iterates freed slab memory. */ void nfsd4_revoke_export_states (struct nfsd_net *nn,const struct path *path) {unsigned int idhashval;unsigned int sc_types; +lockdep_assert_held (&nfsd_mutex); + sc_types =SC_TYPE_OPEN |SC_TYPE_LOCK |SC_TYPE_DELEG |SC_TYPE_LAYOUT ;spin_lock (&nn->client_lock);
Signature · 1 line
2.54.0
Quoted 42 lines · Chuck Lever
Musaab Khan reported that a local administrator holding
CAP_SYS_ADMINcan trigger a use-after-free in nfsd by writing a filesystem path to/proc/fs/nfsd/unlock_filesystemafter the server has been stopped.
write_unlock_fs()callsnfsd4_cancel_copy_by_sb()before it takesnfsd_mutexand before it confirms thatnn->nfsd_servis set. Once nfsd has shut down,nfs4_state_destroy_net()has freednn->conf_id_hashtblbut left the pointer intact, so the cancel helper walks freed slab memory as an array of structlist_headand then dereferences a bogusnfs4_clientwhen it takesclp->async_lock. KASAN reports a slab-use-after-free read innfsd4_cancel_copy_by_sb().
nfsd4_revoke_states()walks the same state tables and for that reason already runs only undernfsd_mutexwithnn->nfsd_servconfirmed present. The async COPY cancel was added ahead of that protected section, so it escaped the guard. The first two patches move the cancel inside the protected section on both the procfsunlock_filesystempath and theNFSD_CMD_UNLOCK_FILESYSTEMnetlink path. Async copies exist only while the server runs, so gating the cancel onnn->nfsd_servloses nothing.The netlink command that patch 2 corrects,
327c5168eff2("NFSD: AddNFSD_CMD_UNLOCK_FILESYSTEMnetlink command"), has not yet reached mainline; it sits in nfsd-next, destined for v7.2. Patch 2 therefore applies to nfsd-next, not to a released kernel, and needs no stable backport.The last patch adds
lockdep_assert_held(&nfsd_mutex)to the state-table walkers and documents thenfsd_mutex/nn->nfsd_servprecondition in a Context: kdoc section, so a future caller added to this path cannot silently reintroduce the same use-after-free.---
Chuck Lever (3):
NFSD: Prevent post-shutdown use-after-free inunlock_filesystem
NFSD: Prevent post-shutdown use-after-free inNFSD_CMD_UNLOCK_FILESYSTEM
NFSD: Annotate caller preconditions for the state-table walkersfs/nfsd/nfs4proc.c | 6 ++++++ fs/nfsd/nfs4state.c | 16 +++++++++++++++- fs/nfsd/nfsctl.c | 14 ++++++++------ 3 files changed, 29 insertions(+), 7 deletions(-)
Quoted 7 lines
---
base-commit:56d6b74e37708005739ef00bb59db34bffac15a8
change-id: 20260613-unlock-filesystem-uaf-99a7577caea2Best regards,
Signature · 1 line
Chuck Lever
This all looks good.