From 06472685e239c4b2d013710378c34224d2b8586c Mon Sep 17 00:00:00 2001 From: TheApadayo Date: Tue, 21 Feb 2017 14:15:16 -0500 Subject: [PATCH] [Memory] Add non buggy IsBadReadPtr implementation --- src/Components/Modules/Console.cpp | 2 +- src/Components/Modules/D3D9Ex.cpp | 2 +- src/Steam/Proxy.cpp | 16 ++++++---------- src/Utils/Memory.cpp | 30 ++++++++++++++++++++++++++++++ src/Utils/Memory.hpp | 3 +++ 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/Components/Modules/Console.cpp b/src/Components/Modules/Console.cpp index 1c63c920..3a8a66f7 100644 --- a/src/Components/Modules/Console.cpp +++ b/src/Components/Modules/Console.cpp @@ -26,7 +26,7 @@ namespace Components char** Console::GetAutoCompleteFileList(const char *path, const char *extension, Game::FsListBehavior_e behavior, int *numfiles, int allocTrackType) { - if (path == reinterpret_cast(0xBAADF00D) || path == reinterpret_cast(0xCDCDCDCD) || IsBadReadPtr(path, 1)) return nullptr; + if (path == reinterpret_cast(0xBAADF00D) || path == reinterpret_cast(0xCDCDCDCD) || ::Utils::Memory::IsBadReadPtr(path)) return nullptr; return Game::FS_GetFileList(path, extension, behavior, numfiles, allocTrackType); } diff --git a/src/Components/Modules/D3D9Ex.cpp b/src/Components/Modules/D3D9Ex.cpp index bb49ce30..481b582b 100644 --- a/src/Components/Modules/D3D9Ex.cpp +++ b/src/Components/Modules/D3D9Ex.cpp @@ -569,7 +569,7 @@ namespace Components HRESULT D3D9Ex::D3D9Device::SetPixelShaderConstantF(UINT StartRegister, CONST float* pConstantData, UINT Vector4fCount) { - if (IsBadReadPtr(pConstantData, Vector4fCount * 16)) + if (::Utils::Memory::IsBadReadPtr(pConstantData/*, Vector4fCount * 16*/)) { //Logger::Print("Invalid shader constant array!\n"); return D3DERR_INVALIDCALL; diff --git a/src/Steam/Proxy.cpp b/src/Steam/Proxy.cpp index 58235c78..f2ace795 100644 --- a/src/Steam/Proxy.cpp +++ b/src/Steam/Proxy.cpp @@ -42,10 +42,10 @@ namespace Steam void* Interface::lookupMethod(std::string method) { - if (IsBadReadPtr(this->interfacePtr, 4)) return nullptr; + if (::Utils::Memory::IsBadReadPtr(this->interfacePtr)) return nullptr; unsigned char** vftbl = *static_cast(this->interfacePtr); - while (!IsBadReadPtr(vftbl, 4) && !IsBadCodePtr((FARPROC(*vftbl)))) + while (!::Utils::Memory::IsBadReadPtr(vftbl) && !::Utils::Memory::IsBadCodePtr((FARPROC(*vftbl)))) { if(this->getMethodName(*vftbl) == method) return *vftbl; ++vftbl; @@ -57,7 +57,7 @@ namespace Steam size_t Interface::getMethodParamSize(void* method) { unsigned char* methodPtr = static_cast(method); - for (; !IsBadReadPtr(methodPtr, 3); ++methodPtr) + for (; !::Utils::Memory::IsBadReadPtr(methodPtr); ++methodPtr) { if (methodPtr[0] == 0xC2 && methodPtr[2] == 0) // __stdcall return { @@ -71,12 +71,12 @@ namespace Steam std::string Interface::getMethodName(unsigned char* methodPtr) { - for(;!IsBadReadPtr(methodPtr, 3); ++methodPtr) + for(;!::Utils::Memory::IsBadReadPtr(methodPtr); ++methodPtr) { if(methodPtr[0] == 0x68) // Push { char* name = *reinterpret_cast(&methodPtr[1]); - if(!IsBadReadPtr(name, 1)) return name; + if(::Utils::Memory::IsBadReadPtr(name)) return name; } else if(methodPtr[0] == 0xC2 && methodPtr[2] == 0) // __stdcall return { @@ -322,11 +322,7 @@ namespace Steam void Proxy::StartSteamIfNecessary() { - if (Proxy::GetSteamDirectory().empty() -#ifndef DEBUG - || !Steam::Enabled() -#endif - ) return; + if (Proxy::GetSteamDirectory().empty() || !Steam::Enabled()) return; HKEY hRegKey; DWORD pid = 0; diff --git a/src/Utils/Memory.cpp b/src/Utils/Memory.cpp index 54192a9b..e3f3ac42 100644 --- a/src/Utils/Memory.cpp +++ b/src/Utils/Memory.cpp @@ -64,4 +64,34 @@ namespace Utils return true; } + + bool Memory::IsBadReadPtr(const void* ptr) + { + MEMORY_BASIC_INFORMATION mbi = { 0 }; + if (::VirtualQuery(ptr, &mbi, sizeof(mbi))) + { + DWORD mask = (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY); + bool b = !(mbi.Protect & mask); + // check the page is not a guard page + if (mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS)) b = true; + + return b; + } + return true; + } + + bool Memory::IsBadCodePtr(const void* ptr) + { + MEMORY_BASIC_INFORMATION mbi = { 0 }; + if (::VirtualQuery(ptr, &mbi, sizeof(mbi))) + { + DWORD mask = (PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY); + bool b = !(mbi.Protect & mask); + // check the page is not a guard page + if (mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS)) b = true; + + return b; + } + return true; + } } diff --git a/src/Utils/Memory.hpp b/src/Utils/Memory.hpp index 74505c18..acd5497d 100644 --- a/src/Utils/Memory.hpp +++ b/src/Utils/Memory.hpp @@ -130,5 +130,8 @@ namespace Utils static void FreeAlign(const void* data); static bool IsSet(void* mem, char chr, size_t length); + + static bool IsBadReadPtr(const void* ptr); + static bool IsBadCodePtr(const void* ptr); }; }