Skip to content

Commit 49d027e

Browse files
Michael Chang via Grub-devellsandov1
authored andcommitted
fs/ext2: Rework out-of-bounds read for inline and external extents
Previously, the number of extent entries was not properly capped based on the actual available space. This could lead to insufficient reads for external extents, since the computation was based solely on the inline extent layout. In this patch, when processing the extent header, we determine whether the header is stored inline (i.e., at inode->blocks.dir_blocks) or in an external extent block. We then clamp the number of entries accordingly (using max_inline_ext for inline extents and max_external_ext for external extent blocks). This change ensures that only the valid number of extent entries is processed, preventing out-of-bound reads and potential filesystem corruption. Fixes: 7e2f750 (fs/ext2: Fix out-of-bounds read for inline extents) Signed-off-by: Michael Chang <mchang@suse.com> Tested-by: Christian Hesse <mail@eworm.de> Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
1 parent 40ba551 commit 49d027e

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

grub-core/fs/ext2.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,10 @@ grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
482482
int i;
483483
grub_disk_addr_t ret;
484484
grub_uint16_t nent;
485+
/* maximum number of extent entries in the inode's inline extent area */
485486
const grub_uint16_t max_inline_ext = sizeof (inode->blocks) / sizeof (*ext) - 1; /* Minus 1 extent header. */
487+
/* maximum number of extent entries in the external extent block */
488+
const grub_uint16_t max_external_ext = EXT2_BLOCK_SIZE(data) / sizeof (*ext) - 1; /* Minus 1 extent header. */
486489

487490
leaf = grub_ext4_find_leaf (data, (struct grub_ext4_extent_header *) inode->blocks.dir_blocks, fileblock);
488491
if (! leaf)
@@ -495,8 +498,18 @@ grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
495498

496499
nent = grub_le_to_cpu16 (leaf->entries);
497500

498-
if (leaf->depth == 0)
501+
/*
502+
* Determine the effective number of extent entries (nent) to process:
503+
* If the extent header (leaf) is stored inline in the inode’s block
504+
* area (i.e. at inode->blocks.dir_blocks), then only max_inline_ext
505+
* entries can fit.
506+
* Otherwise, if the header was read from an external extent block, use
507+
* the larger limit, max_external_ext, based on the full block size.
508+
*/
509+
if (leaf == (struct grub_ext4_extent_header *) inode->blocks.dir_blocks)
499510
nent = grub_min (nent, max_inline_ext);
511+
else
512+
nent = grub_min (nent, max_external_ext);
500513

501514
for (i = 0; i < nent; i++)
502515
{

0 commit comments

Comments
 (0)