All of lore.kernel.org
 help / color / mirror / Atom feed
From: Colin Watson <cjwatson@ubuntu.com>
To: The development of GNU GRUB <grub-devel@gnu.org>
Subject: Re: segfault in grub_device_iterate on 64bit platforms
Date: Wed, 20 Jan 2010 02:12:49 +0000	[thread overview]
Message-ID: <20100120021249.GP5847@riva.ucam.org> (raw)
In-Reply-To: <4B565B20.9050603@dan.merillat.org>

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]



  parent reply	other threads:[~2010-01-20  2:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2010-01-20  3:24   ` Dan Merillat

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100120021249.GP5847@riva.ucam.org \
    --to=cjwatson@ubuntu.com \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.