[PATCH 09/10] i2c: mlxbf: Reject zero-length SMBus reads to prevent stack overflow

From: Lee Jones

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


For I2C_SMBUS_QUICK with read set, mlxbf_i2c_smbus_quick_command()
sets operation->length to 0 and leaves operation->buffer NULL while
setting MLXBF_I2C_F_READ. In mlxbf_i2c_smbus_start_transaction(),
subtracting 1 from the 8-bit length underflows read_len to 255,
overflowing the 128-byte data_desc stack buffer and dereferencing NULL
in memcpy().

Reject I2C_SMBUS_QUICK reads with -EOPNOTSUPP in mlxbf_i2c_smbus_xfer()
and validate operation->length and operation->buffer in
mlxbf_i2c_smbus_start_transaction() before computing read_len.

Fixes: b5b5b32081cd ("i2c: mlxbf: I2C SMBus driver for Mellanox BlueField SoC")
Closes: https://sashiko.dev/#/bug/linux-9d901144-2e2e-4106-9c52-4e02d97fca08
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/i2c/busses/i2c-mlxbf.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/drivers/i2c/busses/i2c-mlxbf.c b/drivers/i2c/busses/i2c-mlxbf.c
index 432d9588f32d..e85345906c76 100644
--- a/drivers/i2c/busses/i2c-mlxbf.c
+++ b/drivers/i2c/busses/i2c-mlxbf.c
@@ -772,6 +772,12 @@ mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
* than once?
*/
if (flags & MLXBF_I2C_F_READ) {
+ if (!operation->length ||
+ operation->length > MLXBF_I2C_MASTER_DATA_DESC_SIZE ||
+ !operation->buffer) {
+ ret = -EINVAL;
+ goto out_unlock;
+ }
read_en = 1;
/* Subtract 1 as required by HW. */
read_len = operation->length - 1;
@@ -2038,6 +2044,8 @@ static s32 mlxbf_i2c_smbus_xfer(struct i2c_adapter *adap, u16 addr,

switch (size) {
case I2C_SMBUS_QUICK:
+ if (read)
+ return -EOPNOTSUPP;
mlxbf_i2c_smbus_quick_command(&request, read);
dev_dbg(&adap->dev, "smbus quick, slave 0x%02x\n", addr);
break;
--
2.56.1.911.g883d8bcb20-goog