Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1mvN0w-0002Ir-RV for mharc-grub-devel@gnu.org; Thu, 09 Dec 2021 12:15:26 -0500 Received: from eggs.gnu.org ([209.51.188.92]:44826) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mvN0r-0002FC-6g for grub-devel@gnu.org; Thu, 09 Dec 2021 12:15:21 -0500 Received: from [2607:f8b0:4864:20::72b] (port=33611 helo=mail-qk1-x72b.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mvN0o-00056b-Da for grub-devel@gnu.org; Thu, 09 Dec 2021 12:15:19 -0500 Received: by mail-qk1-x72b.google.com with SMTP id de30so5525855qkb.0 for ; Thu, 09 Dec 2021 09:15:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficientek-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OUD9HiVAbdjw5Wuq8+Lpl6eMObHqf5rkWdCMck+ITeA=; b=BahPgRMkRoJMEvDTpsW4EJgYgTw53NMQD2oXc/WHwHzozsJsAVPhwE97yeVUyiNoSh eqSaDoRtcFfUBuOfKTDtqEaiQ1vahq+wcM77B+C/1359BooaHYSpuHTSx9SymdqHudEq 675j6W3lo7f7ZAR4hc41SIpvuEKiqnRMjjfKxTqzkZgedrvJjnt6BigDkriI9PvM+Frc 0uSRfxHzJEmRgBa3Kq7/DQMntxtp/Apq74HZoodRK8QbVOJ0YFw0s6fj0jemC04P4D0s d0eBBSaegS09I0eDUTL7vHWysGHiMcKj/wxj3p5PiIgLwvhJ5jeSfiRdSZHt0NbK39l6 957g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OUD9HiVAbdjw5Wuq8+Lpl6eMObHqf5rkWdCMck+ITeA=; b=wkbUDwpm9Ve0oN42NSu+VkPLpXd9sbej5N9Vgbu9wIrDSjZn3ZuhaDvBC8a3VnOprZ edwYnGKRbDNLn0DBs5aj9Olvo8cd+Q8ywEw+pu7MJn2A5QUz2GucE2o8lV3JQXB1JJW3 0yP7KTDUtp69ZAzFHgK/JNtvJpq1+dbauk9bS0BFiQ+EvytZ7oV0DIneFqKzjZJhGa1a hesbcIGubAD901rIw6DxAAYyjJo2pnR9dv/1/VsB9jTOvBqAzuf1+2ykrsoo/Z4oiE/P 9xfqfIY+USQefscrY4dvF4TJO3VgF5xeMtYOb9QM7tiCUM/N7ErO4j715Gz0cmerG8I1 VsIg== X-Gm-Message-State: AOAM531ok+RiLCtIiUDCnRjH0rm9gFJxA0fU4Zi1Ln74WsvpVcTfPC3M Ke1t8Zgu6KktkPxf3gadlGh0mw== X-Google-Smtp-Source: ABdhPJxf4HarzF/kpPoo2SkRlBa6L/17l/ERgh7apr21/W9JKB9fDYfdyDSsp+eyOGOuQcjfdup06w== X-Received: by 2002:a05:620a:29d0:: with SMTP id s16mr15011220qkp.219.1639070117530; Thu, 09 Dec 2021 09:15:17 -0800 (PST) Received: from localhost.localdomain ([37.218.244.251]) by smtp.gmail.com with ESMTPSA id m9sm151839qkn.59.2021.12.09.09.15.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Dec 2021 09:15:17 -0800 (PST) From: Glenn Washburn To: Daniel Kiper , grub-devel@gnu.org Cc: Denis 'GNUtoo' Carikli , Patrick Steinhardt , James Bottomley , Glenn Washburn Subject: [PATCH v5 2/9] cryptodisk: Refactor to discard have_it global Date: Thu, 9 Dec 2021 11:14:51 -0600 Message-Id: <5056163ca5fe9f324766e0ada5f44a12f7fe852f.1639069499.git.development@efficientek.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::72b (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::72b; envelope-from=development@efficientek.com; helo=mail-qk1-x72b.google.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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, 09 Dec 2021 17:15:21 -0000 The global "have_it" was never used by the crypto-backends, but was used to determine if a crypto-backend successfully mounted a cryptodisk with a given uuid. This is not needed however, because grub_device_iterate() will return 1 if and only if grub_cryptodisk_scan_device() returns 1. And grub_cryptodisk_scan_device() will now only return 1 if a search_uuid has been specified and a cryptodisk was successfully setup by a crypto-backend or a cryptodisk of the requested uuid is already open. To implement this grub_cryptodisk_scan_device_real is modified to return a cryptodisk or NULL on failure and having the appropriate grub_errno set to indicated failure. Note that grub_cryptodisk_scan_device_real will fail now with a new errno GRUB_ERR_BAD_MODULE when none of the cryptodisk backend modules succeed in identifying the source disk. With this change grub_device_iterate() will return 1 when a crypto device is successfully decrypted or when the source device has already been successfully opened. Prior to this change, trying to mount an already successfully opened device would trigger an error with the message "no such cryptodisk found", which is at best misleading. The mount should silently succeed in this case, which is what happens with this patch. Signed-off-by: Glenn Washburn --- grub-core/disk/cryptodisk.c | 56 +++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c index 90f82b2d3..9df3d310f 100644 --- a/grub-core/disk/cryptodisk.c +++ b/grub-core/disk/cryptodisk.c @@ -983,7 +983,7 @@ grub_util_cryptodisk_get_uuid (grub_disk_t disk) #endif -static int check_boot, have_it; +static int check_boot; static char *search_uuid; static void @@ -995,7 +995,7 @@ cryptodisk_close (grub_cryptodisk_t dev) grub_free (dev); } -static grub_err_t +static grub_cryptodisk_t grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source) { grub_err_t err; @@ -1005,13 +1005,13 @@ grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source) dev = grub_cryptodisk_get_by_source_disk (source); if (dev) - return GRUB_ERR_NONE; + return dev; FOR_CRYPTODISK_DEVS (cr) { dev = cr->scan (source, search_uuid, check_boot); if (grub_errno) - return grub_errno; + return NULL; if (!dev) continue; @@ -1019,16 +1019,16 @@ grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source) if (err) { cryptodisk_close (dev); - return err; + return NULL; } grub_cryptodisk_insert (dev, name, source); - have_it = 1; - - return GRUB_ERR_NONE; + return dev; } - return GRUB_ERR_NONE; + + grub_error (GRUB_ERR_BAD_MODULE, "no cryptodisk module can handle this device"); + return NULL; } #ifdef GRUB_UTIL @@ -1082,8 +1082,10 @@ static int grub_cryptodisk_scan_device (const char *name, void *data __attribute__ ((unused))) { - grub_err_t err; + int ret = 0; grub_disk_t source; + grub_cryptodisk_t dev; + grub_errno = GRUB_ERR_NONE; /* Try to open disk. */ source = grub_disk_open (name); @@ -1093,13 +1095,26 @@ grub_cryptodisk_scan_device (const char *name, return 0; } - err = grub_cryptodisk_scan_device_real (name, source); + dev = grub_cryptodisk_scan_device_real (name, source); + if (dev) + { + ret = (search_uuid != NULL && grub_strcasecmp (search_uuid, dev->uuid) == 0); + goto cleanup; + } - grub_disk_close (source); - - if (err) + /* + * Do not print error when err is GRUB_ERR_BAD_MODULE to avoid many unhelpful + * error messages. + */ + if (grub_errno == GRUB_ERR_BAD_MODULE) + grub_error_pop (); + + if (grub_errno != GRUB_ERR_NONE) grub_print_error (); - return have_it && search_uuid ? 1 : 0; + + cleanup: + grub_disk_close (source); + return ret; } static grub_err_t @@ -1110,9 +1125,9 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) if (argc < 1 && !state[1].set && !state[2].set) return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required"); - have_it = 0; if (state[0].set) { + int found_uuid; grub_cryptodisk_t dev; dev = grub_cryptodisk_get_by_uuid (args[0]); @@ -1125,10 +1140,10 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) check_boot = state[2].set; search_uuid = args[0]; - grub_device_iterate (&grub_cryptodisk_scan_device, NULL); + found_uuid = grub_device_iterate (&grub_cryptodisk_scan_device, NULL); search_uuid = NULL; - if (!have_it) + if (!found_uuid) return grub_error (GRUB_ERR_BAD_ARGUMENT, "no such cryptodisk found"); return GRUB_ERR_NONE; } @@ -1142,7 +1157,6 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) } else { - grub_err_t err; grub_disk_t disk; grub_cryptodisk_t dev; char *diskname; @@ -1178,13 +1192,13 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) return GRUB_ERR_NONE; } - err = grub_cryptodisk_scan_device_real (diskname, disk); + dev = grub_cryptodisk_scan_device_real (diskname, disk); grub_disk_close (disk); if (disklast) *disklast = ')'; - return err; + return (dev == NULL) ? grub_errno : GRUB_ERR_NONE; } } -- 2.27.0