Skip to contentSkip to conversation

i2c: stub: Reject I2C block transfers exceeding I2C_SMBUS_BLOCK_MAX

PATCH In review Weiming Shi 2 participants 3 messages Mar 29 – Apr 14

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

PATCH 1/1 i2c: stub: Reject I2C block transfers exceeding I2C_SMBUS_BLOCK_MAX

The I2C_SMBUS_I2C_BLOCK_DATA case in stub_xfer() uses data->block[0] as the transfer length. The existing check only clamps it to avoid overrunning the chip->words[256] register array, but does not validate it against I2C_SMBUS_BLOCK_MAX (32), which is the limit of the union i2c_smbus_data.block buffer (34 bytes total). The driver is a development/test tool (CONFIG_I2C_STUB=m, not built by default) that must be loaded with a chip_addr= parameter.

A local user with access to /dev/i2c-* can issue an I2C_SMBUS ioctl with I2C_SMBUS_I2C_BLOCK_DATA and data->block[0] > 32, causing stub_xfer() to read or write past the end of the union i2c_smbus_data.block buffer:

 BUG: KASAN: stack-out-of-bounds in stub_xfer (drivers/i2c/i2c-stub.c:223)
 Read of size 1 at addr ffff88800abcfd92 by task exploit/81
 Call Trace:
  <TASK>
  stub_xfer (drivers/i2c/i2c-stub.c:223)
  __i2c_smbus_xfer (drivers/i2c/i2c-core-smbus.c:593)
  i2c_smbus_xfer (drivers/i2c/i2c-core-smbus.c:536)
  i2cdev_ioctl_smbus (drivers/i2c/i2c-dev.c:391)
  i2cdev_ioctl (drivers/i2c/i2c-dev.c:478)
  __x64_sys_ioctl (fs/ioctl.c:583)
  do_syscall_64 (arch/x86/entry/syscall_64.c:94)
  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:130)
  </TASK>

The bug exists because i2c-stub implements .smbus_xfer directly, bypassing the I2C_SMBUS_BLOCK_MAX validation in i2c_smbus_xfer_emulated(). The I2C_SMBUS_BLOCK_DATA case in the same function correctly validates against I2C_SMBUS_BLOCK_MAX, but the I2C_SMBUS_I2C_BLOCK_DATA case does not.

Fix by rejecting oversized transfers with -EINVAL when data->block[0] exceeds I2C_SMBUS_BLOCK_MAX, consistent with both the I2C_SMBUS_BLOCK_DATA case in the same function and the I2C_SMBUS_I2C_BLOCK_DATA validation in i2c_smbus_xfer_emulated().

Fixes 4710317891e4 ("i2c-stub: Implement I2C block support")
---
 drivers/i2c/i2c-stub.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/i2c/i2c-stub.c +4 -0
index fbb0db41b10e1..349ef9fb2fdbc 100644
@@ -214,6 +214,10 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
 		 * We ignore banks here, because banked chips don't use I2C
 		 * block transfers
 		 */
+		if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+			ret = -EINVAL;
+			break;
+		}
 		if (data->block[0] > 256 - command)	/* Avoid overrun */
 			data->block[0] = 256 - command;
 		len = data->block[0];
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Weiming,

Quoted 27 lines · Weiming Shi

The I2C_SMBUS_I2C_BLOCK_DATA case in stub_xfer() uses data->block[0] as the transfer length. The existing check only clamps it to avoid overrunning the chip->words[256] register array, but does not validate it against I2C_SMBUS_BLOCK_MAX (32), which is the limit of the union i2c_smbus_data.block buffer (34 bytes total). The driver is a development/test tool (CONFIG_I2C_STUB=m, not built by default) that must be loaded with a chip_addr= parameter.

A local user with access to /dev/i2c-* can issue an I2C_SMBUS ioctl with I2C_SMBUS_I2C_BLOCK_DATA and data->block[0] > 32, causing stub_xfer() to read or write past the end of the union i2c_smbus_data.block buffer:

 BUG: KASAN: stack-out-of-bounds in stub_xfer (drivers/i2c/i2c-stub.c:223)
 Read of size 1 at addr ffff88800abcfd92 by task exploit/81
 Call Trace:
  <TASK>
  stub_xfer (drivers/i2c/i2c-stub.c:223)
  __i2c_smbus_xfer (drivers/i2c/i2c-core-smbus.c:593)
  i2c_smbus_xfer (drivers/i2c/i2c-core-smbus.c:536)
  i2cdev_ioctl_smbus (drivers/i2c/i2c-dev.c:391)
  i2cdev_ioctl (drivers/i2c/i2c-dev.c:478)
  __x64_sys_ioctl (fs/ioctl.c:583)
  do_syscall_64 (arch/x86/entry/syscall_64.c:94)
  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:130)
  </TASK>
Quoted 5 lines

The bug exists because i2c-stub implements .smbus_xfer directly, bypassing the I2C_SMBUS_BLOCK_MAX validation in i2c_smbus_xfer_emulated(). The I2C_SMBUS_BLOCK_DATA case in the same function correctly validates against I2C_SMBUS_BLOCK_MAX, but the I2C_SMBUS_I2C_BLOCK_DATA case does not.

Thank you for the excellent analysis and detailed description. I agree with everything you wrote above.

