From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 604BDAC17B2 for ; Tue, 7 Nov 2023 17:39:24 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=4wl76YKbaQM0YYmsGL1KVDSc9LiZASdyY1y2coTl3Sg=; c=relaxed/simple; d=groups.io; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From:In-Reply-To:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Language:Content-Type:Content-Transfer-Encoding; s=20140610; t=1699378762; v=1; b=dsoAD2EH3fj917NMZv53qQFEFiQR4uUm5bhOcz4cAz3z+4C0apYN1Q3e1DvCqCOMcz9AjJyy 4cwIrUBllH7WdV+2cLPq082u4zxf4oNz/R/D0Dqx11KGeQ0/UbH/nlEFYXLUcEfQKqUOMCYXiak 8sVHWVv4SkVJULERof9HfFcs= X-Received: by 127.0.0.2 with SMTP id UlWZYY7687511xEGtu3S66xY; Tue, 07 Nov 2023 09:39:22 -0800 X-Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.groups.io with SMTP id smtpd.web10.1116.1699378762034285377 for ; Tue, 07 Nov 2023 09:39:22 -0800 X-Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-194-9c3aK5wQOgqVTDPqLkdyYQ-1; Tue, 07 Nov 2023 12:39:18 -0500 X-MC-Unique: 9c3aK5wQOgqVTDPqLkdyYQ-1 X-Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 982A23C1F122; Tue, 7 Nov 2023 17:39:17 +0000 (UTC) X-Received: from [10.39.193.64] (unknown [10.39.193.64]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6374325C0; Tue, 7 Nov 2023 17:39:16 +0000 (UTC) Message-ID: Date: Tue, 7 Nov 2023 18:39:13 +0100 MIME-Version: 1.0 Subject: Re: [edk2-devel] [PATCH v2 2/2] FatPkg/EnhancedFatDxe: Fix OVERFLOW_BEFORE_WIDEN Coverity issue To: devel@edk2.groups.io, rsingh@ventanamicro.com Cc: Ray Ni , Veeresh Sangolli References: <20231107062842.116670-1-rsingh@ventanamicro.com> <20231107062842.116670-3-rsingh@ventanamicro.com> From: "Laszlo Ersek" In-Reply-To: <20231107062842.116670-3-rsingh@ventanamicro.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,lersek@redhat.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: wivbaEPzzJYqXpA7s7apHB4Bx7686176AA= Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=dsoAD2EH; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=redhat.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io On 11/7/23 07:28, Ranbir Singh wrote: > From: Ranbir Singh >=20 > The function FatInitializeDiskCache evaluates an expression >=20 > FAT_DATACACHE_GROUP_COUNT << DiskCache[CacheData].PageAlignment >=20 > and assigns it to DataCacheSize which is of type UINTN. >=20 > As per Coverity report, > FAT_DATACACHE_GROUP_COUNT << DiskCache[CacheData].PageAlignment is a > potentially overflowing expression with type "int" (32 bits, signed) > evaluated using 32-bit arithmetic, and then used in a context that > expects an expression of type "UINTN" (64 bits, unsigned). >=20 > To avoid overflow, cast "FAT_DATACACHE_GROUP_COUNT" to type "UINTN". >=20 > REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4249 >=20 > Cc: Ray Ni > Co-authored-by: Veeresh Sangolli > Signed-off-by: Ranbir Singh > Signed-off-by: Ranbir Singh > --- > FatPkg/EnhancedFatDxe/DiskCache.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) >=20 > diff --git a/FatPkg/EnhancedFatDxe/DiskCache.c b/FatPkg/EnhancedFatDxe/Di= skCache.c > index d1a34a6a646f..d56e338586d8 100644 > --- a/FatPkg/EnhancedFatDxe/DiskCache.c > +++ b/FatPkg/EnhancedFatDxe/DiskCache.c > @@ -477,7 +477,7 @@ FatInitializeDiskCache ( > DiskCache[CacheFat].BaseAddress =3D Volume->FatPos; > DiskCache[CacheFat].LimitAddress =3D Volume->FatPos + Volume->FatSize= ; > FatCacheSize =3D FatCacheGroupCount << DiskCache[= CacheFat].PageAlignment; > - DataCacheSize =3D FAT_DATACACHE_GROUP_COUNT << Dis= kCache[CacheData].PageAlignment; > + DataCacheSize =3D (UINTN)FAT_DATACACHE_GROUP_COUNT= << DiskCache[CacheData].PageAlignment; > // > // Allocate the Fat Cache buffer > // I don't understand Coverity here. This is the larger context (extract): // // Configure the parameters of disk cache // if (Volume->FatType =3D=3D Fat12) { DiskCache[CacheData].PageAlignment =3D FAT_DATACACHE_PAGE_MIN_ALIGNMENT= ; } else { DiskCache[CacheData].PageAlignment =3D FAT_DATACACHE_PAGE_MAX_ALIGNMENT= ; } DataCacheSize =3D FAT_DATACACHE_GROUP_COUNT << DiskCa= che[CacheData].PageAlignment; In practice, one of two things can happen: either DataCacheSize =3D 64 << 13; or DataCacheSize =3D 64 << 16; The larger value is 2 MB, it happily fits in an INT32. I don't mind the patch, but the commit message, and a new code comment as w= ell, should state that we're only casting to shut up Coverity. If the shift count "DiskCache[CacheData].PageAlignment" were *indeed* unbou= nded, then we'd have much more serious problems. First, we could shift by 6= 4 bits or more, which is undefined even if we cast to UINT64 at first. Seco= nd, even assuming "DataCacheSize" can be calculated correctly, just below i= t we have CacheBuffer =3D AllocateZeroPool (FatCacheSize + DataCacheSize); where the *addition* can nicely overflow regardless. The patch is OK but it requires a comment, and a commit message update. Thanks Laszlo -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110869): https://edk2.groups.io/g/devel/message/110869 Mute This Topic: https://groups.io/mt/102438365/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/leave/12367111/7686176/19134562= 12/xyzzy [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-