Skip to contentSkip to conversation

SUNRPC: Fix a stale RPC GSS Reply match and two decode bugs

PATCH New Chuck Lever 1 participant 6 messages Dec 5

Subsystems (3)
lore
Share & tools
lore

Chuck Lever

author maintainer
#0 lore raw

v2 cover 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. Those 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.

Accepting a Reply body that matches an older entry is safe only if every cached sequence number belongs to the current GSS context. Currently a credential renewal leaves the old context's numbers in the cache, so renewal has to empty the cache before the body check changes.

Auditing gss_unwrap_resp_integ() for the EIO 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.

v1 was 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. v2 adds patch 2, which has been build-tested only.

---
Changes in v2:
- New patch 2: discard cached seqnos on cred renewal (sashiko-bot).
- Link to v1: https://patch.msgid.link/[email protected]

---
Chuck Lever (5):
SUNRPC: Fix integer overflow in gss_unwrap_resp_integ() bounds check
SUNRPC: Discard cached GSS sequence numbers on credential renewal
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 | 26 +++++++++++++++++++-------
 2 files changed, 20 insertions(+), 7 deletions(-)

---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261204-gss-stale-reply-452ba46a9d91

Best regards,

Signature · 1 line
Chuck Lever
author maintainer
#1 lore raw

PATCH v2 1/5 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.

Fixes 4047aa909c4a ("sunrpc: Fix gss_unwrap_resp_integ() again")
---
 net/sunrpc/auth_gss/auth_gss.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sunrpc/auth_gss/auth_gss.c +1 -1
index 8ddc65e894da..50b8503d3a12 100644
@@ -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
author maintainer
#2 lore raw

PATCH v2 2/5 SUNRPC: Discard cached GSS sequence numbers on credential renewal

In reply to Chuck Lever

When gss_marshal() finds that a GSS context has expired, call_refresh() and gss_renew_cred() give the same rpc_rqst a new credential. rq_seqnos[] still holds sequence numbers from the old context. The new context starts numbering again at 1, so a stale entry can equal the sequence number of a different request sent on the new context.

The reply verifier covers the sequence number but not the XID. gss_validate() therefore accepts the verifier from the other request's reply if that reply arrives with the renewed request's XID. A server that echoes XIDs correctly does not send such a reply. With sec=krb5, SUNRPC then decodes the body as the result of the renewed request.

RFC 2203 Section 5.3.3.1 permits a client to match a reply verifier against cached sequence numbers. A match identifies an earlier encoding of a request only if every cached entry comes from the current context. Accepting the body of a late krb5i or krb5p reply, which the unwrap functions currently reject, depends on the same condition.

Empty rq_seqnos[] when replacing the credential. A reply to an encoding under the old context cannot verify with the new context's key, so the old entries are of no further use.

Fixes 08d6ee6d8a10 ("sunrpc: implement rfc2203 rpcsec_gss seqnum cache")
---
 net/sunrpc/auth_gss/auth_gss.c | 2 ++
 1 file changed, 2 insertions(+)
net/sunrpc/auth_gss/auth_gss.c +2 -0
index 50b8503d3a12..6919b2865cfc 100644
@@ -1620,6 +1620,8 @@ static int gss_renew_cred(struct rpc_task *task)
 		return PTR_ERR(new);
 
 	task->tk_rqstp->rq_cred = new;
+	/* Cached sequence numbers belong to the old GSS context */
+	task->tk_rqstp->rq_seqno_count = 0;
 	put_rpccred(oldcred);
 	return 0;
 }
Signature · 1 line
2.56.0
author maintainer
#3 lore raw

PATCH v2 3/5 SUNRPC: Check a GSS reply body against the verifier's sequence number

In reply to Chuck Lever

With 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.

Fixes 08d6ee6d8a10 ("sunrpc: implement rfc2203 rpcsec_gss seqnum cache")
---
 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
index 780eaa157a9f..dabb68be9130 100644
@@ -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
index 6919b2865cfc..57759fd96956 100644
@@ -1726,6 +1726,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))
@@ -2008,7 +2011,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;
@@ -2052,7 +2055,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);
@@ -2066,7 +2069,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));
@@ -2088,7 +2091,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.
@@ -2104,7 +2108,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
author maintainer
#4 lore raw

PATCH v2 4/5 SUNRPC: Limit the size of a GSS reply checksum

In reply to Chuck Lever

gss_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. Both are MICs from the same GSS context, and gss_validate() runs first, so a mechanism with larger tokens would already fail there.

Fixes 4047aa909c4a ("sunrpc: Fix gss_unwrap_resp_integ() again")
---
 net/sunrpc/auth_gss/auth_gss.c | 2 ++
 1 file changed, 2 insertions(+)
net/sunrpc/auth_gss/auth_gss.c +2 -0
index 57759fd96956..55adad44d381 100644
@@ -2029,6 +2029,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
author maintainer
#5 lore raw

PATCH v2 5/5 SUNRPC: Stop krb5i reply decoding at the end of databody_integ

In reply to Chuck Lever

gss_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.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 net/sunrpc/auth_gss/auth_gss.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
net/sunrpc/auth_gss/auth_gss.c +5 -1
index 55adad44d381..568eb5d4a90f 100644
@@ -2006,7 +2006,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))
@@ -2046,6 +2046,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