Skip to contentSkip to conversation

osdep/linux: Fix md array device enumeration

PATCH In review Julian Andres Klode 3 participants 8 messages Jun 6, 2023 – Jun 12, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 osdep/linux: Fix md array device enumeration

From: Kees Cook <[email protected]>

GET_ARRAY_INFO's info.nr_disks does not map to GET_DISK_INFO's disk.number, which is an internal kernel index. If an array has had drives added, removed, etc, there may be gaps in GET_DISK_INFO's results. But since the consumer of devicelist cannot tolerate gaps (it expects to walk a NULL-terminated list of device name strings), the devicelist index (j) must be tracked separately from the disk.number index (i). But grub wants to only examine active (i.e. present and non-failed) disks, so how many disks are left to be queried must be also separately tracked (remaining).

Fixes 49de079bbe1c ("... (grub_util_raid_getmembers): Handle "removed" disks")
Fixes 2b00217369ac ("... Added support for RAID and LVM")
---
 grub-core/osdep/linux/getroot.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
grub-core/osdep/linux/getroot.c +19 -8
index 001b818fe..d7b62e702 100644
@@ -135,10 +135,18 @@ struct mountinfo_entry
   char fstype[ESCAPED_PATH_MAX + 1], device[ESCAPED_PATH_MAX + 1];
 };
 
