BaseTools: Fix Section header size larger than elf file size bug

Add the logic to handle the case that Section header size larger than
elf file size.

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
This commit is contained in:
Yunhua Feng 2018-06-01 17:21:02 +08:00 committed by Yonghong Zhu
parent 3f02180031
commit d78675d195
4 changed files with 24 additions and 5 deletions

View File

@ -674,6 +674,9 @@ WriteSections32 (
switch (Shdr->sh_type) { switch (Shdr->sh_type) {
case SHT_PROGBITS: case SHT_PROGBITS:
/* Copy. */ /* Copy. */
if (Shdr->sh_offset + Shdr->sh_size > mFileBufferSize) {
return FALSE;
}
memcpy(mCoffFile + mCoffSectionsOffset[Idx], memcpy(mCoffFile + mCoffSectionsOffset[Idx],
(UINT8*)mEhdr + Shdr->sh_offset, (UINT8*)mEhdr + Shdr->sh_offset,
Shdr->sh_size); Shdr->sh_size);

View File

@ -670,6 +670,9 @@ WriteSections64 (
switch (Shdr->sh_type) { switch (Shdr->sh_type) {
case SHT_PROGBITS: case SHT_PROGBITS:
/* Copy. */ /* Copy. */
if (Shdr->sh_offset + Shdr->sh_size > mFileBufferSize) {
return FALSE;
}
memcpy(mCoffFile + mCoffSectionsOffset[Idx], memcpy(mCoffFile + mCoffSectionsOffset[Idx],
(UINT8*)mEhdr + Shdr->sh_offset, (UINT8*)mEhdr + Shdr->sh_offset,
(size_t) Shdr->sh_size); (size_t) Shdr->sh_size);

View File

@ -1,7 +1,7 @@
/** @file /** @file
Elf convert solution Elf convert solution
Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR> Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available This program and the accompanying materials are licensed and made available
under the terms and conditions of the BSD License which accompanies this under the terms and conditions of the BSD License which accompanies this
@ -57,6 +57,11 @@ UINT32 mCoffOffset;
// //
UINT32 mTableOffset; UINT32 mTableOffset;
//
//mFileBufferSize
//
UINT32 mFileBufferSize;
// //
//***************************************************************************** //*****************************************************************************
// Common ELF Functions // Common ELF Functions
@ -173,6 +178,7 @@ ConvertElf (
ELF_FUNCTION_TABLE ElfFunctions; ELF_FUNCTION_TABLE ElfFunctions;
UINT8 EiClass; UINT8 EiClass;
mFileBufferSize = *FileLength;
// //
// Determine ELF type and set function table pointer correctly. // Determine ELF type and set function table pointer correctly.
// //
@ -201,9 +207,15 @@ ConvertElf (
// Write and relocate sections. // Write and relocate sections.
// //
VerboseMsg ("Write and relocate sections."); VerboseMsg ("Write and relocate sections.");
ElfFunctions.WriteSections (SECTION_TEXT); if (!ElfFunctions.WriteSections (SECTION_TEXT)) {
ElfFunctions.WriteSections (SECTION_DATA); return FALSE;
ElfFunctions.WriteSections (SECTION_HII); }
if (!ElfFunctions.WriteSections (SECTION_DATA)) {
return FALSE;
}
if (!ElfFunctions.WriteSections (SECTION_HII)) {
return FALSE;
}
// //
// Translate and write relocations. // Translate and write relocations.

View File

@ -1,7 +1,7 @@
/** @file /** @file
Header file for Elf convert solution Header file for Elf convert solution
Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR> Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available This program and the accompanying materials are licensed and made available
under the terms and conditions of the BSD License which accompanies this under the terms and conditions of the BSD License which accompanies this
@ -29,6 +29,7 @@ extern UINT32 mImageTimeStamp;
extern UINT8 *mCoffFile; extern UINT8 *mCoffFile;
extern UINT32 mTableOffset; extern UINT32 mTableOffset;
extern UINT32 mOutImageType; extern UINT32 mOutImageType;
extern UINT32 mFileBufferSize;
// //
// Common EFI specific data. // Common EFI specific data.