Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6742603rdb; Tue, 2 Jan 2024 11:42:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IE+8DfHPfuwJD4EYFkiIASjEghzNuCcBbgLsyISpxhHwKxB6pGe/crVswnJOnV11AQhly/w X-Received: by 2002:a17:906:9b54:b0:a25:c3c1:5578 with SMTP id ep20-20020a1709069b5400b00a25c3c15578mr5493649ejc.71.1704224548774; Tue, 02 Jan 2024 11:42:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704224548; cv=none; d=google.com; s=arc-20160816; b=fUkUnIyGtknAYOMfhUsawM1c3CtTdEuyM4wwVf36hEgt8lbofk1QmoS2KwOCyCAR7h tEkV9xLWTOXiHdg1YjdDtDXj/ZWvGns73VsSQKqEWWfHKzZ90ws6AvtA6a5IX184uKmK g6EJvaFpSDbomPvoycLaW8PRu5TzSHm4St4zNXAFxRHsZ//0/6ofe5MONkY78EDGqoMf e1CGnmSwl6TnAaAThsGjvAOohNN3YNaTn3ZaV6sapmR8dqaazJuGjylq0/s0XfoqTHXI 95JDTKSOfqa5MpV+NvtlVoZdNkUuNj5FJ6HeBY5MOmaY7wsNuEJtsSBYNR73Xn+WJcIw 6g1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=aw0hYFoqi4XfWHjMiN9px6u3ThxQAxq3DVeNMmc541s=; fh=if3UhIgSfvcnF40BmqtsR1plXFLPrRKZa41EYMpoivo=; b=XbalLDv0yLzqUr3FhzR0CNcPTiI9mOOvgKrRUcQBj9O5Zjfz1wrSo/RxbYMyhMJXHj cGURaS3muPk8esUcq8tc9s93wkLftxWcW8tYefMl4ME2wImxdN3AE98JRUZgt6dDOyQX bY+NydfR6UvZVbPSxelYUCYBAIqZZFjURLlUawiq/eOO7sdCklF7lzPvLsO9zeXCNn8b qivt3Kr6tU5Ou/SQYb/rg4Z8hhokwN/TXQqbaWwROwyg2cHjMrdLRZeOmXNO7VaA10dI VTt8Rb3by7TfihBfM2Jr6XuLsguE5awF6hGS5Y8ZYXCqJnB9JVQvQHwth0b0ZJzjxJ9/ b4Uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=C9HQ7G7B; spf=pass (google.com: domain of linux-kernel+bounces-14781-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14781-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id p23-20020a170907911700b00a2695e24f6csi10298385ejq.761.2024.01.02.11.42.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 11:42:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14781-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=C9HQ7G7B; spf=pass (google.com: domain of linux-kernel+bounces-14781-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14781-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 7AAF51F22EDB for ; Tue, 2 Jan 2024 19:42:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9834C15EBD; Tue, 2 Jan 2024 19:42:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="C9HQ7G7B" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C34B315E96; Tue, 2 Jan 2024 19:42:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8A7BAC433C7; Tue, 2 Jan 2024 19:42:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1704224537; bh=PaPzSK5xpA5yNPUsXf1i+zSw0U93BBhc+sQEbo5cMqI=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=C9HQ7G7BFl5okQ2vuavV72FAxqlAyD0LjS6KZzt1xIePGgLnu4Zf1Bmci2e5M10JB O664QYQcItyQjciCvh8Rua30UZNjlQhcBP9POS5Ez0MxapzHYC/dqxHhQBwoNieW+k sOGyMUcuSGPKngPa826J1SRROqGJgLOLfsi7PtTqGw5HcuPTX386vSsIVlaqTtQ23y YzQR9Q/QvAeKttcBxB4dCHR5+so/C+v+tbUr21WOeuyYt+ECxs1cFSBVeuNQEICzqx yGKa0EBqF098KAGYmb29a2c/y5Yiizb4E///ml5bu1MA1f3TyK2O08448Rt8FDHBTJ TWulcoLNQfktw== Date: Tue, 2 Jan 2024 19:42:09 +0000 From: Jonathan Cameron To: zhouzhouyi@gmail.com Cc: songqiang1304521@gmail.com, lars@metafoo.de, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, "zhili.liu" Subject: Re: [PATCH v3] iio: magnetometer: rm3100: add boundary check for the value read from RM3100_REG_TMRC Message-ID: <20240102194209.39dac963@jic23-huawei> In-Reply-To: <1704157631-3814-1-git-send-email-zhouzhouyi@gmail.com> References: <1704157631-3814-1-git-send-email-zhouzhouyi@gmail.com> X-Mailer: Claws Mail 4.2.0 (GTK 3.24.38; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Tue, 2 Jan 2024 09:07:11 +0800 zhouzhouyi@gmail.com wrote: > From: "zhili.liu" > > Recently, we encounter kernel crash in function rm3100_common_probe > caused by out of bound access of array rm3100_samp_rates (because of > underlying hardware failures). Add boundary check to prevent out of > bound access. > > Fixes: 121354b2eceb ("iio: magnetometer: Add driver support for PNI RM3100") Fixes is a formal tag so needs to be part of the main tags block (i.e. No blank line here!) I'll fix that whilst applying. > > Suggested-by: Zhouyi Zhou > Signed-off-by: zhili.liu > --- > drivers/iio/magnetometer/rm3100-core.c | 9 ++++++++- > 1 file changed, 8 insertions(+), 1 deletion(-) > > diff --git a/drivers/iio/magnetometer/rm3100-core.c b/drivers/iio/magnetometer/rm3100-core.c > index 69938204456f..12c2e3b0aeb6 100644 > --- a/drivers/iio/magnetometer/rm3100-core.c > +++ b/drivers/iio/magnetometer/rm3100-core.c > @@ -530,6 +530,7 @@ int rm3100_common_probe(struct device *dev, struct regmap *regmap, int irq) > struct rm3100_data *data; > unsigned int tmp; > int ret; > + int samp_rate_index; > > indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); > if (!indio_dev) > @@ -586,8 +587,14 @@ int rm3100_common_probe(struct device *dev, struct regmap *regmap, int irq) > ret = regmap_read(regmap, RM3100_REG_TMRC, &tmp); > if (ret < 0) > return ret; > + > + samp_rate_index = tmp - RM3100_TMRC_OFFSET; > + if (samp_rate_index < 0 || samp_rate_index >= RM3100_SAMP_NUM) { > + dev_err(dev, "The value read from RM3100_REG_TMRC is invalid!\n"); > + return -EINVAL; > + } > /* Initializing max wait time, which is double conversion time. */ > - data->conversion_time = rm3100_samp_rates[tmp - RM3100_TMRC_OFFSET][2] > + data->conversion_time = rm3100_samp_rates[samp_rate_index][2] > * 2; I've rewrapped this to be on one line whilst applying. Make sure to check for side effects like this when updating code. If we don't tidy it up at the time we end up with gradually worse formatting over a long period! Applied to the fixes-togreg branch of iio.git and marked for stable. Thanks, Jonathan > > /* Cycle count values may not be what we want. */