Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oMCJU-0006Hz-85 for mharc-grub-devel@gnu.org; Thu, 11 Aug 2022 13:49:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48970) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oMCJO-000690-Ah for grub-devel@gnu.org; Thu, 11 Aug 2022 13:49:38 -0400 Received: from mail-qv1-xf2f.google.com ([2607:f8b0:4864:20::f2f]:39923) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oMCJM-0005ng-C5 for grub-devel@gnu.org; Thu, 11 Aug 2022 13:49:38 -0400 Received: by mail-qv1-xf2f.google.com with SMTP id h8so13865578qvs.6 for ; Thu, 11 Aug 2022 10:49:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficientek-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=2qEhS3Pn/xJDV0/v6mTaMvMZIG2r8RK6fIqOC5g2QqU=; b=pKaIt2o+9PDyKZhQeVoUiIbhCbmCw1TP+YcEEMfOUjThC2t+bcy62jLzRiKmUlmNL9 a0vhNVba3V5UtWgn2b9uP63q+ugUNw7+E5TeQpvMmA+U2pX/iPl2OA4dpBZBl6SDsIYL TMeh9mhIWleSucu5P7FS3Y7TjYMOGuSfdWFEhDngWg9VSacTSQQ8duabIsWw6Oz9g6Xe p/8jC7FR/nQNoBIrMyM8VaBNklVa2Jclgy7e0deE9FxonVpD4cHDQ8ErbLTH8z0FhJtM OfxFcz5BJn8h9R4ZGnvqNvz+yPmKBQJIUTRp6uR61LYbLuInAcSt0xYakEs0vMbPJ7uu R1Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=2qEhS3Pn/xJDV0/v6mTaMvMZIG2r8RK6fIqOC5g2QqU=; b=F2oZf8V8vYYQPb544inwOaoXHZEaYPGIZ/P6wUZOAnCbg3bYZwuDOm5zN69FdM8ysS vD4HQLteK89rqmsHjJBdXPSGjnpAvHCW1roVBmSSlKFUYsLcnOJKn4Ty3J4KRo3HlRtj Wlj3SUkHcUYnOPqQRSZdYLxaJyVIYK2fxrY0+ZQ+sNOw0eCbRFN7HhQfNtBiFomQUlp7 2TkzwZKl4XAMe884zCnSZbgd1QWDeBlVeAghWqr+Yg832iFw1Fu6oSFJKRw6xVqwwPRJ eq8SmDm7Pi9vsAKmfEXxLyuG2K9m/2SpVa6whYfEvF0FUguwbXZVgwcQ1cwiQTp5ZMg3 DIgA== X-Gm-Message-State: ACgBeo0Fnq6FLPpgmMNWYPzIE3LWfQldgn776kcJcvpiY+ClfjX6R4CI ItvhtTeZ4HutaQXQNZmeTqlu3KJF+x1ME62K X-Google-Smtp-Source: AA6agR5vVG39lYQUFDpnewcXV1Yde66L/Pa/xuxplA1aaX3egV8Bg65GHW7eYH+Pz9d6ONNF8/PBVw== X-Received: by 2002:a05:6214:20a8:b0:477:1882:3e7 with SMTP id 8-20020a05621420a800b00477188203e7mr61373qvd.44.1660240174306; Thu, 11 Aug 2022 10:49:34 -0700 (PDT) Received: from localhost.localdomain ([199.58.83.12]) by smtp.gmail.com with ESMTPSA id s11-20020a05620a0bcb00b006b5c061844fsm2584657qki.49.2022.08.11.10.49.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Aug 2022 10:49:33 -0700 (PDT) From: Glenn Washburn To: grub-devel@gnu.org, Daniel Kiper Cc: Patrick Steinhardt , Glenn Washburn Subject: [PATCH v5 2/2] cryptodisk: Allows UUIDs to be compared in a dash-insensitive manner Date: Thu, 11 Aug 2022 12:48:43 -0500 Message-Id: <7d6d9dbebd6aaa0156c2655630634541b54f8215.1659560233.git.development@efficientek.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::f2f; envelope-from=development@efficientek.com; helo=mail-qv1-xf2f.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Aug 2022 17:49:38 -0000 A user can now specify UUID strings with dashes, instead of having to remove dashes. This is backwards-compatability preserving and also fixes a source of user confusion over the inconsistency with how UUIDs are specified between file system UUIDs and cryptomount UUIDs. Since cryptsetup, the reference implementation for LUKS, displays and generates UUIDs with dashes there has been additional confusion when using the UUID strings from cryptsetup as exact input into GRUB does not find the expected cryptodisk. A new function grub_uuidcasecmp is added that is general enough to be used other places where UUIDs are being compared. Signed-off-by: Glenn Washburn --- grub-core/disk/cryptodisk.c | 4 ++-- grub-core/disk/geli.c | 2 +- grub-core/disk/luks.c | 21 ++++----------------- grub-core/disk/luks2.c | 15 ++++----------- include/grub/misc.h | 28 ++++++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 31 deletions(-) diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c index f1fe0d390..00d66ab02 100644 --- a/grub-core/disk/cryptodisk.c +++ b/grub-core/disk/cryptodisk.c @@ -701,7 +701,7 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk) if (grub_memcmp (name, "cryptouuid/", sizeof ("cryptouuid/") - 1) == 0) { for (dev = cryptodisk_list; dev != NULL; dev = dev->next) - if (grub_strcasecmp (name + sizeof ("cryptouuid/") - 1, dev->uuid) == 0) + if (grub_uuidcasecmp (name + sizeof ("cryptouuid/") - 1, dev->uuid, sizeof (dev->uuid)) == 0) break; } else @@ -928,7 +928,7 @@ grub_cryptodisk_get_by_uuid (const char *uuid) { grub_cryptodisk_t dev; for (dev = cryptodisk_list; dev != NULL; dev = dev->next) - if (grub_strcasecmp (dev->uuid, uuid) == 0) + if (grub_uuidcasecmp (dev->uuid, uuid, sizeof (dev->uuid)) == 0) return dev; return NULL; } diff --git a/grub-core/disk/geli.c b/grub-core/disk/geli.c index e190066f9..722910d64 100644 --- a/grub-core/disk/geli.c +++ b/grub-core/disk/geli.c @@ -305,7 +305,7 @@ geli_scan (grub_disk_t disk, grub_cryptomount_args_t cargs) return NULL; } - if (cargs->search_uuid != NULL && grub_strcasecmp (cargs->search_uuid, uuid) != 0) + if (cargs->search_uuid != NULL && grub_uuidcasecmp (cargs->search_uuid, uuid, sizeof (uuid)) != 0) { grub_dprintf ("geli", "%s != %s\n", uuid, cargs->search_uuid); return NULL; diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c index 7f837d52c..9e1e6a5d9 100644 --- a/grub-core/disk/luks.c +++ b/grub-core/disk/luks.c @@ -66,10 +66,7 @@ static grub_cryptodisk_t luks_scan (grub_disk_t disk, grub_cryptomount_args_t cargs) { grub_cryptodisk_t newdev; - const char *iptr; struct grub_luks_phdr header; - char *optr; - char uuid[sizeof (header.uuid) + 1]; char ciphername[sizeof (header.cipherName) + 1]; char ciphermode[sizeof (header.cipherMode) + 1]; char hashspec[sizeof (header.hashSpec) + 1]; @@ -92,19 +89,9 @@ luks_scan (grub_disk_t disk, grub_cryptomount_args_t cargs) || grub_be_to_cpu16 (header.version) != 1) return NULL; - grub_memset (uuid, 0, sizeof (uuid)); - optr = uuid; - for (iptr = header.uuid; iptr < &header.uuid[ARRAY_SIZE (header.uuid)]; - iptr++) + if (cargs->search_uuid != NULL && grub_uuidcasecmp (cargs->search_uuid, header.uuid, sizeof (header.uuid)) != 0) { - if (*iptr != '-') - *optr++ = *iptr; - } - *optr = 0; - - if (cargs->search_uuid != NULL && grub_strcasecmp (cargs->search_uuid, uuid) != 0) - { - grub_dprintf ("luks", "%s != %s\n", uuid, cargs->search_uuid); + grub_dprintf ("luks", "%s != %s\n", header.uuid, cargs->search_uuid); return NULL; } @@ -123,7 +110,7 @@ luks_scan (grub_disk_t disk, grub_cryptomount_args_t cargs) newdev->source_disk = NULL; newdev->log_sector_size = GRUB_LUKS1_LOG_SECTOR_SIZE; newdev->total_sectors = grub_disk_native_sectors (disk) - newdev->offset_sectors; - grub_memcpy (newdev->uuid, uuid, sizeof (uuid)); + grub_memcpy (newdev->uuid, header.uuid, sizeof (header.uuid)); newdev->modname = "luks"; /* Configure the hash used for the AF splitter and HMAC. */ @@ -143,7 +130,7 @@ luks_scan (grub_disk_t disk, grub_cryptomount_args_t cargs) return NULL; } - COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= sizeof (uuid)); + COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= sizeof (header.uuid)); return newdev; } diff --git a/grub-core/disk/luks2.c b/grub-core/disk/luks2.c index bf741d70f..ac3ab095c 100644 --- a/grub-core/disk/luks2.c +++ b/grub-core/disk/luks2.c @@ -350,8 +350,6 @@ luks2_scan (grub_disk_t disk, grub_cryptomount_args_t cargs) { grub_cryptodisk_t cryptodisk; grub_luks2_header_t header; - char uuid[sizeof (header.uuid) + 1]; - grub_size_t i, j; if (cargs->check_boot) return NULL; @@ -362,14 +360,9 @@ luks2_scan (grub_disk_t disk, grub_cryptomount_args_t cargs) return NULL; } - for (i = 0, j = 0; i < sizeof (header.uuid); i++) - if (header.uuid[i] != '-') - uuid[j++] = header.uuid[i]; - uuid[j] = '\0'; - - if (cargs->search_uuid != NULL && grub_strcasecmp (cargs->search_uuid, uuid) != 0) + if (cargs->search_uuid != NULL && grub_uuidcasecmp (cargs->search_uuid, header.uuid, sizeof (header.uuid)) != 0) { - grub_dprintf ("luks2", "%s != %s\n", uuid, cargs->search_uuid); + grub_dprintf ("luks2", "%s != %s\n", header.uuid, cargs->search_uuid); return NULL; } @@ -377,8 +370,8 @@ luks2_scan (grub_disk_t disk, grub_cryptomount_args_t cargs) if (!cryptodisk) return NULL; - COMPILE_TIME_ASSERT (sizeof (cryptodisk->uuid) >= sizeof (uuid)); - grub_memcpy (cryptodisk->uuid, uuid, sizeof (uuid)); + COMPILE_TIME_ASSERT (sizeof (cryptodisk->uuid) >= sizeof (header.uuid)); + grub_memcpy (cryptodisk->uuid, header.uuid, sizeof (header.uuid)); cryptodisk->modname = "luks2"; return cryptodisk; diff --git a/include/grub/misc.h b/include/grub/misc.h index 1c25c1767..2ed378188 100644 --- a/include/grub/misc.h +++ b/include/grub/misc.h @@ -244,6 +244,34 @@ grub_strncasecmp (const char *s1, const char *s2, grub_size_t n) - (int) grub_tolower ((grub_uint8_t) *s2); } +/* Do a case insensitive compare of two UUID strings by ignoring all dashes */ +static inline int +grub_uuidcasecmp (const char *uuid1, const char *uuid2, grub_size_t n) +{ + if (n == 0) + return 0; + + while (*uuid1 && *uuid2 && --n) + { + /* Skip forward to non-dash on both UUIDs. */ + while ('-' == *uuid1) + ++uuid1; + + while ('-' == *uuid2) + ++uuid2; + + if (grub_tolower ((grub_uint8_t) *uuid1) + != grub_tolower ((grub_uint8_t) *uuid2)) + break; + + uuid1++; + uuid2++; + } + + return (int) grub_tolower ((grub_uint8_t) *uuid1) + - (int) grub_tolower ((grub_uint8_t) *uuid2); +} + /* * Note that these differ from the C standard's definitions of strtol, * strtoul(), and strtoull() by the addition of two const qualifiers on the end -- 2.34.1