All of lore.kernel.org
 help / color / mirror / Atom feed
* segfault in grub_device_iterate on 64bit platforms
@ 2010-01-20  1:23 Dan Merillat
  2010-01-20  1:44 ` Colin Watson
  2010-01-20  2:12 ` Colin Watson
  0 siblings, 2 replies; 5+ messages in thread
From: Dan Merillat @ 2010-01-20  1:23 UTC (permalink / raw)
  To: GNU GRUB

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:

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.




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: segfault in grub_device_iterate on 64bit platforms
  2010-01-20  1:23 segfault in grub_device_iterate on 64bit platforms Dan Merillat
@ 2010-01-20  1:44 ` Colin Watson
  2010-01-20  3:25   ` Dan Merillat
  2010-01-20  2:12 ` Colin Watson
  1 sibling, 1 reply; 5+ messages in thread
From: Colin Watson @ 2010-01-20  1:44 UTC (permalink / raw)
  To: The development of GNU GRUB

On Tue, Jan 19, 2010 at 08:23:44PM -0500, Dan Merillat wrote:
> 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;

(Nit: stick to the prevailing whitespace style.)

> 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.

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.

-- 
Colin Watson                                       [cjwatson@ubuntu.com]



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: segfault in grub_device_iterate on 64bit platforms
  2010-01-20  1:23 segfault in grub_device_iterate on 64bit platforms Dan Merillat
  2010-01-20  1:44 ` Colin Watson
@ 2010-01-20  2:12 ` Colin Watson
  2010-01-20  3:24   ` Dan Merillat
  1 sibling, 1 reply; 5+ messages in thread
From: Colin Watson @ 2010-01-20  2:12 UTC (permalink / raw)
  To: The development of GNU GRUB

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]



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: segfault in grub_device_iterate on 64bit platforms
  2010-01-20  2:12 ` Colin Watson
@ 2010-01-20  3:24   ` Dan Merillat
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Merillat @ 2010-01-20  3:24 UTC (permalink / raw)
  To: The development of GNU GRUB

Colin Watson wrote:
>> 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:

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.

>       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.

> 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!

 > 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.




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: segfault in grub_device_iterate on 64bit platforms
  2010-01-20  1:44 ` Colin Watson
@ 2010-01-20  3:25   ` Dan Merillat
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Merillat @ 2010-01-20  3:25 UTC (permalink / raw)
  To: The development of GNU GRUB

Colin Watson wrote:
> On Tue, Jan 19, 2010 at 08:23:44PM -0500, Dan Merillat wrote:
>> 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.

> 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.

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?




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-01-20  3:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-01-20  1:23 segfault in grub_device_iterate on 64bit platforms Dan Merillat
2010-01-20  1:44 ` Colin Watson
2010-01-20  3:25   ` Dan Merillat
2010-01-20  2:12 ` Colin Watson
2010-01-20  3:24   ` Dan Merillat

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.