Commit 9f864c80 authored by Jens Axboe's avatar Jens Axboe

block: take care not to overflow when calculating total iov length

Reported-by: default avatarDan Rosenberg <drosenberg@vsecurity.com>
Cc: stable@kernel.org
Signed-off-by: default avatarJens Axboe <jaxboe@fusionio.com>
parent 9284bcf4
...@@ -321,33 +321,47 @@ static int sg_io(struct request_queue *q, struct gendisk *bd_disk, ...@@ -321,33 +321,47 @@ static int sg_io(struct request_queue *q, struct gendisk *bd_disk,
if (hdr->iovec_count) { if (hdr->iovec_count) {
const int size = sizeof(struct sg_iovec) * hdr->iovec_count; const int size = sizeof(struct sg_iovec) * hdr->iovec_count;
size_t iov_data_len; size_t iov_data_len;
struct sg_iovec *iov; struct sg_iovec *sg_iov;
struct iovec *iov;
int i;
iov = kmalloc(size, GFP_KERNEL); sg_iov = kmalloc(size, GFP_KERNEL);
if (!iov) { if (!sg_iov) {
ret = -ENOMEM; ret = -ENOMEM;
goto out; goto out;
} }
if (copy_from_user(iov, hdr->dxferp, size)) { if (copy_from_user(sg_iov, hdr->dxferp, size)) {
kfree(iov); kfree(sg_iov);
ret = -EFAULT; ret = -EFAULT;
goto out; goto out;
} }
/*
* Sum up the vecs, making sure they don't overflow
*/
iov = (struct iovec *) sg_iov;
iov_data_len = 0;
for (i = 0; i < hdr->iovec_count; i++) {
if (iov_data_len + iov[i].iov_len < iov_data_len) {
kfree(sg_iov);
ret = -EINVAL;
goto out;
}
iov_data_len += iov[i].iov_len;
}
/* SG_IO howto says that the shorter of the two wins */ /* SG_IO howto says that the shorter of the two wins */
iov_data_len = iov_length((struct iovec *)iov,
hdr->iovec_count);
if (hdr->dxfer_len < iov_data_len) { if (hdr->dxfer_len < iov_data_len) {
hdr->iovec_count = iov_shorten((struct iovec *)iov, hdr->iovec_count = iov_shorten(iov,
hdr->iovec_count, hdr->iovec_count,
hdr->dxfer_len); hdr->dxfer_len);
iov_data_len = hdr->dxfer_len; iov_data_len = hdr->dxfer_len;
} }
ret = blk_rq_map_user_iov(q, rq, NULL, iov, hdr->iovec_count, ret = blk_rq_map_user_iov(q, rq, NULL, sg_iov, hdr->iovec_count,
iov_data_len, GFP_KERNEL); iov_data_len, GFP_KERNEL);
kfree(iov); kfree(sg_iov);
} else if (hdr->dxfer_len) } else if (hdr->dxfer_len)
ret = blk_rq_map_user(q, rq, NULL, hdr->dxferp, hdr->dxfer_len, ret = blk_rq_map_user(q, rq, NULL, hdr->dxferp, hdr->dxfer_len,
GFP_KERNEL); GFP_KERNEL);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment