From patchwork Thu Nov 8 11:07:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tudor Ambarus X-Patchwork-Id: 994763 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=microchip.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="h+DFe+RK"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=infradead.org header.i=@infradead.org header.b="V0vipPTO"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=microchiptechnology.onmicrosoft.com header.i=@microchiptechnology.onmicrosoft.com header.b="qzQObTVD"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42rL950PvBz9sBZ for ; Thu, 8 Nov 2018 22:07:37 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:In-Reply-To:References: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=w7sjRJPSQNSe2v6Z7W3O2T/iMD+pIhFvVD/41KhBfRI=; b=h+DFe+RKKwhVDi cfC/4Vkzf55zQvFE1SgaNTEVxzHgm5Kqoul85WeRL6DHih9pk3WJILXQeyG4JpUX8+83VJJUPMUNm 9GNQaevDgl+H7C2fImgBIWiVkp81zhTVYW4Cx9Z/RNqYITctaB+u3Ds9v5Yu6hAgSS9J/nTe71MPD zv9/1eEYXBZDOuuOxuqk1NRT+jy2YJK3/XisRLL3Yu6Hc0DJRfPfwavs9pJRbt3KTwTKc86Pa1Gow rkYuhtmfGQKfqDApKubBR6Wd4tl9CW0Attua9128KCX3Td/wJ8loNvQhgju+bKLTFJN1koa+OpTUp 1lhMZFTcUVC0k/bVE4EQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gKi9t-0007OE-0L; Thu, 08 Nov 2018 11:07:33 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gKi9o-0007FZ-8Z for linux-mtd@bombadil.infradead.org; Thu, 08 Nov 2018 11:07:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=MIME-Version:Content-Transfer-Encoding: Content-Type:In-Reply-To:References:Message-ID:Date:Subject:CC:To:From:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=9qBLrX4OJMi6nva5Gagm1lxJejQ6UsnDhO4vKo0e0BQ=; b=V0vipPTOZRFBvN93LmeJLsn13A o0Jo5HT/ESTTCKFrd76dtw7Qt2RBklkzW8Q0p5attx9px9Xo6+/8UBd9EMnzhVljRUymVpUIuFuxs e1RcTavW1KXgo7lsbLGnViBXF7Li6WC+3AXis/JmiedawMVkQmvamNb1FFCzxnQFbL4ixEs1n+cCc cNVYEqogt+gWB7JloGL7qA95NNRB7ovOSSh+oLwOac+1kTUiIvA3Zl0N/hLIzsf7iCQ2FOAFs64yu IE/nQrC3G1ZkYQulEDJj3O3odj9UIRAkppag8RZA0SrcFWeg56e1vbpRt/LK6yPp/Mnjt8+nTQDV5 8FrKpkDw==; Received: from esa5.microchip.iphmx.com ([216.71.150.166]) by casper.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gKi9k-0004A7-Q2 for linux-mtd@lists.infradead.org; Thu, 08 Nov 2018 11:07:26 +0000 X-IronPort-AV: E=Sophos;i="5.54,478,1534834800"; d="scan'208";a="20844180" Received: from smtpout.microchip.com (HELO email.microchip.com) ([198.175.253.82]) by esa5.microchip.iphmx.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 08 Nov 2018 04:07:13 -0700 Received: from NAM01-BN3-obe.outbound.protection.outlook.com (10.10.215.89) by email.microchip.com (10.10.76.106) with Microsoft SMTP Server (TLS) id 14.3.352.0; Thu, 8 Nov 2018 04:07:11 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microchiptechnology.onmicrosoft.com; s=selector1-microchiptechnology-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9qBLrX4OJMi6nva5Gagm1lxJejQ6UsnDhO4vKo0e0BQ=; b=qzQObTVD1W9Ljhv16wX4kRUCrLktsBi0jOj+pxMmMroapMhGZ0YSQWNtW0mNyilSyrjqbVyHh3NgJuTMaKoNvfTOqrUmrA7kP2vDx0L8h7+MxIOGK0QapRV6i4/T4U+ZQLrw01QdprtegGRKfj2Hbz1BE3T+C57jYA3ylYIbuXY= Received: from BN6PR11MB1842.namprd11.prod.outlook.com (10.175.99.146) by BN6PR11MB1987.namprd11.prod.outlook.com (10.173.32.22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1294.28; Thu, 8 Nov 2018 11:07:09 +0000 Received: from BN6PR11MB1842.namprd11.prod.outlook.com ([fe80::11b7:21db:803a:7cfa]) by BN6PR11MB1842.namprd11.prod.outlook.com ([fe80::11b7:21db:803a:7cfa%5]) with mapi id 15.20.1294.034; Thu, 8 Nov 2018 11:07:09 +0000 From: To: , , , , Subject: [PATCH 2/7] mtd: spi-nor: fix iteration over smpt array Thread-Topic: [PATCH 2/7] mtd: spi-nor: fix iteration over smpt array Thread-Index: AQHUd1MxxRyOdK7pfkW8L7t/3gBulQ== Date: Thu, 8 Nov 2018 11:07:09 +0000 Message-ID: <20181108110653.21063-3-tudor.ambarus@microchip.com> References: <20181108110653.21063-1-tudor.ambarus@microchip.com> In-Reply-To: <20181108110653.21063-1-tudor.ambarus@microchip.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: VI1PR0401CA0009.eurprd04.prod.outlook.com (2603:10a6:800:4a::19) To BN6PR11MB1842.namprd11.prod.outlook.com (2603:10b6:404:103::18) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Tudor.Ambarus@microchip.com; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [94.177.32.154] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; BN6PR11MB1987; 6:zMy041KOomJEannMvClW2UR4Rrw3iHPakqhtCih1Yrfk6uiFG6pyanRklH9tmcAg/RKc/AkK9aKwkiEEkDUgtuWxw0ACnP8s4NYcZJBAjv2dSPJXXIHn182sJb0Lje5FmzQ1Vg5LGAGAyxJffOQtCMe62U4+NVMst7D7jV166gSAQzjgbEoYlJrNGIgITqLF0u72Swsi1PveLhSA4wrZkbgV59z8VxFR2nXhFuDs4gFWbxc0u69+6CoXZ/U6RfxjLFnrdNWAV44gR9PIlXZAyba4i2pRTs8MqAOqbcXV7mXsZcSWzGs15lAJsM0DFYOkWvq2dPpiNqX9d5sL2blJd//ubEVmRIrZo1aOSnehOMfRrNIVL4rdcMfLxG/YJmemarDVlXnk1XrjreD6fFW1uG9GwlApFr5/ahRQJHF+WXqcMTWM2oP/5/HHrySCKZ2KkJglKhvZjOZDbiAbl+cYNg==; 5:hmyBh1CRgp75gy0qByq+ltHKUBUtVtU3pj7Ry8hfZ5szeKv/BxjakH7TWPVviPitbaccB65qyphUUtEbaJoKI1CkQk3Uvox9+nKg+jcTXrLF79sjdRS6fGQVAEROE12EOrMyhe+gIRKehMO76D4BEeSScF0BcR0EuFl0BeQdOX0=; 7:47B05BLNbcAqwm53Qih/2SqrCLuDgRTe18ttYhcJl0dt4bvBDeOk6ZV/4gXyim4MR4PLQJ8GxWxb9EE0N95By+mzAHHZ/ry90c4q8zaepcwagbKqSM3OBmixdlUxUrpsBoxoUcxck5hfu4fIAtwDOw== x-ms-office365-filtering-correlation-id: 086cfef4-65c7-4e59-9192-08d6456a539a x-microsoft-antispam: BCL:0; PCL:0; RULEID:(7020095)(4652040)(8989299)(5600074)(711020)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7153060)(7193020); SRVR:BN6PR11MB1987; x-ms-traffictypediagnostic: BN6PR11MB1987: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:; x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(6040522)(2401047)(8121501046)(5005006)(3231382)(944501410)(52105095)(3002001)(10201501046)(93006095)(93001095)(148016)(149066)(150057)(6041310)(20161123558120)(20161123564045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(201708071742011)(7699051)(76991095); SRVR:BN6PR11MB1987; BCL:0; PCL:0; RULEID:; SRVR:BN6PR11MB1987; x-forefront-prvs: 0850800A29 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(136003)(39860400002)(346002)(396003)(366004)(376002)(189003)(199004)(72206003)(8676002)(81166006)(5660300001)(81156014)(102836004)(14454004)(68736007)(305945005)(26005)(86362001)(1076002)(2201001)(39060400002)(7736002)(8936002)(2906002)(2900100001)(36756003)(3846002)(6116002)(71200400001)(71190400001)(107886003)(97736004)(6436002)(6486002)(110136005)(54906003)(2501003)(2616005)(11346002)(446003)(99286004)(53936002)(6512007)(256004)(4326008)(478600001)(66066001)(52116002)(386003)(6506007)(186003)(25786009)(316002)(105586002)(76176011)(476003)(486006)(106356001); DIR:OUT; SFP:1101; SCL:1; SRVR:BN6PR11MB1987; H:BN6PR11MB1842.namprd11.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; received-spf: None (protection.outlook.com: microchip.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: Qp8/6CV1ExjtUksJ8G6GCgw4nkA2Fz3R8PBYXL74duF3md650GUGl1bByuQsnJ7itPp6CUYS6WdbiMNUbpNBsNmh+NMtVaetQHAeHIBtlLe8lQTniFHLjgTw1ovVztr3OzrN8s63UrOp4hPSJbMxrpiNJcjgVIbLo+mvhdAw5kCUxQj23wynCmu4e0Jsm+A4ykFcttRUtiVqulroZW/AOJEy8DuqfsTS0RZZyZg6Y90aXrJpVlX6AAYs5wIwaW+6HC4jK1ocCvJnPaqxgkOMON/+uR8DwNo2+8fF66tSJWZErDGelhZ7CKB6aZldGhEwlpptq+i2EWRRsblL4U+GsvALLFAYKx5lrRMi9uBVtrE= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: 086cfef4-65c7-4e59-9192-08d6456a539a X-MS-Exchange-CrossTenant-originalarrivaltime: 08 Nov 2018 11:07:09.4055 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 3f4057f3-b418-4d4e-ba84-d55b4e897d88 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR11MB1987 X-OriginatorOrg: microchip.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181108_110725_118658_02C0FF41 X-CRM114-Status: GOOD ( 19.71 ) X-Spam-Score: -0.8 (/) X-Spam-Report: SpamAssassin version 3.4.1 on casper.infradead.org summary: Content analysis details: (-0.8 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [216.71.150.166 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIMWL_WL_MED DKIMwl.org - Whitelisted Medium sender X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yogeshnarayan.gaur@nxp.com, linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, Tudor.Ambarus@microchip.com, cyrille.pitchen@wedev4u.fr Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Iterate over smpt array using its starting address and length instead of the blindly iterations that used data found in the array. This prevents possible memory accesses outside of the smpt array boundaries in case software, or manufacturers, misrepresent smpt array fields. Suggested-by: Boris Brezillon Signed-off-by: Tudor Ambarus --- drivers/mtd/spi-nor/spi-nor.c | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c index 2cdf96013689..59dcedb08691 100644 --- a/drivers/mtd/spi-nor/spi-nor.c +++ b/drivers/mtd/spi-nor/spi-nor.c @@ -2860,12 +2860,15 @@ static u8 spi_nor_smpt_read_dummy(const struct spi_nor *nor, const u32 settings) * spi_nor_get_map_in_use() - get the configuration map in use * @nor: pointer to a 'struct spi_nor' * @smpt: pointer to the sector map parameter table + * @smpt_len: sector map parameter table length */ -static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt) +static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt, + u8 smpt_len) { const u32 *ret = NULL; - u32 i, addr; + u32 addr; int err; + u8 i; u8 addr_width, read_opcode, read_dummy; u8 read_data_mask, data_byte, map_id; @@ -2874,9 +2877,10 @@ static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt) read_opcode = nor->read_opcode; map_id = 0; - i = 0; /* Determine if there are any optional Detection Command Descriptors */ - while (!(smpt[i] & SMPT_DESC_TYPE_MAP)) { + for (i = 0; i < smpt_len; i += 2) { + if (smpt[i] & SMPT_DESC_TYPE_MAP) + break; read_data_mask = SMPT_CMD_READ_DATA(smpt[i]); nor->addr_width = spi_nor_smpt_addr_width(nor, smpt[i]); nor->read_dummy = spi_nor_smpt_read_dummy(nor, smpt[i]); @@ -2892,18 +2896,33 @@ static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt) * Configuration that is currently in use. */ map_id = map_id << 1 | !!(data_byte & read_data_mask); - i = i + 2; } - /* Find the matching configuration map */ - while (SMPT_MAP_ID(smpt[i]) != map_id) { + /* + * If command descriptors are provided, they always precede map + * descriptors in the table. There is no need to start the iteration + * over smpt array all over again. + * + * Find the matching configuration map. + */ + while (i < smpt_len) { + if (SMPT_MAP_ID(smpt[i]) == map_id) { + ret = smpt + i; + break; + } + + /* + * If there are no more configuration map descriptors and no + * configuration ID matched the configuration identifier, the + * sector address map is unknown. + */ if (smpt[i] & SMPT_DESC_END) - goto out; + break; + /* increment the table index to the next map */ i += SMPT_MAP_REGION_COUNT(smpt[i]) + 1; } - ret = smpt + i; /* fall through */ out: nor->addr_width = addr_width; @@ -3025,7 +3044,7 @@ static int spi_nor_parse_smpt(struct spi_nor *nor, for (i = 0; i < smpt_header->length; i++) smpt[i] = le32_to_cpu(smpt[i]); - sector_map = spi_nor_get_map_in_use(nor, smpt); + sector_map = spi_nor_get_map_in_use(nor, smpt, smpt_header->length); if (!sector_map) { ret = -EINVAL; goto out;