Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1457643pxb; Mon, 11 Oct 2021 06:29:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxJV7tAfgA5UjjN1HMCt3ZmidY5VRcQHwsHfGS6tYa/GdawYOZNNhN5FrwKu9JmdZHBeib9 X-Received: by 2002:a17:906:f9c8:: with SMTP id lj8mr906775ejb.347.1633958993244; Mon, 11 Oct 2021 06:29:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633958993; cv=none; d=google.com; s=arc-20160816; b=MHRlk6J5rC5y6jzhUtV96XnAPsMfbJUfjqGV0gO4g1ytJPbBL+n8EYFjLm+euLaNwu mmtRlLKDKoi/QEkxxSaFs7ei3+0hA8XVkvA+pJA/VxKQZw82trbSr47Bcn+iQS0if72w uKyM+A6OSvwWPzhHWNk61IOrlUDZQy+CLk+0E89dkh5OnjPFNlybLQg9hNaI1egAPlq1 /bqcpirC9lrn3MCJgiVH+/rUzVC2MEEeIU4wmaWjAoTjTBM6A4VMuMf/n8vFI1qiNCcI 7CByB5PANJfiuW3HgEQ/4m4zjokUDrrH/EbLx8I/zzaOgBwKEfoOC7bSRF2acmclIm4f jJNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ALpyYgiYloovCu3ijPjqSLGm6T9zkRiBQLjOTW3gJvA=; b=M4zLvcWGffj4sbwq/xiXwQcqnDGsEQyz2cwP6upinZvf2gr/BqiBrCN2Q9m1F0U+bh UHJn89Vhhy9DDVkutKYdxhHzJxYz7+MOkiG8sCYyPt0ZfT5FrR3VKT9tOscdXC8MiMXk nzm0krsKaE6aDuYG26JQWCC5nhOtgyciNo0HFsY9vpLA8E/eqUQ1slBtTVGQyWf/Osc6 ezT4vWp/aMqbIswW72H4qsKqjPvL9y7O3pRGcOJJ0wR8QsM61POFm7XkrVKiaFIQs9e1 Dc7F240Fz4GcwVksSiWZSr3fWtTzzSffgi+BbfJQeUw1Zxpev3jFIuG+2U/ySgVMd1Pz jqCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=coJ5KbQZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dj1si10481214edb.462.2021.10.11.06.29.27; Mon, 11 Oct 2021 06:29:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=coJ5KbQZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235590AbhJKJt2 (ORCPT + 99 others); Mon, 11 Oct 2021 05:49:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:43520 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235539AbhJKJt2 (ORCPT ); Mon, 11 Oct 2021 05:49:28 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B370260D07; Mon, 11 Oct 2021 09:47:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1633945648; bh=vB/8tYwO/Zfs45j4rAaUI7XHZeBWjc49gNuQjGutcoU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=coJ5KbQZlymBO0G8Sbc6a5iqQnWrSlhnuEkEUvudOmeny2EN0Y/KeRxdBULIqHoGz C9WqO/tQo9K+krtq4wexsUJWZU3HIl76U5tEb/AfQwUEe0TMHIRb1ZvoRTUOWFOxov 8pkMQ/pwhG2cA9JBkyJDfIY7RKM/JcXepvIr9LIOLmZMpeAIBP7H2sU55k/D1rrXb8 vnjC7LOj1lV1im+UCNGARZJ0j68QfL2G5f0ElsNQKtzEEGFE1goTagsQdKJRI1oB5B EI+15FZAiZBHbL90r69f400MVcuIuiba8icA+wpf8B4yb4kabo44hPpz0goAyjnL6z zLiXbUxQTcHvA== Date: Mon, 11 Oct 2021 11:47:24 +0200 From: Lorenzo Bianconi To: Lars-Peter Clausen Cc: Teng Qi , lorenzo.bianconi83@gmail.com, jic23@kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, islituo@gmail.com, baijiaju1990@gmail.com, TOTE Robot Subject: Re: [PATCH] iio: imu: st_lsm6dsx: Fix an array overflow in st_lsm6dsx_set_odr() Message-ID: References: <20211010091230.895549-1-starmiku1207184332@gmail.com> <8efeaffd-ae9a-d0ef-0222-6c94c0e31e7e@metafoo.de> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="gLTk5hY2TkhJnKDu" Content-Disposition: inline In-Reply-To: <8efeaffd-ae9a-d0ef-0222-6c94c0e31e7e@metafoo.de> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --gLTk5hY2TkhJnKDu Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable > On 10/10/21 11:12 AM, Teng Qi wrote: > > The length of hw->settings->odr_table is 2 and ref_sensor->id is an enum > > variable whose value is between 0 and 5. > > However, the value ST_LSM6DSX_ID_MAX (i.e. 5) is not catched properly in > > switch (sensor->id) { > >=20 > > If ref_sensor->id is ST_LSM6DSX_ID_MAX, an array overflow will ocurrs in > > function st_lsm6dsx_check_odr(): > > odr_table =3D &sensor->hw->settings->odr_table[sensor->id]; > >=20 > > and in function st_lsm6dsx_set_odr(): > > reg =3D &hw->settings->odr_table[ref_sensor->id].reg; > >=20 > > To fix this possible array overflow, ref_sensor->id should be checked > > first. If it is greater than or equal to 2, the function > > st_lsm6dsx_set_odr() returns -EINVAL. > >=20 > > Reported-by: TOTE Robot > > Signed-off-by: Teng Qi >=20 > Hi, >=20 > Thanks for the patch, good catch. But there are a few things to improve > here, the goal should not be to silence the output of your checker, but to > write good code. >=20 > Let's start with ST_LSM6DSX_ID_MAX. As the name suggests this is an entry= at > the end of the enum that is used to get the size of the enum. But its val= ue > itself is never assigned to any variable of that type. This is a very com= mon > pattern. So strictly speaking there is no bug, since the out-of-range val= ue > of ST_LSM6DSX_ID_MAX is never used. >=20 > The other thing is that we usually don't want to hardcode range checks for > array sizes with integer literals, but rather use ARRAY_SIZE() instead. T= his > makes sure that the code stays correct when the array size changes. >=20 > If you really wanted to modify the code sot that your code checker does n= ot > detect a false positive I'd modify the switch statement to explicitly han= dle > ST_LSM6DSX_ID_GYRO and ST_LSM6DSX_ID_ACCEL and the return an error for the > default case with a comment that default case should never occur. +1 :) Regards, Lorenzo >=20 > - Lars >=20 > > --- > > drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 4 ++++ > > 1 file changed, 4 insertions(+) > >=20 > > diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio= /imu/st_lsm6dsx/st_lsm6dsx_core.c > > index db45f1fc0b81..edf5d33dd256 100644 > > --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > > +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c > > @@ -1308,6 +1308,10 @@ st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sen= sor, u32 req_odr) > > break; > > } > > + if (ref_sensor->id >=3D 2) { > > + return -EINVAL; > > + } > > + > > if (req_odr > 0) { > > err =3D st_lsm6dsx_check_odr(ref_sensor, req_odr, &val); > > if (err < 0) >=20 >=20 --gLTk5hY2TkhJnKDu Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQTquNwa3Txd3rGGn7Y6cBh0uS2trAUCYWQILAAKCRA6cBh0uS2t rB6iAQCIGK44O0mznjUHIz1nfCz74dh/KY7G2nH0FupurFNatwD7BaTRDpzoNjdX oV4e2lPCW+wy7PU1cXyPRA7hWIz/3AQ= =Vtt3 -----END PGP SIGNATURE----- --gLTk5hY2TkhJnKDu--