This patch revises GetPhdrByIndex and GetShdrByIndex to cause GenFw to exit with an error message when a section header lookup fails. The current behavior of those functions in such circumstances is to return NULL, which can cause GenFw to subsequently fault when it attempts to dereference the null pointer.
Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Michael LeMay <michael.le...@intel.com> Reviewed-by: Yonghong Zhu <yonghong....@intel.com> --- BaseTools/Source/C/GenFw/Elf32Convert.c | 10 +++++++--- BaseTools/Source/C/GenFw/Elf64Convert.c | 7 +++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/BaseTools/Source/C/GenFw/Elf32Convert.c b/BaseTools/Source/C/GenFw/Elf32Convert.c index eede645..dfd8bec 100644 --- a/BaseTools/Source/C/GenFw/Elf32Convert.c +++ b/BaseTools/Source/C/GenFw/Elf32Convert.c @@ -189,23 +189,27 @@ STATIC Elf_Shdr* GetShdrByIndex ( UINT32 Num ) { - if (Num >= mEhdr->e_shnum) - return NULL; + if (Num >= mEhdr->e_shnum) { + Error (NULL, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num); + exit(EXIT_FAILURE); + } + return (Elf_Shdr*)((UINT8*)mShdrBase + Num * mEhdr->e_shentsize); } STATIC Elf_Phdr* GetPhdrByIndex ( UINT32 num ) { if (num >= mEhdr->e_phnum) { - return NULL; + Error (NULL, 0, 3000, "Invalid", "GetPhdrByIndex: Index %u is too high.", num); + exit(EXIT_FAILURE); } return (Elf_Phdr *)((UINT8*)mPhdrBase + num * mEhdr->e_phentsize); } diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/GenFw/Elf64Convert.c index 90d80a2..3b55afa 100644 --- a/BaseTools/Source/C/GenFw/Elf64Convert.c +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c @@ -195,12 +195,15 @@ STATIC Elf_Shdr* GetShdrByIndex ( UINT32 Num ) { - if (Num >= mEhdr->e_shnum) - return NULL; + if (Num >= mEhdr->e_shnum) { + Error (NULL, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num); + exit(EXIT_FAILURE); + } + return (Elf_Shdr*)((UINT8*)mShdrBase + Num * mEhdr->e_shentsize); } STATIC UINT32 -- 2.6.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel