Skip to content

Commit 40e0520

Browse files
mansrwsakernel
authored andcommitted
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: 209d27c ("i2c: Emulate SMBus block read over I2C") Signed-off-by: Mans Rullgard <mans@mansr.com> [wsa: use better errno] Signed-off-by: Wolfram Sang <wsa@kernel.org>
1 parent 502035e commit 40e0520

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

drivers/i2c/i2c-core-smbus.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,13 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
495495
break;
496496
case I2C_SMBUS_BLOCK_DATA:
497497
case I2C_SMBUS_BLOCK_PROC_CALL:
498+
if (msg[1].buf[0] > I2C_SMBUS_BLOCK_MAX) {
499+
dev_err(&adapter->dev,
500+
"Invalid block size returned: %d\n",
501+
msg[1].buf[0]);
502+
status = -EPROTO;
503+
goto cleanup;
504+
}
498505
for (i = 0; i < msg[1].buf[0] + 1; i++)
499506
data->block[i] = msg[1].buf[i];
500507
break;

0 commit comments

Comments
 (0)