Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1mYctH-0006eD-J7 for mharc-grub-devel@gnu.org; Thu, 07 Oct 2021 19:33:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40342) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mYctF-0006du-Ky for grub-devel@gnu.org; Thu, 07 Oct 2021 19:33:29 -0400 Received: from mta.outflux.net ([2001:19d0:2:6:c0de:0:736d:7471]:60345 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 1mYctD-0002bo-J3 for grub-devel@gnu.org; Thu, 07 Oct 2021 19:33:29 -0400 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 ESMTPS id 197NXJ80022225 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Thu, 7 Oct 2021 16:33:20 -0700 Authentication-Results: vinyl.outflux.net; dkim=none; dkim-atps=neutral From: Kees Cook To: Daniel Kiper Cc: Kees Cook , Petr Vorel , grub-devel@gnu.org Subject: [PATCH v2] osdep/linux: Fix md array device enumeration Date: Thu, 7 Oct 2021 16:33:16 -0700 Message-Id: <20211007233316.2320903-1-kees@ubuntu.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3106; i=keescook@chromium.org; h=from:subject; bh=akvXVwPMlTFxdxcqVryW9cv+D2YhctN3xfE8U8XOkDg=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBhX4O7yQw9u+FqdJBdBaj3ZG6t6wiiFbCp2GAIQkw5 cFYr9pKJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCYV+DuwAKCRCJcvTf3G3AJq36EA CFzb0igDoqX7GRax3hH6dXMbbo8NHkUFjMtvIdNg9Yr1TZFH+VlcsnOTc57WonK0E9DTWKKFQrTKbd WE8hvOOskFGax1bG/icW7eSFDTwwvAyxWw/dH/HANpbc8Rs4B0WmhEMATeMp8bXBnShcSkh2kxdRPf I5lVtzzvAv1VgF27wbOyjJu85CVyT8s2cGiqVucMY6ycYOps06CxOF2zGzpsP1v84FFS5XBzU6NzXX YVg7kPw5ySCoqjLMcpb62PGce4msPM5XcCT60RZ9VJNSleHMYjcAljBAkxpYFYg8xBudDpX/cgW6+Y H0oQe4mosyCjs7Os4rgRPoQTyVqKdoGtO8WcT3r7XXXZLcdJ6QKksEmzxnrkaq6fIGJ838wCOfx6yy A3frLhWkVAmqetRqzzXEhN6M2zhqOQfaldg1X79W3s67o9RfJzOzcog5Ot+QymBOesrwguUsMPMOZ2 AOI7PhGEVxjtPQQMihQG/PrnXZctboecmfn+0/3BNocG/2wq0HsxA8o6hBvYUYtfJJAEvFHxoHBF5o 03ZdjB2kbKgT45Ban8q0aj2O2WZuiWcQDbD6BiyBrV3h/rdiM/F/BxskoZ+j6u4LFJMcyBIZ264qe3 zlrLxdWIbvcQGJR7DrcmOKFe6aVf8qaHRWTAkr2vSsvGunmTOJ29d+J! ui07Q== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit 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: Thu, 07 Oct 2021 23:33:29 -0000 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). As part of this, since grub wants to only examine active (i.e. present and non-failed) disks, the count of remaining disks (remaining) must be tracked separately from the devicelist index (j). Fixes: 49de079bbe1c ("... (grub_util_raid_getmembers): Handle "removed" 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 Reviewed-by: Petr Vorel --- grub-core/osdep/linux/getroot.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/grub-core/osdep/linux/getroot.c b/grub-core/osdep/linux/getroot.c index cd588588eecf..90660107050b 100644 --- a/grub-core/osdep/linux/getroot.c +++ b/grub-core/osdep/linux/getroot.c @@ -130,10 +130,20 @@ 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; @@ -165,22 +175,23 @@ 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: MD_DISK_REMOVED slots don't contribute to "remaining" count. */ if (disk.state & (1 << MD_DISK_REMOVED)) continue; + remaining--; + /* Only record disks that are actively participating in the array. */ if (disk.state & (1 << MD_DISK_ACTIVE)) - devicelist[j] = grub_find_device (NULL, - makedev (disk.major, disk.minor)); - else - devicelist[j] = NULL; - j++; + devicelist[j++] = grub_find_device (NULL, + makedev (disk.major, disk.minor)); } devicelist[j] = NULL; -- 2.30.2