Cryptomount keyfile support
cover Cryptomount keyfile support
I'm breaking the keyfile and detached header patch series into two series. I think that the detached header patches can be improved and I don't want to hold up the more trivial keyfile support patches. This series is patches #1, #2, #5, and a split of #7, the documentation patch.
The first two patches are unchanged. The third contains changes addressing comments by Daniel on the v9 keyfile and detached header patch series. And the last patch is the same #7 except removing reference to the detached header option.
Glenn
Denis 'GNUtoo' Carikli (2):
cryptodisk: luks: Unify grub_cryptodisk_dev function names
cryptodisk: geli: Unify grub_cryptodisk_dev function names
Glenn Washburn (1):
docs: Add documentation on keyfile option to cryptomount
John Lane (1):
cryptodisk: Add options to cryptomount to support keyfiles
docs/grub.texi | 14 +++--- grub-core/disk/cryptodisk.c | 86 ++++++++++++++++++++++++++++++++++++- grub-core/disk/geli.c | 8 ++-- grub-core/disk/luks.c | 4 +- include/grub/cryptodisk.h | 2 + include/grub/file.h | 2 + 6 files changed, 104 insertions(+), 12 deletions(-)
Interdiff:
grub-core/disk/cryptodisk.c +20 -19
@@ -1179,33 +1179,29 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) {const char *p =NULL ;grub_file_t keyfile; -int keyfile_offset; -grub_size_t keyfile_size =0 ; - +unsigned long long keyfile_offset =0 , keyfile_size =0 ;if (state[5 ].set)/* keyfile-offset */ { - keyfile_offset =grub_strtoul (state[5 ].arg, &p,0 ); + keyfile_offset =grub_strtoull (state[5 ].arg, &p,0 );if (grub_errno !=GRUB_ERR_NONE )return grub_errno; -if (*p !='\0' ) +if (state[5 ].arg[0 ] =='\0' || *p !='\0' )return grub_error (GRUB_ERR_BAD_ARGUMENT , -N_ ("unrecognized number" )); - } -else - { - keyfile_offset =0 ; +N_ ("non-numeric or invalid keyfile offset `%s'" ), + state[5 ].arg); }if (state[6 ].set)/* keyfile-size */ { keyfile_size =grub_strtoul (state[6 ].arg, &p,0 ); -if (*p !='\0' ) +if (state[6 ].arg[0 ] =='\0' || *p !='\0' )return grub_error (GRUB_ERR_BAD_ARGUMENT , -N_ ("unrecognized number" )); +N_ ("non-numeric or invalid keyfile size `%s'" ), + state[6 ].arg);if (grub_errno !=GRUB_ERR_NONE )return grub_errno;
@@ -1224,16 +1220,23 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)if (keyfile ==NULL )return grub_errno; -if (grub_file_seek (keyfile, keyfile_offset) == (grub_off_t)-1 ) +if (keyfile_offset > keyfile->size) + { + keyfile_offset = keyfile->size; +grub_dprintf ("cryptodisk" ,"Keyfile offset, %llu, is greater than" +"keyfile size, %" PRIuGRUB_UINT64_T"\n" , + keyfile_offset, keyfile->size); + } + +if (grub_file_seek (keyfile, (grub_off_t ) keyfile_offset) == (grub_off_t) -1 )return grub_errno;if (keyfile_size >0 ) {if (keyfile_size > (keyfile->size - keyfile_offset))return grub_error (GRUB_ERR_FILE_READ_ERROR , -N_ ("keyfile is too small: " -"requested %" PRIuGRUB_SIZE" bytes, " -"but the file only has %" PRIuGRUB_UINT64_T +N_ ("keyfile is too small: requested %llu bytes," +" but the file only has %" PRIuGRUB_UINT64_T" bytes" ), keyfile_size, keyfile->size);
@@ -1241,9 +1244,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) cargs.key_len = keyfile_size; }else - { - cargs.key_len = keyfile->size - keyfile_offset; - } + cargs.key_len = keyfile->size - keyfile_offset; cargs.key_data =grub_malloc (cargs.key_len);if (cargs.key_data ==NULL )
Signature · 1 line
2.34.1
PATCH 1/4 cryptodisk: luks: Unify grub_cryptodisk_dev function names
From: Denis 'GNUtoo' Carikli <[email protected]>
--- grub-core/disk/luks.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/disk/luks.c +2 -2
@@ -63,7 +63,7 @@ gcry_err_code_t AF_merge (const gcry_md_spec_t * hash, grub_uint8_t * src, grub_size_t blocknumbers);static grub_cryptodisk_t -configure_ciphers (grub_disk_t disk,grub_cryptomount_args_t cargs) +luks_scan (grub_disk_t disk,grub_cryptomount_args_t cargs) {grub_cryptodisk_t newdev;const char *iptr;
@@ -297,7 +297,7 @@ luks_recover_key (grub_disk_t source, }struct grub_cryptodisk_dev luks_crypto = { - .scan = configure_ciphers, + .scan = luks_scan, .recover_key = luks_recover_key };
Signature · 1 line
2.34.1
PATCH 2/4 cryptodisk: geli: Unify grub_cryptodisk_dev function names
In reply to Glenn WashburnFrom: Denis 'GNUtoo' Carikli <[email protected]>
--- grub-core/disk/geli.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
grub-core/disk/geli.c +4 -4
@@ -240,7 +240,7 @@ grub_util_get_geli_uuid (const char *dev)#endif static grub_cryptodisk_t -configure_ciphers (grub_disk_t disk,grub_cryptomount_args_t cargs) +geli_scan (grub_disk_t disk,grub_cryptomount_args_t cargs) {grub_cryptodisk_t newdev;struct grub_geli_phdr header;
@@ -395,7 +395,7 @@ configure_ciphers (grub_disk_t disk, grub_cryptomount_args_t cargs) }static grub_err_t -recover_key (grub_disk_t source,grub_cryptodisk_t dev,grub_cryptomount_args_t cargs) +geli_recover_key (grub_disk_t source,grub_cryptodisk_t dev,grub_cryptomount_args_t cargs) {grub_size_t keysize;grub_uint8_t digest[GRUB_CRYPTO_MAX_MDLEN ];
@@ -567,8 +567,8 @@ recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_cryptomount_args_t }struct grub_cryptodisk_dev geli_crypto = { - .scan = configure_ciphers, - .recover_key = recover_key + .scan = geli_scan, + .recover_key = geli_recover_key };GRUB_MOD_INIT (geli)
Signature · 1 line
2.34.1
PATCH 3/4 cryptodisk: Add options to cryptomount to support keyfiles
In reply to Glenn WashburnFrom: John Lane <[email protected]>
Add the options --key-file, --keyfile-offset, and --keyfile-size to cryptomount and code to put read the requested key file data and pass via the cargs struct. Note, key file data is for all intents and purposes equivalent to a password given to cryptomount. So there is no need to enable support for key files in the various crypto backends (eg. LUKS1) because the key data is passed just as if it were a password.
[email protected]: rebase, patch split, small fixes, commit message
[email protected]: rebase and rework to use cryptomount arg passing,
minor fixes, improve commit message
--- grub-core/disk/cryptodisk.c | 86 ++++++++++++++++++++++++++++++++++++- include/grub/cryptodisk.h | 2 + include/grub/file.h | 2 + 3 files changed, 89 insertions(+), 1 deletion(-)
grub-core/disk/cryptodisk.c +85 -1
@@ -42,6 +42,9 @@ static const struct grub_arg_option options[] = {"all", 'a', 0, N_("Mount all."), 0, 0}, {"boot", 'b', 0, N_("Mount all volumes with `boot' flag set."), 0, 0}, {"password", 'p', 0, N_("Password to open volumes."), 0, ARG_TYPE_STRING}, + {"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}, {0, 0, 0, 0, 0, 0} };
@@ -1172,6 +1175,85 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) cargs.key_len =grub_strlen (state[3 ].arg); } +if (state[4 ].set)/* keyfile */ + { +const char *p =NULL ; +grub_file_t keyfile; +unsigned long long keyfile_offset =0 , keyfile_size =0 ; + +if (state[5 ].set)/* keyfile-offset */ + { + keyfile_offset =grub_strtoull (state[5 ].arg, &p,0 ); + +if (grub_errno !=GRUB_ERR_NONE ) +return grub_errno; + +if (state[5 ].arg[0 ] =='\0' || *p !='\0' ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("non-numeric or invalid keyfile offset `%s'" ), + state[5 ].arg); + } + +if (state[6 ].set)/* keyfile-size */ + { + keyfile_size =grub_strtoul (state[6 ].arg, &p,0 ); + +if (state[6 ].arg[0 ] =='\0' || *p !='\0' ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("non-numeric or invalid keyfile size `%s'" ), + state[6 ].arg); + +if (grub_errno !=GRUB_ERR_NONE ) +return grub_errno; + +if (keyfile_size >GRUB_CRYPTODISK_MAX_KEYFILE_SIZE ) +return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("key file size exceeds maximum (%d)" ), +GRUB_CRYPTODISK_MAX_KEYFILE_SIZE ); + +if (keyfile_size ==0 ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,N_ ("key file size is 0" )); + } + + keyfile =grub_file_open (state[4 ].arg, +GRUB_FILE_TYPE_CRYPTODISK_ENCRYPTION_KEY ); +if (keyfile ==NULL ) +return grub_errno; + +if (keyfile_offset > keyfile->size) + { + keyfile_offset = keyfile->size; +grub_dprintf ("cryptodisk" ,"Keyfile offset, %llu, is greater than" +"keyfile size, %" PRIuGRUB_UINT64_T"\n" , + keyfile_offset, keyfile->size); + } + +if (grub_file_seek (keyfile, (grub_off_t ) keyfile_offset) == (grub_off_t) -1 ) +return grub_errno; + +if (keyfile_size >0 ) + { +if (keyfile_size > (keyfile->size - keyfile_offset)) +return grub_error (GRUB_ERR_FILE_READ_ERROR , +N_ ("keyfile is too small: requested %llu bytes," +" but the file only has %" PRIuGRUB_UINT64_T +" bytes" ), + keyfile_size, + keyfile->size); + + cargs.key_len = keyfile_size; + } +else + cargs.key_len = keyfile->size - keyfile_offset; + + cargs.key_data =grub_malloc (cargs.key_len); +if (cargs.key_data ==NULL ) +return GRUB_ERR_OUT_OF_MEMORY ; + +if (grub_file_read (keyfile, cargs.key_data, cargs.key_len) != (grub_ssize_t ) cargs.key_len) +return grub_error (GRUB_ERR_FILE_READ_ERROR , (N_ ("reading key file" ))); + } +if (state[0 ].set)/* uuid */ {int found_uuid;
@@ -1384,7 +1466,9 @@ GRUB_MOD_INIT (cryptodisk) {grub_disk_dev_register (&grub_cryptodisk_dev); cmd =grub_register_extcmd ("cryptomount" , grub_cmd_cryptomount,0 , -N_ ("[-p password] <SOURCE|-u UUID|-a|-b>" ), +N_ ("[ [-p password] | [-k keyfile" +" [-O keyoffset] [-S keysize] ] ]" +" <SOURCE|-u UUID|-a|-b>" ),N_ ("Mount a crypto device." ), options);grub_procfs_register ("luks_script" , &luks_script); }
include/grub/cryptodisk.h +2 -0
@@ -61,6 +61,8 @@ typedef enum#define GRUB_CRYPTODISK_MAX_KEYLEN 128#define GRUB_CRYPTODISK_MAX_PASSPHRASE 256 +#define GRUB_CRYPTODISK_MAX_KEYFILE_SIZE 8192 +struct grub_cryptodisk ; typedef gcry_err_code_t
include/grub/file.h +2 -0
@@ -90,6 +90,8 @@ enum grub_file_typeGRUB_FILE_TYPE_FONT ,/* File holding encryption key for encrypted ZFS. */ GRUB_FILE_TYPE_ZFS_ENCRYPTION_KEY , +/* File holding the encryption key */ +GRUB_FILE_TYPE_CRYPTODISK_ENCRYPTION_KEY ,/* File we open n grub-fstest. */ GRUB_FILE_TYPE_FSTEST,/* File we open n grub-mount. */
Signature · 1 line
2.34.1
PATCH 4/4 docs: Add documentation on keyfile option to cryptomount
In reply to Glenn Washburn--- docs/grub.texi | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
docs/grub.texi +9 -5
@@ -4356,11 +4356,15 @@ Alias for @code{hashsum --hash crc32 arg @dots{}}. See command @command{hashsum} @node cryptomount @subsection cryptomount -@deffn Command cryptomount [@option{-p} password] device|@option{-u} uuid|@option{-a}|@option{-b} -Setup access to encrypted device. If @option{-p} is not given, a passphrase -is requested interactively. Otherwise, the given @var{password} will be used and -no passphrase will be requested interactively. -Option @var{device} configures specific grub device +@deffn Command cryptomount [ [@option{-p} password] | [@option{-k} keyfile [@option{-O} keyoffset] [@option{-S} keysize] ] ] device|@option{-u} uuid|@option{-a}|@option{-b} +Setup access to encrypted device. A passphrase will be requested interactively, +if neither the @option{-p} nor @option{-k} options are given. The option +@option{-p} can be used to supply a passphrase (useful for scripts). +Alternatively the @option{-k} option can be used to supply a keyfile with +options @option{-O} and @option{-S} optionally supplying the offset and size, +respectively, of the key data in the given key file. + +Argument @var{device} configures specific grub device (@pxref{Naming convention}); option @option{-u} @var{uuid} configures device with specified @var{uuid}; option @option{-a} configures all detected encrypted devices; option @option{-b} configures all geli containers that have boot flag set.
Signature · 1 line
2.34.1
Re: [PATCH 3/4] cryptodisk: Add options to cryptomount to support keyfiles
In reply to Glenn WashburnQuoted 38 lines · Glenn Washburn
From: John Lane <[email protected]>
Add the options --key-file, --keyfile-offset, and --keyfile-size to cryptomount and code to put read the requested key file data and pass via the cargs struct. Note, key file data is for all intents and purposes equivalent to a password given to cryptomount. So there is no need to enable support for key files in the various crypto backends (eg. LUKS1) because the key data is passed just as if it were a password.
Signed-off-by John Lane <[email protected]>[email protected]: rebase, patch split, small fixes, commit message
Signed-off-by Denis 'GNUtoo' Carikli <[email protected]>[email protected]: rebase and rework to use cryptomount arg passing,
minor fixes, improve commit messageSigned-off-by Glenn Washburn <[email protected]>--- grub-core/disk/cryptodisk.c | 86 ++++++++++++++++++++++++++++++++++++- include/grub/cryptodisk.h | 2 + include/grub/file.h | 2 + 3 files changed, 89 insertions(+), 1 deletion(-)grub-core/disk/cryptodisk.c +3
-0@@ -42,6 +42,9 @@ static const struct grub_arg_option options[] = {"all", 'a', 0, N_("Mount all."), 0, 0}, {"boot", 'b', 0, N_("Mount all volumes with `boot' flag set."), 0, 0}, {"password", 'p', 0, N_("Password to open volumes."), 0, ARG_TYPE_STRING}, + {"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}, {0, 0, 0, 0, 0, 0} };@@ -1172,6 +1175,85 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) cargs.key_len =grub_strlen (state[3 ].arg); }
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1186
Quoted 10 lines
grub-core/disk/cryptodisk.c +9
-0+if (state[4 ].set)/* keyfile */ + { +const char *p =NULL ; +grub_file_t keyfile; +unsigned long long keyfile_offset =0 , keyfile_size =0 ; + +if (state[5 ].set)/* keyfile-offset */ + { + keyfile_offset =grub_strtoull (state[5 ].arg, &p,0 );
Hmmm... Could not you use grub_strtoul() instead of grub_strtoull()?
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1189
Quoted 3 lines
grub-core/disk/cryptodisk.c +3
-0+ +if (grub_errno !=GRUB_ERR_NONE ) +return grub_errno;
Could you explain why do you think this is needed? I think this check is redundant. "man strtoul" says:
If endptr is not NULL, strtoul() stores the address of the first invalid character in *endptr. If there were no digits at all, strtoul() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid.
Quoted 7 lines
grub-core/disk/cryptodisk.c +7
-0+if (state[5 ].arg[0 ] =='\0' || *p !='\0' ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("non-numeric or invalid keyfile offset `%s'" ), + state[5 ].arg); + } + +if (state[6 ].set)/* keyfile-size */
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1207
Quoted 10 lines
grub-core/disk/cryptodisk.c +10
-0+ { + keyfile_size =grub_strtoul (state[6 ].arg, &p,0 ); + +if (state[6 ].arg[0 ] =='\0' || *p !='\0' ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("non-numeric or invalid keyfile size `%s'" ), + state[6 ].arg); + +if (grub_errno !=GRUB_ERR_NONE ) +return grub_errno;
Ditto.
Quoted 7 lines
grub-core/disk/cryptodisk.c +7
-0+if (keyfile_size >GRUB_CRYPTODISK_MAX_KEYFILE_SIZE ) +return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("key file size exceeds maximum (%d)" ), +GRUB_CRYPTODISK_MAX_KEYFILE_SIZE ); + +if (keyfile_size ==0 ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,N_ ("key file size is 0" ));
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1225
Quoted 10 lines
grub-core/disk/cryptodisk.c +10
-0+ } + + keyfile =grub_file_open (state[4 ].arg, +GRUB_FILE_TYPE_CRYPTODISK_ENCRYPTION_KEY ); +if (keyfile ==NULL ) +return grub_errno; + +if (keyfile_offset > keyfile->size) + { + keyfile_offset = keyfile->size;
I do not understand this keyfile_offset clamp. Could you enlighten me? In general I think you should fail here.
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1228
Quoted 3 lines
grub-core/disk/cryptodisk.c +3
-0+grub_dprintf ("cryptodisk" ,"Keyfile offset, %llu, is greater than" +"keyfile size, %" PRIuGRUB_UINT64_T"\n" , + keyfile_offset, keyfile->size);
Both keyfile_offset and keyfile->size are the same here.
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1234
Quoted 6 lines
grub-core/disk/cryptodisk.c +6
-0+ } + +if (grub_file_seek (keyfile, (grub_off_t ) keyfile_offset) == (grub_off_t) -1 ) +return grub_errno; + +if (keyfile_size >0 )
I think "!= 0" would be more natural here.
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1242
Quoted 8 lines
grub-core/disk/cryptodisk.c +8
-0+ { +if (keyfile_size > (keyfile->size - keyfile_offset)) +return grub_error (GRUB_ERR_FILE_READ_ERROR , +N_ ("keyfile is too small: requested %llu bytes," +" but the file only has %" PRIuGRUB_UINT64_T +" bytes" ), + keyfile_size, + keyfile->size);
This error message is confusing. I think "keyfile->size" should be "keyfile->size - keyfile_offset".
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1254
Quoted 11 lines
grub-core/disk/cryptodisk.c +11
-0+ cargs.key_len = keyfile_size; + } +else + cargs.key_len = keyfile->size - keyfile_offset; + + cargs.key_data =grub_malloc (cargs.key_len); +if (cargs.key_data ==NULL ) +return GRUB_ERR_OUT_OF_MEMORY ; + +if (grub_file_read (keyfile, cargs.key_data, cargs.key_len) != (grub_ssize_t ) cargs.key_len) +return grub_error (GRUB_ERR_FILE_READ_ERROR , (N_ ("reading key file" )));
s/reading key file/cannot read key file/?
Daniel
Re: [PATCH 4/4] docs: Add documentation on keyfile option to cryptomount
In reply to Glenn WashburnQuoted 1 line · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>
Daniel
Re: [PATCH 3/4] cryptodisk: Add options to cryptomount to support keyfiles
In reply to Daniel KiperQuoted 40 lines · On Thu, 12 May 2022 19:45:48 +0200 Daniel Kiper <[email protected]> wrote:
Quoted 39 lines · Glenn Washburn
From: John Lane <[email protected]>
Add the options --key-file, --keyfile-offset, and --keyfile-size to cryptomount and code to put read the requested key file data and pass via the cargs struct. Note, key file data is for all intents and purposes equivalent to a password given to cryptomount. So there is no need to enable support for key files in the various crypto backends (eg. LUKS1) because the key data is passed just as if it were a password.
Signed-off-by John Lane <[email protected]>[email protected]: rebase, patch split, small fixes, commit message
Signed-off-by Denis 'GNUtoo' Carikli <[email protected]>[email protected]: rebase and rework to use cryptomount arg passing,
minor fixes, improve commit messageSigned-off-by Glenn Washburn <[email protected]>--- grub-core/disk/cryptodisk.c | 86 ++++++++++++++++++++++++++++++++++++- include/grub/cryptodisk.h | 2 + include/grub/file.h | 2 + 3 files changed, 89 insertions(+), 1 deletion(-)grub-core/disk/cryptodisk.c +3
-0@@ -42,6 +42,9 @@ static const struct grub_arg_option options[] = {"all", 'a', 0, N_("Mount all."), 0, 0}, {"boot", 'b', 0, N_("Mount all volumes with `boot' flag set."), 0, 0}, {"password", 'p', 0, N_("Password to open volumes."), 0, ARG_TYPE_STRING}, + {"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}, {0, 0, 0, 0, 0, 0} };@@ -1172,6 +1175,85 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) cargs.key_len =grub_strlen (state[3 ].arg); }
Quoted 11 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1186
Quoted 9 lines
grub-core/disk/cryptodisk.c +9
-0+if (state[4 ].set)/* keyfile */ + { +const char *p =NULL ; +grub_file_t keyfile; +unsigned long long keyfile_offset =0 , keyfile_size =0 ; + +if (state[5 ].set)/* keyfile-offset */ + { + keyfile_offset =grub_strtoull (state[5 ].arg, &p,0 );Hmmm... Could not you use
grub_strtoul()instead ofgrub_strtoull()?
I was thinking the allowing the largest possible offset might be useful to allow for using unallocated space at the end of large block devices. This could still be done with smaller offsets, by using the blocklist syntax to create a "file" that starts near the end and then use a much smaller offset. Why don't you like it as is?
Quoted 11 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1189
Quoted 3 lines
grub-core/disk/cryptodisk.c +3
-0+ +if (grub_errno !=GRUB_ERR_NONE ) +return grub_errno;Could you explain why do you think this is needed? I think this check is redundant. "man strtoul" says:
If endptr is not NULL,
strtoul()stores the address of the first invalid character in *endptr. If there were no digits at all,strtoul()stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid.
Yep, just checked the implementation, you're right, this isn't needed. I didn't realize that endptr would be set in the case of an overflow of an otherwise valid number.
Quoted 32 lines
Quoted 7 lines
grub-core/disk/cryptodisk.c +7
-0+if (state[5 ].arg[0 ] =='\0' || *p !='\0' ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("non-numeric or invalid keyfile offset `%s'" ), + state[5 ].arg); + } + +if (state[6 ].set)/* keyfile-size */ grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1207
Quoted 10 lines
grub-core/disk/cryptodisk.c +10
-0+ { + keyfile_size =grub_strtoul (state[6 ].arg, &p,0 ); + +if (state[6 ].arg[0 ] =='\0' || *p !='\0' ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("non-numeric or invalid keyfile size `%s'" ), + state[6 ].arg); + +if (grub_errno !=GRUB_ERR_NONE ) +return grub_errno;Ditto.
Quoted 12 lines
grub-core/disk/cryptodisk.c +12
-0+if (keyfile_size >GRUB_CRYPTODISK_MAX_KEYFILE_SIZE ) +return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("key file size exceeds maximum (%d)" ), +GRUB_CRYPTODISK_MAX_KEYFILE_SIZE ); + +if (keyfile_size ==0 ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,N_ ("key file size is 0" )); + } + + keyfile =grub_file_open (state[4 ].arg, +GRUB_FILE_TYPE_CRYPTODISK_ENCRYPTION_KEY ); +if (keyfile ==NULL )
Quoted 8 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1225
Quoted 5 lines
grub-core/disk/cryptodisk.c +5
-0+return grub_errno; + +if (keyfile_offset > keyfile->size) + { + keyfile_offset = keyfile->size;I do not understand this
keyfile_offsetclamp. Could you enlighten me? In general I think you should fail here.
Hmm, yes, the clamp just makes the keyfile effectively zero bytes long, but that makes little sense.
Quoted 5 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1228
Quoted 3 lines
grub-core/disk/cryptodisk.c +3
-0+grub_dprintf ("cryptodisk" ,"Keyfile offset, %llu, is greater than" +"keyfile size, %" PRIuGRUB_UINT64_T"\n" , + keyfile_offset, keyfile->size);Both
keyfile_offsetandkeyfile->sizeare the same here.
This should've been before the clamp.
Quoted 8 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1234
Quoted 6 lines
grub-core/disk/cryptodisk.c +6
-0+ } + +if (grub_file_seek (keyfile, (grub_off_t ) keyfile_offset) == (grub_off_t) -1 ) +return grub_errno; + +if (keyfile_size >0 )I think "!= 0" would be more natural here.
Sounds good.
Quoted 10 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1242
Quoted 8 lines
grub-core/disk/cryptodisk.c +8
-0+ { +if (keyfile_size > (keyfile->size - keyfile_offset)) +return grub_error (GRUB_ERR_FILE_READ_ERROR , +N_ ("keyfile is too small: requested %llu bytes," +" but the file only has %" PRIuGRUB_UINT64_T +" bytes" ), + keyfile_size, + keyfile->size);This error message is confusing. I think "
keyfile->size" should be "keyfile->size-keyfile_offset".
Good catch. I think it could be still confusing. I'll think of a better way to say this.
Quoted 5 lines
Quoted 5 lines
grub-core/disk/cryptodisk.c +5
-0+ cargs.key_len = keyfile_size; + } +else + cargs.key_len = keyfile->size - keyfile_offset; +
Quoted 8 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1254
Quoted 6 lines
grub-core/disk/cryptodisk.c +6
-0+ cargs.key_data =grub_malloc (cargs.key_len); +if (cargs.key_data ==NULL ) +return GRUB_ERR_OUT_OF_MEMORY ; + +if (grub_file_read (keyfile, cargs.key_data, cargs.key_len) != (grub_ssize_t ) cargs.key_len) +return grub_error (GRUB_ERR_FILE_READ_ERROR , (N_ ("reading key file" )));s/reading key file/cannot read key file/?
Sure.
Glenn
Re: [PATCH 3/4] cryptodisk: Add options to cryptomount to support keyfiles
Quoted 43 lines · Glenn Washburn
Quoted 40 lines · On Thu, 12 May 2022 19:45:48 +0200 Daniel Kiper <[email protected]> wrote:
Quoted 39 lines · Glenn Washburn
From: John Lane <[email protected]>
Add the options --key-file, --keyfile-offset, and --keyfile-size to cryptomount and code to put read the requested key file data and pass via the cargs struct. Note, key file data is for all intents and purposes equivalent to a password given to cryptomount. So there is no need to enable support for key files in the various crypto backends (eg. LUKS1) because the key data is passed just as if it were a password.
Signed-off-by John Lane <[email protected]>[email protected]: rebase, patch split, small fixes, commit message
Signed-off-by Denis 'GNUtoo' Carikli <[email protected]>[email protected]: rebase and rework to use cryptomount arg passing,
minor fixes, improve commit messageSigned-off-by Glenn Washburn <[email protected]>--- grub-core/disk/cryptodisk.c | 86 ++++++++++++++++++++++++++++++++++++- include/grub/cryptodisk.h | 2 + include/grub/file.h | 2 + 3 files changed, 89 insertions(+), 1 deletion(-)grub-core/disk/cryptodisk.c +3
-0@@ -42,6 +42,9 @@ static const struct grub_arg_option options[] = {"all", 'a', 0, N_("Mount all."), 0, 0}, {"boot", 'b', 0, N_("Mount all volumes with `boot' flag set."), 0, 0}, {"password", 'p', 0, N_("Password to open volumes."), 0, ARG_TYPE_STRING}, + {"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}, {0, 0, 0, 0, 0, 0} };@@ -1172,6 +1175,85 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) cargs.key_len =grub_strlen (state[3 ].arg); }
Quoted 5 lines
Quoted 5 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1182
Quoted 5 lines
grub-core/disk/cryptodisk.c +5
-0+if (state[4 ].set)/* keyfile */ + { +const char *p =NULL ; +grub_file_t keyfile; +unsigned long long keyfile_offset =0 , keyfile_size =0 ;
I think keyfile_size should be "unsigned long" if you use grub_strtoul() below.
Quoted 7 lines
Quoted 6 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1186
Quoted 4 lines
grub-core/disk/cryptodisk.c +4
-0+ +if (state[5 ].set)/* keyfile-offset */ + { + keyfile_offset =grub_strtoull (state[5 ].arg, &p,0 );Hmmm... Could not you use
grub_strtoul()instead ofgrub_strtoull()?
Quoted 5 lines
I was thinking the allowing the largest possible offset might be useful to allow for using unallocated space at the end of large block devices. This could still be done with smaller offsets, by using the blocklist syntax to create a "file" that starts near the end and then use a much smaller offset. Why don't you like it as is?
I would not say I do not like it. I just want to understand discrepancy between this conversion and one below. If you need "unsigned long long" go ahead with it. Though I would suggest to check the value does not exceed maximum value of target type. I think GRUB_TYPE_U_MAX() can be useful here. Same applies to conversion below.
Daniel
Re: [PATCH 3/4] cryptodisk: Add options to cryptomount to support keyfiles
Quoted 44 lines · On Fri, 13 May 2022 13:12:35 +0200 Daniel Kiper <[email protected]> wrote:
Quoted 43 lines · Glenn Washburn
Quoted 40 lines · On Thu, 12 May 2022 19:45:48 +0200 Daniel Kiper <[email protected]> wrote:
Quoted 39 lines · Glenn Washburn
From: John Lane <[email protected]>
Add the options --key-file, --keyfile-offset, and --keyfile-size to cryptomount and code to put read the requested key file data and pass via the cargs struct. Note, key file data is for all intents and purposes equivalent to a password given to cryptomount. So there is no need to enable support for key files in the various crypto backends (eg. LUKS1) because the key data is passed just as if it were a password.
Signed-off-by John Lane <[email protected]>[email protected]: rebase, patch split, small fixes, commit message
Signed-off-by Denis 'GNUtoo' Carikli <[email protected]>[email protected]: rebase and rework to use cryptomount arg passing,
minor fixes, improve commit messageSigned-off-by Glenn Washburn <[email protected]>--- grub-core/disk/cryptodisk.c | 86 ++++++++++++++++++++++++++++++++++++- include/grub/cryptodisk.h | 2 + include/grub/file.h | 2 + 3 files changed, 89 insertions(+), 1 deletion(-)grub-core/disk/cryptodisk.c +3
-0@@ -42,6 +42,9 @@ static const struct grub_arg_option options[] = {"all", 'a', 0, N_("Mount all."), 0, 0}, {"boot", 'b', 0, N_("Mount all volumes with `boot' flag set."), 0, 0}, {"password", 'p', 0, N_("Password to open volumes."), 0, ARG_TYPE_STRING}, + {"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}, {0, 0, 0, 0, 0, 0} };@@ -1172,6 +1175,85 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) cargs.key_len =grub_strlen (state[3 ].arg); }
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1182
Quoted 5 lines
grub-core/disk/cryptodisk.c +5
-0+if (state[4 ].set)/* keyfile */ + { +const char *p =NULL ; +grub_file_t keyfile; +unsigned long long keyfile_offset =0 , keyfile_size =0 ;I think
keyfile_sizeshould be "unsigned long" if you usegrub_strtoul()below.
I made them both unsigned long long because I wanted both using grub_strtoull().
Quoted 13 lines
Quoted 7 lines
Quoted 6 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1186
Quoted 4 lines
grub-core/disk/cryptodisk.c +4
-0+ +if (state[5 ].set)/* keyfile-offset */ + { + keyfile_offset =grub_strtoull (state[5 ].arg, &p,0 );Hmmm... Could not you use
grub_strtoul()instead ofgrub_strtoull()?Quoted 5 lines
I was thinking the allowing the largest possible offset might be useful to allow for using unallocated space at the end of large block devices. This could still be done with smaller offsets, by using the blocklist syntax to create a "file" that starts near the end and then use a much smaller offset. Why don't you like it as is?
Quoted 5 lines
I would not say I do not like it. I just want to understand discrepancy between this conversion and one below. If you need "unsigned long long" go ahead with it. Though I would suggest to check the value does not exceed maximum value of target type. I think
GRUB_TYPE_U_MAX()can be useful here. Same applies to conversion below.
Ahh, now I see what you're getting at. I hadn't noticed the difference. I meant to change the other one to grub_strtoull, but forgot. I'll do that.
Glenn
Re: [PATCH 3/4] cryptodisk: Add options to cryptomount to support keyfiles
In reply to Daniel KiperQuoted 65 lines · On Fri, 13 May 2022 13:12:35 +0200 Daniel Kiper <[email protected]> wrote:
Quoted 43 lines · Glenn Washburn
Quoted 40 lines · On Thu, 12 May 2022 19:45:48 +0200 Daniel Kiper <[email protected]> wrote:
Quoted 39 lines · Glenn Washburn
From: John Lane <[email protected]>
Add the options --key-file, --keyfile-offset, and --keyfile-size to cryptomount and code to put read the requested key file data and pass via the cargs struct. Note, key file data is for all intents and purposes equivalent to a password given to cryptomount. So there is no need to enable support for key files in the various crypto backends (eg. LUKS1) because the key data is passed just as if it were a password.
Signed-off-by John Lane <[email protected]>[email protected]: rebase, patch split, small fixes, commit message
Signed-off-by Denis 'GNUtoo' Carikli <[email protected]>[email protected]: rebase and rework to use cryptomount arg passing,
minor fixes, improve commit messageSigned-off-by Glenn Washburn <[email protected]>--- grub-core/disk/cryptodisk.c | 86 ++++++++++++++++++++++++++++++++++++- include/grub/cryptodisk.h | 2 + include/grub/file.h | 2 + 3 files changed, 89 insertions(+), 1 deletion(-)grub-core/disk/cryptodisk.c +3
-0@@ -42,6 +42,9 @@ static const struct grub_arg_option options[] = {"all", 'a', 0, N_("Mount all."), 0, 0}, {"boot", 'b', 0, N_("Mount all volumes with `boot' flag set."), 0, 0}, {"password", 'p', 0, N_("Password to open volumes."), 0, ARG_TYPE_STRING}, + {"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}, {0, 0, 0, 0, 0, 0} };@@ -1172,6 +1175,85 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) cargs.key_len =grub_strlen (state[3 ].arg); }Quoted 5 lines
Quoted 5 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1182
Quoted 5 lines
grub-core/disk/cryptodisk.c +5
-0+if (state[4 ].set)/* keyfile */ + { +const char *p =NULL ; +grub_file_t keyfile; +unsigned long long keyfile_offset =0 , keyfile_size =0 ;I think
keyfile_sizeshould be "unsigned long" if you usegrub_strtoul()below.Quoted 7 lines
Quoted 6 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1186
Quoted 4 lines
grub-core/disk/cryptodisk.c +4
-0+ +if (state[5 ].set)/* keyfile-offset */ + { + keyfile_offset =grub_strtoull (state[5 ].arg, &p,0 );Hmmm... Could not you use
grub_strtoul()instead ofgrub_strtoull()?Quoted 5 lines
I was thinking the allowing the largest possible offset might be useful to allow for using unallocated space at the end of large block devices. This could still be done with smaller offsets, by using the blocklist syntax to create a "file" that starts near the end and then use a much smaller offset. Why don't you like it as is?
Quoted 5 lines
I would not say I do not like it. I just want to understand discrepancy between this conversion and one below. If you need "unsigned long long" go ahead with it. Though I would suggest to check the value does not exceed maximum value of target type. I think
GRUB_TYPE_U_MAX()can be useful here. Same applies to conversion below.
I sent the last response too hastily. In case its not clear, I'm using unsigned long long because that is the return type of grub_strtoull(). Once I change from grub_strtoul() -> grub_strtoull(), I don't think need the suggested check. I'll submit the updated code and see if you think otherwise.
Glenn