From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Shengzhuo Wei <me@cherr.cc>
Cc: "Jonathan Cameron" <jic23@kernel.org>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
"Sean Nyekjaer" <sean@geanix.com>,
linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] iio: accel: fxls8962af: clamp FIFO sample count
Date: Sat, 8 Aug 2026 23:15:58 +0300 [thread overview]
Message-ID: <aneOfps4hcZAaohn@ashevche-desk.local> (raw)
In-Reply-To: <20260806-fxls8962af-fifo-v1-1-bd9d27047fee@cherr.cc>
On Thu, Aug 06, 2026 at 05:14:53AM +0800, Shengzhuo Wei wrote:
> fxls8962af_fifo_flush() copies the number of samples the device reports
> in its FIFO status register into an on-stack buffer
>
> u16 buffer[FXLS8962AF_FIFO_LENGTH * 3];
>
> which is sized for at most FXLS8962AF_FIFO_LENGTH (32) samples. The
> sample count is read from the BUF_STATUS register and only masked to its
> 6 valid bits:
>
> count = reg & FXLS8962AF_BUF_STATUS_BUF_CNT;
>
> so it can be 0..63, while the buffer holds 32. The only other limit,
> the watermark, is applied on the write path (fxls8962af_set_watermark)
> but not here on the read path. count samples are then transferred into
> buffer[]:
>
> fxls8962af_fifo_transfer(data, buffer, count);
>
> fxls8962af_fifo_transfer() reads count * 6 bytes through regmap, so a
> malfunctioning, malicious or counterfeit accelerometer (or an attacker
> tampering with the I2C/SPI bus) that reports up to 63 samples writes up
> to 378 bytes into the 192-byte buffer: a stack out-of-bounds write of up
> to 186 bytes that clobbers the stack canary, saved registers and the
> return address.
>
> Clamp count to FXLS8962AF_FIFO_LENGTH, the number of samples buffer[] is
> sized for, before the transfer, mirroring the watermark clamp already
> done in fxls8962af_set_watermark(). A well-formed flush reports at most
> FXLS8962AF_FIFO_LENGTH samples, so legitimate devices are unaffected.
...
> count = reg & FXLS8962AF_BUF_STATUS_BUF_CNT;
> + count = min_t(u8, count, FXLS8962AF_FIFO_LENGTH);
min_t(u8, ...) is almost always wrong. Please, find a better solution (even if
the current approach is correct).
Hint: first of all, do not use min_t() (mind '_t' part!).
> if (!count)
> return 0;
--
With Best Regards,
Andy Shevchenko
prev parent reply other threads:[~2026-08-08 20:16 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-05 21:14 [PATCH] iio: accel: fxls8962af: clamp FIFO sample count Shengzhuo Wei
2026-08-07 10:20 ` Joshua Crofts
2026-08-08 20:15 ` Andy Shevchenko [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=aneOfps4hcZAaohn@ashevche-desk.local \
--to=andriy.shevchenko@intel.com \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=me@cherr.cc \
--cc=nuno.sa@analog.com \
--cc=sean@geanix.com \
--cc=stable@vger.kernel.org \
/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