From 4afbf67ce18c5d6c654d73a042dc0f2f7547197b Mon Sep 17 00:00:00 2001 From: Scott Ehlert Date: Sat, 31 Jul 2010 16:06:16 -0500 Subject: [PATCH] Fixed potential crash with IMemUtils::FindPattern on Linux (bug 4554, r+a13=dvander). --- core/MemoryUtils.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/core/MemoryUtils.cpp b/core/MemoryUtils.cpp index 9cacaf35..0543608e 100644 --- a/core/MemoryUtils.cpp +++ b/core/MemoryUtils.cpp @@ -2,7 +2,7 @@ * vim: set ts=4 sw=4 tw=99 noet : * ============================================================================= * SourceMod - * Copyright (C) 2004-2009 AlliedModders LLC. All rights reserved. + * Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved. * ============================================================================= * * This program is free software; you can redistribute it and/or modify it under @@ -33,6 +33,9 @@ #include #include #include + +#define PAGE_SIZE 4096 +#define PAGE_ALIGN_UP(x) ((x + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) #endif MemoryUtils g_MemUtils; @@ -354,14 +357,22 @@ bool MemoryUtils::GetLibraryInfo(const void *libPtr, DynLibInfo &lib) phdrCount = file->e_phnum; phdr = reinterpret_cast(baseAddr + file->e_phoff); - /* Add up the memory sizes of segments marked as PT_LOAD as those are the only ones that should be in memory */ for (uint16_t i = 0; i < phdrCount; i++) { Elf32_Phdr &hdr = phdr[i]; - if (hdr.p_type == PT_LOAD) + /* We only really care about the segment with executable code */ + if (hdr.p_type == PT_LOAD && hdr.p_flags == (PF_X|PF_R)) { - lib.memorySize += hdr.p_memsz; + /* From glibc, elf/dl-load.c: + * c->mapend = ((ph->p_vaddr + ph->p_filesz + GLRO(dl_pagesize) - 1) + * & ~(GLRO(dl_pagesize) - 1)); + * + * In glibc, the segment file size is aligned up to the nearest page size and + * added to the virtual address of the segment. We just want the size here. + */ + lib.memorySize = PAGE_ALIGN_UP(hdr.p_filesz); + break; } }