Skip to content

Instantly share code, notes, and snippets.

@rkr35
Forked from arbv/IsBadMemPtr.c
Last active February 23, 2025 07:04
Show Gist options
  • Save rkr35/79264c540856849a4cfa7f9edcbc493e to your computer and use it in GitHub Desktop.
Save rkr35/79264c540856849a4cfa7f9edcbc493e to your computer and use it in GitHub Desktop.
Re-wrote Boldarev's C-like version into C++. Description from Artem Boldarev's original Gist: "A safer replacement for the obsolete IsBadReadPtr() and IsBadWritePtr() WinAPI functions on top of VirtualQuery() which respects Windows guard pages and does not use SEH."
/*
Copyright (c) 2017 Artem Boldarev <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files(the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <windows.h>
/*
*https://gist.github.com/arbv/531040b8036050c5648fc55471d50352
A safer replacement for the obsolete IsBadReadPtr() and IsBadWritePtr() WinAPI functions
on top of VirtualQuery() which respects Windows guard pages. It does not use SEH
and is designed to be compatible with the above-mentioned functions.
The calls to the IsBadReadPtr() and IsBadWritePtr() can be replaced with the calls to
the is_bad_mem_ptr() as follows:
- IsBadReadPtr(...) => is_bad_mem_ptr(false, ...)
- IsBadWritePtr(...) => is_bad_mem_ptr(true, ...)
*/
template <typename T>
bool is_bad_mem_ptr(const bool write, const T* ptr, const std::size_t size)
{
const auto min_ptr = 0x10000;
const auto max_ptr = 0x000F000000000000;
if (ptr == nullptr || address(ptr) < min_ptr || address(ptr) >= max_ptr)
{
return true;
}
DWORD mask = PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY;
if (write)
{
mask = PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY;
}
auto current = address(ptr);
const auto last = current + size;
// So we are considering the region:
// [ptr, ptr+size)
while (current < last)
{
MEMORY_BASIC_INFORMATION mbi;
if (VirtualQuery(LPCVOID(current), &mbi, sizeof mbi) == 0)
{
// We couldn't get any information on this region.
// Let's not risk any read/write operation.
return true;
}
if ((mbi.Protect & mask) == 0)
{
// We can't perform our desired read/write operations in this region.
return true;
}
if (mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS))
{
// We can't access this region.
return true;
}
// Let's consider the next region.
current = address(mbi.BaseAddress) + mbi.RegionSize;
}
return false;
}
template <typename T>
bool is_bad_read_ptr(const T* ptr, const std::size_t size)
{
return is_bad_mem_ptr(false, ptr, size);
}
template <typename T>
bool is_bad_read_ptr(const T* ptr)
{
return is_bad_read_ptr(ptr, sizeof ptr);
}
template <typename T>
bool is_bad_write_ptr(const T* ptr, const std::size_t size)
{
return is_bad_mem_ptr(true, ptr, size);
}
template <typename T>
bool is_bad_write_ptr(const T* ptr)
{
return is_bad_write_ptr(ptr, sizeof ptr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment