From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1o0l4n-0008E9-Qd for mharc-grub-devel@gnu.org; Mon, 13 Jun 2022 10:30:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47182) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o0l4l-0008C6-6x for grub-devel@gnu.org; Mon, 13 Jun 2022 10:29:55 -0400 Received: from smtp-out1.suse.de ([195.135.220.28]:33630) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o0l4i-0006zD-Be for grub-devel@gnu.org; Mon, 13 Jun 2022 10:29:53 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 76D4821C90; Mon, 13 Jun 2022 14:29:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1655130589; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jjh/8n24jnxxBHvySK1jKqKGSSpwkejREaFvmfYddSc=; b=utQYd7KL8b4W52BOyP92c278RJaf7jtWhoOKCoactei/9cZg2y4s+HxD7eMOZLTsJjgclY hyJ7VOxvFcyUXaCgwyo/3GHaQSX3VJQL67rBKl3mvHgHZm8+FXLq739DrMoVtCVeGoyszT x2TFZTRzYQmD8Q3E92Sza6SpPLpzKL4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1655130589; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jjh/8n24jnxxBHvySK1jKqKGSSpwkejREaFvmfYddSc=; b=LB19QV8VfOBBKmWLrCf/udWytdQhTKGl3+XHIPwFqi7mAItPkHe2+qN5idulhqfWRzSjlR dbq2CHR4KtTrcFAw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 1ECEE13443; Mon, 13 Jun 2022 14:29:49 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id W6yPBd1Jp2JcRQAAMHmgww (envelope-from ); Mon, 13 Jun 2022 14:29:49 +0000 From: Fabian Vogt To: "grub-devel@gnu.org" Cc: Josselin Poiret , Glenn Washburn , Patrick Steinhardt , Michael Chang , Pierre-Louis Bonicoli , aplanas@suse.de Subject: [PATCH v2] disk/cryptodisk: When cheatmounting, use the sector info of the cheat device Date: Mon, 13 Jun 2022 16:29:48 +0200 Message-ID: <8075647.T7Z3S40VBb@linux-e202.suse.de> In-Reply-To: <20220520191353.2d55b59a@crass-HP-ZBook-15-G2> References: <877da6rf6s.fsf@jpoiret.xyz> <20220520191353.2d55b59a@crass-HP-ZBook-15-G2> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Received-SPF: pass client-ip=195.135.220.28; envelope-from=fvogt@suse.de; helo=smtp-out1.suse.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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: Mon, 13 Jun 2022 14:29:55 -0000 When using grub-probe with cryptodisk, the mapped block device from the host is used directly instead of decrypting the source device in GRUB code. In that case, the sector size and count of the host device needs to be used. This is especially important when using luks2, which does not assign total_sectors and log_sector_size when scanning, but only later when the segments in the JSON area are evaluated. With an unset log_sector_size, grub_open_device complains. This fixes grub-probe failing with "error: sector sizes of 1 bytes aren't supported yet." Signed-off-by: Fabian Vogt --- v2: Moved new code from grub_cryptodisk_cheat_mount to grub_cryptodisk_open, which allowed to simplify the code a bit. Also improved error handling. grub-core/disk/cryptodisk.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c index c80cf9907..14e105b84 100644 --- a/grub-core/disk/cryptodisk.c +++ b/grub-core/disk/cryptodisk.c @@ -686,6 +686,11 @@ static grub_err_t grub_cryptodisk_open (const char *name, grub_disk_t disk) { grub_cryptodisk_t dev; +#ifdef GRUB_UTIL + grub_uint64_t cheat_dev_size; + unsigned int cheat_log_sector_size; + const char *errmsg; +#endif if (grub_memcmp (name, "crypto", sizeof ("crypto") - 1) != 0) return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "No such device"); @@ -709,8 +714,6 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk) if (!dev) return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "No such device"); - disk->log_sector_size = dev->log_sector_size; - #ifdef GRUB_UTIL if (dev->cheat) { @@ -719,6 +722,20 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk) if (!GRUB_UTIL_FD_IS_VALID (dev->cheat_fd)) return grub_error (GRUB_ERR_IO, N_("cannot open `%s': %s"), dev->cheat, grub_util_fd_strerror ()); + + /* Use the sector size and count of the cheat device */ + cheat_dev_size = grub_util_get_fd_size (dev->cheat_fd, dev->cheat, &cheat_log_sector_size); + if (cheat_dev_size == -1) + { + errmsg = grub_util_fd_strerror (); + grub_util_fd_close (dev->cheat_fd); + dev->cheat_fd = GRUB_UTIL_FD_INVALID; + return grub_error (GRUB_ERR_IO, N_("failed to query size of device `%s': %s"), + dev->cheat, errmsg); + } + + dev->log_sector_size = cheat_log_sector_size; + dev->total_sectors = cheat_dev_size >> dev->log_sector_size; } #endif @@ -732,6 +749,7 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk) } disk->data = dev; + disk->log_sector_size = dev->log_sector_size; disk->total_sectors = dev->total_sectors; disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE; disk->id = dev->id; -- 2.36.1