From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) by mx.groups.io with SMTP id smtpd.web10.25928.1683616169245177400 for ; Tue, 09 May 2023 00:09:29 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@posteo.de header.s=2017 header.b=dwkR9SVm; spf=pass (domain: posteo.de, ip: 185.67.36.66, mailfrom: mhaeuser@posteo.de) Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id A756A240291 for ; Tue, 9 May 2023 09:09:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1683616167; bh=0S9CElsKrm5kL/nBXWevDldVTtlEKjFZ9cI00SBdAHc=; h=From:Subject:Date:Cc:To:From; b=dwkR9SVmZaAdX3UMXSuDp+pBMok+7Vu0AcwYsYbpm4dQGooNdYhiuIGhaIr6c9bFA 2wsNMdc16xwlZrFHT+ENdQRKTqReKDwxLvigAKUlN9ockp5op+l2r7ns1DljL/UNj+ cyjQL9a+LisleDsYKIhItuC0QRRVhvlSpT3FBosu2LU0Y+zX7CpSq7Cz4OSQbX0EBj kepUNNio5IKvDvbMWxq0OApFQn4gqMnF2gUxGl8t9wGk1zbPFwc5IwzG60WjOzvXgv 8S1kd/aueaWdsSL9dPjA8yj1hpyUVU90hFQnOGZ3/t8+KOphpG51d04SFoVtOEPQ70 /ThH/BgdPTQBQ== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4QFq5z16qTz9rxP; Tue, 9 May 2023 09:09:27 +0200 (CEST) From: =?UTF-8?B?TWFydmluIEjDpHVzZXI=?= Mime-Version: 1.0 (1.0) Subject: Re: [PATCH v2 1/2] Ext4Pkg: Improve extent node validation on the number of entries Date: Tue, 9 May 2023 07:09:26 +0000 Message-Id: <2055B7B1-954C-47D1-BE31-1DD32D21919D@posteo.de> References: <20230509005458.245291-1-pedro.falcato@gmail.com> Cc: devel@edk2.groups.io, Savva Mitrofanov In-Reply-To: <20230509005458.245291-1-pedro.falcato@gmail.com> To: Pedro Falcato Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Sorry, saw V2 too late=E2=80=A6 > On 9. May 2023, at 02:55, 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 > --- > v2: > Accidentally based my previous patch on the wrong version of Extents.c, wh= ich accidentally undid some previous changes. >=20 > 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..2d86a7abdedb 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 > + if ((Header->eh_max > MaxEntries) || (Header->eh_entries > MaxEntries))= { My comment on V1 is still valid, this is implicit for eh_entries via the che= ck below. The other comment does not apply to V2. Best regards, Marvin > + DEBUG (( > + DEBUG_ERROR, > + "[ext4] Invalid extent header entries (extent header: %u max," > + " %u entries, theoretical max: %u) (larger than permitted)\n", > + Header->eh_max, > + Header->eh_entries, > + 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