From 29bda2b678457618ddef33e4db827ba8340ef606 Mon Sep 17 00:00:00 2001 From: Michael LeMay Date: Fri, 29 Jan 2016 11:23:21 +0800 Subject: BaseTools/GenFw: Exit with error when header lookup fails 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 Reviewed-by: Yonghong Zhu (cherry picked from commit 17751c5fa473fd4f830007590d59e8d15a2d2935) --- BaseTools/Source/C/GenFw/Elf32Convert.c | 10 +++++++--- BaseTools/Source/C/GenFw/Elf64Convert.c | 7 +++++-- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'BaseTools/Source/C') diff --git a/BaseTools/Source/C/GenFw/Elf32Convert.c b/BaseTools/Source/C/GenFw/Elf32Convert.c index 9bf58555c0..dbdf05671b 100644 --- a/BaseTools/Source/C/GenFw/Elf32Convert.c +++ b/BaseTools/Source/C/GenFw/Elf32Convert.c @@ -191,8 +191,11 @@ 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); } @@ -203,7 +206,8 @@ GetPhdrByIndex ( ) { 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 fb85b3821b..974f3ca53a 100644 --- a/BaseTools/Source/C/GenFw/Elf64Convert.c +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c @@ -197,8 +197,11 @@ 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); } -- cgit v1.2.3