SUNRPC: serialize pipedir removal on the rpc_client_register() error path
Subsystems (5)
PATCH v2 1/1 SUNRPC: serialize pipedir removal on the rpc_client_register() error path
When rpcauth_create() fails, rpc_client_register() only holds sn->pipefs_sb_lock for the pipedir removal if rpc_get_sb_net() finds a pipefs superblock. If it does not, the client is taken off sn->all_clients and its directory is removed with no lock held, and a pipefs mount in that window gives the client a new directory that is then removed while a concurrent umount drops the same dentries.
syzbot hit this racing rpc_pipefs mount/umount with NFSv4 mounts:
WARNING: fs/dcache.c:1063 at d_make_discardable+0x28e/0x2f0 __simple_recursive_removal+0x276/0x5c0 fs/libfs.c:625 rpc_remove_client_dir+0xf0/0x140 net/sunrpc/rpc_pipe.c:898 rpc_client_register+0x605/0x6f0 net/sunrpc/clnt.c:333 rpc_new_client+0x977/0x1300 net/sunrpc/clnt.c:437
Take pipefs_sb_lock unconditionally around rpc_unregister_client() and __rpc_clnt_remove_pipedir(), so no mount or umount notification can run between the two.
---
Tested on Linus' master 4f069ab1ec24 (arm64, QEMU, generic KASAN, PROVE_LOCKING, NFSv4 without RPCSEC_GSS_KRB5) with the syzbot C reproducer. The window is narrow, so a test-only msleep(20) was added on the err_auth path to widen it (after rpc_get_sb_net() returns NULL in the unpatched code; before the lock and between the two calls with this patch):
unpatched + delay: 2 of 2 boots hit d_make_discardable / fast_dput
WARNINGs (9 and 12)
patched + delay: 3 of 3 boots (9 runs) clean
patched, no delay: 2 of 2 boots (6 runs) clean
No lockdep report. net/sunrpc/clnt.o builds cleanly with W=1.
---
Changes in v2:
- Hold pipefs_sb_lock across rpc_unregister_client() as well, instead of unregistering first and locking afterwards. v1 left a window where a umount no longer found the client and its directory was never removed (Trond Myklebust).
- Link to v1: https://lore.kernel.org/r/[email protected]
--- net/sunrpc/clnt.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
net/sunrpc/clnt.c +10 -1
@@ -303,6 +303,7 @@ static int rpc_client_register(struct rpc_clnt *clnt, };struct rpc_auth *auth;struct net *net =rpc_net_ns (clnt); +struct sunrpc_net *sn =net_generic (net, sunrpc_net_id);struct super_block *pipefs_sb;int err;
@@ -328,12 +329,20 @@ static int rpc_client_register(struct rpc_clnt *clnt, }return 0 ; err_auth: - pipefs_sb =rpc_get_sb_net (net); +/* +* Hold pipefs_sb_lock across both steps, whether or not pipefs is +* mounted, so that no mount or umount notification can run between +* taking the client off the list and removing its directory. +*/ +mutex_lock (&sn->pipefs_sb_lock);rpc_unregister_client (clnt);__rpc_clnt_remove_pipedir (clnt); +mutex_unlock (&sn->pipefs_sb_lock); + goto out_destroy; out:if (pipefs_sb)rpc_put_sb_net (net); +out_destroy:rpc_sysfs_client_destroy (clnt);rpc_clnt_debugfs_unregister (clnt);return err;
---
base-commit: 4f069ab1ec243e6b60dc438772905855c60774e8
change-id: 20261129-sunrpc-err-auth-v2-6fda2dd1427b
Best regards,
Signature · 1 line
Joas Antonio dos Santos <[email protected]>