From nobody Fri Dec 19 20:23:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1734059617; cv=none; d=zohomail.com; s=zohoarc; b=RxYifaW2UDOvL+DFoM7qD6o31J06dDbQf9w6QAJw4zfQ5CACIaMRqP4ko0eC1Ynuff/4wDsIqkp3hL6aNuMyiCsu6060G+maAku/hlXX7Y+0aaeperetHycYBFEMLNCaATOjq7FeoUxPC8BR9l1KxzmeOC5cJ4zRh346tcgW1bE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1734059617; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=l8ElYlV9OGZOk3kCFLf3Bzeo9NJ8glbOWpHK8TL2x80=; b=K5ix0HCwikTi05xsWj/qqEjhf7rs9D8UiAKsA+lbTc+/DMN/4JYPrKT7eFCB7b4j2kWJQ6SneuLEx8cpJA9k4rHxCmVneUePTzUOlSvM/Zk1Jc4uwCwERwJs6Ircb90YuSRhvdqHeYIYTcVBbcjHhfr19xb/nv+47dUR5XXYKE4= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17340596170597.796626363097516; Thu, 12 Dec 2024 19:13:37 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tLw6W-0001q8-2a; Thu, 12 Dec 2024 22:12:36 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tLw6U-0001oa-Do; Thu, 12 Dec 2024 22:12:34 -0500 Received: from mail.aspeedtech.com ([211.20.114.72] helo=TWMBX01.aspeed.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tLw6P-0002gT-Dk; Thu, 12 Dec 2024 22:12:33 -0500 Received: from TWMBX01.aspeed.com (192.168.0.62) by TWMBX01.aspeed.com (192.168.0.62) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.12; Fri, 13 Dec 2024 11:12:05 +0800 Received: from localhost.localdomain (192.168.10.10) by TWMBX01.aspeed.com (192.168.0.62) with Microsoft SMTP Server id 15.2.1258.12 via Frontend Transport; Fri, 13 Dec 2024 11:12:05 +0800 To: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Bin Meng , "open list:SD (Secure Card)" , "open list:All patches CC here" CC: , , Subject: [PATCH v2 1/2] hw/sd/sdhci: Fix boundary_count overflow in sdhci_sdma_transfer_multi_blocks Date: Fri, 13 Dec 2024 11:12:04 +0800 Message-ID: <20241213031205.641009-2-jamin_lin@aspeedtech.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241213031205.641009-1-jamin_lin@aspeedtech.com> References: <20241213031205.641009-1-jamin_lin@aspeedtech.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=211.20.114.72; envelope-from=jamin_lin@aspeedtech.com; helo=TWMBX01.aspeed.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_FAIL=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Jamin Lin From: Jamin Lin via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1734059619208116600 Content-Type: text/plain; charset="utf-8" How to reproduce it: 1. The value of "s->blksie" was 0x7200. The bits[14:12] was "111", so the b= uffer boundary was 0x80000.(512Kbytes). This SDMA buffer boundary was the same= as u-boot default value. The bit[11:0] was "001000000000", so the block size was 0x200.(512bytes) 2. The SDMA address was 0x83123456 which was not page aligned and "s->sdmasysad % boundary_chk" was 0x23456. The value of boundary_count w= as 0x5cbaa.("boundary_chk - (s->sdmasysad % boundary_chk)" --> "(0x80000 - 0x23456)") However, boundary_count did not align the block size 512 bytes and the SDMA address was not page aligned(0x80000), so the following if-statement never = be true, ``` if (((boundary_count + begin) < block_size) && page_aligned) ```` Finally, it caused boundary_count overflow because its data type was uint32= _t. Ex: the last boundary_count was 0x1aa and "0x1aa - 0x200" became "0xffffffa= a". It is the wrong behavior. To fix it, change to check boundary_count smaller than block size if system address did not page align Signed-off-by: Jamin Lin --- hw/sd/sdhci.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hw/sd/sdhci.c b/hw/sd/sdhci.c index 37875c02c3..f1a329fdaf 100644 --- a/hw/sd/sdhci.c +++ b/hw/sd/sdhci.c @@ -618,7 +618,7 @@ static void sdhci_sdma_transfer_multi_blocks(SDHCIState= *s) sdbus_read_data(&s->sdbus, s->fifo_buffer, block_size); } begin =3D s->data_count; - if (((boundary_count + begin) < block_size) && page_aligned) { + if (((boundary_count + begin) < block_size) && !page_aligned) { s->data_count =3D boundary_count + begin; boundary_count =3D 0; } else { @@ -634,7 +634,7 @@ static void sdhci_sdma_transfer_multi_blocks(SDHCIState= *s) if (s->data_count =3D=3D block_size) { s->data_count =3D 0; } - if (page_aligned && boundary_count =3D=3D 0) { + if (boundary_count =3D=3D 0) { break; } } @@ -642,7 +642,7 @@ static void sdhci_sdma_transfer_multi_blocks(SDHCIState= *s) s->prnsts |=3D SDHC_DOING_WRITE; while (s->blkcnt) { begin =3D s->data_count; - if (((boundary_count + begin) < block_size) && page_aligned) { + if (((boundary_count + begin) < block_size) && !page_aligned) { s->data_count =3D boundary_count + begin; boundary_count =3D 0; } else { @@ -659,7 +659,7 @@ static void sdhci_sdma_transfer_multi_blocks(SDHCIState= *s) s->blkcnt--; } } - if (page_aligned && boundary_count =3D=3D 0) { + if (boundary_count =3D=3D 0) { break; } } --=20 2.34.1