Blob Blame History Raw
From: Chuck Lever <chuck.lever@oracle.com>
Date: Thu, 1 Sep 2022 15:10:24 -0400
Subject: [PATCH] NFSD: Protect against send buffer overflow in NFSv3 READ
Git-commit: fa6be9cc6e80ec79892ddf08a8c10cabab9baf38
Patch-mainline: v6.1
References: bsc#1205128 CVE-2022-43945

Since before the git era, NFSD has conserved the number of pages
held by each nfsd thread by combining the RPC receive and send
buffers into a single array of pages. This works because there are
no cases where an operation needs a large RPC Call message and a
large RPC Reply at the same time.

Once an RPC Call has been received, svc_process() updates
svc_rqst::rq_res to describe the part of rq_pages that can be
used for constructing the Reply. This means that the send buffer
(rq_res) shrinks when the received RPC record containing the RPC
Call is large.

A client can force this shrinkage on TCP by sending a correctly-
formed RPC Call header contained in an RPC record that is
excessively large. The full maximum payload size cannot be
constructed in that case.

Cc: <stable@vger.kernel.org>
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Acked-by: NeilBrown <neilb@suse.com>

---
 fs/nfsd/nfs3proc.c |    4 ++++
 fs/nfsd/nfs3xdr.c  |    1 +
 2 files changed, 5 insertions(+)

--- a/fs/nfsd/nfs3proc.c
+++ b/fs/nfsd/nfs3proc.c
@@ -156,6 +156,8 @@ nfsd3_proc_read(struct svc_rqst *rqstp)
 	u32	max_blocksize = svc_max_payload(rqstp);
 	unsigned long cnt = min(argp->count, max_blocksize);
 
+	cnt = min_t(unsigned long, cnt, rqstp->rq_res.buflen);
+
 	dprintk("nfsd: READ(3) %s %lu bytes at %Lu\n",
 				SVCFH_fmt(&argp->fh),
 				(unsigned long) argp->count,
@@ -166,6 +168,8 @@ nfsd3_proc_read(struct svc_rqst *rqstp)
 	 * + 1 (xdr opaque byte count) = 26
 	 */
 	resp->count = cnt;
+	if (argp->offset > (u64)OFFSET_MAX)
+		argp->offset = (u64)OFFSET_MAX;
 	svc_reserve_auth(rqstp, ((1 + NFS3_POST_OP_ATTR_WORDS + 3)<<2) + resp->count +4);
 
 	fh_copy(&resp->fh, &argp->fh);
--- a/fs/nfsd/nfs3xdr.c
+++ b/fs/nfsd/nfs3xdr.c
@@ -377,6 +377,7 @@ nfs3svc_decode_readargs(struct svc_rqst
 
 	args->count = ntohl(*p++);
 	len = min(args->count, max_blocksize);
+	len = min(len, rqstp->rq_res.buflen);
 
 	/* set up the kvec */
 	v=0;