Quoted 4 lines

Fix by rejecting oversized transfers with -EINVAL when data->block[0] exceeds I2C_SMBUS_BLOCK_MAX, consistent with both the I2C_SMBUS_BLOCK_DATA case in the same function and the I2C_SMBUS_I2C_BLOCK_DATA validation in i2c_smbus_xfer_emulated().

Would it make sense to also reject len == 0? That's what the i2c-stub driver does in the I2C_SMBUS_BLOCK_DATA case, so it would seem consistent to do the same for the I2C_SMBUS_I2C_BLOCK_DATA case.

Quoted 4 lines
Fixes 4710317891e4 ("i2c-stub: Implement I2C block support")

If there any public link to that report? If so, it should be mentioned here with a Closes: tag.

Quoted 10 lines
---
 drivers/i2c/i2c-stub.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/i2c/i2c-stub.c +0 -0
index fbb0db41b10e1..349ef9fb2fdbc 100644

drivers/i2c/i2c-stub.c static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags, · line 223

Quoted 10 lines
drivers/i2c/i2c-stub.c +4 -0
@@ -214,6 +214,10 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
 		 * We ignore banks here, because banked chips don't use I2C
 		 * block transfers
 		 */
+		if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
+			ret = -EINVAL;
+			break;
+		}
 		if (data->block[0] > 256 - command)	/* Avoid overrun */
 			data->block[0] = 256 - command;
 		len = data->block[0];
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#2 lore raw
Quoted 70 lines · Jean Delvare

Hi Weiming,

Quoted 27 lines · Weiming Shi

The I2C_SMBUS_I2C_BLOCK_DATA case in stub_xfer() uses data->block[0] as the transfer length. The existing check only clamps it to avoid overrunning the chip->words[256] register array, but does not validate it against I2C_SMBUS_BLOCK_MAX (32), which is the limit of the union i2c_smbus_data.block buffer (34 bytes total). The driver is a development/test tool (CONFIG_I2C_STUB=m, not built by default) that must be loaded with a chip_addr= parameter.

A local user with access to /dev/i2c-* can issue an I2C_SMBUS ioctl with I2C_SMBUS_I2C_BLOCK_DATA and data->block[0] > 32, causing stub_xfer() to read or write past the end of the union i2c_smbus_data.block buffer:

 BUG: KASAN: stack-out-of-bounds in stub_xfer (drivers/i2c/i2c-stub.c:223)
 Read of size 1 at addr ffff88800abcfd92 by task exploit/81
 Call Trace:
  <TASK>
  stub_xfer (drivers/i2c/i2c-stub.c:223)
  __i2c_smbus_xfer (drivers/i2c/i2c-core-smbus.c:593)
  i2c_smbus_xfer (drivers/i2c/i2c-core-smbus.c:536)
  i2cdev_ioctl_smbus (drivers/i2c/i2c-dev.c:391)
  i2cdev_ioctl (drivers/i2c/i2c-dev.c:478)
  __x64_sys_ioctl (fs/ioctl.c:583)
  do_syscall_64 (arch/x86/entry/syscall_64.c:94)
  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:130)
  </TASK>
Quoted 5 lines

The bug exists because i2c-stub implements .smbus_xfer directly, bypassing the I2C_SMBUS_BLOCK_MAX validation in i2c_smbus_xfer_emulated(). The I2C_SMBUS_BLOCK_DATA case in the same function correctly validates against I2C_SMBUS_BLOCK_MAX, but the I2C_SMBUS_I2C_BLOCK_DATA case does not.

Thank you for the excellent analysis and detailed description. I agree with everything you wrote above.

Quoted 4 lines

Fix by rejecting oversized transfers with -EINVAL when data->block[0] exceeds I2C_SMBUS_BLOCK_MAX, consistent with both the I2C_SMBUS_BLOCK_DATA case in the same function and the I2C_SMBUS_I2C_BLOCK_DATA validation in i2c_smbus_xfer_emulated().

Would it make sense to also reject len == 0? That's what the i2c-stub driver does in the I2C_SMBUS_BLOCK_DATA case, so it would seem consistent to do the same for the I2C_SMBUS_I2C_BLOCK_DATA case.

Quoted 4 lines
Fixes 4710317891e4 ("i2c-stub: Implement I2C block support")

If there any public link to that report? If so, it should be mentioned here with a Closes: tag.

Quoted 14 lines
---
 drivers/i2c/i2c-stub.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/i2c/i2c-stub.c +1 -0
index fbb0db41b10e1..349ef9fb2fdbc 100644
@@ -214,6 +214,10 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
 		 * We ignore banks here, because banked chips don't use I2C
 		 * block transfers
 		 */
+		if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
Quoted 12 lines

drivers/i2c/i2c-stub.c static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags, · line 223

Quoted 6 lines
drivers/i2c/i2c-stub.c +3 -0
+			ret = -EINVAL;
+			break;
+		}
 		if (data->block[0] > 256 - command)	/* Avoid overrun */
 			data->block[0] = 256 - command;
 		len = data->block[0];
Signature · 2 lines
Jean Delvare
SUSE L3 Support

Hi Jean,

Thanks for the review.
I'll add a data->block[0] == 0 check to reject zero-lenght in v2.

The report no publick URL. sorry

Thanks,
Weiming Shi