Skip to content

Commit

Permalink
NVMe: check for integer overflow in nvme_map_user_pages()
Browse files Browse the repository at this point in the history
You need to have CAP_SYS_ADMIN to trigger this overflow but it makes the
static checkers complain so we should fix it.  The worry is that
"length" comes from copy_from_user() so we need to check that "length +
offset" can't overflow.

I also changed the min_t() cast to be unsigned instead of signed.  Now
that we cap "length" to INT_MAX it doesn't make a difference, but it's a
little easier for reviewers to know that large values aren't cast to
negative.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Matthew Wilcox <matthew.r.wilcox@intel.com>
  • Loading branch information
Dan Carpenter authored and Matthew Wilcox committed May 17, 2013
1 parent 5be37bf commit 5460fc0
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions drivers/block/nvme-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,

if (addr & 3)
return ERR_PTR(-EINVAL);
if (!length)
if (!length || length > INT_MAX - PAGE_SIZE)
return ERR_PTR(-EINVAL);

offset = offset_in_page(addr);
Expand All @@ -1227,7 +1227,8 @@ struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
sg_init_table(sg, count);
for (i = 0; i < count; i++) {
sg_set_page(&sg[i], pages[i],
min_t(int, length, PAGE_SIZE - offset), offset);
min_t(unsigned, length, PAGE_SIZE - offset),
offset);
length -= (PAGE_SIZE - offset);
offset = 0;
}
Expand Down

0 comments on commit 5460fc0

Please sign in to comment.