From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796551433; b=KGUQMUnHX61M3CwbnVkPdgCSgm1Df8iehlyfrxduZEAZfngh81/bvP9Rntaew82E8Nu1 ZYAOqy2qzfTY98RWm2sSdGGw6N4iIo/zi0FHT3ZhZ5jk34Xp3qNuFNYBVhWrYi84Ph7iw lGe8vGxSgHEhhrHN82ACoS8MoOKalSHssKUBK4NYGJSlAXs6KQXVIzF1TG3x+Jd5cKf5m 9+OqNI+csIxOGBYzi/zCeV4Qb2n5yq+0kFpYEFRbYnD5Tb7yf2wwlcxo4hjKFDcEzV4dQ S36sshaCdaHB635oVGzQdf7t80OMvzG+8Fvze3fRbP5q/mlUqYh5guARixwCT2h+fNA== 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-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=C9qYfazELuKcYUyprmJfccKlSYfHtDi31LAl+uOarxnrHfbIkGDBviR9qGQFwdc9LKdy p7R4qyIqChBkX/uYeVbzefMKSbCywhcXDJpfKmnkNP7YXhN7hQCBeU4cOjIy/o2otvHRX 44IYf+PdZOzHaOeaurelDY4b/hIZSaL25f5vdoUn5nLq8GYYp7PM4Y+I+PVOUqHWHyx8p hFKxEJLD2ebDlkrWsHkZ1gVJUj2C41/zXABFoGE4J1N1aRKhEGKCc9LTyGJs9MoAFq03F c+yn+TM+yj5tUw4CnV7dnMOJ0R5BSLfv9xail1G9MkdeED5dlppH07NOuksl/l6tA9A== 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.44 Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) (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 E4F7E606A335 for ; Sun, 6 Dec 2026 10:03:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E4F7E606A335 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-f44.google.com with SMTP id 2adb3069b0e04-5c3683264fcso1590183e87.2 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=PQOgfSADYi+LbE22QVv+aLBhIs28AH4vGX8nDdcSjCY8Ns730QC7E5FuYACqmUOiRw 1UbdIUpSzH0bAfiSVazU2RZd2vtUOgxGz9wrnkwO6EFL8PtmumMhFe/mMuy3vOQHKr1b wuAJ0wQkRXCEjMrtN0W6luF61k26umi//JiKT+bMRH0xhbzZl6jeLKFQZ+A0Asi9bG00 DOpAOHIEWG/8UGGWi8qC1TZ9iWk7sN0sPsPwSYT2eSKRn5vvUeKqXxCmU/k411gr4oCo X3LZnHoA+Z9Wc6L9IJhyBjiObrgNKcVjDU5pkiYR82vT3ae1ueQvRX+rNDqscueaytgY QDTA== X-Gm-Message-State: AB4q+uGI3CrCyp6C5wcJkIY6qg9ysG4mmGJbJJQdoUeJfBibaAUphakb U6AcEap9in2komBIj1Fc9YJvVeyjyIujHLh26SLRyKN9+hSRJqGBGo3Pu51GaBII X-Gm-Gg: AZFdTZeLc75GoqUSWfMhdiFF/bvzcWs44QoUGhmuyQP32yE+gvW6flLJs5Hfsdd38JF 9zY0CotGg2CZOT12IthPY+U/1DX2vJqgWI9W9sItuiH709gutRmyshTjQN86hDitn7++25ooPgP LcSBhbqk3r2OVxx4MflB4/T6/pnaYvG+uOlsQGS7ihlsJmldNnL3zQU57V2KChghH0FR/yCnx0H KOIxZ8aiLki5RR++sGU18wMDepglfcKvxn6DSi6J4j3zV1Lw00/z8C4YJdjxHl7EepsrBG1LhLt vklgytqsiorfk9MSDOpflI8HhNrwpuYD7wKHKXXFGbO5gfh5/bjQoTYALR3xz8glBie2h/2bk7U PXy/Gyr9KbY1d4kLdx87G8mmEWJVNTP8cj4AQl4T7l4UxsWun9xI001TYctqDvp+YI/m3B59EWJ gsCx2SnRXKpRjud5phQ0dKZyL0GVjG4GiGSJlyv36wM4PwpSikK861Vxe+HoDYtn/fVjtqG+jit dU+XTLGs16ur4GVlymbIWD3TxC2k9NZwL8TUlHrVGsTYxk93S7Zb7KTupEEsSciH0ECM0/M8yvb b4hmLMM13b13izaVvwNZhrqKlN4YmAhbDQAqZxaAu0X8qDOWiRSTM52yN66pjhEiVlFGtACPrjR 0kIrqLC73a0F0f9zh71ErEd1o7lZBI6Ktmm4jqrVJDc7fkIEb9MreoPDe1OILQuhDZ7DJfsbh2C 3gelJUC6KOHN7koXBoFYSYVUu5I4+ec2AleGnXxeCFVDFZBO0R04UVdt56Ebg/crTLIbI0cUZcZ J/nVJyOYyoMicW8bqE2/PbI1Uxb68wQAGh7ZA== 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-usb@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