+/* GET_DISK_INFO nr_disks (total count) does not map to disk.number,
+   which is an internal kernel index. Instead, do what mdadm does
+   and keep scanning until we find enough valid disks. The limit is
+   copied from there, which notes that it is sufficiently high given
+   that the on-disk metadata for v1.x can only support 1920.  */
+#define MD_MAX_DISKS       4096
+
 static char **
 grub_util_raid_getmembers (const char *name, int bootable)
 {
   int fd, ret, i, j;
+  int remaining;
   char **devicelist;
   mdu_version_t version;
   mdu_array_info_t info;
@@ -170,22 +178,25 @@ grub_util_raid_getmembers (const char *name, int bootable)
 
   devicelist = xcalloc (info.nr_disks + 1, sizeof (char *));
 
-  for (i = 0, j = 0; j < info.nr_disks; i++)
+  remaining = info.nr_disks;
+  for (i = 0, j = 0; i < MD_MAX_DISKS && remaining > 0; i++)
     {
       disk.number = i;
       ret = ioctl (fd, GET_DISK_INFO, &disk);
       if (ret != 0)
 	grub_util_error (_("ioctl GET_DISK_INFO error: %s"), strerror (errno));
-      
+
+      /* Skip empty slot: MD_DISK_REMOVED slots don't count toward nr_disks. */
       if (disk.state & (1 << MD_DISK_REMOVED))
 	continue;
+      remaining--;
 
-      if (disk.state & (1 << MD_DISK_ACTIVE))
-	devicelist[j] = grub_find_device (NULL,
-					  makedev (disk.major, disk.minor));
-      else
-	devicelist[j] = NULL;
-      j++;
+      /* Only examine disks that are actively participating in the array. */
+      if (!(disk.state & (1 << MD_DISK_ACTIVE)))
+	continue;
+
+      devicelist[j++] = grub_find_device (NULL, makedev (disk.major,
+							 disk.minor));
     }
 
   devicelist[j] = NULL;
Signature · 1 line
2.39.2
#1 lore raw
Quoted 12 lines · Julian Andres Klode

From: Kees Cook <[email protected]>

GET_ARRAY_INFO's info.nr_disks does not map to GET_DISK_INFO's disk.number, which is an internal kernel index. If an array has had drives added, removed, etc, there may be gaps in GET_DISK_INFO's results. But since the consumer of devicelist cannot tolerate gaps (it expects to walk a NULL-terminated list of device name strings), the devicelist index (j) must be tracked separately from the disk.number index (i). But grub wants to only examine active (i.e. present and non-failed) disks, so how many disks are left to be queried must be also separately tracked (remaining).

Quoted 5 lines
Fixes 49de079bbe1c ("... (grub_util_raid_getmembers): Handle "removed" disks")
Fixes 2b00217369ac ("... Added support for RAID and LVM")

I did not add a cover letter, I just found this patch in a merge request on Debian's GitLab, and it still applies, but I haven't tested it further than Kees did, and don't know what the test case is quite honestly.

Signature · 2 lines
debian developer - deb.li/jak | jak-linux.org - free software dev
ubuntu core developer                              i speak de, en
#2 lore raw
Quoted 19 lines · Julian Andres Klode
Quoted 12 lines · Julian Andres Klode

From: Kees Cook <[email protected]>

GET_ARRAY_INFO's info.nr_disks does not map to GET_DISK_INFO's disk.number, which is an internal kernel index. If an array has had drives added, removed, etc, there may be gaps in GET_DISK_INFO's results. But since the consumer of devicelist cannot tolerate gaps (it expects to walk a NULL-terminated list of device name strings), the devicelist index (j) must be tracked separately from the disk.number index (i). But grub wants to only examine active (i.e. present and non-failed) disks, so how many disks are left to be queried must be also separately tracked (remaining).

Quoted 5 lines
Fixes 49de079bbe1c ("... (grub_util_raid_getmembers): Handle "removed" disks")
Fixes 2b00217369ac ("... Added support for RAID and LVM")
Quoted 4 lines

I did not add a cover letter, I just found this patch in a merge request on Debian's GitLab, and it still applies, but I haven't tested it further than Kees did, and don't know what the test case is quite honestly.

This patch is in upstream as commit c39f27cd6 (osdep/linux: Fix md array device enumeration).

I realized right now that MD_MAX_DISKS defined in commit c39f27cd6 (osdep/linux: Fix md array device enumeration) is not in sync with commit 2a5e3c1f2 (disk/diskfilter: Don't make a RAID array with more than 1024 disks). I think we should sync both numbers down to 1024...

Daniel

#3 lore raw
Quoted 20 lines · Daniel Kiper
Quoted 19 lines · Julian Andres Klode
Quoted 17 lines · Julian Andres Klode

From: Kees Cook <[email protected]>

GET_ARRAY_INFO's info.nr_disks does not map to GET_DISK_INFO's disk.number, which is an internal kernel index. If an array has had drives added, removed, etc, there may be gaps in GET_DISK_INFO's results. But since the consumer of devicelist cannot tolerate gaps (it expects to walk a NULL-terminated list of device name strings), the devicelist index (j) must be tracked separately from the disk.number index (i). But grub wants to only examine active (i.e. present and non-failed) disks, so how many disks are left to be queried must be also separately tracked (remaining).

Fixes 49de079bbe1c ("... (grub_util_raid_getmembers): Handle "removed" disks")
Fixes 2b00217369ac ("... Added support for RAID and LVM")
Quoted 7 lines
Quoted 4 lines

I did not add a cover letter, I just found this patch in a merge request on Debian's GitLab, and it still applies, but I haven't tested it further than Kees did, and don't know what the test case is quite honestly.

This patch is in upstream as commit c39f27cd6 (osdep/linux: Fix md array device enumeration).

Huh sorry,

I must have been looking at a wrong branch and applied to a wrong branch before forwarding.

Quoted 5 lines

I realized right now that MD_MAX_DISKS defined in commit c39f27cd6 (osdep/linux: Fix md array device enumeration) is not in sync with commit 2a5e3c1f2 (disk/diskfilter: Don't make a RAID array with more than 1024 disks). I think we should sync both numbers down to 1024...

+1

Signature · 2 lines
debian developer - deb.li/jak | jak-linux.org - free software dev
ubuntu core developer                              i speak de, en
#4 lore raw
Quoted 7 lines · Julian Andres Klode
Quoted 5 lines · Daniel Kiper
Quoted 1 line · Julian Andres Klode

On Tue, Jun 06, 2023 at 06:10:21PM +0200, Julian Andres Klode wrote:

[...]
This patch is in upstream as commit c39f27cd6 (osdep/linux: Fix md array device enumeration).

Oh good. I really thought it had landed already, so thanks for checking. I got worried this morning when I saw the email to grub-devel. :P "Wasn't that fixed already?" :) But thank you for making sure it hadn't gotten lost! Is there a way to close the tracker item for it?

Quoted 7 lines

[...]

Quoted 4 lines

I realized right now that MD_MAX_DISKS defined in commit c39f27cd6 (osdep/linux: Fix md array device enumeration) is not in sync with commit 2a5e3c1f2 (disk/diskfilter: Don't make a RAID array with more than 1024 disks). I think we should sync both numbers down to 1024...

+1

Yeah, seems reasonable, though as I hinted in the original patch, this number appeared to have been arbitrarily chosen by mdadm at the time.

-Kees

Signature · 1 line
Kees Cook
#5 lore raw
Quoted 10 lines · Kees Cook
Quoted 7 lines · Julian Andres Klode
Quoted 5 lines · Daniel Kiper
Quoted 1 line · Julian Andres Klode

On Tue, Jun 06, 2023 at 06:10:21PM +0200, Julian Andres Klode wrote:

[...]
This patch is in upstream as commit c39f27cd6 (osdep/linux: Fix md array device enumeration).

Quoted 5 lines

Oh good. I really thought it had landed already, so thanks for checking. I got worried this morning when I saw the email to grub-devel. :P "Wasn't that fixed already?" :) But thank you for making sure it hadn't gotten lost! Is there a way to close the tracker item for it?

I think you should be able to do that. If not I can try to do that for you.

Quoted 10 lines
Quoted 7 lines

[...]

Quoted 4 lines

I realized right now that MD_MAX_DISKS defined in commit c39f27cd6 (osdep/linux: Fix md array device enumeration) is not in sync with commit 2a5e3c1f2 (disk/diskfilter: Don't make a RAID array with more than 1024 disks). I think we should sync both numbers down to 1024...

+1

Yeah, seems reasonable, though as I hinted in the original patch, this number appeared to have been arbitrarily chosen by mdadm at the time.

OK, we will bump it to 4096 as well.

Daniel

#6 lore raw
Quoted 11 lines · Daniel Kiper
Quoted 10 lines · Kees Cook
Quoted 7 lines · Julian Andres Klode
Quoted 5 lines · Daniel Kiper
Quoted 1 line · Julian Andres Klode

On Tue, Jun 06, 2023 at 06:10:21PM +0200, Julian Andres Klode wrote:

[...]
This patch is in upstream as commit c39f27cd6 (osdep/linux: Fix md array device enumeration).

Quoted 7 lines
Quoted 5 lines

Oh good. I really thought it had landed already, so thanks for checking. I got worried this morning when I saw the email to grub-devel. :P "Wasn't that fixed already?" :) But thank you for making sure it hadn't gotten lost! Is there a way to close the tracker item for it?

I think you should be able to do that.

Ah-ha, yes, I've closed it now. :)
https://salsa.debian.org/grub-team/grub/-/merge_requests/23

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

[...]

Quoted 4 lines

I realized right now that MD_MAX_DISKS defined in commit c39f27cd6 (osdep/linux: Fix md array device enumeration) is not in sync with commit 2a5e3c1f2 (disk/diskfilter: Don't make a RAID array with more than 1024 disks). I think we should sync both numbers down to 1024...

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

+1

Yeah, seems reasonable, though as I hinted in the original patch, this number appeared to have been arbitrarily chosen by mdadm at the time.

OK, we will bump it to 4096 as well.

Yeah, I think _technically_ it can be higher than 1024, though ... I struggle to imagine this for a boot device. ;)

Signature · 1 line
Kees Cook
#7 lore raw
Quoted 18 lines · Kees Cook
Quoted 17 lines · Daniel Kiper
Quoted 15 lines · Kees Cook
Quoted 7 lines · Julian Andres Klode
Quoted 5 lines · Daniel Kiper
Quoted 1 line · Julian Andres Klode

On Tue, Jun 06, 2023 at 06:10:21PM +0200, Julian Andres Klode wrote:

[...]
This patch is in upstream as commit c39f27cd6 (osdep/linux: Fix md array device enumeration).

Oh good. I really thought it had landed already, so thanks for checking. I got worried this morning when I saw the email to grub-devel. :P "Wasn't that fixed already?" :) But thank you for making sure it hadn't gotten lost! Is there a way to close the tracker item for it?

Quoted 4 lines
Quoted 1 line

I think you should be able to do that.

Ah-ha, yes, I've closed it now. :)
https://salsa.debian.org/grub-team/grub/-/merge_requests/23

Cool! Thanks a lot!

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

[...]

Quoted 4 lines

I realized right now that MD_MAX_DISKS defined in commit c39f27cd6 (osdep/linux: Fix md array device enumeration) is not in sync with commit 2a5e3c1f2 (disk/diskfilter: Don't make a RAID array with more than 1024 disks). I think we should sync both numbers down to 1024...

+1

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Yeah, seems reasonable, though as I hinted in the original patch, this number appeared to have been arbitrarily chosen by mdadm at the time.

OK, we will bump it to 4096 as well.

Yeah, I think _technically_ it can be higher than 1024, though ... I struggle to imagine this for a boot device. ;)

Yeah, same here... :-)

Daniel