Skip to content

Commit db3f58a

Browse files
author
J. Bruce Fields
committed
rpc: define xdr_restrict_buflen
With this xdr_reserve_space can help us enforce various limits. Signed-off-by: J. Bruce Fields <[email protected]>
1 parent 3059676 commit db3f58a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

include/linux/sunrpc/xdr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32
217217
extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes);
218218
extern void xdr_commit_encode(struct xdr_stream *xdr);
219219
extern void xdr_truncate_encode(struct xdr_stream *xdr, size_t len);
220+
extern int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen);
220221
extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages,
221222
unsigned int base, unsigned int len);
222223
extern unsigned int xdr_stream_pos(const struct xdr_stream *xdr);

net/sunrpc/xdr.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,35 @@ void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
648648
}
649649
EXPORT_SYMBOL(xdr_truncate_encode);
650650

651+
/**
652+
* xdr_restrict_buflen - decrease available buffer space
653+
* @xdr: pointer to xdr_stream
654+
* @newbuflen: new maximum number of bytes available
655+
*
656+
* Adjust our idea of how much space is available in the buffer.
657+
* If we've already used too much space in the buffer, returns -1.
658+
* If the available space is already smaller than newbuflen, returns 0
659+
* and does nothing. Otherwise, adjusts xdr->buf->buflen to newbuflen
660+
* and ensures xdr->end is set at most offset newbuflen from the start
661+
* of the buffer.
662+
*/
663+
int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
664+
{
665+
struct xdr_buf *buf = xdr->buf;
666+
int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
667+
int end_offset = buf->len + left_in_this_buf;
668+
669+
if (newbuflen < 0 || newbuflen < buf->len)
670+
return -1;
671+
if (newbuflen > buf->buflen)
672+
return 0;
673+
if (newbuflen < end_offset)
674+
xdr->end = (void *)xdr->end + newbuflen - end_offset;
675+
buf->buflen = newbuflen;
676+
return 0;
677+
}
678+
EXPORT_SYMBOL(xdr_restrict_buflen);
679+
651680
/**
652681
* xdr_write_pages - Insert a list of pages into an XDR buffer for sending
653682
* @xdr: pointer to xdr_stream

0 commit comments

Comments
 (0)