CVE-2024-56738.patch 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. From 4cef2fc7308b2132317ad166939994f098b41561 Mon Sep 17 00:00:00 2001
  2. From: Ross Burton <ross.burton@arm.com>
  3. Date: Tue, 9 Sep 2025 14:23:14 +0100
  4. Subject: [PATCH] CVE-2024-56738
  5. Backport an algorithmic change to grub_crypto_memcmp() so that it completes in
  6. constant time and thus isn't susceptible to side-channel attacks.
  7. This is a partial backport of grub 0739d24cd
  8. ("libgcrypt: Adjust import script, definitions and API users for libgcrypt 1.11")
  9. CVE: CVE-2024-56738
  10. Upstream-Status: Backport [0739d24cd]
  11. Signed-off-by: Ross Burton <ross.burton@arm.com>
  12. ---
  13. grub-core/lib/crypto.c | 23 ++++++++++++++++-------
  14. include/grub/crypto.h | 2 +-
  15. 2 files changed, 17 insertions(+), 8 deletions(-)
  16. diff --git a/grub-core/lib/crypto.c b/grub-core/lib/crypto.c
  17. index 396f76410..19db7870a 100644
  18. --- a/grub-core/lib/crypto.c
  19. +++ b/grub-core/lib/crypto.c
  20. @@ -433,19 +433,28 @@ grub_crypto_gcry_error (gcry_err_code_t in)
  21. return GRUB_ACCESS_DENIED;
  22. }
  23. +/*
  24. + * Compare byte arrays of length LEN, return 1 if it's not same,
  25. + * 0, otherwise.
  26. + */
  27. int
  28. -grub_crypto_memcmp (const void *a, const void *b, grub_size_t n)
  29. +grub_crypto_memcmp (const void *b1, const void *b2, grub_size_t len)
  30. {
  31. - register grub_size_t counter = 0;
  32. - const grub_uint8_t *pa, *pb;
  33. + const grub_uint8_t *a = b1;
  34. + const grub_uint8_t *b = b2;
  35. + int ab, ba;
  36. + grub_size_t i;
  37. - for (pa = a, pb = b; n; pa++, pb++, n--)
  38. + /* Constant-time compare. */
  39. + for (i = 0, ab = 0, ba = 0; i < len; i++)
  40. {
  41. - if (*pa != *pb)
  42. - counter++;
  43. + /* If a[i] != b[i], either ab or ba will be negative. */
  44. + ab |= a[i] - b[i];
  45. + ba |= b[i] - a[i];
  46. }
  47. - return !!counter;
  48. + /* 'ab | ba' is negative when buffers are not equal, extract sign bit. */
  49. + return ((unsigned int)(ab | ba) >> (sizeof(unsigned int) * 8 - 1)) & 1;
  50. }
  51. #ifndef GRUB_UTIL
  52. diff --git a/include/grub/crypto.h b/include/grub/crypto.h
  53. index 31c87c302..20ad4c5f7 100644
  54. --- a/include/grub/crypto.h
  55. +++ b/include/grub/crypto.h
  56. @@ -393,7 +393,7 @@ grub_crypto_pbkdf2 (const struct gcry_md_spec *md,
  57. grub_uint8_t *DK, grub_size_t dkLen);
  58. int
  59. -grub_crypto_memcmp (const void *a, const void *b, grub_size_t n);
  60. +grub_crypto_memcmp (const void *b1, const void *b2, grub_size_t len);
  61. int
  62. grub_password_get (char buf[], unsigned buf_size);
  63. --
  64. 2.43.0