Skip to contentSkip to conversation

grub 1.96 svn 20080813 and circular lvm2 metadata

DISCUSSION Hans Lambermont 5 participants 17 messages Aug 28, 2008 – Oct 5, 2008

lore
Share & tools
lore
#0 lore raw

Hi all,

My colleague Jan Derk Gerlings found a bug in the way grub 1.96, svn version 20080813 (and earlier) reads the circular lvm2 metadata buffer.

If the lvm2 metadata is too large to fit in the buffer given an offset, then it is continued at the start of the buffer. In this case the current grub-setup will exit with a segmentation fault and on boot grub will not find the root drive.

The origin of this bug is traced to the way grub handles lvm2 metadata. Lvm2 has defined (and uses) the on-disk metadata as a circular list. Each change to the metadate (lvm lvchange, lvm create, lvm remove, etc.) forces lvm to write the complete metadata to the circular buffer. It will be placed after the last instance of the metadata with a new sequence number.

When the end of the buffer is reached, lvm will continue (wrap-around to) at the start of the buffer. Grub treats the buffer as linear, so when wrap-around occurs grub fails because it does not continue at the start of the buffer. This results in the grub-setup segmentation error, and the inability to boot.

On request we can send two (literal) copies of the circular buffer (it's too big for the mailing list 100kibi limit).
In md0_ok.txt the latest sequence number of the metadata is 280. It ends before it reaches the end of the buffer. Grub-install works and the machine boots.
In md0_err the latest sequence number of the metadata is 282. It wraps around at the end of the buffer, continuing at the start of the buffer. The end of the metadata comes before the start. Grub-install fails and so does the boot.

At grub-1.96_svn20080813/disk/lvm.c line 303 and further down the notion of circular metadata is not taken into account.

Shall we attempt to fix this and send a patch ?

regards,
Hans Lambermont, Jan Derk Gerlings

Signature · 2 lines
Hans Lambermont, M.Sc.  -  Newtec  -  OS-Platform&VAS
http://newtec.eu/    t:+31408519234    m:+31629064887
#1 lore raw

Am Donnerstag, den 28.08.2008, 13:14 +0200 schrieb Hans Lambermont:

Quoted 1 line

Hi all,

Hello,

Quoted 2 lines

My colleague Jan Derk Gerlings found a bug in the way grub 1.96, svn version 20080813 (and earlier) reads the circular lvm2 metadata buffer.

Great, on Debian BTS we have 2 bugs [0] related to LVM and I wasn't able yet to trace them down, but that could be the reason for them.

Quoted 1 line

Shall we attempt to fix this and send a patch ?

Yes please do :)

Because GRUB is a GNU project, you have to obey the GNU Coding Standards [1]

The Maintainer standards [2] says that on ~15 line changes you need to assign copyright to FSF, but for this the others can tell you more after you did your patch.

So just try to obey the coding style used in GRUB and please try to write a changelog entry, see GCS as a manual and the changelog file for examples

[0] http://bugs.debian.org/462835

    http://bugs.debian.org/495949

[1] http://www.gnu.org/prep/standards/html_node/index.html
[2] http://www.gnu.org/prep/maintain/maintain.html#Legally-Significant

Signature · 1 line
Felix Zielcke
#2 lore raw

Felix Zielcke wrote on 20080828:

Quoted 11 lines

Am Donnerstag, den 28.08.2008, 13:14 +0200 schrieb Hans Lambermont:

Quoted 3 lines

My colleague Jan Derk Gerlings found a bug in the way grub 1.96, svn version 20080813 (and earlier) reads the circular lvm2 metadata buffer.

Great, on Debian BTS we have 2 bugs [0] related to LVM and I wasn't able yet to trace them down, but that could be the reason for them.

Quoted 1 line

Shall we attempt to fix this and send a patch ?

Yes please do :)

Please find the patch attached, this was tested with circular metadata and the segfault in grub-setup is gone and the system boots fine.

Quoted 1 line

Because GRUB is a GNU project, you have to obey the GNU Coding Standards [1]

Ok, I hope I got it right. If not please let me know.

