Skip to contentSkip to conversation

i2c-stub: Avoid an array overrun on I2C block transfers

PATCH Applied Jean Delvare 3 participants 9 messages Jul 13, 2014 – Jul 18, 2014 5 messages not in the archive

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

PATCH 1/1 i2c-stub: Avoid an array overrun on I2C block transfers

I2C block transfers can have a size up to 32 bytes. If starting close to the end of the address space, there may not be enough room to write that many bytes (on I2C block writes) or not enough bytes to be read (on I2C block reads.) In that case, we must shorten the transfer so that it does not exceed the address space.

Cc Guenter Roeck <[email protected]>
Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
 drivers/i2c/i2c-stub.c |    2 ++
 1 file changed, 2 insertions(+)
drivers/i2c/i2c-stub.c +2 -0
@@ -220,6 +220,8 @@ static s32 stub_xfer(struct i2c_adapter
 		 * We ignore banks here, because banked chips don't use I2C
 		 * block transfers
 		 */
+		if (data->block[0] > 256 - command)	/* Avoid overrun */
+			data->block[0] = 256 - command;
 		len = data->block[0];
 		if (read_write == I2C_SMBUS_WRITE) {
 			for (i = 0; i < len; i++) {
Signature · 2 lines
Jean Delvare
SUSE L3 Support

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 16 lines · Jean Delvare

I2C block transfers can have a size up to 32 bytes. If starting close to the end of the address space, there may not be enough room to write that many bytes (on I2C block writes) or not enough bytes to be read (on I2C block reads.) In that case, we must shorten the transfer so that it does not exceed the address space.

Cc Guenter Roeck <[email protected]>
Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
  drivers/i2c/i2c-stub.c |    2 ++
  1 file changed, 2 insertions(+)

drivers/i2c/i2c-stub.c static s32 stub_xfer(struct i2c_adapter · line 224

Quoted 5 lines
drivers/i2c/i2c-stub.c +2 -0
@@ -220,6 +220,8 @@ static s32 stub_xfer(struct i2c_adapter
  		 * We ignore banks here, because banked chips don't use I2C
  		 * block transfers
  		 */
+		if (data->block[0] > 256 - command)	/* Avoid overrun */
+			data->block[0] = 256 - command;

Hi Jean,

is it safe to overwrite data->block[0], or should it be something like the following ?

		if (data->block[0] > 256 - command)	/* Avoid overrun */
			len = 256 - command;
		else
			len = data->block[0];

Also, wonder what happens in the real world if anyone does that. Would the write stop at offset 255, or would it wrap and write from 0 ?

Thanks,
Guenter

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Hi Guenter,

Quoted 26 lines · Guenter Roeck
Quoted 16 lines · Jean Delvare

I2C block transfers can have a size up to 32 bytes. If starting close to the end of the address space, there may not be enough room to write that many bytes (on I2C block writes) or not enough bytes to be read (on I2C block reads.) In that case, we must shorten the transfer so that it does not exceed the address space.

Cc Guenter Roeck <[email protected]>
Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
  drivers/i2c/i2c-stub.c |    2 ++
  1 file changed, 2 insertions(+)

drivers/i2c/i2c-stub.c static s32 stub_xfer(struct i2c_adapter · line 224

Quoted 5 lines
drivers/i2c/i2c-stub.c +2 -0
@@ -220,6 +220,8 @@ static s32 stub_xfer(struct i2c_adapter
  		 * We ignore banks here, because banked chips don't use I2C
  		 * block transfers
  		 */
+		if (data->block[0] > 256 - command)	/* Avoid overrun */
+			data->block[0] = 256 - command;

is it safe to overwrite data->block[0], or should it be something like the following ?

Quoted 4 lines
		if (data->block[0] > 256 - command)	/* Avoid overrun */
			len = 256 - command;
		else
			len = data->block[0];

It's not only safe, it is desired. Otherwise the caller doesn't know this is a short read, and it may take the end of the block buffer for actual data. Check the code in i2c-core.c:i2c_smbus_read_i2c_block_data(), you'll see it uses and even returns block[0]. Same for writes, that's the only way to notify the caller of short writes.

Quoted 2 lines

Also, wonder what happens in the real world if anyone does that. Would the write stop at offset 255, or would it wrap and write from 0 ?

Depends on the chip, I've seen both implementations.

It doesn't really matter what i2c-stub does, as device drivers should never do that. I just did not want to risk data leak or corruption in case it ever happens.

Signature · 2 lines
Jean Delvare
SUSE L3 Support

A message that is not in the archive (sent off-list or not yet received): [email protected]

Reviewed-by
#6 lore raw
Quoted 41 lines · Jean Delvare

Hi Guenter,

Quoted 26 lines · Guenter Roeck
Quoted 16 lines · Jean Delvare

I2C block transfers can have a size up to 32 bytes. If starting close to the end of the address space, there may not be enough room to write that many bytes (on I2C block writes) or not enough bytes to be read (on I2C block reads.) In that case, we must shorten the transfer so that it does not exceed the address space.

Cc Guenter Roeck <[email protected]>
Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
   drivers/i2c/i2c-stub.c |    2 ++
   1 file changed, 2 insertions(+)

drivers/i2c/i2c-stub.c static s32 stub_xfer(struct i2c_adapter · line 224

Quoted 5 lines
drivers/i2c/i2c-stub.c +2 -0
@@ -220,6 +220,8 @@ static s32 stub_xfer(struct i2c_adapter
   		 * We ignore banks here, because banked chips don't use I2C
   		 * block transfers
   		 */
+		if (data->block[0] > 256 - command)	/* Avoid overrun */
+			data->block[0] = 256 - command;

is it safe to overwrite data->block[0], or should it be something like the following ?

Quoted 4 lines
		if (data->block[0] > 256 - command)	/* Avoid overrun */
			len = 256 - command;
		else
			len = data->block[0];

It's not only safe, it is desired. Otherwise the caller doesn't know this is a short read, and it may take the end of the block buffer for actual data. Check the code in i2c-core.c:i2c_smbus_read_i2c_block_data(), you'll see it uses and even returns block[0]. Same for writes, that's the only way to notify the caller of short writes.

Quoted 9 lines
Quoted 2 lines

Also, wonder what happens in the real world if anyone does that. Would the write stop at offset 255, or would it wrap and write from 0 ?

Depends on the chip, I've seen both implementations.

It doesn't really matter what i2c-stub does, as device drivers should never do that. I just did not want to risk data leak or corruption in case it ever happens.

Ok, makes sense.

Thanks,
Guenter

#7 lore raw
In reply to a message not in the archive
Quoted 1 line · Jean Delvare

I2C block transfers can have a size up to 32 bytes. If starting close

Shouldn't that be "256 bytes"? 32 is SMBUS transfer size? Otherwise I don't understand the patch.

Quoted 28 lines

to the end of the address space, there may not be enough room to write that many bytes (on I2C block writes) or not enough bytes to be read (on I2C block reads.) In that case, we must shorten the transfer so that it does not exceed the address space.

Cc Guenter Roeck <[email protected]>
Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
 drivers/i2c/i2c-stub.c |    2 ++
 1 file changed, 2 insertions(+)
drivers/i2c/i2c-stub.c +2 -0
@@ -220,6 +220,8 @@ static s32 stub_xfer(struct i2c_adapter
 		 * We ignore banks here, because banked chips don't use I2C
 		 * block transfers
 		 */
+		if (data->block[0] > 256 - command)	/* Avoid overrun */
+			data->block[0] = 256 - command;
 		len = data->block[0];
 		if (read_write == I2C_SMBUS_WRITE) {
 			for (i = 0; i < len; i++) {
Signature · 3 lines
-- 
Jean Delvare
SUSE L3 Support
#8 lore raw
Quoted 6 lines · Wolfram Sang
Quoted 1 line · Jean Delvare

I2C block transfers can have a size up to 32 bytes. If starting close

Shouldn't that be "256 bytes"? 32 is SMBUS transfer size? Otherwise I don't understand the patch.

If I understand correctly, this is still an SMBus command, which is limited to 32 bytes. Maybe the description should read "SMBus I2C block transfers ...".

Guenter

Quoted 28 lines
Quoted 28 lines

to the end of the address space, there may not be enough room to write that many bytes (on I2C block writes) or not enough bytes to be read (on I2C block reads.) In that case, we must shorten the transfer so that it does not exceed the address space.

Cc Guenter Roeck <[email protected]>
Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>
---
 drivers/i2c/i2c-stub.c |    2 ++
 1 file changed, 2 insertions(+)
drivers/i2c/i2c-stub.c +2 -0
@@ -220,6 +220,8 @@ static s32 stub_xfer(struct i2c_adapter
 		 * We ignore banks here, because banked chips don't use I2C
 		 * block transfers
 		 */
+		if (data->block[0] > 256 - command)	/* Avoid overrun */
+			data->block[0] = 256 - command;
 		len = data->block[0];
 		if (read_write == I2C_SMBUS_WRITE) {
 			for (i = 0; i < len; i++) {
Signature · 3 lines
-- 
Jean Delvare
SUSE L3 Support

A message that is not in the archive (sent off-list or not yet received): [email protected]

#10 lore raw
Quoted 9 lines · Guenter Roeck
Quoted 5 lines · Wolfram Sang
Quoted 1 line · Jean Delvare

I2C block transfers can have a size up to 32 bytes. If starting close

Shouldn't that be "256 bytes"? 32 is SMBUS transfer size? Otherwise I don't understand the patch.

If I understand correctly, this is still an SMBus command, which is limited to 32 bytes. Maybe the description should read "SMBus I2C block transfers ...".

That's correct, "I2C block transfers" despite the name really are SMBus-style transfers and the SMBus buffer size limit of 32 bytes thus applies.

With this clarified, Wolfram, what is is that you do not understand? The start of the block transfer can be anywhere in 0-255, and the size can be in 1-32, so without a boundary check, one can attempt to read or write up to offset 255 + 32 - 1 = 286, which is way beyond the the end of the chip->words array. My patch prevents that from happening.

Signature · 2 lines
Jean Delvare
SUSE L3 Support

A message that is not in the archive (sent off-list or not yet received): [email protected]

#12 lore raw
Quoted 1 line

With this clarified, Wolfram, what is is that you do not understand?

Dunno anymore, probably the confusion confused me :)

Applied
#13 lore raw
In reply to a message not in the archive
Quoted 9 lines · Jean Delvare

I2C block transfers can have a size up to 32 bytes. If starting close to the end of the address space, there may not be enough room to write that many bytes (on I2C block writes) or not enough bytes to be read (on I2C block reads.) In that case, we must shorten the transfer so that it does not exceed the address space.

Cc Guenter Roeck <[email protected]>
Cc Wolfram Sang <wsa-z923LK4zBo2bacvFa/[email protected]>

Applied to for-next, thanks!