Forwarded: [PATCH] nfsd: fix memory leak in nfsd_create_serv error paths

syzbot
2025-12-16T08:02:23Z

For archival purposes, forwarding an incoming command email to
linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com.

***

Subject: [PATCH] nfsd: fix memory leak in nfsd_create_serv error paths
Author: kartikey406@gmail.com

#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master

When svc_create_pooled() or svc_bind() fails, nfsd_create_serv() returns
an error without calling percpu_ref_exit() to clean up the percpu
reference that was initialized earlier in the function.

This causes two memory leaks per failed call:
- 8 bytes of percpu memory (the reference counter)
- 64 bytes of slab memory (the percpu_ref_data structure)

Fix this by adding an error label that calls percpu_ref_exit() before
returning.

Reported-by: syzbot+6ee3b889bdeada0a6226@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=6ee3b889bdeada0a6226
Signed-off-by: Deepanshu Kartikey <kartikey406@gmail.com>
---
fs/nfsd/nfssvc.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/fs/nfsd/nfssvc.c b/fs/nfsd/nfssvc.c
index b08ae85d53ef..49fd77c8f98d 100644
--- a/fs/nfsd/nfssvc.c
+++ b/fs/nfsd/nfssvc.c
@@ -615,13 +615,14 @@ int nfsd_create_serv(struct net *net)
serv = svc_create_pooled(nfsd_programs, ARRAY_SIZE(nfsd_programs),
&nn->nfsd_svcstats,
nfsd_max_blksize, nfsd);
- if (serv == NULL)
- return -ENOMEM;
-
+ if (serv == NULL) {
+ error = -ENOMEM;
+ goto err_percpu;
+ }
error = svc_bind(serv, net);
if (error < 0) {
svc_destroy(&serv);
- return error;
+ goto err_percpu;
}
spin_lock(&nfsd_notifier_lock);
nn->nfsd_serv = serv;
@@ -636,6 +637,9 @@ int nfsd_create_serv(struct net *net)
}
nfsd_reset_write_verifier(nn);
return 0;
+err_percpu:
+ percpu_ref_exit(&nn->nfsd_net_ref);
+ return error;
}

int nfsd_nrpools(struct net *net)
--
2.43.0