Skip to content

Commit e4185be

Browse files
Dan Carpentermiquelraynal
authored andcommitted
mtdchar: fix integer overflow in read/write ioctls
The "req.start" and "req.len" variables are u64 values that come from the user at the start of the function. We mask away the high 32 bits of "req.len" so that's capped at U32_MAX but the "req.start" variable can go up to U64_MAX which means that the addition can still integer overflow. Use check_add_overflow() to fix this bug. Fixes: 095bb6e ("mtdchar: add MEMREAD ioctl") Fixes: 6420ac0 ("mtdchar: prevent unbounded allocation in MEMWRITE ioctl") Cc: stable@vger.kernel.org Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
1 parent 3a86608 commit e4185be

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

drivers/mtd/mtdchar.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
599599
uint8_t *datbuf = NULL, *oobbuf = NULL;
600600
size_t datbuf_len, oobbuf_len;
601601
int ret = 0;
602+
u64 end;
602603

603604
if (copy_from_user(&req, argp, sizeof(req)))
604605
return -EFAULT;
@@ -618,7 +619,7 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
618619
req.len &= 0xffffffff;
619620
req.ooblen &= 0xffffffff;
620621

621-
if (req.start + req.len > mtd->size)
622+
if (check_add_overflow(req.start, req.len, &end) || end > mtd->size)
622623
return -EINVAL;
623624

624625
datbuf_len = min_t(size_t, req.len, mtd->erasesize);
@@ -698,6 +699,7 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
698699
size_t datbuf_len, oobbuf_len;
699700
size_t orig_len, orig_ooblen;
700701
int ret = 0;
702+
u64 end;
701703

702704
if (copy_from_user(&req, argp, sizeof(req)))
703705
return -EFAULT;
@@ -724,7 +726,7 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
724726
req.len &= 0xffffffff;
725727
req.ooblen &= 0xffffffff;
726728

727-
if (req.start + req.len > mtd->size) {
729+
if (check_add_overflow(req.start, req.len, &end) || end > mtd->size) {
728730
ret = -EINVAL;
729731
goto out;
730732
}

0 commit comments

Comments
 (0)