From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) by mx.groups.io with SMTP id smtpd.web11.5564.1670602283583145376 for ; Fri, 09 Dec 2022 08:11:23 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20210112 header.b=jNxu1CZp; spf=pass (domain: gmail.com, ip: 209.85.167.49, mailfrom: savvamtr@gmail.com) Received: by mail-lf1-f49.google.com with SMTP id p36so7734603lfa.12 for ; Fri, 09 Dec 2022 08:11:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=k9g9FNMFZalpxZG2+c8Ek1vVb3gGJxNhcXnojNAVmzM=; b=jNxu1CZpwAP9i+gN1QcVWdAPAptSPYSokXKL0+gviEmb+wZd8BPr9MosrEDOsqhr7d HRtW9X02NQJTUcizL1J3bJYRJJqFpVmniiy08BrEfVvbS3qOGXYECmsM5itcqC/uRDWD PZoCwIWe1eeMXtEBl43p46vWS207T0oVWAa9yupEfWT1sdic2EMT9PT2gYfMGJw2NybP sQBrAy3hKXlw0RK0Z0Ws4zRc3215rQHTKU8PBSuy0CcrzUcMEFVuKpskVsNPvmHnPoG2 unz8XKqfCGjnEp7HofAm/Yi9kODvsWLLhQNUljn/jA4F7FOMCBFB0zKjd+E0feIu5AD4 cN2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=k9g9FNMFZalpxZG2+c8Ek1vVb3gGJxNhcXnojNAVmzM=; b=ZN3d7sT5YtHxFNXMYOz/HAiySED+Ri+k70y2bGZ2t6sxy94gPZyp3CPWGBBX31DcTc dkkRxDA1HcBtmAoGOhc68VH/Atn1l5SjlgiTBTYsXE+y/Z3rFSfet8o+koCwm92G6jnO wwbhTyHW86GmoER9UZvAs6IXG5wTI96NeZZMCbXjkMCxGda7vsMOREd5IcSMTxXYUpWi B1obV5TU7svO2NYkvxssHXkflMC7Fr7cYs5dEzhS1yvBoS06FFvhyDUyJ41ogiCVGQJ2 M3Jkye+55Z3mGUzyxUyD3Jz1TTXCwazTLZNlzcaSV76Wjfr9j+0WjmF+B0PqjDKfFIaB Swrg== X-Gm-Message-State: ANoB5pmAdDE+hdXFXdkVlB2SMoBp5Fvu2UURgs+qjx9ozs669HJTdUY2 JE7EJLHXNMW5TrF+gBIHUcIELsr7Y0tc+uPu X-Google-Smtp-Source: AA0mqf6srGq7SIHHTAWSsg+9cU7/DC+mBvpVHMA+PO8oWpkjqxf6W4HktIhq4uqVW1+V6A4UfN6m7A== X-Received: by 2002:a05:6512:15a6:b0:4b4:b5bf:3ce6 with SMTP id bp38-20020a05651215a600b004b4b5bf3ce6mr3734933lfb.38.1670602281553; Fri, 09 Dec 2022 08:11:21 -0800 (PST) Return-Path: Received: from localhost.localdomain ([109.194.121.139]) by smtp.gmail.com with ESMTPSA id v5-20020a05651203a500b00492c663bba2sm318430lfp.124.2022.12.09.08.11.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Dec 2022 08:11:21 -0800 (PST) From: "Savva Mitrofanov" To: devel@edk2.groups.io Cc: =?UTF-8?q?Marvin=20H=C3=A4user?= , Pedro Falcato , Vitaly Cheptsov Subject: [edk2-platforms][PATCH v1 07/12] Ext4Pkg: Add inode number validity check Date: Fri, 9 Dec 2022 22:10:59 +0600 Message-Id: <20221209161104.70220-8-savvamtr@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221209161104.70220-1-savvamtr@gmail.com> References: <20221209161104.70220-1-savvamtr@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable We need to validate inode number to prevent possible null-pointer dereference of directory parent in Ext4OpenDirent. Also checks that inode number valid across opened partition before we read it in Ext4ReadInode. Cc: Marvin H=C3=A4user Cc: Pedro Falcato Cc: Vitaly Cheptsov Signed-off-by: Savva Mitrofanov --- Features/Ext4Pkg/Ext4Dxe/Ext4Disk.h | 15 +++++++++--- Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h | 25 ++++++++++++++++++++ Features/Ext4Pkg/Ext4Dxe/BlockGroup.c | 5 ++++ Features/Ext4Pkg/Ext4Dxe/Directory.c | 10 ++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/Features/Ext4Pkg/Ext4Dxe/Ext4Disk.h b/Features/Ext4Pkg/Ext4Dxe= /Ext4Disk.h index 1285644dcb25..6b56ce6813fc 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Ext4Disk.h +++ b/Features/Ext4Pkg/Ext4Dxe/Ext4Disk.h @@ -397,7 +397,7 @@ typedef struct _Ext4Inode { UINT32 i_projid;=0D } EXT4_INODE;=0D =0D -#define EXT4_NAME_MAX 255=0D +#define EXT4_NAME_MAX 255=0D =0D typedef struct {=0D UINT32 inode;=0D @@ -469,8 +469,17 @@ typedef UINT64 EXT4_BLOCK_NR; typedef UINT32 EXT2_BLOCK_NR;=0D typedef UINT32 EXT4_INO_NR;=0D =0D -// 2 is always the root inode number in ext4=0D -#define EXT4_ROOT_INODE_NR 2=0D +/* Special inode numbers */=0D +#define EXT4_ROOT_INODE_NR 2=0D +#define EXT4_USR_QUOTA_INODE_NR 3=0D +#define EXT4_GRP_QUOTA_INODE_NR 4=0D +#define EXT4_BOOT_LOADER_INODE_NR 5=0D +#define EXT4_UNDEL_DIR_INODE_NR 6=0D +#define EXT4_RESIZE_INODE_NR 7=0D +#define EXT4_JOURNAL_INODE_NR 8=0D +=0D +/* First non-reserved inode for old ext4 filesystems */=0D +#define EXT4_GOOD_OLD_FIRST_INODE_NR 11=0D =0D #define EXT4_BLOCK_FILE_HOLE 0=0D =0D diff --git a/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h b/Features/Ext4Pkg/Ext4Dxe/= Ext4Dxe.h index 1dcb644e3b35..d135892633af 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h +++ b/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h @@ -287,6 +287,31 @@ Ext4GetBlockGroupDesc ( IN UINT32 BlockGroup=0D );=0D =0D +/**=0D + Retrieves the first usable non-reserved inode number from the superbloc= k=0D + of the opened partition.=0D +=0D + @param[in] Partition Pointer to the opened ext4 partition.=0D +=0D + @return The first usable inode number (non-reserved).=0D +**/=0D +#define EXT4_FIRST_INODE_NR(Partition) = \=0D + ((Partition->SuperBlock.s_rev_level =3D=3D EXT4_GOOD_OLD_REV) ? = \=0D + EXT4_GOOD_OLD_FIRST_INODE_NR : = \=0D + Partition->SuperBlock.s_first_ino)=0D +=0D +/**=0D + Checks inode number validity across superblock of the opened partition.= =0D +=0D + @param[in] Partition Pointer to the opened ext4 partition.=0D +=0D + @return TRUE if inode number is valid.=0D +**/=0D +#define EXT4_IS_VALID_INODE_NR(Partition, InodeNum) = \=0D + (InodeNum =3D=3D EXT4_ROOT_INODE_NR || = \=0D + (InodeNum >=3D EXT4_FIRST_INODE_NR(Partition) && = \=0D + InodeNum <=3D Partition->SuperBlock.s_inodes_count))=0D +=0D /**=0D Reads an inode from disk.=0D =0D diff --git a/Features/Ext4Pkg/Ext4Dxe/BlockGroup.c b/Features/Ext4Pkg/Ext4D= xe/BlockGroup.c index cba96cd95afc..f34cdc5dbad7 100644 --- a/Features/Ext4Pkg/Ext4Dxe/BlockGroup.c +++ b/Features/Ext4Pkg/Ext4Dxe/BlockGroup.c @@ -50,6 +50,11 @@ Ext4ReadInode ( EXT4_BLOCK_NR InodeTableStart;=0D EFI_STATUS Status;=0D =0D + if (!EXT4_IS_VALID_INODE_NR (Partition, InodeNum)) {=0D + DEBUG ((DEBUG_ERROR, "[ext4] Error reading inode: inode number %lu isn= 't valid\n", InodeNum));=0D + return EFI_VOLUME_CORRUPTED;=0D + }=0D +=0D BlockGroupNumber =3D (UINT32)DivU64x64Remainder (=0D InodeNum - 1,=0D Partition->SuperBlock.s_inodes_per_group,=0D diff --git a/Features/Ext4Pkg/Ext4Dxe/Directory.c b/Features/Ext4Pkg/Ext4Dx= e/Directory.c index ffc0e8043076..ff476c8641e8 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Directory.c +++ b/Features/Ext4Pkg/Ext4Dxe/Directory.c @@ -163,6 +163,10 @@ Ext4RetrieveDirent ( if (Entry->inode =3D=3D 0) {=0D BlockOffset +=3D Entry->rec_len;=0D continue;=0D + } else if (!EXT4_IS_VALID_INODE_NR (Partition, Entry->inode)) {=0D + DEBUG ((DEBUG_ERROR, "[ext4] Ext4RetrieveDirent directory entry in= ode number %u isn't valid\n", Entry->inode));=0D + Status =3D EFI_VOLUME_CORRUPTED;=0D + goto Out;=0D }=0D =0D Status =3D Ext4GetUcs2DirentName (Entry, DirentUcs2Name);=0D @@ -498,6 +502,12 @@ Ext4ReadDir ( // When inode =3D 0, it's unused.=0D ShouldSkip =3D Entry.inode =3D=3D 0 || IsDotOrDotDot;=0D =0D + if ((Entry.inode !=3D 0) && !EXT4_IS_VALID_INODE_NR (Partition, Entry.= inode)) {=0D + DEBUG ((DEBUG_ERROR, "[ext4] Ext4ReadDir directory entry inode numbe= r %u isn't valid\n", Entry.inode));=0D + Status =3D EFI_VOLUME_CORRUPTED;=0D + goto Out;=0D + }=0D +=0D if (ShouldSkip) {=0D Offset +=3D Entry.rec_len;=0D continue;=0D --=20 2.38.1