usb: misc: onboard_usb_dev: check i2c_transfer() return values
Subsystems (2)
PATCH 1/1 usb: misc: onboard_usb_dev: check i2c_transfer() return values
i2c_transfer() returns the number of messages transferred or a negative error code. The USB5744 I2C helpers only check for negative errors and therefore treat short transfers as successful.
In particular, if the two-message read completes only the register address write, the driver consumes uninitialized data from rd_buf and uses it in a read-modify-write operation.
Require every transfer to complete the expected number of messages, preserve negative error codes, and return -EIO for short transfers.
---
OpenCode was used to identify the issue and draft the code and changelog.
Compile-tested with CONFIG_I2C=y, CONFIG_USB_ONBOARD_DEV=m, and CONFIG_USB_ONBOARD_DEV_USB5744=y using:
make O=/tmp/opencode/linux-build-usb5744 W=1 \
drivers/usb/misc/onboard_usb_dev.oNo hardware or I2C fault-injection testing was performed.
drivers/usb/misc/onboard_usb_dev.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/usb/misc/onboard_usb_dev.c +11 -0
@@ -329,6 +329,8 @@ static int onboard_dev_5744_i2c_read_byte(struct i2c_client *client, u16 addr, u ret =i2c_transfer (client->adapter, msg,1 );if (ret <0 )return ret; +if (ret !=1 ) +return -EIO ; wr_buf[0 ] =USB5744_CMD_CREG_ACCESS ; wr_buf[1 ] =USB5744_CMD_CREG_ACCESS_LSB ;
@@ -338,6 +340,8 @@ static int onboard_dev_5744_i2c_read_byte(struct i2c_client *client, u16 addr, u ret =i2c_transfer (client->adapter, msg,1 );if (ret <0 )return ret; +if (ret !=1 ) +return -EIO ; wr_buf[0 ] =0 ; wr_buf[1 ] =USB5744_CREG_MEM_RD_ADDR ;
@@ -351,6 +355,9 @@ static int onboard_dev_5744_i2c_read_byte(struct i2c_client *client, u16 addr, u ret =i2c_transfer (client->adapter, msg,2 );if (ret <0 )return ret; +if (ret !=2 ) +return -EIO ; + *data = rd_buf[1 ];return 0 ;
@@ -374,6 +381,8 @@ static int onboard_dev_5744_i2c_write_byte(struct i2c_client *client, u16 addr, ret =i2c_transfer (client->adapter, msg,1 );if (ret <0 )return ret; +if (ret !=1 ) +return -EIO ; msg[0 ].len =3 ; wr_buf[0 ] =USB5744_CMD_CREG_ACCESS ;
@@ -383,6 +392,8 @@ static int onboard_dev_5744_i2c_write_byte(struct i2c_client *client, u16 addr, ret =i2c_transfer (client->adapter, msg,1 );if (ret <0 )return ret; +if (ret !=1 ) +return -EIO ;return 0 ; }
Signature · 1 line
2.20.1
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 8 lines
i2c_transfer()returns the number of messages transferred or a negative error code. The USB5744 I2C helpers only check for negative errors and therefore treat short transfers as successful.In particular, if the two-message read completes only the register address write, the driver consumes uninitialized data from
rd_bufand uses it in a read-modify-write operation.
Quoted 5 lines
Require every transfer to complete the expected number of messages, preserve negative error codes, and return
-EIOfor short transfers.Fixes 1143d41922c0 ("usb: misc: onboard_usb_dev: Fix usb5744 initialization sequence")Signed-off-by Siqi Chen <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 8 lines · Siqi Chen
i2c_transfer()returns the number of messages transferred or a negative error code. The USB5744 I2C helpers only check for negative errors and therefore treat short transfers as successful.In particular, if the two-message read completes only the register address write, the driver consumes uninitialized data from
rd_bufand uses it in a read-modify-write operation.
Quoted 6 lines
Require every transfer to complete the expected number of messages, preserve negative error codes, and return
-EIOfor short transfers.Fixes 1143d41922c0 ("usb: misc: onboard_usb_dev: Fix usb5744 initialization sequence")Signed-off-by Siqi Chen <[email protected]>---
Fix is right. However five identical blocks is a lot of churn for one invariant. A local helper like below simplifies it.
static int onboard_dev_5744_i2c_xfer(struct i2c_client *client,
struct i2c_msg *msg, int num)
{
int ret;
ret = i2c_transfer(client->adapter, msg, num);
if (ret < 0)
return ret;
return ret == num ? 0 : -EIO;}
Quoted 63 lines
OpenCode was used to identify the issue and draft the code and changelog.
Compile-tested with
CONFIG_I2C=y,CONFIG_USB_ONBOARD_DEV=m, andCONFIG_USB_ONBOARD_DEV_USB5744=yusing:make O=/tmp/opencode/linux-build-usb5744 W=1 \ drivers/usb/misc/onboard_usb_dev.oNo hardware or I2C fault-injection testing was performed.
drivers/usb/misc/onboard_usb_dev.c | 11 +++++++++++ 1 file changed, 11 insertions(+)drivers/usb/misc/onboard_usb_dev.c +11
-0@@ -329,6 +329,8 @@ static int onboard_dev_5744_i2c_read_byte(struct i2c_client *client, u16 addr, u ret =i2c_transfer (client->adapter, msg,1 );if (ret <0 )return ret; +if (ret !=1 ) +return -EIO ; wr_buf[0 ] =USB5744_CMD_CREG_ACCESS ; wr_buf[1 ] =USB5744_CMD_CREG_ACCESS_LSB ;@@ -338,6 +340,8 @@ static int onboard_dev_5744_i2c_read_byte(struct i2c_client *client, u16 addr, u ret =i2c_transfer (client->adapter, msg,1 );if (ret <0 )return ret; +if (ret !=1 ) +return -EIO ; wr_buf[0 ] =0 ; wr_buf[1 ] =USB5744_CREG_MEM_RD_ADDR ;@@ -351,6 +355,9 @@ static int onboard_dev_5744_i2c_read_byte(struct i2c_client *client, u16 addr, u ret =i2c_transfer (client->adapter, msg,2 );if (ret <0 )return ret; +if (ret !=2 ) +return -EIO ; + *data = rd_buf[1 ];return 0 ;@@ -374,6 +381,8 @@ static int onboard_dev_5744_i2c_write_byte(struct i2c_client *client, u16 addr, ret =i2c_transfer (client->adapter, msg,1 );if (ret <0 )return ret; +if (ret !=1 ) +return -EIO ; msg[0 ].len =3 ; wr_buf[0 ] =USB5744_CMD_CREG_ACCESS ;@@ -383,6 +392,8 @@ static int onboard_dev_5744_i2c_write_byte(struct i2c_client *client, u16 addr, ret =i2c_transfer (client->adapter, msg,1 );if (ret <0 )return ret; +if (ret !=1 ) +return -EIO ;return 0 ; }