Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: typec: ucsi: clamp returned message length to buffer size
@ 2026-12-06 10:03 Matti Paksula
  2026-12-06 10:22 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Matti Paksula @ 2026-12-06 10:03 UTC (permalink / raw)
  To: Heikki Krogerus, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

ucsi_sync_control_common() only copies at most `size` bytes from
MESSAGE_IN into `data`, yet ucsi_run_command() returned
UCSI_CCI_LENGTH(*cci) unclamped.

If a PPM reports a CCI length larger than the caller's buffer `size`,
callers that derive element counts from the return value can read past
the end of their buffers. On a Lenovo ThinkPad X1 Carbon Gen 14 (UCSI
2.x/3.x PPM), the first UCSI_GET_PDOS call in ucsi_get_pdos() requests
UCSI_MAX_PDOS (4) PDOs into a 16-byte slice of con->src_pdos[7], yet
firmware reports UCSI_CCI_LENGTH(*cci) == 24 (6 PDOs). Because the
unclamped return value gives num_pdos = 6 (which is >= UCSI_MAX_PDOS),
the second call fetches the remaining 2 PDOs and ucsi_get_pdos()
returns 8, setting con->num_pdos = 8 and triggering out-of-bounds reads
in ucsi_psy_get_voltage_max() and ucsi_psy_get_current_max():

  UBSAN: array-index-out-of-bounds in drivers/usb/typec/ucsi/psy.c:122:23
  index 7 is out of range for type 'u32 [7]'
  Hardware name: LENOVO 21V7CTO1WW/21V7CTO1WW, BIOS N4OET56W (1.19 ) 11/11/2026
  Call Trace:
   ucsi_psy_get_prop+0x4d4/0x510 [typec_ucsi]
   power_supply_format_property+0x102/0x3c0

  UBSAN: array-index-out-of-bounds in drivers/usb/typec/ucsi/psy.c:188:23
  index 7 is out of range for type 'u32 [7]'

The same unclamped return value also affects ucsi_register_altmodes(),
which iterates up to `len / sizeof(alt[0])` over `alt[2]`.

Clamp the length returned by ucsi_run_command() to `size` whenever a
destination buffer `data` was supplied, so callers never observe more
bytes than were actually copied into `data`. Also guard `index <=
con->num_pdos` in ucsi_psy_get_voltage_now() so an RDO object position
beyond the stored PDOs (both during port registration before
ucsi_get_src_pdos() has populated con->src_pdos, and for EPR object
positions > 7) cannot index past valid entries in `con->src_pdos`.

Fixes: 1f4642b72be7 ("usb: typec: ucsi: Retrieve all the PDOs instead of just the first 4")
Fixes: 992a60ed0d5e ("usb: typec: ucsi: register with power_supply class")
Cc: stable@vger.kernel.org
Signed-off-by: Matti Paksula <matti.paksula@gmail.com>
---
 drivers/usb/typec/ucsi/psy.c  | 2 +-
 drivers/usb/typec/ucsi/ucsi.c | 8 +++++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/typec/ucsi/psy.c b/drivers/usb/typec/ucsi/psy.c
index c8ebab8ba..bd4c33cf9 100644
--- a/drivers/usb/typec/ucsi/psy.c
+++ b/drivers/usb/typec/ucsi/psy.c
@@ -149,7 +149,7 @@ static int ucsi_psy_get_voltage_now(struct ucsi_connector *con,
 	switch (UCSI_CONSTAT(con, PWR_OPMODE)) {
 	case UCSI_CONSTAT_PWR_OPMODE_PD:
 		index = rdo_index(con->rdo);
-		if (index > 0) {
+		if (index > 0 && index <= con->num_pdos) {
 			pdo = con->src_pdos[index - 1];
 			val->intval = pdo_fixed_voltage(pdo) * 1000;
 		} else {
diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index 9e453d216..04af3d786 100644
--- a/drivers/usb/typec/ucsi/ucsi.c
+++ b/drivers/usb/typec/ucsi/ucsi.c
@@ -163,7 +163,13 @@ static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci,
 	if (ret)
 		return ret;
 
-	return err ?: UCSI_CCI_LENGTH(*cci);
+	if (err)
+		return err;
+
+	if (data)
+		return min_t(size_t, UCSI_CCI_LENGTH(*cci), size);
+
+	return UCSI_CCI_LENGTH(*cci);
 }
 
 static int ucsi_read_error(struct ucsi *ucsi, u8 connector_num)
-- 
2.56.1


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

end of thread, other threads:[~2026-12-06 10:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06 10:03 [PATCH] usb: typec: ucsi: clamp returned message length to buffer size Matti Paksula
2026-12-06 10:22 ` sashiko-bot

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