regards,
Hans Lambermont

Signature · 2 lines
Hans Lambermont, M.Sc.  -  Newtec  -  OS-Platform&VAS
http://newtec.eu/    t:+31408519234    m:+31629064887
ChangeLog +5 -0
diff -uwr grub-1.96_svn20080813-org/ChangeLog grub-1.96_svn20080813-new/ChangeLog
@@ -1,3 +1,8 @@
+2008-08-28 Hans Lambermont <[email protected]> (tiny change)
+	   Jan Derk Gerlings <[email protected]> (tiny change)
+
+	* disk/lvm.c: Add capability to read circular metadata
+
 2008-08-12  Robert Millan  <[email protected]>
 
 	* loader/i386/pc/multiboot.c (grub_multiboot_load_elf32): Move part
disk/lvm.c +8 -1
diff -uwr grub-1.96_svn20080813-org/disk/lvm.c grub-1.96_svn20080813-new/disk/lvm.c
@@ -281,7 +281,8 @@
       goto fail;
     }
 
-  metadatabuf = grub_malloc (mda_size);
+  /* alloc for circular worst-case scenario */
+  metadatabuf = grub_malloc (2*mda_size);
   if (! metadatabuf)
     goto fail;
 
@@ -300,6 +301,12 @@
     }
 
   rlocn = mdah->raw_locns;
+  if (rlocn->offset + rlocn->size > mdah->size)
+    {
+      /* metadata is circular */
+      grub_memcpy(metadatabuf + mda_size, metadatabuf + mdah->start,
+		  ((rlocn->offset + rlocn->size) - mdah->size));
+    }
   p = q = metadatabuf + grub_le_to_cpu64 (rlocn->offset);
 
   while (*q != ' ' && q < metadatabuf + mda_size)
#3 lore raw

Am Freitag, den 29.08.2008, 11:00 +0200 schrieb Hans Lambermont:

Quoted 2 lines

Please find the patch attached, this was tested with circular metadata and the segfault in grub-setup is gone and the system boots fine.

I just forwarded it now to the reporters of these 2 Debian bugs I mentioned. I hope they try it out soon and reply if it helps for them.

Thanks again to your colleague and you.

Quoted 3 lines
Quoted 1 line

Because GRUB is a GNU project, you have to obey the GNU Coding Standards [1]

Ok, I hope I got it right. If not please let me know.

Oh it seems I forgot you to tell you 2 things ;)
The Changelog entry should be seperate, i.e. not a diff against the actual ChangeLog file but you can have it above inside your patch For example here my last small patch send to the list

http://lists.gnu.org/archive/html/grub-devel/2008-08/txtUBorY42rw2.txt

Second please use `svn diff --diff-cmd diff -x -up' so the C function name is printed inside the diff.

2008-08-28 Hans Lambermont <[email protected]> (tiny change)

Yep, GCS mentions this `tiny change' but it's never used in GRUB's ChangeLog.

+ metadatabuf = grub_malloc (2*mda_size);

should be (2 * mda_size)

+ grub_memcpy(metadatabuf + mda_size, metadatabuf + mdah->start,

again another space missing

Except of this it looks fine for me, but I just started to contribute to GRUB and in fact my C experience isn't that great either. And I only started to use LVM for bugs in GRUB.

No need to send another patch just for these little changes, better just wait a few days for comments from the others and if nobody replies then feel free to bring it up again with a 2nd patch :)

Signature · 1 line
Felix Zielcke
#4 lore raw
Quoted 3 lines · Felix Zielcke

The Changelog entry should be seperate, i.e. not a diff against the actual ChangeLog file but you can have it above inside your patch For example here my last small patch send to the list

That is perfectly fine there :)

Thou, you can also copy it within your email too. But everyone can spot it from patch too. Most important aspect is that it has to be there...

Thanks,
Vesa Jääskeläinen

#5 lore raw

Hello,

Am Freitag, den 29.08.2008, 18:00 +0300 schrieb Vesa Jääskeläinen:

Quoted 9 lines
Quoted 3 lines · Felix Zielcke

