From nobody Sat May 18 05:34:45 2024 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1713963293233781.1746477728794; Wed, 24 Apr 2024 05:54:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rzc8d-0002na-Gp; Wed, 24 Apr 2024 08:54:15 -0400 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 1rzc8b-0002n2-2N; Wed, 24 Apr 2024 08:54:13 -0400 Received: from new-mail.astralinux.ru ([51.250.53.164]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rzc8Y-0007U8-SA; Wed, 24 Apr 2024 08:54:12 -0400 Received: from rbta-msk-lt-302690.astralinux.ru (unknown [10.177.236.253]) by new-mail.astralinux.ru (Postfix) with ESMTPA id 4VPf7S5hhRzqSQj; Wed, 24 Apr 2024 15:53:56 +0300 (MSK) From: Alexandra Diupina To: Alistair Francis Cc: Alexandra Diupina , "Konrad, Frederic" , "Edgar E. Iglesias" , Peter Maydell , qemu-arm@nongnu.org, qemu-devel@nongnu.org, sdl.qemu@linuxtesting.org Subject: [PATCH v2 RFC] fix host-endianness bug and prevent overflow Date: Wed, 24 Apr 2024 15:53:24 +0300 Message-Id: <20240424125324.1628-1-adiupina@astralinux.ru> X-Mailer: git-send-email 2.30.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-DrWeb-SpamScore: -100 X-DrWeb-SpamState: legit X-DrWeb-SpamDetail: gggruggvucftvghtrhhoucdtuddrgedvfedrvdehuddgtddvucetufdoteggodetrfcurfhrohhfihhlvgemucfftfghgfeunecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeetlhgvgigrnhgurhgrucffihhuphhinhgruceorgguihhuphhinhgrsegrshhtrhgrlhhinhhugidrrhhuqeenucggtffrrghtthgvrhhnpeefkedufedvkeffuedtgfdugeeutdegvdffffejfffgleffieduhfejvdelffdvudenucffohhmrghinheplhhinhhugihtvghsthhinhhgrdhorhhgnecukfhppedutddrudejjedrvdefiedrvdehfeenucfrrghrrghmpehhvghloheprhgsthgrqdhmshhkqdhlthdqfedtvdeiledtrdgrshhtrhgrlhhinhhugidrrhhupdhinhgvthepuddtrddujeejrddvfeeirddvheefmeefjedufeekpdhmrghilhhfrhhomheprgguihhuphhinhgrsegrshhtrhgrlhhinhhugidrrhhupdhnsggprhgtphhtthhopeekpdhrtghpthhtoheprghlihhsthgrihhrsegrlhhishhtrghirhdvfedrmhgvpdhrtghpthhtoheprgguihhuphhinhgrsegrshhtrhgrlhhinhhugidrrhhupdhrtghpthhtohephfhrvgguvghrihgtrdfmohhnrhgrugesrghmugdrtghomhdprhgtphhtthhopegvughgrghrrdhighhlvghsihgrshesghhmrghilhdrtghomhdprhgtphhtth hopehpvghtvghrrdhmrgihuggvlhhlsehlihhnrghrohdrohhrghdprhgtphhtthhopehqvghmuhdqrghrmhesnhhonhhgnhhurdhorhhgpdhrtghpthhtohepqhgvmhhuqdguvghvvghlsehnohhnghhnuhdrohhrghdprhgtphhtthhopehsughlrdhqvghmuheslhhinhhugihtvghsthhinhhgrdhorhhg X-DrWeb-SpamVersion: Vade Retro 01.423.251#02 AS+AV+AP Profile: DRWEB; Bailout: 300 X-AntiVirus: Checked by Dr.Web [MailD: 11.1.19.2307031128, SE: 11.1.12.2210241838, Core engine: 7.00.62.01180, Virus records: 12628564, Updated: 2024-Apr-24 10:54:44 UTC] 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=51.250.53.164; envelope-from=adiupina@astralinux.ru; helo=new-mail.astralinux.ru 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, SPF_HELO_NONE=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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1713963295888100003 Content-Type: text/plain; charset="utf-8" Add a type cast and use extract64() instead of extract32() to avoid integer overflow on addition. Fix bit fields extraction according to documentation. Also fix host-endianness bug by swapping desc fields from guest memory order to host memory order after dma_memory_read(). Found by Linux Verification Center (linuxtesting.org) with SVACE. Fixes: d3c6369a96 ("introduce xlnx-dpdma") Signed-off-by: Alexandra Diupina --- hw/dma/xlnx_dpdma.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/hw/dma/xlnx_dpdma.c b/hw/dma/xlnx_dpdma.c index dd66be5265..d22b942274 100644 --- a/hw/dma/xlnx_dpdma.c +++ b/hw/dma/xlnx_dpdma.c @@ -175,24 +175,24 @@ static uint64_t xlnx_dpdma_desc_get_source_address(DP= DMADescriptor *desc, =20 switch (frag) { case 0: - addr =3D desc->source_address - + (extract32(desc->address_extension, 16, 12) << 20); + addr =3D (uint64_t)desc->source_address + + (extract64(desc->address_extension, 16, 16) << 32); break; case 1: - addr =3D desc->source_address2 - + (extract32(desc->address_extension_23, 0, 12) << 8); + addr =3D (uint64_t)desc->source_address2 + + (extract64(desc->address_extension_23, 0, 16) << 32); break; case 2: - addr =3D desc->source_address3 - + (extract32(desc->address_extension_23, 16, 12) << 20); + addr =3D (uint64_t)desc->source_address3 + + (extract64(desc->address_extension_23, 16, 16) << 32); break; case 3: - addr =3D desc->source_address4 - + (extract32(desc->address_extension_45, 0, 12) << 8); + addr =3D (uint64_t)desc->source_address4 + + (extract64(desc->address_extension_45, 0, 16) << 32); break; case 4: - addr =3D desc->source_address5 - + (extract32(desc->address_extension_45, 16, 12) << 20); + addr =3D (uint64_t)desc->source_address5 + + (extract64(desc->address_extension_45, 16, 16) << 32); break; default: addr =3D 0; @@ -660,6 +660,24 @@ size_t xlnx_dpdma_start_operation(XlnxDPDMAState *s, u= int8_t channel, break; } =20 + /* Convert from LE into host endianness. */ + desc.control =3D le32_to_cpu(desc.control); + desc.descriptor_id =3D le32_to_cpu(desc.descriptor_id); + desc.xfer_size =3D le32_to_cpu(desc.xfer_size); + desc.line_size_stride =3D le32_to_cpu(desc.line_size_stride); + desc.timestamp_lsb =3D le32_to_cpu(desc.timestamp_lsb); + desc.timestamp_msb =3D le32_to_cpu(desc.timestamp_msb); + desc.address_extension =3D le32_to_cpu(desc.address_extension); + desc.next_descriptor =3D le32_to_cpu(desc.next_descriptor); + desc.source_address =3D le32_to_cpu(desc.source_address); + desc.address_extension_23 =3D le32_to_cpu(desc.address_extension_2= 3); + desc.address_extension_45 =3D le32_to_cpu(desc.address_extension_4= 5); + desc.source_address2 =3D le32_to_cpu(desc.source_address2); + desc.source_address3 =3D le32_to_cpu(desc.source_address3); + desc.source_address4 =3D le32_to_cpu(desc.source_address4); + desc.source_address5 =3D le32_to_cpu(desc.source_address5); + desc.crc =3D le32_to_cpu(desc.crc); + xlnx_dpdma_update_desc_info(s, channel, &desc); =20 #ifdef DEBUG_DPDMA --=20 2.30.2