[PATCH] i2c: i801: clear the byte-by-byte buffer pointer before IRQ sync
From: Zou Jianqiang
Date: Fri Nov 13 2026 - 10:24:36 EST
From: Jianqiang Zou <jq.zou@xxxxxxxxxxx>
The interrupt-driven byte-by-byte transfer stores a pointer to the
caller's buffer in priv->data. After a transfer times out, a late
BYTE_DONE interrupt can still access the buffer after the caller
returns. This can cause a KASAN stack-out-of-bounds report in
i801_isr_byte_done().
Clear priv->data before synchronizing the shared IRQ. Have the
handler snapshot the pointer once and skip byte handling if it is
NULL. This lets any handler that already acquired the pointer
finish before the caller can release the buffer.
Fixes: d3ff6ce40031 ("i2c-i801: Enable IRQ for byte_by_byte transactions")
Signed-off-by: Jianqiang Zou <jq.zou@xxxxxxxxxxx>
---
drivers/i2c/busses/i2c-i801.c | 41 +++++++++++++++++++++++++++++++----
1 file changed, 37 insertions(+), 4 deletions(-)
diff --git a/drivers/i2c/busses/i2c-i801.c b/drivers/i2c/busses/i2c-i801.c
index b29c99ed3..9d1a95a18 100644
--- a/drivers/i2c/busses/i2c-i801.c
+++ b/drivers/i2c/busses/i2c-i801.c
@@ -562,6 +562,17 @@ static int i801_block_transaction_by_block(struct i801_priv *priv,
static void i801_isr_byte_done(struct i801_priv *priv)
{
+ u8 *data = READ_ONCE(priv->data);
+
+ /*
+ * The transaction may have finished and cleared priv->data
+ * while a BYTE_DONE interrupt was still pending. Bail out
+ * instead of writing through a pointer into the caller's
+ * stack buffer, which is gone by then.
+ */
+ if (unlikely(!data))
+ return;
+
if (priv->is_read) {
/*
* At transfer start i801_smbus_block_transaction() marks
@@ -574,12 +585,12 @@ static void i801_isr_byte_done(struct i801_priv *priv)
/* FIXME: Recover */
priv->len = I2C_SMBUS_BLOCK_MAX;
- priv->data[-1] = priv->len;
+ data[-1] = priv->len;
}
/* Read next byte */
if (priv->count < priv->len)
- priv->data[priv->count++] = ioread8(SMBBLKDAT(priv));
+ data[priv->count++] = ioread8(SMBBLKDAT(priv));
else
pci_dbg(priv->pci_dev, "Discarding extra byte on block read\n");
@@ -589,7 +600,7 @@ static void i801_isr_byte_done(struct i801_priv *priv)
SMBHSTCNT(priv));
} else if (priv->count < priv->len - 1) {
/* Write next byte, except for IRQ after last byte */
- iowrite8(priv->data[++priv->count], SMBBLKDAT(priv));
+ iowrite8(data[++priv->count], SMBBLKDAT(priv));
}
}
@@ -702,7 +713,7 @@ static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
priv->cmd = smbcmd | SMBHSTCNT_INTREN;
priv->len = len;
priv->count = 0;
- priv->data = &data->block[1];
+ WRITE_ONCE(priv->data, &data->block[1]);
reinit_completion(&priv->done);
iowrite8(priv->cmd | SMBHSTCNT_START, SMBHSTCNT(priv));
@@ -938,6 +949,28 @@ static s32 i801_access(struct i2c_adapter *adap, u16 addr,
iowrite8(SMBHSTSTS_INUSE_STS | STATUS_FLAGS, SMBHSTSTS(priv));
out:
+ /*
+ * A byte-by-byte transaction points priv->data into the
+ * caller's union i2c_smbus_data, which lives on the stack of
+ * i801_access()'s caller. On a timeout i801_check_post()
+ * writes SMBHSTCNT_KILL and then writes 0 to SMBHSTCNT,
+ * which drops SMBHSTCNT_INTREN and stops the controller from
+ * raising further BYTE_DONE interrupts. That does not wait
+ * for a handler already in flight, and it does not
+ * invalidate priv->data for a spurious interrupt arriving
+ * after this function returns.
+ *
+ * Clear the pointer before waiting for in-flight handlers.
+ * A new handler on the shared IRQ can run after the wait,
+ * so it must already see NULL. A handler that took a
+ * snapshot before the clear must finish before the caller
+ * can release the buffer. This also covers successful
+ * transfers.
+ */
+ if ((priv->features & FEATURE_IRQ) && READ_ONCE(priv->data)) {
+ WRITE_ONCE(priv->data, NULL);
+ synchronize_irq(priv->pci_dev->irq);
+ }
pm_runtime_put_autosuspend(&priv->pci_dev->dev);
mutex_unlock(&priv->acpi_lock);
return ret;
--
2.43.0