Cryptomount keyfile support
v2 cover Cryptomount keyfile support
Updates from v1:
* Make some changes suggested by Daniel
* Improve error message for grub_strtoull() failures
* Add patch to use enum constants to index parsed option array
Glenn
Denis 'GNUtoo' Carikli (2):
cryptodisk: luks: Unify grub_cryptodisk_dev function names
cryptodisk: geli: Unify grub_cryptodisk_dev function names
Glenn Washburn (2):
cryptodisk: Use enum constants as indexes into cryptomount option
array
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 | 133 +++++++++++++++++++++++++++++++++--- 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, 142 insertions(+), 21 deletions(-)
Range-diff against v1:
1: b19b567a6 = 1: b19b567a6 cryptodisk: luks: Unify grub_cryptodisk_dev function names
2: 80a284dbe = 2: 80a284dbe cryptodisk: geli: Unify grub_cryptodisk_dev function names
3: 8c2cd5ce6 ! 3: 34816c265 cryptodisk: Add options to cryptomount to support keyfiles
@@ grub-core/disk/cryptodisk.c: grub_cmd_cryptomount (grub_extcmd_context_t ctxt, i
+ if (state[4].set) /* keyfile */
+ {
++ char tmp_errmsg[GRUB_MAX_ERRMSG];
+ const char *p = NULL;
+ grub_file_t keyfile;
+ unsigned long long keyfile_offset = 0, keyfile_size = 0;
+
+ if (state[5].set) /* keyfile-offset */
+ {
++ grub_errno = GRUB_ERR_NONE;
+ 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 (grub_errno != GRUB_ERR_NONE)
++ {
++ grub_strncpy (tmp_errmsg, grub_errmsg, GRUB_MAX_ERRMSG);
++ return grub_error (grub_errno,
++ N_("non-numeric or invalid keyfile offset `%s': %s"),
++ state[5].arg, tmp_errmsg);
++ }
++ else
++ return grub_error (GRUB_ERR_BAD_ARGUMENT,
++ N_("invalid keyfile offset `%s': non-numeric"
++ " characters at end of number"),
++ state[5].arg);
++ }
+ }
+
+ if (state[6].set) /* keyfile-size */
+ {
-+ keyfile_size = grub_strtoul (state[6].arg, &p, 0);
++ grub_errno = GRUB_ERR_NONE;
++ keyfile_size = grub_strtoull (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 (grub_errno != GRUB_ERR_NONE)
++ {
++ grub_strncpy (tmp_errmsg, grub_errmsg, GRUB_MAX_ERRMSG);
++ return grub_error (grub_errno,
++ N_("non-numeric or invalid keyfile offset `%s': %s"),
++ state[5].arg, tmp_errmsg);
++ }
++ else
++ return grub_error (GRUB_ERR_BAD_ARGUMENT,
++ N_("invalid keyfile offset `%s': non-numeric"
++ " characters at end of number"),
++ state[6].arg);
++ }
+
+ if (keyfile_size > GRUB_CRYPTODISK_MAX_KEYFILE_SIZE)
+ return grub_error (GRUB_ERR_OUT_OF_RANGE,
@@ grub-core/disk/cryptodisk.c: grub_cmd_cryptomount (grub_extcmd_context_t ctxt, i
+ 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);
-+ }
++ return grub_error (GRUB_ERR_OUT_OF_RANGE,
++ N_("Keyfile offset, %llu, is greater than"
++ "keyfile size, %" PRIuGRUB_UINT64_T),
++ 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 != 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"),
++ " bytes left at offset %llu"),
+ keyfile_size,
-+ keyfile->size);
++ (grub_size_t) (keyfile->size - keyfile_offset),
++ keyfile_offset);
+
+ cargs.key_len = keyfile_size;
+ }
@@ grub-core/disk/cryptodisk.c: grub_cmd_cryptomount (grub_extcmd_context_t ctxt, i
+ 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")));
++ return grub_error (GRUB_ERR_FILE_READ_ERROR, (N_("failed to read key file")));
+ }
+
if (state[0].set) /* uuid */
-: --------- > 4: e7fd2c8cb cryptodisk: Use enum constants as indexes into cryptomount option array
4: 459a61800 = 5: dccdd7e03 docs: Add documentation on keyfile option to cryptomountSignature · 1 line
2.34.1
PATCH v2 1/5 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 v2 2/5 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 v2 3/5 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 | 104 +++++++++++++++++++++++++++++++++++- include/grub/cryptodisk.h | 2 + include/grub/file.h | 2 + 3 files changed, 107 insertions(+), 1 deletion(-)
grub-core/disk/cryptodisk.c +103 -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,103 @@ 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 */ + { +char tmp_errmsg[GRUB_MAX_ERRMSG ]; +const char *p =NULL ; +grub_file_t keyfile; +unsigned long long keyfile_offset =0 , keyfile_size =0 ; + +if (state[5 ].set)/* keyfile-offset */ + { + grub_errno =GRUB_ERR_NONE ; + keyfile_offset =grub_strtoull (state[5 ].arg, &p,0 ); + +if (state[5 ].arg[0 ] =='\0' || *p !='\0' ) + { +if (grub_errno !=GRUB_ERR_NONE ) + { +grub_strncpy (tmp_errmsg, grub_errmsg,GRUB_MAX_ERRMSG ); +return grub_error (grub_errno, +N_ ("non-numeric or invalid keyfile offset `%s': %s" ), + state[5 ].arg, tmp_errmsg); + } +else +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("invalid keyfile offset `%s': non-numeric" +" characters at end of number" ), + state[5 ].arg); + } + } + +if (state[6 ].set)/* keyfile-size */ + { + grub_errno =GRUB_ERR_NONE ; + keyfile_size =grub_strtoull (state[6 ].arg, &p,0 ); + +if (state[6 ].arg[0 ] =='\0' || *p !='\0' ) + { +if (grub_errno !=GRUB_ERR_NONE ) + { +grub_strncpy (tmp_errmsg, grub_errmsg,GRUB_MAX_ERRMSG ); +return grub_error (grub_errno, +N_ ("non-numeric or invalid keyfile offset `%s': %s" ), + state[5 ].arg, tmp_errmsg); + } +else +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("invalid keyfile offset `%s': non-numeric" +" characters at end of number" ), + state[6 ].arg); + } + +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) +return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Keyfile offset, %llu, is greater than" +"keyfile size, %" PRIuGRUB_UINT64_T), + 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 left at offset %llu" ), + keyfile_size, + (grub_size_t) (keyfile->size - keyfile_offset), + keyfile_offset); + + 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_ ("failed to read key file" ))); + } +if (state[0 ].set)/* uuid */ {int found_uuid;
@@ -1384,7 +1484,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
Re: [PATCH v2 3/5] cryptodisk: Add options to cryptomount to support keyfiles
Quoted 55 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 | 104 +++++++++++++++++++++++++++++++++++- include/grub/cryptodisk.h | 2 + include/grub/file.h | 2 + 3 files changed, 107 insertions(+), 1 deletion(-)grub-core/disk/cryptodisk.c +19
-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,103 @@ 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 */ + { +char tmp_errmsg[GRUB_MAX_ERRMSG ]; +const char *p =NULL ; +grub_file_t keyfile; +unsigned long long keyfile_offset =0 , keyfile_size =0 ; + +if (state[5 ].set)/* keyfile-offset */ + { + grub_errno =GRUB_ERR_NONE ; + keyfile_offset =grub_strtoull (state[5 ].arg, &p,0 ); + +if (state[5 ].arg[0 ] =='\0' || *p !='\0' ) + { +if (grub_errno !=GRUB_ERR_NONE ) + {
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1203
Quoted 10 lines
grub-core/disk/cryptodisk.c +10
-0+grub_strncpy (tmp_errmsg, grub_errmsg,GRUB_MAX_ERRMSG ); +return grub_error (grub_errno, +N_ ("non-numeric or invalid keyfile offset `%s': %s" ), + state[5 ].arg, tmp_errmsg); + } +else +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("invalid keyfile offset `%s': non-numeric" +" characters at end of number" ), + state[5 ].arg);
I think this does not give us a lot of value. I would just do
if (state[5].arg[0] == '\0' || *p != '\0')
return grub_error (GRUB_ERR_BAD_NUMBER, ...
Quoted 12 lines
grub-core/disk/cryptodisk.c +12
-0+ } + } + +if (state[6 ].set)/* keyfile-size */ + { + grub_errno =GRUB_ERR_NONE ; + keyfile_size =grub_strtoull (state[6 ].arg, &p,0 ); + +if (state[6 ].arg[0 ] =='\0' || *p !='\0' ) + { +if (grub_errno !=GRUB_ERR_NONE ) + {
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+grub_strncpy (tmp_errmsg, grub_errmsg,GRUB_MAX_ERRMSG ); +return grub_error (grub_errno, +N_ ("non-numeric or invalid keyfile offset `%s': %s" ), + state[5 ].arg, tmp_errmsg); + } +else +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("invalid keyfile offset `%s': non-numeric" +" characters at end of number" ), + state[6 ].arg);
Ditto.
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1234
Quoted 9 lines
grub-core/disk/cryptodisk.c +9
-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" ));
Nit, I would move this before the "if (keyfile_size > ...". This would be more natural.
Daniel
Re: [PATCH v2 3/5] cryptodisk: Add options to cryptomount to support keyfiles
Quoted 69 lines · On Thu, 19 May 2022 20:23:12 +0200 Daniel Kiper <[email protected]> wrote:
Quoted 55 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 | 104 +++++++++++++++++++++++++++++++++++- include/grub/cryptodisk.h | 2 + include/grub/file.h | 2 + 3 files changed, 107 insertions(+), 1 deletion(-)grub-core/disk/cryptodisk.c +19
-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,103 @@ 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 */ + { +char tmp_errmsg[GRUB_MAX_ERRMSG ]; +const char *p =NULL ; +grub_file_t keyfile; +unsigned long long keyfile_offset =0 , keyfile_size =0 ; + +if (state[5 ].set)/* keyfile-offset */ + { + grub_errno =GRUB_ERR_NONE ; + keyfile_offset =grub_strtoull (state[5 ].arg, &p,0 ); + +if (state[5 ].arg[0 ] =='\0' || *p !='\0' ) + { +if (grub_errno !=GRUB_ERR_NONE ) + {grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1203
Quoted 10 lines
grub-core/disk/cryptodisk.c +10
-0+grub_strncpy (tmp_errmsg, grub_errmsg,GRUB_MAX_ERRMSG ); +return grub_error (grub_errno, +N_ ("non-numeric or invalid keyfile offset `%s': %s" ), + state[5 ].arg, tmp_errmsg); + } +else +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("invalid keyfile offset `%s': non-numeric" +" characters at end of number" ), + state[5 ].arg);I think this does not give us a lot of value. I would just do
Quoted 2 lines
if (state[5].arg[0] == '\0' || *p != '\0')
returngrub_error(GRUB_ERR_BAD_NUMBER, ...
Even low value error information can be quite valuable to those that need it. Despite that, I'll make the suggested change.
Quoted 26 lines
Quoted 13 lines
grub-core/disk/cryptodisk.c +12
-0+ } + } + +if (state[6 ].set)/* keyfile-size */ + { + grub_errno =GRUB_ERR_NONE ; + keyfile_size =grub_strtoull (state[6 ].arg, &p,0 ); + +if (state[6 ].arg[0 ] =='\0' || *p !='\0' ) + { +if (grub_errno !=GRUB_ERR_NONE ) + {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+grub_strncpy (tmp_errmsg, grub_errmsg,GRUB_MAX_ERRMSG ); +return grub_error (grub_errno, +N_ ("non-numeric or invalid keyfile offset `%s': %s" ), + state[5 ].arg, tmp_errmsg); + } +else +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("invalid keyfile offset `%s': non-numeric" +" characters at end of number" ), + state[6 ].arg);Ditto.
Quoted 12 lines
grub-core/disk/cryptodisk.c grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) · line 1234
Quoted 9 lines
grub-core/disk/cryptodisk.c +9
-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" ));Nit, I would move this before the "if (
keyfile_size> ...". This would be more natural.
Sure thing.
Glenn
Quoted 2 lines
Daniel
PATCH v2 4/5 cryptodisk: Use enum constants as indexes into cryptomount option array
In reply to Glenn Washburn--- grub-core/disk/cryptodisk.c | 53 ++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 21 deletions(-)
grub-core/disk/cryptodisk.c +32 -21
@@ -35,6 +35,17 @@ GRUB_MOD_LICENSE ("GPLv3+");grub_cryptodisk_dev_t grub_cryptodisk_list; +enum + { +OPTION_UUID , +OPTION_ALL , +OPTION_BOOT , +OPTION_PASSWORD , +OPTION_KEYFILE , +OPTION_KEYFILE_OFFSET , +OPTION_KEYFILE_SIZE + }; +static const struct grub_arg_option options[] = { {"uuid" ,'u' ,0 ,N_ ("Mount by UUID." ),0 ,0 },
@@ -1163,66 +1174,66 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)struct grub_arg_list *state = ctxt->state;struct grub_cryptomount_args cargs = {0 }; -if (argc <1 && !state[1 ].set && !state[2 ].set) +if (argc <1 && !state[OPTION_ALL ].set && !state[OPTION_BOOT ].set)return grub_error (GRUB_ERR_BAD_ARGUMENT ,"device name required" );if (grub_cryptodisk_list ==NULL )return grub_error (GRUB_ERR_BAD_MODULE ,"no cryptodisk modules loaded" ); -if (state[3 ].set)/* password */ +if (state[OPTION_PASSWORD ].set)/* password */ { - cargs.key_data = (grub_uint8_t *) state[3 ].arg; - cargs.key_len =grub_strlen (state[3 ].arg); + cargs.key_data = (grub_uint8_t *) state[OPTION_PASSWORD ].arg; + cargs.key_len =grub_strlen (state[OPTION_PASSWORD ].arg); } -if (state[4 ].set)/* keyfile */ +if (state[OPTION_KEYFILE ].set)/* keyfile */ {char tmp_errmsg[GRUB_MAX_ERRMSG ];const char *p =NULL ;grub_file_t keyfile;unsigned long long keyfile_offset =0 , keyfile_size =0 ; -if (state[5 ].set)/* keyfile-offset */ +if (state[OPTION_KEYFILE_OFFSET ].set)/* keyfile-offset */ { grub_errno =GRUB_ERR_NONE ; - keyfile_offset =grub_strtoull (state[5 ].arg, &p,0 ); + keyfile_offset =grub_strtoull (state[OPTION_KEYFILE_OFFSET ].arg, &p,0 ); -if (state[5 ].arg[0 ] =='\0' || *p !='\0' ) +if (state[OPTION_KEYFILE_OFFSET ].arg[0 ] =='\0' || *p !='\0' ) {if (grub_errno !=GRUB_ERR_NONE ) {grub_strncpy (tmp_errmsg, grub_errmsg,GRUB_MAX_ERRMSG );return grub_error (grub_errno,N_ ("non-numeric or invalid keyfile offset `%s': %s" ), - state[5 ].arg, tmp_errmsg); + state[OPTION_KEYFILE_OFFSET ].arg, tmp_errmsg); }else return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("invalid keyfile offset `%s': non-numeric" " characters at end of number" ), - state[5 ].arg); + state[OPTION_KEYFILE_OFFSET ].arg); } } -if (state[6 ].set)/* keyfile-size */ +if (state[OPTION_KEYFILE_SIZE ].set)/* keyfile-size */ { grub_errno =GRUB_ERR_NONE ; - keyfile_size =grub_strtoull (state[6 ].arg, &p,0 ); + keyfile_size =grub_strtoull (state[OPTION_KEYFILE_SIZE ].arg, &p,0 ); -if (state[6 ].arg[0 ] =='\0' || *p !='\0' ) +if (state[OPTION_KEYFILE_SIZE ].arg[0 ] =='\0' || *p !='\0' ) {if (grub_errno !=GRUB_ERR_NONE ) {grub_strncpy (tmp_errmsg, grub_errmsg,GRUB_MAX_ERRMSG );return grub_error (grub_errno,N_ ("non-numeric or invalid keyfile offset `%s': %s" ), - state[5 ].arg, tmp_errmsg); + state[OPTION_KEYFILE_SIZE ].arg, tmp_errmsg); }else return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("invalid keyfile offset `%s': non-numeric" " characters at end of number" ), - state[6 ].arg); + state[OPTION_KEYFILE_SIZE ].arg); }if (keyfile_size >GRUB_CRYPTODISK_MAX_KEYFILE_SIZE )
@@ -1234,7 +1245,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)return grub_error (GRUB_ERR_OUT_OF_RANGE ,N_ ("key file size is 0" )); } - keyfile =grub_file_open (state[4 ].arg, + keyfile =grub_file_open (state[OPTION_KEYFILE ].arg,GRUB_FILE_TYPE_CRYPTODISK_ENCRYPTION_KEY );if (keyfile ==NULL )return grub_errno;
@@ -1272,7 +1283,7 @@ 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[0 ].set)/* uuid */ +if (state[OPTION_UUID ].set)/* uuid */ {int found_uuid;grub_cryptodisk_t dev;
@@ -1285,7 +1296,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)return GRUB_ERR_NONE ; } - cargs.check_boot = state[2 ].set; + cargs.check_boot = state[OPTION_BOOT ].set; cargs.search_uuid = args[0 ]; found_uuid =grub_device_iterate (&grub_cryptodisk_scan_device, &cargs);
@@ -1303,9 +1314,9 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) }return grub_errno; } -else if (state[1 ].set || (argc ==0 && state[2 ].set))/* -a|-b */ +else if (state[OPTION_ALL ].set || (argc ==0 && state[OPTION_BOOT ].set))/* -a|-b */ { - cargs.check_boot = state[2 ].set; + cargs.check_boot = state[OPTION_BOOT ].set;grub_device_iterate (&grub_cryptodisk_scan_device, &cargs);return GRUB_ERR_NONE ; }
@@ -1317,7 +1328,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)char *disklast =NULL ;grub_size_t len; - cargs.check_boot = state[2 ].set; + cargs.check_boot = state[OPTION_BOOT ].set; diskname = args[0 ]; len =grub_strlen (diskname);if (len && diskname[0 ] =='(' && diskname[len -1 ] ==')' )
Signature · 1 line
2.34.1
Re: [PATCH v2 4/5] cryptodisk: Use enum constants as indexes into cryptomount option array
Quoted 13 lines · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/cryptodisk.c | 53 ++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 21 deletions(-)grub-core/disk/cryptodisk.c +0
-0@@ -35,6 +35,17 @@ GRUB_MOD_LICENSE ("GPLv3+");grub_cryptodisk_dev_t grub_cryptodisk_list;
grub-core/disk/cryptodisk.c GRUB_MOD_LICENSE ("GPLv3+"); · line 47
Quoted 10 lines
grub-core/disk/cryptodisk.c +10
-0+enum + { +OPTION_UUID , +OPTION_ALL , +OPTION_BOOT , +OPTION_PASSWORD , +OPTION_KEYFILE , +OPTION_KEYFILE_OFFSET , +OPTION_KEYFILE_SIZE + };
I would prefer constants here.
Otherwise patches LGTM.
Daniel
Re: [PATCH v2 4/5] cryptodisk: Use enum constants as indexes into cryptomount option array
Quoted 14 lines · On Thu, 19 May 2022 20:24:11 +0200 Daniel Kiper <[email protected]> wrote:
Quoted 13 lines · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/cryptodisk.c | 53 ++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 21 deletions(-)grub-core/disk/cryptodisk.c +0
-0@@ -35,6 +35,17 @@ GRUB_MOD_LICENSE ("GPLv3+");grub_cryptodisk_dev_t grub_cryptodisk_list;
Quoted 12 lines
grub-core/disk/cryptodisk.c GRUB_MOD_LICENSE ("GPLv3+"); · line 47
Quoted 10 lines
grub-core/disk/cryptodisk.c +10
-0+enum + { +OPTION_UUID , +OPTION_ALL , +OPTION_BOOT , +OPTION_PASSWORD , +OPTION_KEYFILE , +OPTION_KEYFILE_OFFSET , +OPTION_KEYFILE_SIZE + };I would prefer constants here.
I chose enum because that is consistent with many other commands. By "constants" do you mean CPP defined macros? I can imagine you would mean variables with the "const" property. Without having done an exhaustive search, I don't believe any commands use CPP macros for the index. Here's a list of files for commands that use enums as the index:
grub-core/commands/i386/pc/drivemap.cgrub-core/commands/file.cgrub-core/commands/pgp.cgrub-core/commands/search_wrap.cgrub-core/term/gfxterm_background.cgrub-core/term/serial.cgrub-core/term/terminfo.c
The benefits of enum over CPP macro is that inserting into the enum list automatically renumbers the subsequent constants in the list. I failing to think of a good way to do that with CPP macros. Could you explain a little further precisely what you're wanting (maybe I've guessed wrong) and why you see it as superior to using enums?
Glenn
Quoted 4 lines
Otherwise patches LGTM.
Daniel
Re: [PATCH v2 4/5] cryptodisk: Use enum constants as indexes into cryptomount option array
Quoted 44 lines · Glenn Washburn
Quoted 14 lines · On Thu, 19 May 2022 20:24:11 +0200 Daniel Kiper <[email protected]> wrote:
Quoted 13 lines · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/cryptodisk.c | 53 ++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 21 deletions(-)grub-core/disk/cryptodisk.c +0
-0@@ -35,6 +35,17 @@ GRUB_MOD_LICENSE ("GPLv3+");grub_cryptodisk_dev_t grub_cryptodisk_list;Quoted 12 lines
grub-core/disk/cryptodisk.c GRUB_MOD_LICENSE ("GPLv3+"); · line 47
Quoted 10 lines
grub-core/disk/cryptodisk.c +10
-0+enum + { +OPTION_UUID , +OPTION_ALL , +OPTION_BOOT , +OPTION_PASSWORD , +OPTION_KEYFILE , +OPTION_KEYFILE_OFFSET , +OPTION_KEYFILE_SIZE + };I would prefer constants here.
I chose enum because that is consistent with many other commands. By "constants" do you mean CPP defined macros? I can imagine you would mean variables with the "const" property. Without having done an exhaustive search, I don't believe any commands use CPP macros for the index. Here's a list of files for commands that use enums as the index:
grub-core/commands/i386/pc/drivemap.cgrub-core/commands/file.cgrub-core/commands/pgp.cgrub-core/commands/search_wrap.cgrub-core/term/gfxterm_background.cgrub-core/term/serial.cgrub-core/term/terminfo.c
Quoted 5 lines
The benefits of enum over CPP macro is that inserting into the enum list automatically renumbers the subsequent constants in the list. I failing to think of a good way to do that with CPP macros. Could you explain a little further precisely what you're wanting (maybe I've guessed wrong) and why you see it as superior to using enums?
If enum is used in the other commands please ignore my comment. Sorry for the noise.
Daniel
PATCH v2 5/5 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
@@ -4490,11 +4490,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