SUNRPC: Fix a stale RPC GSS Reply match and two decode bugs
Subsystems (3)
Chuck Lever
maintainercover SUNRPC: Fix a stale RPC GSS Reply match and two decode bugs
We've been chasing the GSS sequence number window under-run bug for many years. One of the side effects of that issue is frequent transport disconnects. Concurrent with those disconnects, I saw a number of EIO failures on client workloads that I couldn't explain:
these RPC tasks should be successful since the request executed on the server, and the Reply is still valid even though its connection is gone.
After a disconnect, call_encode() assigns a waiting request a new GSS sequence number, but the Reply to the earlier encoding can still arrive. gss_validate() tries every cached sequence number against the reply verifier, as RFC 2203 Section 5.3.3.1 expects, and accepts a match against an older entry. gss_unwrap_resp_integ() and gss_unwrap_resp_priv() then compare the Reply body against only the newest cached sequence number, so a Reply that matched on an older entry is rejected and the caller gets EIO for a request the server already executed.
Auditing gss_unwrap_resp_integ() for that fix turned up two further bugs in the same function. The checksum length it reads is bounded only by the space left in the receive buffer, so a forged length near the end of that buffer reaches kmalloc() with no upper bound. Once the checksum verifies, the function also never truncates the xdr_stream to the end of databody_integ, so the upper-layer decoder can read bytes the checksum does not cover.
Tested on 7.4-rc5 against an NFS server, vers=3 and vers=4.2, sec=krb5/krb5i/krb5p, over TCP and RDMA: READ, READDIR, READLINK, and NFSv3 GETACL data matched the server's across a range of sizes, with no unwrap, MIC, or XDR overflow trace events. A 60-second run of concurrent chmod(2) calls against a server dropping connections (via fail_sunrpc) completed with no EIO, during which thousands of replies verified against an older cached sequence number. Forged Reply content was not tested.
---
Chuck Lever (4):
SUNRPC: Fix integer overflow in gss_unwrap_resp_integ() bounds check
SUNRPC: Check a GSS reply body against the verifier's sequence number
SUNRPC: Limit the size of a GSS reply checksum
SUNRPC: Stop krb5i reply decoding at the end of databody_integ
include/linux/sunrpc/xprt.h | 1 + net/sunrpc/auth_gss/auth_gss.c | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-)
---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261204-gss-stale-reply-452ba46a9d91
Best regards,
Signature · 1 line
Chuck Lever
Chuck Lever
maintainerPATCH 1/4 SUNRPC: Fix integer overflow in gss_unwrap_resp_integ() bounds check
gss_unwrap_resp_integ() reads the length of the reply checksum before anything has verified that length. The bounds check adds the length to an offset in 32 bits, so a value close to U32_MAX wraps the sum and passes. kmalloc() then warns about the oversized request. The warning panics an NFS client running with panic_on_warn. A faulty server, or anyone able to modify replies, can send such a length.
Compare the length with the space left in the receive buffer. Check the offset first, because xdr_decode_word() does not limit reads to rcv_buf->len and the subtraction could otherwise underflow.
--- net/sunrpc/auth_gss/auth_gss.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/sunrpc/auth_gss/auth_gss.c +1 -1
@@ -2024,7 +2024,7 @@ gss_unwrap_resp_integ(struct rpc_task *task, struct rpc_cred *cred,if (xdr_decode_word (rcv_buf, offset, &len)) goto unwrap_failed; offset +=sizeof (__be32); -if (offset + len > rcv_buf->len) +if (offset > rcv_buf->len || len > rcv_buf->len - offset) goto unwrap_failed; mic.len = len; mic.data =kmalloc (len,GFP_KERNEL );
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH 2/4 SUNRPC: Check a GSS reply body against the verifier's sequence number
In reply to Chuck LeverWith krb5i or krb5p, an NFS client whose server closes the connection can return EIO for requests the server has already executed. In a 60-second krb5p test where NFSD dropped connections, 130 chmod(2) calls failed.
After a disconnect, call_encode() gives a waiting request a new GSS sequence number, but the reply to the earlier encoding can still arrive. gss_validate() tries all of rq_seqnos[], as RFC 2203 Section 5.3.3.1 suggests, and accepts that reply's verifier. The unwrap functions then compare the reply body with only the newest entry.
Require the reply body to carry the sequence number that matched the verifier.
--- include/linux/sunrpc/xprt.h | 1 + net/sunrpc/auth_gss/auth_gss.c | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-)
include/linux/sunrpc/xprt.h +1 -0
@@ -70,6 +70,7 @@ struct rpc_rqst {int rq_cong;/* has incremented xprt->cong */ u32 rq_seqnos[RPC_GSS_SEQNO_ARRAY_SIZE ];/* past gss req seq nos. */ unsigned int rq_seqno_count;/* number of entries in rq_seqnos */ +u32 rq_verf_seqno;/* seq no. the reply verifier matched */ int rq_enc_pages_num;struct page **rq_enc_pages;/* scratch pages for use by gss privacy code */
net/sunrpc/auth_gss/auth_gss.c +9 -5
@@ -1724,6 +1724,9 @@ gss_validate(struct rpc_task *task, struct xdr_stream *xdr)if (maj_stat) goto bad_mic; +/* The reply body carries the same sequence number as its verifier */ + task->tk_rqstp->rq_verf_seqno = task->tk_rqstp->rq_seqnos[i -1 ]; +/* We leave it to unwrap to calculate au_rslack. For now we just * calculate the length of the verifier: */ if (test_bit (RPCAUTH_AUTH_UPDATE_SLACK , &cred->cr_auth->au_flags))
@@ -2006,7 +2009,7 @@ gss_unwrap_resp_integ(struct rpc_task *task, struct rpc_cred *cred, offset = rcv_buf->len -xdr_stream_remaining (xdr);if (xdr_stream_decode_u32 (xdr, &seqno)) goto unwrap_failed; -if (seqno != *rqstp->rq_seqnos) +if (seqno != rqstp->rq_verf_seqno) goto bad_seqno;if (xdr_buf_subsegment (rcv_buf, &gss_data, offset, len)) goto unwrap_failed;
@@ -2050,7 +2053,7 @@ gss_unwrap_resp_integ(struct rpc_task *task, struct rpc_cred *cred,trace_rpcgss_unwrap_failed (task); goto out; bad_seqno: -trace_rpcgss_bad_seqno (task, *rqstp->rq_seqnos, seqno); +trace_rpcgss_bad_seqno (task, rqstp->rq_verf_seqno, seqno); goto out; bad_mic:trace_rpcgss_verify_mic (task, maj_stat);
@@ -2064,7 +2067,7 @@ gss_unwrap_resp_priv(struct rpc_task *task, struct rpc_cred *cred, {struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;struct kvec *head = rqstp->rq_rcv_buf.head; -u32 offset, opaque_len, maj_stat; +u32 offset, opaque_len, maj_stat, seqno;__be32 *p; p =xdr_inline_decode (xdr,2 *sizeof (*p));
@@ -2086,7 +2089,8 @@ gss_unwrap_resp_priv(struct rpc_task *task, struct rpc_cred *cred,if (maj_stat !=GSS_S_COMPLETE ) goto bad_unwrap;/* gss_unwrap decrypted the sequence number */ -if (be32_to_cpup (p++) != *rqstp->rq_seqnos) + seqno =be32_to_cpup (p++); +if (seqno != rqstp->rq_verf_seqno) goto bad_seqno;/* gss_unwrap redacts the opaque blob from the head iovec.
@@ -2102,7 +2106,7 @@ gss_unwrap_resp_priv(struct rpc_task *task, struct rpc_cred *cred,trace_rpcgss_unwrap_failed (task);return -EIO ; bad_seqno: -trace_rpcgss_bad_seqno (task, *rqstp->rq_seqnos,be32_to_cpup (--p)); +trace_rpcgss_bad_seqno (task, rqstp->rq_verf_seqno, seqno);return -EIO ; bad_unwrap:trace_rpcgss_unwrap (task, maj_stat);
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH 3/4 SUNRPC: Limit the size of a GSS reply checksum
In reply to Chuck Levergss_unwrap_resp_integ() passes the length of the reply checksum to kmalloc() before anything has verified the reply body. The only limit is the space left in the receive buffer. On a large READ, a faulty server, or anyone able to modify replies, can make the NFS client attempt a contiguous allocation of nearly the size of the reply.
Reject a checksum longer than RPC_MAX_AUTH_SIZE, which is the limit gss_validate() applies to the MIC in the reply verifier.
--- net/sunrpc/auth_gss/auth_gss.c | 2 ++ 1 file changed, 2 insertions(+)
net/sunrpc/auth_gss/auth_gss.c +2 -0
@@ -2027,6 +2027,8 @@ gss_unwrap_resp_integ(struct rpc_task *task, struct rpc_cred *cred,if (xdr_decode_word (rcv_buf, offset, &len)) goto unwrap_failed; offset +=sizeof (__be32); +if (len >RPC_MAX_AUTH_SIZE ) + goto unwrap_failed;if (offset > rcv_buf->len || len > rcv_buf->len - offset) goto unwrap_failed; mic.len = len;
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH 4/4 SUNRPC: Stop krb5i reply decoding at the end of databody_integ
In reply to Chuck Levergss_unwrap_resp_integ() verifies the reply checksum over databody_integ but leaves the rest of the reply in the xdr_stream. The upper layer decoder can therefore read bytes that the checksum does not cover.
gss_unwrap_resp_integ() also accepts a databody_integ that holds only the sequence number. The MIC in the reply verifier covers the same four bytes, so anyone able to modify replies can copy that MIC in as the checksum. The NFS client then decodes its results from the checksum field and from any bytes appended to the reply.
Truncate the xdr_stream at the end of databody_integ once the checksum verifies. Reject a databody_integ too short to hold the sequence number.
--- net/sunrpc/auth_gss/auth_gss.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
net/sunrpc/auth_gss/auth_gss.c +5 -1
@@ -2004,7 +2004,7 @@ gss_unwrap_resp_integ(struct rpc_task *task, struct rpc_cred *cred,/* opaque databody_integ<>; */ if (xdr_stream_decode_u32 (xdr, &len)) goto unwrap_failed; -if (len &3 ) +if (len <XDR_UNIT || len &3 ) goto unwrap_failed; offset = rcv_buf->len -xdr_stream_remaining (xdr);if (xdr_stream_decode_u32 (xdr, &seqno))
@@ -2044,6 +2044,10 @@ gss_unwrap_resp_integ(struct rpc_task *task, struct rpc_cred *cred,if (maj_stat !=GSS_S_COMPLETE ) goto bad_mic; +/* Nothing past databody_integ is covered by the checksum */ +xdr_truncate_decode (xdr,xdr_stream_remaining (xdr) - + (gss_data.len -XDR_UNIT )); +gss_update_rslack (task, cred,2 ,2 +1 +XDR_QUADLEN (mic.len)); ret =0 ;
Signature · 1 line
2.56.0