mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Matti Paksula <matti.paksula@gmail.com>
To: Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] usb: typec: ucsi: clamp returned message length to buffer size
Date: Sun,  6 Dec 2026 10:03:49 +0000	[thread overview]
Message-ID: <20261206100349.15-1-matti.paksula@gmail.com> (raw)

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


                 reply	other threads:[~2026-12-06 10:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261206100349.15-1-matti.paksula@gmail.com \
    --to=matti.paksula@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®