osdep/linux: Fix md array device enumeration
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).
---
Hi, this is a resend. Petr reviewed an earlier version back in Jan[1], but given the changes[2] I didn't want to assume that still stood. :) Regardless, I'd still like to see this merged so I don't have to trip over this bug again. ;)
[1] https://lists.gnu.org/archive/html/grub-devel/2021-01/msg00040.html
[2] https://lists.gnu.org/archive/html/grub-devel/2021-01/msg00030.html
--- grub-core/osdep/linux/getroot.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)
grub-core/osdep/linux/getroot.c +19 -8
@@ -130,10 +130,18 @@ struct mountinfo_entrychar 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;
@@ -165,22 +173,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.30.2
Quoted 12 lines · On Sat, Sep 25, 2021 at 07:03:35PM -0700, [email protected] wrote:
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 4 lines
Fixes 49de079bbe1c ("... (grub_util_raid_getmembers): Handle "removed" disks")Fixes 2b00217369ac ("... Added support for RAID and LVM")
Please add empty line here.
Quoted 17 lines
Signed-off-by Kees Cook <[email protected]>---
Hi, this is a resend. Petr reviewed an earlier version back in Jan[1], but given the changes[2] I didn't want to assume that still stood. :) Regardless, I'd still like to see this merged so I don't have to trip over this bug again. ;)
[1] https://lists.gnu.org/archive/html/grub-devel/2021-01/msg00040.html
[2] https://lists.gnu.org/archive/html/grub-devel/2021-01/msg00030.html--- grub-core/osdep/linux/getroot.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)grub-core/osdep/linux/getroot.c +0
-0
grub-core/osdep/linux/getroot.c struct mountinfo_entry · line 137
Quoted 8 lines
grub-core/osdep/linux/getroot.c +5
-0@@ -130,10 +130,18 @@ struct mountinfo_entrychar 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. */
Please format comments according to this [1].
Quoted 14 lines
grub-core/osdep/linux/getroot.c +3
-0+#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;@@ -165,22 +173,25 @@ grub_util_raid_getmembers (const char *name, int bootable) devicelist =xcalloc (info.nr_disks +1 ,sizeof (char *));
grub-core/osdep/linux/getroot.c grub_util_raid_getmembers (const char *name, int bootable) · line 183
Quoted 10 lines
grub-core/osdep/linux/getroot.c +3
-2-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)); - +
I am OK with this change but please mention in the commit message that you are fixing this on the occasion.
Quoted 7 lines
grub-core/osdep/linux/getroot.c +2
-2+/* 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,
grub-core/osdep/linux/getroot.c grub_util_raid_getmembers (const char *name, int bootable) · line 194
Quoted 10 lines
grub-core/osdep/linux/getroot.c +6
-4- 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));
I would prefer if you leave original if statement as is and update grub_find_device() call accordingly... And of course drop else as needed... :-)
Daniel
[1] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Comments
Petr Vorel
Reviewed-byHi Kees, Daniel,
Quoted 12 lines · On Sat, Sep 25, 2021 at 07:03:35PM -0700, [email protected] wrote:
Quoted 11 lines
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).
Kees, thanks a lot for taking care for this.
<snip>
Quoted 5 lines
Quoted 4 lines
grub-core/osdep/linux/getroot.c +2
-0+/* Skip empty slot: MD_DISK_REMOVED slots don't count toward nr_disks. */ if (disk.state & (1 <<MD_DISK_REMOVED ))continue ; + remaining--;
Quoted 16 lines
grub-core/osdep/linux/getroot.c grub_util_raid_getmembers (const char *name, int bootable) · line 194
Quoted 12 lines
grub-core/osdep/linux/getroot.c +6
-6-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));I would prefer if you leave original if statement as is and update
grub_find_device()call accordingly... And of course drop else as needed... :-)
I suppose you'll need to send v2 due this, but for now:
Kind regards,
Petr
Hi Petr,
Quoted 15 lines · Petr Vorel
Hi Kees, Daniel,
Quoted 12 lines · On Sat, Sep 25, 2021 at 07:03:35PM -0700, [email protected] wrote:
Quoted 11 lines
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 1 line
Kees, thanks a lot for taking care for this.
You bet! I seem to find some amazing corner cases. ;)
Quoted 23 lines
<snip>
Quoted 5 lines
Quoted 4 lines
grub-core/osdep/linux/getroot.c +2
-0+/* Skip empty slot: MD_DISK_REMOVED slots don't count toward nr_disks. */ if (disk.state & (1 <<MD_DISK_REMOVED ))continue ; + remaining--;Quoted 16 lines
grub-core/osdep/linux/getroot.c grub_util_raid_getmembers (const char *name, int bootable) · line 194
Quoted 12 lines
grub-core/osdep/linux/getroot.c +6
-6-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));I would prefer if you leave original if statement as is and update
grub_find_device()call accordingly... And of course drop else as needed... :-)
Quoted 3 lines
I suppose you'll need to send v2 due this, but for now:
Reviewed-by Petr Vorel <[email protected]>
Thanks! I've included your reviewed-by in the v2; hopefully that's okay as the changes were just stylistic.
-Kees
Signature · 1 line
Kees Cook
Hi Kees,
Quoted 18 lines
Hi Petr,
Quoted 14 lines · Petr Vorel
Hi Kees, Daniel,
Quoted 12 lines · On Sat, Sep 25, 2021 at 07:03:35PM -0700, [email protected] wrote:
Quoted 11 lines
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 3 lines
Quoted 1 line
Kees, thanks a lot for taking care for this.
You bet! I seem to find some amazing corner cases. ;)
Yep, but both of your bug reports are really worth of fixing.
Quoted 22 lines
Quoted 22 lines
<snip>
Quoted 21 lines
Quoted 7 lines
grub-core/osdep/linux/getroot.c +2
-2+/* 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,grub-core/osdep/linux/getroot.c grub_util_raid_getmembers (const char *name, int bootable) · line 194
Quoted 10 lines
grub-core/osdep/linux/getroot.c +6
-4- 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));I would prefer if you leave original if statement as is and update
grub_find_device()call accordingly... And of course drop else as needed... :-)
Quoted 6 lines
Quoted 3 lines
I suppose you'll need to send v2 due this, but for now:
Reviewed-by Petr Vorel <[email protected]>Thanks! I've included your reviewed-by in the v2; hopefully that's okay as the changes were just stylistic.
Sure, changes in v2 are really minor + already asked to do by Daniel. Thanks!
Kind regards,
Petr
Quoted 1 line
-Kees
Hi Daniel,
Quoted 13 lines · Daniel Kiper
Quoted 12 lines · On Sat, Sep 25, 2021 at 07:03:35PM -0700, [email protected] wrote:
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 6 lines
Quoted 4 lines
Fixes 49de079bbe1c ("... (grub_util_raid_getmembers): Handle "removed" disks")Fixes 2b00217369ac ("... Added support for RAID and LVM")Please add empty line here.
Done!
Quoted 21 lines
Quoted 21 lines
Signed-off-by Kees Cook <[email protected]>---
Hi, this is a resend. Petr reviewed an earlier version back in Jan[1], but given the changes[2] I didn't want to assume that still stood. :) Regardless, I'd still like to see this merged so I don't have to trip over this bug again. ;)
[1] https://lists.gnu.org/archive/html/grub-devel/2021-01/msg00040.html
[2] https://lists.gnu.org/archive/html/grub-devel/2021-01/msg00030.html--- grub-core/osdep/linux/getroot.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)grub-core/osdep/linux/getroot.c +0
-0@@ -130,10 +130,18 @@ struct mountinfo_entrychar fstype[ESCAPED_PATH_MAX +1 ], device[ESCAPED_PATH_MAX +1 ]; };
Quoted 7 lines
grub-core/osdep/linux/getroot.c struct mountinfo_entry · line 137
Quoted 5 lines
grub-core/osdep/linux/getroot.c +5
-0+/* 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. */ Please format comments according to this [1].
Sure! I was trying to match the comments already in that file. :)
Quoted 15 lines
Quoted 15 lines
grub-core/osdep/linux/getroot.c +3
-0+#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;@@ -165,22 +173,25 @@ grub_util_raid_getmembers (const char *name, int bootable) devicelist =xcalloc (info.nr_disks +1 ,sizeof (char *));
Quoted 13 lines
grub-core/osdep/linux/getroot.c grub_util_raid_getmembers (const char *name, int bootable) · line 183
Quoted 10 lines
grub-core/osdep/linux/getroot.c +3
-2-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)); - +I am OK with this change but please mention in the commit message that you are fixing this on the occasion.
Er, okay, yes. It's kind of part of the same overall problem, but I've tried to specifically call it out in the changelog for v2.
Quoted 6 lines
Quoted 6 lines
grub-core/osdep/linux/getroot.c +2
-0+/* Skip empty slot: MD_DISK_REMOVED slots don't count toward nr_disks. */ if (disk.state & (1 <<MD_DISK_REMOVED ))continue ; + remaining--;
Quoted 16 lines
grub-core/osdep/linux/getroot.c grub_util_raid_getmembers (const char *name, int bootable) · line 194
Quoted 12 lines
grub-core/osdep/linux/getroot.c +6
-6-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));I would prefer if you leave original if statement as is and update
grub_find_device()call accordingly... And of course drop else as needed... :-)
Sure!
Thanks for the review; I've sent a v2 now.
-Kees
Signature · 1 line
Kees Cook