diff mbox series

[v2,2/6] usb: typec: ucsi: Don't truncate the reads

Message ID 20240816135859.3499351-3-heikki.krogerus@linux.intel.com (mailing list archive)
State Accepted
Commit 1d05c382ddb4e43ce251a50f308df5e42a2f6088
Headers show
Series usb: typec: ucsi: Minor improvements | expand

Commit Message

Heikki Krogerus Aug. 16, 2024, 1:58 p.m. UTC
That may silently corrupt the data. Instead, failing attempts
to read more than the interface can handle.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/ucsi/ucsi.c | 8 ++------
 drivers/usb/typec/ucsi/ucsi.h | 2 ++
 2 files changed, 4 insertions(+), 6 deletions(-)

Comments

Abhishek Pandit-Subedi Aug. 18, 2024, 11:59 p.m. UTC | #1
On Fri, Aug 16, 2024 at 6:59 AM Heikki Krogerus
<heikki.krogerus@linux.intel.com> wrote:
>
> That may silently corrupt the data. Instead, failing attempts
> to read more than the interface can handle.
>
> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> ---
>  drivers/usb/typec/ucsi/ucsi.c | 8 ++------
>  drivers/usb/typec/ucsi/ucsi.h | 2 ++
>  2 files changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
> index 4039851551c1..96ef099a6f84 100644
> --- a/drivers/usb/typec/ucsi/ucsi.c
> +++ b/drivers/usb/typec/ucsi/ucsi.c
> @@ -99,12 +99,8 @@ static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci,
>
>         *cci = 0;
>
> -       /*
> -        * Below UCSI 2.0, MESSAGE_IN was limited to 16 bytes. Truncate the
> -        * reads here.
> -        */
> -       if (ucsi->version <= UCSI_VERSION_1_2)
> -               size = clamp(size, 0, 16);
> +       if (size > UCSI_MAX_DATA_LENGTH(ucsi))
> +               return -EINVAL;
>
>         ret = ucsi->ops->sync_control(ucsi, command);
>         if (ret)
> diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h
> index 7bc132b59027..5e3c6cb822c8 100644
> --- a/drivers/usb/typec/ucsi/ucsi.h
> +++ b/drivers/usb/typec/ucsi/ucsi.h
> @@ -412,6 +412,8 @@ struct ucsi {
>  #define UCSI_DELAY_DEVICE_PDOS BIT(1)  /* Reading PDOs fails until the parter is in PD mode */
>  };
>
> +#define UCSI_MAX_DATA_LENGTH(u) (((u)->version < UCSI_VERSION_2_0) ? 0x10 : 0xff)
> +
>  #define UCSI_MAX_SVID          5
>  #define UCSI_MAX_ALTMODES      (UCSI_MAX_SVID * 6)
>
> --
> 2.43.0
>
>

Looking through the list of commands that changed between UCSI1.2 and
UCSI2+, only GET_CONNECTOR_STATUS seems to have a size that exceeds 16
bytes (128 bits). As long as we treat that structure specially, this
change should be fine.

Reviewed-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
diff mbox series

Patch

diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index 4039851551c1..96ef099a6f84 100644
--- a/drivers/usb/typec/ucsi/ucsi.c
+++ b/drivers/usb/typec/ucsi/ucsi.c
@@ -99,12 +99,8 @@  static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci,
 
 	*cci = 0;
 
-	/*
-	 * Below UCSI 2.0, MESSAGE_IN was limited to 16 bytes. Truncate the
-	 * reads here.
-	 */
-	if (ucsi->version <= UCSI_VERSION_1_2)
-		size = clamp(size, 0, 16);
+	if (size > UCSI_MAX_DATA_LENGTH(ucsi))
+		return -EINVAL;
 
 	ret = ucsi->ops->sync_control(ucsi, command);
 	if (ret)
diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h
index 7bc132b59027..5e3c6cb822c8 100644
--- a/drivers/usb/typec/ucsi/ucsi.h
+++ b/drivers/usb/typec/ucsi/ucsi.h
@@ -412,6 +412,8 @@  struct ucsi {
 #define UCSI_DELAY_DEVICE_PDOS	BIT(1)	/* Reading PDOs fails until the parter is in PD mode */
 };
 
+#define UCSI_MAX_DATA_LENGTH(u) (((u)->version < UCSI_VERSION_2_0) ? 0x10 : 0xff)
+
 #define UCSI_MAX_SVID		5
 #define UCSI_MAX_ALTMODES	(UCSI_MAX_SVID * 6)