Received: from list by lists.gnu.org with archive (Exim 4.71) id 1VJVoo-0008Tm-Ax for mharc-grub-devel@gnu.org; Tue, 10 Sep 2013 17:49:54 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36472) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VJVoe-0008Sw-BA for grub-devel@gnu.org; Tue, 10 Sep 2013 17:49:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VJVoV-0000nQ-Og for grub-devel@gnu.org; Tue, 10 Sep 2013 17:49:44 -0400 Received: from aserp1040.oracle.com ([141.146.126.69]:34756) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VJVoV-0000nE-Is for grub-devel@gnu.org; Tue, 10 Sep 2013 17:49:35 -0400 Received: from acsinet22.oracle.com (acsinet22.oracle.com [141.146.126.238]) by aserp1040.oracle.com (Sentrion-MTA-4.3.1/Sentrion-MTA-4.3.1) with ESMTP id r8ALnXbq013736 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 10 Sep 2013 21:49:33 GMT Received: from aserz7022.oracle.com (aserz7022.oracle.com [141.146.126.231]) by acsinet22.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r8ALnWMI022247 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Tue, 10 Sep 2013 21:49:33 GMT Received: from abhmt117.oracle.com (abhmt117.oracle.com [141.146.116.69]) by aserz7022.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r8ALnWb1022240 for ; Tue, 10 Sep 2013 21:49:32 GMT Received: from bergsoft (/10.132.136.136) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 10 Sep 2013 14:49:32 -0700 Date: Tue, 10 Sep 2013 14:46:41 -0700 (PDT) From: Seth Goldberg X-X-Sender: sethg@bergsoft To: The development of GNU GRUB Subject: Re: calculation overflow in grub_mm_init_region (patch) In-Reply-To: <20130910121333.GA2737@rocoto.smurfnet.nu> Message-ID: References: <20130829172603.GP18151@rocoto.smurfnet.nu> <20130910121333.GA2737@rocoto.smurfnet.nu> User-Agent: Alpine 2.00 (GSO 1167 2008-08-23) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Source-IP: acsinet22.oracle.com [141.146.126.238] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 141.146.126.69 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 Sep 2013 21:49:53 -0000 Quoting Leif Lindholm, who wrote the following on Tue, 10 Sep 2013: > On Thu, Aug 29, 2013 at 07:26:03PM +0200, Leif Lindholm wrote: >> When allocating memory for the heap on ARMv7 UEFI, the init code >> pretty much just allocates a chunk from the top of available RAM. >> >> This means that when grub_mm_init_region is called for a region >> extending to the top of the 32-bit address space, addr + size == 0. >> However, this is not taken into account by arithmetic in this >> function, causing Grub to fail on one of my platforms[1] before >> "Welcome to GRUB". >> >> Having some trouble getting my head around the grub_mm_init_region >> code right now (where is grub_mm_base initialised?), so don't have >> a patch. > > So, sat down to dissect the code a bit, and turns out that apart from > a debug message (which is #ifdef 0 by default), grub_mm_init_region() > is actually innocent. > > The error lies in get_header_from_pointer() (also kern/mm.c), and its > comparisons regarding whether a pointer lies within the current > region or not. > > My suggested fix is as follows (with a little bit of refactoring to > help my brain). > > / > Leif > > === modified file 'grub-core/kern/mm.c' > --- grub-core/kern/mm.c 2013-04-20 15:39:49 +0000 > +++ grub-core/kern/mm.c 2013-09-10 11:13:36 +0000 > @@ -86,13 +86,20 @@ > static void > get_header_from_pointer (void *ptr, grub_mm_header_t *p, grub_mm_region_t *r) > { > + grub_addr_t block_start = (grub_addr_t) ptr; > + > if ((grub_addr_t) ptr & (GRUB_MM_ALIGN - 1)) > grub_fatal ("unaligned pointer %p", ptr); > > for (*r = grub_mm_base; *r; *r = (*r)->next) > - if ((grub_addr_t) ptr > (grub_addr_t) ((*r) + 1) > - && (grub_addr_t) ptr <= (grub_addr_t) ((*r) + 1) + (*r)->size) > - break; > + { > + grub_addr_t region_start = (grub_addr_t) ((*r) + 1); > + grub_addr_t region_end = (grub_addr_t) ((*r) + 1) + (*r)->size; > + > + if (block_start > region_start) > + if ((block_start <= region_end) || (region_end == 0)) Why would region_end be zero? That sounds like an invalid value. --S