Skip to contentSkip to conversation

Should LDM check be less aggressive?

PATCH Andrey Borzenkov 3 participants 6 messages Nov 21, 2012 – May 28, 2013 Kbuild

lore
Share & tools
lore
#0 lore raw

There are multiple reports of GRUB2 refusing to install on disk which was used for LDM in the past. Example is:
https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/1061255

I tested behavior of Windows and Linux on a disk with valid LDM signature but manually modified partition table. Both ignore LDM labels if disk does not contain SFS partition (0x42) and treat is as pure MBR disk. This differs from GRUB2 behavior which always checks for LDM label whether SFS partition exists or not.

Would the following be appropriate? It adds additional check for partition 0x42 before checking for LDM labels.

-andrey

grub-core/disk/ldm.c +37 -2
Index: grub-2.00/grub-core/disk/ldm.c
===================================================================
@@ -22,6 +22,7 @@
 #include <grub/err.h>
 #include <grub/misc.h>
 #include <grub/diskfilter.h>
+#include <grub/msdos_partition.h>
 #include <grub/gpt_partition.h>
 #include <grub/i18n.h>
 
@@ -103,6 +104,34 @@ read_int (grub_uint8_t *in, grub_size_t
   return ret;
 }
 
+static int
+msdos_has_ldm_partition (grub_disk_t dsk)
+{
+  grub_err_t err;
+  int has_sfs = 0;
+  auto int hook (grub_disk_t disk, const grub_partition_t p);
+  int hook (grub_disk_t disk __attribute__ ((unused)), const grub_partition_t p)
+  {
+    if (p->number >= 4)
+      return 1;
+    if (p->msdostype == GRUB_PC_PARTITION_TYPE_SFS)
+      {
+	has_sfs = 1;
+	return 1;
+      }
+    return 0;
+  }
+
+  err = grub_partition_msdos_iterate (dsk, hook);
+  if (err)
+    {
+      grub_errno = GRUB_ERR_NONE;
+      return 0;
+    }
+
+  return has_sfs;
+}
+
 static const grub_gpt_part_type_t ldm_type = GRUB_GPT_PARTITION_TYPE_LDM;
 
 static grub_disk_addr_t
