[PATCH 04/10] i2c: omap: Check buf_len before buffer accesses in data transfer helpers

From: Lee Jones

Date: Thu Dec 03 2026 - 13:33:00 EST


omap_i2c_receive_data() and omap_i2c_transmit_data() dereference
omap->buf without checking omap->buf_len. On controllers with
OMAP_I2C_FLAG_16BIT_DATA_REG, any odd-length message reads or writes
one byte past msg->buf and underflows omap->buf_len; FIFO counts
exceeding omap->buf_len also overflow the message buffer.

Guard all omap->buf accesses and omap->buf_len decrements against a
zero remaining length, matching omap_i2c_omap1_isr().

Fixes: dd74548ddece ("i2c: omap: resize fifos before each message")
Closes: https://sashiko.dev/#/bug/linux-288ca65a-55c7-4651-8d27-2629e158106e
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/i2c/busses/i2c-omap.c | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/drivers/i2c/busses/i2c-omap.c b/drivers/i2c/busses/i2c-omap.c
index 6a577db3cb64..4684e0d64cff 100644
--- a/drivers/i2c/busses/i2c-omap.c
+++ b/drivers/i2c/busses/i2c-omap.c
@@ -1003,14 +1003,17 @@ static void omap_i2c_receive_data(struct omap_i2c_dev *omap, u8 num_bytes,

while (num_bytes--) {
w = omap_i2c_read_reg(omap, OMAP_I2C_DATA_REG);
- *omap->buf++ = w;
- omap->buf_len--;
+ if (omap->buf_len) {
+ *omap->buf++ = w;
+ omap->buf_len--;
+ }

/*
* Data reg in 2430, omap3 and
* omap4 is 8 bit wide
*/
- if (omap->flags & OMAP_I2C_FLAG_16BIT_DATA_REG) {
+ if ((omap->flags & OMAP_I2C_FLAG_16BIT_DATA_REG) &&
+ omap->buf_len) {
*omap->buf++ = w >> 8;
omap->buf_len--;
}
@@ -1023,6 +1026,8 @@ static int omap_i2c_transmit_data(struct omap_i2c_dev *omap, u8 num_bytes,
u16 w;

while (num_bytes--) {
+ if (!omap->buf_len)
+ break;
w = *omap->buf++;
omap->buf_len--;

@@ -1030,7 +1035,8 @@ static int omap_i2c_transmit_data(struct omap_i2c_dev *omap, u8 num_bytes,
* Data reg in 2430, omap3 and
* omap4 is 8 bit wide
*/
- if (omap->flags & OMAP_I2C_FLAG_16BIT_DATA_REG) {
+ if ((omap->flags & OMAP_I2C_FLAG_16BIT_DATA_REG) &&
+ omap->buf_len) {
w |= *omap->buf++ << 8;
omap->buf_len--;
}
--
2.56.1.911.g883d8bcb20-goog