Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp48796imu; Thu, 8 Nov 2018 04:53:43 -0800 (PST) X-Google-Smtp-Source: AJdET5ctwyiLhxq7S+222hi8UAEaaxjAgSO4R2XAOq1gk7q9+RCSCxihd8aBocvLZth/EIh1Ltji X-Received: by 2002:a17:902:4624:: with SMTP id o33-v6mr4462147pld.285.1541681623270; Thu, 08 Nov 2018 04:53:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541681623; cv=none; d=google.com; s=arc-20160816; b=prkt9co7U0smYaH8l4S6dvub7Uh4TkAIAFme9eAtE+fi9l5FBeWxV+J5rNIj6z7507 UwTZ7wbpF/jRJ6LTu1NwihQWCLKEMzfZrPN06WhMkvX4GARC8+p2/Zdgp2HVBRm1gLBZ HY/D28djhIELJs+MC7BDDUcGQwGpw3aHQMFBeonBeh++qc3EmKqu51lXLHMkqVDIIa5E CUcWd9oWBkR/di+UBoZvul1rGwcPPj5gReGmoA9DULZWLORW1wN8XByB9R3lzSzIBYcN W1Mj6PUdp28elShtA/koUPvgET2RnEWwXimIfW1EZMvAG5xpRf3NQeof8YTnKz+gEWvm qksg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=gbwmj1iCnYcuOTkWJ5CgaPPqJgNYkB/KMZ6Hhy8xBS4=; b=j/Bzq7IoNjTsVkYYhNiWpt26A6RcOMJZODSDNYbl2tnvckUQtBRnE6ohnghP4auaKg BAjRykEJ8xuMGrqsJ1QQalxpffTeqbtMOreqHC/hRDcJ1Fv1LTVSkNcUI6s9yTMPYzdF Ux+VYhEey5MaO+OHilpNOBfKbkoVO1+oHLEeQvAXsXrTrGa3UpCgslppMNkLvIw8uUyl 0Jq6AKod3sUHD9M8RbrWtFUkN5/1/pGC3UirfHhf6NabCx7B8Fmm+NGmRDuM/MhbXxHz 0Q6vfRywV4wfUg6RXyfCNtpB6/uH39KoGCfFSfeMWV1pe7G+YAPfI1yDBvHfnLnel7KE YciA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q201-v6si5430231pfq.14.2018.11.08.04.53.27; Thu, 08 Nov 2018 04:53:43 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727239AbeKHW0C (ORCPT + 99 others); Thu, 8 Nov 2018 17:26:02 -0500 Received: from mail.bootlin.com ([62.4.15.54]:36827 "EHLO mail.bootlin.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726541AbeKHW0C (ORCPT ); Thu, 8 Nov 2018 17:26:02 -0500 Received: by mail.bootlin.com (Postfix, from userid 110) id 0B46F207B0; Thu, 8 Nov 2018 13:50:39 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.bootlin.com X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,SHORTCIRCUIT, URIBL_BLOCKED shortcircuit=ham autolearn=disabled version=3.4.2 Received: from bbrezillon (aaubervilliers-681-1-30-49.w90-88.abo.wanadoo.fr [90.88.15.49]) by mail.bootlin.com (Postfix) with ESMTPSA id 8F51B206D8; Thu, 8 Nov 2018 13:50:38 +0100 (CET) Date: Thu, 8 Nov 2018 13:50:38 +0100 From: Boris Brezillon To: Cc: , , , , , , , Subject: Re: [PATCH 2/7] mtd: spi-nor: fix iteration over smpt array Message-ID: <20181108135038.3fb9e995@bbrezillon> In-Reply-To: <20181108110653.21063-3-tudor.ambarus@microchip.com> References: <20181108110653.21063-1-tudor.ambarus@microchip.com> <20181108110653.21063-3-tudor.ambarus@microchip.com> X-Mailer: Claws Mail 3.16.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 8 Nov 2018 11:07:09 +0000 wrote: > Iterate over smpt array using its starting address and length > instead of the blindly iterations that used data found in the array. ^blind > > 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 I think we should consider this patch as a fix. Would you mind adding a Fixes tag? > --- > 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; nit: add a blank line here. > 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;