]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
NFSD: Insulate nfsd4_encode_read() from page boundaries in the encode buffer
authorChuck Lever <chuck.lever@oracle.com>
Tue, 31 Dec 2024 00:28:53 +0000 (19:28 -0500)
committerChuck Lever <chuck.lever@oracle.com>
Sat, 11 Jan 2025 04:39:46 +0000 (23:39 -0500)
Commit 28d5bc468efe ("NFSD: Optimize nfsd4_encode_readv()") replaced
the use of write_bytes_to_xdr_buf() because it's expensive and the
data items to be encoded are already properly aligned.

However, the current code will corrupt the encoded data if the XDR
data items that are reserved early and then poked into the XDR
buffer later happen to fall on a page boundary in the XDR encoding
buffer.

__xdr_commit_encode can shift encoded data items in the encoding
buffer so that pointers returned from xdr_reserve_space() no longer
address the same part of the encoding stream.

This isn't an issue for splice reads because the reserved encode
buffer areas must fall in the XDR buffers header for the splice to
work without error. For vectored reads, however, there is a
possibility of send buffer corruption in rare cases.

Fixes: 28d5bc468efe ("NFSD: Optimize nfsd4_encode_readv()")
Reviewed-by: NeilBrown <neilb@suse.de>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
fs/nfsd/nfs4xdr.c

index b2243b5dbf188702f42807139086e98720841155..2399b17db052d4da10f8d553f5d260b294af6dfc 100644 (file)
@@ -4317,6 +4317,15 @@ static __be32 nfsd4_encode_splice_read(
        int status, space_left;
        __be32 nfserr;
 
+       /*
+        * Splice read doesn't work if encoding has already wandered
+        * into the XDR buf's page array.
+        */
+       if (unlikely(xdr->buf->page_len)) {
+               WARN_ON_ONCE(1);
+               return nfserr_serverfault;
+       }
+
        /*
         * Make sure there is room at the end of buf->head for
         * svcxdr_encode_opaque_pages() to create a tail buffer
@@ -4399,25 +4408,23 @@ nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
        struct nfsd4_compoundargs *argp = resp->rqstp->rq_argp;
        struct nfsd4_read *read = &u->read;
        struct xdr_stream *xdr = resp->xdr;
-       int starting_len = xdr->buf->len;
        bool splice_ok = argp->splice_ok;
+       unsigned int eof_offset;
        unsigned long maxcount;
+       __be32 wire_data[2];
        struct file *file;
-       __be32 *p;
 
        if (nfserr)
                return nfserr;
+
+       eof_offset = xdr->buf->len;
        file = read->rd_nf->nf_file;
 
-       p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
-       if (!p) {
+       /* Reserve space for the eof flag and byte count */
+       if (unlikely(!xdr_reserve_space(xdr, XDR_UNIT * 2))) {
                WARN_ON_ONCE(splice_ok);
                return nfserr_resource;
        }
-       if (resp->xdr->buf->page_len && splice_ok) {
-               WARN_ON_ONCE(1);
-               return nfserr_serverfault;
-       }
        xdr_commit_encode(xdr);
 
        maxcount = min_t(unsigned long, read->rd_length,
@@ -4428,12 +4435,13 @@ nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
        else
                nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
        if (nfserr) {
-               xdr_truncate_encode(xdr, starting_len);
+               xdr_truncate_encode(xdr, eof_offset);
                return nfserr;
        }
 
-       p = xdr_encode_bool(p, read->rd_eof);
-       *p = cpu_to_be32(read->rd_length);
+       wire_data[0] = read->rd_eof ? xdr_one : xdr_zero;
+       wire_data[1] = cpu_to_be32(read->rd_length);
+       write_bytes_to_xdr_buf(xdr->buf, eof_offset, &wire_data, XDR_UNIT * 2);
        return nfs_ok;
 }
 
@@ -5304,10 +5312,6 @@ nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
        p = xdr_reserve_space(xdr, 4 + 8 + 4);
        if (!p)
                return nfserr_io;
-       if (resp->xdr->buf->page_len && splice_ok) {
-               WARN_ON_ONCE(splice_ok);
-               return nfserr_serverfault;
-       }
 
        maxcount = min_t(unsigned long, read->rd_length,
                         (xdr->buf->buflen - xdr->buf->len));