From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) by mx.groups.io with SMTP id smtpd.web11.36551.1683647367816838272 for ; Tue, 09 May 2023 08:49:28 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="signature has expired" header.i=@gmail.com header.s=20221208 header.b=UZP2N2tn; spf=pass (domain: gmail.com, ip: 209.85.128.53, mailfrom: pedro.falcato@gmail.com) Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-3f42c865535so7958135e9.1 for ; Tue, 09 May 2023 08:49:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683647366; x=1686239366; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=QZ+wAlOFJzZxGwjYwlO6W8KfTbRZftSEb2DNjEhY+fg=; b=UZP2N2tnmoOaMyWFW1epLagi/iwnhMXC5mCWxCWsl3c2wzBkvSdvUfS4pU20Z0FxkA 16wWzk5G6YkUKQ4ySFeT5d5McfsDCCI/e2ICxkX2mWGtBvkiOWf3j1B3DVTyJMJuQqT8 28WsVQdMkNfP901lDOqix/LpNWiY5lKVYqUxz9misOCzaZHMrpSGsZdhk18aRvb0i5WN YP3c0eCujwoe/dkuF9auc0E5ZbbJMgKWqJgOaknBsUBV6jHGMVNOJnWZcyu3cpZ5jQVN 1V9WLin6C1ARjWeBnsUVKsDpG9ZSZ4EjcjVanL4F3sXtTywq6zPAijgc5ZLXXYGxTm2D iD/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683647366; x=1686239366; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QZ+wAlOFJzZxGwjYwlO6W8KfTbRZftSEb2DNjEhY+fg=; b=iMswNR45IvuYH+/Lc0FcDkko/+M1aPVTTzVMol6MptpkEAFr573owTxLIN9WVyKwlh wrd6Xb9rioWJzBQn0/v1cXtBiUPuNTu8tK+QJlClxU3/1L8ozwsiL3XMS6kqCLUxJ9dP 9oEBZzT9GWKwxp+JYIoz034c2IAEoFGmf1B2WkVQbGXVcCPfZl5RrcyGIZTSEZvhyOHT cKcyx5zxmW26cAca3mkmBB1PD2OMBJTOgN8k7cpygDkVZLnBdb2PLlD4WtfJRZlngnPq GjGtp60vCGXLOvfSURg5bCBG/5TFBM0NWt5yeP4qvKHh7BvvEhbR7U1HP0pHxyKVLcXl NyLA== X-Gm-Message-State: AC+VfDzovxuSo8SfKhD9+Ir1a4H5QUqGAEih0D52RtfQwkOne/PZhNtP fc5v2AkPdaZ/THXN+LzxAUywFSNOnWQa1CMY X-Google-Smtp-Source: ACHHUZ6D/rOo0uPTuHMMlX7jG6dm0yyqK4Lv3PygFS43/t6HwzUQjMRaex8mnpZ2x7jaTC+uW/y87A== X-Received: by 2002:a7b:c8c9:0:b0:3f0:46ca:f201 with SMTP id f9-20020a7bc8c9000000b003f046caf201mr9783447wml.1.1683647365787; Tue, 09 May 2023 08:49:25 -0700 (PDT) Return-Path: Received: from PC-PEDRO-ARCH.lan ([2001:8a0:7280:5801:9441:3dce:686c:bfc7]) by smtp.gmail.com with ESMTPSA id t25-20020a7bc3d9000000b003f42d3111b8sm1295463wmj.30.2023.05.09.08.49.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 08:49:25 -0700 (PDT) From: "Pedro Falcato" To: devel@edk2.groups.io Cc: Pedro Falcato , =?UTF-8?q?Marvin=20H=C3=A4user?= , Savva Mitrofanov Subject: [PATCH v3 1/2] Ext4Pkg: Improve extent node validation on the number of entries Date: Tue, 9 May 2023 16:49:21 +0100 Message-Id: <20230509154922.278200-1-pedro.falcato@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Improve the extent tree node validation by validating the number of 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). Previously, we did not validate the number of entries. This could be exploited for out-of-bounds reads and crashes. Cc: Marvin Häuser 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(-) 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 - 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 **/ @@ -80,13 +80,15 @@ Ext4GetInoExtentHeader ( /** Checks if an extent header is valid. @param[in] Header Pointer to the EXT4_EXTENT_HEADER structure. + @param[in] MaxEntries Maximum number of entries possible for this tree node. @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; } + // Note: We do not need to check eh_entries here, as the next branch makes sure max >= 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; } +// Results of sizeof(i_data) / sizeof(extent) - 1 = 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; Inode = File->Inode; @@ -275,12 +293,17 @@ Ext4GetExtent ( ExtHeader = Ext4GetInoExtentHeader (Inode); - if (!Ext4ExtentHeaderValid (ExtHeader)) { + if (!Ext4ExtentHeaderValid (ExtHeader, EXT4_NR_INLINE_EXTENTS)) { return EFI_VOLUME_CORRUPTED; } CurrentDepth = ExtHeader->eh_depth; + // A single node fits into a single block, so we can only have (BlockSize / sizeof(EXT4_EXTENT)) - 1 + // extents in a single node. Note the -1, because both leaf and internal node headers are 12 bytes, + // and so are individual entries. + MaxExtentsPerNode = (Partition->BlockSize / sizeof (EXT4_EXTENT)) - 1; + while (ExtHeader->eh_depth != 0) { CurrentDepth--; // While depth != 0, we're traversing the tree itself and not any leaves @@ -309,6 +332,7 @@ Ext4GetExtent ( } // Read the leaf block onto the previously-allocated buffer. + Status = Ext4ReadBlocks (Partition, Buffer, 1, BlockNumber); if (EFI_ERROR (Status)) { FreePool (Buffer); @@ -317,7 +341,7 @@ Ext4GetExtent ( ExtHeader = Buffer; - if (!Ext4ExtentHeaderValid (ExtHeader)) { + if (!Ext4ExtentHeaderValid (ExtHeader, MaxExtentsPerNode)) { FreePool (Buffer); return EFI_VOLUME_CORRUPTED; } -- 2.40.1