Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1NXPIX-0000FJ-GB for mharc-grub-devel@gnu.org; Tue, 19 Jan 2010 20:23:53 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NXPIW-0000Ek-FB for grub-devel@gnu.org; Tue, 19 Jan 2010 20:23:52 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NXPIQ-0000BS-QJ for grub-devel@gnu.org; Tue, 19 Jan 2010 20:23:51 -0500 Received: from [199.232.76.173] (port=48864 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NXPIQ-0000BM-LW for grub-devel@gnu.org; Tue, 19 Jan 2010 20:23:46 -0500 Received: from hurricane.eginity.com ([207.218.123.225]:43465 helo=zazu.eginity.com) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NXPIQ-0005S0-64 for grub-devel@gnu.org; Tue, 19 Jan 2010 20:23:46 -0500 Received: from [192.168.1.51] (208.eginity.com [207.218.123.208]) (Authenticated sender: dmerillat@eginity.com) by zazu.eginity.com (Postfix) with ESMTPSA id 2E97A4924 for ; Tue, 19 Jan 2010 20:23:45 -0500 (EST) Message-ID: <4B565B20.9050603@dan.merillat.org> Date: Tue, 19 Jan 2010 20:23:44 -0500 From: Dan Merillat User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090707) MIME-Version: 1.0 To: GNU GRUB Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Subject: 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 01:23:52 -0000 Yo Grub2, I'm really happy for you and I'mma let you finish but... allocating sizeof(p->next) instead of sizeof(*p) is the worst idiom of all time. OF ALL TIME. Diff against debian's 1.98-experimental-20100111, to which I submit the following patches: 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. On 64bit, the structure is 16 bytes long, and trashes whatever the next allocation is, which leads to nasty problems and random crashes later. Electric Fence. Learn it, love it, live it. And here's a freebie to avoid a null pointer deref that -lefence pointed out: diff --git a/util/grub-probe.c b/util/grub-probe.c index 4ba8a98..acb0887 100644 --- a/util/grub-probe.c +++ b/util/grub-probe.c @@ -94,6 +94,8 @@ probe_partmap (grub_disk_t disk) static int probe_raid_level (grub_disk_t disk) { + if (!disk) + return -1; if (disk->dev->id != GRUB_DISK_DEVICE_RAID_ID) return -1; Someone may want to look into why probe_raid_level can get passed an invalid disk pointer, but the combo of these two patches makes it work properly. I don't know if it's possible to have a disk pointer without a dev pointer, but that's a question for people with some deeper knowledge. This may fix some of the other bug reports - I see stacktraces crossing grub_device_iterate which tells me that this is a potential corruption in all of them.