Harden server-side RPCSEC GSS decode and backchannel teardown
Subsystems (4)
Chuck Lever
maintainercover Harden server-side RPCSEC GSS decode and backchannel teardown
Address findings from an audit of how the server-side RPCSEC GSS accept path handles partially decoded and stale credential state. The common defect class: gss_svc_data and the rpc_gss_wire_cred it embeds (svcdata->clcred) are allocated with non-zeroing kmalloc and reused across requests, so an early decode failure leaves this request's partial writes mixed with the previous request's residue for a later consumer to trust.
The two gssx option-array decoder patches carry a hard ordering dependency. The error-path fix must come first: it wires free_svc_cred() into free_creds: and converts the out_free_groups:
teardown to the refcount-aware put_group_info(). Only once that refcount-aware free path exists does rejecting a duplicate CREDS_VALUE option actually release the single installed group_info instead of leaking it. Reviewed out of order, the duplicate-rejection patch looks incomplete.
The backchannel patch stands apart from the GSS decode work: a genuine race and use-after-free in callback-service teardown. It closes the producer side -- clearing xprt->bc_serv under bc_pa_lock -- before the callback threads stop, then drains any request that raced in before svc_destroy() frees the service.
---
Chris Mason (4):
SUNRPC: fix gssx_dec_option_array error path bugs
SUNRPC: reject duplicate CREDS_VALUE options
SUNRPC: Guard svcauth_gss_release() dispatch on rq_auth_stat
SUNRPC: Zero rpc_gss_wire_cred at svcauth_gss_decode_credbody() entry
Chuck Lever (2):
SUNRPC: Reject krb5 v2 wrap tokens with oversized ec field
SUNRPC: close backchannel before destroying callback service
fs/nfs/callback.c | 4 +++- include/linux/sunrpc/bc_xprt.h | 5 +++++ net/sunrpc/auth_gss/gss_krb5_wrap.c | 2 ++ net/sunrpc/auth_gss/gss_rpc_xdr.c | 15 ++++++++++++--- net/sunrpc/auth_gss/svcauth_gss.c | 5 +++++ net/sunrpc/backchannel_rqst.c | 38 ++++++++++++++++++++++++++++++------- 6 files changed, 58 insertions(+), 11 deletions(-)
---
base-commit: 4d4d6605de5f91a40335729b6a7cc15e83b280f3
change-id: 20260528-tier2-d0dedb949b3d
Best regards,
Signature · 1 line
Chuck Lever <[email protected]>
Chuck Lever
maintainerPATCH 1/6 SUNRPC: Reject krb5 v2 wrap tokens with oversized ec field
From: Chuck Lever <[email protected]>
gss_krb5_unwrap_v2() sets buf->len to a logical length, which can be much smaller than head[0].iov_len (the allocated receive-page capacity). It then calls xdr_buf_trim() with a trim length derived from the 16-bit "extra count" (ec) field in the Kerberos v2 token header.
The ec field is authenticated by the post-decrypt memcmp() against the encrypted header copy, so a randomly-mutated value is rejected. However, any peer holding a valid GSS context can legitimately encrypt a token whose ec exceeds the plaintext length. Per RFC 4121, such a token is structurally malformed.
Although xdr_buf_trim() now clamps the buf->len subtraction to avoid unsigned underflow, the buffer is still left in a semantically invalid state (zero length, inconsistent iov lengths) when ec is oversized.
Reject these tokens before calling xdr_buf_trim(), giving callers a well-defined GSS_S_DEFECTIVE_TOKEN error and keeping the xdr_buf internally consistent. The wrapped blob begins at a nonzero offset -- both callers pass len as offset + opaque_len -- so buf->len still counts the offset bytes that precede the blob. Compare the trim length against the remaining wrapped segment, buf->len - offset, rather than the whole buffer; comparing against buf->len alone leaves an offset-wide window in which an oversized ec passes the test and xdr_buf_trim() cuts into the bytes ahead of the blob.
--- net/sunrpc/auth_gss/gss_krb5_wrap.c | 2 ++ 1 file changed, 2 insertions(+)
net/sunrpc/auth_gss/gss_krb5_wrap.c +2 -0
@@ -235,6 +235,8 @@ gss_krb5_unwrap_v2(struct krb5_ctx *kctx, int offset, int len, buf->len = len - (GSS_KRB5_TOK_HDR_LEN + headskip);/* Trim off the trailing "extra count" and checksum blob */ +if (ec +GSS_KRB5_TOK_HDR_LEN + tailskip > buf->len - offset) +return GSS_S_DEFECTIVE_TOKEN ;xdr_buf_trim (buf, ec +GSS_KRB5_TOK_HDR_LEN + tailskip); *align =XDR_QUADLEN (GSS_KRB5_TOK_HDR_LEN + headskip);
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH 2/6 SUNRPC: fix gssx_dec_option_array error path bugs
In reply to Chuck LeverFrom: Chris Mason <[email protected]>
Four coupled defects in the gssx XDR option-array decoder make the error paths unsafe: a NULL deref in the caller, a refcount leak on the decoded group_info, and a latent use-after-free that the leak fix would otherwise expose.
gssx_dec_option_array() sets oa->count = 1 before allocating oa->data. If that allocation fails, -ENOMEM is returned with oa->count == 1 and oa->data == NULL. All other error paths jump to free_oa: which frees oa->data and NULLs it but also leaves oa->count == 1. The caller trusts the count:
gssp_accept_sec_context_upcall()
gssx_dec_accept_sec_context()
gssx_dec_option_array() /* fails, count=1 data=NULL */
data = res.options.data[0].value /* NULL deref */
Independently, free_creds: releases the partially decoded svc_cred with a bare kfree(creds). gssx_dec_linux_creds() installs a groups_alloc() result into creds->cr_group_info; that object is kvmalloc-backed and refcounted, and only put_group_info() reaches kvfree(). A plain kfree(creds) drops the wrapper and leaks the group_info allocation.
The natural fix for the leak is to call free_svc_cred(creds) before kfree(creds), but free_svc_cred() invokes put_group_info() on creds->cr_group_info unconditionally when non-NULL. The existingout_free_groups: path in gssx_dec_linux_creds() already called groups_free() on that pointer without clearing it, so once free_svc_cred() is wired in, the subsequent put_group_info() would touch freed memory.
Fix all four together:
- Move the oa->count = 1 assignment below the oa->data allocation
so it is never set when oa->data is NULL.
- Reset oa->count to 0 at free_oa: so count and data stay coherent and the caller sees an empty option array.
- Call free_svc_cred(creds) before kfree(creds) at free_creds:
so the refcounted cr_group_info is released. free_svc_cred() either NULL-guards each field explicitly (cr_group_info has an if() check) or delegates to a helper that is NULL-safe itself (kfree for the string fields, gss_mech_put() which guards with if(gm) at gss_mech_switch.c:342), so it is safe to call on a partially decoded svc_cred where only cr_uid/cr_gid/cr_group_info have been written and everything else is zero from kzalloc.
- In gssx_dec_linux_creds()'s out_free_groups: path, release cr_group_info with put_group_info() rather than groups_free() so the teardown matches free_svc_cred()'s refcount-aware path, and clear the pointer so a later free_svc_cred() on the same creds does not release it a second time.
--- net/sunrpc/auth_gss/gss_rpc_xdr.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
net/sunrpc/auth_gss/gss_rpc_xdr.c +6 -3
@@ -222,7 +222,8 @@ static int gssx_dec_linux_creds(struct xdr_stream *xdr,return 0 ; out_free_groups: -groups_free (creds->cr_group_info); +put_group_info (creds->cr_group_info); + creds->cr_group_info =NULL ;return err; }
@@ -242,12 +243,12 @@ static int gssx_dec_option_array(struct xdr_stream *xdr,return 0 ;/* we recognize only 1 currently: CREDS_VALUE */ - oa->count =1 ; - oa->data = kmalloc_obj(struct gssx_option);if (!oa->data)return -ENOMEM ; + oa->count =1 ; + creds = kzalloc_obj(struct svc_cred);if (!creds) { err = -ENOMEM ;
@@ -294,8 +295,10 @@ static int gssx_dec_option_array(struct xdr_stream *xdr,return 0 ; free_creds: +free_svc_cred (creds);kfree (creds); free_oa: + oa->count =0 ;kfree (oa->data); oa->data =NULL ;return err;
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH 3/6 SUNRPC: reject duplicate CREDS_VALUE options
In reply to Chuck LeverFrom: Chris Mason <[email protected]>
gssx_dec_option_array() walks the wire-supplied option array and, for every entry whose name matches CREDS_VALUE, calls gssx_dec_linux_creds() on the same struct svc_cred. That helper unconditionally installs a fresh groups_alloc() result into creds->cr_group_info without releasing whatever pointer was already there:
for (i = 0; i < count; i++) {
... decode name ...
if (length == sizeof(CREDS_VALUE) &&
memcmp(p, CREDS_VALUE, sizeof(CREDS_VALUE)) == 0) {
err = gssx_dec_linux_creds(xdr, creds);
...
}
}A reply that carries two CREDS_VALUE entries therefore overwrites cr_group_info on the second iteration and orphans the group_info allocated by the first call. The earlier free_creds path only releases the last cr_group_info via free_svc_cred(), so the first allocation's refcount stays at one and its kvmalloc-backed storage is leaked. No in-tree caller of gssp_accept_sec_context_upcall() expects more than one CREDS_VALUE per reply.
Fix by tracking whether a CREDS_VALUE option has already been decoded and returning -EINVAL on any subsequent match, so the free_creds path releases the single group_info that was installed.
--- net/sunrpc/auth_gss/gss_rpc_xdr.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/sunrpc/auth_gss/gss_rpc_xdr.c +6 -0
@@ -231,6 +231,7 @@ static int gssx_dec_option_array(struct xdr_stream *xdr, struct gssx_option_array *oa) {struct svc_cred *creds; +bool creds_decoded = false;u32 count, i;__be32 *p;int err;
@@ -281,9 +282,14 @@ static int gssx_dec_option_array(struct xdr_stream *xdr,if (length ==sizeof (CREDS_VALUE ) &&memcmp (p,CREDS_VALUE ,sizeof (CREDS_VALUE )) ==0 ) {/* We have creds here. parse them */ +if (creds_decoded) { + err = -EINVAL ; + goto free_creds; + } err =gssx_dec_linux_creds (xdr, creds);if (err) goto free_creds; + creds_decoded = true; oa->data[0 ].value.len =1 ;/* presence */ }else {/* consume uninteresting buffer */
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH 4/6 SUNRPC: Guard svcauth_gss_release() dispatch on rq_auth_stat
In reply to Chuck LeverFrom: Chris Mason <[email protected]>
svcauth_gss_release() reads gc_proc and switches on gc_svc before consulting rq_auth_stat. On the SVC_DENIED path after a failed svcauth_gss_accept(), those fields may hold stale values from a prior request or uninitialized slab residue: svcauth_gss_accept() allocates gss_svc_data with non-zeroing kmalloc and clears only gsd_databody_offset and rsci per request, not clcred.
Because RPC_GSS_PROC_DATA is zero, a zeroed or stale-zero gc_proc passes the existing guard and falls through into the gc_svc switch, which can dispatch to svcauth_gss_wrap_integ() or svcauth_gss_wrap_priv(). Both wrap helpers call svcauth_gss_prepare_to_wrap() before any rsci->mechctx dereference, and that helper already returns early when rq_auth_stat is not rpc_auth_ok, so the downstream NULL dereference is blocked. The dispatch itself remains structurally wrong: it reads scalars that the caller has no contract to have initialized after a failed authentication.
Mirror the existing rq_auth_stat gate in svcauth_gss_prepare_to_wrap() one frame up, so svcauth_gss_release() skips the clcred dispatch entirely when authentication has not succeeded. The cleanup tail that releases rq_client, rq_gssclient, cr_group_info, and rsci still runs.
--- net/sunrpc/auth_gss/svcauth_gss.c | 2 ++ 1 file changed, 2 insertions(+)
net/sunrpc/auth_gss/svcauth_gss.c +2 -0
@@ -1944,6 +1944,8 @@ svcauth_gss_release(struct svc_rqst *rqstp)if (!gsd) goto out; +if (rqstp->rq_auth_stat != rpc_auth_ok) + goto out; gc = &gsd->clcred;if (gc->gc_proc !=RPC_GSS_PROC_DATA ) goto out;
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH 5/6 SUNRPC: Zero rpc_gss_wire_cred at svcauth_gss_decode_credbody() entry
In reply to Chuck LeverFrom: Chris Mason <[email protected]>
svcauth_gss_decode_credbody() writes the caller's rpc_gss_wire_cred field by field and assigns gc_ctx.len only on the success tail. The caller storage is svcdata->clcred, which lives in the per-svc_rqst gss_svc_data and is reused across requests. Early decode failures leave partially decoded state mixed with residue from the prior request.
The trailing body_len tightness check is the sharpest case:xdr_stream_decode_opaque_inline() has already written gc_ctx.data with a borrowed inline pointer into the current request's XDR pages, but gc_ctx.len retains its prior value. Once the request pages are released the pooled clcred carries a dangling pointer paired with a stale length.
Zero the caller's rpc_gss_wire_cred at function entry so that every early-return path leaves a deterministic all-zero cred. On the trailing tightness-check path, gc_ctx.len is now zero instead of stale, which neuters length-driven consumers such as gss_svc_searchbyctx() that would otherwise walk the dangling data pointer.
--- net/sunrpc/auth_gss/svcauth_gss.c | 3 +++ 1 file changed, 3 insertions(+)
net/sunrpc/auth_gss/svcauth_gss.c +3 -0
@@ -1575,6 +1575,9 @@ svcauth_gss_decode_credbody(struct xdr_stream *xdr,u32 body_len;__be32 *p; +/* Early-return paths leave deterministic state, not stale residue. */ +memset (gc,0 ,sizeof (*gc)); + p =xdr_inline_decode (xdr,XDR_UNIT );if (!p)return false;
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH 6/6 SUNRPC: close backchannel before destroying callback service
In reply to Chuck LeverFrom: Chuck Lever <[email protected]>
A backchannel receive can complete a request while the NFS callback service is being torn down. xprt_complete_bc_request() removes the request from bc_pa_list, drops bc_alloc_count, marks the request in use, and then asks xprt_enqueue_bc_request() to hand it to the callback service.
If teardown has already cleared xprt->bc_serv, xprt_enqueue_bc_request() currently returns without enqueueing or freeing the committed request. The xprt_get() taken on entry is leaked as well. If the producer wins the race before bc_serv is cleared, it can also enqueue onto sv_cb_list after nfs_callback_down() has stopped the callback threads, leaving the request linked to a svc_serv that is about to be freed.
Close the producer side before callback threads are stopped. Add xprt_svc_shutdown_bc() to clear xprt->bc_serv under bc_pa_lock, and call it on callback shutdown and callback-start failure before stopping the service threads. Requests that lose the NULL transition in xprt_enqueue_bc_request() are released through the normal backchannel free path after balancing bc_slot_count. Finally, drain any remaining sv_cb_list requests after the callback threads have stopped and before svc_destroy() frees the service.
--- fs/nfs/callback.c | 4 +++- include/linux/sunrpc/bc_xprt.h | 5 +++++ net/sunrpc/backchannel_rqst.c | 38 +++++++++++++++++++++++++++++++------- 3 files changed, 39 insertions(+), 8 deletions(-)
fs/nfs/callback.c +3 -1
@@ -231,8 +231,9 @@ int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt) cb_info->users++; err_net:if (!cb_info->users) { +xprt_svc_shutdown_bc (xprt);svc_set_num_threads (cb_info->serv,0 ,0 ); -svc_destroy (&cb_info->serv); +xprt_svc_destroy_nullify_bc (xprt, &cb_info->serv); } err_create:mutex_unlock (&nfs_callback_mutex);
@@ -254,6 +255,7 @@ void nfs_callback_down(int minorversion, struct net *net, struct rpc_xprt *xprt)mutex_lock (&nfs_callback_mutex); serv = cb_info->serv; +xprt_svc_shutdown_bc (xprt);nfs_callback_down_net (minorversion, serv, net); cb_info->users--;if (cb_info->users ==0 ) {
include/linux/sunrpc/bc_xprt.h +5 -0
@@ -32,6 +32,7 @@ int xprt_setup_bc(struct rpc_xprt *xprt, unsigned int min_reqs);void xprt_destroy_bc (struct rpc_xprt *xprt,unsigned int max_reqs);void xprt_free_bc_rqst (struct rpc_rqst *req);unsigned int xprt_bc_max_slots (struct rpc_xprt *xprt); +void xprt_svc_shutdown_bc (struct rpc_xprt *xprt);void xprt_svc_destroy_nullify_bc (struct rpc_xprt *xprt,struct svc_serv **serv);/*
@@ -71,6 +72,10 @@ static inline void xprt_free_bc_request(struct rpc_rqst *req) { } +static inline void xprt_svc_shutdown_bc (struct rpc_xprt *xprt) +{ +} +static inline void xprt_svc_destroy_nullify_bc (struct rpc_xprt *xprt,struct svc_serv **serv) {svc_destroy (serv);
net/sunrpc/backchannel_rqst.c +31 -7
@@ -25,20 +25,39 @@ unsigned int xprt_bc_max_slots(struct rpc_xprt *xprt) }/* -* Helper function to nullify backchannel server pointer in transport. -* We need to synchronize setting the pointer to NULL (done so after -* the backchannel server is shutdown) with the usage of that pointer -* by the backchannel request processing routines -* xprt_complete_bc_request() and rpcrdma_bc_receive_call(). +* Close the backchannel producer side, drain any requests still +* queued on sv_cb_list, then destroy the callback service. */ void xprt_svc_destroy_nullify_bc (struct rpc_xprt *xprt,struct svc_serv **serv) { -spin_lock (&xprt->bc_pa_lock); +struct svc_serv *bc_serv = *serv; +struct rpc_rqst *req; + +xprt_svc_shutdown_bc (xprt); + while ((req = lwq_dequeue(&bc_serv->sv_cb_list, struct rpc_rqst, + rq_bc_list)) !=NULL ) { +atomic_dec (&req->rq_xprt->bc_slot_count); +xprt_free_bc_request (req); + }svc_destroy (serv); +} +EXPORT_SYMBOL_GPL (xprt_svc_destroy_nullify_bc); + +/* +* Clear the backchannel server pointer in the transport. The NULL +* store is serialized under bc_pa_lock against readers of +* xprt->bc_serv in xprt_complete_bc_request() and +* rpcrdma_bc_receive_call(). Clearing it before the callback service +* is stopped prevents a producer from enqueueing onto a service that +* is being torn down. +*/ +void xprt_svc_shutdown_bc (struct rpc_xprt *xprt) +{ +spin_lock (&xprt->bc_pa_lock); xprt->bc_serv =NULL ;spin_unlock (&xprt->bc_pa_lock); } -EXPORT_SYMBOL_GPL (xprt_svc_destroy_nullify_bc); +EXPORT_SYMBOL_GPL (xprt_svc_shutdown_bc);/* * Helper routines that track the number of preallocation elements
@@ -393,7 +412,12 @@ void xprt_enqueue_bc_request(struct rpc_rqst *req)if (bc_serv) {lwq_enqueue (&req->rq_bc_list, &bc_serv->sv_cb_list);svc_pool_wake_idle_thread (&bc_serv->sv_pools[0 ]); +spin_unlock (&xprt->bc_pa_lock); +return ; }spin_unlock (&xprt->bc_pa_lock); + +atomic_dec (&xprt->bc_slot_count); +xprt_free_bc_request (req); }EXPORT_SYMBOL_GPL (xprt_enqueue_bc_request);
Signature · 1 line
2.54.0
Quoted 41 lines · Chuck Lever
Address findings from an audit of how the server-side RPCSEC GSS accept path handles partially decoded and stale credential state. The common defect class:
gss_svc_dataand therpc_gss_wire_credit embeds (svcdata->clcred) are allocated with non-zeroing kmalloc and reused across requests, so an early decode failure leaves this request's partial writes mixed with the previous request's residue for a later consumer to trust.The two gssx option-array decoder patches carry a hard ordering dependency. The error-path fix must come first: it wires
free_svc_cred()intofree_creds: and converts theout_free_groups:
teardown to the refcount-awareput_group_info(). Only once that refcount-aware free path exists does rejecting a duplicateCREDS_VALUEoption actually release the single installedgroup_infoinstead of leaking it. Reviewed out of order, the duplicate-rejection patch looks incomplete.The backchannel patch stands apart from the GSS decode work: a genuine race and use-after-free in callback-service teardown. It closes the producer side -- clearing
xprt->bc_servunderbc_pa_lock-- before the callback threads stop, then drains any request that raced in beforesvc_destroy()frees the service.---
Chris Mason (4):
SUNRPC: fixgssx_dec_option_arrayerror path bugs
SUNRPC: reject duplicateCREDS_VALUEoptions
SUNRPC: Guardsvcauth_gss_release()dispatch onrq_auth_stat
SUNRPC: Zerorpc_gss_wire_credatsvcauth_gss_decode_credbody()entryChuck Lever (2):
SUNRPC: Reject krb5 v2 wrap tokens with oversized ec field
SUNRPC: close backchannel before destroying callback servicefs/nfs/callback.c | 4 +++- include/linux/sunrpc/bc_xprt.h | 5 +++++ net/sunrpc/auth_gss/gss_krb5_wrap.c | 2 ++ net/sunrpc/auth_gss/gss_rpc_xdr.c | 15 ++++++++++++--- net/sunrpc/auth_gss/svcauth_gss.c | 5 +++++ net/sunrpc/backchannel_rqst.c | 38 ++++++++++++++++++++++++++++++------- 6 files changed, 58 insertions(+), 11 deletions(-)
Quoted 7 lines
---
base-commit:4d4d6605de5f91a40335729b6a7cc15e83b280f3
change-id: 20260528-tier2-d0dedb949b3dBest regards,
Signature · 1 line
Chuck Lever <[email protected]>