Skip to content

Commit bfeea1d

Browse files
Trond MyklebustTrond Myklebust
authored andcommitted
SUNRPC: Don't decode beyond the end of the RPC reply message
Now that xdr_inline_decode() will automatically cross into the page buffers, we need to ensure that it doesn't exceed the total reply message length. This patch sets up a counter that tracks the number of words remaining in the reply message, and ensures that xdr_inline_decode, xdr_read_pages and xdr_enter_page respect the end of message boundary. Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>
1 parent 1537693 commit bfeea1d

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

‎include/linux/sunrpc/xdr.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ struct xdr_stream {
205205
struct kvec *iov; /* pointer to the current kvec */
206206
struct kvec scratch; /* Scratch buffer */
207207
struct page **page_ptr; /* pointer to the current page */
208+
unsigned int nwords; /* Remaining decode buffer length */
208209
};
209210

210211
/*

‎net/sunrpc/xdr.c‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -630,12 +630,15 @@ void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
630630
xdr->buf = buf;
631631
xdr->scratch.iov_base = NULL;
632632
xdr->scratch.iov_len = 0;
633+
xdr->nwords = XDR_QUADLEN(buf->len);
633634
if (buf->head[0].iov_len != 0)
634635
xdr_set_iov(xdr, buf->head, buf->len);
635636
else if (buf->page_len != 0)
636637
xdr_set_page_base(xdr, 0, buf->len);
637-
if (p != NULL && p > xdr->p && xdr->end >= p)
638+
if (p != NULL && p > xdr->p && xdr->end >= p) {
639+
xdr->nwords -= p - xdr->p;
638640
xdr->p = p;
641+
}
639642
}
640643
EXPORT_SYMBOL_GPL(xdr_init_decode);
641644

@@ -660,12 +663,14 @@ EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
660663

661664
static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
662665
{
666+
unsigned int nwords = XDR_QUADLEN(nbytes);
663667
__be32 *p = xdr->p;
664-
__be32 *q = p + XDR_QUADLEN(nbytes);
668+
__be32 *q = p + nwords;
665669

666-
if (unlikely(q > xdr->end || q < p))
670+
if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
667671
return NULL;
668672
xdr->p = q;
673+
xdr->nwords -= nwords;
669674
return p;
670675
}
671676

@@ -746,9 +751,16 @@ void xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
746751
struct xdr_buf *buf = xdr->buf;
747752
struct kvec *iov;
748753
ssize_t shift;
754+
unsigned int nwords = XDR_QUADLEN(len);
749755
unsigned int end;
750756
int padding;
751757

758+
if (xdr->nwords == 0)
759+
return;
760+
if (nwords > xdr->nwords) {
761+
nwords = xdr->nwords;
762+
len = nwords << 2;
763+
}
752764
/* Realign pages to current pointer position */
753765
iov = buf->head;
754766
shift = iov->iov_len + (char *)iov->iov_base - (char *)xdr->p;
@@ -758,22 +770,23 @@ void xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
758770
/* Truncate page data and move it into the tail */
759771
if (buf->page_len > len)
760772
xdr_shrink_pagelen(buf, buf->page_len - len);
761-
padding = (XDR_QUADLEN(len) << 2) - len;
773+
padding = (nwords << 2) - len;
762774
xdr->iov = iov = buf->tail;
763775
/* Compute remaining message length. */
764776
end = iov->iov_len;
765777
shift = buf->buflen - buf->len;
766-
if (shift < end)
778+
if (end > shift + padding)
767779
end -= shift;
768-
else if (shift > 0)
769-
end = 0;
780+
else
781+
end = padding;
770782
/*
771783
* Position current pointer at beginning of tail, and
772784
* set remaining message length.
773785
*/
774786
xdr->p = (__be32 *)((char *)iov->iov_base + padding);
775787
xdr->end = (__be32 *)((char *)iov->iov_base + end);
776788
xdr->page_ptr = NULL;
789+
xdr->nwords = XDR_QUADLEN(end - padding);
777790
}
778791
EXPORT_SYMBOL_GPL(xdr_read_pages);
779792

@@ -795,6 +808,7 @@ void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
795808
* set remaining message length.
796809
*/
797810
xdr_set_page_base(xdr, 0, len);
811+
xdr->nwords += XDR_QUADLEN(xdr->buf->page_len);
798812
}
799813
EXPORT_SYMBOL_GPL(xdr_enter_page);
800814

0 commit comments

Comments
 (0)