From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796551433; b=QQ06R0XWycwNtr8+YEYQZ0zgn8nIvjkTbWIC3yda9wD13SH8UEKs4wwOUIs/5yNqqGsF fWgEPIdmy51a6PyuK765tGAXjMqKKzwMFlCnon170hNifyTmPLk5HTN5ufVuoPA0CVuvz I+BsgG8o7V7WDSe1AMQzNQC1pcdjV/cAd8tbAhBzI917sCWiBKBvH1UkHEbriNIIt7ZC+ Zw8BUdR+LnGWuTZLuYfKyn5PhKGzpVgz9YKajeLbueU3J4r7MPh13bOUe0Fslum3wBxj9 1kkHd4tN6sT9Mv0bK/cmeuqFfk/sCmKEnO+/9EqT6EWPoajcBwN+x9JXhXbdab6etzA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796551433; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=RtR5ujaUD9zg6X1tNMujIta6BPyIuOS7vKKrPo670iE=; b=RT0TGB4VeC8IjWLBDJFEDNXsdnjlYsw7AOAZDSiLICY/Cq6dbIFocnrIcP76Ka5kqNFP zmQh6z4PVOzUXMKrYyNo4oI9Sg32RmEmaXPVuG4FrdnPmYO1a+gltIVAESeibDMQnnFVA Mv1pOc/C0JAyT5L9woti6vVfKUImeot5st5YqAvtowj8ZwZVVxxMNdhaHYf4ECmWX8iGX zRQGmhx/Mxil0yxDfNsr1fWu06ei82ZJCCgelvAaG2Mi/FJOJ2IPEjfCy/4vVG6a8xfDV ZkWDBdXbG4OazgyoKk1e/JRLGyvF7uZ4FHTC70l58WFNhYPf5tZqPwtm8mD+3Xw26Rg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=pWJh21mk; arc=none smtp.remote-ip=209.85.167.47 Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.47]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18AED606A337 for ; Sun, 6 Dec 2026 10:03:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 18AED606A337 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=pWJh21mk Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-5be53e5fce1so2643581e87.1 for ; Sun, 06 Dec 2026 02:03:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796551431; x=1797156231; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=RtR5ujaUD9zg6X1tNMujIta6BPyIuOS7vKKrPo670iE=; b=pWJh21mkbF+bPX3bhiArlw5FsC5T33RB8fksqrvKTfkV07lLgX4yRJCoFRCwe3Os6o XNJoWTr0/t8ZPphUjEMnKpn+DERTTji//E1WJwNBfO+BtUlzw/tUaVbwYivvKZFOO+Mf xYaiR+cs/JWJUr1yeYgZ2NgptFB7zx4989OT2pADMz0DxD53A1EgCj482BBN8ddLd9d1 0Rn4AL5I6uTUd+eAHHx17G8JOUNLNK9nPowcOxXavdVZSUarRl6kJMTz8puCy4q3+Kcd SSGRpye+1h90E/e2r+csOuwzM/c1J8IM8p4uOLfuSJ3Cp2j3O2JeneUCBwtI7JbrpwZR mSsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796551431; x=1797156231; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=RtR5ujaUD9zg6X1tNMujIta6BPyIuOS7vKKrPo670iE=; b=i4o1+f+NqUZpeDLrpYKA/Tz+KjyVzxNwIsYyHCQu+HzfjVv+9J3g04eeZkuEwxlui3 /uORS8tvSQbPYNVaLjJFkOzp1/DiqmaliKM3CPtyUT1x16zCExMa2k3+VuFKv0QvOP1D MCq+gb1TZ7SpzxXRQEhm9pF3yucPCSwM+FVsaJZ0KAlCu11EW8V4C1IcMghLFheqnFfd X8oGYPh1psyAK8gd2UmETU3NQfllXvZYzp09zYvCoO9drMe9QHUwp+9i/1i9V2Du7ymc d4P/TyY+H/fC6F9OcAm5oX23g+hxt2/NNyvGpdeBK9Z0DU3qx6pnh5bUoH9WgYFVhB9R +dcw== X-Forwarded-Encrypted: i=1; AL+hErHYTDYe/XyuoyFPu3NcU4kBsciTSA3tUkMMf7A+J+HUVYH3/Cb3UB0gdq6/CXONo9oXGbYHcb0fVFPwPDw=@vger.kernel.org X-Gm-Message-State: AB4q+uEdPK5dKO33T9y1wm+BkgRsMMu8ky+Kbf8I/QF8MiMrUuwlkStX s/rl1J5ngbijMn6E9EZh5Pkl3kRMm+OhXljt/ncwnGsqXMSnFwpGfUGc X-Gm-Gg: AZFdTZf+BHRAfgwWEDkAcam4DxRTLOnrSwOm3bo+HUsrC+PnQtN0arKBgAkTPJhyFCs q/h0B7zKfPe3Vlu/IxzuspZ+hwGxnbwMUoKhR+nYAKeJX3OMgIvE9JaYJJMx0/gEg6pNVazDs73 a0zXPDXX81z4gzrdJfqhwIv5R+UPS0nt41wR27EiiXiopRxfzBux2KBE1bUUgWqGAnFeGzHFUUn 4veVg7FSShoSBEzANUbXk6bCPVUT27H3HOrXuzgmfDQPg51KPmrhDBR23OEnsL0trBnig6UsDUs IdU2j9+MF0Sbc7ECLuGBIycPzk93VlVFpdnnXurO+TcoPgrtM4bNZgmmlqXtF9ALdkMUnFe1yBj cNfbsCHVoQnk5s2wzdELSBuvt7CjINhRfXRLY64dWqX5LcBInBfc8sZJlDqfF6mfozNat2xHhfO v3RYSwfRcp+v4Jcj8WzVS8LOYkdkUkcmNFJaWZObYEjrRChVGlzWibnHzyVFdu/WhedZwofngZU eB0S8LZCbjB6J++HIYv/T5YJwXSOnGhRYCOTMG6UAK558p9k1XTExpP5vcZ5u283OtVtDcW/Yn2 Qr+OTrI9AaTBXmEhHpwFtSUZLmbJxojLUxzDX8+abkkLtpxOK0TIpSBWIqSY8faCa2bvqeYCR4U dzg/cMgOsff0bJQk8HXkb0vOQydYEC2vc9joDNRqdS4GzBgIsCIDa18GoU20QDm+srtp8w1APz+ Mtc9yQ38hQp3WViLuVYgl8KpUJVM7GyjnsyUR4Tuu/OsXalF2qxb4iKQGPagumlt1d7pmqqWSTK Li29B+kp7HnIL/id3aiBxMOj9msCMgrva8byw== X-Received: by 2002:a05:6512:3d8b:b0:5bf:f1fe:6c0e with SMTP id 2adb3069b0e04-5c3861c4ddbmr1897294e87.30.1796551430984; Sun, 06 Dec 2026 02:03:50 -0800 (PST) Received: from 77a38ea603f2.centaur-major.ts.net (212-90-93-49.bb.dnainternet.fi. [212.90.93.49]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5c384a768e4sm2289326e87.43.2026.12.06.02.03.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2026 02:03:50 -0800 (PST) From: Matti Paksula To: Heikki Krogerus , Greg Kroah-Hartman 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 Message-ID: <20261206100349.15-1-matti.paksula@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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