Skip to contentSkip to conversation

i2c: dln2: prevent buffer overflow in dln2_i2c_write()

PATCH In review Dan Carpenter 1 participant 2 messages Oct 12, 2022 – Oct 14, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: dln2: prevent buffer overflow in dln2_i2c_write()

The "data_len" value is use controlled via the ioctl. It needs to be bounds checked to prevent a buffer overflow.

Fixes db23e5001f75 ("i2c: add support for Diolan DLN-2 USB-I2C adapter")
---
 drivers/i2c/busses/i2c-dln2.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/i2c/busses/i2c-dln2.c +3 -0
index 2a2089db71a5..14f4aeeb263d 100644
@@ -83,6 +83,9 @@ static int dln2_i2c_write(struct dln2_i2c *dln2, u8 addr,
 
 	BUILD_BUG_ON(sizeof(*tx) > DLN2_I2C_BUF_SIZE);
 
+	if (data_len > sizeof(tx->buf))
+		return -EINVAL;
+
 	tx->port = dln2->port;
 	tx->addr = addr;
 	tx->mem_addr_len = 0;
Signature · 1 line
2.35.1
#1 lore raw
Quoted 14 lines · Dan Carpenter

The "data_len" value is use controlled via the ioctl. It needs to be bounds checked to prevent a buffer overflow.

Fixes db23e5001f75 ("i2c: add support for Diolan DLN-2 USB-I2C adapter")
---
 drivers/i2c/busses/i2c-dln2.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/i2c/busses/i2c-dln2.c +0 -0
index 2a2089db71a5..14f4aeeb263d 100644

drivers/i2c/busses/i2c-dln2.c static int dln2_i2c_write(struct dln2_i2c *dln2, u8 addr, · line 87

Quoted 5 lines
drivers/i2c/busses/i2c-dln2.c +2 -0
@@ -83,6 +83,9 @@ static int dln2_i2c_write(struct dln2_i2c *dln2, u8 addr,
 
 	BUILD_BUG_ON(sizeof(*tx) > DLN2_I2C_BUF_SIZE);
 
+	if (data_len > sizeof(tx->buf))
+		return -EINVAL;

Never mind. This is checked in i2c_check_for_quirks() so the patch is not required. My bad.

regards,
dan carpenter