Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1l0pMQ-0001E3-W4 for mharc-grub-devel@gnu.org; Sat, 16 Jan 2021 12:27:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39528) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l0pMP-0001Dx-BR for grub-devel@gnu.org; Sat, 16 Jan 2021 12:27:37 -0500 Received: from mta.outflux.net ([2001:19d0:2:6:c0de:0:736d:7471]:40871 helo=smtp.outflux.net) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l0pML-0007SL-Ja for grub-devel@gnu.org; Sat, 16 Jan 2021 12:27:36 -0500 Received: from www.outflux.net (serenity.outflux.net [10.2.0.2]) by vinyl.outflux.net (8.15.2/8.15.2/Debian-10) with ESMTP id 10GHRSeu001311 for ; Sat, 16 Jan 2021 09:27:29 -0800 Received: by www.outflux.net (Postfix, from userid 501) id C6C196054E; Sat, 16 Jan 2021 09:27:28 -0800 (PST) Date: Sat, 16 Jan 2021 09:27:28 -0800 From: Kees Cook To: grub-devel@gnu.org Subject: [PATCH] Fix md RAID enumeration Message-ID: <20210116172728.GB4996@outflux.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Organization: Outflux X-MIMEDefang-Filter: outflux$Revision: 1.316 $ X-HELO: www.outflux.net X-Scanned-By: MIMEDefang 2.83 Received-SPF: none client-ip=2001:19d0:2:6:c0de:0:736d:7471; envelope-from=kees@ubuntu.com; helo=smtp.outflux.net X-Spam_score_int: -4 X-Spam_score: -0.5 X-Spam_bar: / X-Spam_report: (-0.5 / 5.0 requ) BAYES_00=-1.9, FORGED_SPF_HELO=1, KHOP_HELO_FCRDNS=0.399, SPF_HELO_PASS=-0.001, SPF_NONE=0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 16 Jan 2021 17:27:37 -0000 In addition to the prior patch to avoid leaving holes in the returned devicelist, the enumeration logic also needed fixing. 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. Instead, do what mdadm does and keep scanning until we find enough valid (major/minor != 0) disks. Fixes: 2b00217369ac ("... Added support for RAID and LVM") Fixes: https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/1912043 Fixes: https://savannah.gnu.org/bugs/index.php?59887 Signed-off-by: Kees Cook (This is intended to be applied on top of the earlier patch sent in https://lists.gnu.org/archive/html/grub-devel/2021-01/msg00027.html) Index: grub2-2.04/grub-core/osdep/linux/getroot.c =================================================================== --- grub2-2.04.orig/grub-core/osdep/linux/getroot.c +++ grub2-2.04/grub-core/osdep/linux/getroot.c @@ -135,10 +135,18 @@ struct mountinfo_entry char fstype[ESCAPED_PATH_MAX + 1], device[ESCAPED_PATH_MAX + 1]; }; +/* GET_ARRAY_INFO's info.nr_disks does not map to GET_DISK_INFO's + 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,12 +178,17 @@ grub_util_raid_getmembers (const char *n devicelist = xcalloc (info.nr_disks + 1, sizeof (char *)); - for (i = 0, j = 0; i < 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 disk slot. */ + if (disk.major == 0 && disk.minor == 0) + continue; + remaining--; if (disk.state & (1 << MD_DISK_REMOVED)) continue; -- Kees Cook @outflux.net