Skip to contentSkip to conversation

Fix potential truncation of mdraid device list

PATCH In review Kees Cook 2 participants 2 messages Jan 16, 2021 – Jan 19, 2021

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 Fix potential truncation of mdraid device list

The consumer of grub_util_raid_getmembers() expects a NULL terminated list of device names. It has no idea about how many devices are registered in the array; it only cares about active devices. As a result, there cannot be gaps in the list, otherwise the first listed inactive device will cause all remaining devices to effectively vanish. This is especially troublesome if a root filesystem were on an array with the first device being a hot spare: the array would appear to have no disks and the root filesystem would become invisible to grub.

Fixes 49de079bbe1c ("... (grub_util_raid_getmembers): Handle "removed" disks")
grub-core/osdep/linux/getroot.c +7 -7
Index: grub2-2.04/grub-core/osdep/linux/getroot.c
===================================================================
@@ -170,21 +170,21 @@ grub_util_raid_getmembers (const char *n
 
   devicelist = xcalloc (info.nr_disks + 1, sizeof (char *));
 
-  for (i = 0, j = 0; j < info.nr_disks; i++)
+  for (i = 0, j = 0; i < info.nr_disks; 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));
-      
+
       if (disk.state & (1 << MD_DISK_REMOVED))
 	continue;
 
-      if (disk.state & (1 << MD_DISK_ACTIVE))
-	devicelist[j] = grub_find_device (NULL,
-					  makedev (disk.major, disk.minor));
-      else
-	devicelist[j] = NULL;
+      if (!(disk.state & (1 << MD_DISK_ACTIVE)))
+	continue;
+
+      devicelist[j] = grub_find_device (NULL,
+					makedev (disk.major, disk.minor));
       j++;
     }
 
Signature · 1 line
Kees Cook                                            @outflux.net
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 9 lines

The consumer of grub_util_raid_getmembers() expects a NULL terminated list of device names. It has no idea about how many devices are registered in the array; it only cares about active devices. As a result, there cannot be gaps in the list, otherwise the first listed inactive device will cause all remaining devices to effectively vanish. This is especially troublesome if a root filesystem were on an array with the first device being a hot spare: the array would appear to have no disks and the root filesystem would become invisible to grub.

Quoted 4 lines
Fixes 49de079bbe1c ("... (grub_util_raid_getmembers): Handle "removed" disks")

Kind regards,
Petr

Quoted 32 lines

Index: grub2-2.04/grub-core/osdep/linux/getroot.c
===================================================================

grub-core/osdep/linux/getroot.c +7 -7
@@ -170,21 +170,21 @@ grub_util_raid_getmembers (const char *n
 
   devicelist = xcalloc (info.nr_disks + 1, sizeof (char *));
 
-  for (i = 0, j = 0; j < info.nr_disks; i++)
+  for (i = 0, j = 0; i < info.nr_disks; 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));
-      
+
       if (disk.state & (1 << MD_DISK_REMOVED))
 	continue;
 
-      if (disk.state & (1 << MD_DISK_ACTIVE))
-	devicelist[j] = grub_find_device (NULL,
-					  makedev (disk.major, disk.minor));
-      else
-	devicelist[j] = NULL;
+      if (!(disk.state & (1 << MD_DISK_ACTIVE)))
+	continue;
+
+      devicelist[j] = grub_find_device (NULL,
+					makedev (disk.major, disk.minor));
       j++;
     }