[PATCH 02/10] iio: accel: adxl313: Prevent FIFO buffer overflow in adxl313_fifo_transfer()
From: Lee Jones
Date: Thu Nov 19 2026 - 10:33:03 EST
The ADXL313 32-entry FIFO plus one sample held in the output filter can
yield up to 33 3-axis samples (ADXL313_FIFO_SIZE + 1). Because of
missing parentheses in ADXL313_NUM_AXIS * ADXL313_FIFO_SIZE + 1,
fifo_buf is sized at 97 16-bit elements instead of 99, overflowing the
buffer by four bytes whenever all 33 entries are read.
In addition, adxl313_get_samples() returns the 6-bit FIFO_STATUS entries
field (0..63) without bounds checking, allowing a malfunctioning or
spoofed bus device to overflow fifo_buf on the heap in
adxl313_fifo_transfer().
Parenthesise (ADXL313_FIFO_SIZE + 1) when sizing fifo_buf and reject
entry counts exceeding ADXL313_FIFO_SIZE + 1 in adxl313_get_samples().
Fixes: ff8093fa6ba4 ("iio: accel: adxl313: add buffered FIFO watermark with interrupt handling")
Closes: https://sashiko.dev/#/bug/linux-6baeb7fe-ad68-4961-b5f9-5f0c7628df1e
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/iio/accel/adxl313.h | 2 +-
drivers/iio/accel/adxl313_core.c | 7 ++++++-
2 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/accel/adxl313.h b/drivers/iio/accel/adxl313.h
index 75ef54b60f75..de0bccfcdd0a 100644
--- a/drivers/iio/accel/adxl313.h
+++ b/drivers/iio/accel/adxl313.h
@@ -93,7 +93,7 @@ struct adxl313_data {
struct mutex lock; /* lock to protect transf_buf */
u8 watermark;
__le16 transf_buf __aligned(IIO_DMA_MINALIGN);
- __le16 fifo_buf[ADXL313_NUM_AXIS * ADXL313_FIFO_SIZE + 1];
+ __le16 fifo_buf[ADXL313_NUM_AXIS * (ADXL313_FIFO_SIZE + 1)];
};
struct adxl313_chip_info {
diff --git a/drivers/iio/accel/adxl313_core.c b/drivers/iio/accel/adxl313_core.c
index 6dc918c4ae17..72ef8652d398 100644
--- a/drivers/iio/accel/adxl313_core.c
+++ b/drivers/iio/accel/adxl313_core.c
@@ -921,7 +921,12 @@ static int adxl313_get_samples(struct adxl313_data *data)
if (ret)
return ret;
- return FIELD_GET(ADXL313_REG_FIFO_STATUS_ENTRIES_MSK, regval);
+ /* 32 FIFO entries plus 1 entry at the output filter */
+ ret = FIELD_GET(ADXL313_REG_FIFO_STATUS_ENTRIES_MSK, regval);
+ if (ret > ADXL313_FIFO_SIZE + 1)
+ return -EINVAL;
+
+ return ret;
}
static int adxl313_fifo_transfer(struct adxl313_data *data, int samples)
--
2.56.1.708.g77570a6a1d-goog