From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Sasha Levin To: "stable@vger.kernel.org" , "linux-kernel@vger.kernel.org" CC: Jann Horn , Boris Brezillon , Sasha Levin Subject: [PATCH AUTOSEL 4.4 03/20] mtdchar: fix overflows in adjustment of `count` Date: Sat, 15 Sep 2018 01:35:07 +0000 Message-ID: <20180915013502.180110-3-alexander.levin@microsoft.com> References: <20180915013502.180110-1-alexander.levin@microsoft.com> In-Reply-To: <20180915013502.180110-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org List-ID: From: Jann Horn [ Upstream commit 6c6bc9ea84d0008024606bf5ba10519e20d851bf ] The first checks in mtdchar_read() and mtdchar_write() attempt to limit `count` such that `*ppos + count <=3D mtd->size`. However, they ignore the possibility of `*ppos > mtd->size`, allowing the calculation of `count` to wrap around. `mtdchar_lseek()` prevents seeking beyond mtd->size, but the pread/pwrite syscalls bypass this. I haven't found any codepath on which this actually causes dangerous behavior, but it seems like a sensible change anyway. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Jann Horn Signed-off-by: Boris Brezillon Signed-off-by: Sasha Levin --- drivers/mtd/mtdchar.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c index 6d19835b80a9..0d244dac1ccb 100644 --- a/drivers/mtd/mtdchar.c +++ b/drivers/mtd/mtdchar.c @@ -160,8 +160,12 @@ static ssize_t mtdchar_read(struct file *file, char __= user *buf, size_t count, =20 pr_debug("MTD_read\n"); =20 - if (*ppos + count > mtd->size) - count =3D mtd->size - *ppos; + if (*ppos + count > mtd->size) { + if (*ppos < mtd->size) + count =3D mtd->size - *ppos; + else + count =3D 0; + } =20 if (!count) return 0; @@ -246,7 +250,7 @@ static ssize_t mtdchar_write(struct file *file, const c= har __user *buf, size_t c =20 pr_debug("MTD_write\n"); =20 - if (*ppos =3D=3D mtd->size) + if (*ppos >=3D mtd->size) return -ENOSPC; =20 if (*ppos + count > mtd->size) --=20 2.17.1