Skip to contentSkip to conversation

grub-core/disk/cryptodisk.c: Fix unintentional integer overflow

PATCH Superseded Alec Brown 2 participants 2 messages Oct 13, 2022 – Oct 14, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Alec Brown

author
#0 lore raw

PATCH 1/1 grub-core/disk/cryptodisk.c: Fix unintentional integer overflow

In the function grub_cryptodisk_endecrypt(), a for loop is incrementing the variable i by (1U << log_sector_size). The variable i is of type grub_size_t which is a 64-bit unsigned integer on x86_64 architecture. On the other hand, 1U is a 32-bit unsigned integer. By performing a left shift between these two values of different types, there may be potential for an integer overflow. To avoid this, we replace 1U with (grub_size_t) 1.

Fixes CID 307788
---
 grub-core/disk/cryptodisk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/disk/cryptodisk.c +1 -1
index 9f5dc7acb..cdcb882ca 100644
@@ -239,7 +239,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 << log_sector_size))
+  for (i = 0; i < len; i += ((grub_size_t) 1 << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
Signature · 1 line
2.27.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Alec,

This looks good, thanks for fixing it.

Quoted 10 lines · Alec Brown

In the function grub_cryptodisk_endecrypt(), a for loop is incrementing the variable i by (1U << log_sector_size). The variable i is of type grub_size_t which is a 64-bit unsigned integer on x86_64 architecture. On the other hand, 1U is a 32-bit unsigned integer. By performing a left shift between these two values of different types, there may be potential for an integer overflow. To avoid this, we replace 1U with (grub_size_t) 1.

Fixes CID 307788

Thanks,

Darren.

Quoted 19 lines
---
 grub-core/disk/cryptodisk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/disk/cryptodisk.c +1 -1
index 9f5dc7acb..cdcb882ca 100644
@@ -239,7 +239,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 << log_sector_size))
+  for (i = 0; i < len; i += ((grub_size_t) 1 << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
Signature · 1 line
2.27.0