From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) by mx.groups.io with SMTP id smtpd.web10.98477.1674811790268316894 for ; Fri, 27 Jan 2023 01:29:50 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20210112 header.b=i/cQVQ5E; spf=pass (domain: gmail.com, ip: 209.85.167.54, mailfrom: savvamtr@gmail.com) Received: by mail-lf1-f54.google.com with SMTP id x40so7177168lfu.12 for ; Fri, 27 Jan 2023 01:29:50 -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=7PM5Lvv6lbSAl/Yyp8eicwV5cnQ68A40VA6a0+1rs48=; b=i/cQVQ5EBHuDGyR4kGvAiX3HL1z5/YhGuI26j2piGRy8tQkJZxpibn50u7dSdzq061 xaiPCRenGidTbF/k1u0A64FeR3g07z/LMcqYAr5Mnsjt6TJ5rMDWqrsiFpo3vAox7JbR ZR/F/3wnZ2OxakkqvTMLrpO5L1yaUbg0im4eQ5i01HXi12OXyCu4kmUvQ2iBpTDJl9e4 MXzS2WAhzpPcdMVwRgFXS+nWS3guLMNeG3J8NWN8AORbmpQLfhvtPS9Qg52RhwQ+idKK 5XaisrYboJe3hlRy/HWbeKDFaPh9wTn3dEb7XtzVasI99fnhICVajyJ2V59LkOkg3Y43 qz4g== 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=7PM5Lvv6lbSAl/Yyp8eicwV5cnQ68A40VA6a0+1rs48=; b=CMkWYvVXdEV9jLIk76xLQPZ3iZ3Tsu1CVr07yNCtR++J2NuMnS+07lkddezU4d8NoI QHyWTNVc1HpbY0fW2WZ+RLFn9IFPBjEAG0W6loYnJXqp1rPDUfeIZDMOtaslcvglvGVf 3d13cdlCMNDxno2J6iDj4qCE9b3JQcwdgHez7uKWZJrGFJus2CQ9+SSJ/C4L3bmEnegB vjoOmTCnVvR48XEmGupYHbkfuLnFWEbp1jxedhDkRKL7afCyigmf10LMqzQspO9uC9DD 2md3Q4Pive6hGJYtGTgvSva+5O/LgUyO70neolUGB2j4vNEbgDYLGDUfV5DsRerNibA6 XCIg== X-Gm-Message-State: AFqh2kq5iWOxBCyH5TOwEcMngydWZN0aR2VVnKwK2JssbjT3SG2sflcJ +ZV9WU09gv+oU+KdInYPffKUyc0H10Dw7vRE X-Google-Smtp-Source: AMrXdXtI1QQF3rMri9EdRtCQGOfi7yKvQVLMHdG1o7LxGrcAFSEUnMhGZ6DBA5M8I3FbsgNfVHePng== X-Received: by 2002:ac2:4a7a:0:b0:4cb:880:581c with SMTP id q26-20020ac24a7a000000b004cb0880581cmr8398064lfp.65.1674811788286; Fri, 27 Jan 2023 01:29:48 -0800 (PST) Return-Path: Received: from localhost.localdomain ([176.62.67.29]) by smtp.gmail.com with ESMTPSA id t11-20020a056512068b00b004cc8196a308sm229071lfe.98.2023.01.27.01.29.47 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 27 Jan 2023 01:29:48 -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 v3 01/11] Ext4Pkg: Fix memory leak in Ext4RetrieveDirent Date: Fri, 27 Jan 2023 15:29:35 +0600 Message-Id: <20230127092945.94389-2-savvamtr@gmail.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230127092945.94389-1-savvamtr@gmail.com> References: <20230127092945.94389-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: d9ceedca6c8f ("Ext4Pkg: Add Ext4Dxe driver.") 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 456916453952..f80b1aacd459 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Directory.c +++ b/Features/Ext4Pkg/Ext4Dxe/Directory.c @@ -113,8 +113,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 @@ -128,7 +127,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 @@ -137,8 +137,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 @@ -146,19 +145,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 @@ -193,8 +192,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 @@ -203,8 +202,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.39.0