Fix two latent server-side initialization bugs
Subsystems (3)
Chuck Lever
maintainerv2 cover Fix two latent server-side initialization bugs
Both patches fix the same bug shape in the SUNRPC server: an object becomes reachable by a consumer while a field it depends on is still invalid. The use-gss-proxy proc entry is published before its mutex is initialized; a half-allocated svc_serv whose per-pool percpu counters never got backing storage is returned to its caller. In each case the common path hides the flaw, and only a narrow trigger exposes it -- a preemption-timed write to /proc/net/rpc/use-gss-proxy, or a percpu allocation failure under memory pressure during RPC server startup.
Each fix rests on an argument that no consumer observes the object mid-initialization, and the two arguments differ. For the proc entry, the guarantee comes from module load order: sunrpc.ko is a build-time dependency of auth_rpcgss.ko, so sunrpc_init_net() has initialized gssp_lock on every net namespace before auth_gss pernet init can publish the file. Moving the init there ties the lock's lifetime to the sunrpc_net it lives in and lets the lazy init_gssp_clnt() helper go away. For the svc_serv, the guarantee is local: the percpu allocation failure is caught and unwound inside __svc_create(), so a half-constructed service never reaches nfsd, lockd, or the NFS callback service. Both arguments are worth checking against the diffs.
The two fixes are independent, touch different files, and may be applied in any order.
---
Changes in v2:
- Reverted the percpu_counter_init_many() consolidation back to
discrete percpu_counter_init()/percpu_counter_destroy() calls.
- Link to v1: https://patch.msgid.link/[email protected]
---
Chris Mason (1):
sunrpc: init gssp_lock before publishing proc entry
Chuck Lever (1):
SUNRPC: Check svc pool percpu counter allocation
net/sunrpc/auth_gss/gss_rpc_upcall.c | 6 ----- net/sunrpc/auth_gss/gss_rpc_upcall.h | 1 - net/sunrpc/auth_gss/svcauth_gss.c | 1 - net/sunrpc/sunrpc_syms.c | 1 + net/sunrpc/svc.c | 45 +++++++++++++++++++++++++++++++----- 5 files changed, 40 insertions(+), 14 deletions(-)
---
base-commit: 4d4d6605de5f91a40335729b6a7cc15e83b280f3
change-id: 20260530-tier2-local-58cbe6d49337
Best regards,
Signature · 1 line
Chuck Lever <[email protected]>
Chuck Lever
maintainerPATCH v2 1/2 sunrpc: init gssp_lock before publishing proc entry
From: Chris Mason <[email protected]>
create_use_gss_proxy_proc_entry() publishes /proc/net/rpc/use-gss-proxy via proc_create_data() before init_gssp_clnt() runs mutex_init() on sn->gssp_lock. Once the dentry is linked under proc_subdir_lock it is immediately reachable from userspace, so a write that lands in the window drives set_gssp_clnt() into mutex_lock() on a zero-initialized struct mutex.
create_use_gss_proxy_proc_entry(net)
proc_create_data("use-gss-proxy", ...) /* dentry live */
init_gssp_clnt(sn)
mutex_init(&sn->gssp_lock) /* too late */
write_gssp()
set_gssp_clnt(net)
mutex_lock(&sn->gssp_lock) /* uninitialized */
gssp_rpc_create(...)
sn->gssp_clnt = clnt
mutex_unlock(&sn->gssp_lock)The window spans only the two statements between proc_create_data() returning and init_gssp_clnt(), so a writer reaches it only if the registering thread is preempted there while another task is already opening the freshly published file. register_pernet_subsys() runs in preemptible context under pernet_ops_rwsem, so that preemption is possible, and the window widens on auth_rpcgss module load, when the proc entry is created for every live net namespace whose tasks are already running. A writer that wins the race locks a zero-filled struct mutex. On CONFIG_DEBUG_MUTEXES the missing magic value trips a "lock used without init" splat; on a production kernel the fast path acquires the lock via CMPXCHG(owner, 0, current). In the latter case a second writer that arrives before init_gssp_clnt() re-zeroes owner can enter set_gssp_clnt() concurrently, shut down the first writer's clnt while it is still in use, and leak the loser's clnt.
Fix by initializing sn->gssp_lock in sunrpc_init_net() so its lifetime matches the sunrpc_net it lives in. sn->gssp_clnt is already NULL from the kzalloc that backs net_generic storage, so the lazy helper is no longer needed; drop init_gssp_clnt(), its prototype, and the call from create_use_gss_proxy_proc_entry(). sunrpc.ko is a build-time dependency of auth_rpcgss.ko, so sunrpc_init_net() has always run on every netns before any auth_gss pernet init can publish the proc entry.
--- net/sunrpc/auth_gss/gss_rpc_upcall.c | 6 ------ net/sunrpc/auth_gss/gss_rpc_upcall.h | 1 - net/sunrpc/auth_gss/svcauth_gss.c | 1 - net/sunrpc/sunrpc_syms.c | 1 + 4 files changed, 1 insertion(+), 8 deletions(-)
net/sunrpc/auth_gss/gss_rpc_upcall.c +0 -6
@@ -121,12 +121,6 @@ static int gssp_rpc_create(struct net *net, struct rpc_clnt **_clnt)return result; } -void init_gssp_clnt (struct sunrpc_net *sn) -{ -mutex_init (&sn->gssp_lock); - sn->gssp_clnt =NULL ; -} -int set_gssp_clnt (struct net *net) {struct sunrpc_net *sn =net_generic (net, sunrpc_net_id);
net/sunrpc/auth_gss/gss_rpc_upcall.h +0 -1
@@ -29,7 +29,6 @@ int gssp_accept_sec_context_upcall(struct net *net, struct gssp_upcall_data *data);void gssp_free_upcall_data (struct gssp_upcall_data *data); -void init_gssp_clnt (struct sunrpc_net *);int set_gssp_clnt (struct net *);void clear_gssp_clnt (struct sunrpc_net *);
net/sunrpc/auth_gss/svcauth_gss.c +0 -1
@@ -1468,7 +1468,6 @@ static int create_use_gss_proxy_proc_entry(struct net *net) &use_gss_proxy_proc_ops, net);if (!*p)return -ENOMEM ; -init_gssp_clnt (sn);return 0 ; }
net/sunrpc/sunrpc_syms.c +1 -0
@@ -57,6 +57,7 @@ static __net_init int sunrpc_init_net(struct net *net)INIT_LIST_HEAD (&sn->all_clients);spin_lock_init (&sn->rpc_client_lock);spin_lock_init (&sn->rpcb_clnt_lock); +mutex_init (&sn->gssp_lock);return 0 ; err_pipefs:
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v2 2/2 SUNRPC: Check svc pool percpu counter allocation
In reply to Chuck LeverFrom: Chuck Lever <[email protected]>
__svc_create() initializes three per-pool percpu_counter stats and ignores every return value. On SMP, percpu_counter_init() fails when __alloc_percpu_gfp() cannot satisfy the allocation, leaving the failed counter with fbc->counters == NULL and its embedded raw_spinlock_t, list_head, and count never initialized. __svc_create() returns the half-constructed svc_serv to nfsd, lockd, or the NFS callback service anyway.
Once that service is live, the hot-path increments in svc_xprt_enqueue(), svc_handle_xprt(), and svc_pool_wake_idle_thread() reach a counter whose backing pointer is NULL. The pointer is a per-cpu offset, so the access does not fault:
it resolves to offset zero of the current CPU's per-cpu area and silently corrupts whatever variable lives there. A /proc/fs/nfsd/pool_stats read walks the same NULL per-cpu storage and returns garbage, and on CONFIG_DEBUG_SPINLOCK or lockdep it splats on the never-initialized lock.
Creating the broken service requires a percpu allocation failure during RPC server startup, so it is reachable only by a local administrator under memory pressure or fault injection; a remote peer cannot induce the bad state on its own.
Check each percpu_counter_init() return value in __svc_create() and fail when an allocation fails, unwinding the counters already set up in the current pool and in every pool initialized before it. A discrete percpu_counter_destroy() per counter at teardown frees each per-cpu allocation exactly once.
--- net/sunrpc/svc.c | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-)
net/sunrpc/svc.c +39 -6
@@ -476,6 +476,35 @@ __svc_init_bc(struct svc_serv *serv) }#endif +static int svc_pool_init_counters (struct svc_pool *pool) +{ +int err; + + err =percpu_counter_init (&pool->sp_messages_arrived,0 ,GFP_KERNEL ); +if (err) +return err; + err =percpu_counter_init (&pool->sp_sockets_queued,0 ,GFP_KERNEL ); +if (err) + goto err_sockets; + err =percpu_counter_init (&pool->sp_threads_woken,0 ,GFP_KERNEL ); +if (err) + goto err_threads; +return 0 ; + +err_threads: +percpu_counter_destroy (&pool->sp_sockets_queued); +err_sockets: +percpu_counter_destroy (&pool->sp_messages_arrived); +return err; +} + +static void svc_pool_destroy_counters (struct svc_pool *pool) +{ +percpu_counter_destroy (&pool->sp_messages_arrived); +percpu_counter_destroy (&pool->sp_sockets_queued); +percpu_counter_destroy (&pool->sp_threads_woken); +} +/* * Create an RPC service */
@@ -540,12 +569,18 @@ __svc_create(struct svc_program *prog, int nprogs, struct svc_stat *stats,INIT_LIST_HEAD (&pool->sp_all_threads);init_llist_head (&pool->sp_idle_threads); -percpu_counter_init (&pool->sp_messages_arrived,0 ,GFP_KERNEL ); -percpu_counter_init (&pool->sp_sockets_queued,0 ,GFP_KERNEL ); -percpu_counter_init (&pool->sp_threads_woken,0 ,GFP_KERNEL ); +if (svc_pool_init_counters (pool)) + goto out_err; }return serv; + +out_err: +while (i--) +svc_pool_destroy_counters (&serv->sv_pools[i]); +kfree (serv->sv_pools); +kfree (serv); +return NULL ; }/**
@@ -624,9 +659,7 @@ svc_destroy(struct svc_serv **servp)for (i =0 ; i < serv->sv_nrpools; i++) {struct svc_pool *pool = &serv->sv_pools[i]; -percpu_counter_destroy (&pool->sp_messages_arrived); -percpu_counter_destroy (&pool->sp_sockets_queued); -percpu_counter_destroy (&pool->sp_threads_woken); +svc_pool_destroy_counters (pool); }kfree (serv->sv_pools);kfree (serv);
Signature · 1 line
2.54.0
Re: [PATCH v2 2/2] SUNRPC: Check svc pool percpu counter allocation
Quoted 100 lines · Chuck Lever
From: Chuck Lever <[email protected]>
__svc_create()initializes three per-poolpercpu_counterstats and ignores every return value. On SMP,percpu_counter_init()fails when__alloc_percpu_gfp()cannot satisfy the allocation, leaving the failed counter withfbc->counters== NULL and its embeddedraw_spinlock_t,list_head, and count never initialized.__svc_create()returns the half-constructedsvc_servto nfsd, lockd, or the NFS callback service anyway.Once that service is live, the hot-path increments in
svc_xprt_enqueue(),svc_handle_xprt(), andsvc_pool_wake_idle_thread()reach a counter whose backing pointer is NULL. The pointer is a per-cpu offset, so the access does not fault:
it resolves to offset zero of the current CPU's per-cpu area and silently corrupts whatever variable lives there. A/proc/fs/nfsd/pool_statsread walks the same NULL per-cpu storage and returns garbage, and onCONFIG_DEBUG_SPINLOCKor lockdep it splats on the never-initialized lock.Creating the broken service requires a percpu allocation failure during RPC server startup, so it is reachable only by a local administrator under memory pressure or fault injection; a remote peer cannot induce the bad state on its own.
Check each
percpu_counter_init()return value in__svc_create()and fail when an allocation fails, unwinding the counters already set up in the current pool and in every pool initialized before it. A discretepercpu_counter_destroy()per counter at teardown frees each per-cpu allocation exactly once.Fixes ccf08bed6e7a ("SUNRPC: Replace pool stats with per-CPU variables")Signed-off-by Chuck Lever <[email protected]>--- net/sunrpc/svc.c | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-)net/sunrpc/svc.c +38
-3@@ -476,6 +476,35 @@ __svc_init_bc(struct svc_serv *serv) }#endif +static int svc_pool_init_counters (struct svc_pool *pool) +{ +int err; + + err =percpu_counter_init (&pool->sp_messages_arrived,0 ,GFP_KERNEL ); +if (err) +return err; + err =percpu_counter_init (&pool->sp_sockets_queued,0 ,GFP_KERNEL ); +if (err) + goto err_sockets; + err =percpu_counter_init (&pool->sp_threads_woken,0 ,GFP_KERNEL ); +if (err) + goto err_threads; +return 0 ; + +err_threads: +percpu_counter_destroy (&pool->sp_sockets_queued); +err_sockets: +percpu_counter_destroy (&pool->sp_messages_arrived); +return err; +} + +static void svc_pool_destroy_counters (struct svc_pool *pool) +{ +percpu_counter_destroy (&pool->sp_messages_arrived); +percpu_counter_destroy (&pool->sp_sockets_queued); +percpu_counter_destroy (&pool->sp_threads_woken); +} +/* * Create an RPC service */ @@ -540,12 +569,18 @@ __svc_create(struct svc_program *prog, int nprogs, struct svc_stat *stats,INIT_LIST_HEAD (&pool->sp_all_threads);init_llist_head (&pool->sp_idle_threads); -percpu_counter_init (&pool->sp_messages_arrived,0 ,GFP_KERNEL ); -percpu_counter_init (&pool->sp_sockets_queued,0 ,GFP_KERNEL ); -percpu_counter_init (&pool->sp_threads_woken,0 ,GFP_KERNEL ); +if (svc_pool_init_counters (pool)) + goto out_err; }return serv; + +out_err: +while (i--) +svc_pool_destroy_counters (&serv->sv_pools[i]); +kfree (serv->sv_pools); +kfree (serv); +return NULL ; }/**
net/sunrpc/svc.c svc_destroy(struct svc_serv **servp) · line 665
Quoted 10 lines
net/sunrpc/svc.c +1
-3@@ -624,9 +659,7 @@ svc_destroy(struct svc_serv **servp)for (i =0 ; i < serv->sv_nrpools; i++) {struct svc_pool *pool = &serv->sv_pools[i]; -percpu_counter_destroy (&pool->sp_messages_arrived); -percpu_counter_destroy (&pool->sp_sockets_queued); -percpu_counter_destroy (&pool->sp_threads_woken); +svc_pool_destroy_counters (pool); }kfree (serv->sv_pools);kfree (serv);