Skip to contentSkip to conversation

i2c: core: check returned size of emulated smbus block read

PATCH Applied Mans Rullgard 2 participants 4 messages Jun 13, 2020 – Jun 28, 2020

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

PATCH 1/1 i2c: core: check returned size of emulated smbus block read

If the i2c bus driver ignores the I2C_M_RECV_LEN flag (as some of them do), it is possible for an I2C_SMBUS_BLOCK_DATA read issued on some random device to return an arbitrary value in the first byte (and nothing else). When this happens, i2c_smbus_xfer_emulated() will happily write past the end of the supplied data buffer, thus causing Bad Things to happen. To prevent this, check the size before copying the data block and return an error if it is too large.

Fixes 209d27c3b167 ("i2c: Emulate SMBus block read over I2C")
---
 drivers/i2c/i2c-core-smbus.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/i2c/i2c-core-smbus.c +7 -0
index 3ac426a8ab5a..a719c26b98ac 100644
@@ -495,6 +495,13 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
 			break;
 		case I2C_SMBUS_BLOCK_DATA:
 		case I2C_SMBUS_BLOCK_PROC_CALL:
+			if (msg[1].buf[0] > I2C_SMBUS_BLOCK_MAX) {
+				dev_err(&adapter->dev,
+					"Invalid block size returned: %d\n",
+					msg[1].buf[0]);
+				status = -EINVAL;
+				goto cleanup;
+			}
 			for (i = 0; i < msg[1].buf[0] + 1; i++)
 				data->block[i] = msg[1].buf[i];
 			break;
Signature · 1 line
2.27.0
Applied
#1 lore raw
Quoted 2 lines · Mans Rullgard

If the i2c bus driver ignores the I2C_M_RECV_LEN flag (as some of them do), it is possible for an I2C_SMBUS_BLOCK_DATA read issued

Out of interest, which driver did you use?

Quoted 5 lines

on some random device to return an arbitrary value in the first byte (and nothing else). When this happens, i2c_smbus_xfer_emulated() will happily write past the end of the supplied data buffer, thus causing Bad Things to happen. To prevent this, check the size before copying the data block and return an error if it is too large.

Good catch, we were relying on the drivers too much here. I think the same fix is needed for the non-emulated case as well. Will have a look.

drivers/i2c/i2c-core-smbus.c static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, · line 502

Quoted 5 lines
drivers/i2c/i2c-core-smbus.c +5 -0
+			if (msg[1].buf[0] > I2C_SMBUS_BLOCK_MAX) {
+				dev_err(&adapter->dev,
+					"Invalid block size returned: %d\n",
+					msg[1].buf[0]);
+				status = -EINVAL;

I changed this to -EPROTO as described in
Documentation/i2c/fault-codes.rst.

Applied to for-current, thanks!

#2 lore raw
Quoted 5 lines · Wolfram Sang
Quoted 2 lines · Mans Rullgard

If the i2c bus driver ignores the I2C_M_RECV_LEN flag (as some of them do), it is possible for an I2C_SMBUS_BLOCK_DATA read issued

Out of interest, which driver did you use?

I saw it with the mv64xxx (Allwinner) and omap (Beaglebone) drivers. From a quick look, it seems like quite a few others have the same problem.

Quoted 20 lines
Quoted 5 lines

on some random device to return an arbitrary value in the first byte (and nothing else). When this happens, i2c_smbus_xfer_emulated() will happily write past the end of the supplied data buffer, thus causing Bad Things to happen. To prevent this, check the size before copying the data block and return an error if it is too large.

Good catch, we were relying on the drivers too much here. I think the same fix is needed for the non-emulated case as well. Will have a look.

drivers/i2c/i2c-core-smbus.c static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, · line 502

Quoted 5 lines
drivers/i2c/i2c-core-smbus.c +5 -0
+			if (msg[1].buf[0] > I2C_SMBUS_BLOCK_MAX) {
+				dev_err(&adapter->dev,
+					"Invalid block size returned: %d\n",
+					msg[1].buf[0]);
+				status = -EINVAL;

I changed this to -EPROTO as described in
Documentation/i2c/fault-codes.rst.

Applied to for-current, thanks!

Signature · 1 line
Måns Rullgård
#3 lore raw
Quoted 4 lines

I saw it with the mv64xxx (Allwinner) and omap (Beaglebone) drivers. From a quick look, it seems like quite a few others have the same problem.

Okay, well, to be fair, both drivers don't advertise I2C_SMBUS_BLOCK_DATA. The client driver should check for that. Anyhow, it makes sense to have your additional check in the core as fallback safety.