Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1lO4LZ-0002vH-Q6 for mharc-grub-devel@gnu.org; Sun, 21 Mar 2021 16:06:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34482) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lO4LX-0002p3-03 for grub-devel@gnu.org; Sun, 21 Mar 2021 16:06:47 -0400 Received: from mail-qv1-xf2d.google.com ([2607:f8b0:4864:20::f2d]:36520) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lO4LM-0002SW-PB for grub-devel@gnu.org; Sun, 21 Mar 2021 16:06:46 -0400 Received: by mail-qv1-xf2d.google.com with SMTP id x16so7722431qvk.3 for ; Sun, 21 Mar 2021 13:06:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficientek-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=taCQcZYos3u8ix70y/QDSB46+i4188wO3AIEXaYDQ8U=; b=0y3TD+CLlslPwZzmSAgeYVV0mJTzkZuJaJiSbF7TnJ9czNLh0V9A9UEeSSWgx5qZrU fLdIxbnkcUgTsXUVeVveFHwW3SO7oVGlzMACEIwZXEYzSSAIARIirYiwE/zjFLRFn4Wh KIqLoyDFwN+eIdUDpmlpCW+X2OOHjhQfU9ML25RpNDJtmQFC4uW51A8eGs9YEAAEq1Bh v5wNA0A/+FjIqCIRGrsZzTCHTYrBZZ/I+0jOnXDNskIO2qWI3QCyKyquNvYIkNZu6JY9 QYYCMKOu7pOAzEE6uP5wTeEdk0LdnaFp0mntz9BHsbQKx4xFsEz5+BKYkIi0YqdcskvR iZJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=taCQcZYos3u8ix70y/QDSB46+i4188wO3AIEXaYDQ8U=; b=do9LN0bwKjW6w98m1cXLdtEgz+jZL9PCxjZqxhDXRX0v1defIRS93ZIf2FGvF+HBZa 2VVLmxZV66xwbz6JYzyU8W+4yIvgavw4WFZyhVCCernIUY9kekvf/mbTyQATDXqI9oyd KeEI1p/O9gidbJUjoJ3BEFR9Uo0k0eh6nVchwguykqTagFbkILzD8pmKNcHn1KEN7CRA 4pG7lJ1wINdo2Z1N/mSlePm/x6Ru60Ar+/4gKqXMEhiSNHG74694yztTl8hyOOnOci5Q AhqLWRX9Q0o2dMCy65qgnpbj+d9ImSW8VS5MbHAzLW4kpWNPBPsXjUdBPgleBGvREU0I KrkQ== X-Gm-Message-State: AOAM530AKKOU/1qBYmHLM2dv8+/djBQGeKmK5jruzqf9oSH4bAp5ZIId VLHA7O1POlXqkYB6gWmodmVEEaBj8tR2jw== X-Google-Smtp-Source: ABdhPJysxRFgenEDHG8nRQU2PCqYIR1VHNacAs90t9sBw2ekeRo/Qaoqzjk7ljbO2oBnLx7iNrBf+g== X-Received: by 2002:a0c:e148:: with SMTP id c8mr18485102qvl.56.1616357195307; Sun, 21 Mar 2021 13:06:35 -0700 (PDT) Received: from localhost.localdomain ([2605:a601:ab16:db00:23af:6780:efd4:8e25]) by smtp.gmail.com with ESMTPSA id q15sm7643284qtx.47.2021.03.21.13.06.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Mar 2021 13:06:34 -0700 (PDT) From: Glenn Washburn To: grub-devel@gnu.org, Daniel Kiper Cc: Glenn Washburn Subject: [PATCH v2 2/2] cryptodisk: Allows UUIDs to be compared in a dash-insensitive manner Date: Sun, 21 Mar 2021 15:06:22 -0500 Message-Id: X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::f2d; envelope-from=development@efficientek.com; helo=mail-qv1-xf2d.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 autolearn=ham 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: Sun, 21 Mar 2021 20:06:47 -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 | 13 +++---------- include/grub/misc.h | 27 +++++++++++++++++++++++++++ 5 files changed, 37 insertions(+), 30 deletions(-) diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c index 90f82b2d3..f1a30d80c 100644 --- a/grub-core/disk/cryptodisk.c +++ b/grub-core/disk/cryptodisk.c @@ -678,7 +678,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 @@ -908,7 +908,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 2f34a35e6..f7fac73df 100644 --- a/grub-core/disk/geli.c +++ b/grub-core/disk/geli.c @@ -304,7 +304,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid, return NULL; } - if (check_uuid && grub_strcasecmp (check_uuid, uuid) != 0) + if (check_uuid && grub_uuidcasecmp (check_uuid, uuid, sizeof (uuid)) != 0) { grub_dprintf ("geli", "%s != %s\n", uuid, check_uuid); return NULL; diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c index 9a7323920..eed0345b4 100644 --- a/grub-core/disk/luks.c +++ b/grub-core/disk/luks.c @@ -69,10 +69,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid, int check_boot) { 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]; @@ -98,19 +95,9 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid, || 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 (check_uuid && grub_uuidcasecmp (check_uuid, header.uuid, sizeof (header.uuid)) != 0) { - if (*iptr != '-') - *optr++ = *iptr; - } - *optr = 0; - - if (check_uuid && grub_strcasecmp (check_uuid, uuid) != 0) - { - grub_dprintf ("luks", "%s != %s\n", uuid, check_uuid); + grub_dprintf ("luks", "%s != %s\n", header.uuid, check_uuid); return NULL; } @@ -129,7 +116,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid, 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. */ @@ -149,7 +136,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid, 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 a7e614470..e723977ee 100644 --- a/grub-core/disk/luks2.c +++ b/grub-core/disk/luks2.c @@ -355,8 +355,6 @@ luks2_scan (grub_disk_t disk, const char *check_uuid, int check_boot) { grub_cryptodisk_t cryptodisk; grub_luks2_header_t header; - char uuid[sizeof (header.uuid) + 1]; - grub_size_t i, j; if (check_boot) return NULL; @@ -367,20 +365,15 @@ luks2_scan (grub_disk_t disk, const char *check_uuid, int check_boot) 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 (check_uuid && grub_strcasecmp (check_uuid, uuid) != 0) + if (check_uuid && grub_uuidcasecmp (check_uuid, header.uuid, sizeof (header.uuid)) != 0) return NULL; cryptodisk = grub_zalloc (sizeof (*cryptodisk)); 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 7d2b55196..f0141a6cf 100644 --- a/include/grub/misc.h +++ b/include/grub/misc.h @@ -27,6 +27,8 @@ #include #include +#define GRUB_MAX_UUID_LENGTH 71 + #define ALIGN_UP(addr, align) \ (((addr) + (typeof (addr)) (align) - 1) & ~((typeof (addr)) (align) - 1)) #define ALIGN_UP_OVERHEAD(addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1)) @@ -243,6 +245,31 @@ 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 first stripping out + * all dashes from both UUID arguments and then doing a regular + * grub_strncasecmp. + */ +static inline int +grub_uuidcasecmp (const char *uuid1, const char *uuid2, grub_size_t n) +{ + char *ptr; + char ruuid1[GRUB_MAX_UUID_LENGTH + 1]; + char ruuid2[GRUB_MAX_UUID_LENGTH + 1]; + + for (ptr = ruuid1; *uuid1; uuid1++) + if (*uuid1 != '-') + *(ptr++) = *uuid1; + *ptr = '\0'; + + for (ptr = ruuid2; *uuid2; uuid2++) + if (*uuid2 != '-') + *(ptr++) = *uuid2; + *ptr = '\0'; + + return grub_strncasecmp (ruuid1, ruuid2, n); +} + /* * 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.27.0