From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796385083; b=0Be+naawrgua1O7VpJLKX6tO/QwfB8iJbKqVNmaQz11kMEzzUe4k+UwOS230YgQGSEoR N1pasG1YlkISgYdoHMeRQzSBlLQPzwQT3MPqCcbfZfAGUrY3oTeSC9FzJZHg/vt1z81GJ gqPbN+VQsCeIpUZJiZCy1gE1nH7uK+S+A7Qf/gBkWD5o/CCyJNT1HOi/6og/+RCl/ys7q 0NQG144CtxSkQjo800eGU9CibJKC8bC1mf0Bb+fEMEuOEzGSS9xTnSj8Vpl57y5HhQwdQ ZBRbqHuyMJ8B7tI6FW7Vv/pWwaVzs3yqABA6NVb7kKp6Ho0IHTGE0Hym0O+MmEnHBpg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796385083; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=wHlYIieY4rJFJx9+7Sy58ZnQBB8vhUVSr4oHA5AxHWo=; b=QrFgIl3IWY6U4iXBeGC+zmIqIBbYorKGaUlQDK3qM1BkvWoTYUuD/YgWj4PUEad8AVzO F7cmGpH8L8IwCGpOjsk3Hvx4kznIlnzTPC9sOmfxV6cLSiGa0m3HqdfBPf6R1pXlU88g1 q0tamfEKX5GKPIEcIpvYdDfCNmhgjfcH8ZRaYSmDwvdI8ZLFmDTbuwb+3MivXmx/1sGzd Q5W/gJnD2FAxlUrG00Zz8gJNZU8J1Opa+oNUEDsUOu9hQHUx3ovr6SbXe9qz1k6iiTLUZ VEpHcNp4k8byRbqajnXsivVpps51oXJKajPNqSrDwPQ61C55wIgeUreq9y7KjEVw5qw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=In3JwOmT; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 16677606A680; Fri, 4 Dec 2026 11:51:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=In3JwOmT Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F5801F00936; Fri, 4 Dec 2026 11:51:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796385082; bh=wHlYIieY4rJFJx9+7Sy58ZnQBB8vhUVSr4oHA5AxHWo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=In3JwOmTO/+IM0t3nOnVYQjy6ybNSvRz9FMXLlCLeqzpoKQFTdfJUCQ9H2MYuDN6f iPlGCaOyh/8aAWAWeFMp/ZnQZul6k2pCgMVx7aYjANpSSNp0sgbiFtyTIWdhD8Y4Ie kmtH/bCyH5p/GfEGdxerCH2xoCEnr3LZuZIl+QVc= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Chris Lu , Luiz Augusto von Dentz , Sasha Levin Subject: [PATCH 6.6 0410/1167] Bluetooth: btmtk: Validate the firmware layout before parsing it Date: Fri, 4 Dec 2026 18:57:17 +0900 Message-ID: <20261204100712.702682145@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@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 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Chris Lu [ Upstream commit 760ac9d02dce001f2e20c17deb18e7bb63d8355e ] btmtk_setup_firmware_79xx() reads the section count out of the firmware's global descriptor, then walks a 64-byte section map per section and hands each section's file-supplied offset and length to the WMT download path. None of that is checked against fw->size, so a truncated or corrupted file makes the driver read past the end of request_firmware()'s buffer. On 32-bit the section count can also wrap the section map size calculation. Add btmtk_fw_validate_layout() to check that the header, global descriptor and section map array all fit, and btmtk_fw_validate_section() to check one section's offset and length, both written so the size arithmetic cannot wrap. A section count of zero is rejected as well: it passes every size check but leaves the download loop with nothing to do, so the function would report success on hardware it never programmed. They are helpers rather than open-coded because the MT7928 CBMCU download path added later needs the same arithmetic. This is hardening against a corrupted /lib/firmware rather than a bug an unprivileged user can trigger, hence bluetooth-next and no Fixes: tag. Well-formed firmware files are unaffected. Signed-off-by: Chris Lu Assisted-by: Claude:claude-opus-5 Signed-off-by: Luiz Augusto von Dentz [ sashal: Adjusted the include to the older name and dropped the dev_id parameter and MT6639 context that this tree lacks; the validation logic is unchanged. ] Signed-off-by: Sasha Levin --- drivers/bluetooth/btmtk.c | 74 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 3 deletions(-) diff --git a/drivers/bluetooth/btmtk.c b/drivers/bluetooth/btmtk.c index 9ab4157be92f9..bf894abd13ee4 100644 --- a/drivers/bluetooth/btmtk.c +++ b/drivers/bluetooth/btmtk.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -56,6 +57,67 @@ struct btmtk_section_map { }; } __packed; +/* Reject a firmware image whose header, global descriptor or section map + * array does not fit in the file, and hand back the validated section + * count, so that the section map walks stay inside the firmware buffer. + */ +static int btmtk_fw_validate_layout(struct hci_dev *hdev, + const struct firmware *fw, + u32 *section_num) +{ + const struct btmtk_global_desc *globaldesc; + size_t need; + + if (fw->size < MTK_FW_ROM_PATCH_HEADER_SIZE + MTK_FW_ROM_PATCH_GD_SIZE) { + bt_dev_err(hdev, "Firmware too small: %zu bytes, need at least %u", + fw->size, + MTK_FW_ROM_PATCH_HEADER_SIZE + MTK_FW_ROM_PATCH_GD_SIZE); + return -EINVAL; + } + + globaldesc = (const struct btmtk_global_desc *)(fw->data + + MTK_FW_ROM_PATCH_HEADER_SIZE); + *section_num = le32_to_cpu(globaldesc->section_num); + + if (!*section_num) { + bt_dev_err(hdev, "Firmware declares no sections"); + return -EINVAL; + } + + if (check_mul_overflow((size_t)MTK_FW_ROM_PATCH_SEC_MAP_SIZE, + (size_t)*section_num, &need) || + check_add_overflow(need, + (size_t)(MTK_FW_ROM_PATCH_HEADER_SIZE + + MTK_FW_ROM_PATCH_GD_SIZE), + &need)) { + bt_dev_err(hdev, "Firmware section count too large: %u", + *section_num); + return -EINVAL; + } + + if (fw->size < need) { + bt_dev_err(hdev, "Firmware truncated: %zu bytes, need %zu for %u sections", + fw->size, need, *section_num); + return -EINVAL; + } + + return 0; +} + +/* Reject a section whose payload does not lie within the firmware file. */ +static int btmtk_fw_validate_section(struct hci_dev *hdev, + const struct firmware *fw, int index, + u32 offset, u32 size) +{ + if (offset > fw->size || size > fw->size - offset) { + bt_dev_err(hdev, "Firmware section %d out of bounds: offset=%u, size=%u, fw_size=%zu", + index, offset, size, fw->size); + return -EINVAL; + } + + return 0; +} + static void btmtk_coredump(struct hci_dev *hdev) { int err; @@ -129,7 +191,6 @@ int btmtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname, { struct btmtk_hci_wmt_params wmt_params; struct btmtk_patch_header *hdr; - struct btmtk_global_desc *globaldesc = NULL; struct btmtk_section_map *sectionmap; const struct firmware *fw; const u8 *fw_ptr; @@ -145,11 +206,13 @@ int btmtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname, return err; } + err = btmtk_fw_validate_layout(hdev, fw, §ion_num); + if (err < 0) + goto err_release_fw; + fw_ptr = fw->data; fw_bin_ptr = fw_ptr; hdr = (struct btmtk_patch_header *)fw_ptr; - globaldesc = (struct btmtk_global_desc *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE); - section_num = le32_to_cpu(globaldesc->section_num); bt_dev_info(hdev, "HW/SW Version: 0x%04x%04x, Build Time: %s", le16_to_cpu(hdr->hwver), le16_to_cpu(hdr->swver), hdr->datetime); @@ -164,6 +227,11 @@ int btmtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname, dl_size = le32_to_cpu(sectionmap->bin_info_spec.dlsize); if (dl_size > 0) { + err = btmtk_fw_validate_section(hdev, fw, i, + section_offset, dl_size); + if (err < 0) + goto err_release_fw; + retry = 20; while (retry > 0) { cmd[0] = 0; /* 0 means legacy dl mode. */ -- 2.53.0