segfault in grub_device_iterate on 64bit platforms
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.
</Kayne West>
Diff against debian's 1.98-experimental-20100111, to which I submit the following patches:
kern/device.c +1 -1
@@ -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:
util/grub-probe.c +2 -0
@@ -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.
Quoted 8 lines · Dan Merillat
And here's a freebie to avoid a null pointer deref that -lefence pointed out:
util/grub-probe.c +0
-0
util/grub-probe.c probe_partmap (grub_disk_t disk) · line 100
Quoted 7 lines
util/grub-probe.c +2
-0@@ -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 ;
(Nit: stick to the prevailing whitespace style.)
Quoted 4 lines
Someone may want to look into why
probe_raid_levelcan 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.
probe_raid_level is called in two places: one on dev->disk from grub_device_open, and one on each member list->disk from dev->disk->dev->memberlist (dev->disk). grub_device_open guarantees either !dev or dev->disk, so we must be looking at the latter, and the membership function pointer here is always grub_lvm_memberlist.
grub_lvm_memberlist just constructs its return value from lv->vg->pvs, so we'll need to look at how that's constructed. The only time pv->disk is assigned a non-NULL value is in grub_lvm_scan_device:
/* Match the device we are currently reading from with the right
PV. */
if (vg->pvs)
for (pv = vg->pvs; pv; pv = pv->next)
{
if (! grub_memcmp (pv->id, pv_id, GRUB_LVM_ID_STRLEN))
{
/* This could happen to LVM on RAID, pv->disk points to the
raid device, we shouldn't change it. */
if (! pv->disk)
pv->disk = grub_disk_open (name);
break;
}
}So it's possible for pv->disk to be NULL if that PV doesn't have an LVM signature (as I interpret this). That's either an error condition or an indication that we aren't managing to understand LVM well enough. If you can reproduce this it would be useful if you could clarify which.
In the meantime, my gut feel is that grub_lvm_memberlist is reporting the member list accurately, and since grub-probe is the thing that cares whether the disk member is non-NULL, it should also test that. It doesn't much matter whether we do this in probe or in probe_raid_level.
Which is all a laborious way of saying that I think your patch is OK, although I'm going to add a comment to explain why. Thanks - I'll apply this and your other patch in just a moment.
Signature · 1 line
Colin Watson [[email protected]]
Quoted 16 lines · Dan Merillat
kern/device.c +1
-1@@ -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);
Quoted 7 lines
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 structpart_entis 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.
Quoted 1 line
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. :-)
Signature · 1 line
Colin Watson [[email protected]]
Quoted 10 lines · Colin Watson
Quoted 7 lines
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 structpart_entis 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:
I thought the Kayne West reference would have indicated that it was mostly tongue in cheek. The question was more serious than critical though - is there code out there that uses that for linked lists? I've never seen that particular idiom, but it wouldn't be the first time something that just happens to work has been passed on as gospel.
Quoted 11 lines
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]; };
Previously, the code looked like this:
struct part_ent
{
struct part_ent *next;
char *name;} *ents;
...
Two pointers, with ->name referencing the return from asprintf:
p = grub_malloc(sizeof(p->next));
...
p->name=grub_asprintf ("%s,%s", disk->name, partition_name);
The bug was that you only were allocating the first pointer and not the char *. which happened to work on x86 due to malloc padding everything to 8 bytes.
Quoted 8 lines
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.
Actually, it was a flat-out bug on every processor before, hence my criticism. The current code has a very subtle bug on some unusual systems with alignment larger than the pointer size. - Worse, it'd only show up for certain length strings!
Quoted 1 line
I've committed your patches now.
If it looks like:
p = grub_malloc (sizeof (*p) + grub_strlen (disk->name) + 1 +
grub_strlen (partition_name) + 1);then it should be correct.
Quoted 5 lines · Colin Watson
Quoted 2 lines · Dan Merillat
And here's a freebie to avoid a null pointer deref that -lefence pointed out:
(Nit: stick to the prevailing whitespace style.)
:) Glad that's the worst problem with my submission.
Quoted 19 lines
grub_lvm_memberlistjust constructs its return value fromlv->vg->pvs, so we'll need to look at how that's constructed. The only timepv->diskis assigned a non-NULL value is ingrub_lvm_scan_device:/* Match the device we are currently reading from with the right PV. */ if (vg->pvs) for (pv = vg->pvs; pv; pv = pv->next) { if (! grub_memcmp (pv->id, pv_id, GRUB_LVM_ID_STRLEN)) { /* This could happen to LVM on RAID, pv->disk points to the raid device, we shouldn't change it. */ if (! pv->disk) pv->disk = grub_disk_open (name); break; } }
Quoted 4 lines
So it's possible for
pv->diskto be NULL if that PV doesn't have an LVM signature (as I interpret this). That's either an error condition or an indication that we aren't managing to understand LVM well enough. If you can reproduce this it would be useful if you could clarify which.
Ok, here goes - grub_lvm_scan_device is called once ("md0"), it finds the LVM signature, searches the vg list to see if we've seen this vg before, creates a new vg, pv list (disk = NULL) etc.
Then it attaches the disk with this name to the appropriate pv entry in that code snippet you posted.
So the only way for a PV->disk to be attached is if it is called by name in grub_lvm_scan_device().
I'm running the following setup:/dev/md0 pv0 contains lv_root/dev/sdb2 pv1 contains lv_test1, lv_test2, etc.
invoke as:
# grub-probe -t abstraction /
Grub discovers / is on /dev/md0, probes md0 for LVM information, never looks at other disks, segfaults on a null pointer dereference because pv1 disk = NULL.
So either you can ignore it via my patch, or add a loop to get disks by ID and add their information to the pvs.
Does my analysis make sense?