2
1

0069-fs-ext2-Rework-out-of-bounds-read-for-inline-and-ext.patch 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. From 156ee67f3e76aee99d6e40e5e029f56d681cb80a Mon Sep 17 00:00:00 2001
  2. From: Michael Chang <mchang@suse.com>
  3. Date: Fri, 21 Feb 2025 09:06:12 +0800
  4. Subject: [PATCH] fs/ext2: Rework out-of-bounds read for inline and external
  5. extents
  6. Previously, the number of extent entries was not properly capped based
  7. on the actual available space. This could lead to insufficient reads for
  8. external extents, since the computation was based solely on the inline
  9. extent layout.
  10. In this patch, when processing the extent header, we determine whether
  11. the header is stored inline (i.e., at inode->blocks.dir_blocks) or in an
  12. external extent block. We then clamp the number of entries accordingly
  13. (using max_inline_ext for inline extents and max_external_ext for
  14. external extent blocks).
  15. This change ensures that only the valid number of extent entries is
  16. processed, preventing out-of-bound reads and potential filesystem
  17. corruption.
  18. Fixes: 7e2f750f0a (fs/ext2: Fix out-of-bounds read for inline extents)
  19. Signed-off-by: Michael Chang <mchang@suse.com>
  20. Upstream: 348cd416a3574348f4255bf2b04ec95938990997
  21. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  22. ---
  23. grub-core/fs/ext2.c | 17 +++++++++++++++--
  24. 1 file changed, 15 insertions(+), 2 deletions(-)
  25. diff --git a/grub-core/fs/ext2.c b/grub-core/fs/ext2.c
  26. index c3058f7e7..a38c86c4f 100644
  27. --- a/grub-core/fs/ext2.c
  28. +++ b/grub-core/fs/ext2.c
  29. @@ -496,7 +496,10 @@ grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
  30. int i;
  31. grub_disk_addr_t ret;
  32. grub_uint16_t nent;
  33. + /* maximum number of extent entries in the inode's inline extent area */
  34. const grub_uint16_t max_inline_ext = sizeof (inode->blocks) / sizeof (*ext) - 1; /* Minus 1 extent header. */
  35. + /* maximum number of extent entries in the external extent block */
  36. + const grub_uint16_t max_external_ext = EXT2_BLOCK_SIZE(data) / sizeof (*ext) - 1; /* Minus 1 extent header. */
  37. if (grub_ext4_find_leaf (data, (struct grub_ext4_extent_header *) inode->blocks.dir_blocks,
  38. fileblock, &leaf) != GRUB_ERR_NONE)
  39. @@ -513,8 +516,18 @@ grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
  40. nent = grub_le_to_cpu16 (leaf->entries);
  41. - if (leaf->depth == 0)
  42. - nent = grub_min (nent, max_inline_ext);
  43. + /*
  44. + * Determine the effective number of extent entries (nent) to process:
  45. + * If the extent header (leaf) is stored inline in the inode's block
  46. + * area (i.e. at inode->blocks.dir_blocks), then only max_inline_ext
  47. + * entries can fit.
  48. + * Otherwise, if the header was read from an external extent block, use
  49. + * the larger limit, max_external_ext, based on the full block size.
  50. + */
  51. + if (leaf == (struct grub_ext4_extent_header *) inode->blocks.dir_blocks)
  52. + nent = grub_min (nent, max_inline_ext);
  53. + else
  54. + nent = grub_min (nent, max_external_ext);
  55. for (i = 0; i < nent; i++)
  56. {
  57. --
  58. 2.50.1