From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E20B1C44514 for ; Fri, 17 Jul 2026 07:42:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type: Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date :Subject:CC:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=NrLmXw2+grSg8BDLzNZP31Z2rCqGp6CpomTh+kmD5Sk=; b=llbJs3sBMmJkBhIb9DRVYY1TpF KI+ST0mBiFfBL17Lmu0lM0PdmF2/QUEdN9r5aoIgQeGfF/eBabOA4V0BxubN8wGjMeZHtoDWAXxHE 28AoTv+GUKbRe/Msesh3mQb+MmdlQkZpqC8CiT8h12atFCSRvfuwyo0w9io/Ooe5F3QGd9BvgNzGr eJ33JBhknf+y7TiHjvjRJV5tX/wlsalBWSai5EdBbMkHrdj1yq9HP2xXNZqXkr/R4NqCkOBFIY3j4 gA9Rn7kXU98LmykveqVq/0o7h3SEs9s5RESuhQrHlrA3I6USy7FOfuH3N/FL6/W9jKaPNMFILAhQJ le7iPlRA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wkdDK-00000001UR9-37No; Fri, 17 Jul 2026 07:42:30 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wkdDJ-00000001UQX-1RHb for linux-mediatek@bombadil.infradead.org; Fri, 17 Jul 2026 07:42:29 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Content-Transfer-Encoding :MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From: Sender:Reply-To:Content-ID:Content-Description; bh=NrLmXw2+grSg8BDLzNZP31Z2rCqGp6CpomTh+kmD5Sk=; b=CnbGa3Rp7svrF0ong9QHqPjYF8 fg3DoiersF8qTpw4JNKJIaeSnufC/P1G/+Nv5m901yOWso9RkdGpqfuikDPRbk3VMDP0FQxipYD24 1mOxU5nEuAlpQ2A+WRCMC7VkbfQfUOkl4h6ewJHjdBGIU3zTztsI0Xow2y3FI3YLH8BIh2NaPslj/ IPprDwtVYc6eGtx8v/FIpAQjGKkPLBaqI15453glNnZHCZOyeVaswLLkFG1VjrTssE2iDx21AurD9 XxuuFTf7Cd0YKKg2RnIC8QjoSqGTU7R+7PwCL26kA98Uem8sRcmxksgjlZZ8pnw+DeiXulk1AxkTJ 4XcvmhLw==; Received: from [216.200.240.185] (helo=mailgw02.mediatek.com) by desiato.infradead.org with esmtps (Exim 4.99.2 #2 (Red Hat Linux)) id 1wkdDE-0000000Ecro-46gZ for linux-mediatek@lists.infradead.org; Fri, 17 Jul 2026 07:42:27 +0000 X-UUID: 05806b7881b311f1acbe4559397dec65-20260717 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=NrLmXw2+grSg8BDLzNZP31Z2rCqGp6CpomTh+kmD5Sk=; b=UzEISW0VoUjO17TsqDpuedWwNMiwAVHRZy6jmvODRpwVPPtNUOqRAOQm0GKDZhcO8t2uZDzuueZSHfYepETQrObiw6GNjn+4P5DYm73yKp0WV9i20xzv2GpUCVRDis3t7RolXFveeY42Nu0dfOLOOMCEpFXXc0J7FzbpRlIVfq4=; X-CID-CACHE: Type:Local,Time:202607170021-07,HitQuantity:1 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.18,REQID:3446f402-a887-402d-94b6-dca5b15b0f15,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:8a735a6,CLOUDID:c5be12d7-d84b-4e52-b801-314e13fb1691,B ulkID:nil,BulkQuantity:0,SF:81|82|102|836|865|888|898,TC:-5,Content:0|15|5 0|99|200|213,EDM:-3,IP:nil,URL:0,File:130,RT:0,Bulk:nil,QS:nil,BEC:-1,COL: 0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 05806b7881b311f1acbe4559397dec65-20260717 Received: from mtkmbs14n1.mediatek.inc [(172.21.101.75)] by mailgw02.mediatek.com (envelope-from ) (musrelay.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 613517552; Fri, 17 Jul 2026 00:42:11 -0700 Received: from mtkmbs13n1.mediatek.inc (172.21.101.193) by MTKMBS14N1.mediatek.inc (172.21.101.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.29; Fri, 17 Jul 2026 15:42:08 +0800 Received: from mtksitap99.mediatek.inc (10.233.130.16) by mtkmbs13n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.2562.29 via Frontend Transport; Fri, 17 Jul 2026 15:42:08 +0800 From: Chris Lu To: Marcel Holtmann , Johan Hedberg , Luiz Von Dentz CC: Sean Wang , Will Lee , SS Wu , Steve Lee , Paul Menzel , linux-bluetooth , linux-kernel , linux-mediatek , Chris Lu Subject: [PATCH v15 2/7] Bluetooth: btmtk: Add firmware size validation in btmtk_setup_firmware_79xx() Date: Fri, 17 Jul 2026 15:42:01 +0800 Message-ID: <20260717074207.2869999-3-chris.lu@mediatek.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20260717074207.2869999-1-chris.lu@mediatek.com> References: <20260717074207.2869999-1-chris.lu@mediatek.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260717_084225_869822_96804BD3 X-CRM114-Status: GOOD ( 11.84 ) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=archiver.kernel.org@lists.infradead.org Add firmware size validation to prevent out-of-bounds access when loading truncated or malicious firmware files. Add three levels of validation: 1. Minimum size check for header and global descriptor 2. Section map bounds check with integer overflow protection using check_mul_overflow() and check_add_overflow() 3. Section data bounds check before accessing each section This matches the validation approach used in btmtk_load_cbmcu_firmware(). Signed-off-by: Chris Lu Assisted-by: Claude:Sonnet-4.5 --- drivers/bluetooth/btmtk.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/drivers/bluetooth/btmtk.c b/drivers/bluetooth/btmtk.c index 02a96342e964..3491060b3ae9 100644 --- a/drivers/bluetooth/btmtk.c +++ b/drivers/bluetooth/btmtk.c @@ -145,6 +145,7 @@ int btmtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname, int err, dlen, i, status; u8 flag, first_block, retry; u32 section_num, dl_size, section_offset; + size_t expected_size; u8 cmd[64]; err = request_firmware(&fw, fwname, &hdev->dev); @@ -153,12 +154,40 @@ int btmtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname, return err; } + /* Validate minimum firmware size for header and global descriptor */ + if (fw->size < MTK_FW_ROM_PATCH_HEADER_SIZE + MTK_FW_ROM_PATCH_GD_SIZE) { + bt_dev_err(hdev, "Firmware file too small: size=%zu, expected at least %u bytes", + fw->size, MTK_FW_ROM_PATCH_HEADER_SIZE + MTK_FW_ROM_PATCH_GD_SIZE); + err = -EINVAL; + 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); + /* Check for potential integer overflow in size calculation */ + if (check_mul_overflow((size_t)MTK_FW_ROM_PATCH_SEC_MAP_SIZE, + (size_t)section_num, &expected_size) || + check_add_overflow(expected_size, + (size_t)(MTK_FW_ROM_PATCH_HEADER_SIZE + + MTK_FW_ROM_PATCH_GD_SIZE), + &expected_size)) { + bt_dev_err(hdev, "Firmware size calculation overflow (section_num=%u)", + section_num); + err = -EINVAL; + goto err_release_fw; + } + + if (fw->size < expected_size) { + bt_dev_err(hdev, "Firmware truncated: size=%zu, expected=%zu (section_num=%u)", + fw->size, expected_size, section_num); + err = -EINVAL; + goto err_release_fw; + } + 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); @@ -171,6 +200,16 @@ int btmtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname, section_offset = le32_to_cpu(sectionmap->secoffset); dl_size = le32_to_cpu(sectionmap->bin_info_spec.dlsize); + /* Validate section boundaries to prevent out-of-bounds access */ + if (dl_size > 0 && + (section_offset > fw->size || + dl_size > fw->size - section_offset)) { + bt_dev_err(hdev, "Section %d out of bounds: offset=%u, size=%u, fw_size=%zu", + i, section_offset, dl_size, fw->size); + err = -EINVAL; + goto err_release_fw; + } + /* MT6639: only download sections where dlmode byte0 == 0x01, * matching the Windows driver behavior which skips WiFi/other * sections that would cause the chip to hang. -- 2.45.2