0022-kern-partition-Limit-recursion-in-part_iterate.patch 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. From 3f1c5f55e7ef7b872c3ae59c0c41f1e07508a943 Mon Sep 17 00:00:00 2001
  2. From: B Horn <b@horn.uk>
  3. Date: Sat, 16 Nov 2024 21:24:19 +0000
  4. Subject: [PATCH] kern/partition: Limit recursion in part_iterate()
  5. The part_iterate() is used by grub_partition_iterate() as a callback in
  6. the partition iterate functions. However, part_iterate() may also call
  7. the partition iterate functions which may lead to recursion. Fix potential
  8. issue by limiting the recursion depth.
  9. Signed-off-by: B Horn <b@horn.uk>
  10. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  11. Upstream: 8a7103fddfd6664f41081f3bb88eebbf2871da2a
  12. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  13. ---
  14. grub-core/kern/partition.c | 10 +++++++++-
  15. 1 file changed, 9 insertions(+), 1 deletion(-)
  16. diff --git a/grub-core/kern/partition.c b/grub-core/kern/partition.c
  17. index edad9f9e4..704512a20 100644
  18. --- a/grub-core/kern/partition.c
  19. +++ b/grub-core/kern/partition.c
  20. @@ -28,6 +28,9 @@
  21. grub_partition_map_t grub_partition_map_list;
  22. +#define MAX_RECURSION_DEPTH 32
  23. +static unsigned int recursion_depth = 0;
  24. +
  25. /*
  26. * Checks that disk->partition contains part. This function assumes that the
  27. * start of part is relative to the start of disk->partition. Returns 1 if
  28. @@ -208,7 +211,12 @@ part_iterate (grub_disk_t dsk, const grub_partition_t partition, void *data)
  29. FOR_PARTITION_MAPS(partmap)
  30. {
  31. grub_err_t err;
  32. - err = partmap->iterate (dsk, part_iterate, ctx);
  33. + recursion_depth++;
  34. + if (recursion_depth <= MAX_RECURSION_DEPTH)
  35. + err = partmap->iterate (dsk, part_iterate, ctx);
  36. + else
  37. + err = grub_error (GRUB_ERR_RECURSION_DEPTH, "maximum recursion depth exceeded");
  38. + recursion_depth--;
  39. if (err)
  40. grub_errno = GRUB_ERR_NONE;
  41. if (ctx->ret)
  42. --
  43. 2.50.1