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 bsc#1210124

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 |    2 ++
 fs/nfsd/nfs3xdr.c  |   11 +++++++++--
 2 files changed, 11 insertions(+), 2 deletions(-)

--- a/fs/nfsd/nfs3proc.c
+++ b/fs/nfsd/nfs3proc.c
@@ -171,6 +171,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
@@ -369,14 +369,21 @@ nfs3svc_decode_readargs(struct svc_rqst
 	unsigned int len;
 	int v;
 	u32 max_blocksize = svc_max_payload(rqstp);
+	unsigned int pages;
+
+	/* calculate available pages for reply body */
+	pages = (rqstp->rq_server->sv_max_mesg / PAGE_SIZE + 1);
+	pages -= (rqstp->rq_next_page - rqstp->rq_pages);
 
 	p = decode_fh(p, &args->fh);
 	if (!p)
 		return 0;
 	p = xdr_decode_hyper(p, &args->offset);
 
-	args->count = ntohl(*p++);
-	len = min(args->count, max_blocksize);
+	len = ntohl(*p++);
+	len = min(len, max_blocksize);
+	len = min_t(unsigned int, len, pages * PAGE_SIZE);
+	args->count = len;
 
 	/* set up the kvec */
 	v=0;