From 1ce48bf0400a708e94293e598103d810457cf1a8 Mon Sep 17 00:00:00 2001 From: Download-Fritz Date: Fri, 19 Oct 2018 14:10:46 +0200 Subject: [PATCH] OcMachoLib: Verify FileSize can handle at least the header and the minimal LC size. --- Library/OcMachoLib/Header.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Library/OcMachoLib/Header.c b/Library/OcMachoLib/Header.c index 8582e223..df88bd76 100644 --- a/Library/OcMachoLib/Header.c +++ b/Library/OcMachoLib/Header.c @@ -37,6 +37,7 @@ MachoInitializeContext ( OUT OC_MACHO_CONTEXT *Context ) { + UINTN MinCommandsSize; UINTN TopOfCommands; UINTN Index; CONST MACH_LOAD_COMMAND *Command; @@ -44,8 +45,11 @@ MachoInitializeContext ( // // Verify MACH-O Header sanity. // - TopOfCommands = ((UINTN)MachHeader->Commands + MachHeader->CommandsSize); - if ((MachHeader->Signature != MACH_HEADER_64_SIGNATURE) + TopOfCommands = ((UINTN)MachHeader->Commands + MachHeader->CommandsSize); + MinCommandsSize = (MachHeader->NumberOfCommands * sizeof (*MachHeader->Commands)); + if ((FileSize < sizeof (*MachHeader)) + || (MachHeader->Signature != MACH_HEADER_64_SIGNATURE) + || (MachHeader->CommandsSize < MinCommandsSize) || (TopOfCommands > ((UINTN)MachHeader + FileSize))) { return FALSE; }