Skip to content

Commit 42f5b80

Browse files
committed
SUNRPC: Bound-check xdr_buf_to_bvec() stores before writing
xdr_buf_to_bvec() writes a bio_vec into the caller's array before testing whether that slot is in range, and the head branch performs the store with no check at all. When the caller's budget is exactly used up, the next store lands one element past the end of the array. The overflow label returns count - 1, which masks the surplus store but cannot undo it. rq_bvec, the array passed by nfsd_vfs_write(), is allocated to exactly rq_maxpages entries with no slack. The OOB store can land in adjacent slab memory; the bv_len and bv_offset fields written there are derived from client-supplied RPC payload sizes. Move the in-range check ahead of the store in the head, page-loop, and tail branches. With the check at the top of each sequence, count is incremented only after a successful store, so the overflow label can return count directly. Reported-by: Chris Mason <clm@meta.com> Fixes: 2eb2b93 ("SUNRPC: Convert svc_tcp_sendmsg to use bio_vecs directly") Cc: stable@vger.kernel.org Reviewed-by: Jeff Layton <jlayton@kernel.org> Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
1 parent 30d55c8 commit 42f5b80

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

net/sunrpc/xdr.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ unsigned int xdr_buf_to_bvec(struct bio_vec *bvec, unsigned int bvec_size,
152152
unsigned int count = 0;
153153

154154
if (head->iov_len) {
155+
if (unlikely(count >= bvec_size))
156+
goto bvec_overflow;
155157
bvec_set_virt(bvec++, head->iov_base, head->iov_len);
156158
++count;
157159
}
@@ -165,25 +167,27 @@ unsigned int xdr_buf_to_bvec(struct bio_vec *bvec, unsigned int bvec_size,
165167
while (remaining > 0) {
166168
len = min_t(unsigned int, remaining,
167169
PAGE_SIZE - offset);
170+
if (unlikely(count >= bvec_size))
171+
goto bvec_overflow;
168172
bvec_set_page(bvec++, *pages++, len, offset);
169173
remaining -= len;
170174
offset = 0;
171-
if (unlikely(++count > bvec_size))
172-
goto bvec_overflow;
175+
++count;
173176
}
174177
}
175178

176179
if (tail->iov_len) {
177-
bvec_set_virt(bvec, tail->iov_base, tail->iov_len);
178-
if (unlikely(++count > bvec_size))
180+
if (unlikely(count >= bvec_size))
179181
goto bvec_overflow;
182+
bvec_set_virt(bvec, tail->iov_base, tail->iov_len);
183+
++count;
180184
}
181185

182186
return count;
183187

184188
bvec_overflow:
185189
pr_warn_once("%s: bio_vec array overflow\n", __func__);
186-
return count - 1;
190+
return count;
187191
}
188192
EXPORT_SYMBOL_GPL(xdr_buf_to_bvec);
189193

0 commit comments

Comments
 (0)