0066-kern-misc-Add-sanity-check-after-grub_strtoul-call.patch 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. From 009d1b7189c6eba0b8d284c94a9ef7dc9db351d1 Mon Sep 17 00:00:00 2001
  2. From: Lidong Chen <lidong.chen@oracle.com>
  3. Date: Thu, 6 Feb 2025 18:16:57 +0000
  4. Subject: [PATCH] kern/misc: Add sanity check after grub_strtoul() call
  5. When the format string, fmt0, includes a positional argument
  6. grub_strtoul() or grub_strtoull() is called to extract the argument
  7. position. However, the returned argument position isn't fully validated.
  8. If the format is something like "%0$x" then these functions return
  9. 0 which leads to an underflow in the calculation of the args index, curn.
  10. The fix is to add a check to ensure the extracted argument position is
  11. greater than 0 before computing curn. Additionally, replace one
  12. grub_strtoull() with grub_strtoul() and change curn type to make code
  13. more correct.
  14. Fixes: CID 473841
  15. Signed-off-by: Lidong Chen <lidong.chen@oracle.com>
  16. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  17. Upstream: a8d6b06331a75d75b46f3dd6cc6fcd40dcf604b7
  18. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  19. ---
  20. grub-core/kern/misc.c | 9 +++++++--
  21. 1 file changed, 7 insertions(+), 2 deletions(-)
  22. diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
  23. index 7cee5d75c..2b7922393 100644
  24. --- a/grub-core/kern/misc.c
  25. +++ b/grub-core/kern/misc.c
  26. @@ -830,7 +830,7 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
  27. while ((c = *fmt++) != 0)
  28. {
  29. int longfmt = 0;
  30. - grub_size_t curn;
  31. + unsigned long curn;
  32. const char *p;
  33. if (c != '%')
  34. @@ -848,7 +848,10 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
  35. if (*fmt == '$')
  36. {
  37. - curn = grub_strtoull (p, 0, 10) - 1;
  38. + curn = grub_strtoul (p, 0, 10);
  39. + if (curn == 0)
  40. + continue;
  41. + curn--;
  42. fmt++;
  43. }
  44. @@ -1034,6 +1037,8 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0,
  45. if (*fmt == '$')
  46. {
  47. + if (format1 == 0)
  48. + continue;
  49. curn = format1 - 1;
  50. fmt++;
  51. format1 = 0;
  52. --
  53. 2.50.1