Cryptodisk fixes for v2.06 redux
PATCH v8 6/18 luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
In reply to Glenn WashburnThis allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.
Get rid of idx local variable in luks2_get_keyslot() which was overloaded to be used for both keyslot and segment slot keys.
--- grub-core/disk/luks2.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +9 -5
@@ -65,6 +65,8 @@ typedef struct grub_luks2_header grub_luks2_header_t;struct grub_luks2_keyslot { +/* The integer key to the associative array of keyslots */ +grub_uint64_t idx;grub_int64_t key_size;grub_int64_t priority; struct
@@ -103,6 +105,7 @@ typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;struct grub_luks2_segment { +grub_uint64_t idx;grub_uint64_t offset;const char *size;const char *encryption;
@@ -112,6 +115,7 @@ typedef struct grub_luks2_segment grub_luks2_segment_t;struct grub_luks2_digest { +grub_uint64_t idx;/* Both keyslots and segments are interpreted as bitfields here */ grub_uint64_t keyslots;grub_uint64_t segments;
@@ -261,12 +265,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s {grub_json_t keyslots, keyslot, digests, digest, segments, segment;grub_size_t i, size; -grub_uint64_t idx;/* Get nth keyslot */ if (grub_json_getvalue (&keyslots, root,"keyslots" ) ||grub_json_getchild (&keyslot, &keyslots, keyslot_idx) || -grub_json_getuint64 (&idx, &keyslot,NULL ) || +grub_json_getuint64 (&k->idx, &keyslot,NULL ) ||grub_json_getchild (&keyslot, &keyslot,0 ) ||luks2_parse_keyslot (k, &keyslot))return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse keyslot %" PRIuGRUB_SIZE, keyslot_idx);
@@ -278,11 +281,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sfor (i =0 ; i < size; i++) {if (grub_json_getchild (&digest, &digests, i) || +grub_json_getuint64 (&d->idx, &digest,NULL ) ||grub_json_getchild (&digest, &digest,0 ) ||luks2_parse_digest (d, &digest))return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse digest %" PRIuGRUB_SIZE, i); -if ((d->keyslots & (1 << idx))) +if ((d->keyslots & (1 << k->idx)))break ; }if (i == size)
@@ -295,12 +299,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sfor (i =0 ; i < size; i++) {if (grub_json_getchild (&segment, &segments, i) || -grub_json_getuint64 (&idx, &segment,NULL ) || +grub_json_getuint64 (&s->idx, &segment,NULL ) ||grub_json_getchild (&segment, &segment,0 ) ||luks2_parse_segment (s, &segment))return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse segment %" PRIuGRUB_SIZE, i); -if ((d->segments & (1 << idx))) +if ((d->segments & (1 << s->idx)))break ; }if (i == size)
Signature · 1 line
2.27.0
PATCH v8 17/18 misc: Add grub_log2ull macro for calculating log base 2 of 64-bit integers
In reply to Glenn Washburn--- include/grub/misc.h | 3 +++ 1 file changed, 3 insertions(+)
include/grub/misc.h +3 -0
@@ -482,4 +482,7 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file,#define grub_max (a, b) (((a) > (b)) ? (a) : (b))#define grub_min (a, b) (((a) < (b)) ? (a) : (b)) +#define grub_log2ull (n) (GRUB_TYPE_BITS (grub_uint64_t) \ + - __builtin_clzll (n) - 1) +#endif /* ! GRUB_MISC_HEADER */
Signature · 1 line
2.27.0
PATCH v8 2/18 misc: Add parentheses around ALIGN_UP and ALIGN_DOWN arguments
In reply to Glenn WashburnThis ensures that expected order of operations is preserved when arguments are expressions.
--- include/grub/misc.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/grub/misc.h +2 -2
@@ -28,10 +28,10 @@#include <grub/compiler.h> #define ALIGN_UP (addr, align) \ - ((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1)) + (((addr) + (typeof (addr)) (align) - 1) & ~((typeof (addr)) (align) - 1))#define ALIGN_UP_OVERHEAD (addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1))#define ALIGN_DOWN (addr, align) \ - ((addr) & ~((typeof (addr)) align - 1)) + ((addr) & ~((typeof (addr)) (align) - 1))#define ARRAY_SIZE (array) (sizeof (array) / sizeof (array[0]))#define COMPILE_TIME_ASSERT (cond) switch (0) { case 1: case !(cond): ; }
Signature · 1 line
2.27.0
PATCH v8 9/18 luks2: Add string "index" to user strings using a json index.
In reply to Glenn WashburnThis allows error messages to be more easily distinguishable between indexes and slot keys. The former include the string "index" in the error/debug string, and the later are surrounded in quotes.
--- grub-core/disk/luks2.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +3 -3
@@ -272,7 +272,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s grub_json_getuint64 (&k->idx, &keyslot, NULL) || grub_json_getchild (&keyslot, &keyslot, 0) || luks2_parse_keyslot (k, &keyslot)) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse keyslot %" PRIuGRUB_SIZE, keyslot_json_idx); +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse keyslot index %" PRIuGRUB_SIZE, keyslot_json_idx);/* Get digest that matches the keyslot. */ if (grub_json_getvalue (&digests, root, "digests") ||
@@ -284,7 +284,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s grub_json_getuint64 (&d->idx, &digest, NULL) || grub_json_getchild (&digest, &digest, 0) || luks2_parse_digest (d, &digest)) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse digest %" PRIuGRUB_SIZE, json_idx); +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse digest index %" PRIuGRUB_SIZE, json_idx);if ((d->keyslots & (1 << k->idx)))break ;
@@ -302,7 +302,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s grub_json_getuint64 (&s->idx, &segment, NULL) || grub_json_getchild (&segment, &segment, 0) || luks2_parse_segment (s, &segment)) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse segment %" PRIuGRUB_SIZE, json_idx); +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse segment index %" PRIuGRUB_SIZE, json_idx);if ((d->segments & (1 << s->idx)))break ;
Signature · 1 line
2.27.0
PATCH v8 10/18 cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals
In reply to Glenn WashburnThe new macro GRUB_TYPE_BITS(type) returns the number of bits allocated for type.
--- grub-core/disk/cryptodisk.c | 7 ++++--- include/grub/types.h | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-)
grub-core/disk/cryptodisk.c +4 -3
@@ -281,20 +281,21 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, }break ;case GRUB_CRYPTODISK_MODE_IV_PLAIN64 : - iv[1 ] =grub_cpu_to_le32 (sector >>32 ); + iv[1 ] =grub_cpu_to_le32 (sector >>GRUB_TYPE_BITS (iv[0 ]));/* FALLTHROUGH */ case GRUB_CRYPTODISK_MODE_IV_PLAIN : iv[0 ] =grub_cpu_to_le32 (sector &0xFFFFFFFF );break ;case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64 : - iv[1 ] =grub_cpu_to_le32 (sector >> (32 - dev->log_sector_size)); + iv[1 ] =grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1 ]) + - dev->log_sector_size)); iv[0 ] =grub_cpu_to_le32 ((sector << dev->log_sector_size) &0xFFFFFFFF );break ;case GRUB_CRYPTODISK_MODE_IV_BENBI : {grub_uint64_t num = (sector << dev->benbi_log) +1 ; - iv[sz -2 ] =grub_cpu_to_be32 (num >>32 ); + iv[sz -2 ] =grub_cpu_to_be32 (num >>GRUB_TYPE_BITS (iv[0 ])); iv[sz -1 ] =grub_cpu_to_be32 (num &0xFFFFFFFF ); }break ;
include/grub/types.h +2 -0
@@ -80,6 +80,8 @@# define GRUB_CHAR_BIT __CHAR_BIT__#endif +#define GRUB_TYPE_BITS (type) (sizeof(type) * GRUB_CHAR_BIT) +/* Define various wide integers. */ typedef signed char grub_int8_t ;typedef short grub_int16_t ;
Signature · 1 line
2.27.0
PATCH v8 14/18 luks2: Better error handling when setting up the cryptodisk
In reply to Glenn WashburnFirst, check to make sure that source disk has a known size. If not, print debug message and return error. There are 4 cases where GRUB_DISK_SIZE_UNKNOWN is set (biosdisk, obdisk, ofdisk, and uboot), and in all those cases processing continues. So this is probably a bit conservative. However, 3 of the cases seem pathological, and the other, biosdisk, happens when booting from a cd. Since I doubt booting from a LUKS2 volume on a cd is a big use case, we'll error until someone complains.
Do some sanity checking on data coming from the luks header. If segment.size is "dynamic", verify that the offset is not past the end of disk. Otherwise, check for errors from grub_strtoull when converting segment size from string. If a GRUB_ERR_BAD_NUMBER error was returned, then the string was not a valid parsable number, so skip the key. If GRUB_ERR_OUT_OF_RANGE was returned, then there was an overflow in converting to a 64-bit unsigned integer. So this could be a very large disk (perhaps large raid array). In this case, we want to continue to try to use this key, but only allow access up to the end of the source disk.
--- grub-core/disk/luks2.c | 84 ++++++++++++++++++++++++++++++++++++++++-- include/grub/disk.h | 17 +++++++++ 2 files changed, 98 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +81 -3
@@ -600,12 +600,26 @@ luks2_recover_key (grub_disk_t source, goto err; } +if (source->total_sectors ==GRUB_DISK_SIZE_UNKNOWN ) + { +/* FIXME: Allow use of source disk, and maybe cause errors in read. */ +grub_dprintf ("luks2" ,"Source disk %s has an unknown size, " +"conservatively returning error\n" , source->name); + ret =grub_error (GRUB_ERR_BUG ,"Unknown size of luks2 source device" ); + goto err; + } +/* Try all keyslot */ for (json_idx =0 ; json_idx < size; json_idx++) { + typeof(source->total_sectors) max_crypt_sectors = 0; + + grub_errno =GRUB_ERR_NONE ; ret =luks2_get_keyslot (&keyslot, &digest, &segment, json, json_idx);if (ret) goto err; +if (grub_errno !=GRUB_ERR_NONE ) +grub_dprintf ("luks2" ,"Ignoring unhandled error %d from luks2_get_keyslot\n" , grub_errno);if (keyslot.priority ==0 ) {
@@ -619,11 +633,75 @@ luks2_recover_key (grub_disk_t source, crypt->offset_sectors =grub_divmod64 (segment.offset, segment.sector_size,NULL ); crypt->log_sector_size =sizeof (unsigned int ) *8 -__builtin_clz ((unsigned int ) segment.sector_size) -1 ; +/* Set to the source disk size, which is the maximum we allow. */ + max_crypt_sectors =grub_disk_convert_sector (source, + source->total_sectors, + crypt->log_sector_size); + +if (max_crypt_sectors < crypt->offset_sectors) + { +grub_dprintf ("luks2" ,"Segment \"%" PRIuGRUB_UINT64_T"\" has offset" +" %" PRIuGRUB_UINT64_T" which is greater than" +" source disk size %" PRIuGRUB_UINT64_T"," +" skipping\n" , + segment.idx, crypt->offset_sectors, + max_crypt_sectors); +continue ; + } +if (grub_strcmp (segment.size,"dynamic" ) ==0 ) - crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size)) - - crypt->offset_sectors; + crypt->total_sectors = max_crypt_sectors - crypt->offset_sectors;else - crypt->total_sectors =grub_strtoull (segment.size,NULL ,10 ) >> crypt->log_sector_size; + { + grub_errno =GRUB_ERR_NONE ; +/* Convert segment.size to sectors, rounding up to nearest sector */ + crypt->total_sectors =grub_strtoull (segment.size,NULL ,10 ); + crypt->total_sectors =ALIGN_UP (crypt->total_sectors, +1 << crypt->log_sector_size); + crypt->total_sectors >>= crypt->log_sector_size; + +if (grub_errno ==GRUB_ERR_NONE ) + ; +else if (grub_errno ==GRUB_ERR_BAD_NUMBER ) + { +grub_dprintf ("luks2" ,"Segment \"%" PRIuGRUB_UINT64_T"\" size" +" \"%s\" is not a parsable number\n" , + segment.idx, segment.size); +continue ; + } +else if (grub_errno ==GRUB_ERR_OUT_OF_RANGE ) + { +/* +* There was an overflow in parsing segment.size, so disk must +* be very large or the string is incorrect. +*/ +grub_dprintf ("luks2" ,"Segment \"%" PRIuGRUB_UINT64_T"\" size" +" %s overflowed 64-bit unsigned integer," +" the end of the crypto device will be" +" inaccessible\n" , + segment.idx, segment.size); +if (crypt->total_sectors > max_crypt_sectors) + crypt->total_sectors = max_crypt_sectors; + } + } + +if (crypt->total_sectors ==0 ) + { +grub_dprintf ("luks2" ,"Segment \"%" PRIuGRUB_UINT64_T"\" has zero" +" sectors, skipping\n" , + segment.idx); +continue ; + } +else if (max_crypt_sectors < (crypt->offset_sectors + crypt->total_sectors)) + { +grub_dprintf ("luks2" ,"Segment \"%" PRIuGRUB_UINT64_T"\" has last" +" data position greater than source disk size," +" the end of the crypto device will be" +" inaccessible\n" , + segment.idx); +/* Allow decryption up to the end of the source disk. */ + crypt->total_sectors = max_crypt_sectors - crypt->offset_sectors; + } ret =luks2_decrypt_key (candidate_key, source, crypt, &keyslot, (const grub_uint8_t *) passphrase,grub_strlen (passphrase));
include/grub/disk.h +17 -0
@@ -27,6 +27,8 @@#include <grub/device.h> /* For NULL. */ #include <grub/mm.h> +/* For ALIGN_UP. */ +#include <grub/misc.h> /* These are used to set a device id. When you add a new disk device, you must define a new id for it here. */
@@ -174,6 +176,21 @@ typedef struct grub_disk_memberlist *grub_disk_memberlist_t;/* Return value of grub_disk_native_sectors() in case disk size is unknown. */ #define GRUB_DISK_SIZE_UNKNOWN 0xffffffffffffffffULL +/* Convert sector number from disk sized sectors to a log-size sized sector. */ +static inline grub_disk_addr_t +grub_disk_convert_sector (grub_disk_t disk, +grub_disk_addr_t sector, +grub_size_t log_sector_size) +{ +if (disk->log_sector_size < log_sector_size) + { + sector =ALIGN_UP (sector,1 << (log_sector_size / disk->log_sector_size)); +return sector >> (log_sector_size - disk->log_sector_size); + } +else +return sector << (disk->log_sector_size - log_sector_size); +} +/* Convert to GRUB native disk sized sector from disk sized sector. */ static inline grub_disk_addr_t grub_disk_from_native_sector (grub_disk_t disk,grub_disk_addr_t sector)
Signature · 1 line
2.27.0
PATCH v8 15/18 luks2: Error check segment.sector_size
In reply to Glenn Washburn--- grub-core/disk/luks2.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
grub-core/disk/luks2.c +11 -0
@@ -629,6 +629,17 @@ luks2_recover_key (grub_disk_t source,grub_dprintf ("luks2" ,"Trying keyslot \"%" PRIuGRUB_UINT64_T"\"\n" , keyslot.idx); +/* Sector size should be one of 512, 1024, 2048, or 4096. */ +if (!(segment.sector_size ==512 || segment.sector_size ==1024 || + segment.sector_size ==2048 || segment.sector_size ==4096 )) + { +grub_dprintf ("luks2" ,"Segment \"%" PRIuGRUB_UINT64_T"\" sector" +" size %" PRIuGRUB_UINT64_T" is not one of" +" 512, 1024, 2048, or 4096\n" , + segment.idx, segment.sector_size); +continue ; + } +/* Set up disk according to keyslot's segment. */ crypt->offset_sectors =grub_divmod64 (segment.offset, segment.sector_size,NULL ); crypt->log_sector_size =sizeof (unsigned int ) *8
Signature · 1 line
2.27.0
PATCH v8 16/18 mips: Enable __clzdi2()
In reply to Glenn WashburnThis patch is similiar to commit 9dab2f51e (sparc: Enable __clzsi2() and __clzdi2()) but for MIPS target and __clzdi2 only, __clzsi2 was already enabled.
--- grub-core/kern/compiler-rt.c | 2 +- include/grub/compiler-rt.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
grub-core/kern/compiler-rt.c +1 -1
@@ -448,7 +448,7 @@ __clzsi2 (grub_uint32_t val) }#endif -#if defined(__riscv) || defined(__sparc__) +#if defined(__mips__) || defined(__riscv) || defined(__sparc__)int __clzdi2 (grub_uint64_t val) {
include/grub/compiler-rt.h +1 -1
@@ -115,7 +115,7 @@ intEXPORT_FUNC (__clzsi2) (grub_uint32_t val);#endif -#if defined(__riscv) || defined(__sparc__) +#if defined(__mips__) || defined(__riscv) || defined(__sparc__)int EXPORT_FUNC (__clzdi2 ) (grub_uint64_t val);#endif
Signature · 1 line
2.27.0
PATCH v8 18/18 luks2: Use grub_log2ull to calculate log_sector_size and improve readability
In reply to Glenn Washburn--- grub-core/disk/luks2.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
grub-core/disk/luks2.c +1 -2
@@ -642,8 +642,7 @@ luks2_recover_key (grub_disk_t source,/* Set up disk according to keyslot's segment. */ crypt->offset_sectors =grub_divmod64 (segment.offset, segment.sector_size,NULL ); - crypt->log_sector_size =sizeof (unsigned int ) *8 - -__builtin_clz ((unsigned int ) segment.sector_size) -1 ; + crypt->log_sector_size =grub_log2ull (segment.sector_size);/* Set to the source disk size, which is the maximum we allow. */ max_crypt_sectors = grub_disk_convert_sector(source, source->total_sectors,
Signature · 1 line
2.27.0
Re: [PATCH v8 01/18] disk: Rename grub_disk_get_size to grub_disk_native_sectors
In reply to Glenn WashburnQuoted 8 lines · Glenn Washburn
The function
grub_disk_get_sizeis confusingly named because it actually returns a sector count where the sectors are sized in the grub native sector size. Rename to something more appropriate.Signed-off-by Glenn Washburn <[email protected]>Suggested-by Daniel Kiper <[email protected]>Reviewed-by Daniel Kiper <[email protected]>Reviewed-by Patrick Steinhardt <[email protected]>
The proper order is:
First I suggested the patch, you wrote it, Patrick reviewed the patch and finally I did it.
I will fix it before committing.
Daniel
Re: [PATCH v8 06/18] luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
In reply to Glenn WashburnQuoted 8 lines · Glenn Washburn
This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.
Get rid of idx local variable in
luks2_get_keyslot()which was overloaded to be used for both keyslot and segment slot keys.Signed-off-by Glenn Washburn <[email protected]>
Daniel
Re: [PATCH v8 07/18] luks2: Use more intuitive object name instead of json index in user messages
In reply to Glenn WashburnQuoted 10 lines · Glenn Washburn
Use the object name in the json array rather than the 0 based index in the json array for keyslots, segments, and digests. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.
Signed-off-by Glenn Washburn <[email protected]>
Daniel
Re: [PATCH v8 08/18] luks2: Rename json index variables to names that they are obviously json indexes
In reply to Glenn WashburnQuoted 1 line · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>
Daniel
Re: [PATCH v8 09/18] luks2: Add string "index" to user strings using a json index.
In reply to Glenn WashburnQuoted 5 lines · Glenn Washburn
This allows error messages to be more easily distinguishable between indexes and slot keys. The former include the string "index" in the error/debug string, and the later are surrounded in quotes.
Signed-off-by Glenn Washburn <[email protected]>
Daniel
Re: [PATCH v8 10/18] cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals
In reply to Glenn WashburnQuoted 4 lines · Glenn Washburn
The new macro
GRUB_TYPE_BITS(type)returns the number of bits allocated for type.Signed-off-by Glenn Washburn <[email protected]>
Daniel
Re: [PATCH v8 11/18] cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
In reply to Glenn WashburnQuoted 4 lines · Glenn Washburn
Add GRUB_TYPE_U_MAX/MIN(type) macros to get the max/min values for an unsigned number with size of type.
Signed-off-by Glenn Washburn <[email protected]>
Daniel
Re: [PATCH v8 14/18] luks2: Better error handling when setting up the cryptodisk
In reply to Glenn WashburnQuoted 100 lines · Glenn Washburn
First, check to make sure that source disk has a known size. If not, print debug message and return error. There are 4 cases where
GRUB_DISK_SIZE_UNKNOWNis set (biosdisk, obdisk, ofdisk, and uboot), and in all those cases processing continues. So this is probably a bit conservative. However, 3 of the cases seem pathological, and the other, biosdisk, happens when booting from a cd. Since I doubt booting from a LUKS2 volume on a cd is a big use case, we'll error until someone complains.Do some sanity checking on data coming from the luks header. If segment.size is "dynamic", verify that the offset is not past the end of disk. Otherwise, check for errors from
grub_strtoullwhen converting segment size from string. If aGRUB_ERR_BAD_NUMBERerror was returned, then the string was not a valid parsable number, so skip the key. IfGRUB_ERR_OUT_OF_RANGEwas returned, then there was an overflow in converting to a 64-bit unsigned integer. So this could be a very large disk (perhaps large raid array). In this case, we want to continue to try to use this key, but only allow access up to the end of the source disk.Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 84 ++++++++++++++++++++++++++++++++++++++++-- include/grub/disk.h | 17 +++++++++ 2 files changed, 98 insertions(+), 3 deletions(-)grub-core/disk/luks2.c +50
-3@@ -600,12 +600,26 @@ luks2_recover_key (grub_disk_t source, goto err; } +if (source->total_sectors ==GRUB_DISK_SIZE_UNKNOWN ) + { +/* FIXME: Allow use of source disk, and maybe cause errors in read. */ +grub_dprintf ("luks2" ,"Source disk %s has an unknown size, " +"conservatively returning error\n" , source->name); + ret =grub_error (GRUB_ERR_BUG ,"Unknown size of luks2 source device" ); + goto err; + } +/* Try all keyslot */ for (json_idx =0 ; json_idx < size; json_idx++) { + typeof(source->total_sectors) max_crypt_sectors = 0; + + grub_errno =GRUB_ERR_NONE ; ret =luks2_get_keyslot (&keyslot, &digest, &segment, json, json_idx);if (ret) goto err; +if (grub_errno !=GRUB_ERR_NONE ) +grub_dprintf ("luks2" ,"Ignoring unhandled error %d from luks2_get_keyslot\n" , grub_errno);if (keyslot.priority ==0 ) {@@ -619,11 +633,75 @@ luks2_recover_key (grub_disk_t source, crypt->offset_sectors =grub_divmod64 (segment.offset, segment.sector_size,NULL ); crypt->log_sector_size =sizeof (unsigned int ) *8 -__builtin_clz ((unsigned int ) segment.sector_size) -1 ; +/* Set to the source disk size, which is the maximum we allow. */ + max_crypt_sectors =grub_disk_convert_sector (source, + source->total_sectors, + crypt->log_sector_size); + +if (max_crypt_sectors < crypt->offset_sectors) + { +grub_dprintf ("luks2" ,"Segment \"%" PRIuGRUB_UINT64_T"\" has offset" +" %" PRIuGRUB_UINT64_T" which is greater than" +" source disk size %" PRIuGRUB_UINT64_T"," +" skipping\n" , + segment.idx, crypt->offset_sectors, + max_crypt_sectors); +continue ; + } +if (grub_strcmp (segment.size,"dynamic" ) ==0 ) - crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size)) - - crypt->offset_sectors; + crypt->total_sectors = max_crypt_sectors - crypt->offset_sectors;else - crypt->total_sectors =grub_strtoull (segment.size,NULL ,10 ) >> crypt->log_sector_size; + { + grub_errno =GRUB_ERR_NONE ; +/* Convert segment.size to sectors, rounding up to nearest sector */ + crypt->total_sectors =grub_strtoull (segment.size,NULL ,10 ); + crypt->total_sectors =ALIGN_UP (crypt->total_sectors, +1 << crypt->log_sector_size); + crypt->total_sectors >>= crypt->log_sector_size; + +if (grub_errno ==GRUB_ERR_NONE ) + ; +else if (grub_errno ==GRUB_ERR_BAD_NUMBER ) + { +grub_dprintf ("luks2" ,"Segment \"%" PRIuGRUB_UINT64_T"\" size" +" \"%s\" is not a parsable number\n" , + segment.idx, segment.size); +continue ; + } +else if (grub_errno ==GRUB_ERR_OUT_OF_RANGE ) + {
grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 683
Quoted 10 lines
grub-core/disk/luks2.c +10
-0+/* +* There was an overflow in parsing segment.size, so disk must +* be very large or the string is incorrect. +*/ +grub_dprintf ("luks2" ,"Segment \"%" PRIuGRUB_UINT64_T"\" size" +" %s overflowed 64-bit unsigned integer," +" the end of the crypto device will be" +" inaccessible\n" , + segment.idx, segment.size); +if (crypt->total_sectors > max_crypt_sectors)
I think this if is bogus. You should clamp crypt->total_sectors without any checks here.
Quoted 40 lines
grub-core/disk/luks2.c +21
-0+ crypt->total_sectors = max_crypt_sectors; + } + } + +if (crypt->total_sectors ==0 ) + { +grub_dprintf ("luks2" ,"Segment \"%" PRIuGRUB_UINT64_T"\" has zero" +" sectors, skipping\n" , + segment.idx); +continue ; + } +else if (max_crypt_sectors < (crypt->offset_sectors + crypt->total_sectors)) + { +grub_dprintf ("luks2" ,"Segment \"%" PRIuGRUB_UINT64_T"\" has last" +" data position greater than source disk size," +" the end of the crypto device will be" +" inaccessible\n" , + segment.idx); +/* Allow decryption up to the end of the source disk. */ + crypt->total_sectors = max_crypt_sectors - crypt->offset_sectors; + } ret =luks2_decrypt_key (candidate_key, source, crypt, &keyslot, (const grub_uint8_t *) passphrase,grub_strlen (passphrase));include/grub/disk.h +2
-0@@ -27,6 +27,8 @@#include <grub/device.h> /* For NULL. */ #include <grub/mm.h> +/* For ALIGN_UP. */ +#include <grub/misc.h> /* These are used to set a device id. When you add a new disk device, you must define a new id for it here. */ @@ -174,6 +176,21 @@ typedef struct grub_disk_memberlist *grub_disk_memberlist_t;/* Return value of grub_disk_native_sectors() in case disk size is unknown. */ #define GRUB_DISK_SIZE_UNKNOWN 0xffffffffffffffffULL
include/grub/disk.h typedef struct grub_disk_memberlist *grub_disk_memberlist_t; · line 187
Quoted 10 lines
include/grub/disk.h +9
-0+/* Convert sector number from disk sized sectors to a log-size sized sector. */ +static inline grub_disk_addr_t +grub_disk_convert_sector (grub_disk_t disk, +grub_disk_addr_t sector, +grub_size_t log_sector_size) +{ +if (disk->log_sector_size < log_sector_size) + { + sector =ALIGN_UP (sector,1 << (log_sector_size / disk->log_sector_size));
s#/#-#?
include/grub/disk.h typedef struct grub_disk_memberlist *grub_disk_memberlist_t; · line 192
Quoted 5 lines
include/grub/disk.h +5
-0+return sector >> (log_sector_size - disk->log_sector_size); + } +else +return sector << (disk->log_sector_size - log_sector_size); +}
Daniel
Re: [PATCH v8 15/18] luks2: Error check segment.sector_size
In reply to Glenn WashburnQuoted 1 line · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>
Daniel
Re: [PATCH v8 16/18] mips: Enable __clzdi2()
In reply to Glenn WashburnQuoted 6 lines · Glenn Washburn
This patch is similiar to commit
9dab2f51e(sparc: Enable__clzsi2()and__clzdi2()) but for MIPS target and__clzdi2only,__clzsi2was already enabled.Signed-off-by Glenn Washburn <[email protected]>Suggested-by Daniel Kiper <[email protected]>
Daniel
Quoted 24 lines · Glenn Washburn
This patch series, fixes issues with v7 series. Some of those patches had changes applied to the wrong patch. The added structure member was renamed again, this time to idx. And there is an added patch that renames some json index variables to note that they are such.
Glenn
Glenn Washburn (18):
disk: Renamegrub_disk_get_sizetogrub_disk_native_sectors
misc: Add parentheses aroundALIGN_UPandALIGN_DOWNarguments
whitespace: convert 8 spaces to tabs
luks2: Remove unused argument ingrub_error
luks2: Make sure all fields of output argument inluks2_parse_digest()
are written to
luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
luks2: Use more intuitive object name instead of json index in user
messages
luks2: Rename json index variables to names that they are obviously
json indexes
luks2: Add string "index" to user strings using a json index.
cryptodisk: Add macroGRUB_TYPE_BITS()to replace some literals
cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
luks2:grub_cryptodisk_t->total_sectorsis the max number of device
native sectors
Quoted 8 lines
cryptodisk: Properly handle non-512 byte sized sectors
luks2: Better error handling when setting up the cryptodisk
luks2: Error check segment.sector_size
mips: Enable__clzdi2()
misc: Addgrub_log2ullmacro for calculating log base 2 of 64-bit
integers
luks2: Usegrub_log2ullto calculatelog_sector_sizeand improve
readability
I will get all the patches except #14 and maybe some changes after that one which does not apply due to lack of it.
Thank you for doing the work!
Daniel
Re: [PATCH v8 14/18] luks2: Better error handling when setting up the cryptodisk
In reply to Daniel KiperQuoted 131 lines · On Thu, 10 Dec 2020 17:07:07 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 130 lines · Glenn Washburn
First, check to make sure that source disk has a known size. If not, print debug message and return error. There are 4 cases where
GRUB_DISK_SIZE_UNKNOWNis set (biosdisk, obdisk, ofdisk, and uboot), and in all those cases processing continues. So this is probably a bit conservative. However, 3 of the cases seem pathological, and the other, biosdisk, happens when booting from a cd. Since I doubt booting from a LUKS2 volume on a cd is a big use case, we'll error until someone complains.Do some sanity checking on data coming from the luks header. If segment.size is "dynamic", verify that the offset is not past the end of disk. Otherwise, check for errors from
grub_strtoullwhen converting segment size from string. If aGRUB_ERR_BAD_NUMBERerror was returned, then the string was not a valid parsable number, so skip the key. IfGRUB_ERR_OUT_OF_RANGEwas returned, then there was an overflow in converting to a 64-bit unsigned integer. So this could be a very large disk (perhaps large raid array). In this case, we want to continue to try to use this key, but only allow access up to the end of the source disk.Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 84++++++++++++++++++++++++++++++++++++++++--
include/grub/disk.h|
17 +++++++++ 2 files changed, 98 insertions(+), 3 deletions(-)grub-core/disk/luks2.c +3
-0@@ -600,12 +600,26 @@ luks2_recover_key (grub_disk_t source, goto err; } +if (source->total_sectors ==GRUB_DISK_SIZE_UNKNOWN ) + { +/* FIXME: Allow use of source disk, and maybe cause errors in read. */
grub-core/disk/luks2.c +2
-0+ grub_dprintf ("luks2", "Source disk %s has an unknown size, " + "conservatively returning error\n",
source->name);
+ ret =grub_error(GRUB_ERR_BUG, "Unknown size of luks2
source device");grub-core/disk/luks2.c +6
-0+ goto err; + } +/* Try all keyslot */ for (json_idx =0 ; json_idx < size; json_idx++) { + typeof(source->total_sectors) max_crypt_sectors = 0; + + grub_errno =GRUB_ERR_NONE ; ret = luks2_get_keyslot (&keyslot, &digest, &segment, json,
json_idx); if (ret)grub-core/disk/luks2.c +2
-0goto err; +if (grub_errno !=GRUB_ERR_NONE ) + grub_dprintf ("luks2", "Ignoring unhandled error %d fromluks2_get_keyslot\n",
grub_errno);if (keyslot.priority == 0)grub-core/disk/luks2.c +0
-0{@@ -619,11 +633,75 @@ luks2_recover_key (grub_disk_t source, crypt->offset_sectors = grub_divmod64 (segment.offset,segment.sector_size, NULL);
crypt->log_sector_size= sizeof (unsigned int) * 8
-__builtin_clz((unsigned int)
segment.sector_size) - 1;
+ /* Set to the source disk size, which is the maximum we
allow. */grub-core/disk/luks2.c +2
-0+ max_crypt_sectors = grub_disk_convert_sector(source, +
source->total_sectors,
+crypt->log_sector_size); +grub-core/disk/luks2.c +3
-0+ if (max_crypt_sectors < crypt->offset_sectors) + { + grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\"has offset"
grub-core/disk/luks2.c +1
-0+ " %"PRIuGRUB_UINT64_T" which isgreater than"
grub-core/disk/luks2.c +1
-0+ " source disk size%"PRIuGRUB_UINT64_T","
grub-core/disk/luks2.c +2
-0+" skipping\n" , + segment.idx,
crypt->offset_sectors,grub-core/disk/luks2.c +4
-1+ max_crypt_sectors); +continue ; + } +if (grub_strcmp (segment.size,"dynamic" ) ==0 ) - crypt->total_sectors = (grub_disk_get_size (source) >>(
crypt->log_sector_size-source->log_sector_size))grub-core/disk/luks2.c +1
-1- - crypt->offset_sectors; + crypt->total_sectors = max_crypt_sectors -
crypt->offset_sectors; elsegrub-core/disk/luks2.c +0
-1- crypt->total_sectors = grub_strtoull (segment.size, NULL,10) >>
crypt->log_sector_size;grub-core/disk/luks2.c +3
-0+ { + grub_errno =GRUB_ERR_NONE ; +/* Convert segment.size to sectors, rounding up to nearest sector */
grub-core/disk/luks2.c +1
-0+ crypt->total_sectors = grub_strtoull (segment.size,NULL, 10);
grub-core/disk/luks2.c +2
-0+ crypt->total_sectors = ALIGN_UP (crypt->total_sectors, + 1 <<
crypt->log_sector_size);grub-core/disk/luks2.c +7
-0+ crypt->total_sectors >>= crypt->log_sector_size; + +if (grub_errno ==GRUB_ERR_NONE ) + ; +else if (grub_errno ==GRUB_ERR_BAD_NUMBER ) + { + grub_dprintf ("luks2", "Segment\"%"PRIuGRUB_UINT64_T"\" size"
grub-core/disk/luks2.c +1
-0+ " \"%s\" is not a parsablenumber\n",
grub-core/disk/luks2.c +7
-0+ segment.idx, segment.size); +continue ; + } +else if (grub_errno ==GRUB_ERR_OUT_OF_RANGE ) + { +/* +* There was an overflow in parsing segment.size, so disk must
grub-core/disk/luks2.c +3
-0+* be very large or the string is incorrect. +*/ + grub_dprintf ("luks2", "Segment\"%"PRIuGRUB_UINT64_T"\" size"
grub-core/disk/luks2.c +1
-0+ " %s overflowed 64-bitunsigned integer,"
Quoted 8 lines
grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 683
Quoted 5 lines
grub-core/disk/luks2.c +1
-0+ " the end of the cryptodevice will be"
grub-core/disk/luks2.c +3
-0+" inaccessible\n" , + segment.idx, segment.size); +if (crypt->total_sectors > max_crypt_sectors)I think this if is bogus. You should clamp
crypt->total_sectorswithout any checks here.
Actually, I wouldn't call this a clamp because in the overflow case crypt->total_sectors always equals 0. I just realized this, and its because grub_strtoull will return 2^64-1 thus causing the following ALIGN_UP to overflow returning 0. Suffice to say that's not what we want. My original intent was what happened before the ALIGN_UP code was introduced, which would ALIGN_DOWN.
Here's an example illustrating why I wanted and still think the intent of this check is reasonable. Suppose we have a disk size 2^67 bytes with 512 byte (2^9) sized sectors. It will have 2^58 sectors. Further suppose, there is a LUKS volume with size 2^65 bytes starting at the beginning and a sector size of 4096 bytes (2^12). This will cause an overflow, so grub_strtoull will return 2^64-1, this should have us set crypt->total_sectors to 2^52-1. Since we don't know how much the overflow is (1 byte or 1 terabyte), we don't know how many more sectors til the end of the LUKS encrypted area. In this case max_crypt_sectors will be 2^(58+9-12) => 2^57 sectors. So here we see that crypt->total_sectors < max_crypt_sectors in the overflow case. If we do as you suggest crypt->total_sectors will be set to 2^57, and thus it will be valid to read past the end of the encrypted data (ie. block 2^56 of the 4k sector crypt will be a sector starting at byte 2^68, which is more than the 2^65 byte size volume).
On the one hand, I like your suggestion because it allows reading all possible encrypted data, at the cost of reading, decrypting, and returning non-encrypted data (ie random garbage). While keeping the check, will prevent returning garbage at the cost of not allowing access to all encrypted sectors. I think we should keep the check and document a known limitation of 2^64 byte maximum sized LUKS volumes. And that larger sized volumes can be read only up to byte 2^64.
Quoted 48 lines
Quoted 48 lines
grub-core/disk/luks2.c +7
-0+ crypt->total_sectors = max_crypt_sectors; + } + } + + if (crypt->total_sectors == 0) + { + grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\"has zero"
grub-core/disk/luks2.c +5
-0+" sectors, skipping\n" , + segment.idx); +continue ; + } + else if (max_crypt_sectors < (crypt->offset_sectors +
crypt->total_sectors))grub-core/disk/luks2.c +2
-0+ { + grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\"has last"
grub-core/disk/luks2.c +1
-0+ " data position greater thansource disk size,"
grub-core/disk/luks2.c +1
-0+ " the end of the crypto devicewill be"
grub-core/disk/luks2.c +4
-0+" inaccessible\n" , + segment.idx); +/* Allow decryption up to the end of the source disk. */ + crypt->total_sectors = max_crypt_sectors -
crypt->offset_sectors;grub-core/disk/luks2.c +1
-0+ } ret = luks2_decrypt_key (candidate_key, source, crypt,&keyslot, (const
grub_uint8_t*) passphrase,grub_strlen(passphrase)); diff --gita/include/grub/disk.hb/include/grub/disk.hindex0fb727d3d..f9227f285100644include/grub/disk.h +2
-0@@ -27,6 +27,8 @@#include <grub/device.h> /* For NULL. */ #include <grub/mm.h> +/* For ALIGN_UP. */ +#include <grub/misc.h> /* These are used to set a device id. When you add a new disk device, you must define a new id for it here. */
include/grub/disk.h +0
-0@@ -174,6 +176,21 @@ typedef struct grub_disk_memberlist *grub_disk_memberlist_t;/* Return value of
grub_disk_native_sectors()in case disk size is unknown. */ #defineGRUB_DISK_SIZE_UNKNOWN0xffffffffffffffffULL
Quoted 12 lines
Quoted 10 lines
+/* Convert sector number from disk sized sectors to a log-size sized sector. */ +static inline
grub_disk_addr_tinclude/grub/disk.h +7
-0+grub_disk_convert_sector (grub_disk_t disk, + grub_disk_addr_t sector, + grub_size_t log_sector_size) +{ + if (disk->log_sector_size < log_sector_size) + { + sector = ALIGN_UP (sector, 1 << (log_sector_size /
disk->log_sector_size));s#/#-#?
Yep, looks like this passed my tests because an overflow is caused and crypt->total_sectors is set to a very large value, which doesn't affect the tests. I'll fix it.
Glenn
Quoted 7 lines
include/grub/disk.h typedef struct grub_disk_memberlist *grub_disk_memberlist_t; · line 192
Quoted 5 lines
include/grub/disk.h +5
-0+return sector >> (log_sector_size - disk->log_sector_size); + } +else +return sector << (disk->log_sector_size - log_sector_size); +}Daniel
Re: [PATCH v8 06/18] luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
In reply to Glenn WashburnQuoted 8 lines · Glenn Washburn
This allows code using these structs to know the named key associated with these json data structures. In the future we can use these to provide better error messages to the user.
Get rid of idx local variable in
luks2_get_keyslot()which was overloaded to be used for both keyslot and segment slot keys.Signed-off-by Glenn Washburn <[email protected]>
Quoted 79 lines
--- grub-core/disk/luks2.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)grub-core/disk/luks2.c +9
-5@@ -65,6 +65,8 @@ typedef struct grub_luks2_header grub_luks2_header_t;struct grub_luks2_keyslot { +/* The integer key to the associative array of keyslots */ +grub_uint64_t idx;grub_int64_t key_size;grub_int64_t priority; struct@@ -103,6 +105,7 @@ typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;struct grub_luks2_segment { +grub_uint64_t idx;grub_uint64_t offset;const char *size;const char *encryption;@@ -112,6 +115,7 @@ typedef struct grub_luks2_segment grub_luks2_segment_t;struct grub_luks2_digest { +grub_uint64_t idx;/* Both keyslots and segments are interpreted as bitfields here */ grub_uint64_t keyslots;grub_uint64_t segments;@@ -261,12 +265,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s {grub_json_t keyslots, keyslot, digests, digest, segments, segment;grub_size_t i, size; -grub_uint64_t idx;/* Get nth keyslot */ if (grub_json_getvalue (&keyslots, root,"keyslots" ) ||grub_json_getchild (&keyslot, &keyslots, keyslot_idx) || -grub_json_getuint64 (&idx, &keyslot,NULL ) || +grub_json_getuint64 (&k->idx, &keyslot,NULL ) ||grub_json_getchild (&keyslot, &keyslot,0 ) ||luks2_parse_keyslot (k, &keyslot))return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse keyslot %" PRIuGRUB_SIZE, keyslot_idx);@@ -278,11 +281,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sfor (i =0 ; i < size; i++) {if (grub_json_getchild (&digest, &digests, i) || +grub_json_getuint64 (&d->idx, &digest,NULL ) ||grub_json_getchild (&digest, &digest,0 ) ||luks2_parse_digest (d, &digest))return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse digest %" PRIuGRUB_SIZE, i); -if ((d->keyslots & (1 << idx))) +if ((d->keyslots & (1 << k->idx)))break ; }if (i == size)@@ -295,12 +299,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sfor (i =0 ; i < size; i++) {if (grub_json_getchild (&segment, &segments, i) || -grub_json_getuint64 (&idx, &segment,NULL ) || +grub_json_getuint64 (&s->idx, &segment,NULL ) ||grub_json_getchild (&segment, &segment,0 ) ||luks2_parse_segment (s, &segment))return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse segment %" PRIuGRUB_SIZE, i); -if ((d->segments & (1 << idx))) +if ((d->segments & (1 << s->idx)))break ; }if (i == size)Signature · 1 line
2.27.0
Re: [PATCH v8 07/18] luks2: Use more intuitive object name instead of json index in user messages
In reply to Glenn WashburnQuoted 10 lines · Glenn Washburn
Use the object name in the json array rather than the 0 based index in the json array for keyslots, segments, and digests. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.
Signed-off-by Glenn Washburn <[email protected]>
Quoted 73 lines
--- grub-core/disk/luks2.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)grub-core/disk/luks2.c +9
-9@@ -290,7 +290,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sbreak ; }if (i == size) -return grub_error (GRUB_ERR_FILE_NOT_FOUND ,"No digest for keyslot %" PRIuGRUB_SIZE, keyslot_idx); +return grub_error (GRUB_ERR_FILE_NOT_FOUND ,"No digest for keyslot \"%" PRIuGRUB_UINT64_T"\"" , k->idx);/* Get segment that matches the digest. */ if (grub_json_getvalue (&segments, root, "segments") ||@@ -308,7 +308,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sbreak ; }if (i == size) -return grub_error (GRUB_ERR_FILE_NOT_FOUND ,"No segment for digest %" PRIuGRUB_SIZE); +return grub_error (GRUB_ERR_FILE_NOT_FOUND ,"No segment for digest \"%" PRIuGRUB_UINT64_T"\"" , d->idx);return GRUB_ERR_NONE ; }@@ -604,11 +604,11 @@ luks2_recover_key (grub_disk_t source,if (keyslot.priority ==0 ) { -grub_dprintf ("luks2" ,"Ignoring keyslot %" PRIuGRUB_SIZE" due to priority\n" , i); +grub_dprintf ("luks2" ,"Ignoring keyslot \"%" PRIuGRUB_UINT64_T"\" due to priority\n" , keyslot.idx);continue ; } -grub_dprintf ("luks2" ,"Trying keyslot %" PRIuGRUB_SIZE"\n" , i); +grub_dprintf ("luks2" ,"Trying keyslot \"%" PRIuGRUB_UINT64_T"\"\n" , keyslot.idx);/* Set up disk according to keyslot's segment. */ crypt->offset_sectors =grub_divmod64 (segment.offset, segment.sector_size,NULL );@@ -623,16 +623,16 @@ luks2_recover_key (grub_disk_t source, (const grub_uint8_t *) passphrase, grub_strlen (passphrase));if (ret) { -grub_dprintf ("luks2" ,"Decryption with keyslot %" PRIuGRUB_SIZE" failed: %s\n" , - i, grub_errmsg); +grub_dprintf ("luks2" ,"Decryption with keyslot \"%" PRIuGRUB_UINT64_T"\" failed: %s\n" , + keyslot.idx, grub_errmsg);continue ; } ret =luks2_verify_key (&digest, candidate_key, keyslot.key_size);if (ret) { -grub_dprintf ("luks2" ,"Could not open keyslot %" PRIuGRUB_SIZE": %s\n" , - i, grub_errmsg); +grub_dprintf ("luks2" ,"Could not open keyslot \"%" PRIuGRUB_UINT64_T"\": %s\n" , + keyslot.idx, grub_errmsg);continue ; }@@ -640,7 +640,7 @@ luks2_recover_key (grub_disk_t source,* TRANSLATORS: It's a cryptographic key slot: one element of an array * where each element is either empty or holds a key. */ -grub_printf_ (N_ ("Slot %" PRIuGRUB_SIZE" opened\n" ), i); +grub_printf_ (N_ ("Slot \"%" PRIuGRUB_UINT64_T"\" opened\n" ), keyslot.idx); candidate_key_len = keyslot.key_size;break ;Signature · 1 line
2.27.0
Re: [PATCH v8 08/18] luks2: Rename json index variables to names that they are obviously json indexes
In reply to Glenn WashburnQuoted 1 line · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>
Quoted 98 lines
--- grub-core/disk/luks2.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)grub-core/disk/luks2.c +15
-15@@ -261,53 +261,53 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)static grub_err_t luks2_get_keyslot (grub_luks2_keyslot_t *k,grub_luks2_digest_t *d,grub_luks2_segment_t *s, -const grub_json_t *root,grub_size_t keyslot_idx) +const grub_json_t *root,grub_size_t keyslot_json_idx) {grub_json_t keyslots, keyslot, digests, digest, segments, segment; -grub_size_t i, size; +grub_size_t json_idx, size;/* Get nth keyslot */ if (grub_json_getvalue (&keyslots, root,"keyslots" ) || -grub_json_getchild (&keyslot, &keyslots, keyslot_idx) || +grub_json_getchild (&keyslot, &keyslots, keyslot_json_idx) ||grub_json_getuint64 (&k->idx, &keyslot,NULL ) ||grub_json_getchild (&keyslot, &keyslot,0 ) ||luks2_parse_keyslot (k, &keyslot)) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse keyslot %" PRIuGRUB_SIZE, keyslot_idx); +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse keyslot %" PRIuGRUB_SIZE, keyslot_json_idx);/* Get digest that matches the keyslot. */ if (grub_json_getvalue (&digests, root,"digests" ) ||grub_json_getsize (&size, &digests))return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not get digests" ); -for (i =0 ; i < size; i++) +for (json_idx =0 ; json_idx < size; json_idx++) { -if (grub_json_getchild (&digest, &digests, i) || +if (grub_json_getchild (&digest, &digests, json_idx) ||grub_json_getuint64 (&d->idx, &digest,NULL ) ||grub_json_getchild (&digest, &digest,0 ) ||luks2_parse_digest (d, &digest)) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse digest %" PRIuGRUB_SIZE, i); +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse digest %" PRIuGRUB_SIZE, json_idx);if ((d->keyslots & (1 << k->idx)))break ; } -if (i == size) +if (json_idx == size)return grub_error (GRUB_ERR_FILE_NOT_FOUND ,"No digest for keyslot \"%" PRIuGRUB_UINT64_T"\"" , k->idx);/* Get segment that matches the digest. */ if (grub_json_getvalue (&segments, root,"segments" ) ||grub_json_getsize (&size, &segments))return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not get segments" ); -for (i =0 ; i < size; i++) +for (json_idx =0 ; json_idx < size; json_idx++) { -if (grub_json_getchild (&segment, &segments, i) || +if (grub_json_getchild (&segment, &segments, json_idx) ||grub_json_getuint64 (&s->idx, &segment,NULL ) ||grub_json_getchild (&segment, &segment,0 ) ||luks2_parse_segment (s, &segment)) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse segment %" PRIuGRUB_SIZE, i); +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse segment %" PRIuGRUB_SIZE, json_idx);if ((d->segments & (1 << s->idx)))break ; } -if (i == size) +if (json_idx == size)return grub_error (GRUB_ERR_FILE_NOT_FOUND ,"No segment for digest \"%" PRIuGRUB_UINT64_T"\"" , d->idx);return GRUB_ERR_NONE ;@@ -542,7 +542,7 @@ luks2_recover_key (grub_disk_t source,grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN ];char passphrase[MAX_PASSPHRASE ], cipher[32 ];char *json_header =NULL , *part =NULL , *ptr; -grub_size_t candidate_key_len =0 , i, size; +grub_size_t candidate_key_len =0 , json_idx, size;grub_luks2_header_t header;grub_luks2_keyslot_t keyslot;grub_luks2_digest_t digest;@@ -596,9 +596,9 @@ luks2_recover_key (grub_disk_t source, }/* Try all keyslot */ -for (i =0 ; i < size; i++) +for (json_idx =0 ; json_idx < size; json_idx++) { - ret =luks2_get_keyslot (&keyslot, &digest, &segment, json, i); + ret =luks2_get_keyslot (&keyslot, &digest, &segment, json, json_idx);if (ret) goto err;Signature · 1 line
2.27.0
Re: [PATCH v8 09/18] luks2: Add string "index" to user strings using a json index.
In reply to Glenn WashburnQuoted 5 lines · Glenn Washburn
This allows error messages to be more easily distinguishable between indexes and slot keys. The former include the string "index" in the error/debug string, and the later are surrounded in quotes.
Signed-off-by Glenn Washburn <[email protected]>
Nit: the commit subject still has a trailing dot. I guess Daniel can fix it when applying, though. So:
Quoted 38 lines
--- grub-core/disk/luks2.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)grub-core/disk/luks2.c +3
-3@@ -272,7 +272,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s grub_json_getuint64 (&k->idx, &keyslot, NULL) || grub_json_getchild (&keyslot, &keyslot, 0) || luks2_parse_keyslot (k, &keyslot)) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse keyslot %" PRIuGRUB_SIZE, keyslot_json_idx); +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse keyslot index %" PRIuGRUB_SIZE, keyslot_json_idx);/* Get digest that matches the keyslot. */ if (grub_json_getvalue (&digests, root, "digests") ||@@ -284,7 +284,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s grub_json_getuint64 (&d->idx, &digest, NULL) || grub_json_getchild (&digest, &digest, 0) || luks2_parse_digest (d, &digest)) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse digest %" PRIuGRUB_SIZE, json_idx); +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse digest index %" PRIuGRUB_SIZE, json_idx);if ((d->keyslots & (1 << k->idx)))break ;@@ -302,7 +302,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s grub_json_getuint64 (&s->idx, &segment, NULL) || grub_json_getchild (&segment, &segment, 0) || luks2_parse_segment (s, &segment)) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse segment %" PRIuGRUB_SIZE, json_idx); +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Could not parse segment index %" PRIuGRUB_SIZE, json_idx);if ((d->segments & (1 << s->idx)))break ;Signature · 1 line
2.27.0
Re: [PATCH v8 10/18] cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals
In reply to Glenn WashburnQuoted 4 lines · Glenn Washburn
The new macro
GRUB_TYPE_BITS(type)returns the number of bits allocated for type.Signed-off-by Glenn Washburn <[email protected]>
Quoted 50 lines
--- grub-core/disk/cryptodisk.c | 7 ++++--- include/grub/types.h | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-)grub-core/disk/cryptodisk.c +4
-3@@ -281,20 +281,21 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, }break ;case GRUB_CRYPTODISK_MODE_IV_PLAIN64 : - iv[1 ] =grub_cpu_to_le32 (sector >>32 ); + iv[1 ] =grub_cpu_to_le32 (sector >>GRUB_TYPE_BITS (iv[0 ]));/* FALLTHROUGH */ case GRUB_CRYPTODISK_MODE_IV_PLAIN : iv[0 ] =grub_cpu_to_le32 (sector &0xFFFFFFFF );break ;case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64 : - iv[1 ] =grub_cpu_to_le32 (sector >> (32 - dev->log_sector_size)); + iv[1 ] =grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1 ]) + - dev->log_sector_size)); iv[0 ] =grub_cpu_to_le32 ((sector << dev->log_sector_size) &0xFFFFFFFF );break ;case GRUB_CRYPTODISK_MODE_IV_BENBI : {grub_uint64_t num = (sector << dev->benbi_log) +1 ; - iv[sz -2 ] =grub_cpu_to_be32 (num >>32 ); + iv[sz -2 ] =grub_cpu_to_be32 (num >>GRUB_TYPE_BITS (iv[0 ])); iv[sz -1 ] =grub_cpu_to_be32 (num &0xFFFFFFFF ); }break ;include/grub/types.h +2
-0@@ -80,6 +80,8 @@# define GRUB_CHAR_BIT __CHAR_BIT__#endif +#define GRUB_TYPE_BITS (type) (sizeof(type) * GRUB_CHAR_BIT) +/* Define various wide integers. */ typedef signed char grub_int8_t ;typedef short grub_int16_t ;Signature · 1 line
2.27.0
Re: [PATCH v8 11/18] cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
In reply to Glenn WashburnQuoted 4 lines · Glenn Washburn
Add GRUB_TYPE_U_MAX/MIN(type) macros to get the max/min values for an unsigned number with size of type.
Signed-off-by Glenn Washburn <[email protected]>
Quoted 58 lines
--- grub-core/disk/cryptodisk.c | 8 ++++---- include/grub/types.h | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-)grub-core/disk/cryptodisk.c +4
-4@@ -284,23 +284,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, iv[1 ] =grub_cpu_to_le32 (sector >>GRUB_TYPE_BITS (iv[0 ]));/* FALLTHROUGH */ case GRUB_CRYPTODISK_MODE_IV_PLAIN : - iv[0 ] =grub_cpu_to_le32 (sector &0xFFFFFFFF ); + iv[0 ] =grub_cpu_to_le32 (sector &GRUB_TYPE_U_MAX (iv[0 ]));break ;case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64 : iv[1 ] =grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1 ]) - dev->log_sector_size)); iv[0 ] =grub_cpu_to_le32 ((sector << dev->log_sector_size) - &0xFFFFFFFF ); + &GRUB_TYPE_U_MAX (iv[0 ]));break ;case GRUB_CRYPTODISK_MODE_IV_BENBI : {grub_uint64_t num = (sector << dev->benbi_log) +1 ; iv[sz -2 ] =grub_cpu_to_be32 (num >>GRUB_TYPE_BITS (iv[0 ])); - iv[sz -1 ] =grub_cpu_to_be32 (num &0xFFFFFFFF ); + iv[sz -1 ] =grub_cpu_to_be32 (num &GRUB_TYPE_U_MAX (iv[0 ])); }break ;case GRUB_CRYPTODISK_MODE_IV_ESSIV : - iv[0 ] =grub_cpu_to_le32 (sector &0xFFFFFFFF ); + iv[0 ] =grub_cpu_to_le32 (sector &GRUB_TYPE_U_MAX (iv[0 ])); err =grub_crypto_ecb_encrypt (dev->essiv_cipher, iv, iv, dev->cipher->cipher->blocksize);if (err)include/grub/types.h +7
-0@@ -161,6 +161,13 @@ typedef grub_int32_t grub_ssize_t;#endif # define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1) +/* +* Cast to unsigned long long so that the "return value" is always a consistent +* type and to ensure an unsigned value regardless of type parameter. +*/ +#define GRUB_TYPE_U_MAX (type) ((unsigned long long)((typeof (type))(~0))) +#define GRUB_TYPE_U_MIN (type) 0ULL +typedef grub_uint64_t grub_properly_aligned_t ;#define GRUB_PROPERLY_ALIGNED_ARRAY (name, size) grub_properly_aligned_t name[((size) + sizeof (grub_properly_aligned_t) - 1) / sizeof (grub_properly_aligned_t)]Signature · 1 line
2.27.0
Re: [PATCH v8 12/18] luks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors
In reply to Glenn WashburnQuoted 8 lines · Glenn Washburn
We need to convert the sectors from the size of the underlying device to the cryptodisk sector size; segment.size is in bytes which need to be converted to cryptodisk sectors as well.
Also, removed an empty statement.
Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Daniel Kiper <[email protected]>
Quoted 33 lines
--- grub-core/disk/luks2.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)grub-core/disk/luks2.c +4
-3@@ -429,7 +429,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,grub_uint8_t salt[GRUB_CRYPTODISK_MAX_KEYLEN ];grub_uint8_t *split_key =NULL ;grub_size_t saltlen =sizeof (salt); -char cipher[32 ], *p;; +char cipher[32 ], *p;const gcry_md_spec_t *hash;gcry_err_code_t gcry_ret;grub_err_t ret;@@ -615,9 +615,10 @@ luks2_recover_key (grub_disk_t source, crypt->log_sector_size =sizeof (unsigned int ) *8 -__builtin_clz ((unsigned int ) segment.sector_size) -1 ;if (grub_strcmp (segment.size,"dynamic" ) ==0 ) - crypt->total_sectors =grub_disk_get_size (source) - crypt->offset_sectors; + crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size)) + - crypt->offset_sectors;else - crypt->total_sectors =grub_strtoull (segment.size,NULL ,10 ); + crypt->total_sectors =grub_strtoull (segment.size,NULL ,10 ) >> crypt->log_sector_size; ret =luks2_decrypt_key (candidate_key, source, crypt, &keyslot, (const grub_uint8_t *) passphrase,grub_strlen (passphrase));Signature · 1 line
2.27.0
Re: [PATCH v8 13/18] cryptodisk: Properly handle non-512 byte sized sectors
In reply to Glenn WashburnQuoted 17 lines · Glenn Washburn
By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.
There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the
iv_large_sectorsoption and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.
Quoted 8 lines
One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.
Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Daniel Kiper <[email protected]>
Quoted 234 lines
--- grub-core/disk/cryptodisk.c | 55 +++++++++++++++++++++++-------------- grub-core/disk/luks.c | 5 ++-- grub-core/disk/luks2.c | 7 ++++- include/grub/cryptodisk.h | 8 +++++- 4 files changed, 50 insertions(+), 25 deletions(-)grub-core/disk/cryptodisk.c +34
-21@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,static gcry_err_code_t grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,grub_uint8_t * data,grub_size_t len, -grub_disk_addr_t sector,int do_encrypt) +grub_disk_addr_t sector,grub_size_t log_sector_size, +int do_encrypt) {grub_size_t i;gcry_err_code_t err;@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,return (do_encrypt ?grub_crypto_ecb_encrypt (dev->cipher, data, data, len) :grub_crypto_ecb_decrypt (dev->cipher, data, data, len)); -for (i =0 ; i < len; i += (1U << dev->log_sector_size)) +for (i =0 ; i < len; i += (1U << log_sector_size)) {grub_size_t sz = ((dev->cipher->cipher->blocksize + sizeof (grub_uint32_t) - 1)@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,if (!ctx)return GPG_ERR_OUT_OF_MEMORY ; - tmp =grub_cpu_to_le64 (sector << dev->log_sector_size); + tmp =grub_cpu_to_le64 (sector << log_sector_size); dev->iv_hash->init (ctx); dev->iv_hash->write (ctx, dev->iv_prefix, dev->iv_prefix_len); dev->iv_hash->write (ctx, &tmp,sizeof (tmp));@@ -281,15 +282,27 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, }break ;case GRUB_CRYPTODISK_MODE_IV_PLAIN64 : - iv[1 ] =grub_cpu_to_le32 (sector >>GRUB_TYPE_BITS (iv[0 ])); -/* FALLTHROUGH */ case GRUB_CRYPTODISK_MODE_IV_PLAIN : - iv[0 ] =grub_cpu_to_le32 (sector &GRUB_TYPE_U_MAX (iv[0 ])); +/* +* The IV is a 32 or 64 bit value of the dm-crypt native sector +* number. If using 32 bit IV mode, zero out the most significant +* 32 bits. +*/ + { +grub_uint64_t iv64; + + iv64 =grub_cpu_to_le64 (sector << (log_sector_size + -GRUB_CRYPTODISK_IV_LOG_SIZE )); +grub_set_unaligned64 (iv, iv64); +if (dev->mode_iv ==GRUB_CRYPTODISK_MODE_IV_PLAIN ) + iv[1 ] =0 ; + }break ;case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64 : +/* The IV is the 64 bit byte offset of the sector. */ iv[1 ] =grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1 ]) - - dev->log_sector_size)); - iv[0 ] =grub_cpu_to_le32 ((sector << dev->log_sector_size) + - log_sector_size)); + iv[0 ] =grub_cpu_to_le32 ((sector << log_sector_size) &GRUB_TYPE_U_MAX (iv[0 ]));break ;case GRUB_CRYPTODISK_MODE_IV_BENBI :@@ -312,10 +325,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,case GRUB_CRYPTODISK_MODE_CBC :if (do_encrypt) err =grub_crypto_cbc_encrypt (dev->cipher, data + i, data + i, - (1U << dev->log_sector_size), iv); + (1U << log_sector_size), iv);else err =grub_crypto_cbc_decrypt (dev->cipher, data + i, data + i, - (1U << dev->log_sector_size), iv); + (1U << log_sector_size), iv);if (err)return err;break ;@@ -323,10 +336,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,case GRUB_CRYPTODISK_MODE_PCBC :if (do_encrypt) err =grub_crypto_pcbc_encrypt (dev->cipher, data + i, data + i, - (1U << dev->log_sector_size), iv); + (1U << log_sector_size), iv);else err =grub_crypto_pcbc_decrypt (dev->cipher, data + i, data + i, - (1U << dev->log_sector_size), iv); + (1U << log_sector_size), iv);if (err)return err;break ;@@ -338,7 +351,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,if (err)return err; -for (j =0 ; j < (1U << dev->log_sector_size); +for (j =0 ; j < (1U << log_sector_size); j += dev->cipher->cipher->blocksize) { grub_crypto_xor (data + i + j, data + i + j, iv,@@ -369,11 +382,11 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,if (do_encrypt) err =grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i, - (1U << dev->log_sector_size)); + (1U << log_sector_size));else err =grub_crypto_ecb_decrypt (dev->cipher, data + i, data + i, - (1U << dev->log_sector_size)); + (1U << log_sector_size));if (err)return err;lrw_xor (&sec, dev, data + i);@@ -382,10 +395,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,case GRUB_CRYPTODISK_MODE_ECB :if (do_encrypt) err =grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i, - (1U << dev->log_sector_size)); + (1U << log_sector_size));else err =grub_crypto_ecb_decrypt (dev->cipher, data + i, data + i, - (1U << dev->log_sector_size)); + (1U << log_sector_size));if (err)return err;break ;@@ -400,9 +413,9 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,gcry_err_code_t grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,grub_uint8_t * data,grub_size_t len, -grub_disk_addr_t sector) +grub_disk_addr_t sector,grub_size_t log_sector_size) { -return grub_cryptodisk_endecrypt (dev, data, len, sector,0 ); +return grub_cryptodisk_endecrypt (dev, data, len, sector, log_sector_size,0 ); } grub_err_t@@ -767,7 +780,7 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector, } gcry_err =grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf, size << disk->log_sector_size, - sector,0 ); + sector, dev->log_sector_size,0 );return grub_crypto_gcry_error (gcry_err); }@@ -808,7 +821,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector, gcry_err =grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp, size << disk->log_sector_size, - sector,1 ); + sector, disk->log_sector_size,1 );if (gcry_err) {grub_free (tmp);grub-core/disk/luks.c +3
-2@@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,return NULL ; newdev->offset_sectors =grub_be_to_cpu32 (header.payloadOffset); newdev->source_disk =NULL ; - newdev->log_sector_size =9 ; + newdev->log_sector_size =GRUB_LUKS1_LOG_SECTOR_SIZE ; newdev->total_sectors =grub_disk_native_sectors (disk) - newdev->offset_sectors;grub_memcpy (newdev->uuid, uuid,sizeof (uuid)); newdev->modname ="luks" ;@@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source,return err; } - gcry_err =grub_cryptodisk_decrypt (dev, split_key, length,0 ); + gcry_err =grub_cryptodisk_decrypt (dev, split_key, length,0 , +GRUB_LUKS1_LOG_SECTOR_SIZE );if (gcry_err) {grub_free (split_key);grub-core/disk/luks2.c +6
-1@@ -504,7 +504,12 @@ luks2_decrypt_key (grub_uint8_t *out_key, goto err; } - gcry_ret =grub_cryptodisk_decrypt (crypt, split_key, k->area.size,0 ); +/* +* The key slots area is always encrypted in 512-byte sectors, +* regardless of encrypted data sector size. +*/ + gcry_ret =grub_cryptodisk_decrypt (crypt, split_key, k->area.size,0 , +GRUB_LUKS1_LOG_SECTOR_SIZE );if (gcry_ret) { ret =grub_crypto_gcry_error (gcry_ret);include/grub/cryptodisk.h +7
-1@@ -48,6 +48,12 @@ typedef enum#define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71 +/* LUKS1 specification defines the block size to always be 512 bytes. */ +#define GRUB_LUKS1_LOG_SECTOR_SIZE 9 + +/* By default dm-crypt increments the IV every 512 bytes. */ +#define GRUB_CRYPTODISK_IV_LOG_SIZE 9 +#define GRUB_CRYPTODISK_GF_LOG_SIZE 7#define GRUB_CRYPTODISK_GF_SIZE (1U << GRUB_CRYPTODISK_GF_LOG_SIZE)#define GRUB_CRYPTODISK_GF_LOG_BYTES (GRUB_CRYPTODISK_GF_LOG_SIZE - 3)@@ -144,7 +150,7 @@ grub_cryptodisk_setkey (grub_cryptodisk_t dev,gcry_err_code_t grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,grub_uint8_t * data,grub_size_t len, -grub_disk_addr_t sector); +grub_disk_addr_t sector,grub_size_t log_sector_size);grub_err_t grub_cryptodisk_insert (grub_cryptodisk_t newdev,const char *name,grub_disk_t source);Signature · 1 line
2.27.0
Re: [PATCH v8 15/18] luks2: Error check segment.sector_size
In reply to Glenn WashburnQuoted 1 line · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>
Quoted 29 lines
--- grub-core/disk/luks2.c | 11 +++++++++++ 1 file changed, 11 insertions(+)grub-core/disk/luks2.c +11
-0@@ -629,6 +629,17 @@ luks2_recover_key (grub_disk_t source,grub_dprintf ("luks2" ,"Trying keyslot \"%" PRIuGRUB_UINT64_T"\"\n" , keyslot.idx); +/* Sector size should be one of 512, 1024, 2048, or 4096. */ +if (!(segment.sector_size ==512 || segment.sector_size ==1024 || + segment.sector_size ==2048 || segment.sector_size ==4096 )) + { +grub_dprintf ("luks2" ,"Segment \"%" PRIuGRUB_UINT64_T"\" sector" +" size %" PRIuGRUB_UINT64_T" is not one of" +" 512, 1024, 2048, or 4096\n" , + segment.idx, segment.sector_size); +continue ; + } +/* Set up disk according to keyslot's segment. */ crypt->offset_sectors =grub_divmod64 (segment.offset, segment.sector_size,NULL ); crypt->log_sector_size =sizeof (unsigned int ) *8 Signature · 1 line
2.27.0
Re: [PATCH v8 16/18] mips: Enable __clzdi2()
In reply to Glenn WashburnQuoted 6 lines · Glenn Washburn
This patch is similiar to commit
9dab2f51e(sparc: Enable__clzsi2()and__clzdi2()) but for MIPS target and__clzdi2only,__clzsi2was already enabled.Signed-off-by Glenn Washburn <[email protected]>Suggested-by Daniel Kiper <[email protected]>
Quoted 34 lines
--- grub-core/kern/compiler-rt.c | 2 +- include/grub/compiler-rt.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)grub-core/kern/compiler-rt.c +1
-1@@ -448,7 +448,7 @@ __clzsi2 (grub_uint32_t val) }#endif -#if defined(__riscv) || defined(__sparc__) +#if defined(__mips__) || defined(__riscv) || defined(__sparc__)int __clzdi2 (grub_uint64_t val) {include/grub/compiler-rt.h +1
-1@@ -115,7 +115,7 @@ intEXPORT_FUNC (__clzsi2) (grub_uint32_t val);#endif -#if defined(__riscv) || defined(__sparc__) +#if defined(__mips__) || defined(__riscv) || defined(__sparc__)int EXPORT_FUNC (__clzdi2 ) (grub_uint64_t val);#endif Signature · 1 line
2.27.0
Re: [PATCH v8 17/18] misc: Add grub_log2ull macro for calculating log base 2 of 64-bit integers
In reply to Glenn WashburnQuoted 2 lines · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Daniel Kiper <[email protected]>
Quoted 19 lines
--- include/grub/misc.h | 3 +++ 1 file changed, 3 insertions(+)include/grub/misc.h +3
-0@@ -482,4 +482,7 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file,#define grub_max (a, b) (((a) > (b)) ? (a) : (b))#define grub_min (a, b) (((a) < (b)) ? (a) : (b)) +#define grub_log2ull (n) (GRUB_TYPE_BITS (grub_uint64_t) \ + - __builtin_clzll (n) - 1) +#endif /* ! GRUB_MISC_HEADER */ Signature · 1 line
2.27.0
Re: [PATCH v8 18/18] luks2: Use grub_log2ull to calculate log_sector_size and improve readability
In reply to Glenn WashburnQuoted 2 lines · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Daniel Kiper <[email protected]>
Quoted 21 lines
--- grub-core/disk/luks2.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)grub-core/disk/luks2.c +1
-2@@ -642,8 +642,7 @@ luks2_recover_key (grub_disk_t source,/* Set up disk according to keyslot's segment. */ crypt->offset_sectors =grub_divmod64 (segment.offset, segment.sector_size,NULL ); - crypt->log_sector_size =sizeof (unsigned int ) *8 - -__builtin_clz ((unsigned int ) segment.sector_size) -1 ; + crypt->log_sector_size =grub_log2ull (segment.sector_size);/* Set to the source disk size, which is the maximum we allow. */ max_crypt_sectors = grub_disk_convert_sector(source, source->total_sectors,Signature · 1 line
2.27.0
Quoted 34 lines · Daniel Kiper
Quoted 24 lines · Glenn Washburn
This patch series, fixes issues with v7 series. Some of those patches had changes applied to the wrong patch. The added structure member was renamed again, this time to idx. And there is an added patch that renames some json index variables to note that they are such.
Glenn
Glenn Washburn (18):
disk: Renamegrub_disk_get_sizetogrub_disk_native_sectors
misc: Add parentheses aroundALIGN_UPandALIGN_DOWNarguments
whitespace: convert 8 spaces to tabs
luks2: Remove unused argument ingrub_error
luks2: Make sure all fields of output argument inluks2_parse_digest()
are written to
luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
luks2: Use more intuitive object name instead of json index in user
messages
luks2: Rename json index variables to names that they are obviously
json indexes
luks2: Add string "index" to user strings using a json index.
cryptodisk: Add macroGRUB_TYPE_BITS()to replace some literals
cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
luks2:grub_cryptodisk_t->total_sectorsis the max number of device
native sectorsQuoted 8 lines
cryptodisk: Properly handle non-512 byte sized sectors
luks2: Better error handling when setting up the cryptodisk
luks2: Error check segment.sector_size
mips: Enable__clzdi2()
misc: Addgrub_log2ullmacro for calculating log base 2 of 64-bit
integers
luks2: Usegrub_log2ullto calculatelog_sector_sizeand improve
readability
Quoted 6 lines
I will get all the patches except #14 and maybe some changes after that one which does not apply due to lack of it.
Thank you for doing the work!
Daniel
For what it's worth, I've also gone through all patches except 14 and added by RBs. Thanks!
Patrick
Quoted 35 lines · Patrick Steinhardt
Quoted 34 lines · Daniel Kiper
Quoted 32 lines · Glenn Washburn
This patch series, fixes issues with v7 series. Some of those patches had changes applied to the wrong patch. The added structure member was renamed again, this time to idx. And there is an added patch that renames some json index variables to note that they are such.
Glenn
Glenn Washburn (18):
disk: Renamegrub_disk_get_sizetogrub_disk_native_sectors
misc: Add parentheses aroundALIGN_UPandALIGN_DOWNarguments
whitespace: convert 8 spaces to tabs
luks2: Remove unused argument ingrub_error
luks2: Make sure all fields of output argument inluks2_parse_digest()
are written to
luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
luks2: Use more intuitive object name instead of json index in user
messages
luks2: Rename json index variables to names that they are obviously
json indexes
luks2: Add string "index" to user strings using a json index.
cryptodisk: Add macroGRUB_TYPE_BITS()to replace some literals
cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
luks2:grub_cryptodisk_t->total_sectorsis the max number of device
native sectors
cryptodisk: Properly handle non-512 byte sized sectors
luks2: Better error handling when setting up the cryptodisk
luks2: Error check segment.sector_size
mips: Enable__clzdi2()
misc: Addgrub_log2ullmacro for calculating log base 2 of 64-bit
integers
luks2: Usegrub_log2ullto calculatelog_sector_sizeand improve
readability
Quoted 2 lines
Quoted 2 lines
I will get all the patches except #14 and maybe some changes after that one which does not apply due to lack of it.
Thanks! However, I have pushed some of these patches before you did review.
Daniel
Re: [PATCH v8 14/18] luks2: Better error handling when setting up the cryptodisk
In reply to Glenn WashburnQuoted 143 lines · Glenn Washburn
Quoted 131 lines · On Thu, 10 Dec 2020 17:07:07 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 130 lines · Glenn Washburn
First, check to make sure that source disk has a known size. If not, print debug message and return error. There are 4 cases where
GRUB_DISK_SIZE_UNKNOWNis set (biosdisk, obdisk, ofdisk, and uboot), and in all those cases processing continues. So this is probably a bit conservative. However, 3 of the cases seem pathological, and the other, biosdisk, happens when booting from a cd. Since I doubt booting from a LUKS2 volume on a cd is a big use case, we'll error until someone complains.Do some sanity checking on data coming from the luks header. If segment.size is "dynamic", verify that the offset is not past the end of disk. Otherwise, check for errors from
grub_strtoullwhen converting segment size from string. If aGRUB_ERR_BAD_NUMBERerror was returned, then the string was not a valid parsable number, so skip the key. IfGRUB_ERR_OUT_OF_RANGEwas returned, then there was an overflow in converting to a 64-bit unsigned integer. So this could be a very large disk (perhaps large raid array). In this case, we want to continue to try to use this key, but only allow access up to the end of the source disk.Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 84++++++++++++++++++++++++++++++++++++++++--
include/grub/disk.h|
17 +++++++++ 2 files changed, 98 insertions(+), 3 deletions(-)grub-core/disk/luks2.c +3
-0@@ -600,12 +600,26 @@ luks2_recover_key (grub_disk_t source, goto err; } +if (source->total_sectors ==GRUB_DISK_SIZE_UNKNOWN ) + { +/* FIXME: Allow use of source disk, and maybe cause errors in read. */
grub-core/disk/luks2.c +2
-0+ grub_dprintf ("luks2", "Source disk %s has an unknown size, " + "conservatively returning error\n",
source->name);
+ ret =grub_error(GRUB_ERR_BUG, "Unknown size of luks2
source device");grub-core/disk/luks2.c +6
-0+ goto err; + } +/* Try all keyslot */ for (json_idx =0 ; json_idx < size; json_idx++) { + typeof(source->total_sectors) max_crypt_sectors = 0; + + grub_errno =GRUB_ERR_NONE ; ret = luks2_get_keyslot (&keyslot, &digest, &segment, json,
json_idx); if (ret)grub-core/disk/luks2.c +2
-0goto err; +if (grub_errno !=GRUB_ERR_NONE ) + grub_dprintf ("luks2", "Ignoring unhandled error %d fromluks2_get_keyslot\n",
grub_errno);if (keyslot.priority == 0)grub-core/disk/luks2.c +0
-0{@@ -619,11 +633,75 @@ luks2_recover_key (grub_disk_t source, crypt->offset_sectors = grub_divmod64 (segment.offset,segment.sector_size, NULL);
crypt->log_sector_size= sizeof (unsigned int) * 8
-__builtin_clz((unsigned int)
segment.sector_size) - 1;
+ /* Set to the source disk size, which is the maximum we
allow. */grub-core/disk/luks2.c +2
-0+ max_crypt_sectors = grub_disk_convert_sector(source, +
source->total_sectors,
+crypt->log_sector_size); +grub-core/disk/luks2.c +3
-0+ if (max_crypt_sectors < crypt->offset_sectors) + { + grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\"has offset"
grub-core/disk/luks2.c +1
-0+ " %"PRIuGRUB_UINT64_T" which isgreater than"
grub-core/disk/luks2.c +1
-0+ " source disk size%"PRIuGRUB_UINT64_T","
grub-core/disk/luks2.c +2
-0+" skipping\n" , + segment.idx,
crypt->offset_sectors,grub-core/disk/luks2.c +4
-1+ max_crypt_sectors); +continue ; + } +if (grub_strcmp (segment.size,"dynamic" ) ==0 ) - crypt->total_sectors = (grub_disk_get_size (source) >>(
crypt->log_sector_size-source->log_sector_size))grub-core/disk/luks2.c +1
-1- - crypt->offset_sectors; + crypt->total_sectors = max_crypt_sectors -
crypt->offset_sectors; elsegrub-core/disk/luks2.c +0
-1- crypt->total_sectors = grub_strtoull (segment.size, NULL,10) >>
crypt->log_sector_size;grub-core/disk/luks2.c +3
-0+ { + grub_errno =GRUB_ERR_NONE ; +/* Convert segment.size to sectors, rounding up to nearest sector */
grub-core/disk/luks2.c +1
-0+ crypt->total_sectors = grub_strtoull (segment.size,NULL, 10);
grub-core/disk/luks2.c +2
-0+ crypt->total_sectors = ALIGN_UP (crypt->total_sectors, + 1 <<
crypt->log_sector_size);grub-core/disk/luks2.c +7
-0+ crypt->total_sectors >>= crypt->log_sector_size; + +if (grub_errno ==GRUB_ERR_NONE ) + ; +else if (grub_errno ==GRUB_ERR_BAD_NUMBER ) + { + grub_dprintf ("luks2", "Segment\"%"PRIuGRUB_UINT64_T"\" size"
grub-core/disk/luks2.c +1
-0+ " \"%s\" is not a parsablenumber\n",
grub-core/disk/luks2.c +7
-0+ segment.idx, segment.size); +continue ; + } +else if (grub_errno ==GRUB_ERR_OUT_OF_RANGE ) + { +/* +* There was an overflow in parsing segment.size, so disk must
grub-core/disk/luks2.c +3
-0+* be very large or the string is incorrect. +*/ + grub_dprintf ("luks2", "Segment\"%"PRIuGRUB_UINT64_T"\" size"
grub-core/disk/luks2.c +1
-0+ " %s overflowed 64-bitunsigned integer,"
Quoted 8 lines
grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 683
Quoted 5 lines
grub-core/disk/luks2.c +1
-0+ " the end of the cryptodevice will be"
grub-core/disk/luks2.c +3
-0+" inaccessible\n" , + segment.idx, segment.size); +if (crypt->total_sectors > max_crypt_sectors)I think this if is bogus. You should clamp
crypt->total_sectorswithout any checks here.
Quoted 6 lines
Actually, I wouldn't call this a clamp because in the overflow case
crypt->total_sectorsalways equals 0. I just realized this, and its becausegrub_strtoullwill return 2^64-1 thus causing the followingALIGN_UPto overflow returning 0. Suffice to say that's not what we want. My original intent was what happened before theALIGN_UPcode was introduced, which wouldALIGN_DOWN.
Ugh... Right. That is why I still think you should do further calculations on value returned from grub_strtoull() if grub_errno == GRUB_ERR_NONE only. I understand that then crypt->total_sectors does not contain total sectors for a while but you can rectify this a bit by putting short comment before grub_strtoull() call.
Quoted 16 lines
Here's an example illustrating why I wanted and still think the intent of this check is reasonable. Suppose we have a disk size 2^67 bytes with 512 byte (2^9) sized sectors. It will have 2^58 sectors. Further suppose, there is a LUKS volume with size 2^65 bytes starting at the beginning and a sector size of 4096 bytes (2^12). This will cause an overflow, so
grub_strtoullwill return 2^64-1, this should have us setcrypt->total_sectorsto 2^52-1. Since we don't know how much the overflow is (1 byte or 1 terabyte), we don't know how many more sectors til the end of the LUKS encrypted area. In this casemax_crypt_sectorswill be 2^(58+9-12) => 2^57 sectors. So here we see thatcrypt->total_sectors<max_crypt_sectorsin the overflow case. If we do as you suggestcrypt->total_sectorswill be set to 2^57, and thus it will be valid to read past the end of the encrypted data (ie. block 2^56 of the 4k sector crypt will be a sector starting at byte 2^68, which is more than the 2^65 byte size volume).
Quoted 7 lines
On the one hand, I like your suggestion because it allows reading all possible encrypted data, at the cost of reading, decrypting, and returning non-encrypted data (ie random garbage). While keeping the check, will prevent returning garbage at the cost of not allowing access to all encrypted sectors. I think we should keep the check and document a known limitation of 2^64 byte maximum sized LUKS volumes. And that larger sized volumes can be read only up to byte 2^64.
If think the code should look like this:
/*
* ...a comment saying what crypt->total_sectors contains
* and why LUKS2 volumes larger than 2^64 does not work... */
crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
if (grub_errno == GRUB_ERR_NONE)
{
crypt->total_sectors = ALIGN_UP (crypt->total_sectors, 1 << crypt->log_sector_size);
crypt->total_sectors >>= crypt->log_sector_size;
}
else if (grub_errno == GRUB_ERR_BAD_NUMBER)
{
...
continue;
} if (grub_errno == GRUB_ERR_OUT_OF_RANGE ||
!crypt->total_sectors ||
crypt->total_sectors > max_crypt_sectors)
{
...
continue;
/*
* Yes, I think we should not guess crypt->total_sectors
* value and always fail. It seems safer.
*/
}Daniel
Quoted 36 lines · Daniel Kiper
Quoted 35 lines · Patrick Steinhardt
Quoted 34 lines · Daniel Kiper
Quoted 32 lines · Glenn Washburn
This patch series, fixes issues with v7 series. Some of those patches had changes applied to the wrong patch. The added structure member was renamed again, this time to idx. And there is an added patch that renames some json index variables to note that they are such.
Glenn
Glenn Washburn (18):
disk: Renamegrub_disk_get_sizetogrub_disk_native_sectors
misc: Add parentheses aroundALIGN_UPandALIGN_DOWNarguments
whitespace: convert 8 spaces to tabs
luks2: Remove unused argument ingrub_error
luks2: Make sure all fields of output argument inluks2_parse_digest()
are written to
luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
luks2: Use more intuitive object name instead of json index in user
messages
luks2: Rename json index variables to names that they are obviously
json indexes
luks2: Add string "index" to user strings using a json index.
cryptodisk: Add macroGRUB_TYPE_BITS()to replace some literals
cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
luks2:grub_cryptodisk_t->total_sectorsis the max number of device
native sectors
cryptodisk: Properly handle non-512 byte sized sectors
luks2: Better error handling when setting up the cryptodisk
luks2: Error check segment.sector_size
mips: Enable__clzdi2()
misc: Addgrub_log2ullmacro for calculating log base 2 of 64-bit
integers
luks2: Usegrub_log2ullto calculatelog_sector_sizeand improve
readability
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
I will get all the patches except #14 and maybe some changes after that one which does not apply due to lack of it.
Thanks! However, I have pushed some of these patches before you did review.
Daniel
I thought as much, though I didn't check. I don't really mind, thought it might make sense to send them out in any case.
Patrick
Quoted 40 lines · Patrick Steinhardt
Quoted 39 lines · Daniel Kiper
Quoted 37 lines · Patrick Steinhardt
Quoted 36 lines · Daniel Kiper
Quoted 24 lines · Glenn Washburn
This patch series, fixes issues with v7 series. Some of those patches had changes applied to the wrong patch. The added structure member was renamed again, this time to idx. And there is an added patch that renames some json index variables to note that they are such.
Glenn
Glenn Washburn (18):
disk: Renamegrub_disk_get_sizetogrub_disk_native_sectors
misc: Add parentheses aroundALIGN_UPandALIGN_DOWNarguments
whitespace: convert 8 spaces to tabs
luks2: Remove unused argument ingrub_error
luks2: Make sure all fields of output argument inluks2_parse_digest()
are written to
luks2: Add idx member to struct grub_luks2_keyslot/segment/digest
luks2: Use more intuitive object name instead of json index in user
messages
luks2: Rename json index variables to names that they are obviously
json indexes
luks2: Add string "index" to user strings using a json index.
cryptodisk: Add macroGRUB_TYPE_BITS()to replace some literals
cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals
luks2:grub_cryptodisk_t->total_sectorsis the max number of device
native sectorsQuoted 8 lines
cryptodisk: Properly handle non-512 byte sized sectors
luks2: Better error handling when setting up the cryptodisk
luks2: Error check segment.sector_size
mips: Enable__clzdi2()
misc: Addgrub_log2ullmacro for calculating log base 2 of 64-bit
integers
luks2: Usegrub_log2ullto calculatelog_sector_sizeand improve
readabilityI will get all the patches except #14 and maybe some changes after that one which does not apply due to lack of it.
Quoted 6 lines
Quoted 3 lines
Thanks! However, I have pushed some of these patches before you did review.
Daniel
I thought as much, though I didn't check. I don't really mind, thought it might make sense to send them out in any case.
Yeah, at least I am more sure I did not miss any mistakes... :-)
Daniel
Re: [PATCH v8 14/18] luks2: Better error handling when setting up the cryptodisk
In reply to Daniel KiperQuoted 197 lines · On Sat, 12 Dec 2020 16:19:18 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 149 lines · Glenn Washburn
Quoted 137 lines · On Thu, 10 Dec 2020 17:07:07 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 136 lines · Glenn Washburn
First, check to make sure that source disk has a known size. If not, print debug message and return error. There are 4 cases where
GRUB_DISK_SIZE_UNKNOWNis set (biosdisk, obdisk, ofdisk, and uboot), and in all those cases processing continues. So this is probably a bit conservative. However, 3 of the cases seem pathological, and the other, biosdisk, happens when booting from a cd. Since I doubt booting from a LUKS2 volume on a cd is a big use case, we'll error until someone complains.Do some sanity checking on data coming from the luks header. If segment.size is "dynamic", verify that the offset is not past the end of disk. Otherwise, check for errors from
grub_strtoullwhen converting segment size from string. If aGRUB_ERR_BAD_NUMBERerror was returned, then the string was not a valid parsable number, so skip the key. IfGRUB_ERR_OUT_OF_RANGEwas returned, then there was an overflow in converting to a 64-bit unsigned integer. So this could be a very large disk (perhaps large raid array). In this case, we want to continue to try to use this key, but only allow access up to the end of the source disk.Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 84++++++++++++++++++++++++++++++++++++++++--
include/grub/disk.h
| 17 +++++++++ 2 files changed, 98 insertions(+), 3
deletions(-)grub-core/disk/luks2.c +3
-0@@ -600,12 +600,26 @@ luks2_recover_key (grub_disk_t source, goto err; } +if (source->total_sectors ==GRUB_DISK_SIZE_UNKNOWN ) + { +/* FIXME: Allow use of source disk, and maybe cause errors in read. */
+grub_dprintf("luks2", "Source disk %s has an unknown
size, "grub-core/disk/luks2.c +1
-0+ "conservatively returningerror\n",
source->name);
+ ret =grub_error(GRUB_ERR_BUG, "Unknown size of luks2
source device");grub-core/disk/luks2.c +6
-0+ goto err; + } +/* Try all keyslot */ for (json_idx =0 ; json_idx < size; json_idx++) { + typeof(source->total_sectors) max_crypt_sectors = 0; + + grub_errno =GRUB_ERR_NONE ; ret = luks2_get_keyslot (&keyslot, &digest, &segment,json,
json_idx); if (ret)grub-core/disk/luks2.c +2
-0goto err; +if (grub_errno !=GRUB_ERR_NONE ) + grub_dprintf ("luks2", "Ignoring unhandled error %dfrom luks2_get_keyslot\n",
grub_errno);if (keyslot.priority == 0)grub-core/disk/luks2.c +0
-0{@@ -619,11 +633,75 @@ luks2_recover_key (grub_disk_t source, crypt->offset_sectors = grub_divmod64 (segment.offset,segment.sector_size, NULL);
crypt->log_sector_size= sizeof (unsigned int) * 8
-__builtin_clz((unsigned int)
segment.sector_size) - 1;
+ /* Set to the source disk size, which is the maximum we
allow. */grub-core/disk/luks2.c +2
-0+ max_crypt_sectors = grub_disk_convert_sector(source, +
source->total_sectors,
+crypt->log_sector_size); +grub-core/disk/luks2.c +3
-0+if (max_crypt_sectors < crypt->offset_sectors) + { + grub_dprintf ("luks2", "Segment\"%"PRIuGRUB_UINT64_T"\" has offset"
grub-core/disk/luks2.c +1
-0+ " %"PRIuGRUB_UINT64_T" whichis greater than"
grub-core/disk/luks2.c +1
-0+ " source disk size%"PRIuGRUB_UINT64_T","
grub-core/disk/luks2.c +2
-0+" skipping\n" , + segment.idx,
crypt->offset_sectors,grub-core/disk/luks2.c +4
-1+ max_crypt_sectors); +continue ; + } +if (grub_strcmp (segment.size,"dynamic" ) ==0 ) - crypt->total_sectors = (grub_disk_get_size (source) >>(
crypt->log_sector_size-source->log_sector_size))grub-core/disk/luks2.c +1
-1- - crypt->offset_sectors; + crypt->total_sectors = max_crypt_sectors -
crypt->offset_sectors; elsegrub-core/disk/luks2.c +0
-1- crypt->total_sectors = grub_strtoull (segment.size,NULL, 10) >>
crypt->log_sector_size;grub-core/disk/luks2.c +3
-0+ { + grub_errno =GRUB_ERR_NONE ; +/* Convert segment.size to sectors, rounding up to nearest sector */
grub-core/disk/luks2.c +1
-0+ crypt->total_sectors = grub_strtoull (segment.size,NULL, 10);
grub-core/disk/luks2.c +1
-0+ crypt->total_sectors =ALIGN_UP (
crypt->total_sectors,grub-core/disk/luks2.c +1
-0+ 1 <<
crypt->log_sector_size);grub-core/disk/luks2.c +7
-0+ crypt->total_sectors >>= crypt->log_sector_size; + +if (grub_errno ==GRUB_ERR_NONE ) + ; +else if (grub_errno ==GRUB_ERR_BAD_NUMBER ) + { + grub_dprintf ("luks2", "Segment\"%"PRIuGRUB_UINT64_T"\" size"
grub-core/disk/luks2.c +1
-0+ " \"%s\" is not a parsablenumber\n",
grub-core/disk/luks2.c +1
-0+ segment.idx,segment.size);
grub-core/disk/luks2.c +6
-0+continue ; + } +else if (grub_errno ==GRUB_ERR_OUT_OF_RANGE ) + { +/* +* There was an overflow in parsing segment.size, so disk must
grub-core/disk/luks2.c +3
-0+* be very large or the string is incorrect. +*/ + grub_dprintf ("luks2", "Segment\"%"PRIuGRUB_UINT64_T"\" size"
grub-core/disk/luks2.c +1
-0+ " %s overflowed 64-bitunsigned integer,"
grub-core/disk/luks2.c +1
-0+ " the end of the cryptoQuoted 8 lines
grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 683
Quoted 5 lines
device will be"
grub-core/disk/luks2.c +2
-0+" inaccessible\n" , + segment.idx,segment.size);
grub-core/disk/luks2.c +1
-0+if (crypt->total_sectors > max_crypt_sectors)I think this if is bogus. You should clamp
crypt->total_sectorswithout any checks here.Quoted 6 lines
Actually, I wouldn't call this a clamp because in the overflow case
crypt->total_sectorsalways equals 0. I just realized this, and its becausegrub_strtoullwill return 2^64-1 thus causing the followingALIGN_UPto overflow returning 0. Suffice to say that's not what we want. My original intent was what happened before theALIGN_UPcode was introduced, which wouldALIGN_DOWN.Ugh... Right. That is why I still think you should do further calculations on value returned from
grub_strtoull()ifgrub_errno==GRUB_ERR_NONEonly. I understand that thencrypt->total_sectorsdoes not contain total sectors for a while but you can rectify this a bit by putting short comment beforegrub_strtoull()call.Quoted 17 lines
Here's an example illustrating why I wanted and still think the intent of this check is reasonable. Suppose we have a disk size 2^67 bytes with 512 byte (2^9) sized sectors. It will have 2^58 sectors. Further suppose, there is a LUKS volume with size 2^65 bytes starting at the beginning and a sector size of 4096 bytes (2^12). This will cause an overflow, so
grub_strtoullwill return 2^64-1, this should have us setcrypt->total_sectorsto 2^52-1. Since we don't know how much the overflow is (1 byte or 1 terabyte), we don't know how many more sectors til the end of the LUKS encrypted area. In this casemax_crypt_sectorswill be 2^(58+9-12) => 2^57 sectors. So here we see thatcrypt->total_sectors<max_crypt_sectorsin the overflow case. If we do as you suggestcrypt->total_sectorswill be set to 2^57, and thus it will be valid to read past the end of the encrypted data (ie. block 2^56 of the 4k sector crypt will be a sector starting at byte 2^68, which is more than the 2^65 byte size volume).Quoted 8 lines
On the one hand, I like your suggestion because it allows reading all possible encrypted data, at the cost of reading, decrypting, and returning non-encrypted data (ie random garbage). While keeping the check, will prevent returning garbage at the cost of not allowing access to all encrypted sectors. I think we should keep the check and document a known limitation of 2^64 byte maximum sized LUKS volumes. And that larger sized volumes can be read only up to byte 2^64.
If think the code should look like this:
/*
* ...a comment saying whatcrypt->total_sectorscontains
* and why LUKS2 volumes larger than 2^64 does not work... */
crypt->total_sectors=grub_strtoull(segment.size, NULL, 10);
Quoted 5 lines
if (
grub_errno==GRUB_ERR_NONE){ crypt->total_sectors = ALIGN_UP (crypt->total_sectors, 1 <<
crypt->log_sector_size);crypt->total_sectors>>=crypt->log_sector_size; }
Now that ALIGN_UP is being used which we don't want in the GRUB_ERR_OUT_OF_RANGE case, I'm ok with this. Although, if we just continue in grub_strtoull error case as you suggest below, it doesn't matter. And this actually doesn't prevent ALIGN_UP from potentially overflowing crypt->total_sectors either, when segment.size is very large. Perhaps we should check for that.
Quoted 7 lines
else if (grub_errno == GRUB_ERR_BAD_NUMBER) { ... continue; }if (
grub_errno==GRUB_ERR_OUT_OF_RANGE||
Quoted 9 lines
!crypt->total_sectors ||
crypt->total_sectors > max_crypt_sectors)
{
...
continue;
/*
* Yes, I think we should not guess crypt->total_sectors
* value and always fail. It seems safer.
*/I agree this is safer, but its at the cost of using volumes over 2^64 bytes working at all. I think the question is how safe is it to allow arbitrary disk read failures in grub. Ultimately I don't really like continuing here, which is why I originally had the TODO, because it prevents otherwise accessible drives from being accessible (admittedly in an extremely rare case). Think about a 2^65 sized raid with LUKS on top and LVM on top of that. But really you should probably not be booting from that anyway. I think I'll add a TODO here, with a work around suggesting not to use such large devices to boot from.
Quoted 1 line
}
Glenn