Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nqibw-0001qY-JE for mharc-grub-devel@gnu.org; Mon, 16 May 2022 17:50:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41274) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nqibe-0001b7-AV for grub-devel@gnu.org; Mon, 16 May 2022 17:50:23 -0400 Received: from mail-vs1-xe2e.google.com ([2607:f8b0:4864:20::e2e]:37563) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nqibb-00086b-MA for grub-devel@gnu.org; Mon, 16 May 2022 17:50:21 -0400 Received: by mail-vs1-xe2e.google.com with SMTP id t85so16943544vst.4 for ; Mon, 16 May 2022 14:50:18 -0700 (PDT) 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=SGx/CCTVkmd9fVstbO2rexn4KYrnABVq753/IPkEcsw=; b=lGpXu1kDPef1cDCZ8VamwmBZ7zZ1YkGsqftiyQkE9YJIVKzvLTo5iEqghW1CL6P92E AWlmCcZNplxlWfA+6e6FoRHovHyt7wCWyYGP1YB8JsK2KczlRkPyvyoOTo/LhQnqOPLz RMlZXWzDCCKc3OgbGhGsNrEISuCb4QPOzvUIxA+R2gh+8TMRnmxKah44Xdw9tR/TmIDb zW2HIVVSNuNanfbYv9JQFXh4xfyL75wFDKWEBVg6m73Wte7ynJLfrozub4KWYm2Yr1nO rUkilzl+bSRHlY/xUr5Ya9IGip9GAw7oNWqeUV+ORWNgMCrCTw997lN+twIpia0NBsF0 QQ9g== 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=SGx/CCTVkmd9fVstbO2rexn4KYrnABVq753/IPkEcsw=; b=GsQYJVriMZdVSkXxZSrmZn1SYBQMygwT8XGMmRdZE/abde9pWDjpezGLTIQVpTjt9v B3JR+na/RQiglizfbdb+TexNHTd9k8uFKihgeuylcnPn47bLcVaR19Hq9jzvqrtb+8Bq gWkXadsPqMa6yb8gUK+4/Rknh1c2IVkfJTehAR4080+hqRzxgtnpOp51t5j5H0x4ZKQ0 iJnrWt7K9kmnyGwXlhdpVhNLkGyYJYkB+aVCCU+1X0uIgLhfFjFvmaSigraIwxtLkpxn UPL7t8xYNeXsFV2o3XLzZWy+ApAtn/fuwRSxQ70JmSQNmEtWcH8psE21QerWLaY5ovkK ZSGQ== X-Gm-Message-State: AOAM533jRcNhVzoZutCq8iMeJ9SzwaV2cYGZfayPW5mffOI9H7zNYvfG N4co7ZzdpTJ8cpwwJtSNEpbciHRYZZk5hlB+ X-Google-Smtp-Source: ABdhPJyOPSx8D15xLxzMkhHDqkuadIMfr2y1sgAJ8DzkEhcvN746SFTcgDbhm6lKw7oauNhCFhAUiA== X-Received: by 2002:a67:c508:0:b0:32c:d8fc:18c0 with SMTP id e8-20020a67c508000000b0032cd8fc18c0mr7679388vsk.72.1652737817191; Mon, 16 May 2022 14:50:17 -0700 (PDT) Received: from localhost.localdomain ([37.218.244.249]) by smtp.gmail.com with ESMTPSA id z7-20020a673307000000b0032d275e691esm1018581vsz.30.2022.05.16.14.50.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 May 2022 14:50:16 -0700 (PDT) From: Glenn Washburn To: grub-devel@gnu.org, Daniel Kiper Cc: Denis 'GNUtoo' Carikli , Patrick Steinhardt , John Lane , Glenn Washburn Subject: [PATCH v2 2/3] cryptodisk: Add support for using detached header files Date: Mon, 16 May 2022 16:49:47 -0500 Message-Id: <25db3635ff873144d749c29e12e996af03600ab2.1652736873.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::e2e; envelope-from=development@efficientek.com; helo=mail-vs1-xe2e.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: Mon, 16 May 2022 21:50:24 -0000 Using the disk read hook mechanism, setup a read hook on the source disk which will read from the given header file during the scan and recovery cryptodisk backend functions. Disk read hooks are executed after the data has been read from the disk. This is okay, because the read hook is given the read buffer before its sent back to the caller. In this case, the hook can then overwrite the data read from the disk device with data from the header file sent in as the read hook data. This is transparent to the read caller. Since the callers of this function have just opened the source disk, there are no current read hooks, so there's no need to save/restore them nor consider if they should be called or not. This hook assumes that the header is at the start of the volume, which is not the case for some formats (eg. GELI). So GELI will return an error if a detached header is specified. It also can only be used with formats where the detached header file can be written to the first blocks of the volume and the volume could still be unlocked. So the header file can not be formatted differently from the on-disk header. If these assumpts are not met, detached header file processing must be specially handled in the cryptodisk backend module. The hook will be called potentially many times by a backend. This is fine because of the assumptions mentioned and the read hook reads from absolute offsets and is stateless. Also add a --header (short -H) option to cryptomount which takes a file argument. Signed-off-by: Glenn Washburn --- grub-core/disk/cryptodisk.c | 93 +++++++++++++++++++++++++++++++++++-- grub-core/disk/geli.c | 4 ++ include/grub/cryptodisk.h | 2 + include/grub/file.h | 2 + 4 files changed, 97 insertions(+), 4 deletions(-) diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c index ecbda7ce9..fdb0461a8 100644 --- a/grub-core/disk/cryptodisk.c +++ b/grub-core/disk/cryptodisk.c @@ -43,7 +43,8 @@ enum OPTION_PASSWORD, OPTION_KEYFILE, OPTION_KEYFILE_OFFSET, - OPTION_KEYFILE_SIZE + OPTION_KEYFILE_SIZE, + OPTION_HEADER }; static const struct grub_arg_option options[] = @@ -56,9 +57,16 @@ static const struct grub_arg_option options[] = {"key-file", 'k', 0, N_("Key file"), 0, ARG_TYPE_STRING}, {"keyfile-offset", 'O', 0, N_("Key file offset (bytes)"), 0, ARG_TYPE_INT}, {"keyfile-size", 'S', 0, N_("Key file data size (bytes)"), 0, ARG_TYPE_INT}, + {"header", 'H', 0, N_("Read header from file"), 0, ARG_TYPE_STRING}, {0, 0, 0, 0, 0, 0} }; +struct cryptodisk_read_hook_ctx +{ + grub_file_t hdr_file; +}; +typedef struct cryptodisk_read_hook_ctx *cryptodisk_read_hook_ctx_t; + /* Our irreducible polynom is x^128+x^7+x^2+x+1. Lowest byte of it is: */ #define GF_POLYNOM 0x87 static inline int GF_PER_SECTOR (const struct grub_cryptodisk *dev) @@ -1004,6 +1012,31 @@ cryptodisk_close (grub_cryptodisk_t dev) grub_free (dev); } +static grub_err_t +cryptodisk_read_hook (grub_disk_addr_t sector, unsigned offset, + unsigned length, char *buf, void *data) +{ + grub_err_t ret = GRUB_ERR_NONE; + cryptodisk_read_hook_ctx_t ctx = data; + + if (ctx->hdr_file == NULL) + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("header file not found")); + + if (grub_file_seek (ctx->hdr_file, + (sector * GRUB_DISK_SECTOR_SIZE) + offset) + == (grub_off_t) -1) + return grub_errno; + + if (grub_file_read (ctx->hdr_file, buf, length) != (grub_ssize_t) length) + { + if (grub_errno == GRUB_ERR_NONE) + grub_error (GRUB_ERR_OUT_OF_RANGE, N_("header file too small")); + return grub_errno; + } + + return ret; +} + static grub_cryptodisk_t grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source, @@ -1012,6 +1045,7 @@ grub_cryptodisk_scan_device_real (const char *name, grub_err_t ret = GRUB_ERR_NONE; grub_cryptodisk_t dev; grub_cryptodisk_dev_t cr; + struct cryptodisk_read_hook_ctx read_hook_data = {0}; int askpass = 0; char *part = NULL; @@ -1020,11 +1054,46 @@ grub_cryptodisk_scan_device_real (const char *name, if (dev) return dev; + if (cargs->hdr_file != NULL) + { + /* + * Set read hook to read header from a file instead of the source disk. + * Disk read hooks are executed after the data has been read from the + * disk. This is okay, because the read hook is given the read buffer + * before its sent back to the caller. In this case, the hook can then + * overwrite the data read from the disk device with data from the + * header file sent in as the read hook data. This is transparent to the + * read caller. Since the callers of this function have just opened the + * source disk, there are no current read hooks, so there's no need to + * save/restore them nor consider if they should be called or not. + * + * This hook assumes that the header is at the start of the volume, which + * is not the case for some formats (eg. GELI). It also can only be used + * with formats where the detached header file can be written to the + * first blocks of the volume and the volume could still be unlocked. + * So the header file can not be formatted differently from the on-disk + * header. If these assumpts are not met, detached header file processing + * must be specially handled in the cryptodisk backend module. + * + * This hook needs only be set once and will be called potentially many + * times by a backend. This is fine because of the assumptions mentioned + * and the read hook reads from absolute offsets and is stateless. + */ + read_hook_data.hdr_file = cargs->hdr_file; + source->read_hook = cryptodisk_read_hook; + source->read_hook_data = (void *) &read_hook_data; + } + FOR_CRYPTODISK_DEVS (cr) { + /* + * Loop through each cryptodisk backend that is registered (ie. loaded). + * If the scan returns NULL, then the backend being tested does not + * recognize the source disk, so move on to the next backend. + */ dev = cr->scan (source, cargs); if (grub_errno) - return NULL; + goto error_no_close; if (!dev) continue; @@ -1041,7 +1110,7 @@ grub_cryptodisk_scan_device_real (const char *name, cargs->key_data = grub_malloc (GRUB_CRYPTODISK_MAX_PASSPHRASE); if (cargs->key_data == NULL) - return NULL; + goto error_no_close; if (!grub_password_get ((char *) cargs->key_data, GRUB_CRYPTODISK_MAX_PASSPHRASE)) { @@ -1066,9 +1135,13 @@ grub_cryptodisk_scan_device_real (const char *name, error: cryptodisk_close (dev); + error_no_close: dev = NULL; cleanup: + if (cargs->hdr_file != NULL) + source->read_hook = NULL; + if (askpass) { cargs->key_len = 0; @@ -1283,6 +1356,18 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) return grub_error (GRUB_ERR_FILE_READ_ERROR, (N_("failed to read key file"))); } + if (state[OPTION_HEADER].set) /* header */ + { + if (state[OPTION_UUID].set) + return grub_error (GRUB_ERR_BAD_ARGUMENT, + N_("cannot use UUID lookup with detached header")); + + cargs.hdr_file = grub_file_open (state[OPTION_HEADER].arg, + GRUB_FILE_TYPE_CRYPTODISK_DETACHED_HEADER); + if (cargs.hdr_file == NULL) + return grub_errno; + } + if (state[OPTION_UUID].set) /* uuid */ { int found_uuid; @@ -1496,7 +1581,7 @@ GRUB_MOD_INIT (cryptodisk) grub_disk_dev_register (&grub_cryptodisk_dev); cmd = grub_register_extcmd ("cryptomount", grub_cmd_cryptomount, 0, N_("[ [-p password] | [-k keyfile" - " [-O keyoffset] [-S keysize] ] ]" + " [-O keyoffset] [-S keysize] ] ] [-H file]" " "), N_("Mount a crypto device."), options); grub_procfs_register ("luks_script", &luks_script); diff --git a/grub-core/disk/geli.c b/grub-core/disk/geli.c index 91eb10122..b3c9bbd80 100644 --- a/grub-core/disk/geli.c +++ b/grub-core/disk/geli.c @@ -252,6 +252,10 @@ geli_scan (grub_disk_t disk, grub_cryptomount_args_t cargs) grub_disk_addr_t sector; grub_err_t err; + /* Detached headers are not implemented yet */ + if (cargs->hdr_file != NULL) + return NULL; + if (2 * GRUB_MD_SHA256->mdlen + 1 > GRUB_CRYPTODISK_MAX_UUID_LENGTH) return NULL; diff --git a/include/grub/cryptodisk.h b/include/grub/cryptodisk.h index 467065f00..d94df68b6 100644 --- a/include/grub/cryptodisk.h +++ b/include/grub/cryptodisk.h @@ -20,6 +20,7 @@ #define GRUB_CRYPTODISK_HEADER 1 #include +#include #include #include #ifdef GRUB_UTIL @@ -79,6 +80,7 @@ struct grub_cryptomount_args grub_uint8_t *key_data; /* recover_key: Length of key_data */ grub_size_t key_len; + grub_file_t hdr_file; }; typedef struct grub_cryptomount_args *grub_cryptomount_args_t; diff --git a/include/grub/file.h b/include/grub/file.h index d53ee8edd..43b47bff5 100644 --- a/include/grub/file.h +++ b/include/grub/file.h @@ -92,6 +92,8 @@ enum grub_file_type GRUB_FILE_TYPE_ZFS_ENCRYPTION_KEY, /* File holding the encryption key */ GRUB_FILE_TYPE_CRYPTODISK_ENCRYPTION_KEY, + /* File holding the encryption metadata header */ + GRUB_FILE_TYPE_CRYPTODISK_DETACHED_HEADER, /* File we open n grub-fstest. */ GRUB_FILE_TYPE_FSTEST, /* File we open n grub-mount. */ -- 2.34.1