From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.120, mailfrom: zhiguang.liu@intel.com) Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by groups.io with SMTP; Thu, 09 May 2019 18:50:53 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 09 May 2019 18:50:52 -0700 X-ExtLoop1: 1 Received: from fieedk002.ccr.corp.intel.com ([10.239.157.133]) by fmsmga008.fm.intel.com with ESMTP; 09 May 2019 18:50:51 -0700 From: "Zhiguang Liu" To: devel@edk2.groups.io Cc: Bob Feng , Liming Gao Subject: [PATCH] BaseTools: Check the fread function and avoid dead loop Date: Fri, 10 May 2019 09:50:32 +0800 Message-Id: <20190510015032.2948-1-zhiguang.liu@intel.com> X-Mailer: git-send-email 2.21.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit REF:https://bugzilla.tianocore.org/show_bug.cgi?id=1789 If the input file is not a valid file, it may cause dead loop, because the return of fread function is not checked. Signed-off-by: Zhiguang Liu Cc: Bob Feng Cc: Liming Gao --- BaseTools/Source/C/VolInfo/VolInfo.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/BaseTools/Source/C/VolInfo/VolInfo.c b/BaseTools/Source/C/VolInfo/VolInfo.c index d8216c61e8..ffe0b47f03 100644 --- a/BaseTools/Source/C/VolInfo/VolInfo.c +++ b/BaseTools/Source/C/VolInfo/VolInfo.c @@ -751,6 +751,7 @@ Returns: UINTN Signature[2]; UINTN BytesRead; UINT32 Size; + size_t ReadSize; BytesRead = 0; Size = 0; @@ -764,7 +765,10 @@ Returns: // // Read the header // - fread (&VolumeHeader, sizeof (EFI_FIRMWARE_VOLUME_HEADER) - sizeof (EFI_FV_BLOCK_MAP_ENTRY), 1, InputFile); + ReadSize = fread (&VolumeHeader, sizeof (EFI_FIRMWARE_VOLUME_HEADER) - sizeof (EFI_FV_BLOCK_MAP_ENTRY), 1, InputFile); + if (ReadSize != 1) { + return EFI_ABORTED; + } BytesRead = sizeof (EFI_FIRMWARE_VOLUME_HEADER) - sizeof (EFI_FV_BLOCK_MAP_ENTRY); Signature[0] = VolumeHeader.Signature; Signature[1] = 0; @@ -1053,7 +1057,10 @@ Returns: printf ("Revision: 0x%04X\n", VolumeHeader.Revision); do { - fread (&BlockMap, sizeof (EFI_FV_BLOCK_MAP_ENTRY), 1, InputFile); + ReadSize = fread (&BlockMap, sizeof (EFI_FV_BLOCK_MAP_ENTRY), 1, InputFile); + if (ReadSize != 1) { + return EFI_ABORTED; + } BytesRead += sizeof (EFI_FV_BLOCK_MAP_ENTRY); if (BlockMap.NumBlocks != 0) { -- 2.21.0.windows.1