Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1k1TkL-0006wf-QQ for mharc-grub-devel@gnu.org; Fri, 31 Jul 2020 08:02:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45284) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k1TkJ-0006s3-Vj for grub-devel@gnu.org; Fri, 31 Jul 2020 08:02:44 -0400 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]:54437) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k1TkH-0005W8-VZ for grub-devel@gnu.org; Fri, 31 Jul 2020 08:02:43 -0400 Received: by mail-pj1-x102f.google.com with SMTP id mt12so7169045pjb.4 for ; Fri, 31 Jul 2020 05:02:41 -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=n829BCXf8xWJDlFljsSw8I7P+Lnf12eFRdO2Spo6I0Y=; b=0wVvjaIHHC+MTmq7Nyaqf8ephRBETtefurZ/SyLr1FqSb/pEQ7TKepkbEjN7gWUsgK Po4P2h6vjBcQUVmBpvifLPXmnMJzU15NRvGetYf6t4C8Nwi/7lWDcrYapvw9c+Znm+eJ 2UXNr5RiqQPfnkP02Z6Xpn+tX7eXh4hiI9zIEPXypcR/Wb6Gg75fb4p4pYD93ZIG60eq UFCXkUqCzUK1v15e+WbSDslGS9+YnfKrQxzdOdb7a4Grg4NMfMT2DaRGrH6O/Ky1eaMm h6c1y9VxThUJ4PbGb0Y2balYv2sQzr2ju6wwrQRWtuAhj9ElZTT3yFTQ4XNOdYytyV0n Q09w== 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=n829BCXf8xWJDlFljsSw8I7P+Lnf12eFRdO2Spo6I0Y=; b=GA+M22z2kBWmYuSLS0yZ6p9C8mFA+A14vevTWNYxl8JZJML4RycWLJMBX3vJnMWRO0 hreot+b/GF5Tn5vPfJHuvf07dG/wINO+9tyStN/7PaUA90qgU3Y4+LJZVuQNPqsyMhFn NBTEkRIiv5cAQSRxdWonKBcWFSlSIPdRh170jMF0p+aW9iRHI+Z90lkUkc75OJYl2d4T mvo9gimY1u2dpmreXRn6Zm0xZbt1z0tNqYsyBp3UBv3pMy0TC0rXoQdWy+MZX6K+M+8N ftAyzoyN4R4V/EVLtfYyO1IEsTye7aUr8R3IFt7tC2W6lH8C/hlHUWao7It3Zxc7FtL1 F3Xg== X-Gm-Message-State: AOAM532PZqWel/f2W7bCf9rNV0/Whag0RZf/iIZmkQuhoA3DTiRHnVTG XQPMF64/kGXkVmGMOfYBkoWJcLlcls8= X-Google-Smtp-Source: ABdhPJysN4WcU4zWROFINWq78xk/+olkvqgBpxnrpK45cyhO+MjCqn/ugHZeDz00vOnD/oQs2i8MfA== X-Received: by 2002:a62:7852:: with SMTP id t79mr3438299pfc.174.1596196960113; Fri, 31 Jul 2020 05:02:40 -0700 (PDT) Received: from localhost.localdomain ([189.216.105.24]) by smtp.gmail.com with ESMTPSA id o4sm9781590pfd.25.2020.07.31.05.02.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Jul 2020 05:02:39 -0700 (PDT) From: Glenn Washburn To: grub-devel@gnu.org Cc: Glenn Washburn , Daniel Kiper , Patrick Steinhardt Subject: [CRYPTO-LUKS v1 08/19] cryptodisk, luks: Allow special processing for comparing UUIDs. Date: Fri, 31 Jul 2020 07:01:49 -0500 Message-Id: <0bb8c2733f0ea3911537bc559bc044ac371d0fb8.1596196383.git.development@efficientek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::102f; envelope-from=development@efficientek.com; helo=mail-pj1-x102f.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. 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: Fri, 31 Jul 2020 12:02:44 -0000 Create grub_uuidcasecmp to compare UUIDs in a case-insensitive manner and that ignores '-' characters. This is backwards compatible with the old LUKS1 code that stored and compared against UUIDs without dashes. However, the new LUKS2 code stores and compares UUIDs that contain dashes. Really, the UUID comparison shouldn't care about the dashes, as this change implements. Now your old scripts will continue to work with UUIDs without dashes, but you may choose to use UUIDs with dashes now too for both LUKS1 and LUKS2. Signed-off-by: Glenn Washburn --- grub-core/disk/cryptodisk.c | 4 ++-- grub-core/disk/luks.c | 20 ++++---------------- grub-core/disk/luks2.c | 2 +- include/grub/misc.h | 21 +++++++++++++++++++++ 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c index 847337072..e7a4b8082 100644 --- a/grub-core/disk/cryptodisk.c +++ b/grub-core/disk/cryptodisk.c @@ -660,7 +660,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) == 0) break; } else @@ -897,7 +897,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) == 0) return dev; return NULL; } diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c index 6ae162601..ea54a9d10 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]; @@ -95,18 +92,9 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid, || grub_be_to_cpu16 (header.version) != 1) return NULL; - optr = uuid; - for (iptr = header.uuid; iptr < &header.uuid[ARRAY_SIZE (header.uuid)]; - iptr++) + if (check_uuid && grub_uuidcasecmp (check_uuid, 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; } @@ -125,7 +113,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid, newdev->source_disk = NULL; newdev->log_sector_size = 9; newdev->total_length = grub_disk_get_size (disk) - newdev->offset; - grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid)); + grub_memcpy (newdev->uuid, header.uuid, sizeof (newdev->uuid)); newdev->modname = "luks"; /* Configure the hash used for the AF splitter and HMAC. */ @@ -145,7 +133,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 41329f745..7f5deb469 100644 --- a/grub-core/disk/luks2.c +++ b/grub-core/disk/luks2.c @@ -356,7 +356,7 @@ luks2_scan (grub_disk_t disk, const char *check_uuid, int check_boot) return NULL; } - if (check_uuid && grub_strcasecmp (check_uuid, header.uuid) != 0) + if (check_uuid && grub_uuidcasecmp (check_uuid, header.uuid) != 0) return NULL; cryptodisk = grub_zalloc (sizeof (*cryptodisk)); diff --git a/include/grub/misc.h b/include/grub/misc.h index b7ca6dd58..3f0f42c22 100644 --- a/include/grub/misc.h +++ b/include/grub/misc.h @@ -243,6 +243,27 @@ grub_strncasecmp (const char *s1, const char *s2, grub_size_t n) - (int) grub_tolower ((grub_uint8_t) *s2); } +static inline int +grub_uuidcasecmp (const char *uuid1, const char *uuid2) +{ + while (*uuid1 && *uuid2) + { + while (*uuid1 == '-') + uuid1++; + while (*uuid2 == '-') + uuid2++; + + if (grub_tolower (*uuid1) != grub_tolower (*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.25.1