-
-
Save OCTAGRAM/b8894b26b8620a4c059c39741d0130bf to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
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. | |
*) | |
uses | |
Winapi.Windows; | |
function IsBadMemPtr(Write: Boolean; Ptr: Pointer; Size: NativeUInt): Boolean; | |
var | |
mbi: MEMORY_BASIC_INFORMATION; | |
ok: Boolean; | |
mask: UInt32; | |
cursor, maxCursor, regionEnd: PByte; | |
begin | |
cursor := Ptr; | |
maxCursor := cursor + Size; | |
regionEnd := nil; | |
if Size = 0 then begin | |
Exit(False); | |
end {if}; | |
if not Assigned(cursor) then begin | |
Exit(True); | |
end {if}; | |
if not Write then begin | |
mask := PAGE_READONLY or PAGE_READWRITE or PAGE_WRITECOPY or | |
PAGE_EXECUTE_READ or PAGE_EXECUTE_READWRITE or PAGE_EXECUTE_WRITECOPY; | |
end | |
else begin | |
mask := PAGE_READWRITE or PAGE_WRITECOPY or PAGE_EXECUTE_READWRITE or | |
PAGE_EXECUTE_WRITECOPY; | |
end {if}; | |
repeat | |
if (cursor = ptr) or (cursor = regionEnd) then begin | |
if VirtualQuery(cursor, mbi, SizeOf(mbi)) = 0 then begin | |
Exit(True); | |
end | |
else begin | |
regionEnd := PByte(mbi.BaseAddress) + mbi.RegionSize; | |
end {if}; | |
end {if}; | |
ok := (mbi.Protect and mask) <> 0; | |
if (mbi.Protect and (PAGE_GUARD or PAGE_NOACCESS)) <> 0 then begin | |
ok := False; | |
end {if}; | |
if not ok then begin | |
Exit(True); | |
end {if}; | |
if maxCursor <= regionEnd then begin | |
(* the whole address range is inside the current memory region *) | |
Exit(False); | |
end | |
else if maxCursor > regionEnd then begin | |
(* this region is a part of (or overlaps with) the address range we are checking *) | |
cursor := regionEnd; (* lets move to the next memory region *) | |
end {if}; | |
until cursor >= maxCursor; | |
Exit(False); | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment