Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: misc: onboard_usb_dev: check i2c_transfer() return values
@ 2026-11-24  2:25 Siqi Chen
  2026-11-24  2:31 ` sashiko-bot
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Siqi Chen @ 2026-11-24  2:25 UTC (permalink / raw)
  To: Matthias Kaehlcke, Greg Kroah-Hartman
  Cc: Radhey Shyam Pandey, Jonathan Stroud, linux-usb, linux-kernel

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.

Fixes: 1143d41922c0 ("usb: misc: onboard_usb_dev: Fix usb5744 initialization sequence")
Signed-off-by: Siqi Chen <chensiqi@uniontech.com>
---
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.o

No hardware or I2C fault-injection testing was performed.

 drivers/usb/misc/onboard_usb_dev.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/usb/misc/onboard_usb_dev.c b/drivers/usb/misc/onboard_usb_dev.c
index e357cc8af34f..98380f8459b4 100644
--- a/drivers/usb/misc/onboard_usb_dev.c
+++ b/drivers/usb/misc/onboard_usb_dev.c
@@ -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;
 }
-- 
2.20.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] usb: misc: onboard_usb_dev: check i2c_transfer() return values
  2026-11-24  2:25 [PATCH] usb: misc: onboard_usb_dev: check i2c_transfer() return values Siqi Chen
@ 2026-11-24  2:31 ` sashiko-bot
  2026-11-26  7:53 ` Pandey, Radhey Shyam
  2026-12-01  2:17 ` [PATCH v2] " Siqi Chen
  2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-24  2:31 UTC (permalink / raw)
  To: Siqi Chen; +Cc: linux-usb

> 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.
> 
> Fixes: 1143d41922c0 ("usb: misc: onboard_usb_dev: Fix usb5744 initialization sequence")
> Signed-off-by: Siqi Chen <chensiqi@uniontech.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124022500.1020611-1-chensiqi@uniontech.com?part=1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] usb: misc: onboard_usb_dev: check i2c_transfer() return values
  2026-11-24  2:25 [PATCH] usb: misc: onboard_usb_dev: check i2c_transfer() return values Siqi Chen
  2026-11-24  2:31 ` sashiko-bot
@ 2026-11-26  7:53 ` Pandey, Radhey Shyam
  2026-12-01  2:17 ` [PATCH v2] " Siqi Chen
  2 siblings, 0 replies; 5+ messages in thread
From: Pandey, Radhey Shyam @ 2026-11-26  7:53 UTC (permalink / raw)
  To: Siqi Chen, Matthias Kaehlcke, Greg Kroah-Hartman
  Cc: Radhey Shyam Pandey, Jonathan Stroud, linux-usb, linux-kernel

On 11/24/2026 7:55 AM, Siqi Chen wrote:
> 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.
> 
> Fixes: 1143d41922c0 ("usb: misc: onboard_usb_dev: Fix usb5744 initialization sequence")
> Signed-off-by: Siqi Chen <chensiqi@uniontech.com>
> ---

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;
   }



> 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.o
> 
> No hardware or I2C fault-injection testing was performed.
> 
>   drivers/usb/misc/onboard_usb_dev.c | 11 +++++++++++
>   1 file changed, 11 insertions(+)
> 
> diff --git a/drivers/usb/misc/onboard_usb_dev.c b/drivers/usb/misc/onboard_usb_dev.c
> index e357cc8af34f..98380f8459b4 100644
> --- a/drivers/usb/misc/onboard_usb_dev.c
> +++ b/drivers/usb/misc/onboard_usb_dev.c
> @@ -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;
>   }


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] usb: misc: onboard_usb_dev: check i2c_transfer() return values
  2026-11-24  2:25 [PATCH] usb: misc: onboard_usb_dev: check i2c_transfer() return values Siqi Chen
  2026-11-24  2:31 ` sashiko-bot
  2026-11-26  7:53 ` Pandey, Radhey Shyam
@ 2026-12-01  2:17 ` Siqi Chen
  2026-12-01  2:21   ` sashiko-bot
  2 siblings, 1 reply; 5+ messages in thread
From: Siqi Chen @ 2026-12-01  2:17 UTC (permalink / raw)
  To: Matthias Kaehlcke, Greg Kroah-Hartman
  Cc: Radhey Shyam Pandey, Jonathan Stroud, linux-usb, linux-kernel

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.

Fixes: 1143d41922c0 ("usb: misc: onboard_usb_dev: Fix usb5744 initialization sequence")
Signed-off-by: Siqi Chen <chensiqi@uniontech.com>
---
Changes in v2:
- Add a local helper to centralize transfer count validation, as
  suggested by Radhey Shyam Pandey.

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.o

No hardware or I2C fault-injection testing was performed.

 drivers/usb/misc/onboard_usb_dev.c | 33 +++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/drivers/usb/misc/onboard_usb_dev.c b/drivers/usb/misc/onboard_usb_dev.c
index b17a6c80c4ab..efab71ea4e6d 100644
--- a/drivers/usb/misc/onboard_usb_dev.c
+++ b/drivers/usb/misc/onboard_usb_dev.c
@@ -311,6 +311,18 @@ static void onboard_dev_attach_usb_driver(struct work_struct *work)
 }
 
 #if IS_ENABLED(CONFIG_USB_ONBOARD_DEV_USB5744)
+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;
+}
+
 static int onboard_dev_5744_i2c_read_byte(struct i2c_client *client, u16 addr, u8 *data)
 {
 	struct i2c_msg msg[2];
@@ -326,8 +338,8 @@ static int onboard_dev_5744_i2c_read_byte(struct i2c_client *client, u16 addr, u
 	msg[0].len = sizeof(wr_buf);
 	msg[0].buf = wr_buf;
 
-	ret = i2c_transfer(client->adapter, msg, 1);
-	if (ret < 0)
+	ret = onboard_dev_5744_i2c_xfer(client, msg, 1);
+	if (ret)
 		return ret;
 
 	wr_buf[0] = USB5744_CMD_CREG_ACCESS;
@@ -335,8 +347,8 @@ static int onboard_dev_5744_i2c_read_byte(struct i2c_client *client, u16 addr, u
 	wr_buf[2] = 0;
 	msg[0].len = 3;
 
-	ret = i2c_transfer(client->adapter, msg, 1);
-	if (ret < 0)
+	ret = onboard_dev_5744_i2c_xfer(client, msg, 1);
+	if (ret)
 		return ret;
 
 	wr_buf[0] = 0;
@@ -348,9 +360,10 @@ static int onboard_dev_5744_i2c_read_byte(struct i2c_client *client, u16 addr, u
 	msg[1].len = 2;
 	msg[1].buf = rd_buf;
 
-	ret = i2c_transfer(client->adapter, msg, 2);
-	if (ret < 0)
+	ret = onboard_dev_5744_i2c_xfer(client, msg, 2);
+	if (ret)
 		return ret;
+
 	*data = rd_buf[1];
 
 	return 0;
@@ -371,8 +384,8 @@ static int onboard_dev_5744_i2c_write_byte(struct i2c_client *client, u16 addr,
 	msg[0].len = sizeof(wr_buf);
 	msg[0].buf = wr_buf;
 
-	ret = i2c_transfer(client->adapter, msg, 1);
-	if (ret < 0)
+	ret = onboard_dev_5744_i2c_xfer(client, msg, 1);
+	if (ret)
 		return ret;
 
 	msg[0].len = 3;
@@ -380,8 +393,8 @@ static int onboard_dev_5744_i2c_write_byte(struct i2c_client *client, u16 addr,
 	wr_buf[1] = USB5744_CMD_CREG_ACCESS_LSB;
 	wr_buf[2] = 0;
 
-	ret = i2c_transfer(client->adapter, msg, 1);
-	if (ret < 0)
+	ret = onboard_dev_5744_i2c_xfer(client, msg, 1);
+	if (ret)
 		return ret;
 
 	return 0;
-- 
2.20.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] usb: misc: onboard_usb_dev: check i2c_transfer() return values
  2026-12-01  2:17 ` [PATCH v2] " Siqi Chen
@ 2026-12-01  2:21   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-01  2:21 UTC (permalink / raw)
  To: Siqi Chen; +Cc: linux-usb

> 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.
> 
> Fixes: 1143d41922c0 ("usb: misc: onboard_usb_dev: Fix usb5744 initialization sequence")
> Signed-off-by: Siqi Chen <chensiqi@uniontech.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201021736.2748309-1-chensiqi@uniontech.com?part=1

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-01  2:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  2:25 [PATCH] usb: misc: onboard_usb_dev: check i2c_transfer() return values Siqi Chen
2026-11-24  2:31 ` sashiko-bot
2026-11-26  7:53 ` Pandey, Radhey Shyam
2026-12-01  2:17 ` [PATCH v2] " Siqi Chen
2026-12-01  2:21   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox