Return-Path: Received: from mail-cys01nam02on0139.outbound.protection.outlook.com ([104.47.37.139]:43744 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932923AbeCSQDT (ORCPT ); Mon, 19 Mar 2018 12:03:19 -0400 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Masahiro Yamada , Boris Brezillon , Sasha Levin Subject: [PATCH AUTOSEL for 4.9 175/281] mtd: nand: check ecc->total sanity in nand_scan_tail Date: Mon, 19 Mar 2018 16:01:20 +0000 Message-ID: <20180319155742.13731-175-alexander.levin@microsoft.com> References: <20180319155742.13731-1-alexander.levin@microsoft.com> In-Reply-To: <20180319155742.13731-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: stable-owner@vger.kernel.org List-ID: From: Masahiro Yamada [ Upstream commit 79e0348c4e24fd1affdcf055e0269755580e0fcc ] Drivers are supposed to set correct ecc->{size,strength,bytes} before calling nand_scan_tail(), but it does not complain about ecc->total bigger than oobsize. In this case, chip->scan_bbt() crashes due to memory corruption, but it is hard to debug. It would be kind to fail it earlier with a clear message. Signed-off-by: Masahiro Yamada Signed-off-by: Boris Brezillon Signed-off-by: Sasha Levin --- drivers/mtd/nand/nand_base.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c index 21c03086bb7f..3bfa8e0e44cf 100644 --- a/drivers/mtd/nand/nand_base.c +++ b/drivers/mtd/nand/nand_base.c @@ -4780,6 +4780,11 @@ int nand_scan_tail(struct mtd_info *mtd) goto err_free; } ecc->total =3D ecc->steps * ecc->bytes; + if (ecc->total > mtd->oobsize) { + WARN(1, "Total number of ECC bytes exceeded oobsize\n"); + ret =3D -EINVAL; + goto err_free; + } =20 /* * The number of bytes available for a client to place data into --=20 2.14.1