From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout01.posteo.de (mout01.posteo.de [185.67.36.65]) by mx.groups.io with SMTP id smtpd.web10.37053.1683648456087457863 for ; Tue, 09 May 2023 09:07:36 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@posteo.de header.s=2017 header.b=sBDPXX1o; spf=pass (domain: posteo.de, ip: 185.67.36.65, mailfrom: mhaeuser@posteo.de) Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id ECEDE24002D for ; Tue, 9 May 2023 18:07:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1683648453; bh=bIGZq7DOYzOkjGiG0maMejdqZnAeMVtzurCcvSZs1n8=; h=From:Subject:Date:Cc:To:From; b=sBDPXX1oXhztst4wxcpPhz2sD5xR5UYvP6F/dUtbru0AsKZ8236EtOSUGuotGLuZz QiQxulFBy1RrAAJ88PUaYYRlB/mLasNpFyQ/eGmE1eYMn34DBoszHeqjyWWeXcxMrE 7GMjzX2eXgAI4DV/6tAWuRAyBJkkFx3DiUXIwJh6ja6bsnNlom4Sd5lZKyEkA/ACst 2LsvI+KxQpLuerKU/8eKC0xV45rOGj3rtzYvNOfafSncjFFZHwb+oTsanMMvh9iGel MXOW3QccDPfSOJvt3t/OnGRIB+pbd4PDd009NSvHYZqdWHuWNYwLPlkXi0KwZYMxvY Z1ZFqefzY5tYw== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4QG32r02jSz6tmv; Tue, 9 May 2023 18:07:31 +0200 (CEST) From: =?UTF-8?B?TWFydmluIEjDpHVzZXI=?= Mime-Version: 1.0 (1.0) Subject: Re: [PATCH v3 1/2] Ext4Pkg: Improve extent node validation on the number of entries Date: Tue, 9 May 2023 16:07:31 +0000 Message-Id: <2EC24DA1-FA6A-4FAC-8750-009E7689D5EC@posteo.de> References: <20230509154922.278200-1-pedro.falcato@gmail.com> Cc: devel@edk2.groups.io, Savva Mitrofanov In-Reply-To: <20230509154922.278200-1-pedro.falcato@gmail.com> To: Pedro Falcato Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Reviewed-by: Marvin H=C3=A4user > On 9. May 2023, at 17:49, Pedro Falcato wrote: >=20 > =EF=BB=BFImprove the extent tree node validation by validating the number o= f > entries the node advertises against the theoretical max (derived from > the size of on-disk structs and the block size (or i_data, if inline > extents). >=20 > Previously, we did not validate the number of entries. This could be > exploited for out-of-bounds reads and crashes. >=20 > Cc: Marvin H=C3=A4user > Fixes: d9ceedca6c8f ("Ext4Pkg: Add Ext4Dxe driver.") > Reported-by: Savva Mitrofanov > Signed-off-by: Pedro Falcato > --- > Features/Ext4Pkg/Ext4Dxe/Extents.c | 32 ++++++++++++++++++++++++++---- > 1 file changed, 28 insertions(+), 4 deletions(-) >=20 > diff --git a/Features/Ext4Pkg/Ext4Dxe/Extents.c b/Features/Ext4Pkg/Ext4Dxe= /Extents.c > index 9e4364e50d99..2b82417c9e10 100644 > --- a/Features/Ext4Pkg/Ext4Dxe/Extents.c > +++ b/Features/Ext4Pkg/Ext4Dxe/Extents.c > @@ -1,7 +1,7 @@ > /** @file > Extent related routines >=20 > - Copyright (c) 2021 - 2022 Pedro Falcato All rights reserved. > + Copyright (c) 2021 - 2023 Pedro Falcato All rights reserved. > SPDX-License-Identifier: BSD-2-Clause-Patent > **/ >=20 > @@ -80,13 +80,15 @@ Ext4GetInoExtentHeader ( > /** > Checks if an extent header is valid. > @param[in] Header Pointer to the EXT4_EXTENT_HEADER struct= ure. > + @param[in] MaxEntries Maximum number of entries possible for t= his tree node. >=20 > @return TRUE if valid, FALSE if not. > **/ > STATIC > BOOLEAN > Ext4ExtentHeaderValid ( > - IN CONST EXT4_EXTENT_HEADER *Header > + IN CONST EXT4_EXTENT_HEADER *Header, > + IN UINT16 MaxEntries > ) > { > if (Header->eh_depth > EXT4_EXTENT_TREE_MAX_DEPTH) { > @@ -99,6 +101,18 @@ Ext4ExtentHeaderValid ( > return FALSE; > } >=20 > + // Note: We do not need to check eh_entries here, as the next branch ma= kes sure max >=3D entries > + if (Header->eh_max > MaxEntries) { > + DEBUG (( > + DEBUG_ERROR, > + "[ext4] Invalid extent header max entries (%u eh_max, " > + "theoretical max is %u) (larger than permitted)\n", > + Header->eh_max, > + MaxEntries > + )); > + return FALSE; > + } > + > if (Header->eh_max < Header->eh_entries) { > DEBUG (( > DEBUG_ERROR, > @@ -212,6 +226,9 @@ Ext4ExtentIdxLeafBlock ( > return LShiftU64 (Index->ei_leaf_hi, 32) | Index->ei_leaf_lo; > } >=20 > +// Results of sizeof(i_data) / sizeof(extent) - 1 =3D 4 > +#define EXT4_NR_INLINE_EXTENTS 4 > + > /** > Retrieves an extent from an EXT4 inode. > @param[in] Partition Pointer to the opened EXT4 partition. > @@ -237,6 +254,7 @@ Ext4GetExtent ( > EXT4_EXTENT_HEADER *ExtHeader; > EXT4_EXTENT_INDEX *Index; > EFI_STATUS Status; > + UINT32 MaxExtentsPerNode; > EXT4_BLOCK_NR BlockNumber; >=20 > Inode =3D File->Inode; > @@ -275,12 +293,17 @@ Ext4GetExtent ( >=20 > ExtHeader =3D Ext4GetInoExtentHeader (Inode); >=20 > - if (!Ext4ExtentHeaderValid (ExtHeader)) { > + if (!Ext4ExtentHeaderValid (ExtHeader, EXT4_NR_INLINE_EXTENTS)) { > return EFI_VOLUME_CORRUPTED; > } >=20 > CurrentDepth =3D ExtHeader->eh_depth; >=20 > + // A single node fits into a single block, so we can only have (BlockSi= ze / sizeof(EXT4_EXTENT)) - 1 > + // extents in a single node. Note the -1, because both leaf and interna= l node headers are 12 bytes, > + // and so are individual entries. > + MaxExtentsPerNode =3D (Partition->BlockSize / sizeof (EXT4_EXTENT)) - 1= ; > + > while (ExtHeader->eh_depth !=3D 0) { > CurrentDepth--; > // While depth !=3D 0, we're traversing the tree itself and not any le= aves > @@ -309,6 +332,7 @@ Ext4GetExtent ( > } >=20 > // Read the leaf block onto the previously-allocated buffer. > + > Status =3D Ext4ReadBlocks (Partition, Buffer, 1, BlockNumber); > if (EFI_ERROR (Status)) { > FreePool (Buffer); > @@ -317,7 +341,7 @@ Ext4GetExtent ( >=20 > ExtHeader =3D Buffer; >=20 > - if (!Ext4ExtentHeaderValid (ExtHeader)) { > + if (!Ext4ExtentHeaderValid (ExtHeader, MaxExtentsPerNode)) { > FreePool (Buffer); > return EFI_VOLUME_CORRUPTED; > } > --=20 > 2.40.1 >=20