Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1NXQ41-0002dc-85 for mharc-grub-devel@gnu.org; Tue, 19 Jan 2010 21:12:57 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NXQ40-0002dR-4p for grub-devel@gnu.org; Tue, 19 Jan 2010 21:12:56 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NXQ3v-0002cL-FB for grub-devel@gnu.org; Tue, 19 Jan 2010 21:12:55 -0500 Received: from [199.232.76.173] (port=40008 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NXQ3v-0002cH-7R for grub-devel@gnu.org; Tue, 19 Jan 2010 21:12:51 -0500 Received: from smarthost03.mail.zen.net.uk ([212.23.3.142]:34400) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NXQ3v-0000uD-01 for grub-devel@gnu.org; Tue, 19 Jan 2010 21:12:51 -0500 Received: from [82.69.40.219] (helo=riva.pelham.vpn.ucam.org) by smarthost03.mail.zen.net.uk with esmtp (Exim 4.63) (envelope-from ) id 1NXQ3t-0006EE-SI for grub-devel@gnu.org; Wed, 20 Jan 2010 02:12:50 +0000 Received: from cjwatson by riva.pelham.vpn.ucam.org with local (Exim 3.36 #1 (Debian)) for grub-devel@gnu.org id 1NXQ3t-0004ie-00; Wed, 20 Jan 2010 02:12:49 +0000 Date: Wed, 20 Jan 2010 02:12:49 +0000 From: Colin Watson To: The development of GNU GRUB Message-ID: <20100120021249.GP5847@riva.ucam.org> References: <4B565B20.9050603@dan.merillat.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4B565B20.9050603@dan.merillat.org> User-Agent: Mutt/1.5.18 (2008-05-17) X-Originating-Smarthost03-IP: [82.69.40.219] X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Subject: Re: segfault in grub_device_iterate on 64bit platforms X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 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: Wed, 20 Jan 2010 02:12:56 -0000 On Tue, Jan 19, 2010 at 08:23:44PM -0500, Dan Merillat wrote: > diff --git a/kern/device.c b/kern/device.c > index d4de496..3727ddc 100644 > --- a/kern/device.c > +++ b/kern/device.c > @@ -139,7 +139,7 @@ grub_device_iterate (int (*hook) (const char *name)) > if (! partition_name) > return 1; > > - p = grub_malloc (sizeof (p->next)); > + p = grub_malloc (sizeof (*p)); > if (!p) > { > grub_free (partition_name); > > > > Seriously though, where did someone see sizeof(p->next) and think it was > a valid idiom? It allocates a pointer, not a structure. I've never > seen it used before, and a quick rgrep shows it to be an anomaly in the > grub source as well. It happens to work on 32bit processors for tiny > structures because the default alignment for malloc is 8 bytes - and > struct part_ent is two pointers. You end up overflowing into the > padding and not trashing anything. I think you're a bit overly critical here, although the patch is still sound. Here's the code from current trunk: p = grub_malloc (sizeof (p->next) + grub_strlen (disk->name) + 1 + grub_strlen (partition_name) + 1); sizeof (p->next) is perfectly reasonable on the face of it because the structure being allocated is: struct part_ent { struct part_ent *next; char name[0]; }; To allocate a new instance of this structure, it does look at a quick glance as though we need the size of the next pointer, plus however much is going to be needed for name; so I can easily understand why it was written this way and I think you're going a bit overboard in your criticism. The reason that it breaks is, of course, because of padding within the struct needed to ensure alignment. Easily forgotten. > Electric Fence. Learn it, love it, live it. Valgrind this decade, surely? ;-) I've committed your patches now. A ChangeLog entry would speed up the process for next time. :-) -- Colin Watson [cjwatson@ubuntu.com]