Re: [PATCH 04/10] i2c: omap: Check buf_len before buffer accesses in data transfer helpers
From: Andreas Kemnade
Date: Thu Dec 03 2026 - 17:02:33 EST
On Thu, 3 Dec 2026 18:31:08 +0000
Lee Jones <lee@xxxxxxxxxx> wrote:
> 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.
>
The 16bit argument seems valid. But I do not get why
FIFO count could exceed buf_len.
I would expect the line
omap->threshold = clamp(size, (u8) 1, omap->fifo_size);
to guard against that. At least that seems to be the idea behind
the offending patch, which removes the checks.
If you can easily find any clear reasoning, it might
be nice to have that in the commit message.
But besides of that:
Reviewed-by: Andreas Kemnade <andreas@xxxxxxxxxxxx>
> Guard all omap->buf accesses and omap->buf_len decrements against a
> zero remaining length, matching omap_i2c_omap1_isr().
>
Yes, better err on the side of caution here. At least it is not totally obvious
that these checks are not needed.
> 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) {
BTW: Dead code in next, but not in stable, so your fix is of
course needed. (of course taking the brush now is not needed)
> *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) {
Same here.
> w |= *omap->buf++ << 8;
> omap->buf_len--;
> }