The Changelog entry should be seperate, i.e. not a diff against the actual ChangeLog file but you can have it above inside your patch For example here my last small patch send to the list

That is perfectly fine there :)

Thou, you can also copy it within your email too. But everyone can spot it from patch too. Most important aspect is that it has to be there...

Right ;)

I think that in general it's just better to not make an actual diff against it, but I failed to tell him.
So it's anyway my fault again :)

Hm but now that I think about it, the `ChangeLog' file will probable be mostly first on the diffs'
After a few days it just can't be applies cleanly but for the ChangeLog entry you anyway need to copy and modify it first before commiting :)

#6 lore raw
In reply to Vesa Jääskeläinen
Quoted 9 lines · Vesa Jääskeläinen
Quoted 3 lines · Felix Zielcke

The Changelog entry should be seperate, i.e. not a diff against the actual ChangeLog file but you can have it above inside your patch For example here my last small patch send to the list

That is perfectly fine there :)

Thou, you can also copy it within your email too. But everyone can spot it from patch too. Most important aspect is that it has to be there...

The problem is often when multiple files are patched, the ChangeLog entry won't be the first in the list. Furthermore this makes a patch harder to apply.

--
Marco

#7 lore raw
In reply to Hans Lambermont

Hi Hans,

Quoted 10 lines · [email protected] (Hans Lambermont) writes:

Felix Zielcke wrote on 20080828:

Quoted 8 lines

Am Donnerstag, den 28.08.2008, 13:14 +0200 schrieb Hans Lambermont:

Quoted 3 lines

My colleague Jan Derk Gerlings found a bug in the way grub 1.96, svn version 20080813 (and earlier) reads the circular lvm2 metadata buffer.

Great, on Debian BTS we have 2 bugs [0] related to LVM and I wasn't able yet to trace them down, but that could be the reason for them.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Shall we attempt to fix this and send a patch ?

Yes please do :)

Please find the patch attached, this was tested with circular metadata and the segfault in grub-setup is gone and the system boots fine.

Great! It is really nice to have this fixed :-)

Quoted 3 lines
Quoted 1 line

Because GRUB is a GNU project, you have to obey the GNU Coding Standards [1]

Ok, I hope I got it right. If not please let me know.

Okay.

ChangeLog line 4

Quoted 8 lines

diff -uwr grub-1.96_svn20080813-org/ChangeLog grub-1.96_svn20080813-new/ChangeLog

ChangeLog +4 -0
@@ -1,3 +1,8 @@
+2008-08-28 Hans Lambermont <[email protected]> (tiny change)
+	   Jan Derk Gerlings <[email protected]> (tiny change)
+
+	* disk/lvm.c: Add capability to read circular metadata

Please describe changes in the changelog entry, not the effect.

For example:

* disk/lvm.c (grub_lvm_scan_device): Allocate buffer space for the worst case scenario.
(grub_lvm_scan_device): ...

Where ... has to be filled in, I have no idea what this code actually does or what you changed ;-)

The tiny change syntax does not seem familiar to me, AFAIK it is not from the GCS. Can you please change that? Furthermore, if you both worked on there two parts of the patch, please send in separate patches. It will make my life a lot easier... :-)

If only one person worked on this, for example Jan Derk, which Hans only forwards this patch, please only list Jan Derk as the contributor.

disk/lvm.c line 284

Quoted 12 lines
ChangeLog +0 -0
 2008-08-12  Robert Millan  <[email protected]>
 
 	* loader/i386/pc/multiboot.c (grub_multiboot_load_elf32): Move part

diff -uwr grub-1.96_svn20080813-org/disk/lvm.c grub-1.96_svn20080813-new/disk/lvm.c

disk/lvm.c +1 -1
@@ -281,7 +281,8 @@
       goto fail;
     }
 
-  metadatabuf = grub_malloc (mda_size);
+  /* alloc for circular worst-case scenario */

Nitpick: Please start a sentence with a capital letter and end with a `.'. So this will become:

/* Assume circular buffer in a worst case scenario. */

disk/lvm.c line 285

