Skip to content

Commit ef3675b

Browse files
committed
NFSD: Encode COMPOUND operation status on page boundaries
J. David reports an odd corruption of a READDIR reply sent to a FreeBSD client. xdr_reserve_space() has to do a special trick when the @nbytes value requests more space than there is in the current page of the XDR buffer. In that case, xdr_reserve_space() returns a pointer to the start of the next page, and then the next call to xdr_reserve_space() invokes __xdr_commit_encode() to copy enough of the data item back into the previous page to make that data item contiguous across the page boundary. But we need to be careful in the case where buffer space is reserved early for a data item whose value will be inserted into the buffer later. One such caller, nfsd4_encode_operation(), reserves 8 bytes in the encoding buffer for each COMPOUND operation. However, a READDIR result can sometimes encode file names so that there are only 4 bytes left at the end of the current XDR buffer page (though plenty of pages are left to handle the remaining encoding tasks). If a COMPOUND operation follows the READDIR result (say, a GETATTR), then nfsd4_encode_operation() will reserve 8 bytes for the op number (9) and the op status (usually NFS4_OK). In this weird case, xdr_reserve_space() returns a pointer to byte zero of the next buffer page, as it assumes the data item will be copied back into place (in the previous page) on the next call to xdr_reserve_space(). nfsd4_encode_operation() writes the op num into the buffer, then saves the next 4-byte location for the op's status code. The next xdr_reserve_space() call is part of GETATTR encoding, so the op num gets copied back into the previous page, but the saved location for the op status continues to point to the wrong spot in the current XDR buffer page because __xdr_commit_encode() moved that data item. After GETATTR encoding is complete, nfsd4_encode_operation() writes the op status over the first XDR data item in the GETATTR result. The NFS4_OK status code (0) makes it look like there are zero items in the GETATTR's attribute bitmask. The patch description of commit 2825a7f ("nfsd4: allow encoding across page boundaries") [2014] remarks that NFSD "can't handle a new operation starting close to the end of a page." This bug appears to be one reason for that remark. Reported-by: J David <[email protected]> Closes: https://lore.kernel.org/linux-nfs/[email protected]/T/#t Tested-by: Rick Macklem <[email protected]> Reviewed-by: NeilBrown <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Cc: [email protected] Signed-off-by: Chuck Lever <[email protected]>
1 parent 2530766 commit ef3675b

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

fs/nfsd/nfs4xdr.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5761,15 +5761,14 @@ nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
57615761
struct nfs4_stateowner *so = resp->cstate.replay_owner;
57625762
struct svc_rqst *rqstp = resp->rqstp;
57635763
const struct nfsd4_operation *opdesc = op->opdesc;
5764-
int post_err_offset;
5764+
unsigned int op_status_offset;
57655765
nfsd4_enc encoder;
5766-
__be32 *p;
57675766

5768-
p = xdr_reserve_space(xdr, 8);
5769-
if (!p)
5767+
if (xdr_stream_encode_u32(xdr, op->opnum) != XDR_UNIT)
5768+
goto release;
5769+
op_status_offset = xdr->buf->len;
5770+
if (!xdr_reserve_space(xdr, XDR_UNIT))
57705771
goto release;
5771-
*p++ = cpu_to_be32(op->opnum);
5772-
post_err_offset = xdr->buf->len;
57735772

57745773
if (op->opnum == OP_ILLEGAL)
57755774
goto status;
@@ -5810,20 +5809,21 @@ nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
58105809
* bug if we had to do this on a non-idempotent op:
58115810
*/
58125811
warn_on_nonidempotent_op(op);
5813-
xdr_truncate_encode(xdr, post_err_offset);
5812+
xdr_truncate_encode(xdr, op_status_offset + XDR_UNIT);
58145813
}
58155814
if (so) {
5816-
int len = xdr->buf->len - post_err_offset;
5815+
int len = xdr->buf->len - (op_status_offset + XDR_UNIT);
58175816

58185817
so->so_replay.rp_status = op->status;
58195818
so->so_replay.rp_buflen = len;
5820-
read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
5819+
read_bytes_from_xdr_buf(xdr->buf, op_status_offset + XDR_UNIT,
58215820
so->so_replay.rp_buf, len);
58225821
}
58235822
status:
58245823
op->status = nfsd4_map_status(op->status,
58255824
resp->cstate.minorversion);
5826-
*p = op->status;
5825+
write_bytes_to_xdr_buf(xdr->buf, op_status_offset,
5826+
&op->status, XDR_UNIT);
58275827
release:
58285828
if (opdesc && opdesc->op_release)
58295829
opdesc->op_release(&op->u);

0 commit comments

Comments
 (0)