From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) by mx.groups.io with SMTP id smtpd.web11.44999.1670856421448535571 for ; Mon, 12 Dec 2022 06:47:01 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20210112 header.b=YrzEVpgV; spf=pass (domain: gmail.com, ip: 209.85.167.46, mailfrom: savvamtr@gmail.com) Received: by mail-lf1-f46.google.com with SMTP id y25so18965157lfa.9 for ; Mon, 12 Dec 2022 06:47:01 -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=wp/ki8CoI7EpYvUbz7xKCBTg9ynrZMRYsRbyidj76rQ=; b=YrzEVpgVr5ecJcrJpn9ydbpXSlanCBhiMz9lWuPAmId5tOXXWUOleXgTUGhCweCjSC 291BJi63gwJaP1wlbJHGr7S6hDcEvTCpkZfaOi/9rh2eM8XbEnP5ZNbZoL4R9mFrupED ppWWCj0ILx7Zt5fdw1wBP05h1AWeVkjKdUl3a4wGpL8SdscO8XCFGemFUMIvnfVxTLR2 FwtEJdUJi9lmsChbzwnQjWnk7sjW7XoRfEYmu7fxGZuRZD5WvJ3lp5zE/eXFkvgQ29Rg jCAIMY3g0qlo6FBYKIbNTi2Ec2l7uuzLZfQcrikR8d+j9Tj5gc9GqmPt+7VlGf4WqvN1 5pRg== 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=wp/ki8CoI7EpYvUbz7xKCBTg9ynrZMRYsRbyidj76rQ=; b=6aMCIRt5yGMiPUICs72IhKhl7D+fAncDZhW3Hvl8BYLVC9kL/3zT9NI8vkRvw5Owz/ znC8fUoSHEtBCrJp8b5yq+DHfJq4xKwsD1jKqFMBcWoQKQ1frujV6/NqbxzecnxKV8IU cxrpBOw4fS3HaOW9Zb68D9RiE5jpMnJomILq5/V/0mHrYR4SFOVn9zlshzjSIkRuyGwV tCSUUKg/AwY6hUhy5oXSAG8C21VRnTZuhL5f1i83kfd6CDWb4ArkzUA95+eQcWOIAfQO UTztuo0jvE8Sou4NIbHxtQCP36VQbEjTs7NYYvDCMqQ49r4Zs24f1/2vwEa8DrU8RWwv 918Q== X-Gm-Message-State: ANoB5pkDv1BRv7yC+q92r32gPr+OgqxiLRIFzI+zvKdpkOF/Zoaudyoy qqtwWB1SeU0M3IcKwkaILzs3DBu8aY4rMPTR X-Google-Smtp-Source: AA0mqf4U4GN6DLJory1fLLND67zvo1IOskTv2DKy8AMwlVxuIdRox0LcQdLOyBj4Bf9IcPmwjBpEQg== X-Received: by 2002:a05:6512:3115:b0:4b5:6950:bbb2 with SMTP id n21-20020a056512311500b004b56950bbb2mr3813647lfb.66.1670856419511; Mon, 12 Dec 2022 06:46:59 -0800 (PST) Return-Path: Received: from localhost.localdomain ([77.221.215.144]) by smtp.gmail.com with ESMTPSA id t4-20020a056512030400b0049c29292250sm1643313lfp.149.2022.12.12.06.46.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Dec 2022 06:46:59 -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 v2 01/11] Ext4Pkg: Fix memory leak in Ext4RetrieveDirent Date: Mon, 12 Dec 2022 20:46:44 +0600 Message-Id: <20221212144654.2650-2-savvamtr@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221212144654.2650-1-savvamtr@gmail.com> References: <20221212144654.2650-1-savvamtr@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable We need to free buffer on return if BlockRemainder !=3D 0. Also changed return logic from function to use use common exit to prevent code duplication. Cc: Marvin H=C3=A4user Cc: Pedro Falcato Cc: Vitaly Cheptsov Fixes: e55f0527dde48a5f139c1b8f35acc4e6b59dd794 Signed-off-by: Savva Mitrofanov --- Features/Ext4Pkg/Ext4Dxe/Directory.c | 30 +++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/Features/Ext4Pkg/Ext4Dxe/Directory.c b/Features/Ext4Pkg/Ext4Dx= e/Directory.c index 4441e6d192b6..8b8fce568e43 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Directory.c +++ b/Features/Ext4Pkg/Ext4Dxe/Directory.c @@ -112,8 +112,7 @@ Ext4RetrieveDirent ( UINTN ToCopy;=0D UINTN BlockOffset;=0D =0D - Status =3D EFI_NOT_FOUND;=0D - Buf =3D AllocatePool (Partition->BlockSize);=0D + Buf =3D AllocatePool (Partition->BlockSize);=0D =0D if (Buf =3D=3D NULL) {=0D return EFI_OUT_OF_RESOURCES;=0D @@ -127,7 +126,8 @@ Ext4RetrieveDirent ( DivU64x32Remainder (DirInoSize, Partition->BlockSize, &BlockRemainder);= =0D if (BlockRemainder !=3D 0) {=0D // Directory inodes need to have block aligned sizes=0D - return EFI_VOLUME_CORRUPTED;=0D + Status =3D EFI_VOLUME_CORRUPTED;=0D + goto Out;=0D }=0D =0D while (Off < DirInoSize) {=0D @@ -136,8 +136,7 @@ Ext4RetrieveDirent ( Status =3D Ext4Read (Partition, Directory, Buf, Off, &Length);=0D =0D if (Status !=3D EFI_SUCCESS) {=0D - FreePool (Buf);=0D - return Status;=0D + goto Out;=0D }=0D =0D for (BlockOffset =3D 0; BlockOffset < Partition->BlockSize; ) {=0D @@ -145,19 +144,19 @@ Ext4RetrieveDirent ( RemainingBlock =3D Partition->BlockSize - BlockOffset;=0D // Check if the minimum directory entry fits inside [BlockOffset, En= dOfBlock]=0D if (RemainingBlock < EXT4_MIN_DIR_ENTRY_LEN) {=0D - FreePool (Buf);=0D - return EFI_VOLUME_CORRUPTED;=0D + Status =3D EFI_VOLUME_CORRUPTED;=0D + goto Out;=0D }=0D =0D if (!Ext4ValidDirent (Entry)) {=0D - FreePool (Buf);=0D - return EFI_VOLUME_CORRUPTED;=0D + Status =3D EFI_VOLUME_CORRUPTED;=0D + goto Out;=0D }=0D =0D if ((Entry->name_len > RemainingBlock) || (Entry->rec_len > Remainin= gBlock)) {=0D // Corrupted filesystem=0D - FreePool (Buf);=0D - return EFI_VOLUME_CORRUPTED;=0D + Status =3D EFI_VOLUME_CORRUPTED;=0D + goto Out;=0D }=0D =0D // Unused entry=0D @@ -186,8 +185,8 @@ Ext4RetrieveDirent ( ToCopy =3D MIN (Entry->rec_len, sizeof (EXT4_DIR_ENTRY));=0D =0D CopyMem (Result, Entry, ToCopy);=0D - FreePool (Buf);=0D - return EFI_SUCCESS;=0D + Status =3D EFI_SUCCESS;=0D + goto Out;=0D }=0D =0D BlockOffset +=3D Entry->rec_len;=0D @@ -196,8 +195,11 @@ Ext4RetrieveDirent ( Off +=3D Partition->BlockSize;=0D }=0D =0D + Status =3D EFI_NOT_FOUND;=0D +=0D +Out:=0D FreePool (Buf);=0D - return EFI_NOT_FOUND;=0D + return Status;=0D }=0D =0D /**=0D --=20 2.38.1