@@ -756,17 +785,20 @@ grub_ldm_detect (grub_disk_t disk,
 
   {
     int i;
+    int has_sfs = msdos_has_ldm_partition (disk);
     for (i = 0; i < 3; i++)
       {
 	grub_disk_addr_t sector = LDM_LABEL_SECTOR;
 	switch (i)
 	  {
 	  case 0:
+	    if (!has_sfs)
+	      continue;
 	    sector = LDM_LABEL_SECTOR;
 	    break;
 	  case 1:
 	    /* LDM is never inside a partition.  */
-	    if (disk->partition)
+	    if (!has_sfs || disk->partition)
 	      continue;
 	    sector = grub_disk_get_size (disk);
 	    if (sector == GRUB_DISK_SIZE_UNKNOWN)
@@ -867,6 +899,7 @@ int
 grub_util_is_ldm (grub_disk_t disk)
 {
   int i;
+  int has_sfs = msdos_has_ldm_partition (disk);
   for (i = 0; i < 3; i++)
     {
       grub_disk_addr_t sector = LDM_LABEL_SECTOR;
@@ -876,11 +909,13 @@ grub_util_is_ldm (grub_disk_t disk)
       switch (i)
 	{
 	case 0:
+	  if (!has_sfs)
+	    continue;
 	  sector = LDM_LABEL_SECTOR;
 	  break;
 	case 1:
 	  /* LDM is never inside a partition.  */
-	  if (disk->partition)
+	  if (!has_sfs || disk->partition)
 	    continue;
 	  sector = grub_disk_get_size (disk);
 	  if (sector == GRUB_DISK_SIZE_UNKNOWN)
include/grub/msdos_partition.h +1 -0
Index: grub-2.00/include/grub/msdos_partition.h
===================================================================
@@ -43,6 +43,7 @@
 #define GRUB_PC_PARTITION_TYPE_FAT16_LBA	0xe
 #define GRUB_PC_PARTITION_TYPE_WIN95_EXTENDED	0xf
 #define GRUB_PC_PARTITION_TYPE_PLAN9            0x39
+#define GRUB_PC_PARTITION_TYPE_SFS		0x42
 #define GRUB_PC_PARTITION_TYPE_EZD		0x55
 #define GRUB_PC_PARTITION_TYPE_MINIX		0x80
 #define GRUB_PC_PARTITION_TYPE_LINUX_MINIX	0x81
Makefile.util.def +1 -1
Index: grub-2.00/Makefile.util.def
===================================================================
@@ -32,6 +32,7 @@ library = {
   common = grub-core/disk/ldm.c;
   common = grub-core/disk/diskfilter.c;
   common = grub-core/partmap/gpt.c;
+  common = grub-core/partmap/msdos.c;
 };
 
 library = {
@@ -109,7 +110,6 @@ library = {
   common = grub-core/partmap/acorn.c;
   common = grub-core/partmap/amiga.c;
   common = grub-core/partmap/apple.c;
-  common = grub-core/partmap/msdos.c;
   common = grub-core/partmap/sun.c;
   common = grub-core/partmap/plan.c;
   common = grub-core/partmap/dvh.c;
#1 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 4 lines · Andrey Borzenkov

There are multiple reports of GRUB2 refusing to install on disk which was used for LDM in the past. Example is:
https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/1061255

Quoted 8 lines

I tested behavior of Windows and Linux on a disk with valid LDM signature but manually modified partition table. Both ignore LDM labels if disk does not contain SFS partition (0x42) and treat is as pure MBR disk. This differs from GRUB2 behavior which always checks for LDM label whether SFS partition exists or not.

Would the following be appropriate? It adds additional check for partition 0x42 before checking for LDM labels.

Looks nice to me. The kernel has this requirement so grub should as well.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (MingW32)
Comment: Using GnuPG with undefined - http://www.enigmail.net/

iQEcBAEBAgAGBQJQrUZhAAoJEJrBOlT6nu759uEH/RuU97hAKP2GcAHTkKjvcZqg Jih1USVExNW/CYzXglXXg8KjYOv7J5xQsj00KZ8LW8f0X+ns76kV1NsrraeJFETI repkr9FX55+qvUYzbl7PiykoWnC3RUp+TUzZYotZcfc0j1YraDzhAtNsZmDPfvwq n9Ljo0py/HBPrWU6rjE31voVA8tkYOUz6ztldghDT3x+OJI7w5KnXJgXEOYxqVoJ Sx9dHsCd/AKO68CwCjVuRHUeQQV1EIlVDKX0ZG21ZM2q79YEJnIChXVSvxbaySRD yB6W9BNg7hOtFPkJs9pr81qWu3YE9RwyreOLfgGR4qzPpMBQkGsdEMJqlQytq4k= =2Eyx
-----END PGP SIGNATURE-----

#2 lore raw
In reply to Andrey Borzenkov
Quoted 1 line · Andrey Borzenkov

+ if (p->msdostype == GRUB_PC_PARTITION_TYPE_SFS)

SFS is confusing as the partition in question isn't SFS, it just happens that LDM and SFS ids collide.
Also another problem is that of booting in case of corrupted msdos table if LDM is intact but given that the kernels won't be able to recognize such a case anyway it's a minor one. Could you update the patch not to mention SFS?

Signature · 2 lines
Regards
Vladimir 'φ-coder/phcoder' Serbinenko
#3 lore raw

В Sun, 20 Jan 2013 22:58:26 +0100
Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:

Quoted 11 lines · Andrey Borzenkov
Quoted 2 lines

+ if (p->msdostype == GRUB_PC_PARTITION_TYPE_SFS)

SFS is confusing as the partition in question isn't SFS, it just happens that LDM and SFS ids collide.
Also another problem is that of booting in case of corrupted msdos table if LDM is intact but given that the kernels won't be able to recognize such a case anyway it's a minor one. Could you update the patch not to mention SFS?

Done. Also updated for new hook API.

From: Andrey Borzenkov <[email protected]>
Subject: [PATCH] check for LDM partition before checking LDM label

There are several reports of leftover LDM label at the end of disk blocking installation of grub. Check for existence of LDM partition before checking for LDM label validity. This is the same check as done by Linux kernel.

---
 ChangeLog                      |    7 +++++++
 Makefile.util.def              |    2 +-
 grub-core/disk/ldm.c           |   42 ++++++++++++++++++++++++++++++++++++++--
 include/grub/msdos_partition.h |    1 +
 4 files changed, 49 insertions(+), 3 deletions(-)
ChangeLog +7 -0
index 32b891a..dccdcc1 100644
@@ -1,3 +1,10 @@
+2013-01-21  Andrey Borzenkov <[email protected]>
+
+	* Makefile.util.def: add partmap/msdos.c to common library
+	* include/grub/msdos_partition.h: add GRUB_PC_PARTITION_TYPE_LDM
+	* grub-core/disk/ldm.c: check for existence of
+	GRUB_PC_PARTITION_TYPE_LDM
+
 2013-01-21  Vladimir Serbinenko  <[email protected]>
 
 	Make color variables global instead of it being per-terminal.
Makefile.util.def +1 -1
index 3ee5e4e..1ccf390 100644
@@ -32,6 +32,7 @@ library = {
   common = grub-core/disk/ldm.c;
   common = grub-core/disk/diskfilter.c;
   common = grub-core/partmap/gpt.c;
+  common = grub-core/partmap/msdos.c;
 };
 
 library = {
@@ -110,7 +111,6 @@ library = {
   common = grub-core/partmap/acorn.c;
   common = grub-core/partmap/amiga.c;
   common = grub-core/partmap/apple.c;
-  common = grub-core/partmap/msdos.c;
   common = grub-core/partmap/sun.c;
   common = grub-core/partmap/plan.c;
   common = grub-core/partmap/dvh.c;
grub-core/disk/ldm.c +40 -2

86 lines not on this page · raw message

include/grub/msdos_partition.h +1 -0
index 1e9b65e..92f8539 100644
@@ -43,6 +43,7 @@
 #define GRUB_PC_PARTITION_TYPE_FAT16_LBA	0xe
 #define GRUB_PC_PARTITION_TYPE_WIN95_EXTENDED	0xf
 #define GRUB_PC_PARTITION_TYPE_PLAN9            0x39
+#define GRUB_PC_PARTITION_TYPE_LDM		0x42
 #define GRUB_PC_PARTITION_TYPE_EZD		0x55
 #define GRUB_PC_PARTITION_TYPE_MINIX		0x80
 #define GRUB_PC_PARTITION_TYPE_LINUX_MINIX	0x81
Signature · 1 line
tg: (812be57..) fu/tighten-LDM-check (depends on: master)
#4 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Did this ever get applied?

Quoted 36 lines · Andrey Borzenkov

В Sun, 20 Jan 2013 22:58:26 +0100 Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:

Quoted 11 lines · Andrey Borzenkov
Quoted 2 lines

+ if (p->msdostype == GRUB_PC_PARTITION_TYPE_SFS)

SFS is confusing as the partition in question isn't SFS, it just happens that LDM and SFS ids collide. Also another problem is that of booting in case of corrupted msdos table if LDM is intact but given that the kernels won't be able to recognize such a case anyway it's a minor one. Could you update the patch not to mention SFS?

Done. Also updated for new hook API.

From: Andrey Borzenkov <[email protected]> Subject: [PATCH] check for LDM partition before checking LDM label

There are several reports of leftover LDM label at the end of disk blocking installation of grub. Check for existence of LDM partition before checking for LDM label validity. This is the same check as done by Linux kernel.

--- ChangeLog | 7 +++++++ Makefile.util.def
| 2 +- grub-core/disk/ldm.c | 42
++++++++++++++++++++++++++++++++++++++--
include/grub/msdos_partition.h | 1 + 4 files changed, 49
insertions(+), 3 deletions(-)

ChangeLog index 32b891a..dccdcc1 100644 renamed from ChangeLog +0 -1
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2013-01-21
Quoted 5 lines

Andrey Borzenkov <[email protected]> + + * Makefile.util.def: add
partmap/msdos.c to common library + *
include/grub/msdos_partition.h: add GRUB_PC_PARTITION_TYPE_LDM + *
grub-core/disk/ldm.c: check for existence of +
GRUB_PC_PARTITION_TYPE_LDM

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (MingW32)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJRpMB/AAoJEJrBOlT6nu75QfQH+gI7iPowpcB9UbPWHT2aOWkb 3mjdKlBkZVJbZOm2jn1kMBiNutxcVlNiuE4XT2oiaCWhWI2q6yqBbkXXHUqE+uif huSPxm/czh8mVz7FNSxmos8pJjsXFQ7tX1jI6ByoDqLjwTvnXXYfxgY6L5t/Bo1P +tFrdUBC0rAwFq99tKXkQQ802YbJ+eiLMehn0CQu2GggVsvnXGZkWb4trYr99ySw /eFQjW8mCVarbV1YH3MpLG/Jdevs0war0VmB5BrsxQ1pARiMI5aLhDENGGTJwc6X ucSJYplHdBRjSCr4ukdQNOzr3s/5wPwK9ZDaeOYkVDClnDK54fM8QBya+y6ZKAg= =rKy2
-----END PGP SIGNATURE-----

#5 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

В Tue, 28 May 2013 10:34:39 -0400
Phillip Susi <[email protected]> пишет:

Quoted 5 lines

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Did this ever get applied?

Yes

Quoted 47 lines · Andrey Borzenkov
Quoted 36 lines

В Sun, 20 Jan 2013 22:58:26 +0100 Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:

Quoted 11 lines · Andrey Borzenkov
Quoted 2 lines

+ if (p->msdostype == GRUB_PC_PARTITION_TYPE_SFS)

SFS is confusing as the partition in question isn't SFS, it just happens that LDM and SFS ids collide. Also another problem is that of booting in case of corrupted msdos table if LDM is intact but given that the kernels won't be able to recognize such a case anyway it's a minor one. Could you update the patch not to mention SFS?

Done. Also updated for new hook API.

From: Andrey Borzenkov <[email protected]> Subject: [PATCH] check for LDM partition before checking LDM label

There are several reports of leftover LDM label at the end of disk blocking installation of grub. Check for existence of LDM partition before checking for LDM label validity. This is the same check as done by Linux kernel.

--- ChangeLog | 7 +++++++ Makefile.util.def
| 2 +- grub-core/disk/ldm.c | 42
++++++++++++++++++++++++++++++++++++++--
include/grub/msdos_partition.h | 1 + 4 files changed, 49
insertions(+), 3 deletions(-)

ChangeLog index 32b891a..dccdcc1 100644 renamed from ChangeLog +0 -1
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2013-01-21
Quoted 5 lines

Andrey Borzenkov <[email protected]> + + * Makefile.util.def: add
partmap/msdos.c to common library + *
include/grub/msdos_partition.h: add GRUB_PC_PARTITION_TYPE_LDM + *
grub-core/disk/ldm.c: check for existence of +
GRUB_PC_PARTITION_TYPE_LDM

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (MingW32)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

Quoted 8 lines

iQEcBAEBAgAGBQJRpMB/AAoJEJrBOlT6nu75QfQH+gI7iPowpcB9UbPWHT2aOWkb 3mjdKlBkZVJbZOm2jn1kMBiNutxcVlNiuE4XT2oiaCWhWI2q6yqBbkXXHUqE+uif huSPxm/czh8mVz7FNSxmos8pJjsXFQ7tX1jI6ByoDqLjwTvnXXYfxgY6L5t/Bo1P +tFrdUBC0rAwFq99tKXkQQ802YbJ+eiLMehn0CQu2GggVsvnXGZkWb4trYr99ySw /eFQjW8mCVarbV1YH3MpLG/Jdevs0war0VmB5BrsxQ1pARiMI5aLhDENGGTJwc6X ucSJYplHdBRjSCr4ukdQNOzr3s/5wPwK9ZDaeOYkVDClnDK54fM8QBya+y6ZKAg= =rKy2
-----END PGP SIGNATURE-----

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)

iEYEARECAAYFAlGkx6cACgkQR6LMutpd94yrxQCgwlcSMgwqlno8mywJ3fjX0RvU AcUAn2redHsk3y/qly1UzlXv8mRQuZRi
=nlEF
-----END PGP SIGNATURE-----