Received: from list by lists.gnu.org with archive (Exim 4.71) id 1VJMpK-000607-Pz for mharc-grub-devel@gnu.org; Tue, 10 Sep 2013 08:13:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41221) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VJMpC-0005yl-DK for grub-devel@gnu.org; Tue, 10 Sep 2013 08:13:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VJMp6-0007OY-Do for grub-devel@gnu.org; Tue, 10 Sep 2013 08:13:42 -0400 Received: from mail-ee0-f54.google.com ([74.125.83.54]:52764) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VJMp6-0007OR-7O for grub-devel@gnu.org; Tue, 10 Sep 2013 08:13:36 -0400 Received: by mail-ee0-f54.google.com with SMTP id e53so3909472eek.27 for ; Tue, 10 Sep 2013 05:13:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-type:content-disposition:in-reply-to :user-agent; bh=ln7+nksNsMEPY1WCsIYYsfgzBMW3jcVBRXwQ9x59Wd4=; b=Zii7cZkQNjcSG5P9bAnPPiJxHvMUMSqXIq6MhA3S5poL/rTzljz40RaLE5XatgtwpY 1+/U4ONs58M7wzmTwLRLc6uYlHkQzW5/+MAKWrTxNEaE8lMQh95ZHmdjFbRrAcfFqnia N4z7o6dCfrZmh/SdN0qvIi5rVwfSLCPDV1/fgTi7KwtHfZ+vNKbAXGgn7CxJfCW5ugc5 ox5yU57khH7m+kB1jowPFv/mrDxnFk9q/urRlYEetWpHa1vYmtTjJPUDjQCs/7g4D4vZ yNEEXGLbCdN4nfmkbOcjsJ/3aNKkqba4m/nVbvCkq3n3RAdB+F5lkodEvpmaNYxKC18c 0Bvw== X-Gm-Message-State: ALoCoQlOLhadF/Fcwx4B8dkCN76q1a1qz5FODdvjmk2GpvXV+ngRK6dQfMQ81vh/azo4C8Olfz91 X-Received: by 10.14.8.72 with SMTP id 48mr38787146eeq.25.1378815215085; Tue, 10 Sep 2013 05:13:35 -0700 (PDT) Received: from rocoto.smurfnet.nu ([2001:4b98:dc0:41:216:3eff:fea7:75f3]) by mx.google.com with ESMTPSA id a43sm30856194eep.9.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 10 Sep 2013 05:13:34 -0700 (PDT) Date: Tue, 10 Sep 2013 14:13:34 +0200 From: Leif Lindholm To: grub-devel@gnu.org Subject: Re: calculation overflow in grub_mm_init_region (patch) Message-ID: <20130910121333.GA2737@rocoto.smurfnet.nu> References: <20130829172603.GP18151@rocoto.smurfnet.nu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20130829172603.GP18151@rocoto.smurfnet.nu> User-Agent: Mutt/1.5.21 (2010-09-15) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 74.125.83.54 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 12:13:48 -0000 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)) + break; + } if (! *r) grub_fatal ("out of range pointer %p", ptr);