Quoted 1 line
disk/lvm.c +1 -0
+  metadatabuf = grub_malloc (2*mda_size);

Please use spaces around operators:

2 * md_size

disk/lvm.c line 304

Quoted 8 lines
disk/lvm.c +1 -0
   if (! metadatabuf)
     goto fail;
 
@@ -300,6 +301,12 @@
     }
 
   rlocn = mdah->raw_locns;
+  if (rlocn->offset + rlocn->size > mdah->size)

Here rlcon->offset seems to be little endian (64 bits), so please use grub_le_to_cpu64. Same for rlocn->size, please check the size of this member before you use a macro (I couldn't find it immediately...).

disk/lvm.c line 306

Quoted 2 lines
disk/lvm.c +2 -0
+    {
+      /* metadata is circular */

Same as above.

disk/lvm.c line 309

Quoted 3 lines
disk/lvm.c +3 -0
+      grub_memcpy(metadatabuf + mda_size, metadatabuf + mdah->start,
+		  ((rlocn->offset + rlocn->size) - mdah->size));
+    }

Please check and correct the endianess. you use a lot of parenthesises. Actually, I think none are requires.

Thanks,
Marco

#8 lore raw

Am Freitag, den 29.08.2008, 18:08 +0200 schrieb Marco Gerards:

Quoted 7 lines

Hi Hans,

Quoted 2 lines · [email protected] (Hans Lambermont) writes:

Please find the patch attached, this was tested with circular metadata and the segfault in grub-setup is gone and the system boots fine.

Great! It is really nice to have this fixed :-)

Just for your information. (I don't want to hurry.)
Reporter of Debian bug #462835 just confirmed that it fixes his problem. I hope that the other one replys soon now too.

Signature · 1 line
Felix Zielcke
#9 lore raw
In reply to Marco Gerards

Hans,

could you please address Marco's issues and send a new patch so the topic is brought up again?

Am Freitag, den 29.08.2008, 18:08 +0200 schrieb Marco Gerards:

Quoted 79 lines

ChangeLog line 4

Quoted 9 lines

diff -uwr grub-1.96_svn20080813-org/ChangeLog grub-1.96_svn20080813-new/ChangeLog

ChangeLog +4 -0
@@ -1,3 +1,8 @@
+2008-08-28 Hans Lambermont <[email protected]> (tiny change)
+	   Jan Derk Gerlings <[email protected]> (tiny change)
+
+	* disk/lvm.c: Add capability to read circular metadata

Please describe changes in the changelog entry, not the effect.

For example:

* disk/lvm.c (grub_lvm_scan_device): Allocate buffer space for the worst case scenario.
(grub_lvm_scan_device): ...

Where ... has to be filled in, I have no idea what this code actually does or what you changed ;-)

The tiny change syntax does not seem familiar to me, AFAIK it is not from the GCS. Can you please change that? Furthermore, if you both worked on there two parts of the patch, please send in separate patches. It will make my life a lot easier... :-)

If only one person worked on this, for example Jan Derk, which Hans only forwards this patch, please only list Jan Derk as the contributor.

disk/lvm.c line 284

Quoted 12 lines
ChangeLog +0 -0
 2008-08-12  Robert Millan  <[email protected]>
 
 	* loader/i386/pc/multiboot.c (grub_multiboot_load_elf32): Move part

diff -uwr grub-1.96_svn20080813-org/disk/lvm.c grub-1.96_svn20080813-new/disk/lvm.c

disk/lvm.c +1 -1
@@ -281,7 +281,8 @@
       goto fail;
     }
 
-  metadatabuf = grub_malloc (mda_size);
+  /* alloc for circular worst-case scenario */

Nitpick: Please start a sentence with a capital letter and end with a `.'. So this will become:

/* Assume circular buffer in a worst case scenario. */

disk/lvm.c line 285

Quoted 1 line
disk/lvm.c +1 -0
+  metadatabuf = grub_malloc (2*mda_size);

Please use spaces around operators:

2 * md_size

disk/lvm.c line 304

Quoted 8 lines
disk/lvm.c +1 -0
   if (! metadatabuf)
     goto fail;
 
@@ -300,6 +301,12 @@
     }
 
   rlocn = mdah->raw_locns;
+  if (rlocn->offset + rlocn->size > mdah->size)

Here rlcon->offset seems to be little endian (64 bits), so please use grub_le_to_cpu64. Same for rlocn->size, please check the size of this member before you use a macro (I couldn't find it immediately...).

disk/lvm.c line 306

Quoted 2 lines
disk/lvm.c +2 -0
+    {
+      /* metadata is circular */

Same as above.

disk/lvm.c line 309

Quoted 3 lines
disk/lvm.c +3 -0
+      grub_memcpy(metadatabuf + mda_size, metadatabuf + mdah->start,
+		  ((rlocn->offset + rlocn->size) - mdah->size));
+    }

Please check and correct the endianess. you use a lot of parenthesises. Actually, I think none are requires.

Signature · 1 line
Felix Zielcke
#10 lore raw

Felix Zielcke wrote on 20080903:

Quoted 2 lines

could you please address Marco's issues and send a new patch so the topic is brought up again?

Yes, I'll make some time for it.

regards,
Hans Lambermont

Signature · 2 lines
Hans Lambermont, M.Sc.  -  Newtec  -  OS-Platform&VAS
http://newtec.eu/    t:+31408519234    m:+31629064887
#11 lore raw
In reply to Felix Zielcke

Felix Zielcke wrote on 20080903:

Quoted 2 lines

could you please address Marco's issues and send a new patch so the topic is brought up again?

Please find attached a new patch. I hope I managed to fix all remarks that were made ;-) If not then please let me know.

This is intended for the Changelog :

2008-09-09 Hans Lambermont <[email protected]>

* disk/lvm.c (grub_lvm_scan_device): Allocate buffer space for the circular metadata worst case scenario. If the metadata is circular then copy the wrap in place.
* include/grub/lvm.h: Add GRUB_LVM_MDA_HEADER_SIZE, from the LVM2 project lib/format_text/layout.h
Circular metadata bug found and patch debugged by
Jan Derk Gerlings <[email protected]>

The 'svn diff --diff-cmd diff -x -up' style patch is attached.

regards,
Hans Lambermont

Signature · 2 lines
Hans Lambermont, M.Sc.  -  Newtec  -  OS-Platform&VAS
http://newtec.eu/    t:+31408519234    m:+31629064887
lvm.c +12 -1
Index: disk/lvm.c
===================================================================
@@ -281,7 +281,8 @@ grub_lvm_scan_device (const char *name)
       goto fail;
     }
 
-  metadatabuf = grub_malloc (mda_size);
+  /* Allocate buffer space for the circular worst-case scenario. */
+  metadatabuf = grub_malloc (2 * mda_size);
   if (! metadatabuf)
     goto fail;
 
@@ -300,6 +301,16 @@ grub_lvm_scan_device (const char *name)
     }
 
   rlocn = mdah->raw_locns;
+  if (grub_le_to_cpu64 (rlocn->offset) + grub_le_to_cpu64 (rlocn->size) >
+      grub_le_to_cpu64 (mdah->size))
+    {
+      /* Metadata is circular. Copy the wrap in place. */
+      grub_memcpy (metadatabuf + mda_size,
+                   metadatabuf + GRUB_LVM_MDA_HEADER_SIZE,
+                   grub_le_to_cpu64 (rlocn->offset) +
+                   grub_le_to_cpu64 (rlocn->size) -
+                   grub_le_to_cpu64 (mdah->size));
+    }
   p = q = metadatabuf + grub_le_to_cpu64 (rlocn->offset);
 
   while (*q != ' ' && q < metadatabuf + mda_size)
grub/lvm.h +1 -0
Index: include/grub/lvm.h
===================================================================
@@ -103,6 +103,7 @@ struct grub_lvm_pv_header {
 
 #define GRUB_LVM_FMTT_MAGIC "\040\114\126\115\062\040\170\133\065\101\045\162\060\116\052\076"
 #define GRUB_LVM_FMTT_VERSION 1
+#define GRUB_LVM_MDA_HEADER_SIZE 512
 
 /* On disk */
 struct grub_lvm_raw_locn {
#12 lore raw

Hans Lambermont wrote on 20080909:

Quoted 3 lines

Felix Zielcke wrote on 20080903:

Quoted 2 lines

could you please address Marco's issues and send a new patch so the topic is brought up again?

The new patch was sent to the list 2 weeks ago.
(See http://lists.gnu.org/archive/html/grub-devel/2008-09/msg00210.html )

Is there anything else that is needed ?

regards,
Hans Lambermont

Signature · 2 lines
Hans Lambermont, M.Sc.  -  Newtec  -  OS-Platform&VAS
http://newtec.eu/    t:+31408519234    m:+31629064887
#14 lore raw
Quoted 9 lines · Felix Zielcke

Am Dienstag, den 23.09.2008, 10:29 +0200 schrieb Hans Lambermont:

Quoted 4 lines

The new patch was sent to the list 2 weeks ago.
(See http://lists.gnu.org/archive/html/grub-devel/2008-09/msg00210.html )

Is there anything else that is needed ?

For me personally it looks fine, but it would be good if Marco has a look over it and unfornatunately he doestn't have much free time now.

So let's put him in CC.

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
#15 lore raw

Robert Millan wrote on 20080924:

Quoted 8 lines · Felix Zielcke
Quoted 6 lines

Am Dienstag, den 23.09.2008, 10:29 +0200 schrieb Hans Lambermont:

Quoted 3 lines

The new patch was sent to the list 2 weeks ago.
(See http://lists.gnu.org/archive/html/grub-devel/2008-09/msg00210.html ) Is there anything else that is needed ?

For me personally it looks fine, but it would be good if Marco has a look over it and unfornatunately he doestn't have much free time now.

So let's put him in CC.

My email address I put in the changelog proposal will stop working soon, so here's a new one that'll last longer :

This is intended for the Changelog :

2008-09-09 Hans Lambermont <[email protected]>

* disk/lvm.c (grub_lvm_scan_device): Allocate buffer space for the circular metadata worst case scenario. If the metadata is circular then copy the wrap in place.
* include/grub/lvm.h: Add GRUB_LVM_MDA_HEADER_SIZE, from the LVM2 project lib/format_text/layout.h
Circular metadata bug found and patch debugged by
Jan Derk Gerlings <[email protected]>

The address of Jan Derk will stop working too, I'll send an new one as soon as I have it.

regards,
Hans Lambermont

Signature · 2 lines
Hans Lambermont, M.Sc.  -  Newtec  -  OS-Platform&VAS
http://newtec.eu/    t:+31408519234    m:+31629064887
#16 lore raw
Quoted 26 lines · Hans Lambermont

Robert Millan wrote on 20080924:

Quoted 8 lines · Felix Zielcke
Quoted 6 lines

Am Dienstag, den 23.09.2008, 10:29 +0200 schrieb Hans Lambermont:

Quoted 3 lines

The new patch was sent to the list 2 weeks ago.
(See http://lists.gnu.org/archive/html/grub-devel/2008-09/msg00210.html ) Is there anything else that is needed ?

For me personally it looks fine, but it would be good if Marco has a look over it and unfornatunately he doestn't have much free time now.

So let's put him in CC.

My email address I put in the changelog proposal will stop working soon, so here's a new one that'll last longer :

This is intended for the Changelog :

2008-09-09 Hans Lambermont <[email protected]>

* disk/lvm.c (grub_lvm_scan_device): Allocate buffer space for the circular metadata worst case scenario. If the metadata is circular then copy the wrap in place.
* include/grub/lvm.h: Add GRUB_LVM_MDA_HEADER_SIZE, from the LVM2 project lib/format_text/layout.h
Circular metadata bug found and patch debugged by
Jan Derk Gerlings <[email protected]>

Quoted 2 lines

The address of Jan Derk will stop working too, I'll send an new one as soon as I have it.

Hi,

I checked this in, but ommitted Jan's address, since it will stop working as you said.

Thanks!

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."