From: Lorenzo Bianconi <lorenzo@kernel.org>
To: Lars-Peter Clausen <lars@metafoo.de>
Cc: Teng Qi <starmiku1207184332@gmail.com>,
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 <oslab@tsinghua.edu.cn>
Subject: Re: [PATCH] iio: imu: st_lsm6dsx: Fix an array overflow in st_lsm6dsx_set_odr()
Date: Mon, 11 Oct 2021 11:47:24 +0200 [thread overview]
Message-ID: <YWQILPRHZw2BsAYu@lore-desk> (raw)
In-Reply-To: <8efeaffd-ae9a-d0ef-0222-6c94c0e31e7e@metafoo.de>
[-- Attachment #1: Type: text/plain, Size: 2711 bytes --]
> 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) {
> >
> > If ref_sensor->id is ST_LSM6DSX_ID_MAX, an array overflow will ocurrs in
> > function st_lsm6dsx_check_odr():
> > odr_table = &sensor->hw->settings->odr_table[sensor->id];
> >
> > and in function st_lsm6dsx_set_odr():
> > reg = &hw->settings->odr_table[ref_sensor->id].reg;
> >
> > 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.
> >
> > Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
> > Signed-off-by: Teng Qi <starmiku1207184332@gmail.com>
>
> Hi,
>
> 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.
>
> 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 value
> itself is never assigned to any variable of that type. This is a very common
> pattern. So strictly speaking there is no bug, since the out-of-range value
> of ST_LSM6DSX_ID_MAX is never used.
>
> 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. This
> makes sure that the code stays correct when the array size changes.
>
> If you really wanted to modify the code sot that your code checker does not
> detect a false positive I'd modify the switch statement to explicitly handle
> 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
>
> - Lars
>
> > ---
> > drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 4 ++++
> > 1 file changed, 4 insertions(+)
> >
> > 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 *sensor, u32 req_odr)
> > break;
> > }
> > + if (ref_sensor->id >= 2) {
> > + return -EINVAL;
> > + }
> > +
> > if (req_odr > 0) {
> > err = st_lsm6dsx_check_odr(ref_sensor, req_odr, &val);
> > if (err < 0)
>
>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
prev parent reply other threads:[~2021-10-11 9:47 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-10 9:12 [PATCH] iio: imu: st_lsm6dsx: Fix an array overflow in st_lsm6dsx_set_odr() Teng Qi
2021-10-10 10:03 ` Lars-Peter Clausen
2021-10-11 9:47 ` Lorenzo Bianconi [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YWQILPRHZw2BsAYu@lore-desk \
--to=lorenzo@kernel.org \
--cc=baijiaju1990@gmail.com \
--cc=islituo@gmail.com \
--cc=jic23@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lorenzo.bianconi83@gmail.com \
--cc=oslab@tsinghua.edu.cn \
--cc=starmiku1207184332@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox